From bea1fc9b06f19573aec6dfb532d02e6d43a27c8f Mon Sep 17 00:00:00 2001 From: Sudharshan Muralidharan Date: Fri, 29 Sep 2023 16:58:27 +0530 Subject: [PATCH] ibmcloud: Included Contianer Ports along with nginx pod Added Containerport along with statements for debugging readiness probe Fixes: #1450 Signed-off-by: Sudharshan Muralidharan --- test/e2e/common.go | 31 ++++++++++++++++++++++++++++ test/e2e/common_suite_test.go | 39 ++++++++++++++++++++++++++++++----- test/e2e/ibmcloud_test.go | 12 +++++++++++ 3 files changed, 77 insertions(+), 5 deletions(-) diff --git a/test/e2e/common.go b/test/e2e/common.go index 23e4edc67b..8d9cabd16b 100644 --- a/test/e2e/common.go +++ b/test/e2e/common.go @@ -8,6 +8,7 @@ import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/intstr" ) type podOption func(*corev1.Pod) @@ -18,6 +19,23 @@ func withRestartPolicy(restartPolicy corev1.RestartPolicy) podOption { } } +// Optional method to add ContainerPort and ReadinessProbe to listen Port 80 +func withContainerPort(port int32) podOption { + return func(p *corev1.Pod) { + p.Spec.Containers[0].Ports = []corev1.ContainerPort{{ContainerPort: port}} + p.Spec.Containers[0].ReadinessProbe = &corev1.Probe{ + ProbeHandler: corev1.ProbeHandler{ + HTTPGet: &corev1.HTTPGetAction{ + Path: "/", + Port: intstr.FromInt(int(port)), + }, + }, + InitialDelaySeconds: 10, + PeriodSeconds: 5, + } + } +} + func withCommand(command []string) podOption { return func(p *corev1.Pod) { p.Spec.Containers[0].Command = command @@ -40,6 +58,19 @@ func withImagePullSecrets(secretName string) podOption { } } +// // optional method to add initcontainer in pod defenition +// func withInitContainers() podOption { +// return func(p *corev1.Pod) { +// p.Spec.InitContainers = []corev1.Container{ +// { +// Name: "useradd-init-container", +// Image: "busybox", +// Command: []string{"adduser", "-D", "nginx"}, +// }, +// } +// } +// } + func withConfigMapBinding(mountPath string, configMapName string) podOption { return func(p *corev1.Pod) { p.Spec.Containers[0].VolumeMounts = append(p.Spec.Containers[0].VolumeMounts, corev1.VolumeMount{Name: "config-volume", MountPath: mountPath}) diff --git a/test/e2e/common_suite_test.go b/test/e2e/common_suite_test.go index e68a029cc1..a6e5ad112d 100644 --- a/test/e2e/common_suite_test.go +++ b/test/e2e/common_suite_test.go @@ -185,7 +185,8 @@ func (tc *testCase) run() { if err = wait.For(conditions.New(client.Resources()).PodPhaseMatch(tc.pod, tc.podState), wait.WithTimeout(WAIT_POD_RUNNING_TIMEOUT)); err != nil { t.Fatal(err) } - if tc.podState == v1.PodRunning { + + if tc.podState == v1.PodRunning || tc.pod.Spec.Containers[0].ReadinessProbe != nil { clientset, err := kubernetes.NewForConfig(client.RESTConfig()) if err != nil { t.Fatal(err) @@ -194,9 +195,37 @@ func (tc *testCase) run() { if err != nil { t.Fatal(err) } - //Included logs for debugging nightly tests + //Added logs for debugging nightly tests t.Logf("Expected Pod State: %v", tc.podState) t.Logf("Current Pod State: %v", pod.Status.Phase) + //Getting Readiness probe of a container + for i, condition := range pod.Status.Conditions { + fmt.Printf("===================\n") + fmt.Printf("Checking Conditons - %v....\n", i+1) + fmt.Printf("===================\n") + fmt.Printf("*.Condition Type: %v\n", condition.Type) + fmt.Printf("*.Condition Status: %v\n", condition.Status) + fmt.Printf("*.Condition Last Probe Time: %v\n", condition.LastProbeTime) + fmt.Printf("*.Condition Last Transition Time: %v\n", condition.LastTransitionTime) + fmt.Printf("*.Condition Last Message: %v\n", condition.Message) + fmt.Printf("*.Condition Last Reason: %v\n", condition.Reason) + } + + readinessProbe := pod.Spec.Containers[0].ReadinessProbe + if readinessProbe != nil { + fmt.Printf("===================\n") + fmt.Printf("Checking Readiness Probe....\n") + fmt.Printf("===================\n") + fmt.Printf("*.Initial Delay Seconds: %v\n", readinessProbe.InitialDelaySeconds) + fmt.Printf("*.Timeout Seconds: %v\n", readinessProbe.TimeoutSeconds) + fmt.Printf("*.Success Threshold: %v\n", readinessProbe.SuccessThreshold) + fmt.Printf("*.Failure Threshold: %v\n", readinessProbe.FailureThreshold) + fmt.Printf("*.Period Seconds: %v\n", readinessProbe.PeriodSeconds) + fmt.Printf("*.Probe Handler: %v\n", readinessProbe.ProbeHandler) + fmt.Printf("*.Probe Handler Port: %v\n", readinessProbe.ProbeHandler.HTTPGet.Port) + fmt.Printf("===================\n") + } + } } @@ -636,7 +665,7 @@ func doTestCreatePodWithConfigMap(t *testing.T, assert CloudAssert) { configMapPath := podKubeConfigmapDir + configMapFileName configMapContents := "Hello, world" configMapData := map[string]string{configMapFileName: configMapContents} - pod := newPod(namespace, podName, containerName, imageName, withConfigMapBinding(podKubeConfigmapDir, configMapName)) + pod := newPod(namespace, podName, containerName, imageName, withConfigMapBinding(podKubeConfigmapDir, configMapName), withContainerPort(80)) configMap := newConfigMap(namespace, configMapName, configMapData) testCommands := []testCommand{ { @@ -672,7 +701,7 @@ func doTestCreatePodWithSecret(t *testing.T, assert CloudAssert) { password := "password" passwordPath := podKubeSecretsDir + passwordFileName secretData := map[string][]byte{passwordFileName: []byte(password), usernameFileName: []byte(username)} - pod := newPod(namespace, podName, containerName, imageName, withSecretBinding(podKubeSecretsDir, secretName)) + pod := newPod(namespace, podName, containerName, imageName, withSecretBinding(podKubeSecretsDir, secretName), withContainerPort(80)) secret := newSecret(namespace, secretName, secretData, v1.SecretTypeOpaque) testCommands := []testCommand{ @@ -781,7 +810,7 @@ func doTestCreatePeerPodAndCheckEnvVariableLogsWithDeploymentOnly(t *testing.T, namespace := envconf.RandomName("default", 7) podName := "env-variable-in-config" imageName := "nginx:latest" - pod := newPod(namespace, podName, podName, imageName, withRestartPolicy(v1.RestartPolicyOnFailure), withEnvironmentalVariables([]v1.EnvVar{{Name: "ISPRODUCTION", Value: "true"}}), withCommand([]string{"/bin/sh", "-c", "env"})) + pod := newPod(namespace, podName, podName, imageName, withRestartPolicy(v1.RestartPolicyOnFailure), withEnvironmentalVariables([]v1.EnvVar{{Name: "ISPRODUCTION", Value: "true"}}), withCommand([]string{"/bin/sh", "-c", "env"}), withContainerPort(80)) expectedPodLogString := "ISPRODUCTION=true" newTestCase(t, "EnvVariablePeerPodWithDeploymentOnly", assert, "Peer pod with environmental variables has been created").withPod(pod).withExpectedPodLogString(expectedPodLogString).withCustomPodState(v1.PodSucceeded).run() } diff --git a/test/e2e/ibmcloud_test.go b/test/e2e/ibmcloud_test.go index a00c8dd8b6..c32d010bb7 100644 --- a/test/e2e/ibmcloud_test.go +++ b/test/e2e/ibmcloud_test.go @@ -16,6 +16,7 @@ import ( log "github.com/sirupsen/logrus" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/intstr" "k8s.io/utils/pointer" ) @@ -308,6 +309,17 @@ func newPodWithPVCFromIBMVPCBlockDriver(namespace, podName, containerName, image Name: containerName, Image: imageName, ImagePullPolicy: corev1.PullAlways, + Ports: []corev1.ContainerPort{{ContainerPort: 80}}, + ReadinessProbe: &corev1.Probe{ + ProbeHandler: corev1.ProbeHandler{ + HTTPGet: &corev1.HTTPGetAction{ + Path: "/", + Port: intstr.FromInt(80), + }, + }, + InitialDelaySeconds: 10, + PeriodSeconds: 5, + }, }, }, ServiceAccountName: "ibm-vpc-block-node-sa",