open-cluster-management.io/governance-policy-propagator@v0.13.0/controllers/complianceeventsapi/types_test.go (about) 1 package complianceeventsapi 2 3 import ( 4 "strings" 5 "testing" 6 "time" 7 ) 8 9 func TestClusterValidation(t *testing.T) { 10 tests := map[string]struct { 11 obj Cluster 12 errMsg string 13 }{ 14 "no name": {Cluster{ClusterID: "foo"}, "field not provided: cluster.name"}, 15 "no cluster id": {Cluster{Name: "foo"}, "field not provided: cluster.cluster_id"}, 16 } 17 18 for input, tc := range tests { 19 t.Run(input, func(t *testing.T) { 20 err := tc.obj.Validate() 21 if err == nil { 22 t.Fatal("expected error") 23 } 24 25 if !strings.Contains(err.Error(), tc.errMsg) { 26 t.Fatal("expected error to include", tc.errMsg, "in the error string; got", err.Error()) 27 } 28 }) 29 } 30 } 31 32 func TestEventDetailsValidation(t *testing.T) { 33 tests := map[string]struct { 34 obj EventDetails 35 errMsg string 36 }{ 37 "no compliance": { 38 EventDetails{Message: "hello", Timestamp: time.Now()}, 39 "field not provided: event.compliance", 40 }, 41 "bad compliance": { 42 EventDetails{Compliance: "bad", Message: "hello", Timestamp: time.Now()}, 43 "should be Compliant, NonCompliant, Disabled, or Pending got bad", 44 }, 45 "no message": { 46 EventDetails{Compliance: "Compliant", Timestamp: time.Now()}, 47 "field not provided: event.message", 48 }, 49 "no timestamp": { 50 EventDetails{Compliance: "Compliant", Message: "hello"}, 51 "field not provided: event.timestamp", 52 }, 53 } 54 55 for input, tc := range tests { 56 t.Run(input, func(t *testing.T) { 57 err := tc.obj.Validate() 58 if err == nil { 59 t.Fatal("expected error") 60 } 61 62 if !strings.Contains(err.Error(), tc.errMsg) { 63 t.Fatal("expected error to include", tc.errMsg, "in the error string; got", err.Error()) 64 } 65 }) 66 } 67 } 68 69 func TestParentPolicyValidation(t *testing.T) { 70 tests := map[string]struct { 71 obj ParentPolicy 72 errMsg string 73 }{ 74 "no name": { 75 ParentPolicy{Namespace: "policies", Categories: []string{"hello"}}, 76 "field not provided: parent_policy.name", 77 }, 78 "no namespace": { 79 ParentPolicy{Name: "my-policy", Categories: []string{"hello"}}, 80 "field not provided: parent_policy.namespace", 81 }, 82 } 83 84 for input, tc := range tests { 85 t.Run(input, func(t *testing.T) { 86 err := tc.obj.Validate() 87 if err == nil { 88 t.Fatal("expected error") 89 } 90 91 if !strings.Contains(err.Error(), tc.errMsg) { 92 t.Fatal("expected error to include", tc.errMsg, "in the error string; got", err.Error()) 93 } 94 }) 95 } 96 } 97 98 func TestPolicyValidation(t *testing.T) { 99 var basespec JSONMap = map[string]interface{}{"test": "one", "severity": "low"} 100 101 tests := map[string]struct { 102 obj Policy 103 errMsg string 104 }{ 105 "no name": { 106 Policy{ 107 Kind: "policy", 108 APIGroup: "v1", 109 Spec: basespec, 110 }, 111 "field not provided: policy.name", 112 }, 113 "no API group": { 114 Policy{ 115 Kind: "policy", 116 Name: "foobar", 117 Spec: basespec, 118 }, 119 "field not provided: policy.apiGroup", 120 }, 121 "no kind": { 122 Policy{ 123 APIGroup: "v1", 124 Name: "foobar", 125 Spec: basespec, 126 }, 127 "field not provided: policy.kind", 128 }, 129 "no spec or hash": { 130 Policy{ 131 Kind: "policy", 132 APIGroup: "v1", 133 Name: "foobar", 134 }, 135 "field not provided: policy.spec", 136 }, 137 } 138 139 for input, tc := range tests { 140 t.Run(input, func(t *testing.T) { 141 err := tc.obj.Validate() 142 if err == nil { 143 t.Fatal("expected error") 144 } 145 146 if !strings.Contains(err.Error(), tc.errMsg) { 147 t.Fatal("expected error to include", tc.errMsg, "in the error string; got", err.Error()) 148 } 149 }) 150 } 151 }