Skip to content

Commit

Permalink
Finished unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Yi Cai <[email protected]>
  • Loading branch information
ciiay committed Sep 29, 2023
1 parent 67d0fcc commit c04d657
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 10 deletions.
2 changes: 0 additions & 2 deletions common/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ const (

// K8sOSLinux is the value for kubernetes.io/os key for linux pods
K8sOSLinux = "linux"
// ArgoCDAppSetGitlabSCMTLSCertsConfigMapName is the hard-coded ApplicationSet Gitlab SCM TLS certificate data ConfigMap name.
ArgoCDAppSetGitlabSCMTLSCertsConfigMapName = "argocd-appset-gitlab-scm-tls-certs-cm"

// ArgoCDRedisServerTLSSecretName is the name of the TLS secret for the redis-server
ArgoCDRedisServerTLSSecretName = "argocd-operator-redis-tls"
Expand Down
11 changes: 7 additions & 4 deletions controllers/argocd/applicationset/applicationset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/argoproj-labs/argocd-operator/api/v1beta1"
argoproj "github.com/argoproj-labs/argocd-operator/api/v1beta1"
"github.com/argoproj-labs/argocd-operator/common"
"github.com/argoproj-labs/argocd-operator/controllers/argocd/argocdcommon"
"github.com/stretchr/testify/assert"
Expand All @@ -23,9 +24,11 @@ func makeTestApplicationSetReconciler(t *testing.T, objs ...runtime.Object) *App
logger := ctrl.Log.WithName(ArgoCDApplicationSetControllerComponent)

return &ApplicationSetReconciler{
Client: cl,
Scheme: s,
Instance: argocdcommon.MakeTestArgoCD(),
Logger: logger,
Client: cl,
Scheme: s,
Instance: argocdcommon.MakeTestArgoCD(func(a *argoproj.ArgoCD) {
a.Spec.ApplicationSet = &argoproj.ArgoCDApplicationSet{}
}),
Logger: logger,
}
}
3 changes: 0 additions & 3 deletions controllers/argocd/applicationset/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ import (
func (asr *ApplicationSetReconciler) getDesiredDeployment() *appsv1.Deployment {
desiredDeployment := &appsv1.Deployment{}

appSetEnv := asr.Instance.Spec.ApplicationSet.Env
appSetEnv = util.EnvMerge(appSetEnv, util.ProxyEnvVars(), false)

objMeta := metav1.ObjectMeta{
Name: resourceName,
Namespace: asr.Instance.Namespace,
Expand Down
94 changes: 94 additions & 0 deletions controllers/argocd/applicationset/deployment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package applicationset

import (
"context"
"testing"

"github.com/argoproj-labs/argocd-operator/controllers/argocd/argocdcommon"
"github.com/stretchr/testify/assert"

appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/types"
)

func TestApplicationSetReconciler_reconcileDeployment(t *testing.T) {
resourceName = argocdcommon.TestArgoCDName
resourceLabels = testExpectedLabels
ns := argocdcommon.MakeTestNamespace()
asr := makeTestApplicationSetReconciler(t, ns)

existingDeployment := asr.getDesiredDeployment()

tests := []struct {
name string
setupClient func() *ApplicationSetReconciler
wantErr bool
}{
{
name: "create a deployment",
setupClient: func() *ApplicationSetReconciler {
return makeTestApplicationSetReconciler(t, ns)
},
wantErr: false,
},
{
name: "update a deployment",
setupClient: func() *ApplicationSetReconciler {
outdatedDeployment := existingDeployment
outdatedDeployment.ObjectMeta.Labels = argocdcommon.TestKVP
return makeTestApplicationSetReconciler(t, outdatedDeployment, ns)
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
asr := tt.setupClient()
err := asr.reconcileDeployment()
if (err != nil) != tt.wantErr {
if tt.wantErr {
t.Errorf("Expected error but did not get one")
} else {
t.Errorf("Unexpected error: %v", err)
}
}

updatedDeployment := &appsv1.Deployment{}
err = asr.Client.Get(context.TODO(), types.NamespacedName{Name: resourceName, Namespace: argocdcommon.TestNamespace}, updatedDeployment)
if err != nil {
t.Fatalf("Could not get updated Deployment: %v", err)
}
assert.Equal(t, testExpectedLabels, updatedDeployment.ObjectMeta.Labels)
})
}
}

func TestApplicationSetReconciler_DeleteDeployment(t *testing.T) {
ns := argocdcommon.MakeTestNamespace()
resourceName = argocdcommon.TestArgoCDName
tests := []struct {
name string
setupClient func() *ApplicationSetReconciler
wantErr bool
}{
{
name: "successful delete",
setupClient: func() *ApplicationSetReconciler {
return makeTestApplicationSetReconciler(t, ns)
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
nr := tt.setupClient()
if err := nr.DeleteDeployment(resourceName, ns.Name); (err != nil) != tt.wantErr {
if tt.wantErr {
t.Errorf("Expected error but did not get one")
} else {
t.Errorf("Unexpected error: %v", err)
}
}
})
}
}
2 changes: 1 addition & 1 deletion controllers/argocd/applicationset/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func isMergable(extraArgs []string, cmd []string) error {
for _, arg := range extraArgs {
if len(arg) > 2 && arg[:2] == "--" && util.ContainsString(cmd, arg) {
return fmt.Errorf("Arg %s is already part of the default command arguments", arg)
return fmt.Errorf("arg %s is already part of the default command arguments", arg)
}
}
return nil
Expand Down

0 comments on commit c04d657

Please sign in to comment.