Skip to content

Commit

Permalink
De-race some CSI unit tests that were initializing the plugin manager…
Browse files Browse the repository at this point in the history
……ger (and plugins) twice. Set some const variables earlier to support node info manager initialization and wait for initialization to complete before finishing plugin setup.
  • Loading branch information
davidz627 committed Nov 15, 2019
1 parent e64a4bc commit 1a47bf5
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 108 deletions.
54 changes: 33 additions & 21 deletions pkg/volume/csi/csi_attacher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ import (
"testing"
"time"

v1 "k8s.io/api/core/v1"
storage "k8s.io/api/storage/v1"
apierrs "k8s.io/apimachinery/pkg/api/errors"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -371,7 +373,7 @@ func TestAttacherWithCSIDriver(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
go func(volSpec *volume.Spec, expectAttach bool) {
attachID, err := csiAttacher.Attach(volSpec, types.NodeName("node"))
attachID, err := csiAttacher.Attach(volSpec, types.NodeName("fakeNode"))
defer wg.Done()

if err != nil {
Expand All @@ -383,7 +385,7 @@ func TestAttacherWithCSIDriver(t *testing.T) {
}(spec, test.expectVolumeAttachment)

if test.expectVolumeAttachment {
expectedAttachID := getAttachmentName("test-vol", test.driver, "node")
expectedAttachID := getAttachmentName("test-vol", test.driver, "fakeNode")
status := storage.VolumeAttachmentStatus{
Attached: true,
}
Expand Down Expand Up @@ -433,6 +435,12 @@ func TestAttacherWaitForVolumeAttachmentWithCSIDriver(t *testing.T) {
getTestCSIDriver("not-attachable", nil, &bFalse, nil),
getTestCSIDriver("attachable", nil, &bTrue, nil),
getTestCSIDriver("nil", nil, nil, nil),
&v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "fakeNode",
},
Spec: v1.NodeSpec{},
},
)
plug, tmpDir := newTestPlugin(t, fakeClient)
defer os.RemoveAll(tmpDir)
Expand Down Expand Up @@ -478,21 +486,21 @@ func TestAttacherWaitForAttach(t *testing.T) {
driver: "attachable",
makeAttachment: func() *storage.VolumeAttachment {

testAttachID := getAttachmentName("test-vol", "attachable", "node")
successfulAttachment := makeTestAttachment(testAttachID, "node", "test-pv")
testAttachID := getAttachmentName("test-vol", "attachable", "fakeNode")
successfulAttachment := makeTestAttachment(testAttachID, "fakeNode", "test-pv")
successfulAttachment.Status.Attached = true
return successfulAttachment
},
spec: volume.NewSpecFromPersistentVolume(makeTestPV("test-pv", 10, "attachable", "test-vol"), false),
expectedAttachID: getAttachmentName("test-vol", "attachable", "node"),
expectedAttachID: getAttachmentName("test-vol", "attachable", "fakeNode"),
expectError: false,
},
{
name: "failed attach with vol source",
makeAttachment: func() *storage.VolumeAttachment {

testAttachID := getAttachmentName("test-vol", "attachable", "node")
successfulAttachment := makeTestAttachment(testAttachID, "node", "volSrc01")
testAttachID := getAttachmentName("test-vol", "attachable", "fakeNode")
successfulAttachment := makeTestAttachment(testAttachID, "fakeNode", "volSrc01")
successfulAttachment.Status.Attached = true
return successfulAttachment
},
Expand Down Expand Up @@ -559,21 +567,21 @@ func TestAttacherWaitForAttachWithInline(t *testing.T) {
name: "successful attach with PV",
makeAttachment: func() *storage.VolumeAttachment {

testAttachID := getAttachmentName("test-vol", "attachable", "node")
successfulAttachment := makeTestAttachment(testAttachID, "node", "test-pv")
testAttachID := getAttachmentName("test-vol", "attachable", "fakeNode")
successfulAttachment := makeTestAttachment(testAttachID, "fakeNode", "test-pv")
successfulAttachment.Status.Attached = true
return successfulAttachment
},
spec: volume.NewSpecFromPersistentVolume(makeTestPV("test-pv", 10, "attachable", "test-vol"), false),
expectedAttachID: getAttachmentName("test-vol", "attachable", "node"),
expectedAttachID: getAttachmentName("test-vol", "attachable", "fakeNode"),
expectError: false,
},
{
name: "failed attach with volSrc",
makeAttachment: func() *storage.VolumeAttachment {

testAttachID := getAttachmentName("test-vol", "attachable", "node")
successfulAttachment := makeTestAttachment(testAttachID, "node", "volSrc01")
testAttachID := getAttachmentName("test-vol", "attachable", "fakeNode")
successfulAttachment := makeTestAttachment(testAttachID, "fakeNode", "volSrc01")
successfulAttachment.Status.Attached = true
return successfulAttachment
},
Expand Down Expand Up @@ -625,7 +633,7 @@ func TestAttacherWaitForAttachWithInline(t *testing.T) {
}

func TestAttacherWaitForVolumeAttachment(t *testing.T) {
nodeName := "test-node"
nodeName := "fakeNode"
testCases := []struct {
name string
initAttached bool
Expand Down Expand Up @@ -781,7 +789,7 @@ func TestAttacherVolumesAreAttached(t *testing.T) {
t.Fatalf("failed to create new attacher: %v", err)
}
csiAttacher := attacher.(*csiAttacher)
nodeName := "test-node"
nodeName := "fakeNode"

var specs []*volume.Spec
// create and save volume attchments
Expand Down Expand Up @@ -852,7 +860,7 @@ func TestAttacherVolumesAreAttachedWithInline(t *testing.T) {
t.Fatalf("failed to create new attacher: %v", err)
}
csiAttacher := attacher.(*csiAttacher)
nodeName := "test-node"
nodeName := "fakeNode"

var specs []*volume.Spec
// create and save volume attchments
Expand Down Expand Up @@ -891,8 +899,7 @@ func TestAttacherVolumesAreAttachedWithInline(t *testing.T) {
}

func TestAttacherDetach(t *testing.T) {

nodeName := "test-node"
nodeName := "fakeNode"
testCases := []struct {
name string
volID string
Expand Down Expand Up @@ -1492,6 +1499,12 @@ func newTestWatchPlugin(t *testing.T, fakeClient *fakeclient.Clientset) (*csiPlu
if fakeClient == nil {
fakeClient = fakeclient.NewSimpleClientset()
}
fakeClient.Tracker().Add(&v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "fakeNode",
},
Spec: v1.NodeSpec{},
})
fakeWatcher := watch.NewRaceFreeFake()
fakeClient.Fake.PrependWatchReactor("volumeattachments", core.DefaultWatchReactor(fakeWatcher, nil))

Expand All @@ -1504,12 +1517,11 @@ func newTestWatchPlugin(t *testing.T, fakeClient *fakeclient.Clientset) (*csiPlu
host := volumetest.NewFakeVolumeHostWithCSINodeName(
tmpDir,
fakeClient,
nil,
"node",
ProbeVolumePlugins(),
"fakeNode",
csiDriverLister,
)
plugMgr := &volume.VolumePluginMgr{}
plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, host)
plugMgr := host.GetPluginMgr()

plug, err := plugMgr.FindPluginByName(CSIPluginName)
if err != nil {
Expand Down
36 changes: 0 additions & 36 deletions pkg/volume/csi/csi_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
featuregatetesting "k8s.io/component-base/featuregate/testing"
"k8s.io/kubernetes/pkg/features"
"k8s.io/kubernetes/pkg/volume"
volumetest "k8s.io/kubernetes/pkg/volume/testing"
)

func prepareBlockMapperTest(plug *csiPlugin, specVolumeName string, t *testing.T) (*csiBlockMapper, *volume.Spec, *api.PersistentVolume, error) {
Expand Down Expand Up @@ -244,15 +243,6 @@ func TestBlockMapperSetupDevice(t *testing.T) {

plug, tmpDir := newTestPlugin(t, nil)
defer os.RemoveAll(tmpDir)
fakeClient := fakeclient.NewSimpleClientset()
host := volumetest.NewFakeVolumeHostWithCSINodeName(
tmpDir,
fakeClient,
nil,
"fakeNode",
nil,
)
plug.host = host

csiMapper, _, pv, err := prepareBlockMapperTest(plug, "test-pv", t)
if err != nil {
Expand Down Expand Up @@ -295,15 +285,6 @@ func TestBlockMapperMapPodDevice(t *testing.T) {

plug, tmpDir := newTestPlugin(t, nil)
defer os.RemoveAll(tmpDir)
fakeClient := fakeclient.NewSimpleClientset()
host := volumetest.NewFakeVolumeHostWithCSINodeName(
tmpDir,
fakeClient,
nil,
"fakeNode",
nil,
)
plug.host = host

csiMapper, _, pv, err := prepareBlockMapperTest(plug, "test-pv", t)
if err != nil {
Expand Down Expand Up @@ -371,14 +352,6 @@ func TestBlockMapperMapPodDeviceNotSupportAttach(t *testing.T) {
plug, tmpDir := newTestPlugin(t, fakeClient)
defer os.RemoveAll(tmpDir)

host := volumetest.NewFakeVolumeHostWithCSINodeName(
tmpDir,
fakeClient,
nil,
"fakeNode",
plug.csiDriverLister,
)
plug.host = host
csiMapper, _, _, err := prepareBlockMapperTest(plug, "test-pv", t)
if err != nil {
t.Fatalf("Failed to make a new Mapper: %v", err)
Expand All @@ -401,15 +374,6 @@ func TestBlockMapperTearDownDevice(t *testing.T) {

plug, tmpDir := newTestPlugin(t, nil)
defer os.RemoveAll(tmpDir)
fakeClient := fakeclient.NewSimpleClientset()
host := volumetest.NewFakeVolumeHostWithCSINodeName(
tmpDir,
fakeClient,
nil,
"fakeNode",
nil,
)
plug.host = host

_, spec, pv, err := prepareBlockMapperTest(plug, "test-pv", t)
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions pkg/volume/csi/csi_mounter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
utilfeature "k8s.io/apiserver/pkg/util/feature"
fakeclient "k8s.io/client-go/kubernetes/fake"
featuregatetesting "k8s.io/component-base/featuregate/testing"
"k8s.io/klog"
"k8s.io/kubernetes/pkg/features"
"k8s.io/kubernetes/pkg/volume"
"k8s.io/kubernetes/pkg/volume/util"
Expand Down Expand Up @@ -151,7 +150,6 @@ func MounterSetUpTests(t *testing.T, podInfoEnabled bool) {
currentPodInfoMount := true
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
klog.Infof("Starting test %s", test.name)
// Modes must be set if (and only if) CSIInlineVolume is enabled.
var modes []storagev1beta1.VolumeLifecycleMode
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIInlineVolume, test.csiInlineVolume)()
Expand Down
45 changes: 33 additions & 12 deletions pkg/volume/csi/csi_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ import (
"testing"

api "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
storagev1beta1 "k8s.io/api/storage/v1beta1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
utilfeature "k8s.io/apiserver/pkg/util/feature"
Expand All @@ -50,6 +52,13 @@ func newTestPlugin(t *testing.T, client *fakeclient.Clientset) (*csiPlugin, stri
client = fakeclient.NewSimpleClientset()
}

client.Tracker().Add(&v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "fakeNode",
},
Spec: v1.NodeSpec{},
})

// Start informer for CSIDrivers.
factory := informers.NewSharedInformerFactory(client, CsiResyncPeriod)
csiDriverInformer := factory.Storage().V1beta1().CSIDrivers()
Expand All @@ -59,14 +68,13 @@ func newTestPlugin(t *testing.T, client *fakeclient.Clientset) (*csiPlugin, stri
host := volumetest.NewFakeVolumeHostWithCSINodeName(
tmpDir,
client,
nil,
ProbeVolumePlugins(),
"fakeNode",
csiDriverLister,
)
plugMgr := &volume.VolumePluginMgr{}
plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, host)

plug, err := plugMgr.FindPluginByName(CSIPluginName)
pluginMgr := host.GetPluginMgr()
plug, err := pluginMgr.FindPluginByName(CSIPluginName)
if err != nil {
t.Fatalf("can't find plugin %v", CSIPluginName)
}
Expand Down Expand Up @@ -998,18 +1006,25 @@ func TestPluginFindAttachablePlugin(t *testing.T) {
}
defer os.RemoveAll(tmpDir)

client := fakeclient.NewSimpleClientset(getTestCSIDriver(test.driverName, nil, &test.canAttach, nil))
client := fakeclient.NewSimpleClientset(
getTestCSIDriver(test.driverName, nil, &test.canAttach, nil),
&v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "fakeNode",
},
Spec: v1.NodeSpec{},
},
)
factory := informers.NewSharedInformerFactory(client, CsiResyncPeriod)
host := volumetest.NewFakeVolumeHostWithCSINodeName(
tmpDir,
client,
nil,
ProbeVolumePlugins(),
"fakeNode",
factory.Storage().V1beta1().CSIDrivers().Lister(),
)

plugMgr := &volume.VolumePluginMgr{}
plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, host)
plugMgr := host.GetPluginMgr()

plugin, err := plugMgr.FindAttachablePluginBySpec(test.spec)
if err != nil && !test.shouldFail {
Expand Down Expand Up @@ -1118,10 +1133,16 @@ func TestPluginFindDeviceMountablePluginBySpec(t *testing.T) {
}
defer os.RemoveAll(tmpDir)

client := fakeclient.NewSimpleClientset()
host := volumetest.NewFakeVolumeHost(tmpDir, client, nil)
plugMgr := &volume.VolumePluginMgr{}
plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, host)
client := fakeclient.NewSimpleClientset(
&v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "fakeNode",
},
Spec: v1.NodeSpec{},
},
)
host := volumetest.NewFakeVolumeHostWithCSINodeName(tmpDir, client, ProbeVolumePlugins(), "fakeNode", nil)
plugMgr := host.GetPluginMgr()

plug, err := plugMgr.FindDeviceMountablePluginBySpec(test.spec)
if err != nil && !test.shouldFail {
Expand Down
15 changes: 10 additions & 5 deletions pkg/volume/csi/csi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"time"

api "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
storage "k8s.io/api/storage/v1"
storagebeta1 "k8s.io/api/storage/v1beta1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -239,6 +240,12 @@ func TestCSI_VolumeAll(t *testing.T) {
}
objs = append(objs, driverInfo)
}
objs = append(objs, &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "fakeNode",
},
Spec: v1.NodeSpec{},
})

client := fakeclient.NewSimpleClientset(objs...)
fakeWatcher := watch.NewRaceFreeFake()
Expand All @@ -253,13 +260,11 @@ func TestCSI_VolumeAll(t *testing.T) {
host := volumetest.NewFakeVolumeHostWithCSINodeName(
tmpDir,
client,
nil,
"csi-node",
ProbeVolumePlugins(),
"fakeNode",
csiDriverInformer.Lister(),
)

plugMgr := &volume.VolumePluginMgr{}
plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, host)
plugMgr := host.GetPluginMgr()
csiClient := setupClient(t, true)

volSpec := test.specFunc(test.specName, test.driver, test.volName)
Expand Down
2 changes: 2 additions & 0 deletions pkg/volume/csi/testing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ go_library(
"//pkg/volume:go_default_library",
"//pkg/volume/csi:go_default_library",
"//pkg/volume/testing:go_default_library",
"//staging/src/k8s.io/api/core/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/util/feature:go_default_library",
"//staging/src/k8s.io/client-go/informers:go_default_library",
Expand Down
Loading

0 comments on commit 1a47bf5

Please sign in to comment.