diff --git a/internal/pkg/argocd/argocd.go b/internal/pkg/argocd/argocd.go index c4ef8039..e1dbf96a 100644 --- a/internal/pkg/argocd/argocd.go +++ b/internal/pkg/argocd/argocd.go @@ -327,6 +327,9 @@ func SetArgoCDAppRevision(ctx context.Context, componentPath string, revision st if err != nil { return fmt.Errorf("error finding ArgoCD application for component path %s: %w", componentPath, err) } + if foundApp == nil { + return fmt.Errorf("no ArgoCD application was found for component path: %s", componentPath) + } if foundApp.Spec.Source.TargetRevision == revision { log.Infof("App %s already has revision %s", foundApp.Name, revision) return nil diff --git a/internal/pkg/argocd/argocd_test.go b/internal/pkg/argocd/argocd_test.go index dc78cecc..80e094d9 100644 --- a/internal/pkg/argocd/argocd_test.go +++ b/internal/pkg/argocd/argocd_test.go @@ -3,6 +3,7 @@ package argocd import ( "bytes" "context" + "log" "os" "strings" "testing" @@ -11,6 +12,7 @@ import ( argoappv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" "github.com/golang/mock/gomock" "github.com/wayfair-incubator/telefonistka/internal/pkg/mocks" + "github.com/wayfair-incubator/telefonistka/internal/pkg/testutils" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" ) @@ -273,3 +275,39 @@ func TestFindArgocdAppByPathAnnotationRelative2(t *testing.T) { t.Errorf("App name is not right-app") } } + +func TestFindArgocdAppByPathAnnotationNotFound(t *testing.T) { + t.Parallel() + defer testutils.Quiet()() + ctx := context.Background() + ctrl := gomock.NewController(t) + defer ctrl.Finish() + mockApplicationClient := mocks.NewMockApplicationServiceClient(ctrl) + expectedResponse := &argoappv1.ApplicationList{ + Items: []argoappv1.Application{ + { + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + "argocd.argoproj.io/manifest-generate-paths": "non-existing-path", + }, + Name: "non-existing-app", + }, + Spec: argoappv1.ApplicationSpec{ + Source: &argoappv1.ApplicationSource{ + RepoURL: "", + Path: "non-existing/", + }, + }, + }, + }, + } + + mockApplicationClient.EXPECT().List(gomock.Any(), gomock.Any()).Return(expectedResponse, nil) + app, err := findArgocdAppByManifestPathAnnotation(ctx, "non-existing/path", "some-repo", mockApplicationClient) + if err != nil { + t.Errorf("Error: %v", err) + } + if app != nil { + log.Fatal("expected the application to be nil") + } +} diff --git a/internal/pkg/githubapi/github_test.go b/internal/pkg/githubapi/github_test.go index a2d965aa..1527c75a 100644 --- a/internal/pkg/githubapi/github_test.go +++ b/internal/pkg/githubapi/github_test.go @@ -186,6 +186,10 @@ func TestGenerateArgoCdDiffComments(t *testing.T) { }, } + if err := os.Setenv("TEMPLATES_PATH", "../../../templates/"); err != nil { //nolint:tenv + t.Fatal(err) + } + for name, tc := range tests { tc := tc // capture range variable name := name diff --git a/internal/pkg/testutils/testutils.go b/internal/pkg/testutils/testutils.go new file mode 100644 index 00000000..b7673a67 --- /dev/null +++ b/internal/pkg/testutils/testutils.go @@ -0,0 +1,16 @@ +package testutils + +import ( + "io" + "os" + + log "github.com/sirupsen/logrus" +) + +// Quiet suppresses logs when running go test. +func Quiet() func() { + log.SetOutput(io.Discard) + return func() { + log.SetOutput(os.Stdout) + } +}