Skip to content

Commit

Permalink
failing mc unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Vivek Reddy <[email protected]>
  • Loading branch information
Vivek Reddy committed Jul 22, 2024
1 parent 2b5d202 commit 6f87d39
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 38 deletions.
37 changes: 31 additions & 6 deletions pkg/splunk/enterprise/monitoringconsole_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ func init() {

func TestApplyMonitoringConsole(t *testing.T) {
funcCalls := []spltest.MockFuncCall{
{MetaName: "*v1.Secret-test-splunk-test-secret"},
{MetaName: "*v1.Secret-test-splunk-test-secret"},
{MetaName: "*v1.Secret-test-splunk-test-secret"},
{MetaName: "*v1.Service-test-splunk-stack1-monitoring-console-headless"},
{MetaName: "*v1.Service-test-splunk-stack1-monitoring-console-service"},
{MetaName: "*v1.StatefulSet-test-splunk-stack1-monitoring-console"},
{MetaName: "*v1.ConfigMap-test-splunk-test-probe-configmap"},
{MetaName: "*v1.ConfigMap-test-splunk-test-probe-configmap"},
{MetaName: "*v1.Secret-test-splunk-test-secret"},
{MetaName: "*v1.Secret-test-splunk-stack1-monitoring-console-secret-v1"},
{MetaName: "*v1.ConfigMap-test-splunk-stack1-monitoring-console"},
{MetaName: "*v1.ConfigMap-test-splunk-stack1-monitoring-console"},
{MetaName: "*v1.ConfigMap-test-splunk-stack1-monitoring-console"},
//{MetaName: "*v1.StatefulSet-test-splunk-stack1-monitoring-console"},
//{MetaName: "*v1.StatefulSet-test-splunk-stack1-monitoring-console"},
{MetaName: "*v4.MonitoringConsole-test-stack1"},
{MetaName: "*v4.MonitoringConsole-test-stack1"},
}

createFuncCalls := []spltest.MockFuncCall{
{MetaName: "*v1.Secret-test-splunk-test-secret"},
{MetaName: "*v1.Secret-test-splunk-test-secret"},
{MetaName: "*v1.Secret-test-splunk-test-secret"},
Expand Down Expand Up @@ -91,9 +111,9 @@ func TestApplyMonitoringConsole(t *testing.T) {
{MetaName: "*v1.ConfigMap-test-splunk-stack1-monitoring-console"},
{MetaName: "*v1.ConfigMap-test-splunk-stack1-monitoring-console"},
{MetaName: "*v1.ConfigMap-test-splunk-stack1-monitoring-console"},
{MetaName: "*v1.StatefulSet-test-splunk-stack1-monitoring-console"},
{MetaName: "*v1.StatefulSet-test-splunk-stack1-monitoring-console"},
{MetaName: "*v1.StatefulSet-test-splunk-stack1-monitoring-console"},
//{MetaName: "*v1.StatefulSet-test-splunk-stack1-monitoring-console"},
//{MetaName: "*v1.StatefulSet-test-splunk-stack1-monitoring-console"},
//{MetaName: "*v1.StatefulSet-test-splunk-stack1-monitoring-console"},

{MetaName: "*v4.MonitoringConsole-test-stack1"},
{MetaName: "*v4.MonitoringConsole-test-stack1"},
Expand All @@ -103,15 +123,20 @@ func TestApplyMonitoringConsole(t *testing.T) {
"app.kubernetes.io/component": "versionedSecrets",
"app.kubernetes.io/managed-by": "splunk-operator",
}

listOpts := []client.ListOption{
client.InNamespace("test"),
client.MatchingLabels(labels),
}

listmockCall := []spltest.MockFuncCall{
{ListOpts: listOpts}}
{ListOpts: listOpts},
{MetaName: "test"},
{MetaName: "test"},
}

createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {funcCalls[0], funcCalls[3], funcCalls[4], funcCalls[7], funcCalls[9], funcCalls[10], funcCalls[5]}, "Update": {funcCalls[0], funcCalls[10]}, "List": {listmockCall[0]}}
updateCalls := map[string][]spltest.MockFuncCall{"Get": updateFuncCalls, "Update": {updateFuncCalls[4]}, "List": {listmockCall[0]}}
createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": {createFuncCalls[0], createFuncCalls[3], createFuncCalls[4], createFuncCalls[7], createFuncCalls[9], createFuncCalls[10], {MetaName: "*v1.Event-test-"}}, "Update": {funcCalls[0], funcCalls[10]}, "List": listmockCall}
updateCalls := map[string][]spltest.MockFuncCall{"Get": updateFuncCalls, "Update": {}, "List": {listmockCall[0]}}

current := enterpriseApi.MonitoringConsole{
TypeMeta: metav1.TypeMeta{
Expand Down
6 changes: 3 additions & 3 deletions pkg/splunk/enterprise/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ MonitoringConsole:

for _, item := range clusterManagerList.Items {
if item.Spec.MonitoringConsoleRef.Name == cr.GetName() {
if item.Status.Phase != enterpriseApi.PhaseReady {
if item.Status.Phase != enterpriseApi.PhaseReady {
message := fmt.Sprintf("cluster manager %s is not ready ", item.Name)
return false, fmt.Errorf(message)
}
Expand All @@ -300,7 +300,7 @@ MonitoringConsole:
}
for _, item := range serachHeadClusterList.Items {
if item.Spec.MonitoringConsoleRef.Name == cr.GetName() {
if item.Status.Phase != enterpriseApi.PhaseReady {
if item.Status.Phase != enterpriseApi.PhaseReady {
message := fmt.Sprintf("search head cluster %s is not ready ", item.Name)
return false, fmt.Errorf(message)
}
Expand All @@ -323,7 +323,7 @@ MonitoringConsole:
for _, item := range standaloneList.Items {
if item.Spec.MonitoringConsoleRef.Name == cr.GetName() {
if item.Status.Phase != enterpriseApi.PhaseReady || spec.Image != item.Spec.Image {
message := fmt.Sprintf("standalone %s is not ready" , item.Name)
message := fmt.Sprintf("standalone %s is not ready", item.Name)
return false, fmt.Errorf(message)
}
}
Expand Down
64 changes: 35 additions & 29 deletions pkg/splunk/enterprise/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func xTestUpgradePathValidation(t *testing.T) {
func TestUpgradePathValidation(t *testing.T) {

builder := fake.NewClientBuilder()
client := builder.Build()
Expand Down Expand Up @@ -292,33 +292,10 @@ func xTestUpgradePathValidation(t *testing.T) {
return nil
}

// indexer is not ready as search head is not yet updated
// indexer is not ready as search head is not yet updated
_, err = ApplyIndexerClusterManager(ctx, client, &idx)
if err != nil && !k8serrors.IsNotFound(err) {
t.Errorf("ApplyIndexerClusterManager should not have returned error; err=%v", err)
}

// mointoring console statefulset is created here
_, err = ApplyMonitoringConsole(ctx, client, &mc)
if err != nil && !k8serrors.IsNotFound(err) {
t.Errorf("applyMonitoringConsole should not have returned error; err=%v", err)
}
// create pods for cluster manager
createPods(t, ctx, client, "monitoring-console", fmt.Sprintf("splunk-%s-monitoring-console-0", lm.Name), lm.Namespace, lm.Spec.Image)
updateStatefulSetsInTest(t, ctx, client, 1, fmt.Sprintf("splunk-%s-monitoring-console", lm.Name), lm.Namespace)
// mointoring console statefulset is created here
_, err = ApplyMonitoringConsole(ctx, client, &mc)
if err != nil && !k8serrors.IsNotFound(err) {
t.Errorf("applyMonitoringConsole should not have returned error; err=%v", err)
}

err = client.Get(ctx, namespacedName, &mc)
if err != nil {
t.Errorf("get should not have returned error; err=%v", err)
}

if mc.Status.Phase != enterpriseApi.PhaseReady {
t.Errorf("mc is not in ready state")
if err == nil {
t.Errorf("ApplyIndexerClusterManager should not have succeeded as search head is not ready")
}

// Monitoring console is ready now, now this should crete statefulset but statefulset is not in ready phase
Expand Down Expand Up @@ -431,6 +408,29 @@ func xTestUpgradePathValidation(t *testing.T) {
return extraEnv, err
}

// mointoring console statefulset is created here
_, err = ApplyMonitoringConsole(ctx, client, &mc)
if err != nil && !k8serrors.IsNotFound(err) {
t.Errorf("applyMonitoringConsole should not have returned error; err=%v", err)
}
// create pods for cluster manager
createPods(t, ctx, client, "monitoring-console", fmt.Sprintf("splunk-%s-monitoring-console-0", lm.Name), lm.Namespace, lm.Spec.Image)
updateStatefulSetsInTest(t, ctx, client, 1, fmt.Sprintf("splunk-%s-monitoring-console", lm.Name), lm.Namespace)
// mointoring console statefulset is created here
_, err = ApplyMonitoringConsole(ctx, client, &mc)
if err != nil && !k8serrors.IsNotFound(err) {
t.Errorf("applyMonitoringConsole should not have returned error; err=%v", err)
}

err = client.Get(ctx, namespacedName, &mc)
if err != nil {
t.Errorf("get should not have returned error; err=%v", err)
}

if mc.Status.Phase != enterpriseApi.PhaseReady {
t.Errorf("mc is not in ready state")
}

// ------- Step2 starts here -----
// Update
// standalone
Expand Down Expand Up @@ -522,9 +522,10 @@ func xTestUpgradePathValidation(t *testing.T) {
if err != nil {
t.Errorf("applyClusterManager after update should not have returned error; err=%v", err)
}
_, err = ApplyMonitoringConsole(ctx, client, &mc)

_, 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)
}

shc.Status.TelAppInstalled = true
Expand Down Expand Up @@ -609,6 +610,11 @@ func xTestUpgradePathValidation(t *testing.T) {
t.Errorf("ApplyIndexerClusterManager 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)
}

}

func createPods(t *testing.T, ctx context.Context, client common.ControllerClient, crtype, name, namespace, image string) {
Expand Down

0 comments on commit 6f87d39

Please sign in to comment.