open-cluster-management.io/governance-policy-propagator@v0.13.0/test/e2e/case14_root_policy_metrics_test.go (about) 1 // Copyright (c) 2021 Red Hat, Inc. 2 // Copyright Contributors to the Open Cluster Management project 3 4 package e2e 5 6 import ( 7 "context" 8 "fmt" 9 "strconv" 10 11 . "github.com/onsi/ginkgo/v2" 12 . "github.com/onsi/gomega" 13 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 14 15 "open-cluster-management.io/governance-policy-propagator/test/utils" 16 ) 17 18 var _ = Describe("Test root policy metrics", Ordered, func() { 19 const ( 20 policyName = "case9-test-policy" 21 policyYaml = "../resources/case9_templates/case9-test-policy.yaml" 22 replicatedPolicyYaml = "../resources/case9_templates/case9-test-replpolicy-managed1.yaml" 23 ) 24 25 Describe("Create policy, placement and referenced resource in ns:"+testNamespace, func() { 26 prePolicyDuration := -1 27 28 It("should record root policy duration before the policy is created", func() { 29 durationMetric := utils.GetMetrics( 30 "ocm_handle_root_policy_duration_seconds_bucket_bucket", fmt.Sprintf(`le=\"%d\"`, 10)) 31 Expect(len(durationMetric) != 0) 32 numEvals, err := strconv.Atoi(durationMetric[0]) 33 Expect(err == nil) 34 prePolicyDuration = numEvals 35 Expect(prePolicyDuration > -1) 36 }) 37 38 It("should be created in user ns", func() { 39 By("Creating " + policyYaml) 40 utils.Kubectl("apply", 41 "-f", policyYaml, 42 "-n", testNamespace, 43 "--kubeconfig="+kubeconfigHub) 44 plc := utils.GetWithTimeout( 45 clientHubDynamic, gvrPolicy, policyName, testNamespace, true, defaultTimeoutSeconds, 46 ) 47 Expect(plc).NotTo(BeNil()) 48 }) 49 50 It("should resolve templates and propagate to cluster ns managed1", func() { 51 By("Patching test-policy-plr with decision of cluster managed1") 52 plr := utils.GetWithTimeout( 53 clientHubDynamic, gvrPlacementRule, policyName+"-plr", testNamespace, 54 true, defaultTimeoutSeconds, 55 ) 56 plr.Object["status"] = utils.GeneratePlrStatus("managed1") 57 _, err := clientHubDynamic.Resource(gvrPlacementRule).Namespace(testNamespace).UpdateStatus( 58 context.TODO(), plr, metav1.UpdateOptions{}, 59 ) 60 Expect(err).ToNot(HaveOccurred()) 61 plc := utils.GetWithTimeout( 62 clientHubDynamic, gvrPolicy, testNamespace+"."+policyName, "managed1", 63 true, defaultTimeoutSeconds, 64 ) 65 Expect(plc).ToNot(BeNil()) 66 67 yamlPlc := utils.ParseYaml(replicatedPolicyYaml) 68 Eventually(func(g Gomega) interface{} { 69 replicatedPlc := utils.GetWithTimeout( 70 clientHubDynamic, 71 gvrPolicy, 72 testNamespace+"."+policyName, 73 "managed1", 74 true, 75 defaultTimeoutSeconds, 76 ) 77 78 err := utils.RemovePolicyTemplateDBAnnotations(replicatedPlc) 79 g.Expect(err).ToNot(HaveOccurred()) 80 81 return replicatedPlc.Object["spec"] 82 }, defaultTimeoutSeconds, 1).Should(utils.SemanticEqual(yamlPlc.Object["spec"])) 83 }) 84 85 It("should update root policy duration after the policy is created", func() { 86 By("Checking metric bucket for root policy duration (10 seconds or less)") 87 Eventually(func() interface{} { 88 metric := utils.GetMetrics( 89 "ocm_handle_root_policy_duration_seconds_bucket_bucket", fmt.Sprintf(`le=\"%d\"`, 10)) 90 if len(metric) == 0 { 91 return false 92 } 93 numEvals, err := strconv.Atoi(metric[0]) 94 if err != nil { 95 return false 96 } 97 98 return numEvals > prePolicyDuration 99 }, defaultTimeoutSeconds, 1).Should(Equal(true)) 100 }) 101 102 It("should correctly report root policy hub template watches when propagated", func() { 103 By("Checking metric endpoint for root policy hub template watches") 104 Eventually(func() interface{} { 105 return utils.GetMetrics("hub_templates_active_watches", "\"[0-9]\"") 106 }, defaultTimeoutSeconds, 1).Should(Equal([]string{"3"})) 107 }) 108 109 cleanup := func() { 110 utils.Kubectl("delete", 111 "-f", policyYaml, 112 "-n", testNamespace, 113 "--kubeconfig="+kubeconfigHub) 114 opt := metav1.ListOptions{} 115 utils.ListWithTimeout(clientHubDynamic, gvrPolicy, opt, 0, false, defaultTimeoutSeconds) 116 } 117 118 It("should clean up", cleanup) 119 120 It("should report root policy 0 hub template watches after clean up", func() { 121 By("Checking metric endpoint for root policy hub template watches") 122 Eventually(func() interface{} { 123 return utils.GetMetrics("hub_templates_active_watches", "\"[0-9]\"") 124 }, defaultTimeoutSeconds, 1).Should(Equal([]string{"0"})) 125 }) 126 127 AfterAll(cleanup) 128 }) 129 })