open-cluster-management.io/governance-policy-propagator@v0.13.0/controllers/complianceeventsapi/server_test.go (about) 1 package complianceeventsapi 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 8 . "github.com/onsi/gomega" 9 ) 10 11 func TestSplitQueryValue(t *testing.T) { 12 t.Parallel() 13 14 tests := []struct { 15 queryVal string 16 expected []string 17 }{ 18 {"cluster1", []string{"cluster1"}}, 19 {"cluster1,", []string{"cluster1"}}, 20 {",cluster1", []string{"cluster1"}}, 21 {"cluster1,cluster2", []string{"cluster1", "cluster2"}}, 22 {`cluster\,monkey,not-monkey`, []string{`cluster,monkey`, "not-monkey"}}, 23 } 24 25 for _, test := range tests { 26 test := test 27 28 t.Run( 29 fmt.Sprintf("?cluster.name=%s", test.queryVal), 30 func(t *testing.T) { 31 t.Parallel() 32 33 g := NewWithT(t) 34 g.Expect(splitQueryValue(test.queryVal)).To(Equal(test.expected)) 35 }, 36 ) 37 } 38 } 39 40 func TestConvertToCsvLine(t *testing.T) { 41 t.Parallel() 42 43 theTime := time.Date(2021, 8, 15, 14, 30, 45, 100, time.UTC) 44 45 reportBy := "cat1" 46 47 ce := ComplianceEvent{ 48 EventID: 1, 49 Event: EventDetails{ 50 Compliance: "cp1", 51 Message: "event1 message", 52 Metadata: nil, 53 ReportedBy: &reportBy, 54 Timestamp: theTime, 55 }, 56 Cluster: Cluster{ 57 ClusterID: "1111", 58 Name: "cluster1", 59 }, 60 Policy: Policy{ 61 KeyID: 0, 62 Kind: "", 63 APIGroup: "v1", 64 Name: "", 65 Spec: map[string]interface{}{ 66 "name": "hi", 67 "namespace": "cat-1", 68 }, 69 }, 70 } 71 72 values := convertToCsvLine(&ce, true) 73 74 g := NewWithT(t) 75 g.Expect(values).Should(HaveLen(21)) 76 // Should follow this order 77 // "compliance_events_id", 78 // "compliance_events_compliance", 79 // "compliance_events_message", 80 // "compliance_events_metadata", 81 // "compliance_events_reported_by", 82 // "compliance_events_timestamp", 83 // "clusters_cluster_id", 84 // "clusters_name", 85 // "parent_policies_id", 86 // "parent_policies_name", 87 // "parent_policies_namespace", 88 // "parent_policies_categories", 89 // "parent_policies_controls", 90 // "parent_policies_standards", 91 // "policies_id", 92 // "policies_api_group", 93 // "policies_kind", 94 // "policies_name", 95 // "policies_namespace", 96 // "policies_severity", 97 // "policies_spec", 98 g.Expect(values).Should(Equal([]string{ 99 "1", "cp1", "event1 message", 100 "", "cat1", "2021-08-15 14:30:45.0000001 +0000 UTC", 101 "1111", "cluster1", "", "", "", "", "", "", "", "v1", "", "", "", "", 102 "{\n \"name\": \"hi\",\n \"namespace\": \"cat-1\"\n}", 103 })) 104 105 // Test includeSpec = false 106 values = convertToCsvLine(&ce, false) 107 g.Expect(values).Should(HaveLen(20), "Test Some fields set") 108 109 parentPolicy := &ParentPolicy{ 110 KeyID: 11, 111 Name: "parent-my-name", 112 Namespace: "ns-pp", 113 Categories: []string{"cate-1", "cate-2"}, 114 Controls: []string{"control-1", "control-2"}, 115 Standards: []string{"stand-1", "stand-2"}, 116 } 117 118 // Test All fields set 119 ce = ComplianceEvent{ 120 EventID: 1, 121 ParentPolicy: parentPolicy, 122 Event: EventDetails{ 123 Compliance: "cp1", 124 Message: "event1 message", 125 Metadata: JSONMap{ 126 "pet": "cat1", 127 "flower": []string{"rose", "sunflower"}, 128 "number": 1, 129 }, 130 ReportedBy: &reportBy, 131 Timestamp: theTime, 132 }, 133 Cluster: Cluster{ 134 ClusterID: "22", 135 Name: "cluster1", 136 }, 137 Policy: Policy{ 138 KeyID: 0, 139 Kind: "configuration", 140 APIGroup: "v1", 141 Name: "policy-name", 142 Spec: JSONMap{ 143 "name": "hi", 144 "namespace": "cat-1", 145 }, 146 }, 147 } 148 149 values = convertToCsvLine(&ce, true) 150 g.Expect(values).Should(Equal([]string{ 151 "1", "cp1", "event1 message", 152 "{\n \"flower\": [\n \"rose\",\n \"sunflower\"\n ],\n \"number\": 1,\n \"pet\": \"cat1\"\n}", 153 "cat1", "2021-08-15 14:30:45.0000001 +0000 UTC", "22", "cluster1", 154 "11", "parent-my-name", "ns-pp", "cate-1, cate-2", 155 "control-1, control-2", "stand-1, stand-2", "", 156 "v1", "configuration", "policy-name", "", "", 157 "{\n \"name\": \"hi\",\n \"namespace\": \"cat-1\"\n}", 158 }), "Test All fields set") 159 } 160 161 func TestGetCsvHeader(t *testing.T) { 162 g := NewWithT(t) 163 164 result := getCsvHeader(true) 165 g.Expect(result).Should(HaveLen(21)) 166 g.Expect(result).Should(Equal([]string{ 167 "compliance_events_id", 168 "compliance_events_compliance", 169 "compliance_events_message", "compliance_events_metadata", 170 "compliance_events_reported_by", "compliance_events_timestamp", "clusters_cluster_id", 171 "clusters_name", "parent_policies_id", "parent_policies_name", 172 "parent_policies_namespace", "parent_policies_categories", "parent_policies_controls", 173 "parent_policies_standards", "policies_id", "policies_api_group", "policies_kind", "policies_name", 174 "policies_namespace", "policies_severity", "policies_spec", 175 })) 176 177 result = getCsvHeader(false) 178 g.Expect(result).Should(HaveLen(20)) 179 }