Skip to content

Commit caaf793

Browse files
Sd 766 telefonistka get nil pointer dereference when it tries to diff a new application (#27)
* add nil condition to found app to prevent nil references * add templates path to generate argo cd diff comments test * add nolint * separate error and nil conditions for clarity * add a unit test to verify the nil return value * update test for clarity
1 parent a8a51a9 commit caaf793

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

internal/pkg/argocd/argocd.go

+3
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,9 @@ func SetArgoCDAppRevision(ctx context.Context, componentPath string, revision st
327327
if err != nil {
328328
return fmt.Errorf("error finding ArgoCD application for component path %s: %w", componentPath, err)
329329
}
330+
if foundApp == nil {
331+
return fmt.Errorf("no ArgoCD application was found for component path: %s", componentPath)
332+
}
330333
if foundApp.Spec.Source.TargetRevision == revision {
331334
log.Infof("App %s already has revision %s", foundApp.Name, revision)
332335
return nil

internal/pkg/argocd/argocd_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package argocd
33
import (
44
"bytes"
55
"context"
6+
"log"
67
"os"
78
"strings"
89
"testing"
@@ -11,6 +12,7 @@ import (
1112
argoappv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
1213
"github.com/golang/mock/gomock"
1314
"github.com/wayfair-incubator/telefonistka/internal/pkg/mocks"
15+
"github.com/wayfair-incubator/telefonistka/internal/pkg/testutils"
1416
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1517
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1618
)
@@ -273,3 +275,39 @@ func TestFindArgocdAppByPathAnnotationRelative2(t *testing.T) {
273275
t.Errorf("App name is not right-app")
274276
}
275277
}
278+
279+
func TestFindArgocdAppByPathAnnotationNotFound(t *testing.T) {
280+
t.Parallel()
281+
defer testutils.Quiet()()
282+
ctx := context.Background()
283+
ctrl := gomock.NewController(t)
284+
defer ctrl.Finish()
285+
mockApplicationClient := mocks.NewMockApplicationServiceClient(ctrl)
286+
expectedResponse := &argoappv1.ApplicationList{
287+
Items: []argoappv1.Application{
288+
{
289+
ObjectMeta: metav1.ObjectMeta{
290+
Annotations: map[string]string{
291+
"argocd.argoproj.io/manifest-generate-paths": "non-existing-path",
292+
},
293+
Name: "non-existing-app",
294+
},
295+
Spec: argoappv1.ApplicationSpec{
296+
Source: &argoappv1.ApplicationSource{
297+
RepoURL: "",
298+
Path: "non-existing/",
299+
},
300+
},
301+
},
302+
},
303+
}
304+
305+
mockApplicationClient.EXPECT().List(gomock.Any(), gomock.Any()).Return(expectedResponse, nil)
306+
app, err := findArgocdAppByManifestPathAnnotation(ctx, "non-existing/path", "some-repo", mockApplicationClient)
307+
if err != nil {
308+
t.Errorf("Error: %v", err)
309+
}
310+
if app != nil {
311+
log.Fatal("expected the application to be nil")
312+
}
313+
}

internal/pkg/githubapi/github_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ func TestGenerateArgoCdDiffComments(t *testing.T) {
187187
},
188188
}
189189

190+
if err := os.Setenv("TEMPLATES_PATH", "../../../templates/"); err != nil { //nolint:tenv
191+
t.Fatal(err)
192+
}
193+
190194
for name, tc := range tests {
191195
tc := tc // capture range variable
192196
name := name

internal/pkg/testutils/testutils.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package testutils
2+
3+
import (
4+
"io"
5+
"os"
6+
7+
log "github.com/sirupsen/logrus"
8+
)
9+
10+
// Quiet suppresses logs when running go test.
11+
func Quiet() func() {
12+
log.SetOutput(io.Discard)
13+
return func() {
14+
log.SetOutput(os.Stdout)
15+
}
16+
}

0 commit comments

Comments
 (0)