From 87e0a7504695d9d8c0f8cf4d0b88a8c33cbbe17f Mon Sep 17 00:00:00 2001 From: Jonathan Hess Date: Tue, 30 May 2023 11:58:06 -0600 Subject: [PATCH 1/2] test: Add new integration test to delete misconfigured pods. --- internal/testintegration/integration_test.go | 101 +++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/internal/testintegration/integration_test.go b/internal/testintegration/integration_test.go index 0cabff61..b6a7d378 100644 --- a/internal/testintegration/integration_test.go +++ b/internal/testintegration/integration_test.go @@ -312,6 +312,107 @@ func TestUpdateWorkloadContainerWhenDefaultProxyImageChanges(t *testing.T) { } +// TestDeleteMisconfiguredPod is the test that +// demonstrates that when a pod is created, and the webhook does not work, then +// the PodDeleteController will attempt to delete the pod. +func TestDeleteMisconfiguredPod(t *testing.T) { + ctx := testintegration.TestContext() + + // Use a fresh Manager Harness because we are messing with the operator + // lifecycle. + th, err := testintegration.EnvTestSetup() + defer th.Teardown() + tcc := newTestCaseClient("deletemisconfig", th.Client) + + err = tcc.CreateOrPatchNamespace(ctx) + if err != nil { + t.Fatalf("can't create namespace, %v", err) + } + + const ( + pwlName = "newdeploy" + deploymentAppLabel = "busybox" + ) + key := types.NamespacedName{Name: pwlName, Namespace: tcc.Namespace} + + t.Log("Creating AuthProxyWorkload") + _, err = tcc.CreateAuthProxyWorkload(ctx, key, deploymentAppLabel, tcc.ConnectionString, "Deployment") + if err != nil { + t.Error(err) + return + } + + // Stop the manager before attempting to create the resources + th.StopMgr() + t.Log("Manager is stopped") + + t.Log("Creating deployment") + d := testhelpers.BuildDeployment(key, deploymentAppLabel) + err = tcc.CreateWorkload(ctx, d) + if err != nil { + t.Fatal("unable to create deployment", err) + return + } + + t.Log("Creating deployment replicas") + rs, pl, err := tcc.CreateDeploymentReplicaSetAndPods(ctx, d) + if err != nil { + t.Fatal("unable to create pods", err) + return + } + + // Check that proxy container was not added to pods, because the manager is stopped + err = tcc.ExpectPodContainerCount(ctx, d.Spec.Selector, 1, "all") + if err != nil { + t.Fatal(err) + } + + pods, err := testhelpers.ListPods(ctx, tcc.Client, tcc.Namespace, d.Spec.Selector) + if len(pods.Items) == 0 { + t.Fatal("No pods found") + } + + t.Log("Restarting the manager...") + // Start the manager + err = th.StartMgr(workload.DefaultProxyImage) + if err != nil { + t.Fatal("can't restart container", err) + } + + // Expect that old pods are deleted + err = testhelpers.RetryUntilSuccess(24, testhelpers.DefaultRetryInterval, func() error { + pods, err := testhelpers.ListPods(ctx, tcc.Client, tcc.Namespace, d.Spec.Selector) + if err != nil { + return err + } + var podCount int + for _, pod := range pods.Items { + if !pod.GetDeletionTimestamp().IsZero() { + podCount++ + } + } + if podCount > 0 { + return fmt.Errorf("got %v, want 0 pods that are not yet deleted", podCount) + } + return nil + }) + + if err != nil { + t.Fatal(err) + } + + // Recreate the ReplicaSet and Pods as would happen when the deployment + // PodTemplate changed. + err = recreatePodsAfterDeploymentUpdate(ctx, tcc, d, rs, pl) + if err != nil { + t.Fatal(err) + } + +} + +// recreatePodsAfterDeploymentUpdate acts like the DeploymentController, when +// the Deployment is updated. It deletes the old ReplicaSet and Pods, and +// creates a new ReplicaSet and Pods. func recreatePodsAfterDeploymentUpdate(ctx context.Context, tcc *testhelpers.TestCaseClient, d *appsv1.Deployment, rs1 *appsv1.ReplicaSet, pods []*corev1.Pod) error { // Then we simulate the deployment pods being replaced err := tcc.Client.Delete(ctx, rs1) From 13165379174d889e5037c399e1bc0cdd2fd3d948 Mon Sep 17 00:00:00 2001 From: Jonathan Hess Date: Fri, 9 Jun 2023 11:41:41 -0600 Subject: [PATCH 2/2] fix: apply refactoring from main --- internal/testintegration/integration_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/testintegration/integration_test.go b/internal/testintegration/integration_test.go index b6a7d378..9dcac68d 100644 --- a/internal/testintegration/integration_test.go +++ b/internal/testintegration/integration_test.go @@ -320,7 +320,7 @@ func TestDeleteMisconfiguredPod(t *testing.T) { // Use a fresh Manager Harness because we are messing with the operator // lifecycle. - th, err := testintegration.EnvTestSetup() + th, err := testintegration.NewTestHarness() defer th.Teardown() tcc := newTestCaseClient("deletemisconfig", th.Client) @@ -343,7 +343,7 @@ func TestDeleteMisconfiguredPod(t *testing.T) { } // Stop the manager before attempting to create the resources - th.StopMgr() + th.StopManager() t.Log("Manager is stopped") t.Log("Creating deployment") @@ -374,7 +374,7 @@ func TestDeleteMisconfiguredPod(t *testing.T) { t.Log("Restarting the manager...") // Start the manager - err = th.StartMgr(workload.DefaultProxyImage) + err = th.StartManager(workload.DefaultProxyImage) if err != nil { t.Fatal("can't restart container", err) }