@@ -52,91 +52,102 @@ func createModifiedPolicy(mod policyModFunc) *ngfAPI.ObservabilityPolicy {
5252}
5353
5454func TestValidator_Validate (t * testing.T ) {
55+ validCtx := & policies.ValidationContext {
56+ NginxProxyValid : true ,
57+ TelemetryEnabled : true ,
58+ }
59+
5560 tests := []struct {
56- name string
57- policy * ngfAPI.ObservabilityPolicy
58- globalSettings * policies.GlobalPolicySettings
59- expErrSubstrings []string
61+ name string
62+ policy * ngfAPI.ObservabilityPolicy
63+ policyValidationCtx * policies.ValidationContext
64+ expErrSubstrings []string
6065 }{
6166 {
62- name : "global settings are nil" ,
67+ name : "validation context is nil" ,
6368 policy : createValidPolicy (),
6469 expErrSubstrings : []string {"NginxProxy configuration is either invalid or not attached" },
6570 },
6671 {
67- name : "global settings are invalid" ,
68- policy : createValidPolicy (),
69- globalSettings : & policies.GlobalPolicySettings {NginxProxyValid : false },
70- expErrSubstrings : []string {"NginxProxy configuration is either invalid or not attached" },
72+ name : "validation context is invalid" ,
73+ policy : createValidPolicy (),
74+ policyValidationCtx : & policies.ValidationContext {NginxProxyValid : false },
75+ expErrSubstrings : []string {"NginxProxy configuration is either invalid or not attached" },
76+ },
77+ {
78+ name : "telemetry is not enabled" ,
79+ policy : createValidPolicy (),
80+ policyValidationCtx : & policies.ValidationContext {NginxProxyValid : true , TelemetryEnabled : false },
81+ expErrSubstrings : []string {"Telemetry is not enabled" },
7182 },
7283 {
7384 name : "invalid target ref; unsupported group" ,
7485 policy : createModifiedPolicy (func (p * ngfAPI.ObservabilityPolicy ) * ngfAPI.ObservabilityPolicy {
7586 p .Spec .TargetRefs [0 ].Group = "Unsupported"
7687 return p
7788 }),
78- globalSettings : & policies. GlobalPolicySettings { NginxProxyValid : true } ,
79- expErrSubstrings : []string {"spec.targetRefs.group" },
89+ policyValidationCtx : validCtx ,
90+ expErrSubstrings : []string {"spec.targetRefs.group" },
8091 },
8192 {
8293 name : "invalid target ref; unsupported kind" ,
8394 policy : createModifiedPolicy (func (p * ngfAPI.ObservabilityPolicy ) * ngfAPI.ObservabilityPolicy {
8495 p .Spec .TargetRefs [0 ].Kind = "Unsupported"
8596 return p
8697 }),
87- globalSettings : & policies. GlobalPolicySettings { NginxProxyValid : true } ,
88- expErrSubstrings : []string {"spec.targetRefs.kind" },
98+ policyValidationCtx : validCtx ,
99+ expErrSubstrings : []string {"spec.targetRefs.kind" },
89100 },
90101 {
91102 name : "invalid strategy" ,
92103 policy : createModifiedPolicy (func (p * ngfAPI.ObservabilityPolicy ) * ngfAPI.ObservabilityPolicy {
93104 p .Spec .Tracing .Strategy = "invalid"
94105 return p
95106 }),
96- globalSettings : & policies. GlobalPolicySettings { NginxProxyValid : true } ,
97- expErrSubstrings : []string {"spec.tracing.strategy" },
107+ policyValidationCtx : validCtx ,
108+ expErrSubstrings : []string {"spec.tracing.strategy" },
98109 },
99110 {
100111 name : "invalid context" ,
101112 policy : createModifiedPolicy (func (p * ngfAPI.ObservabilityPolicy ) * ngfAPI.ObservabilityPolicy {
102113 p .Spec .Tracing .Context = helpers.GetPointer [ngfAPI.TraceContext ]("invalid" )
103114 return p
104115 }),
105- globalSettings : & policies. GlobalPolicySettings { NginxProxyValid : true } ,
106- expErrSubstrings : []string {"spec.tracing.context" },
116+ policyValidationCtx : validCtx ,
117+ expErrSubstrings : []string {"spec.tracing.context" },
107118 },
108119 {
109120 name : "invalid span name" ,
110121 policy : createModifiedPolicy (func (p * ngfAPI.ObservabilityPolicy ) * ngfAPI.ObservabilityPolicy {
111122 p .Spec .Tracing .SpanName = helpers .GetPointer ("invalid$$$" )
112123 return p
113124 }),
114- globalSettings : & policies. GlobalPolicySettings { NginxProxyValid : true } ,
115- expErrSubstrings : []string {"spec.tracing.spanName" },
125+ policyValidationCtx : validCtx ,
126+ expErrSubstrings : []string {"spec.tracing.spanName" },
116127 },
117128 {
118129 name : "invalid span attribute key" ,
119130 policy : createModifiedPolicy (func (p * ngfAPI.ObservabilityPolicy ) * ngfAPI.ObservabilityPolicy {
120131 p .Spec .Tracing .SpanAttributes [0 ].Key = "invalid$$$"
121132 return p
122133 }),
123- globalSettings : & policies. GlobalPolicySettings { NginxProxyValid : true } ,
124- expErrSubstrings : []string {"spec.tracing.spanAttributes.key" },
134+ policyValidationCtx : validCtx ,
135+ expErrSubstrings : []string {"spec.tracing.spanAttributes.key" },
125136 },
126137 {
127138 name : "invalid span attribute value" ,
128139 policy : createModifiedPolicy (func (p * ngfAPI.ObservabilityPolicy ) * ngfAPI.ObservabilityPolicy {
129140 p .Spec .Tracing .SpanAttributes [0 ].Value = "invalid$$$"
130141 return p
131142 }),
132- globalSettings : & policies. GlobalPolicySettings { NginxProxyValid : true } ,
133- expErrSubstrings : []string {"spec.tracing.spanAttributes.value" },
143+ policyValidationCtx : validCtx ,
144+ expErrSubstrings : []string {"spec.tracing.spanAttributes.value" },
134145 },
135146 {
136- name : "valid" ,
137- policy : createValidPolicy (),
138- globalSettings : & policies. GlobalPolicySettings { NginxProxyValid : true } ,
139- expErrSubstrings : nil ,
147+ name : "valid" ,
148+ policy : createValidPolicy (),
149+ policyValidationCtx : validCtx ,
150+ expErrSubstrings : nil ,
140151 },
141152 }
142153
@@ -146,7 +157,7 @@ func TestValidator_Validate(t *testing.T) {
146157 t .Run (test .name , func (t * testing.T ) {
147158 g := NewWithT (t )
148159
149- conds := v .Validate (test .policy , test .globalSettings )
160+ conds := v .Validate (test .policy , test .policyValidationCtx )
150161
151162 if len (test .expErrSubstrings ) == 0 {
152163 g .Expect (conds ).To (BeEmpty ())
0 commit comments