Skip to content

Commit

Permalink
feat: Add tests for app cmd (#8872)
Browse files Browse the repository at this point in the history
* add to approvers

Signed-off-by: pashavictorovich <[email protected]>

* test filter resources in app cmd

Signed-off-by: pashavictorovich <[email protected]>
  • Loading branch information
pasha-codefresh authored Mar 23, 2022
1 parent e370813 commit d71cddd
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
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")
}

})
}

0 comments on commit d71cddd

Please sign in to comment.