Skip to content

Commit f448c01

Browse files
Add kubectl diff + server-side dry-run to e2e
1 parent 788a073 commit f448c01

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
@@ -647,7 +647,7 @@ func (b KubectlBuilder) Exec() (string, error) {
647647
rc = int(ee.Sys().(syscall.WaitStatus).ExitStatus())
648648
Logf("rc: %d", rc)
649649
}
650-
return "", uexec.CodeExitError{
650+
return stdout.String(), uexec.CodeExitError{
651651
Err: fmt.Errorf("error running %v:\nCommand stdout:\n%v\nstderr:\n%v\nerror:\n%v", cmd, cmd.Stdout, cmd.Stderr, err),
652652
Code: rc,
653653
}

test/e2e/kubectl/kubectl.go

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

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

0 commit comments

Comments
 (0)