-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract GetManifestGeneratePaths() and add tests
- Loading branch information
Showing
5 changed files
with
129 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package notification | ||
|
||
import ( | ||
argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
type Event struct { | ||
PhaseIsChanged bool | ||
HealthIsChanged bool | ||
Application argocdv1alpha1.Application | ||
ArgoCDURL string | ||
} | ||
|
||
// GetManifestGeneratePaths returns canonical paths of "argocd.argoproj.io/manifest-generate-paths" annotation. | ||
// It returns nil if the field is nil or empty. | ||
// https://argo-cd.readthedocs.io/en/stable/operator-manual/high_availability/#webhook-and-manifest-paths-annotation | ||
func (e Event) GetManifestGeneratePaths() []string { | ||
if e.Application.Annotations == nil { | ||
return nil | ||
} | ||
var canonicalPaths []string | ||
annotatedPaths := strings.Split(e.Application.Annotations["argocd.argoproj.io/manifest-generate-paths"], ";") | ||
for _, path := range annotatedPaths { | ||
// convert to absolute path | ||
absolutePath := path | ||
if !filepath.IsAbs(path) { | ||
absolutePath = filepath.Join(e.Application.Spec.Source.Path, path) | ||
} | ||
// remove leading slash | ||
if absolutePath[0:1] == "/" { | ||
absolutePath = absolutePath[1:] | ||
} | ||
// add to list of manifest paths | ||
canonicalPaths = append(canonicalPaths, absolutePath) | ||
} | ||
return canonicalPaths | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package notification | ||
|
||
import ( | ||
"github.com/google/go-cmp/cmp" | ||
"testing" | ||
) | ||
|
||
func TestEvent_GetManifestGeneratePaths(t *testing.T) { | ||
t.Run("nil annotation", func(t *testing.T) { | ||
var e Event | ||
manifestGeneratePaths := e.GetManifestGeneratePaths() | ||
if manifestGeneratePaths != nil { | ||
t.Errorf("manifestGeneratePaths wants nil but was %+v", manifestGeneratePaths) | ||
} | ||
}) | ||
|
||
t.Run("empty annotation", func(t *testing.T) { | ||
var e Event | ||
e.Application.Spec.Source.Path = "/applications/app1" | ||
e.Application.Annotations = map[string]string{ | ||
"argocd.argoproj.io/manifest-generate-paths": "", | ||
} | ||
manifestGeneratePaths := e.GetManifestGeneratePaths() | ||
if manifestGeneratePaths != nil { | ||
t.Errorf("manifestGeneratePaths wants nil but was %+v", manifestGeneratePaths) | ||
} | ||
}) | ||
|
||
t.Run("absolute path", func(t *testing.T) { | ||
var e Event | ||
e.Application.Spec.Source.Path = "/applications/app1" | ||
e.Application.Annotations = map[string]string{ | ||
"argocd.argoproj.io/manifest-generate-paths": "/components/app1", | ||
} | ||
manifestGeneratePaths := e.GetManifestGeneratePaths() | ||
want := []string{"components/app1"} | ||
if diff := cmp.Diff(want, manifestGeneratePaths); diff != "" { | ||
t.Errorf("want != manifestGeneratePaths:\n%s", diff) | ||
} | ||
}) | ||
|
||
t.Run("relative path of ascendant", func(t *testing.T) { | ||
var e Event | ||
e.Application.Spec.Source.Path = "/applications/app1" | ||
e.Application.Annotations = map[string]string{ | ||
"argocd.argoproj.io/manifest-generate-paths": "../manifests1", | ||
} | ||
manifestGeneratePaths := e.GetManifestGeneratePaths() | ||
want := []string{"applications/manifests1"} | ||
if diff := cmp.Diff(want, manifestGeneratePaths); diff != "" { | ||
t.Errorf("want != manifestGeneratePaths:\n%s", diff) | ||
} | ||
}) | ||
|
||
t.Run("relative path of period", func(t *testing.T) { | ||
var e Event | ||
e.Application.Spec.Source.Path = "/applications/app1" | ||
e.Application.Annotations = map[string]string{ | ||
"argocd.argoproj.io/manifest-generate-paths": ".", | ||
} | ||
manifestGeneratePaths := e.GetManifestGeneratePaths() | ||
want := []string{"applications/app1"} | ||
if diff := cmp.Diff(want, manifestGeneratePaths); diff != "" { | ||
t.Errorf("want != manifestGeneratePaths:\n%s", diff) | ||
} | ||
}) | ||
|
||
t.Run("multiple paths", func(t *testing.T) { | ||
var e Event | ||
e.Application.Spec.Source.Path = "/applications/app1" | ||
e.Application.Annotations = map[string]string{ | ||
"argocd.argoproj.io/manifest-generate-paths": ".;../manifests1", | ||
} | ||
manifestGeneratePaths := e.GetManifestGeneratePaths() | ||
want := []string{ | ||
"applications/app1", | ||
"applications/manifests1", | ||
} | ||
if diff := cmp.Diff(want, manifestGeneratePaths); diff != "" { | ||
t.Errorf("want != manifestGeneratePaths:\n%s", diff) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters