From 3e6ba4d2b17dd58d8d9dc3bbdefa54be85ee6040 Mon Sep 17 00:00:00 2001 From: Riaan Kleinhans Date: Tue, 16 Feb 2021 09:55:01 +1300 Subject: [PATCH] Create ReplicaSet lifecycle test --- test/e2e/apps/replica_set.go | 68 ++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/test/e2e/apps/replica_set.go b/test/e2e/apps/replica_set.go index 7edc94caa9d..24f775d9b9e 100644 --- a/test/e2e/apps/replica_set.go +++ b/test/e2e/apps/replica_set.go @@ -122,6 +122,10 @@ var _ = SIGDescribe("ReplicaSet", func() { ginkgo.It("Replicaset should have a working scale subresource", func() { testRSScaleSubresources(f) }) + + ginkgo.It("ReplicaSet Replace and Patch tests", func() { + testRSLifeCycle(f) + }) }) // A basic test to check the deployment of an image using a ReplicaSet. The @@ -412,3 +416,67 @@ func testRSScaleSubresources(f *framework.Framework) { framework.ExpectEqual(*(rs.Spec.Replicas), int32(4), "replicaset should have 4 replicas") } + +// ReplicaSet Replace and Patch tests +func testRSLifeCycle(f *framework.Framework) { + ns := f.Namespace.Name + c := f.ClientSet + zero := int64(0) + + // Create webserver pods. + rsPodLabels := map[string]string{ + "name": "sample-pod", + "pod": WebserverImageName, + } + + rsName := "test-rs" + replicas := int32(1) + rsPatchReplicas := int32(3) + rsPatchImage := imageutils.GetE2EImage(imageutils.Pause) + + // Create a ReplicaSet + rs := newRS(rsName, replicas, rsPodLabels, WebserverImageName, WebserverImage, nil) + _, err := c.AppsV1().ReplicaSets(ns).Create(context.TODO(), rs, metav1.CreateOptions{}) + framework.ExpectNoError(err) + + // Verify that the required pods have come up. + err = e2epod.VerifyPodsRunning(c, ns, "sample-pod", false, replicas) + framework.ExpectNoError(err, "Failed to create pods: %s", err) + + // Scale the ReplicaSet + ginkgo.By(fmt.Sprintf("Scaling up %q replicaset ", rsName)) + _, err = e2ereplicaset.UpdateReplicaSetWithRetries(c, ns, rsName, func(update *appsv1.ReplicaSet) { + x := int32(2) + update.Spec.Replicas = &x + }) + framework.ExpectNoError(err, "ReplicaSet fail to scale to %q replicasets") + + // Patch the PeplicaSet + ginkgo.By("patching the ReplicaSet") + rsPatch, err := json.Marshal(map[string]interface{}{ + "metadata": map[string]interface{}{ + "labels": map[string]string{"test-rs": "patched"}, + }, + "spec": map[string]interface{}{ + "replicas": rsPatchReplicas, + "template": map[string]interface{}{ + "spec": map[string]interface{}{ + "TerminationGracePeriodSeconds": &zero, + "containers": [1]map[string]interface{}{{ + "name": rsName, + "image": rsPatchImage, + "command": []string{"/bin/sleep", "100000"}, + }}, + }, + }, + }, + }) + framework.ExpectNoError(err, "failed to Marshal ReplicaSet JSON patch") + _, err = f.ClientSet.AppsV1().ReplicaSets(ns).Patch(context.TODO(), rsName, types.StrategicMergePatchType, []byte(rsPatch), metav1.PatchOptions{}) + framework.ExpectNoError(err, "failed to patch ReplicaSet") + + rs, err = c.AppsV1().ReplicaSets(ns).Get(context.TODO(), rsName, metav1.GetOptions{}) + framework.ExpectNoError(err, "Failed to get replicaset resource: %v", err) + framework.ExpectEqual(*(rs.Spec.Replicas), rsPatchReplicas, "replicaset should have 3 replicas") + framework.ExpectEqual(rs.Spec.Template.Spec.Containers[0].Image, rsPatchImage, "replicaset not using rsPatchImage. Is using %v", rs.Spec.Template.Spec.Containers[0].Image) +}