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

chore: resource state tests #9307

Merged
merged 2 commits into from
May 10, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/argocd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1684,6 +1684,7 @@ func groupResourceStates(app *argoappv1.Application, selectedResources []*argoap
return resStates
}

// check if resource health, sync and operation statuses matches watch options
func checkResourceStatus(watch watchOpts, healthStatus string, syncStatus string, operationStatus *argoappv1.Operation) bool {
healthCheckPassed := true
if watch.suspended && watch.health {
Expand Down
145 changes: 145 additions & 0 deletions cmd/argocd/commands/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1070,3 +1070,148 @@ func TestPrintApplicationTableWide(t *testing.T) {
expectation := "NAME CLUSTER NAMESPACE PROJECT STATUS HEALTH SYNCPOLICY CONDITIONS REPO PATH TARGET\napp-name http://localhost:8080 default prj OutOfSync Healthy <none> <none> https://github.com/argoproj/argocd-example-apps guestbook 123\napp-name http://localhost:8080 default prj OutOfSync Healthy <none> <none> https://github.com/argoproj/argocd-example-apps guestbook 123\n"
assert.Equal(t, output, expectation)
}

func TestResourceStateKey(t *testing.T) {
rst := resourceState{
Group: "group",
Kind: "kind",
Namespace: "namespace",
Name: "name",
}

key := rst.Key()
assert.Equal(t, "group/kind/namespace/name", key)
}

func TestFormatItems(t *testing.T) {
rst := resourceState{
Group: "group",
Kind: "kind",
Namespace: "namespace",
Name: "name",
Status: "status",
Health: "health",
Hook: "hook",
Message: "message",
}
items := rst.FormatItems()
assert.Equal(t, "group", items[1])
assert.Equal(t, "kind", items[2])
assert.Equal(t, "namespace", items[3])
assert.Equal(t, "name", items[4])
assert.Equal(t, "status", items[5])
assert.Equal(t, "health", items[6])
assert.Equal(t, "hook", items[7])
assert.Equal(t, "message", items[8])

}

func TestMerge(t *testing.T) {
rst := resourceState{
Group: "group",
Kind: "kind",
Namespace: "namespace",
Name: "name",
Status: "status",
Health: "health",
Hook: "hook",
Message: "message",
}

rstNew := resourceState{
Group: "group",
Kind: "kind",
Namespace: "namespace",
Name: "name",
Status: "status",
Health: "health",
Hook: "hook2",
Message: "message2",
}

updated := rst.Merge(&rstNew)
assert.True(t, updated)
assert.Equal(t, rstNew.Hook, rst.Hook)
assert.Equal(t, rstNew.Message, rst.Message)
assert.Equal(t, rstNew.Status, rst.Status)
}

func TestMergeWitoutUpdate(t *testing.T) {
rst := resourceState{
Group: "group",
Kind: "kind",
Namespace: "namespace",
Name: "name",
Status: "status",
Health: "health",
Hook: "hook",
Message: "message",
}

rstNew := resourceState{
Group: "group",
Kind: "kind",
Namespace: "namespace",
Name: "name",
Status: "status",
Health: "health",
Hook: "hook",
Message: "message",
}

updated := rst.Merge(&rstNew)
assert.False(t, updated)
}

func TestCheckResourceStatus(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not required but would be nice: could you add a docstring to the checkResourceStatus function? It's not immediately obvious what it's meant to do.

t.Run("Suspended and health status passed", func(t *testing.T) {
res := checkResourceStatus(watchOpts{
suspended: true,
health: true,
}, string(health.HealthStatusHealthy), string(v1alpha1.SyncStatusCodeSynced), &v1alpha1.Operation{})
assert.True(t, res)
})
t.Run("Suspended and health status failed", func(t *testing.T) {
res := checkResourceStatus(watchOpts{
suspended: true,
health: true,
}, string(health.HealthStatusProgressing), string(v1alpha1.SyncStatusCodeSynced), &v1alpha1.Operation{})
assert.False(t, res)
})
t.Run("Suspended passed", func(t *testing.T) {
res := checkResourceStatus(watchOpts{
suspended: true,
health: false,
}, string(health.HealthStatusSuspended), string(v1alpha1.SyncStatusCodeSynced), &v1alpha1.Operation{})
assert.True(t, res)
})
t.Run("Suspended failed", func(t *testing.T) {
res := checkResourceStatus(watchOpts{
suspended: true,
health: false,
}, string(health.HealthStatusProgressing), string(v1alpha1.SyncStatusCodeSynced), &v1alpha1.Operation{})
assert.False(t, res)
})
t.Run("Health passed", func(t *testing.T) {
res := checkResourceStatus(watchOpts{
suspended: false,
health: true,
}, string(health.HealthStatusHealthy), string(v1alpha1.SyncStatusCodeSynced), &v1alpha1.Operation{})
assert.True(t, res)
})
t.Run("Health failed", func(t *testing.T) {
res := checkResourceStatus(watchOpts{
suspended: false,
health: true,
}, string(health.HealthStatusProgressing), string(v1alpha1.SyncStatusCodeSynced), &v1alpha1.Operation{})
assert.False(t, res)
})
t.Run("Synced passed", func(t *testing.T) {
res := checkResourceStatus(watchOpts{}, string(health.HealthStatusProgressing), string(v1alpha1.SyncStatusCodeSynced), &v1alpha1.Operation{})
assert.True(t, res)
})
t.Run("Synced failed", func(t *testing.T) {
res := checkResourceStatus(watchOpts{}, string(health.HealthStatusProgressing), string(v1alpha1.SyncStatusCodeOutOfSync), &v1alpha1.Operation{})
assert.True(t, res)
})
}