From e25479c5aee677d1817cbf96c35bbfc09d6acbc5 Mon Sep 17 00:00:00 2001 From: vivekr-splunk <94569031+vivekr-splunk@users.noreply.github.com> Date: Fri, 11 Aug 2023 15:20:02 -0700 Subject: [PATCH] working test code for upgrade Signed-off-by: vivekr-splunk <94569031+vivekr-splunk@users.noreply.github.com> --- pkg/splunk/enterprise/upgrade_test.go | 171 ++++++++++++++++---------- 1 file changed, 105 insertions(+), 66 deletions(-) diff --git a/pkg/splunk/enterprise/upgrade_test.go b/pkg/splunk/enterprise/upgrade_test.go index 358f06291..bd392f103 100644 --- a/pkg/splunk/enterprise/upgrade_test.go +++ b/pkg/splunk/enterprise/upgrade_test.go @@ -459,10 +459,6 @@ func TestUpgradePathValidation(t *testing.T) { if err != nil { t.Errorf("update should not have returned error; err=%v", err) } - _, err = ApplyClusterManager(ctx, client, &cm) - if err != nil { - t.Errorf("ApplyStandalone should not have returned error; err=%v", err) - } // license manager err = client.Get(ctx, namespacedName, &lm) @@ -508,30 +504,30 @@ func TestUpgradePathValidation(t *testing.T) { t.Errorf("update should not have returned error; err=%v", err) } - cm.Status.TelAppInstalled = true - _, err = ApplyClusterManager(ctx, client, &cm) - if err != nil { - t.Errorf("applyClusterManager after update should not have returned error; err=%v", err) - } lm.Status.TelAppInstalled = true _, err = ApplyLicenseManager(ctx, client, &lm) if err != nil { t.Errorf("ApplyLicenseManager after update should not have returned error; err=%v", err) } - _, err = ApplyMonitoringConsole(ctx, client, &mc) + cm.Status.TelAppInstalled = true + _, err = ApplyClusterManager(ctx, client, &cm) if err != nil { - t.Errorf("applyMonitoringConsole after update should not have returned error; err=%v", err) + t.Errorf("applyClusterManager after update should not have returned error; err=%v", err) } - _, err = ApplyIndexerClusterManager(ctx, client, &idx) + _, err = ApplyMonitoringConsole(ctx, client, &mc) if err != nil { - t.Errorf("ApplyIndexerClusterManager after update should not have returned error; err=%v", err) + t.Errorf("applyMonitoringConsole after update should not have returned error; err=%v", err) } + shc.Status.TelAppInstalled = true _, err = ApplySearchHeadCluster(ctx, client, &shc) if err != nil { t.Errorf("applySearchHeadCluster after update should not have returned error; err=%v", err) } - + _, err = ApplyIndexerClusterManager(ctx, client, &idx) + if err != nil { + t.Errorf("ApplyIndexerClusterManager after update should not have returned error; err=%v", err) + } newImage := "splunk/splunk:latest" // create pods for license manager createPods(t, ctx, client, "license-manager", fmt.Sprintf("splunk-%s-license-manager-0", lm.Name), lm.Namespace, newImage) @@ -560,86 +556,127 @@ func TestUpgradePathValidation(t *testing.T) { updateStatefulSetsInTest(t, ctx, client, 1, fmt.Sprintf("splunk-%s-deployer", shc.Name), shc.Namespace) shc.Status.TelAppInstalled = true + lm.Status.TelAppInstalled = true + _, err = ApplyLicenseManager(ctx, client, &lm) + if err != nil { + t.Errorf("ApplyLicenseManager after update should not have returned error; err=%v", err) + } + cm.Status.TelAppInstalled = true _, err = ApplyClusterManager(ctx, client, &cm) if err != nil { t.Errorf("applyClusterManager after update should not have returned error; err=%v", err) } - lm.Status.TelAppInstalled = true - _, err = ApplyLicenseManager(ctx, client, &lm) + + cm.Status.TelAppInstalled = true + _, err = ApplyClusterManager(ctx, client, &cm) if err != nil { - t.Errorf("ApplyLicenseManager after update should not have returned error; err=%v", err) + t.Errorf("applyClusterManager after update should not have returned error; err=%v", err) } + _, err = ApplyMonitoringConsole(ctx, client, &mc) if err != nil { t.Errorf("applyMonitoringConsole after update should not have returned error; err=%v", err) } - _, err = ApplyIndexerClusterManager(ctx, client, &idx) + + _, err = ApplyMonitoringConsole(ctx, client, &mc) if err != nil { - t.Errorf("ApplyIndexerClusterManager after update should not have returned error; err=%v", err) + t.Errorf("applyMonitoringConsole after update should not have returned error; err=%v", err) + } + + shc.Status.TelAppInstalled = true + _, err = ApplySearchHeadCluster(ctx, client, &shc) + if err != nil { + t.Errorf("applySearchHeadCluster after update should not have returned error; err=%v", err) } + shc.Status.TelAppInstalled = true _, err = ApplySearchHeadCluster(ctx, client, &shc) if err != nil { t.Errorf("applySearchHeadCluster after update should not have returned error; err=%v", err) } + + _, err = ApplyIndexerClusterManager(ctx, client, &idx) + if err != nil { + t.Errorf("ApplyIndexerClusterManager after update should not have returned error; err=%v", err) + } + } func createPods(t *testing.T, ctx context.Context, client common.ControllerClient, crtype, name, namespace, image string) { - // create pod - stpod := &corev1.Pod{ - ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: namespace, - Labels: map[string]string{ - "app.kubernetes.io/managed-by": "splunk-operator", - "app.kubernetes.io/component": crtype, - "app.kubernetes.io/name": crtype, - "app.kubernetes.io/part-of": fmt.Sprintf("splunk-test-%s", crtype), - "app.kubernetes.io/instance": fmt.Sprintf("splunk-test-%s", crtype), - }, - Annotations: map[string]string{ - "traffic.sidecar.istio.io/excludeOutboundPorts": "8089,8191,9997", - "traffic.sidecar.istio.io/includeInboundPorts": "8000", + stpod := &corev1.Pod{} + namespacesName := types.NamespacedName{ + Name: name, + Namespace: namespace, + } + err := client.Get(ctx, namespacesName, stpod) + if err != nil && k8serrors.IsNotFound(err) { + // create pod + stpod = &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: name, + Namespace: namespace, + Labels: map[string]string{ + "app.kubernetes.io/managed-by": "splunk-operator", + "app.kubernetes.io/component": crtype, + "app.kubernetes.io/name": crtype, + "app.kubernetes.io/part-of": fmt.Sprintf("splunk-test-%s", crtype), + "app.kubernetes.io/instance": fmt.Sprintf("splunk-test-%s", crtype), + }, + Annotations: map[string]string{ + "traffic.sidecar.istio.io/excludeOutboundPorts": "8089,8191,9997", + "traffic.sidecar.istio.io/includeInboundPorts": "8000", + }, }, - }, - Spec: corev1.PodSpec{ - Containers: []corev1.Container{ - { - Name: "splunk", - Image: image, - Env: []corev1.EnvVar{ - { - Name: "test", - Value: "test", - }, - }, - Ports: []corev1.ContainerPort{ - { - Name: "http-splunkweb", - HostPort: 0, - ContainerPort: 8000, - Protocol: "TCP", - HostIP: "", + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "splunk", + Image: image, + Env: []corev1.EnvVar{ + { + Name: "test", + Value: "test", + }, }, - { - Name: "https-splunkd", - HostPort: 0, - ContainerPort: 8089, - Protocol: "TCP", - HostIP: "", + Ports: []corev1.ContainerPort{ + { + Name: "http-splunkweb", + HostPort: 0, + ContainerPort: 8000, + Protocol: "TCP", + HostIP: "", + }, + { + Name: "https-splunkd", + HostPort: 0, + ContainerPort: 8089, + Protocol: "TCP", + HostIP: "", + }, }, }, }, }, - }, - } - // simulate create stateful set - err := client.Create(ctx, stpod) - if err != nil { - t.Errorf("Unexpected create pod failed %v", err) + } + // simulate create stateful set + err := client.Create(ctx, stpod) + if err != nil { + t.Errorf("Unexpected create pod failed %v", err) + debug.PrintStack() + } + } else if err != nil { + t.Errorf("Unexpected erro while get pod %v", err) debug.PrintStack() } + if stpod.Spec.Containers[0].Image != image { + stpod.Spec.Containers[0].Image = image + err := client.Update(ctx, stpod) + if err != nil { + t.Errorf("Unexpected create pod failed %v", err) + debug.PrintStack() + } + } // update statefulset stpod.Status.Phase = corev1.PodRunning @@ -652,7 +689,7 @@ func createPods(t *testing.T, ctx context.Context, client common.ControllerClien } err = client.Status().Update(ctx, stpod) if err != nil { - t.Errorf("Unexpected update statefulset %v", err) + t.Errorf("Unexpected update pod %v", err) debug.PrintStack() } } @@ -671,6 +708,8 @@ func updateStatefulSetsInTest(t *testing.T, ctx context.Context, client common.C // update statefulset statefulset.Status.ReadyReplicas = replicas statefulset.Status.Replicas = replicas + statefulset.Status.CurrentReplicas = replicas + statefulset.Status.AvailableReplicas = replicas err = client.Status().Update(ctx, statefulset) if err != nil { t.Errorf("Unexpected update statefulset %v", err)