Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sd 766 telefonistka get nil pointer dereference when it tries to diff a new application #27

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/pkg/argocd/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions internal/pkg/argocd/argocd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package argocd
import (
"bytes"
"context"
"log"
"os"
"strings"
"testing"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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": "wrong-path",
},
Name: "right-app",
},
Spec: argoappv1.ApplicationSpec{
Source: &argoappv1.ApplicationSource{
RepoURL: "",
Path: "right/",
},
},
},
},
}

mockApplicationClient.EXPECT().List(gomock.Any(), gomock.Any()).Return(expectedResponse, nil)
app, err := findArgocdAppByManifestPathAnnotation(ctx, "right/path", "some-repo", mockApplicationClient)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the reason this doesn't return an app because right/ != right/path?
If so I would use a different string to make it extra clear

Copy link
Collaborator

@Oded-B Oded-B Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like "Non existing app path" or something like that

if err != nil {
t.Errorf("Error: %v", err)
}
if app != nil {
log.Fatal("expected the application to be nil")
}
}
4 changes: 4 additions & 0 deletions internal/pkg/githubapi/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions internal/pkg/testutils/testutils.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading