From 466c4d24a922104acd687e536ad5be90574d0c31 Mon Sep 17 00:00:00 2001 From: Rodrigo Campos Date: Fri, 24 Jun 2022 17:29:59 +0200 Subject: [PATCH] pkg/kubelet: skip long test on short mode When adding functionality to the kubelet package and a test file, is kind of painful to run unit tests today locally. We usually can't run specifying the test file, as if xx_test.go and xx.go use the same package, we need to specify all the dependencies. As soon as xx.go uses the Kuebelet type (we need to do that to fake a kubelet in the unit tests), this is completely impossible to do in practice. So the other option is to run the unit tests for the whole package or run only a specific funtion. Running a single function can work in some cases, but it is painful when we want to test all the functions we wrote. On the other hand, running the test for the whole package is very slow. Today some unit tests try to connect to the API server (with retries) create and list lot of pods/volumes, etc. This makes running the unit test for the kubelet package slow. This patch tries to make running the unit test for the whole package more palatable. This patch adds a skip if the short version was requested (go test -short ...), so we don't try to connect to the API server or skip other slow tests. Before this patch running the unit tests took in my computer (I've run it several times so the compilation is already done): $ time go test -v real 0m21.303s user 0m9.033s sys 0m2.052s With this patch it takes ~1/3 of the time: $ time go test -short -v real 0m7.825s user 0m9.588s sys 0m1.723s Around 8 seconds is something I can wait to run the tests :) Signed-off-by: Rodrigo Campos --- pkg/kubelet/kubelet_node_status_test.go | 4 ++++ pkg/kubelet/kubelet_test.go | 4 ++++ pkg/kubelet/kubelet_volumes_linux_test.go | 4 ++++ pkg/kubelet/kubelet_volumes_test.go | 24 +++++++++++++++++++++++ pkg/kubelet/pod_workers_test.go | 4 ++++ 5 files changed, 40 insertions(+) diff --git a/pkg/kubelet/kubelet_node_status_test.go b/pkg/kubelet/kubelet_node_status_test.go index 9495e633c3b..88fa09664f1 100644 --- a/pkg/kubelet/kubelet_node_status_test.go +++ b/pkg/kubelet/kubelet_node_status_test.go @@ -503,6 +503,10 @@ func TestUpdateExistingNodeStatus(t *testing.T) { } func TestUpdateExistingNodeStatusTimeout(t *testing.T) { + if testing.Short() { + t.Skip("skipping test in short mode.") + } + attempts := int64(0) failureCallbacks := int64(0) diff --git a/pkg/kubelet/kubelet_test.go b/pkg/kubelet/kubelet_test.go index eb8269be2c2..e708eb8a468 100644 --- a/pkg/kubelet/kubelet_test.go +++ b/pkg/kubelet/kubelet_test.go @@ -661,6 +661,10 @@ func TestHandlePodCleanups(t *testing.T) { } func TestHandlePodRemovesWhenSourcesAreReady(t *testing.T) { + if testing.Short() { + t.Skip("skipping test in short mode.") + } + ready := false testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */) diff --git a/pkg/kubelet/kubelet_volumes_linux_test.go b/pkg/kubelet/kubelet_volumes_linux_test.go index cb6db59823a..38c5e0d5616 100644 --- a/pkg/kubelet/kubelet_volumes_linux_test.go +++ b/pkg/kubelet/kubelet_volumes_linux_test.go @@ -54,6 +54,10 @@ func validateDirNotExists(dir string) error { } func TestCleanupOrphanedPodDirs(t *testing.T) { + if testing.Short() { + t.Skip("skipping test in short mode.") + } + testCases := map[string]struct { pods []*v1.Pod prepareFunc func(kubelet *Kubelet) error diff --git a/pkg/kubelet/kubelet_volumes_test.go b/pkg/kubelet/kubelet_volumes_test.go index d5a0110b068..70290abb897 100644 --- a/pkg/kubelet/kubelet_volumes_test.go +++ b/pkg/kubelet/kubelet_volumes_test.go @@ -35,6 +35,10 @@ import ( ) func TestListVolumesForPod(t *testing.T) { + if testing.Short() { + t.Skip("skipping test in short mode.") + } + testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */) defer testKubelet.Cleanup() kubelet := testKubelet.kubelet @@ -95,6 +99,10 @@ func TestListVolumesForPod(t *testing.T) { } func TestPodVolumesExist(t *testing.T) { + if testing.Short() { + t.Skip("skipping test in short mode.") + } + defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIMigrationGCE, false)() testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */) @@ -207,6 +215,10 @@ func TestPodVolumesExist(t *testing.T) { } func TestVolumeAttachAndMountControllerDisabled(t *testing.T) { + if testing.Short() { + t.Skip("skipping test in short mode.") + } + defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIMigrationGCE, false)() testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */) @@ -264,6 +276,10 @@ func TestVolumeAttachAndMountControllerDisabled(t *testing.T) { } func TestVolumeUnmountAndDetachControllerDisabled(t *testing.T) { + if testing.Short() { + t.Skip("skipping test in short mode.") + } + defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIMigrationGCE, false)() testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */) @@ -351,6 +367,10 @@ func TestVolumeUnmountAndDetachControllerDisabled(t *testing.T) { } func TestVolumeAttachAndMountControllerEnabled(t *testing.T) { + if testing.Short() { + t.Skip("skipping test in short mode.") + } + defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIMigrationGCE, false)() testKubelet := newTestKubelet(t, true /* controllerAttachDetachEnabled */) @@ -433,6 +453,10 @@ func TestVolumeAttachAndMountControllerEnabled(t *testing.T) { } func TestVolumeUnmountAndDetachControllerEnabled(t *testing.T) { + if testing.Short() { + t.Skip("skipping test in short mode.") + } + defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIMigrationGCE, false)() testKubelet := newTestKubelet(t, true /* controllerAttachDetachEnabled */) diff --git a/pkg/kubelet/pod_workers_test.go b/pkg/kubelet/pod_workers_test.go index eafc826b85a..98931b5a6ec 100644 --- a/pkg/kubelet/pod_workers_test.go +++ b/pkg/kubelet/pod_workers_test.go @@ -614,6 +614,10 @@ func TestTerminalPhaseTransition(t *testing.T) { } func TestStaticPodExclusion(t *testing.T) { + if testing.Short() { + t.Skip("skipping test in short mode.") + } + podWorkers, processed := createPodWorkers() var channels WorkChannel podWorkers.workerChannelFn = channels.Intercept