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

feat: Add tests for app cmd #8872

Merged
merged 4 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions cmd/argocd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2169,7 +2169,7 @@ func NewApplicationPatchCommand(clientOpts *argocdclient.ClientOptions) *cobra.C
return &command
}

func filterResources(command *cobra.Command, resources []*argoappv1.ResourceDiff, group, kind, namespace, resourceName string, all bool) []*unstructured.Unstructured {
func filterResources(groupChanged bool, resources []*argoappv1.ResourceDiff, group, kind, namespace, resourceName string, all bool) []*unstructured.Unstructured {
liveObjs, err := liveObjects(resources)
errors.CheckError(err)
filteredObjects := make([]*unstructured.Unstructured, 0)
Expand All @@ -2179,7 +2179,7 @@ func filterResources(command *cobra.Command, resources []*argoappv1.ResourceDiff
continue
}
gvk := obj.GroupVersionKind()
if command.Flags().Changed("group") && group != gvk.Group {
if groupChanged && group != gvk.Group {
continue
}
if namespace != "" && namespace != obj.GetNamespace() {
Expand Down Expand Up @@ -2239,7 +2239,7 @@ func NewApplicationPatchResourceCommand(clientOpts *argocdclient.ClientOptions)
ctx := context.Background()
resources, err := appIf.ManagedResources(ctx, &applicationpkg.ResourcesQuery{ApplicationName: &appName})
errors.CheckError(err)
objectsToPatch := filterResources(command, resources.Items, group, kind, namespace, resourceName, all)
objectsToPatch := filterResources(command.Flags().Changed("group"), resources.Items, group, kind, namespace, resourceName, all)
for i := range objectsToPatch {
obj := objectsToPatch[i]
gvk := obj.GroupVersionKind()
Expand Down Expand Up @@ -2295,7 +2295,7 @@ func NewApplicationDeleteResourceCommand(clientOpts *argocdclient.ClientOptions)
ctx := context.Background()
resources, err := appIf.ManagedResources(ctx, &applicationpkg.ResourcesQuery{ApplicationName: &appName})
errors.CheckError(err)
objectsToDelete := filterResources(command, resources.Items, group, kind, namespace, resourceName, all)
objectsToDelete := filterResources(command.Flags().Changed("group"), resources.Items, group, kind, namespace, resourceName, all)
for i := range objectsToDelete {
obj := objectsToDelete[i]
gvk := obj.GroupVersionKind()
Expand Down
4 changes: 2 additions & 2 deletions cmd/argocd/commands/app_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func NewApplicationResourceActionsListCommand(clientOpts *argocdclient.ClientOpt
ctx := context.Background()
resources, err := appIf.ManagedResources(ctx, &applicationpkg.ResourcesQuery{ApplicationName: &appName})
errors.CheckError(err)
filteredObjects := filterResources(command, resources.Items, group, kind, namespace, resourceName, true)
filteredObjects := filterResources(command.Flags().Changed("group"), resources.Items, group, kind, namespace, resourceName, true)
var availableActions []DisplayedAction
for i := range filteredObjects {
obj := filteredObjects[i]
Expand Down Expand Up @@ -148,7 +148,7 @@ func NewApplicationResourceActionsRunCommand(clientOpts *argocdclient.ClientOpti
ctx := context.Background()
resources, err := appIf.ManagedResources(ctx, &applicationpkg.ResourcesQuery{ApplicationName: &appName})
errors.CheckError(err)
filteredObjects := filterResources(command, resources.Items, group, kind, namespace, resourceName, all)
filteredObjects := filterResources(command.Flags().Changed("group"), resources.Items, group, kind, namespace, resourceName, all)
var resGroup = filteredObjects[0].GroupVersionKind().Group
for i := range filteredObjects[1:] {
if filteredObjects[i].GroupVersionKind().Group != resGroup {
Expand Down
57 changes: 57 additions & 0 deletions cmd/argocd/commands/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,60 @@ func TestFindRevisionHistoryWithPassedIdThatNotExist(t *testing.T) {
}

}

func TestFilterResources(t *testing.T) {

t.Run("Filter by ns", func(t *testing.T) {

resources := []*v1alpha1.ResourceDiff{
{
LiveState: "{\"apiVersion\":\"v1\",\"kind\":\"Service\",\"metadata\":{\"name\":\"test-helm-guestbook\",\"namespace\":\"argocd\"},\"spec\":{\"selector\":{\"app\":\"helm-guestbook\",\"release\":\"test\"},\"sessionAffinity\":\"None\",\"type\":\"ClusterIP\"},\"status\":{\"loadBalancer\":{}}}",
},
{
LiveState: "{\"apiVersion\":\"v1\",\"kind\":\"Service\",\"metadata\":{\"name\":\"test-helm-guestbook\",\"namespace\":\"ns\"},\"spec\":{\"selector\":{\"app\":\"helm-guestbook\",\"release\":\"test\"},\"sessionAffinity\":\"None\",\"type\":\"ClusterIP\"},\"status\":{\"loadBalancer\":{}}}",
},
}

filteredResources := filterResources(false, resources, "g", "Service", "ns", "test-helm-guestbook", true)
if len(filteredResources) != 1 {
t.Fatal("Incorrect number of resources after filter")
}

})

t.Run("Filter by kind", func(t *testing.T) {

resources := []*v1alpha1.ResourceDiff{
{
LiveState: "{\"apiVersion\":\"v1\",\"kind\":\"Service\",\"metadata\":{\"name\":\"test-helm-guestbook\",\"namespace\":\"argocd\"},\"spec\":{\"selector\":{\"app\":\"helm-guestbook\",\"release\":\"test\"},\"sessionAffinity\":\"None\",\"type\":\"ClusterIP\"},\"status\":{\"loadBalancer\":{}}}",
},
{
LiveState: "{\"apiVersion\":\"v1\",\"kind\":\"Deployment\",\"metadata\":{\"name\":\"test-helm-guestbook\",\"namespace\":\"argocd\"},\"spec\":{\"selector\":{\"app\":\"helm-guestbook\",\"release\":\"test\"},\"sessionAffinity\":\"None\",\"type\":\"ClusterIP\"},\"status\":{\"loadBalancer\":{}}}",
},
}

filteredResources := filterResources(false, resources, "g", "Deployment", "argocd", "test-helm-guestbook", true)
if len(filteredResources) != 1 {
t.Fatal("Incorrect number of resources after filter")
}

})

t.Run("Filter by name", func(t *testing.T) {

resources := []*v1alpha1.ResourceDiff{
{
LiveState: "{\"apiVersion\":\"v1\",\"kind\":\"Service\",\"metadata\":{\"name\":\"test-helm-guestbook\",\"namespace\":\"argocd\"},\"spec\":{\"selector\":{\"app\":\"helm-guestbook\",\"release\":\"test\"},\"sessionAffinity\":\"None\",\"type\":\"ClusterIP\"},\"status\":{\"loadBalancer\":{}}}",
},
{
LiveState: "{\"apiVersion\":\"v1\",\"kind\":\"Service\",\"metadata\":{\"name\":\"test-helm\",\"namespace\":\"argocd\"},\"spec\":{\"selector\":{\"app\":\"helm-guestbook\",\"release\":\"test\"},\"sessionAffinity\":\"None\",\"type\":\"ClusterIP\"},\"status\":{\"loadBalancer\":{}}}",
},
}

filteredResources := filterResources(false, resources, "g", "Service", "argocd", "test-helm", true)
if len(filteredResources) != 1 {
t.Fatal("Incorrect number of resources after filter")
}

})
}