Skip to content

Commit

Permalink
Merge pull request #78267 from mucahitkurt/cleanup/operation-generato…
Browse files Browse the repository at this point in the history
…r-migration-scenarios-unit-tests

unit tests for operationGenerator.GenerateUnmapVolumeFunc
  • Loading branch information
k8s-ci-robot committed Jul 10, 2019
2 parents 08a36f6 + db1c077 commit d59a603
Show file tree
Hide file tree
Showing 11 changed files with 395 additions and 10 deletions.
2 changes: 2 additions & 0 deletions pkg/controller/.import-restrictions
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
"github.com/robfig/cron",
"github.com/spf13/pflag",
"github.com/stretchr/testify/assert",
"github.com/stretchr/testify/mock",
"github.com/stretchr/testify/require",
"github.com/google/gofuzz",
"github.com/golang/protobuf/ptypes/wrappers",
Expand Down Expand Up @@ -162,6 +163,7 @@
"k8s.io/client-go/util/flowcontrol",
"k8s.io/client-go/util/retry",
"k8s.io/client-go/util/workqueue",
"k8s.io/client-go/util/testing",
"k8s.io/client-go/transport"
]
},
Expand Down
1 change: 1 addition & 0 deletions pkg/volume/csi/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ filegroup(
"//pkg/volume/csi/csiv0:all-srcs",
"//pkg/volume/csi/fake:all-srcs",
"//pkg/volume/csi/nodeinfomanager:all-srcs",
"//pkg/volume/csi/testing:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
Expand Down
4 changes: 2 additions & 2 deletions pkg/volume/csi/csi_attacher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1447,7 +1447,7 @@ func newTestWatchPlugin(t *testing.T, fakeClient *fakeclient.Clientset) (*csiPlu
fakeClient.Fake.PrependWatchReactor("volumeattachments", core.DefaultWatchReactor(fakeWatcher, nil))

// Start informer for CSIDrivers.
factory := informers.NewSharedInformerFactory(fakeClient, csiResyncPeriod)
factory := informers.NewSharedInformerFactory(fakeClient, CsiResyncPeriod)
csiDriverInformer := factory.Storage().V1beta1().CSIDrivers()
csiDriverLister := csiDriverInformer.Lister()
factory.Start(wait.NeverStop)
Expand All @@ -1474,7 +1474,7 @@ func newTestWatchPlugin(t *testing.T, fakeClient *fakeclient.Clientset) (*csiPlu

if utilfeature.DefaultFeatureGate.Enabled(features.CSIDriverRegistry) {
// Wait until the informer in CSI volume plugin has all CSIDrivers.
wait.PollImmediate(testInformerSyncPeriod, testInformerSyncTimeout, func() (bool, error) {
wait.PollImmediate(TestInformerSyncPeriod, TestInformerSyncTimeout, func() (bool, error) {
return csiDriverInformer.Informer().HasSynced(), nil
})
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/volume/csi/csi_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ const (
volDataFileName = "vol_data.json"
fsTypeBlockName = "block"

// CsiResyncPeriod is default resync period duration
// TODO: increase to something useful
csiResyncPeriod = time.Minute
CsiResyncPeriod = time.Minute
)

var deprecatedSocketDirVersions = []string{"0.1.0", "0.2.0", "0.3.0", "0.4.0"}
Expand Down
6 changes: 3 additions & 3 deletions pkg/volume/csi/csi_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func newTestPlugin(t *testing.T, client *fakeclient.Clientset) (*csiPlugin, stri
}

// Start informer for CSIDrivers.
factory := informers.NewSharedInformerFactory(client, csiResyncPeriod)
factory := informers.NewSharedInformerFactory(client, CsiResyncPeriod)
csiDriverInformer := factory.Storage().V1beta1().CSIDrivers()
csiDriverLister := csiDriverInformer.Lister()
go factory.Start(wait.NeverStop)
Expand All @@ -77,7 +77,7 @@ func newTestPlugin(t *testing.T, client *fakeclient.Clientset) (*csiPlugin, stri

if utilfeature.DefaultFeatureGate.Enabled(features.CSIDriverRegistry) {
// Wait until the informer in CSI volume plugin has all CSIDrivers.
wait.PollImmediate(testInformerSyncPeriod, testInformerSyncTimeout, func() (bool, error) {
wait.PollImmediate(TestInformerSyncPeriod, TestInformerSyncTimeout, func() (bool, error) {
return csiDriverInformer.Informer().HasSynced(), nil
})
}
Expand Down Expand Up @@ -935,7 +935,7 @@ func TestPluginFindAttachablePlugin(t *testing.T) {
defer os.RemoveAll(tmpDir)

client := fakeclient.NewSimpleClientset(getTestCSIDriver(test.driverName, nil, &test.canAttach))
factory := informers.NewSharedInformerFactory(client, csiResyncPeriod)
factory := informers.NewSharedInformerFactory(client, CsiResyncPeriod)
host := volumetest.NewFakeVolumeHostWithCSINodeName(
tmpDir,
client,
Expand Down
2 changes: 1 addition & 1 deletion pkg/volume/csi/csi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestCSI_VolumeAll(t *testing.T) {
client := fakeclient.NewSimpleClientset()
fakeWatcher := watch.NewRaceFreeFake()

factory := informers.NewSharedInformerFactory(client, csiResyncPeriod)
factory := informers.NewSharedInformerFactory(client, CsiResyncPeriod)
factory.Start(wait.NeverStop)

host := volumetest.NewFakeVolumeHostWithCSINodeName(
Expand Down
6 changes: 4 additions & 2 deletions pkg/volume/csi/csi_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ import (
)

const (
testInformerSyncPeriod = 100 * time.Millisecond
testInformerSyncTimeout = 30 * time.Second
// TestInformerSyncPeriod is informer sync period duration for testing
TestInformerSyncPeriod = 100 * time.Millisecond
// TestInformerSyncTimeout is informer timeout duration for testing
TestInformerSyncTimeout = 30 * time.Second
)

func getCredentialsFromSecret(k8s kubernetes.Interface, secretRef *api.SecretReference) (map[string]string, error) {
Expand Down
33 changes: 33 additions & 0 deletions pkg/volume/csi/testing/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["testing.go"],
importpath = "k8s.io/kubernetes/pkg/volume/csi/testing",
visibility = ["//visibility:public"],
deps = [
"//pkg/features:go_default_library",
"//pkg/volume:go_default_library",
"//pkg/volume/csi:go_default_library",
"//pkg/volume/testing: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",
"//staging/src/k8s.io/client-go/kubernetes/fake:go_default_library",
"//staging/src/k8s.io/client-go/util/testing:go_default_library",
],
)

filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)

filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
72 changes: 72 additions & 0 deletions pkg/volume/csi/testing/testing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package testing

import (
"k8s.io/apimachinery/pkg/util/wait"
utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/client-go/informers"
fakeclient "k8s.io/client-go/kubernetes/fake"
utiltesting "k8s.io/client-go/util/testing"
"k8s.io/kubernetes/pkg/features"
"k8s.io/kubernetes/pkg/volume"
"k8s.io/kubernetes/pkg/volume/csi"
volumetest "k8s.io/kubernetes/pkg/volume/testing"
"testing"
)

// NewTestPlugin creates a plugin mgr to load plugins and setup a fake client
func NewTestPlugin(t *testing.T, client *fakeclient.Clientset) (*volume.VolumePluginMgr, *volume.VolumePlugin, string) {
tmpDir, err := utiltesting.MkTmpdir("csi-test")
if err != nil {
t.Fatalf("can't create temp dir: %v", err)
}

if client == nil {
client = fakeclient.NewSimpleClientset()
}

// Start informer for CSIDrivers.
factory := informers.NewSharedInformerFactory(client, csi.CsiResyncPeriod)
csiDriverInformer := factory.Storage().V1beta1().CSIDrivers()
csiDriverLister := csiDriverInformer.Lister()
go factory.Start(wait.NeverStop)

host := volumetest.NewFakeVolumeHostWithCSINodeName(
tmpDir,
client,
nil,
"fakeNode",
csiDriverLister,
)
plugMgr := &volume.VolumePluginMgr{}
plugMgr.InitPlugins(csi.ProbeVolumePlugins(), nil /* prober */, host)

plug, err := plugMgr.FindPluginByName(csi.CSIPluginName)
if err != nil {
t.Fatalf("can't find plugin %v", csi.CSIPluginName)
}

if utilfeature.DefaultFeatureGate.Enabled(features.CSIDriverRegistry) {
// Wait until the informer in CSI volume plugin has all CSIDrivers.
wait.PollImmediate(csi.TestInformerSyncPeriod, csi.TestInformerSyncTimeout, func() (bool, error) {
return csiDriverInformer.Informer().HasSynced(), nil
})
}

return plugMgr, &plug, tmpDir
}
20 changes: 19 additions & 1 deletion pkg/volume/util/operationexecutor/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,34 @@ go_library(

go_test(
name = "go_default_test",
srcs = ["operation_executor_test.go"],
srcs = [
"operation_executor_test.go",
"operation_generator_test.go",
],
embed = [":go_default_library"],
deps = [
"//pkg/features:go_default_library",
"//pkg/util/mount:go_default_library",
"//pkg/volume:go_default_library",
"//pkg/volume/awsebs:go_default_library",
"//pkg/volume/csi:go_default_library",
"//pkg/volume/csi/testing:go_default_library",
"//pkg/volume/gcepd:go_default_library",
"//pkg/volume/testing:go_default_library",
"//pkg/volume/util/types: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/types:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/uuid:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/util/feature:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes/fake:go_default_library",
"//staging/src/k8s.io/client-go/tools/record:go_default_library",
"//staging/src/k8s.io/component-base/featuregate:go_default_library",
"//staging/src/k8s.io/component-base/featuregate/testing:go_default_library",
"//staging/src/k8s.io/csi-translation-lib/plugins:go_default_library",
"//vendor/github.com/prometheus/client_golang/prometheus:go_default_library",
"//vendor/github.com/prometheus/client_model/go:go_default_library",
"//vendor/github.com/stretchr/testify/assert:go_default_library",
],
)

Expand Down

0 comments on commit d59a603

Please sign in to comment.