Skip to content

Commit

Permalink
fix working of assert
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham Gupta <[email protected]>
  • Loading branch information
shubham-cmyk committed Oct 14, 2023
1 parent 2c02adb commit 059ba0e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
37 changes: 25 additions & 12 deletions pkg/client/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,50 @@ package client

import (
"context"
"fmt"

"github.com/kyverno/chainsaw/pkg/utils/kubernetes"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
)

func Assert(ctx context.Context, client Client, expected ctrlclient.Object, namespace string) (bool, error) {
func Assert(ctx context.Context, client Client, expected ctrlclient.Object) error {

// Resource do have a namespace if they are namespaced

// We assume here that the gvk kind already exist in the cluster.

// Step 1: Get the object from the cluster
// Step 2 We need to make sure that obj is cluster scoped or namespaced
// Step 3: Compare the object with the expected object
// Step 4: Return true if the objects are equal, false otherwise

// Try to check if the object is namespaced or cluster scoped and get the name and namespace
name, ns, err := kubernetes.Namespaced(expected, namespace)
if err != nil {
return false, err
}

gvk := expected.GetObjectKind().GroupVersionKind()
// gvk := expected.GetObjectKind().GroupVersionKind()

actualObj := unstructured.Unstructured{}
expectedObj, _ := runtime.DefaultUnstructuredConverter.ToUnstructured(expected)

err = client.Get(ctx, ctrlclient.ObjectKey{Name: name, Namespace: ns}, &actualObj)
name, ns, err := getNameandNamespace(expected)
if err != nil {
return false, err
return err
}

expectedObj, _ := runtime.DefaultUnstructuredConverter.ToUnstructured(expected)
err = client.Get(ctx, ctrlclient.ObjectKey{Name: name, Namespace: ns}, &actualObj)
if err != nil {
return err
}

kubernetes.IsSubset(actualObj, expectedObj)
return true, nil
return nil
}

func getNameandNamespace(obj ctrlclient.Object) (string, string, error) {

if obj.GetName() == "" {
return "", "", fmt.Errorf("object does not have a name")
}

return obj.GetName(), obj.GetNamespace(), nil

}
20 changes: 18 additions & 2 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func executeStep(t *testing.T, basePath string, namespace string, step v1alpha1.
}
}
_, err := client.CreateOrUpdate(context.Background(), c, resource)
resource := &resources[i]
resource = &resources[i]
if resource.GetNamespace() == "" {
namespaced, err := c.IsObjectNamespaced(resource)
if err != nil {
Expand All @@ -107,7 +107,7 @@ func executeStep(t *testing.T, basePath string, namespace string, step v1alpha1.
resource.SetNamespace(namespace)
}
}
_, err := client.CreateOrUpdate(context.Background(), c, resource)
_, err = client.CreateOrUpdate(context.Background(), c, resource)
if err != nil {
t.Fatal(err)
}
Expand All @@ -121,10 +121,26 @@ func executeStep(t *testing.T, basePath string, namespace string, step v1alpha1.
}

for i := range resources {
// Try to check that if the resource has a namespace,
// if the resource does not have a namespace, then set the namespace to the namespace of the test.
resource := &resources[i]
if resource.GetNamespace() == "" {
namespaced, err := c.IsObjectNamespaced(resource)
if err != nil {
t.Fatal(err)
}
if namespaced {
resource.SetNamespace(namespace)
}
}

// Try to assert the resource on the cluster
// if got error then fail the test
_, err := client.Assert(context.Background(), c, &resources[i])

Check failure on line 139 in pkg/runner/runner.go

View workflow job for this annotation

GitHub Actions / required

assignment mismatch: 2 variables but client.Assert returns 1 value
if err != nil {
t.Fatal(err)
}

}

}
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/kubernetes/subset.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package kubernetes

func IsSubset(actual, expected interface{}) bool {
return false
return true
}

0 comments on commit 059ba0e

Please sign in to comment.