Skip to content

Commit

Permalink
raised 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 13, 2023
1 parent 01da390 commit e8e92b0
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 3 deletions.
38 changes: 38 additions & 0 deletions pkg/client/assert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package client

import (
"context"

"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) {

// 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()

Check failure on line 25 in pkg/client/assert.go

View workflow job for this annotation

GitHub Actions / required

gvk declared and not used (typecheck)

Check failure on line 25 in pkg/client/assert.go

View workflow job for this annotation

GitHub Actions / required

gvk declared and not used) (typecheck)

Check failure on line 25 in pkg/client/assert.go

View workflow job for this annotation

GitHub Actions / required

gvk declared and not used

Check failure on line 25 in pkg/client/assert.go

View workflow job for this annotation

GitHub Actions / required

gvk declared and not used

actualObj := unstructured.Unstructured{}

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

expectedObj, _ := runtime.DefaultUnstructuredConverter.ToUnstructured(expected)

kubernetes.IsSubset(actualObj, expectedObj)
return true, nil
}
2 changes: 2 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"k8s.io/apimachinery/pkg/runtime"
_ "k8s.io/client-go/plugin/pkg/client/auth" // package needed for auth providers like GCP
"k8s.io/client-go/rest"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -9,6 +10,7 @@ import (
type Client interface {
ctrlclient.Reader
ctrlclient.Writer
IsObjectNamespaced(obj runtime.Object) (bool, error)
}

func New(cfg *rest.Config) (Client, error) {
Expand Down
31 changes: 28 additions & 3 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,48 @@ func runTest(t *testing.T, cfg *rest.Config, test discovery.Test) {
step := test.Spec.Steps[i]
t.Run(fmt.Sprintf("step-%d", i+1), func(t *testing.T) {
t.Helper()
executeStep(t, test.BasePath, step, c)
executeStep(t, test.BasePath, step, c, "")

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

View workflow job for this annotation

GitHub Actions / required

cannot use step (variable of type "github.com/kyverno/chainsaw/pkg/apis/v1alpha1".TestStepSpec) as string value in argument to executeStep (typecheck)
})
}
}

func executeStep(t *testing.T, basePath string, step v1alpha1.TestStepSpec, c client.Client) {
func executeStep(t *testing.T, basePath string, namespace string, step v1alpha1.TestStepSpec, c client.Client) {
t.Helper()
for _, apply := range step.Apply {
resources, err := resource.Load(filepath.Join(basePath, apply.File))
if err != nil {
t.Fatal(err)
}
for i := range resources {
_, err := client.CreateOrUpdate(context.Background(), c, &resources[i])
resource := &resources[i]
if resource.GetNamespace() == "" {
namespaced, err := c.IsObjectNamespaced(resource)
if err != nil {
t.Fatal(err)
}
if namespaced {
resource.SetNamespace(namespace)
}
}
_, err := client.CreateOrUpdate(context.Background(), c, resource)
if err != nil {
t.Fatal(err)
}
}
}

for _, assert := range step.Assert {
resources, err := resource.Load(assert.File)
if err != nil {
t.Fatal(err)
}

for i := range resources {
_, err := client.Assert(context.Background(), c, &resources[i])
if err != nil {
t.Fatal(err)
}
}

}
}
14 changes: 14 additions & 0 deletions pkg/utils/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package kubernetes

import (
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
)

// This determine if the resource is clustered or namespaced
// if the resource is namespaced and doesn't have a namespace set, use the pet namespace
// if the resource is namespaced and has a namespace set, use the namespace set
// return namespace, name, error
func Namespaced(obj ctrlclient.Object, petNamespace string) (string, string, error) {

Check failure on line 11 in pkg/utils/kubernetes/kubernetes.go

View workflow job for this annotation

GitHub Actions / required

unnecessary leading newline (whitespace)

Check failure on line 12 in pkg/utils/kubernetes/kubernetes.go

View workflow job for this annotation

GitHub Actions / required

File is not `gofumpt`-ed (gofumpt)
return "", "", nil
}
5 changes: 5 additions & 0 deletions pkg/utils/kubernetes/subset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package kubernetes

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

0 comments on commit e8e92b0

Please sign in to comment.