Skip to content

Commit 51d41df

Browse files
authored
Merge pull request kubernetes#89542 from julianvmodesto/diff-dry-run-conformance
Add kubectl diff and server-side dry-run to e2e tests
2 parents b35fdbc + f448c01 commit 51d41df

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

test/e2e/framework/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ func (b KubectlBuilder) Exec() (string, error) {
637637
rc = int(ee.Sys().(syscall.WaitStatus).ExitStatus())
638638
Logf("rc: %d", rc)
639639
}
640-
return "", uexec.CodeExitError{
640+
return stdout.String(), uexec.CodeExitError{
641641
Err: fmt.Errorf("error running %v:\nCommand stdout:\n%v\nstderr:\n%v\nerror:\n%v", cmd, cmd.Stdout, cmd.Stderr, err),
642642
Code: rc,
643643
}

test/e2e/kubectl/kubectl.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,71 @@ metadata:
854854
})
855855
})
856856

857+
ginkgo.Describe("Kubectl diff", func() {
858+
/*
859+
Release : v1.18
860+
Testname: Kubectl, diff Deployment
861+
Description: Create a Deployment with httpd image. Declare the same Deployment with a different image, busybox. Diff of live Deployment with declared Deployment MUST include the difference between live and declared image.
862+
*/
863+
ginkgo.It("should find diff in Deployment", func() {
864+
ginkgo.By("create deployment with httpd image")
865+
deployment := commonutils.SubstituteImageName(string(readTestFileOrDie(httpdDeployment3Filename)))
866+
framework.RunKubectlOrDieInput(ns, deployment, "create", "-f", "-")
867+
868+
ginkgo.By("verify diff finds difference between live and declared image")
869+
deployment = strings.Replace(deployment, httpdImage, busyboxImage, 1)
870+
if !strings.Contains(deployment, busyboxImage) {
871+
framework.Failf("Failed replacing image from %s to %s in:\n%s\n", httpdImage, busyboxImage, deployment)
872+
}
873+
output, err := framework.RunKubectlInput(ns, deployment, "diff", "-f", "-")
874+
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
875+
framework.Failf("Expected kubectl diff exit code of 1, but got %d: %v\n", err.ExitCode(), err)
876+
}
877+
requiredItems := []string{httpdImage, busyboxImage}
878+
for _, item := range requiredItems {
879+
if !strings.Contains(output, item) {
880+
framework.Failf("Missing %s in kubectl diff output:\n%s\n%v\n", item, output, err)
881+
}
882+
}
883+
884+
framework.RunKubectlOrDieInput(ns, deployment, "delete", "-f", "-")
885+
})
886+
})
887+
888+
ginkgo.Describe("Kubectl server-side dry-run", func() {
889+
/*
890+
Release : v1.18
891+
Testname: Kubectl, server-side dry-run Pod
892+
Description: The command 'kubectl run' must create a pod with the specified image name. After, the command 'kubectl replace --dry-run=server' should update the Pod with the new image name and server-side dry-run enabled. The image name must not change.
893+
*/
894+
ginkgo.It("should dry-run update the Pod", func() {
895+
ginkgo.By("running the image " + httpdImage)
896+
podName := "e2e-test-httpd-pod"
897+
nsFlag := fmt.Sprintf("--namespace=%v", ns)
898+
framework.RunKubectlOrDie(ns, "run", podName, "--image="+httpdImage, "--labels=run="+podName, nsFlag)
899+
900+
ginkgo.By("replace the image in the pod with server-side dry-run")
901+
podJSON := framework.RunKubectlOrDie(ns, "get", "pod", podName, "-o", "json", nsFlag)
902+
podJSON = strings.Replace(podJSON, httpdImage, busyboxImage, 1)
903+
if !strings.Contains(podJSON, busyboxImage) {
904+
framework.Failf("Failed replacing image from %s to %s in:\n%s\n", httpdImage, busyboxImage, podJSON)
905+
}
906+
framework.RunKubectlOrDieInput(ns, podJSON, "replace", "-f", "-", "--dry-run", "server", nsFlag)
907+
908+
ginkgo.By("verifying the pod " + podName + " has the right image " + httpdImage)
909+
pod, err := c.CoreV1().Pods(ns).Get(context.TODO(), podName, metav1.GetOptions{})
910+
if err != nil {
911+
framework.Failf("Failed getting pod %s: %v", podName, err)
912+
}
913+
containers := pod.Spec.Containers
914+
if checkContainersImage(containers, httpdImage) {
915+
framework.Failf("Failed creating pod with expected image %s", httpdImage)
916+
}
917+
918+
framework.RunKubectlOrDie(ns, "delete", "pods", podName, nsFlag)
919+
})
920+
})
921+
857922
// definitionMatchesGVK returns true if the specified GVK is listed as an x-kubernetes-group-version-kind extension
858923
definitionMatchesGVK := func(extensions []*openapi_v2.NamedAny, desiredGVK schema.GroupVersionKind) bool {
859924
for _, extension := range extensions {

0 commit comments

Comments
 (0)