Skip to content

Commit

Permalink
Fix the test errors
Browse files Browse the repository at this point in the history
Signed-off-by: mehabhalodiya <mehabhalodiya@gmail.com>
  • Loading branch information
mehabhalodiya committed Feb 22, 2024
1 parent 210b9f9 commit 2752b66
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 118 deletions.
41 changes: 41 additions & 0 deletions controllers/argocd/statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,47 @@ func TestReconcileArgoCD_reconcileApplicationController_withAppSync(t *testing.T
}
}

func TestReconcileArgoCD_reconcileApplicationController_withEnv(t *testing.T) {

expectedEnv := []corev1.EnvVar{
{Name: "CUSTOM_ENV_VAR", Value: "custom-value"},
{Name: "HOME", Value: "/home/argocd"},
}

a := makeTestArgoCD(func(a *argoproj.ArgoCD) {
// Assuming spec.controller.env is a slice
a.Spec.Controller.Env = []corev1.EnvVar{
{Name: "CUSTOM_ENV_VAR", Value: "custom-value"},
}
})

resObjs := []client.Object{a}
subresObjs := []client.Object{a}
runtimeObjs := []runtime.Object{}
sch := makeTestReconcilerScheme(argoproj.AddToScheme)
cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs)
r := makeTestReconciler(cl, sch)

assert.NoError(t, r.reconcileApplicationControllerStatefulSet(a, false))

ss := &appsv1.StatefulSet{}
assert.NoError(t, r.Client.Get(
context.TODO(),
types.NamespacedName{
Name: "argocd-application-controller",
Namespace: a.Namespace,
},
ss))

env := ss.Spec.Template.Spec.Containers[0].Env

diffEnv := cmp.Diff(env, expectedEnv)

if diffEnv != "" {
t.Fatalf("Reconciliation of EnvVars failed:\n%s", diffEnv)
}
}

func Test_UpdateNodePlacementStateful(t *testing.T) {

ss := &appsv1.StatefulSet{
Expand Down
120 changes: 2 additions & 118 deletions controllers/argocd/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ func appSync(s int) argoCDOpt {
}
}

func setEnv(e []v1.EnvVar) argoCDOpt {
func setEnv(e v1.EnvVar) argoCDOpt {
return func(a *argoproj.ArgoCD) {
a.Spec.Controller.Env = e
a.Spec.Controller.Env = []v1.EnvVar{e}
}
}

Expand Down Expand Up @@ -550,122 +550,6 @@ func TestGetArgoApplicationControllerCommand(t *testing.T) {
}
}

type EnvVar struct {
Spec struct {
Controller struct {
Env []interface{} `json:"env" env:"env"`
} `json:"controller" controller:"controller"`
}
}

type ArgoCD struct {
Spec struct {
Controller struct {
Env []interface{} `json:"env"`
} `json:"controller"`
}
}

func TestArgoCDControllerEnv(t *testing.T) {

validEnv := []v1.EnvVar{
v1.EnvVar{Name: "HOME", Value: "/home/argocd", ValueFrom: (*v1.EnvVarSource)(nil)}}

_ = []struct {
name string
opts []argoCDOpt
want []v1.EnvVar
}{
{
"configured setenv to test",
[]argoCDOpt{setEnv(validEnv)},
validEnv,
},
}

// Scenario 1: Valid case with two environment variables
validArgoCDInstance := ArgoCD{
Spec: struct {
Controller struct {
Env []interface{} `json:"env"`
} `json:"controller"`
}{
Controller: struct {
Env []interface{} `json:"env"`
}{
Env: []interface{}{"test1=value1", "test2=value2"},
},
},
}

// Assert the length and values of .spec.controller.env
if len(validArgoCDInstance.Spec.Controller.Env) != 2 {
t.Errorf("Expected 2 elements in .spec.controller.env, but got %d", len(validArgoCDInstance.Spec.Controller.Env))
}

// Assert specific values in the env slice
if validArgoCDInstance.Spec.Controller.Env[0] != "test1=value1" {
t.Errorf("Expected .spec.controller.env[0] to be 'test1=value1', but got '%s'", validArgoCDInstance.Spec.Controller.Env[0])
}

if validArgoCDInstance.Spec.Controller.Env[1] != "test2=value2" {
t.Errorf("Expected .spec.controller.env[1] to be 'test2=value2', but got '%s'", validArgoCDInstance.Spec.Controller.Env[1])
}

// Scenario 2: Empty env slice
emptyArgoCDInstance := ArgoCD{
Spec: struct {
Controller struct {
Env []interface{} `json:"env"`
} `json:"controller"`
}{},
}

// Assert that .spec.controller.env is empty
if len(emptyArgoCDInstance.Spec.Controller.Env) != 0 {
t.Errorf("Expected an empty .spec.controller.env, but got %v", emptyArgoCDInstance.Spec.Controller.Env)
}

// Scenario 3: Invalid case with non-string values in env slice
invalidArgoCDInstance := ArgoCD{
Spec: struct {
Controller struct {
Env []interface{} `json:"env"`
} `json:"controller"`
}{
Controller: struct {
Env []interface{} `json:"env"`
}{
Env: []interface{}{42, true},
},
},
}

// Assert that .spec.controller.env is an empty slice
if len(invalidArgoCDInstance.Spec.Controller.Env) != 0 {
t.Errorf("Expected an empty .spec.controller.env due to invalid values, but got %v", invalidArgoCDInstance.Spec.Controller.Env)
}

// Scenario 4: Cases where .spec.controller.env is nil
nilArgoCDInstance := ArgoCD{
Spec: struct {
Controller struct {
Env []interface{} `json:"env"`
} `json:"controller"`
}{
Controller: struct {
Env []interface{} `json:"env"`
}{},
},
}

// Assert that .spec.controller.env is nil
if !reflect.DeepEqual(nilArgoCDInstance.Spec.Controller.Env, []string{}) {
t.Errorf("Expected .spec.controller.env to be nil, but got %v", nilArgoCDInstance.Spec.Controller.Env)
}

}

func TestGetArgoApplicationContainerEnv(t *testing.T) {

sync60s := []v1.EnvVar{
Expand Down

0 comments on commit 2752b66

Please sign in to comment.