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
33 changes: 29 additions & 4 deletions assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"net/http"
"os"
"strings"

"github.com/gdt-dev/core/api"
Expand Down Expand Up @@ -397,12 +398,36 @@ func (a *assertions) jsonOK(ctx context.Context) bool {
if exp.JSON != nil && a.hasSubject() {
var err error
var b []byte
res, ok := a.r.(*unstructured.Unstructured)
if ok {
if b, err = json.Marshal(res); err != nil {
panic("unable to marshal unstructured.Unstructured")
switch a.r.(type) {
case *unstructured.Unstructured:
res := a.r.(*unstructured.Unstructured)
if b, err = json.Marshal(res.Object); err != nil {
fmt.Fprintf(
os.Stderr,
"unable to marshal unstructured.Unstructured: %s\n",
err,
)
return false
}
case *unstructured.UnstructuredList:
res := a.r.(*unstructured.UnstructuredList)
a := make([]any, len(res.Items))
for x, item := range res.Items {
a[x] = item.Object
}
if b, err = json.Marshal(a); err != nil {
fmt.Fprintf(
os.Stderr, "unable to marshal []any: %s\n", err,
)
return false
}
default:
fmt.Fprintf(
os.Stderr, "unsupported type %T for JSON assertion\n", a.r,
)
return false
}

ja := gdtjson.New(exp.JSON, b)
if !ja.OK(ctx) {
for _, f := range ja.Failures() {
Expand Down
14 changes: 14 additions & 0 deletions testdata/json.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ tests:
$.metadata.uid: uuid4
$.metadata.creationTimestamp: date-time

- name: deployment-json-assertions-list
timeout:
after: 20s
kube:
get:
type: deployments
labels:
app: nginx
assert:
len: 1
json:
paths:
$[0].spec.replicas: 2

- name: delete-deployment
kube:
delete: deployments/nginx
2 changes: 1 addition & 1 deletion var.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func extractFrom(path string, out any) (any, error) {
case *unstructured.Unstructured:
normalized = out.Object
case *unstructured.UnstructuredList:
results := make([]map[string]any, len(out.Items))
results := make([]any, len(out.Items))
for x, item := range out.Items {
results[x] = item.Object
}
Expand Down
Loading