From 783da34f543c0092fe5768e1bcfee4ae378d0500 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Tyczy=C5=84ski?= Date: Mon, 30 May 2022 20:30:58 +0200 Subject: [PATCH] Clean shutdown of disruption integration tests --- pkg/controller/disruption/disruption.go | 17 +++++--- .../integration/disruption/disruption_test.go | 41 +++++++++++-------- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/pkg/controller/disruption/disruption.go b/pkg/controller/disruption/disruption.go index 3c8fcbab770..018157727b7 100644 --- a/pkg/controller/disruption/disruption.go +++ b/pkg/controller/disruption/disruption.go @@ -358,7 +358,18 @@ func verifyGroupKind(controllerRef *metav1.OwnerReference, expectedKind string, func (dc *DisruptionController) Run(ctx context.Context) { defer utilruntime.HandleCrash() + + // Start events processing pipeline. + if dc.kubeClient != nil { + klog.Infof("Sending events to api server.") + dc.broadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: dc.kubeClient.CoreV1().Events("")}) + } else { + klog.Infof("No api server defined - no events will be sent to API server.") + } + defer dc.broadcaster.Shutdown() + defer dc.queue.ShutDown() + defer dc.recheckQueue.ShutDown() klog.Infof("Starting disruption controller") defer klog.Infof("Shutting down disruption controller") @@ -367,12 +378,6 @@ func (dc *DisruptionController) Run(ctx context.Context) { return } - if dc.kubeClient != nil { - klog.Infof("Sending events to api server.") - dc.broadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: dc.kubeClient.CoreV1().Events("")}) - } else { - klog.Infof("No api server defined - no events will be sent to API server.") - } go wait.UntilWithContext(ctx, dc.worker, time.Second) go wait.Until(dc.recheckWorker, time.Second, ctx.Done()) diff --git a/test/integration/disruption/disruption_test.go b/test/integration/disruption/disruption_test.go index ca7fc8b31e0..a810d45d5e6 100644 --- a/test/integration/disruption/disruption_test.go +++ b/test/integration/disruption/disruption_test.go @@ -101,7 +101,9 @@ func setup(t *testing.T) (*kubeapiservertesting.TestServer, *disruption.Disrupti func TestPDBWithScaleSubresource(t *testing.T) { s, pdbc, informers, clientSet, apiExtensionClient, dynamicClient := setup(t) defer s.TearDownFn() - ctx := context.TODO() + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() nsName := "pdb-scale-subresource" createNs(ctx, t, nsName, clientSet) @@ -187,16 +189,14 @@ func TestPDBWithScaleSubresource(t *testing.T) { } func TestEmptySelector(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() testcases := []struct { name string - createPDBFunc func(clientSet clientset.Interface, name, nsName string, minAvailable intstr.IntOrString) error + createPDBFunc func(ctx context.Context, clientSet clientset.Interface, name, nsName string, minAvailable intstr.IntOrString) error expectedCurrentHealthy int32 }{ { name: "v1beta1 should not target any pods", - createPDBFunc: func(clientSet clientset.Interface, name, nsName string, minAvailable intstr.IntOrString) error { + createPDBFunc: func(ctx context.Context, clientSet clientset.Interface, name, nsName string, minAvailable intstr.IntOrString) error { pdb := &v1beta1.PodDisruptionBudget{ ObjectMeta: metav1.ObjectMeta{ Name: name, @@ -213,7 +213,7 @@ func TestEmptySelector(t *testing.T) { }, { name: "v1 should target all pods", - createPDBFunc: func(clientSet clientset.Interface, name, nsName string, minAvailable intstr.IntOrString) error { + createPDBFunc: func(ctx context.Context, clientSet clientset.Interface, name, nsName string, minAvailable intstr.IntOrString) error { pdb := &policyv1.PodDisruptionBudget{ ObjectMeta: metav1.ObjectMeta{ Name: name, @@ -235,6 +235,9 @@ func TestEmptySelector(t *testing.T) { s, pdbc, informers, clientSet, _, _ := setup(t) defer s.TearDownFn() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + nsName := fmt.Sprintf("pdb-empty-selector-%d", i) createNs(ctx, t, nsName, clientSet) @@ -252,7 +255,7 @@ func TestEmptySelector(t *testing.T) { waitToObservePods(t, informers.Core().V1().Pods().Informer(), 4, v1.PodRunning) pdbName := "test-pdb" - if err := tc.createPDBFunc(clientSet, pdbName, nsName, minAvailable); err != nil { + if err := tc.createPDBFunc(ctx, clientSet, pdbName, nsName, minAvailable); err != nil { t.Errorf("Error creating PodDisruptionBudget: %v", err) } @@ -271,16 +274,14 @@ func TestEmptySelector(t *testing.T) { } func TestSelectorsForPodsWithoutLabels(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() testcases := []struct { name string - createPDBFunc func(clientSet clientset.Interface, name, nsName string, minAvailable intstr.IntOrString) error + createPDBFunc func(ctx context.Context, clientSet clientset.Interface, name, nsName string, minAvailable intstr.IntOrString) error expectedCurrentHealthy int32 }{ { name: "pods with no labels can be targeted by v1 PDBs with empty selector", - createPDBFunc: func(clientSet clientset.Interface, name, nsName string, minAvailable intstr.IntOrString) error { + createPDBFunc: func(ctx context.Context, clientSet clientset.Interface, name, nsName string, minAvailable intstr.IntOrString) error { pdb := &policyv1.PodDisruptionBudget{ ObjectMeta: metav1.ObjectMeta{ Name: name, @@ -297,7 +298,7 @@ func TestSelectorsForPodsWithoutLabels(t *testing.T) { }, { name: "pods with no labels can be targeted by v1 PDBs with DoesNotExist selector", - createPDBFunc: func(clientSet clientset.Interface, name, nsName string, minAvailable intstr.IntOrString) error { + createPDBFunc: func(ctx context.Context, clientSet clientset.Interface, name, nsName string, minAvailable intstr.IntOrString) error { pdb := &policyv1.PodDisruptionBudget{ ObjectMeta: metav1.ObjectMeta{ Name: name, @@ -321,7 +322,7 @@ func TestSelectorsForPodsWithoutLabels(t *testing.T) { }, { name: "pods with no labels can be targeted by v1beta1 PDBs with DoesNotExist selector", - createPDBFunc: func(clientSet clientset.Interface, name, nsName string, minAvailable intstr.IntOrString) error { + createPDBFunc: func(ctx context.Context, clientSet clientset.Interface, name, nsName string, minAvailable intstr.IntOrString) error { pdb := &v1beta1.PodDisruptionBudget{ ObjectMeta: metav1.ObjectMeta{ Name: name, @@ -350,6 +351,9 @@ func TestSelectorsForPodsWithoutLabels(t *testing.T) { s, pdbc, informers, clientSet, _, _ := setup(t) defer s.TearDownFn() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + nsName := fmt.Sprintf("pdb-selectors-%d", i) createNs(ctx, t, nsName, clientSet) @@ -360,7 +364,7 @@ func TestSelectorsForPodsWithoutLabels(t *testing.T) { // Create the PDB first and wait for it to settle. pdbName := "test-pdb" - if err := tc.createPDBFunc(clientSet, pdbName, nsName, minAvailable); err != nil { + if err := tc.createPDBFunc(ctx, clientSet, pdbName, nsName, minAvailable); err != nil { t.Errorf("Error creating PodDisruptionBudget: %v", err) } waitPDBStable(ctx, t, clientSet, 0, nsName, pdbName) @@ -498,9 +502,15 @@ func waitToObservePods(t *testing.T, podInformer cache.SharedIndexInformer, podN } func TestPatchCompatibility(t *testing.T) { - s, _, _, clientSet, _, _ := setup(t) + s, pdbc, _, clientSet, _, _ := setup(t) defer s.TearDownFn() + // Even though pdbc isn't used in this test, its creation is already + // spawning some goroutines. So we need to run it to ensure they won't leak. + ctx, cancel := context.WithCancel(context.Background()) + cancel() + pdbc.Run(ctx) + testcases := []struct { name string version string @@ -634,5 +644,4 @@ func TestPatchCompatibility(t *testing.T) { } }) } - }