Skip to content
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
19 changes: 19 additions & 0 deletions pkg/runner/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ func GetClient() *http.Client {
// ResourceValidator represents a generic resource validator
type ResourceValidator interface {
Exist() bool
Count() int
ExpectCount(int) bool
ExpectField(value interface{}, fields ...string) bool
}

Expand All @@ -84,6 +86,7 @@ type defaultResourceValidator struct {
err error
}

// Exist returns true if the specific resource exist
func (v *defaultResourceValidator) Exist() bool {
if v.err != nil {
fmt.Println(v.err)
Expand All @@ -92,6 +95,22 @@ func (v *defaultResourceValidator) Exist() bool {
return v.data != nil && len(v.data) > 0
}

// Count returns the count of the specific resources
func (v *defaultResourceValidator) Count() int {
val, ok, err := unstructured.NestedField(v.data, "items")
if ok && err == nil {
items := val.([]interface{})
return len(items)
}
return -1
}

// ExpectCount returns true if there are specific number of the target k8s resources
func (v *defaultResourceValidator) ExpectCount(expect int) bool {
return expect == v.Count()
}

// ExpectField verify the specific field and value
func (v *defaultResourceValidator) ExpectField(value interface{}, fields ...string) (result bool) {
val, ok, err := unstructured.NestedField(v.data, fields...)
if !ok || err != nil {
Expand Down
7 changes: 6 additions & 1 deletion pkg/runner/kubernetes/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ func TestKubernetesValidatorFunc(t *testing.T) {
expression: `k8s("daemonsets", "ns", "foo").Exist()`,
prepare: prepareDaemonset,
expectBool: true,
}, {
name: "daemonset count",
expression: `k8s("daemonsets", "ns", "foo").ExpectCount(0)`,
prepare: prepareDaemonset,
expectBool: true,
}, {
name: "no kind",
expression: `k8s({"foo": "bar"}, "ns", "foo").Exist()`,
Expand Down Expand Up @@ -121,7 +126,7 @@ func prepareDaemonset() {
Get("/apis/apps/v1/namespaces/ns/daemonsets/foo").
MatchHeader("Authorization", defaultToken).
Reply(http.StatusOK).
JSON(`{"kind":"daemonset"}`)
JSON(`{"kind":"daemonset","items":[]}`)
}

func prepareCRDVM() {
Expand Down