|
| 1 | +// Copyright (c) 2020 Red Hat, Inc. |
| 2 | +// Copyright Contributors to the Open Cluster Management project |
| 3 | + |
| 4 | +package e2e |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "time" |
| 9 | + |
| 10 | + . "github.com/onsi/ginkgo/v2" |
| 11 | + . "github.com/onsi/gomega" |
| 12 | + corev1 "k8s.io/api/core/v1" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + "open-cluster-management.io/governance-policy-propagator/test/utils" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + case11PolicyYaml string = "../resources/case11_ts_collision/case11-policy.yaml" |
| 19 | + case11PolicyName string = "default.case11-test-policy" |
| 20 | + case11Event1 string = "default.case11-test-policy.171a96193d32cf17" |
| 21 | + case11Event2 string = "default.case11-test-policy.171a96193dea32f4" |
| 22 | + case11Event3 string = "default.case11-test-policy.171a96193dea32f8" |
| 23 | + case11Event4 string = "default.case11-test-policy.four.171a96193d32cf17" |
| 24 | + case11Event5 string = "default.case11-test-policy.five.171a96193d32cf17" |
| 25 | + case11Event6 string = "default.case11-test-policy.six.171a96193d32cf17" |
| 26 | + case11hubconfig string = "--kubeconfig=../../kubeconfig_hub" |
| 27 | + case11managedconfig string = "--kubeconfig=../../kubeconfig_managed" |
| 28 | +) |
| 29 | + |
| 30 | +func case11Event(name, namespace, message, evtype string, evtime time.Time, includeMS bool) error { |
| 31 | + event := &corev1.Event{ |
| 32 | + ObjectMeta: metav1.ObjectMeta{ |
| 33 | + Name: name, |
| 34 | + Namespace: namespace, |
| 35 | + CreationTimestamp: metav1.NewTime(evtime), |
| 36 | + }, |
| 37 | + InvolvedObject: corev1.ObjectReference{ |
| 38 | + Kind: "Policy", |
| 39 | + Namespace: namespace, |
| 40 | + Name: "default.case11-test-policy", |
| 41 | + // UID: "53719093-857c-4c9b-a1d2-187dfb8c6657", |
| 42 | + APIVersion: "policy.open-cluster-management.io/v1", |
| 43 | + }, |
| 44 | + Reason: "policy: managed/case11-cfg-policy", |
| 45 | + Message: message, |
| 46 | + Source: corev1.EventSource{ |
| 47 | + Component: "configuration-policy-controller", |
| 48 | + }, |
| 49 | + FirstTimestamp: metav1.NewTime(evtime), |
| 50 | + LastTimestamp: metav1.NewTime(evtime), |
| 51 | + Count: 1, |
| 52 | + Type: evtype, |
| 53 | + } |
| 54 | + |
| 55 | + if includeMS { |
| 56 | + event.EventTime = metav1.NewMicroTime(evtime) |
| 57 | + |
| 58 | + // These fields must also be added to satisfy eventsv1.Event validation |
| 59 | + event.Action = "filler" |
| 60 | + event.ReportingController = "filler" |
| 61 | + event.ReportingInstance = "filler" |
| 62 | + } |
| 63 | + |
| 64 | + _, err := clientManaged.CoreV1().Events(namespace).Create( |
| 65 | + context.TODO(), |
| 66 | + event, |
| 67 | + metav1.CreateOptions{}, |
| 68 | + ) |
| 69 | + |
| 70 | + return err |
| 71 | +} |
| 72 | + |
| 73 | +func case11cleanup() { |
| 74 | + out, err := utils.KubectlWithOutput( |
| 75 | + "delete", "-f", case11PolicyYaml, "-n", clusterNamespaceOnHub, case11hubconfig) |
| 76 | + if err != nil { |
| 77 | + Expect(out).Should(ContainSubstring("NotFound")) |
| 78 | + } |
| 79 | + |
| 80 | + out, err = utils.KubectlWithOutput( |
| 81 | + "delete", "-f", case11PolicyYaml, "-n", clusterNamespace, case11managedconfig) |
| 82 | + if err != nil { |
| 83 | + Expect(out).Should(ContainSubstring("NotFound")) |
| 84 | + } |
| 85 | + |
| 86 | + eventsToDelete := []string{ |
| 87 | + case11Event1, |
| 88 | + case11Event2, |
| 89 | + case11Event3, |
| 90 | + case11Event4, |
| 91 | + case11Event5, |
| 92 | + case11Event6, |
| 93 | + } |
| 94 | + |
| 95 | + for _, evname := range eventsToDelete { |
| 96 | + out, err = utils.KubectlWithOutput( |
| 97 | + "delete", "event", evname, "-n", clusterNamespace, case11managedconfig) |
| 98 | + if err != nil { |
| 99 | + Expect(out).Should(ContainSubstring("NotFound")) |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +var _ = Describe("Test event sorting by name when timestamps collide", Ordered, func() { |
| 105 | + It("Creates the policy and one event, and shows compliant", func() { |
| 106 | + _, err := utils.KubectlWithOutput( |
| 107 | + "apply", "-f", case11PolicyYaml, "-n", clusterNamespaceOnHub, case11hubconfig, |
| 108 | + ) |
| 109 | + Expect(err).Should(BeNil()) |
| 110 | + |
| 111 | + _, err = utils.KubectlWithOutput( |
| 112 | + "apply", "-f", case11PolicyYaml, "-n", clusterNamespace, case11managedconfig, |
| 113 | + ) |
| 114 | + Expect(err).Should(BeNil()) |
| 115 | + |
| 116 | + Expect(case11Event( |
| 117 | + case11Event1, |
| 118 | + clusterNamespace, |
| 119 | + "Compliant; notification - this is the oldest event", |
| 120 | + "Normal", |
| 121 | + time.Date(2022, 10, 3, 14, 40, 47, 0, time.UTC), |
| 122 | + false, |
| 123 | + )).Should(BeNil()) |
| 124 | + |
| 125 | + Eventually(checkCompliance(case11PolicyName), defaultTimeoutSeconds, 1). |
| 126 | + Should(Equal("Compliant")) |
| 127 | + Consistently(checkCompliance(case11PolicyName), "15s", 1). |
| 128 | + Should(Equal("Compliant")) |
| 129 | + }) |
| 130 | + |
| 131 | + It("Creates a second event with the same timestamp, and shows noncompliant", func() { |
| 132 | + Expect(case11Event( |
| 133 | + case11Event2, |
| 134 | + clusterNamespace, |
| 135 | + "NonCompliant; violation - a problem sandwich", |
| 136 | + "Warning", |
| 137 | + time.Date(2022, 10, 3, 14, 40, 47, 0, time.UTC), |
| 138 | + false, |
| 139 | + )).Should(BeNil()) |
| 140 | + |
| 141 | + Eventually(checkCompliance(case11PolicyName), defaultTimeoutSeconds, 1). |
| 142 | + Should(Equal("NonCompliant")) |
| 143 | + Consistently(checkCompliance(case11PolicyName), "15s", 1). |
| 144 | + Should(Equal("NonCompliant")) |
| 145 | + }) |
| 146 | + |
| 147 | + It("Creates a third with the same timestamp, and shows compliant", func() { |
| 148 | + Expect(case11Event( |
| 149 | + case11Event3, |
| 150 | + clusterNamespace, |
| 151 | + "Compliant; notification - this should be the most recent", |
| 152 | + "Normal", |
| 153 | + time.Date(2022, 10, 3, 14, 40, 47, 0, time.UTC), |
| 154 | + false, |
| 155 | + )).Should(BeNil()) |
| 156 | + |
| 157 | + Eventually(checkCompliance(case11PolicyName), defaultTimeoutSeconds, 1). |
| 158 | + Should(Equal("Compliant")) |
| 159 | + Consistently(checkCompliance(case11PolicyName), "15s", 1). |
| 160 | + Should(Equal("Compliant")) |
| 161 | + }) |
| 162 | + |
| 163 | + AfterAll(case11cleanup) |
| 164 | +}) |
| 165 | + |
| 166 | +var _ = Describe("Test event sorting by eventtime when timestamps collide", Ordered, func() { |
| 167 | + It("Creates the policy and one event, and shows compliant", func() { |
| 168 | + _, err := utils.KubectlWithOutput( |
| 169 | + "apply", "-f", case11PolicyYaml, "-n", clusterNamespaceOnHub, case11hubconfig, |
| 170 | + ) |
| 171 | + Expect(err).Should(BeNil()) |
| 172 | + |
| 173 | + _, err = utils.KubectlWithOutput( |
| 174 | + "apply", "-f", case11PolicyYaml, "-n", clusterNamespace, case11managedconfig, |
| 175 | + ) |
| 176 | + Expect(err).Should(BeNil()) |
| 177 | + |
| 178 | + Expect(case11Event( |
| 179 | + case11Event4, |
| 180 | + clusterNamespace, |
| 181 | + "Compliant; notification - this is the oldest event", |
| 182 | + "Normal", |
| 183 | + time.Date(2022, 10, 3, 14, 40, 47, 111111, time.UTC), |
| 184 | + true, |
| 185 | + )).Should(BeNil()) |
| 186 | + |
| 187 | + Eventually(checkCompliance(case11PolicyName), defaultTimeoutSeconds, 1). |
| 188 | + Should(Equal("Compliant")) |
| 189 | + Consistently(checkCompliance(case11PolicyName), "15s", 1). |
| 190 | + Should(Equal("Compliant")) |
| 191 | + }) |
| 192 | + |
| 193 | + It("Creates a second event with the same timestamp, and shows noncompliant", func() { |
| 194 | + Expect(case11Event( |
| 195 | + case11Event5, |
| 196 | + clusterNamespace, |
| 197 | + "NonCompliant; violation - a problem sandwich", |
| 198 | + "Warning", |
| 199 | + time.Date(2022, 10, 3, 14, 40, 47, 222222, time.UTC), |
| 200 | + true, |
| 201 | + )).Should(BeNil()) |
| 202 | + |
| 203 | + Eventually(checkCompliance(case11PolicyName), defaultTimeoutSeconds, 1). |
| 204 | + Should(Equal("NonCompliant")) |
| 205 | + Consistently(checkCompliance(case11PolicyName), "15s", 1). |
| 206 | + Should(Equal("NonCompliant")) |
| 207 | + }) |
| 208 | + |
| 209 | + It("Creates a third with the same timestamp, and shows compliant", func() { |
| 210 | + Expect(case11Event( |
| 211 | + case11Event6, |
| 212 | + clusterNamespace, |
| 213 | + "Compliant; notification - this should be the most recent", |
| 214 | + "Warning", |
| 215 | + time.Date(2022, 10, 3, 14, 40, 47, 333333, time.UTC), |
| 216 | + true, |
| 217 | + )).Should(BeNil()) |
| 218 | + |
| 219 | + Eventually(checkCompliance(case11PolicyName), defaultTimeoutSeconds, 1). |
| 220 | + Should(Equal("Compliant")) |
| 221 | + Consistently(checkCompliance(case11PolicyName), "15s", 1). |
| 222 | + Should(Equal("Compliant")) |
| 223 | + }) |
| 224 | + |
| 225 | + AfterAll(case11cleanup) |
| 226 | +}) |
0 commit comments