diff --git a/.golangci.yml b/.golangci.yml index 7afae8b843..54c3b78ae0 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -12,6 +12,9 @@ linters: - ineffassign - staticcheck - structcheck + - tenv + - thelper + - tparallel - typecheck - unconvert - unused diff --git a/pkg/testrunner/scenario.go b/pkg/testrunner/scenario.go index a0b7d08f73..af48c65f08 100644 --- a/pkg/testrunner/scenario.go +++ b/pkg/testrunner/scenario.go @@ -85,6 +85,7 @@ func getRelativePath(path string) string { } func loadScenario(t *testing.T, path string) (*Scenario, error) { + t.Helper() fileBytes, err := loadFile(t, path) assert.Nil(t, err) @@ -111,6 +112,7 @@ func loadScenario(t *testing.T, path string) (*Scenario, error) { // loadFile loads file in byte buffer func loadFile(t *testing.T, path string) ([]byte, error) { + t.Helper() path = getRelativePath(path) t.Logf("reading file %s", path) if _, err := os.Stat(path); os.IsNotExist(err) { @@ -122,6 +124,7 @@ func loadFile(t *testing.T, path string) ([]byte, error) { } func runScenario(t *testing.T, s *Scenario) bool { + t.Helper() for _, tc := range s.TestCases { runTestCase(t, tc) } @@ -129,6 +132,7 @@ func runScenario(t *testing.T, s *Scenario) bool { } func runTestCase(t *testing.T, tc TestCase) bool { + t.Helper() policy := loadPolicy(t, tc.Input.Policy) if policy == nil { t.Error("Policy not loaded") @@ -204,7 +208,9 @@ func createNamespace(client client.Interface, ns *unstructured.Unstructured) err _, err := client.CreateResource("", "Namespace", "", ns, false) return err } + func validateGeneratedResources(t *testing.T, client client.Interface, policy kyverno.ClusterPolicy, namespace string, expected []kyverno.ResourceSpec) { + t.Helper() t.Log("--validate if resources are generated---") // list of expected generated resources for _, resource := range expected { @@ -215,6 +221,7 @@ func validateGeneratedResources(t *testing.T, client client.Interface, policy ky } func validateResource(t *testing.T, responseResource unstructured.Unstructured, expectedResourceFile string) { + t.Helper() resourcePrint := func(obj unstructured.Unstructured, msg string) { t.Logf("-----%s----", msg) if data, err := obj.MarshalJSON(); err == nil { @@ -243,6 +250,7 @@ func validateResource(t *testing.T, responseResource unstructured.Unstructured, } func validateResponse(t *testing.T, er response.PolicyResponse, expected response.PolicyResponse) { + t.Helper() if reflect.DeepEqual(expected, response.PolicyResponse{}) { t.Log("no response expected") return @@ -270,6 +278,7 @@ func validateResponse(t *testing.T, er response.PolicyResponse, expected respons } func comparePolicySpec(t *testing.T, policy response.PolicySpec, expectedPolicy response.PolicySpec) { + t.Helper() // namespace if policy.Namespace != expectedPolicy.Namespace { t.Errorf("namespace: expected %s, received %s", expectedPolicy.Namespace, policy.Namespace) @@ -281,6 +290,7 @@ func comparePolicySpec(t *testing.T, policy response.PolicySpec, expectedPolicy } func compareResourceSpec(t *testing.T, resource response.ResourceSpec, expectedResource response.ResourceSpec) { + t.Helper() // kind if resource.Kind != expectedResource.Kind { t.Errorf("kind: expected %s, received %s", expectedResource.Kind, resource.Kind) @@ -301,6 +311,7 @@ func compareResourceSpec(t *testing.T, resource response.ResourceSpec, expectedR } func compareRules(t *testing.T, rule response.RuleResponse, expectedRule response.RuleResponse) { + t.Helper() // name if rule.Name != expectedRule.Name { t.Errorf("rule name: expected %s, received %+v", expectedRule.Name, rule.Name) @@ -327,6 +338,7 @@ func compareRules(t *testing.T, rule response.RuleResponse, expectedRule respons } func loadPolicyResource(t *testing.T, file string) *unstructured.Unstructured { + t.Helper() // expect only one resource to be specified in the YAML resources := loadResource(t, file) if resources == nil { @@ -347,6 +359,7 @@ func loadPolicyResource(t *testing.T, file string) *unstructured.Unstructured { } func getClient(t *testing.T, files []string) client.Interface { + t.Helper() var objects []k8sRuntime.Object for _, file := range files { objects = loadObjects(t, file) @@ -379,6 +392,7 @@ func getGVRForResources(objects []k8sRuntime.Object) []schema.GroupVersionResour } func loadResource(t *testing.T, path string) []*unstructured.Unstructured { + t.Helper() var unstrResources []*unstructured.Unstructured t.Logf("loading resource from %s", path) data, err := loadFile(t, path) @@ -407,6 +421,7 @@ func loadResource(t *testing.T, path string) []*unstructured.Unstructured { } func loadObjects(t *testing.T, path string) []k8sRuntime.Object { + t.Helper() var resources []k8sRuntime.Object t.Logf("loading objects from %s", path) data, err := loadFile(t, path) @@ -429,6 +444,7 @@ func loadObjects(t *testing.T, path string) []k8sRuntime.Object { } func loadPolicy(t *testing.T, path string) *kyverno.ClusterPolicy { + t.Helper() t.Logf("loading policy from %s", path) data, err := loadFile(t, path) if err != nil { @@ -463,6 +479,7 @@ func loadPolicy(t *testing.T, path string) *kyverno.ClusterPolicy { } func testScenario(t *testing.T, path string) { + t.Helper() // flag.Set("logtostderr", "true") // flag.Set("v", "8")