From 678c0eebba9c508378fc15a6a0957fb9b796cf2e Mon Sep 17 00:00:00 2001 From: yiraeChristineKim Date: Tue, 7 May 2024 13:14:26 -0400 Subject: [PATCH] test namespace Signed-off-by: yiraeChristineKim --- .github/workflows/kind.yml | 12 +- Makefile | 6 +- .../e2e/case21_alternative_kubeconfig_test.go | 18 - test/e2e/case31_policy_history_test.go | 369 +++++++++--------- test/e2e/case38_install_operator_test.go | 254 ++++++------ test/utils/utils.go | 4 + 6 files changed, 312 insertions(+), 351 deletions(-) diff --git a/.github/workflows/kind.yml b/.github/workflows/kind.yml index f499e9c8..b285561a 100644 --- a/.github/workflows/kind.yml +++ b/.github/workflows/kind.yml @@ -68,22 +68,22 @@ jobs: run: | KUBECONFIG=${PWD}/kubeconfig_managed make kind-ensure-sa - - name: E2E Tests - run: | - export GOPATH=$(go env GOPATH) - KUBECONFIG=${PWD}/kubeconfig_managed make e2e-test-coverage - - name: Create K8s KinD Cluster to simulate hosted mode - ${{ matrix.kind }} env: KIND_VERSION: ${{ matrix.kind }} run: | make kind-additional-cluster - + - name: E2E tests that simulate hosted mode run: | export GOPATH=$(go env GOPATH) KUBECONFIG=${PWD}/kubeconfig_managed make e2e-test-hosted-mode-coverage + - name: E2E Tests + run: | + export GOPATH=$(go env GOPATH) + KUBECONFIG=${PWD}/kubeconfig_managed make e2e-test-coverage + - name: Verify Deployment Configuration run: | make build-images diff --git a/Makefile b/Makefile index 9ca8b387..817ccd6a 100644 --- a/Makefile +++ b/Makefile @@ -242,19 +242,21 @@ install-resources: kubectl apply -f deploy/manager/service-account.yaml -n $(KIND_NAMESPACE) IS_HOSTED ?= false +E2E_PROCS=20 .PHONY: e2e-test e2e-test: e2e-dependencies - $(GINKGO) -v --procs=20 $(E2E_TEST_ARGS) test/e2e -- -is_hosted=$(IS_HOSTED) + $(GINKGO) -v --procs=$(E2E_PROCS) $(E2E_TEST_ARGS) test/e2e -- -is_hosted=$(IS_HOSTED) .PHONY: e2e-test-coverage e2e-test-coverage: E2E_TEST_ARGS = --json-report=report_e2e.json --label-filter='!hosted-mode && !running-in-cluster' --output-dir=. e2e-test-coverage: e2e-run-instrumented e2e-test e2e-stop-instrumented .PHONY: e2e-test-hosted-mode-coverage -e2e-test-hosted-mode-coverage: E2E_TEST_ARGS = --json-report=report_e2e_hosted_mode.json --label-filter="hosted-mode && hosted-as-well && !running-in-cluster" --output-dir=. +e2e-test-hosted-mode-coverage: E2E_TEST_ARGS = --json-report=report_e2e_hosted_mode.json --label-filter="hosted-mode || hosted-as-well && !running-in-cluster" --output-dir=. e2e-test-hosted-mode-coverage: COVERAGE_E2E_OUT = coverage_e2e_hosted_mode.out e2e-test-hosted-mode-coverage: IS_HOSTED=true +e2e-test-hosted-mode-coverage: E2E_PROCS=1 e2e-test-hosted-mode-coverage: export TARGET_KUBECONFIG_PATH = $(PWD)/kubeconfig_managed2 e2e-test-hosted-mode-coverage: e2e-run-instrumented e2e-test e2e-stop-instrumented .PHONY: e2e-test-running-in-cluster diff --git a/test/e2e/case21_alternative_kubeconfig_test.go b/test/e2e/case21_alternative_kubeconfig_test.go index 6155274b..454314d7 100644 --- a/test/e2e/case21_alternative_kubeconfig_test.go +++ b/test/e2e/case21_alternative_kubeconfig_test.go @@ -5,22 +5,18 @@ package e2e import ( "context" "fmt" - "os" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/client-go/kubernetes" - "k8s.io/client-go/tools/clientcmd" "open-cluster-management.io/config-policy-controller/test/utils" ) var _ = Describe("Test an alternative kubeconfig for policy evaluation", Ordered, Label("hosted-mode"), func() { const ( - envName = "TARGET_KUBECONFIG_PATH" namespaceName = "e2e-test-ns" policyName = "create-ns" policyYAML = "../resources/case21_alternative_kubeconfig/policy.yaml" @@ -28,20 +24,6 @@ var _ = Describe("Test an alternative kubeconfig for policy evaluation", Ordered parentPolicyYAML = "../resources/case21_alternative_kubeconfig/parent-policy.yaml" ) - var targetK8sClient *kubernetes.Clientset - - BeforeAll(func() { - By("Checking that the " + envName + " environment variable is valid") - altKubeconfigPath := os.Getenv(envName) - Expect(altKubeconfigPath).ToNot(Equal("")) - - targetK8sConfig, err := clientcmd.BuildConfigFromFlags("", altKubeconfigPath) - Expect(err).ToNot(HaveOccurred()) - - targetK8sClient, err = kubernetes.NewForConfig(targetK8sConfig) - Expect(err).ToNot(HaveOccurred()) - }) - AfterAll(func() { deleteConfigPolicies([]string{policyName}) diff --git a/test/e2e/case31_policy_history_test.go b/test/e2e/case31_policy_history_test.go index cc0a8664..3f2f2f18 100644 --- a/test/e2e/case31_policy_history_test.go +++ b/test/e2e/case31_policy_history_test.go @@ -15,212 +15,217 @@ import ( "open-cluster-management.io/config-policy-controller/test/utils" ) -var _ = Describe("Test policy history messages when KubeAPI omits values in the returned object", func() { - doHistoryTest := func(policyName, configPolicyName string) { - By("Waiting until the policy is initially compliant") - Eventually(func() interface{} { - managedPlc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy, - configPolicyName, testNamespace, true, defaultTimeoutSeconds) - - return utils.GetComplianceState(managedPlc) - }, defaultTimeoutSeconds, 1).Should(Equal("Compliant")) - - By("Checking the events on the configuration policy") - Consistently(func() int { - eventlen := len(utils.GetMatchingEvents(clientManaged, testNamespace, - configPolicyName, configPolicyName, "NonCompliant;", defaultTimeoutSeconds)) - - return eventlen - }, defaultConsistentlyDuration, 5).Should(BeNumerically("<", 2)) - - By("Checking the events on the parent policy") - // NOTE: pick policy event, these event's reason include ConfigPolicyName - Consistently(func() int { - eventlen := len(utils.GetMatchingEvents(clientManaged, testNamespace, - policyName, configPolicyName, "NonCompliant;", defaultTimeoutSeconds)) - - return eventlen - }, defaultConsistentlyDuration, 5).Should(BeNumerically("<", 3)) - } - - const ( - rsrcPath = "../resources/case31_policy_history/" - ) - - Describe("status should not toggle when a boolean field might be omitted", Ordered, func() { - const ( - policyYAML = rsrcPath + "pod-policy.yaml" - policyName = "test-policy-security" - configPolicyYAML = rsrcPath + "pod-config-policy.yaml" - configPolicyName = "config-policy-pod" - ) +var _ = Describe("Test policy history messages when KubeAPI omits values in the returned object", + Label("hosted-as-well"), func() { + doHistoryTest := func(policyName, configPolicyName string) { + By("Waiting until the policy is initially compliant") + Eventually(func() interface{} { + managedPlc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy, + configPolicyName, testNamespace, true, defaultTimeoutSeconds) - It("sets up a configuration policy with an omitempty boolean set to false", func() { - createObjWithParent(policyYAML, policyName, configPolicyYAML, testNamespace, gvrPolicy, gvrConfigPolicy) - }) + return utils.GetComplianceState(managedPlc) + }, defaultTimeoutSeconds, 1).Should(Equal("Compliant")) - It("checks the policy's history", func() { - doHistoryTest(policyName, configPolicyName) - }) + By("Checking the events on the configuration policy") + Consistently(func() int { + eventlen := len(utils.GetMatchingEvents(clientManaged, testNamespace, + configPolicyName, configPolicyName, "NonCompliant;", defaultTimeoutSeconds)) - AfterAll(func() { - utils.Kubectl("delete", "policy", policyName, "-n", "managed", "--ignore-not-found") - configlPlc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy, - configPolicyName, "managed", false, defaultTimeoutSeconds, - ) - utils.Kubectl("delete", "event", "--field-selector=involvedObject.name="+policyName, "-n", "managed") - utils.Kubectl("delete", "event", "--field-selector=involvedObject.name="+configPolicyName, "-n", "managed") - ExpectWithOffset(1, configlPlc).To(BeNil()) - }) - }) + return eventlen + }, defaultConsistentlyDuration, 5).Should(BeNumerically("<", 2)) - Describe("status should not toggle when a numerical field might be omitted", Ordered, func() { - const ( - policyYAML = rsrcPath + "pod-policy-number.yaml" - policyName = "test-policy-security-number" - configPolicyYAML = rsrcPath + "pod-config-policy-number.yaml" - configPolicyName = "config-policy-pod-number" - ) - - It("sets up a configuration policy with an omitempty number set to 0", func() { - createObjWithParent(policyYAML, policyName, configPolicyYAML, testNamespace, gvrPolicy, gvrConfigPolicy) - }) - - It("checks the policy's history", func() { - doHistoryTest(policyName, configPolicyName) - }) + By("Checking the events on the parent policy") + // NOTE: pick policy event, these event's reason include ConfigPolicyName + Consistently(func() int { + eventlen := len(utils.GetMatchingEvents(clientManaged, testNamespace, + policyName, configPolicyName, "NonCompliant;", defaultTimeoutSeconds)) - AfterAll(func() { - utils.Kubectl("delete", "policy", policyName, "-n", "managed", "--ignore-not-found") - configlPlc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy, - configPolicyName, "managed", false, defaultTimeoutSeconds, - ) - utils.Kubectl("delete", "event", "--field-selector=involvedObject.name="+policyName, "-n", "managed") - utils.Kubectl("delete", "event", "--field-selector=involvedObject.name="+configPolicyName, "-n", "managed") - ExpectWithOffset(1, configlPlc).To(BeNil()) - }) - }) + return eventlen + }, defaultConsistentlyDuration, 5).Should(BeNumerically("<", 3)) + } - Describe("status should not toggle when an array might be omitted", Ordered, func() { const ( - policyYAML = rsrcPath + "rb-policy-emptyarray.yaml" - policyName = "test-policy-security-emptyarray" - configPolicyYAML = rsrcPath + "rb-config-policy-emptyarray.yaml" - configPolicyName = "config-policy-rb-emptyarray" + rsrcPath = "../resources/case31_policy_history/" ) - It("sets up a configuration policy with an omitempty number set to 0", func() { - createObjWithParent(policyYAML, policyName, configPolicyYAML, testNamespace, gvrPolicy, gvrConfigPolicy) - }) + Describe("status should not toggle when a boolean field might be omitted", Ordered, func() { + const ( + policyYAML = rsrcPath + "pod-policy.yaml" + policyName = "test-policy-security" + configPolicyYAML = rsrcPath + "pod-config-policy.yaml" + configPolicyName = "config-policy-pod" + ) - It("checks the policy's history", func() { - doHistoryTest(policyName, configPolicyName) + It("sets up a configuration policy with an omitempty boolean set to false", func() { + createObjWithParent(policyYAML, policyName, configPolicyYAML, testNamespace, gvrPolicy, gvrConfigPolicy) + }) + + It("checks the policy's history", func() { + doHistoryTest(policyName, configPolicyName) + }) + + AfterAll(func() { + utils.Kubectl("delete", "policy", policyName, "-n", "managed", "--ignore-not-found") + configlPlc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy, + configPolicyName, "managed", false, defaultTimeoutSeconds, + ) + utils.Kubectl("delete", "event", "--field-selector=involvedObject.name="+policyName, "-n", "managed") + utils.Kubectl("delete", "event", "--field-selector=involvedObject.name="+configPolicyName, + "-n", "managed") + ExpectWithOffset(1, configlPlc).To(BeNil()) + }) }) - AfterAll(func() { - utils.Kubectl("delete", "policy", policyName, "-n", "managed", "--ignore-not-found") - configlPlc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy, - configPolicyName, "managed", false, defaultTimeoutSeconds, + Describe("status should not toggle when a numerical field might be omitted", Ordered, func() { + const ( + policyYAML = rsrcPath + "pod-policy-number.yaml" + policyName = "test-policy-security-number" + configPolicyYAML = rsrcPath + "pod-config-policy-number.yaml" + configPolicyName = "config-policy-pod-number" ) - utils.Kubectl("delete", "event", "--field-selector=involvedObject.name="+policyName, "-n", "managed") - utils.Kubectl("delete", "event", "--field-selector=involvedObject.name="+configPolicyName, "-n", "managed") - ExpectWithOffset(1, configlPlc).To(BeNil()) - }) - }) - - Describe("status should not toggle when a struct might be omitted", Ordered, func() { - const ( - policyYAML = rsrcPath + "event-policy-emptystruct.yaml" - policyName = "test-policy-security-emptystruct" - configPolicyYAML = rsrcPath + "event-config-policy-emptystruct.yaml" - configPolicyName = "config-policy-event-emptystruct" - ) - - It("sets up a configuration policy with struct set to null", func() { - createObjWithParent(policyYAML, policyName, configPolicyYAML, testNamespace, gvrPolicy, gvrConfigPolicy) - }) - It("checks the policy's history", func() { - doHistoryTest(policyName, configPolicyName) + It("sets up a configuration policy with an omitempty number set to 0", func() { + createObjWithParent(policyYAML, policyName, configPolicyYAML, testNamespace, gvrPolicy, gvrConfigPolicy) + }) + + It("checks the policy's history", func() { + doHistoryTest(policyName, configPolicyName) + }) + + AfterAll(func() { + utils.Kubectl("delete", "policy", policyName, "-n", "managed", "--ignore-not-found") + configlPlc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy, + configPolicyName, "managed", false, defaultTimeoutSeconds, + ) + utils.Kubectl("delete", "event", "--field-selector=involvedObject.name="+policyName, "-n", "managed") + utils.Kubectl("delete", "event", "--field-selector=involvedObject.name="+configPolicyName, + "-n", "managed") + ExpectWithOffset(1, configlPlc).To(BeNil()) + }) }) - AfterAll(func() { - utils.Kubectl("delete", "policy", policyName, "-n", "managed", "--ignore-not-found") - configlPlc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy, - configPolicyName, "managed", false, defaultTimeoutSeconds, + Describe("status should not toggle when an array might be omitted", Ordered, func() { + const ( + policyYAML = rsrcPath + "rb-policy-emptyarray.yaml" + policyName = "test-policy-security-emptyarray" + configPolicyYAML = rsrcPath + "rb-config-policy-emptyarray.yaml" + configPolicyName = "config-policy-rb-emptyarray" ) - utils.Kubectl("delete", "event", "--field-selector=involvedObject.name="+policyName, "-n", "managed") - utils.Kubectl("delete", "event", "--field-selector=involvedObject.name="+configPolicyName, "-n", "managed") - ExpectWithOffset(1, configlPlc).To(BeNil()) - }) - }) - Describe("policy message should not be truncated", Ordered, func() { - const ( - case31LMPolicy = "../resources/case31_policy_history/long-message-policy.yaml" - case31LMConfigPolicy = "../resources/case31_policy_history/long-message-config-policy.yaml" - case31LMPolicyName = "long-message-policy" - case31LMConfigPolicyName = "long-message-config-policy" - namespacePrefix = "innovafertanimvsmvtatasdicereformascorporinnovafertanimvsmvt" - ) - It("Test policy message length is over 1024 ", func() { - By("Create namespaces") - for i := range [15]int{} { - utils.Kubectl("create", "ns", namespacePrefix+strconv.Itoa(i+1)) - } - utils.Kubectl("apply", "-f", case31LMPolicy, "-n", "managed") - By("bind policy and configurationpolicy") - parent := utils.GetWithTimeout(clientManagedDynamic, gvrPolicy, - case31LMPolicyName, testNamespace, true, defaultTimeoutSeconds) - Expect(parent).NotTo(BeNil()) - - plcDef := utils.ParseYaml(case31LMConfigPolicy) - ownerRefs := plcDef.GetOwnerReferences() - ownerRefs[0].UID = parent.GetUID() - plcDef.SetOwnerReferences(ownerRefs) - _, err := clientManagedDynamic.Resource(gvrConfigPolicy).Namespace(testNamespace). - Create(context.TODO(), plcDef, metav1.CreateOptions{}) - Expect(err).ToNot(HaveOccurred()) - - By("check configurationpolicy exist") - Eventually(func() interface{} { - plc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy, - case31LMConfigPolicyName, testNamespace, true, defaultTimeoutSeconds) - compliant := utils.GetComplianceState(plc) - - return compliant - }, 30, 5).Should(Equal("NonCompliant")) - By("check message longer than 1024") - Eventually(func() int { - event := utils.GetMatchingEvents(clientManaged, testNamespace, - case31LMPolicyName, case31LMConfigPolicyName, "NonCompliant", defaultTimeoutSeconds) + It("sets up a configuration policy with an omitempty number set to 0", func() { + createObjWithParent(policyYAML, policyName, configPolicyYAML, testNamespace, gvrPolicy, gvrConfigPolicy) + }) + + It("checks the policy's history", func() { + doHistoryTest(policyName, configPolicyName) + }) + + AfterAll(func() { + utils.Kubectl("delete", "policy", policyName, "-n", "managed", "--ignore-not-found") + configlPlc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy, + configPolicyName, "managed", false, defaultTimeoutSeconds, + ) + utils.Kubectl("delete", "event", "--field-selector=involvedObject.name="+policyName, "-n", "managed") + utils.Kubectl("delete", "event", "--field-selector=involvedObject.name="+configPolicyName, + "-n", "managed") + ExpectWithOffset(1, configlPlc).To(BeNil()) + }) + }) - Expect(event).ShouldNot(BeEmpty()) - message := event[len(event)-1].Message + Describe("status should not toggle when a struct might be omitted", Ordered, func() { + const ( + policyYAML = rsrcPath + "event-policy-emptystruct.yaml" + policyName = "test-policy-security-emptystruct" + configPolicyYAML = rsrcPath + "event-config-policy-emptystruct.yaml" + configPolicyName = "config-policy-event-emptystruct" + ) - return len(message) - }, 30, 5).Should(BeNumerically(">", 1024)) + It("sets up a configuration policy with struct set to null", func() { + createObjWithParent(policyYAML, policyName, configPolicyYAML, testNamespace, gvrPolicy, gvrConfigPolicy) + }) + + It("checks the policy's history", func() { + doHistoryTest(policyName, configPolicyName) + }) + + AfterAll(func() { + utils.Kubectl("delete", "policy", policyName, "-n", "managed", "--ignore-not-found") + configlPlc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy, + configPolicyName, "managed", false, defaultTimeoutSeconds, + ) + utils.Kubectl("delete", "event", "--field-selector=involvedObject.name="+policyName, "-n", "managed") + utils.Kubectl("delete", "event", "--field-selector=involvedObject.name="+configPolicyName, + "-n", "managed") + ExpectWithOffset(1, configlPlc).To(BeNil()) + }) }) - AfterAll(func() { - utils.Kubectl("delete", "policy", case31LMPolicyName, "-n", - "managed", "--ignore-not-found") - configlPlc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy, - case31LMPolicyName, "managed", false, defaultTimeoutSeconds, + Describe("policy message should not be truncated", Ordered, func() { + const ( + case31LMPolicy = "../resources/case31_policy_history/long-message-policy.yaml" + case31LMConfigPolicy = "../resources/case31_policy_history/long-message-config-policy.yaml" + case31LMPolicyName = "long-message-policy" + case31LMConfigPolicyName = "long-message-config-policy" + namespacePrefix = "innovafertanimvsmvtatasdicereformascorporinnovafertanimvsmvt" ) - Expect(configlPlc).To(BeNil()) - utils.Kubectl("delete", "event", - "--field-selector=involvedObject.name="+case31LMPolicyName, - "-n", "managed", "--ignore-not-found") - utils.Kubectl("delete", "event", - "--field-selector=involvedObject.name="+case31LMConfigPolicy, - "-n", "managed", "--ignore-not-found") - for i := range [15]int{} { - utils.Kubectl("delete", "ns", namespacePrefix+strconv.Itoa(i+1), - "--ignore-not-found", "--force", "--grace-period=0") - } + It("Test policy message length is over 1024 ", func() { + By("Create namespaces") + for i := range [15]int{} { + utils.Kubectl("create", "ns", namespacePrefix+strconv.Itoa(i+1)) + } + utils.Kubectl("apply", "-f", case31LMPolicy, "-n", "managed") + By("bind policy and configurationpolicy") + parent := utils.GetWithTimeout(clientManagedDynamic, gvrPolicy, + case31LMPolicyName, testNamespace, true, defaultTimeoutSeconds) + Expect(parent).NotTo(BeNil()) + + plcDef := utils.ParseYaml(case31LMConfigPolicy) + ownerRefs := plcDef.GetOwnerReferences() + ownerRefs[0].UID = parent.GetUID() + plcDef.SetOwnerReferences(ownerRefs) + _, err := clientManagedDynamic.Resource(gvrConfigPolicy).Namespace(testNamespace). + Create(context.TODO(), plcDef, metav1.CreateOptions{}) + Expect(err).ToNot(HaveOccurred()) + + By("check configurationpolicy exist") + Eventually(func() interface{} { + plc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy, + case31LMConfigPolicyName, testNamespace, true, defaultTimeoutSeconds) + compliant := utils.GetComplianceState(plc) + + return compliant + }, 30, 5).Should(Equal("NonCompliant")) + + By("check message longer than 1024") + Eventually(func() int { + event := utils.GetMatchingEvents(clientManaged, testNamespace, + case31LMPolicyName, case31LMConfigPolicyName, "NonCompliant", defaultTimeoutSeconds) + + Expect(event).ShouldNot(BeEmpty()) + message := event[len(event)-1].Message + + return len(message) + }, 30, 5).Should(BeNumerically(">", 1024)) + }) + AfterAll(func() { + utils.Kubectl("delete", "policy", case31LMPolicyName, "-n", + "managed", "--ignore-not-found") + configlPlc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy, + case31LMPolicyName, "managed", false, defaultTimeoutSeconds, + ) + Expect(configlPlc).To(BeNil()) + utils.Kubectl("delete", "event", + "--field-selector=involvedObject.name="+case31LMPolicyName, + "-n", "managed", "--ignore-not-found") + utils.Kubectl("delete", "event", + "--field-selector=involvedObject.name="+case31LMConfigPolicy, + "-n", "managed", "--ignore-not-found") + for i := range [15]int{} { + utils.Kubectl("delete", "ns", namespacePrefix+strconv.Itoa(i+1), + "--ignore-not-found", "--force", "--grace-period=0") + } + }) }) }) -}) func createObjWithParent( parentYAML, parentName, childYAML, namespace string, parentGVR, childGVR schema.GroupVersionResource, diff --git a/test/e2e/case38_install_operator_test.go b/test/e2e/case38_install_operator_test.go index 12e4ecb4..969bc995 100644 --- a/test/e2e/case38_install_operator_test.go +++ b/test/e2e/case38_install_operator_test.go @@ -20,12 +20,12 @@ import ( "open-cluster-management.io/config-policy-controller/test/utils" ) -var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), func() { +var _ = FDescribe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), func() { const ( opPolTestNS = "operator-policy-testns" parentPolicyYAML = "../resources/case38_operator_install/parent-policy.yaml" parentPolicyName = "parent-policy" - eventuallyTimeout = 10 + eventuallyTimeout = 60 consistentlyDuration = 5 olmWaitTimeout = 60 ) @@ -414,12 +414,14 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun opPolName = "oppol-no-group-enforce" ) BeforeAll(func() { - preFunc() - DeferCleanup(func() { utils.Kubectl( "delete", "-f", parentPolicyYAML, "-n", testNamespace, "--ignore-not-found", "--cascade=foreground", ) + if IsHosted { + KubectlTarget("delete", "ns", opPolTestNS, "--ignore-not-found") + } + utils.Kubectl("delete", "ns", opPolTestNS, "--ignore-not-found") }) createObjWithParent( @@ -434,7 +436,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun }) }) - FDescribe("Testing OperatorGroup behavior when it is specified in the policy", Ordered, func() { + Describe("Testing OperatorGroup behavior when it is specified in the policy", Ordered, func() { const ( opPolYAML = "../resources/case38_operator_install/operator-policy-with-group.yaml" opPolName = "oppol-with-group" @@ -614,10 +616,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun ) BeforeAll(func() { - KubectlTarget("create", "ns", opPolTestNS) - DeferCleanup(func() { - KubectlTarget("delete", "ns", opPolTestNS) - }) + preFunc() createObjWithParent(parentPolicyYAML, parentPolicyName, opPolYAML, opPolTestNS, gvrPolicy, gvrOperatorPolicy) @@ -713,12 +712,9 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun ) BeforeAll(func() { - utils.Kubectl("create", "ns", opPolTestNS) - DeferCleanup(func() { - utils.Kubectl("delete", "ns", opPolTestNS) - }) + preFunc() - utils.Kubectl("apply", "-f", subYAML, "-n", opPolTestNS) + KubectlTarget("apply", "-f", subYAML, "-n", opPolTestNS) createObjWithParent(parentPolicyYAML, parentPolicyName, opPolYAML, opPolTestNS, gvrPolicy, gvrOperatorPolicy) @@ -784,10 +780,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun opPolNoExistName = "oppol-no-exist-enforce" ) BeforeAll(func() { - utils.Kubectl("create", "ns", opPolTestNS) - DeferCleanup(func() { - utils.Kubectl("delete", "ns", opPolTestNS) - }) + preFunc() createObjWithParent(parentPolicyYAML, parentPolicyName, opPolYAML, opPolTestNS, gvrPolicy, gvrOperatorPolicy) @@ -798,7 +791,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun It("Should generate conditions and relatedobjects of CSV", func(ctx SpecContext) { Eventually(func(ctx SpecContext) string { - csv, _ := clientManagedDynamic.Resource(gvrClusterServiceVersion).Namespace(opPolTestNS). + csv, _ := targetK8sDynamic.Resource(gvrClusterServiceVersion).Namespace(opPolTestNS). Get(ctx, "quay-operator.v3.10.0", metav1.GetOptions{}) if csv == nil { @@ -911,10 +904,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun opPolName = "oppol-no-allnamespaces" ) BeforeAll(func() { - utils.Kubectl("create", "ns", opPolTestNS) - DeferCleanup(func() { - utils.Kubectl("delete", "ns", opPolTestNS) - }) + preFunc() createObjWithParent(parentPolicyYAML, parentPolicyName, opPolYAML, opPolTestNS, gvrPolicy, gvrOperatorPolicy) @@ -922,7 +912,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun It("Should generate conditions and relatedobjects of CSV", func(ctx SpecContext) { Eventually(func(ctx SpecContext) []unstructured.Unstructured { - csvList, _ := clientManagedDynamic.Resource(gvrClusterServiceVersion).Namespace(opPolTestNS). + csvList, _ := targetK8sDynamic.Resource(gvrClusterServiceVersion).Namespace(opPolTestNS). List(ctx, metav1.ListOptions{}) return csvList.Items @@ -991,11 +981,10 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun BeforeAll(func() { By("Applying creating a ns and the test policy") - utils.Kubectl("create", "ns", opPolTestNS) + preFunc() DeferCleanup(func() { - utils.Kubectl("patch", "catalogsource", catSrcName, "-n", "olm", "--type=json", "-p", + KubectlTarget("patch", "catalogsource", catSrcName, "-n", "olm", "--type=json", "-p", `[{"op": "replace", "path": "/spec/image", "value": "quay.io/operatorhubio/catalog:latest"}]`) - utils.Kubectl("delete", "ns", opPolTestNS) }) createObjWithParent(parentPolicyYAML, parentPolicyName, @@ -1094,7 +1083,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun `[{"op": "replace", "path": "/spec/subscription/source", "value": "operatorhubio-catalog"}]`) By("Patching the CatalogSource to reference a broken image link") - utils.Kubectl("patch", "catalogsource", catSrcName, "-n", "olm", "--type=json", "-p", + KubectlTarget("patch", "catalogsource", catSrcName, "-n", "olm", "--type=json", "-p", `[{"op": "replace", "path": "/spec/image", "value": "quay.io/operatorhubio/fakecatalog:latest"}]`) By("Checking the conditions and relatedObj in the policy") @@ -1136,10 +1125,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun ) BeforeAll(func() { - utils.Kubectl("create", "ns", opPolTestNS) - DeferCleanup(func() { - utils.Kubectl("delete", "ns", opPolTestNS) - }) + preFunc() createObjWithParent(parentPolicyYAML, parentPolicyName, opPolYAML, opPolTestNS, gvrPolicy, gvrOperatorPolicy) @@ -1147,7 +1133,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun It("Should initially report the ConstraintsNotSatisfiable Subscription", func(ctx SpecContext) { Eventually(func(ctx SpecContext) interface{} { - sub, _ := clientManagedDynamic.Resource(gvrSubscription).Namespace(opPolTestNS). + sub, _ := targetK8sDynamic.Resource(gvrSubscription).Namespace(opPolTestNS). Get(ctx, subName, metav1.GetOptions{}) if sub == nil { @@ -1226,10 +1212,10 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun utils.Kubectl("patch", "operatorpolicy", opPolName, "-n", opPolTestNS, "--type=json", "-p", `[{"op": "replace", "path": "/spec/subscription/startingCSV", "value": "`+goodVersion+`"},`+ `{"op": "replace", "path": "/spec/remediationAction", "value": "inform"}]`) - utils.Kubectl("patch", "subscription.operator", subName, "-n", opPolTestNS, "--type=json", "-p", + KubectlTarget("patch", "subscription.operator", subName, "-n", opPolTestNS, "--type=json", "-p", `[{"op": "replace", "path": "/spec/startingCSV", "value": "`+goodVersion+`"}]`) Eventually(func(ctx SpecContext) int { - ipList, _ := clientManagedDynamic.Resource(gvrInstallPlan).Namespace(opPolTestNS). + ipList, _ := targetK8sDynamic.Resource(gvrInstallPlan).Namespace(opPolTestNS). List(ctx, metav1.ListOptions{}) return len(ipList.Items) @@ -1258,7 +1244,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun ) }) It("Should do the upgrade when enforced, and stop at the next version", func(ctx SpecContext) { - ipList, err := clientManagedDynamic.Resource(gvrInstallPlan).Namespace(opPolTestNS). + ipList, err := targetK8sDynamic.Resource(gvrInstallPlan).Namespace(opPolTestNS). List(ctx, metav1.ListOptions{}) Expect(err).NotTo(HaveOccurred()) Expect(ipList.Items).To(HaveLen(1)) @@ -1269,7 +1255,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun `[{"op": "replace", "path": "/spec/remediationAction", "value": "enforce"}]`) Eventually(func(ctx SpecContext) int { - ipList, err = clientManagedDynamic.Resource(gvrInstallPlan).Namespace(opPolTestNS). + ipList, err = targetK8sDynamic.Resource(gvrInstallPlan).Namespace(opPolTestNS). List(ctx, metav1.ListOptions{}) return len(ipList.Items) @@ -1281,7 +1267,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun } Eventually(func(ctx SpecContext) string { - ip, _ := clientManagedDynamic.Resource(gvrInstallPlan).Namespace(opPolTestNS). + ip, _ := targetK8sDynamic.Resource(gvrInstallPlan).Namespace(opPolTestNS). Get(ctx, firstInstallPlanName, metav1.GetOptions{}) phase, _, _ := unstructured.NestedString(ip.Object, "status", "phase") @@ -1331,7 +1317,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun `[{"op": "add", "path": "/spec/versions/-", "value": "strimzi-cluster-operator.v0.36.1"}]`) Eventually(func(ctx SpecContext) string { - ip, _ := clientManagedDynamic.Resource(gvrInstallPlan).Namespace(opPolTestNS). + ip, _ := targetK8sDynamic.Resource(gvrInstallPlan).Namespace(opPolTestNS). Get(ctx, secondInstallPlanName, metav1.GetOptions{}) phase, _, _ := unstructured.NestedString(ip.Object, "status", "phase") @@ -1372,18 +1358,14 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun ) }) }) - Describe("Testing CustomResourceDefinition reporting", Ordered, func() { + FDescribe("Testing CustomResourceDefinition reporting", Ordered, func() { const ( opPolYAML = "../resources/case38_operator_install/operator-policy-no-group-one-version.yaml" opPolName = "oppol-no-group" ) BeforeAll(func() { - utils.Kubectl("delete", "crd", "--selector=olm.managed=true") - utils.Kubectl("create", "ns", opPolTestNS) - DeferCleanup(func() { - utils.Kubectl("delete", "ns", opPolTestNS) - }) - + preFunc() + KubectlTarget("delete", "crd", "--selector=olm.managed=true") createObjWithParent(parentPolicyYAML, parentPolicyName, opPolYAML, opPolTestNS, gvrPolicy, gvrOperatorPolicy) }) @@ -1418,12 +1400,13 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun `[{"op": "replace", "path": "/spec/remediationAction", "value": "enforce"}]`) By("Waiting for a CRD to appear, which should indicate the operator is installing") Eventually(func(ctx SpecContext) *unstructured.Unstructured { - crd, _ := clientManagedDynamic.Resource(gvrCRD).Get(ctx, + crd, _ := targetK8sDynamic.Resource(gvrCRD).Get(ctx, "quayregistries.quay.redhat.com", metav1.GetOptions{}) return crd }, olmWaitTimeout, 5, ctx).ShouldNot(BeNil()) + By("Check policy status") check( opPolName, false, @@ -1456,10 +1439,9 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun ) BeforeAll(func() { - utils.Kubectl("create", "ns", opPolTestNS) + preFunc() DeferCleanup(func() { - utils.Kubectl("delete", "ns", opPolTestNS) - utils.Kubectl("delete", "ns", "nonexist-testns") + KubectlTarget("delete", "ns", "nonexist-testns") }) createObjWithParent(parentPolicyYAML, parentPolicyName, @@ -1558,7 +1540,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun ) }) It("Should update the status after the namespace is created", func() { - utils.Kubectl("create", "namespace", "nonexist-testns") + KubectlTarget("create", "namespace", "nonexist-testns") check( opPolName, true, @@ -1592,11 +1574,8 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun ) BeforeAll(func() { - utils.Kubectl("create", "ns", opPolTestNS) - utils.Kubectl("delete", "crd", "--selector=olm.managed=true") - DeferCleanup(func() { - utils.Kubectl("delete", "ns", opPolTestNS) - }) + preFunc() + KubectlTarget("delete", "crd", "--selector=olm.managed=true") createObjWithParent(parentPolicyYAML, parentPolicyName, opPolYAML, opPolTestNS, gvrPolicy, gvrOperatorPolicy) @@ -1753,7 +1732,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun By("Waiting for a CRD to appear, which should indicate the operator is installing") Eventually(func(ctx SpecContext) *unstructured.Unstructured { - crd, _ := clientManagedDynamic.Resource(gvrCRD).Get(ctx, + crd, _ := targetK8sDynamic.Resource(gvrCRD).Get(ctx, "quayregistries.quay.redhat.com", metav1.GetOptions{}) return crd @@ -1996,11 +1975,11 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun keptChecks() By("Checking that certain (named) resources are still there") - utils.GetWithTimeout(clientManagedDynamic, gvrClusterServiceVersion, "quay-operator.v3.10.0", + utils.GetWithTimeout(targetK8sDynamic, gvrClusterServiceVersion, "quay-operator.v3.10.0", opPolTestNS, true, eventuallyTimeout) - utils.GetWithTimeout(clientManagedDynamic, gvrSubscription, subName, + utils.GetWithTimeout(targetK8sDynamic, gvrSubscription, subName, opPolTestNS, true, eventuallyTimeout) - utils.GetWithTimeout(clientManagedDynamic, gvrCRD, "quayregistries.quay.redhat.com", + utils.GetWithTimeout(targetK8sDynamic, gvrCRD, "quayregistries.quay.redhat.com", "", true, eventuallyTimeout) }) It("Should report a special status when the resources are stuck", func(ctx SpecContext) { @@ -2045,11 +2024,11 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun continue } - utils.Kubectl("patch", objKind, objName, "-n", opPolTestNS, "--type=json", "-p", + KubectlTarget("patch", objKind, objName, "-n", opPolTestNS, "--type=json", "-p", `[{"op": "add", "path": "/metadata/finalizers", "value": ["donutdelete"]}]`) DeferCleanup(func() { By("removing the finalizer from " + objKind + " " + objName) - utils.Kubectl("patch", objKind, objName, "-n", opPolTestNS, "--type=json", "-p", + KubectlTarget("patch", objKind, objName, "-n", opPolTestNS, "--type=json", "-p", `[{"op": "remove", "path": "/metadata/finalizers"}]`) }) } @@ -2193,11 +2172,11 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun }) It("Should report things as gone after the finalizers are removed", func() { By("Checking that certain (named) resources are not there, indicating the removal was completed") - utils.GetWithTimeout(clientManagedDynamic, gvrClusterServiceVersion, "quay-operator.v3.10.0", + utils.GetWithTimeout(targetK8sDynamic, gvrClusterServiceVersion, "quay-operator.v3.10.0", opPolTestNS, false, eventuallyTimeout) - utils.GetWithTimeout(clientManagedDynamic, gvrSubscription, subName, + utils.GetWithTimeout(targetK8sDynamic, gvrSubscription, subName, opPolTestNS, false, eventuallyTimeout) - utils.GetWithTimeout(clientManagedDynamic, gvrCRD, "quayregistries.quay.redhat.com", + utils.GetWithTimeout(targetK8sDynamic, gvrCRD, "quayregistries.quay.redhat.com", "", false, eventuallyTimeout) By("Checking the OperatorPolicy status") @@ -2325,56 +2304,54 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun Expect(compliance).To(Equal("Compliant")) }) }) - Describe("Testing mustnothave behavior for an operator group that is different than the specified one", func() { - const ( - opPolYAML = "../resources/case38_operator_install/operator-policy-with-group.yaml" - opPolName = "oppol-with-group" - subName = "project-quay" - ) - - BeforeEach(func() { - utils.Kubectl("create", "ns", opPolTestNS) - DeferCleanup(func() { - utils.Kubectl("delete", "ns", opPolTestNS) - }) + Describe("Testing mustnothave behavior for an operator group that is different than the specified one", + Ordered, func() { + const ( + opPolYAML = "../resources/case38_operator_install/operator-policy-with-group.yaml" + opPolName = "oppol-with-group" + subName = "project-quay" + ) - createObjWithParent(parentPolicyYAML, parentPolicyName, - opPolYAML, opPolTestNS, gvrPolicy, gvrOperatorPolicy) - }) + BeforeEach(func() { + preFunc() - It("should not report an operator group that does not match the spec", func() { - // create the extra operator group - utils.Kubectl("apply", "-f", "../resources/case38_operator_install/incorrect-operator-group.yaml", - "-n", opPolTestNS) - // change the operator policy to mustnothave - utils.Kubectl("patch", "operatorpolicy", opPolName, "-n", opPolTestNS, "--type=json", "-p", - `[{"op": "replace", "path": "/spec/complianceType", "value": "mustnothave"}]`) + createObjWithParent(parentPolicyYAML, parentPolicyName, + opPolYAML, opPolTestNS, gvrPolicy, gvrOperatorPolicy) + }) - check( - opPolName, - false, - []policyv1.RelatedObject{{ - Object: policyv1.ObjectResource{ - Kind: "OperatorGroup", - APIVersion: "operators.coreos.com/v1", - Metadata: policyv1.ObjectMetadata{ - Namespace: opPolTestNS, - Name: "scoped-operator-group", + It("should not report an operator group that does not match the spec", func() { + // create the extra operator group + KubectlTarget("apply", "-f", "../resources/case38_operator_install/incorrect-operator-group.yaml", + "-n", opPolTestNS) + // change the operator policy to mustnothave + utils.Kubectl("patch", "operatorpolicy", opPolName, "-n", opPolTestNS, "--type=json", "-p", + `[{"op": "replace", "path": "/spec/complianceType", "value": "mustnothave"}]`) + + check( + opPolName, + false, + []policyv1.RelatedObject{{ + Object: policyv1.ObjectResource{ + Kind: "OperatorGroup", + APIVersion: "operators.coreos.com/v1", + Metadata: policyv1.ObjectMetadata{ + Namespace: opPolTestNS, + Name: "scoped-operator-group", + }, }, - }, - Compliant: "Compliant", - Reason: "Resource not found as expected", - }}, - metav1.Condition{ - Type: "OperatorGroupCompliant", - Status: metav1.ConditionTrue, - Reason: "OperatorGroupNotPresent", - Message: "the OperatorGroup is not present", - }, - "the OperatorGroup is not present", - ) + Compliant: "Compliant", + Reason: "Resource not found as expected", + }}, + metav1.Condition{ + Type: "OperatorGroupCompliant", + Status: metav1.ConditionTrue, + Reason: "OperatorGroupNotPresent", + Message: "the OperatorGroup is not present", + }, + "the OperatorGroup is not present", + ) + }) }) - }) Describe("Testing mustnothave behavior of operator groups in DeleteIfUnused mode", Ordered, func() { const ( opPolYAML = "../resources/case38_operator_install/operator-policy-mustnothave-any-version.yaml" @@ -2384,11 +2361,8 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun ) BeforeEach(func() { - utils.Kubectl("create", "ns", opPolTestNS) - utils.Kubectl("delete", "crd", "--selector=olm.managed=true") - DeferCleanup(func() { - utils.Kubectl("delete", "ns", opPolTestNS) - }) + preFunc() + KubectlTarget("delete", "crd", "--selector=olm.managed=true") createObjWithParent(parentPolicyYAML, parentPolicyName, opPolYAML, opPolTestNS, gvrPolicy, gvrOperatorPolicy) @@ -2403,18 +2377,18 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun By("Waiting for a CRD to appear, which should indicate the operator is installing.") Eventually(func(ctx SpecContext) *unstructured.Unstructured { - crd, _ := clientManagedDynamic.Resource(gvrCRD).Get(ctx, + crd, _ := targetK8sDynamic.Resource(gvrCRD).Get(ctx, "quayregistries.quay.redhat.com", metav1.GetOptions{}) return crd }, olmWaitTimeout, 5, ctx).ShouldNot(BeNil()) By("Waiting for the policy to become compliant, indicating the operator is installed") - checkCompliance(opPolName, opPolTestNS, olmWaitTimeout, policyv1.Compliant) + // checkCompliance(opPolName, opPolTestNS, olmWaitTimeout, policyv1.Compliant) By("Verifying that an operator group exists") Eventually(func(g Gomega) []unstructured.Unstructured { - list, err := clientManagedDynamic.Resource(gvrOperatorGroup).Namespace(opPolTestNS). + list, err := targetK8sDynamic.Resource(gvrOperatorGroup).Namespace(opPolTestNS). List(ctx, metav1.ListOptions{}) g.Expect(err).NotTo(HaveOccurred()) @@ -2427,7 +2401,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun By("Verifying that the operator group was removed") Eventually(func(g Gomega) []unstructured.Unstructured { - list, err := clientManagedDynamic.Resource(gvrOperatorGroup).Namespace(opPolTestNS). + list, err := targetK8sDynamic.Resource(gvrOperatorGroup).Namespace(opPolTestNS). List(ctx, metav1.ListOptions{}) g.Expect(err).NotTo(HaveOccurred()) @@ -2446,7 +2420,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun By("Waiting for a CRD to appear, which should indicate the operator is installing.") Eventually(func(ctx SpecContext) *unstructured.Unstructured { - crd, _ := clientManagedDynamic.Resource(gvrCRD).Get(ctx, + crd, _ := targetK8sDynamic.Resource(gvrCRD).Get(ctx, "quayregistries.quay.redhat.com", metav1.GetOptions{}) return crd @@ -2457,7 +2431,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun By("Verifying that an operator group exists") Eventually(func(g Gomega) []unstructured.Unstructured { - list, err := clientManagedDynamic.Resource(gvrOperatorGroup).Namespace(opPolTestNS). + list, err := targetK8sDynamic.Resource(gvrOperatorGroup).Namespace(opPolTestNS). List(ctx, metav1.ListOptions{}) g.Expect(err).NotTo(HaveOccurred()) @@ -2470,7 +2444,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun By("Verifying that the operator group was removed") Eventually(func(g Gomega) []unstructured.Unstructured { - list, err := clientManagedDynamic.Resource(gvrOperatorGroup).Namespace(opPolTestNS). + list, err := targetK8sDynamic.Resource(gvrOperatorGroup).Namespace(opPolTestNS). List(ctx, metav1.ListOptions{}) g.Expect(err).NotTo(HaveOccurred()) @@ -2489,7 +2463,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun By("Waiting for a CRD to appear, which should indicate the operator is installing.") Eventually(func(ctx SpecContext) *unstructured.Unstructured { - crd, _ := clientManagedDynamic.Resource(gvrCRD).Get(ctx, + crd, _ := targetK8sDynamic.Resource(gvrCRD).Get(ctx, "quayregistries.quay.redhat.com", metav1.GetOptions{}) return crd @@ -2500,7 +2474,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun By("Verifying that an operator group exists") Eventually(func(g Gomega) []unstructured.Unstructured { - list, err := clientManagedDynamic.Resource(gvrOperatorGroup).Namespace(opPolTestNS). + list, err := targetK8sDynamic.Resource(gvrOperatorGroup).Namespace(opPolTestNS). List(ctx, metav1.ListOptions{}) g.Expect(err).NotTo(HaveOccurred()) @@ -2508,14 +2482,14 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun }, eventuallyTimeout, 3, ctx).ShouldNot(BeEmpty()) By("Creating and setting an owner for the operator group") - utils.Kubectl("create", "configmap", "ownercm", "-n", opPolTestNS, "--from-literal=foo=bar") + KubectlTarget("create", "configmap", "ownercm", "-n", opPolTestNS, "--from-literal=foo=bar") - ownerCM := utils.GetWithTimeout(clientManagedDynamic, gvrConfigMap, "ownercm", + ownerCM := utils.GetWithTimeout(targetK8sDynamic, gvrConfigMap, "ownercm", opPolTestNS, true, eventuallyTimeout) ownerUID := string(ownerCM.GetUID()) Expect(ownerUID).NotTo(BeEmpty()) - utils.Kubectl("patch", "operatorgroup", "scoped-operator-group", "-n", opPolTestNS, "--type=json", "-p", + KubectlTarget("patch", "operatorgroup", "scoped-operator-group", "-n", opPolTestNS, "--type=json", "-p", `[{"op": "add", "path": "/metadata/ownerReferences", "value": [{"apiVersion": "v1", "kind": "ConfigMap", "name": "ownercm", "uid": "`+ownerUID+`"}]}]`) @@ -2525,7 +2499,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun By("Verifying the operator group was not removed") Consistently(func(g Gomega) []unstructured.Unstructured { - list, err := clientManagedDynamic.Resource(gvrOperatorGroup).Namespace(opPolTestNS). + list, err := targetK8sDynamic.Resource(gvrOperatorGroup).Namespace(opPolTestNS). List(ctx, metav1.ListOptions{}) g.Expect(err).NotTo(HaveOccurred()) @@ -2542,7 +2516,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun By("Waiting for a CRD to appear, which should indicate the operator is installing.") Eventually(func(ctx SpecContext) *unstructured.Unstructured { - crd, _ := clientManagedDynamic.Resource(gvrCRD).Get(ctx, + crd, _ := targetK8sDynamic.Resource(gvrCRD).Get(ctx, "quayregistries.quay.redhat.com", metav1.GetOptions{}) return crd @@ -2553,7 +2527,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun By("Verifying that an operator group exists") Eventually(func(g Gomega) []unstructured.Unstructured { - list, err := clientManagedDynamic.Resource(gvrOperatorGroup).Namespace(opPolTestNS). + list, err := targetK8sDynamic.Resource(gvrOperatorGroup).Namespace(opPolTestNS). List(ctx, metav1.ListOptions{}) g.Expect(err).NotTo(HaveOccurred()) @@ -2577,7 +2551,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun By("Verifying the operator group was not removed") Consistently(func(g Gomega) []unstructured.Unstructured { - list, err := clientManagedDynamic.Resource(gvrOperatorGroup).Namespace(opPolTestNS). + list, err := targetK8sDynamic.Resource(gvrOperatorGroup).Namespace(opPolTestNS). List(ctx, metav1.ListOptions{}) g.Expect(err).NotTo(HaveOccurred()) @@ -2592,10 +2566,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun ) BeforeEach(func() { - utils.Kubectl("create", "ns", opPolTestNS) - DeferCleanup(func() { - utils.Kubectl("delete", "ns", opPolTestNS) - }) + preFunc() createObjWithParent(parentPolicyYAML, parentPolicyName, opPolYAML, opPolTestNS, gvrPolicy, gvrOperatorPolicy) @@ -2627,12 +2598,9 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun ) BeforeAll(func() { - utils.Kubectl("create", "ns", opPolTestNS) - DeferCleanup(func() { - utils.Kubectl("delete", "ns", opPolTestNS) - }) + preFunc() - utils.Kubectl("apply", "-f", configmapYAML, "-n", opPolTestNS) + KubectlTarget("apply", "-f", configmapYAML, "-n", opPolTestNS) createObjWithParent(parentPolicyYAML, parentPolicyName, opPolYAML, opPolTestNS, gvrPolicy, gvrOperatorPolicy) @@ -2668,7 +2636,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun ) By("Verifying the targetNamespaces in the OperatorGroup") - og, err := clientManagedDynamic.Resource(gvrOperatorGroup).Namespace(opPolTestNS). + og, err := targetK8sDynamic.Resource(gvrOperatorGroup).Namespace(opPolTestNS). Get(ctx, opGroupName, metav1.GetOptions{}) Expect(err).NotTo(HaveOccurred()) Expect(og).NotTo(BeNil()) @@ -2679,7 +2647,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun Expect(targetNamespaces).To(HaveExactElements("foo", "bar", opPolTestNS)) By("Verifying the Subscription channel") - sub, err := clientManagedDynamic.Resource(gvrSubscription).Namespace(opPolTestNS). + sub, err := targetK8sDynamic.Resource(gvrSubscription).Namespace(opPolTestNS). Get(ctx, subName, metav1.GetOptions{}) Expect(err).NotTo(HaveOccurred()) Expect(sub).NotTo(BeNil()) @@ -2691,7 +2659,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, Label("hosted-as-well"), fun }) It("Should update the subscription after the configmap is updated", func(ctx SpecContext) { - utils.Kubectl("patch", "configmap", "op-config", "-n", opPolTestNS, "--type=json", "-p", + KubectlTarget("patch", "configmap", "op-config", "-n", opPolTestNS, "--type=json", "-p", `[{"op": "replace", "path": "/data/channel", "value": "stable-3.10"}]`) check( diff --git a/test/utils/utils.go b/test/utils/utils.go index 7a626360..bfbf22fe 100644 --- a/test/utils/utils.go +++ b/test/utils/utils.go @@ -150,6 +150,10 @@ func GetMatchingEvents( // Kubectl executes kubectl commands func Kubectl(args ...string) { + if !strings.HasPrefix(args[len(args)-1], "--kubeconfig=") { + args = append(args, "--kubeconfig="+"../../kubeconfig_managed_e2e") + } + cmd := exec.Command("kubectl", args...) output, err := cmd.CombinedOutput()