From 987d005b731783ff77a50f53373ca66409c884a9 Mon Sep 17 00:00:00 2001 From: shivdudhani <shivkumar@nirmata.com> Date: Fri, 14 Jun 2019 15:20:05 -0700 Subject: [PATCH] rearrange test --- pkg/testutils/testbundle.go | 10 +++---- pkg/testutils/testutils_test.go | 50 ++++++++++++++++++++++----------- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/pkg/testutils/testbundle.go b/pkg/testutils/testbundle.go index d408b60f39..32d3ae2549 100644 --- a/pkg/testutils/testbundle.go +++ b/pkg/testutils/testbundle.go @@ -185,7 +185,7 @@ func (tb *testBundle) createClient(t *testing.T, resources []string) *dclient.Cl // Mock Client c, err := dclient.NewMockClient(scheme, objects...) if err != nil { - t.Fatal(err) + t.Error(err) } // set discovery Client c.SetDiscovery(dclient.NewFakeDiscoveryClient(regResources)) @@ -219,13 +219,13 @@ func (tb *testBundle) run(t *testing.T, testingapplyTest IApplyTest) { obj, _, err := decode([]byte(r.rawResource), nil, nil) _, err = c.CreateResource(getResourceFromKind(r.gvk.Kind), "", obj) if err != nil { - t.Fatalf("error while creating namespace %s", ts.Resource) + t.Errorf("error while creating namespace %s", ts.Resource) } } mPatchedResource, mResult, vResult, err := testingapplyTest.applyPolicy(p, r, c) if err != nil { - t.Fatal(err) + t.Error(err) } // check the expected scenario tb.checkMutationResult(t, ts.Mutation, mPatchedResource, mResult) @@ -237,7 +237,7 @@ func (tb *testBundle) run(t *testing.T, testingapplyTest IApplyTest) { func (tb *testBundle) checkGeneration(t *testing.T, expect *tGeneration, c *dclient.Client) { if expect == nil { - glog.Info("No Generatin check defined") + glog.Info("No Generate check defined") return } // iterate throught the expected resources and check if the client has them @@ -254,7 +254,6 @@ func (tb *testBundle) checkValidationResult(t *testing.T, expect *tValidation, v glog.Info("No Validation check defined") return } - // compare result // compare reason if len(expect.Reason) > 0 && expect.Reason != vResult.GetReason().String() { t.Error("Reason not matching") @@ -284,7 +283,6 @@ func (tb *testBundle) checkMutationResult(t *testing.T, expect *tMutation, pr *r glog.Warningf("Expected resource %s ", string(pr.rawResource)) t.Error("Patched resources not as expected") } - // compare result // compare reason if len(expect.Reason) > 0 && expect.Reason != mResult.GetReason().String() { t.Error("Reason not matching") diff --git a/pkg/testutils/testutils_test.go b/pkg/testutils/testutils_test.go index e91e89edae..dc683b19ba 100644 --- a/pkg/testutils/testutils_test.go +++ b/pkg/testutils/testutils_test.go @@ -6,31 +6,49 @@ import ( "github.com/golang/glog" ) +// func TestExamples(t *testing.T) { +// folders := []string{ +// "/Users/shiv/nirmata/code/go/src/github.com/nirmata/kyverno/examples", +// } +// testrunner(t, folders) +// } + +func TestGenerate(t *testing.T) { + folders := []string{ + "/Users/shiv/nirmata/code/go/src/github.com/nirmata/kyverno/examples/generate", + } + testrunner(t, folders) +} + +func TestMutateOverlay(t *testing.T) { + folders := []string{ + "/Users/shiv/nirmata/code/go/src/github.com/nirmata/kyverno/examples/mutate/overlay", + } + testrunner(t, folders) +} + +func TestMutatePatches(t *testing.T) { + folders := []string{ + "/Users/shiv/nirmata/code/go/src/github.com/nirmata/kyverno/examples/mutate/patches", + } + testrunner(t, folders) +} + +func testrunner(t *testing.T, folders []string) { + for _, folder := range folders { + runTest(t, folder) + } +} + func runTest(t *testing.T, path string) { // Load test suites at specified path ts := LoadTestSuite(t, path) - // policy application logic tp := &testPolicy{} ts.setApplyTest(tp) - // run the tests for each test bundle ts.runTests() if ts != nil { glog.Infof("Done running the test at %s", path) } } - -func TestExamples(t *testing.T) { - // folders := []string{ - // "/Users/shiv/nirmata/code/go/src/github.com/nirmata/kyverno/examples/mutate/patches", - // "/Users/shiv/nirmata/code/go/src/github.com/nirmata/kyverno/examples/mutate/overlay", - // "/Users/shiv/nirmata/code/go/src/github.com/nirmata/kyverno/examples/cli", - // } - folders := []string{ - "/Users/shiv/nirmata/code/go/src/github.com/nirmata/kyverno/examples/generate", - } - for _, folder := range folders { - runTest(t, folder) - } -}