|
| 1 | +// Copyright (c) 2022 Red Hat, Inc. |
| 2 | +// Copyright Contributors to the Open Cluster Management project |
| 3 | + |
| 4 | +package integration |
| 5 | + |
| 6 | +import ( |
| 7 | + "encoding/json" |
| 8 | + |
| 9 | + . "github.com/onsi/ginkgo/v2" |
| 10 | + . "github.com/onsi/gomega" |
| 11 | + appsv1 "k8s.io/api/apps/v1" |
| 12 | + corev1 "k8s.io/api/core/v1" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + |
| 15 | + "github.com/stolostron/governance-policy-framework/test/common" |
| 16 | +) |
| 17 | + |
| 18 | +var _ = Describe("GRC: [P1][Sev1][policy-grc] Test add-on configuration", Serial, Label("SVT"), func() { |
| 19 | + BeforeEach(func(ctx SpecContext) { |
| 20 | + DeferCleanup(func() { |
| 21 | + _, _ = common.OcHub("delete", "namespace", "grc-addon-config-test") |
| 22 | + }) |
| 23 | + |
| 24 | + By("Deploying AddonDeploymentConfig grc-addon-config") |
| 25 | + _, _ = common.OcHub("create", "namespace", "grc-addon-config-test") |
| 26 | + _, err := common.OcHub( |
| 27 | + "apply", |
| 28 | + "-n=grc-addon-config-test", |
| 29 | + "-f=../resources/addon_configuration/addondeploymentconfig.yaml") |
| 30 | + Expect(err).ToNot(HaveOccurred()) |
| 31 | + }) |
| 32 | + |
| 33 | + DescribeTable("", |
| 34 | + addonTest, |
| 35 | + Entry(nil, "governance-policy-framework"), |
| 36 | + Entry(nil, "config-policy-controller"), |
| 37 | + Entry(nil, "cert-policy-controller"), |
| 38 | + ) |
| 39 | +}) |
| 40 | + |
| 41 | +var addonTest = func(ctx SpecContext, addOn string) { |
| 42 | + var ( |
| 43 | + expectedResources = `{"limits":{"memory":"1Gi"},"requests":{"memory":"512Mi"}}` |
| 44 | + expectedNodeSelector = `{"kubernetes.io/os":"linux"}` |
| 45 | + expectedTolerations = `[{"key":"dedicated","operator":"Equal","value":"something-else","effect":"NoSchedule"}]` |
| 46 | + ) |
| 47 | + |
| 48 | + // Restore AddOns regardless of test result |
| 49 | + DeferCleanup(func() { |
| 50 | + _, _ = common.OcHub( |
| 51 | + "patch", |
| 52 | + "managedclusteraddon", |
| 53 | + "-n", clusterNamespace, |
| 54 | + addOn, |
| 55 | + "--type=json", |
| 56 | + "--patch-file=../resources/addon_configuration/mcao_restore_patch.json", |
| 57 | + ) |
| 58 | + _, _ = common.OcHub( |
| 59 | + "patch", |
| 60 | + "clustermanagementaddon", |
| 61 | + addOn, |
| 62 | + "--type=json", |
| 63 | + "--patch-file=../resources/addon_configuration/cmao_restore_patch.json", |
| 64 | + ) |
| 65 | + }) |
| 66 | + |
| 67 | + fetchDeployment := func(g Gomega) (*appsv1.Deployment, corev1.Container) { |
| 68 | + deployment, err := clientManaged.AppsV1().Deployments(ocmAddonNS).Get(ctx, addOn, metav1.GetOptions{}) |
| 69 | + Expect(err).ToNot(HaveOccurred()) |
| 70 | + container := deployment.Spec.Template.Spec.Containers |
| 71 | + g.Expect(container).To(HaveLen(1)) |
| 72 | + |
| 73 | + return deployment, container[0] |
| 74 | + } |
| 75 | + |
| 76 | + By("Fetching Deployment to determine default configuration") |
| 77 | + baseDeployment, baseContainer := fetchDeployment(Default) |
| 78 | + |
| 79 | + By("Attaching the AddOnDeploymentConfig to the ManagedClusterAddOn for " + addOn) |
| 80 | + _, err := common.OcHub( |
| 81 | + "patch", |
| 82 | + "managedclusteraddon", |
| 83 | + "-n", clusterNamespace, |
| 84 | + addOn, |
| 85 | + "--type=json", |
| 86 | + "--patch-file=../resources/addon_configuration/mcao_config_patch.json", |
| 87 | + ) |
| 88 | + Expect(err).ToNot(HaveOccurred()) |
| 89 | + Eventually(func(g Gomega) { |
| 90 | + deployment, container := fetchDeployment(g) |
| 91 | + // Check Resources |
| 92 | + g.Expect(json.Marshal(container.Resources)).Should(BeEquivalentTo(expectedResources)) |
| 93 | + // Check NodeSelector |
| 94 | + g.Expect(json.Marshal(deployment.Spec.Template.Spec.NodeSelector)).Should(BeEquivalentTo(expectedNodeSelector)) |
| 95 | + // Check Tolerations |
| 96 | + g.Expect(json.Marshal(deployment.Spec.Template.Spec.Tolerations)).Should(BeEquivalentTo(expectedTolerations)) |
| 97 | + }, defaultTimeoutSeconds*2, 1).Should(Succeed()) |
| 98 | + |
| 99 | + By("Restoring the ManagedClusterAddOn for " + addOn) |
| 100 | + _, err = common.OcHub( |
| 101 | + "patch", |
| 102 | + "managedclusteraddon", |
| 103 | + "-n", clusterNamespace, |
| 104 | + addOn, |
| 105 | + "--type=json", |
| 106 | + "--patch-file=../resources/addon_configuration/mcao_restore_patch.json", |
| 107 | + ) |
| 108 | + Expect(err).ToNot(HaveOccurred()) |
| 109 | + Eventually(func(g Gomega) { |
| 110 | + deployment, container := fetchDeployment(g) |
| 111 | + g.Expect( |
| 112 | + deployment.Spec.Template.Spec.NodeSelector, |
| 113 | + ).Should(Equal( |
| 114 | + baseDeployment.Spec.Template.Spec.NodeSelector, |
| 115 | + )) |
| 116 | + g.Expect(container.Resources).Should(Equal(baseContainer.Resources)) |
| 117 | + }, defaultTimeoutSeconds*2, 1).Should(Succeed()) |
| 118 | + |
| 119 | + By("Attaching the AddOnDeploymentConfig to the ClusterManagementAddOn for " + addOn) |
| 120 | + _, err = common.OcHub( |
| 121 | + "patch", |
| 122 | + "clustermanagementaddon", |
| 123 | + addOn, |
| 124 | + "--type=json", |
| 125 | + "--patch-file=../resources/addon_configuration/cmao_config_patch.json", |
| 126 | + ) |
| 127 | + Expect(err).ToNot(HaveOccurred()) |
| 128 | + Eventually(func(g Gomega) { |
| 129 | + deployment, container := fetchDeployment(g) |
| 130 | + // Check Resources |
| 131 | + g.Expect(json.Marshal(container.Resources)).Should(BeEquivalentTo(expectedResources)) |
| 132 | + // Check NodeSelector |
| 133 | + g.Expect(json.Marshal(deployment.Spec.Template.Spec.NodeSelector)).Should(BeEquivalentTo(expectedNodeSelector)) |
| 134 | + // Check Tolerations |
| 135 | + g.Expect(json.Marshal(deployment.Spec.Template.Spec.Tolerations)).Should(BeEquivalentTo(expectedTolerations)) |
| 136 | + }, defaultTimeoutSeconds*2, 1).Should(Succeed()) |
| 137 | + |
| 138 | + By("Restoring the ManagedClusterAddOn for " + addOn) |
| 139 | + _, err = common.OcHub( |
| 140 | + "patch", |
| 141 | + "clustermanagementaddon", |
| 142 | + addOn, |
| 143 | + "--type=json", |
| 144 | + "--patch-file=../resources/addon_configuration/cmao_restore_patch.json", |
| 145 | + ) |
| 146 | + Expect(err).ToNot(HaveOccurred()) |
| 147 | + Eventually(func(g Gomega) { |
| 148 | + deployment, container := fetchDeployment(g) |
| 149 | + g.Expect( |
| 150 | + deployment.Spec.Template.Spec.NodeSelector, |
| 151 | + ).Should(Equal( |
| 152 | + baseDeployment.Spec.Template.Spec.NodeSelector, |
| 153 | + )) |
| 154 | + g.Expect(container.Resources).Should(Equal(baseContainer.Resources)) |
| 155 | + }, defaultTimeoutSeconds*2, 1).Should(Succeed()) |
| 156 | +} |
0 commit comments