Skip to content

Commit

Permalink
add unit tests for cmd/main.go and cmd/run.go
Browse files Browse the repository at this point in the history
Signed-off-by: Cheng Fang <[email protected]>
  • Loading branch information
chengfang committed Jul 1, 2024
1 parent bb6280d commit 78fafe2
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions cmd/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"os"
"testing"

"github.com/argoproj-labs/argocd-image-updater/pkg/env"

Check failure on line 7 in cmd/run_test.go

View workflow job for this annotation

GitHub Actions / Ensure code is correctly linted

File is not `goimports`-ed with -local github.com/argoproj-labs/argocd-image-updater (goimports)
"github.com/stretchr/testify/assert"
)

// TestNewRunCommand tests various flags and their default values.
func TestNewRunCommand(t *testing.T) {
asser := assert.New(t)
runCmd := newRunCommand()
asser.Contains(runCmd.Use, "run")
asser.Greater(len(runCmd.Short), 25)
asser.NotNil(runCmd.RunE)
asser.Equal(env.GetStringVal("APPLICATIONS_API", applicationsAPIKindK8S), runCmd.Flag("applications-api").Value.String())
asser.Equal(env.GetStringVal("ARGOCD_SERVER", ""), runCmd.Flag("argocd-server-addr").Value.String())
asser.Equal(env.GetStringVal("ARGOCD_GRPC_WEB", "false"), runCmd.Flag("argocd-grpc-web").Value.String())
asser.Equal(env.GetStringVal("ARGOCD_INSECURE", "false"), runCmd.Flag("argocd-insecure").Value.String())
asser.Equal(env.GetStringVal("ARGOCD_PLAINTEXT", "false"), runCmd.Flag("argocd-plaintext").Value.String())
asser.Equal("", runCmd.Flag("argocd-auth-token").Value.String())
asser.Equal("false", runCmd.Flag("dry-run").Value.String())
asser.Equal("2m0s", runCmd.Flag("interval").Value.String())
asser.Equal(env.GetStringVal("IMAGE_UPDATER_LOGLEVEL", "info"), runCmd.Flag("loglevel").Value.String())
asser.Equal("", runCmd.Flag("kubeconfig").Value.String())
asser.Equal("8080", runCmd.Flag("health-port").Value.String())
asser.Equal("8081", runCmd.Flag("metrics-port").Value.String())
asser.Equal("false", runCmd.Flag("once").Value.String())
asser.Equal(defaultRegistriesConfPath, runCmd.Flag("registries-conf-path").Value.String())
asser.Equal("false", runCmd.Flag("disable-kubernetes").Value.String())
asser.Equal("10", runCmd.Flag("max-concurrency").Value.String())
asser.Equal("", runCmd.Flag("argocd-namespace").Value.String())
asser.Equal("[]", runCmd.Flag("match-application-name").Value.String())
asser.Equal("", runCmd.Flag("match-application-label").Value.String())
asser.Equal("true", runCmd.Flag("warmup-cache").Value.String())
asser.Equal(env.GetStringVal("GIT_COMMIT_USER", "argocd-image-updater"), runCmd.Flag("git-commit-user").Value.String())
asser.Equal(env.GetStringVal("GIT_COMMIT_EMAIL", "[email protected]"), runCmd.Flag("git-commit-email").Value.String())
asser.Equal(env.GetStringVal("GIT_COMMIT_SIGNING_KEY", ""), runCmd.Flag("git-commit-signing-key").Value.String())
asser.Equal(env.GetStringVal("GIT_COMMIT_SIGNING_METHOD", "openpgp"), runCmd.Flag("git-commit-signing-method").Value.String())
asser.Equal(env.GetStringVal("GIT_COMMIT_SIGN_OFF", "false"), runCmd.Flag("git-commit-sign-off").Value.String())
asser.Equal(defaultCommitTemplatePath, runCmd.Flag("git-commit-message-path").Value.String())
asser.Equal(env.GetStringVal("IMAGE_UPDATER_KUBE_EVENTS", "false"), runCmd.Flag("disable-kube-events").Value.String())

asser.Nil(runCmd.Help())
}

// TestRootCmd tests main.go#newRootCommand.
func TestRootCmd(t *testing.T) {
//remove the last element from os.Args so that it will not be taken as the arg to the image-updater command
os.Args = os.Args[:len(os.Args)-1]
err := newRootCommand()
assert.Nil(t, err)
}

0 comments on commit 78fafe2

Please sign in to comment.