@@ -52,91 +52,102 @@ func createModifiedPolicy(mod policyModFunc) *ngfAPI.ObservabilityPolicy {
52
52
}
53
53
54
54
func TestValidator_Validate (t * testing.T ) {
55
+ validCtx := & policies.ValidationContext {
56
+ NginxProxyValid : true ,
57
+ TelemetryEnabled : true ,
58
+ }
59
+
55
60
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
60
65
}{
61
66
{
62
- name : "global settings are nil" ,
67
+ name : "validation context is nil" ,
63
68
policy : createValidPolicy (),
64
69
expErrSubstrings : []string {"NginxProxy configuration is either invalid or not attached" },
65
70
},
66
71
{
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" },
71
82
},
72
83
{
73
84
name : "invalid target ref; unsupported group" ,
74
85
policy : createModifiedPolicy (func (p * ngfAPI.ObservabilityPolicy ) * ngfAPI.ObservabilityPolicy {
75
86
p .Spec .TargetRefs [0 ].Group = "Unsupported"
76
87
return p
77
88
}),
78
- globalSettings : & policies. GlobalPolicySettings { NginxProxyValid : true } ,
79
- expErrSubstrings : []string {"spec.targetRefs.group" },
89
+ policyValidationCtx : validCtx ,
90
+ expErrSubstrings : []string {"spec.targetRefs.group" },
80
91
},
81
92
{
82
93
name : "invalid target ref; unsupported kind" ,
83
94
policy : createModifiedPolicy (func (p * ngfAPI.ObservabilityPolicy ) * ngfAPI.ObservabilityPolicy {
84
95
p .Spec .TargetRefs [0 ].Kind = "Unsupported"
85
96
return p
86
97
}),
87
- globalSettings : & policies. GlobalPolicySettings { NginxProxyValid : true } ,
88
- expErrSubstrings : []string {"spec.targetRefs.kind" },
98
+ policyValidationCtx : validCtx ,
99
+ expErrSubstrings : []string {"spec.targetRefs.kind" },
89
100
},
90
101
{
91
102
name : "invalid strategy" ,
92
103
policy : createModifiedPolicy (func (p * ngfAPI.ObservabilityPolicy ) * ngfAPI.ObservabilityPolicy {
93
104
p .Spec .Tracing .Strategy = "invalid"
94
105
return p
95
106
}),
96
- globalSettings : & policies. GlobalPolicySettings { NginxProxyValid : true } ,
97
- expErrSubstrings : []string {"spec.tracing.strategy" },
107
+ policyValidationCtx : validCtx ,
108
+ expErrSubstrings : []string {"spec.tracing.strategy" },
98
109
},
99
110
{
100
111
name : "invalid context" ,
101
112
policy : createModifiedPolicy (func (p * ngfAPI.ObservabilityPolicy ) * ngfAPI.ObservabilityPolicy {
102
113
p .Spec .Tracing .Context = helpers.GetPointer [ngfAPI.TraceContext ]("invalid" )
103
114
return p
104
115
}),
105
- globalSettings : & policies. GlobalPolicySettings { NginxProxyValid : true } ,
106
- expErrSubstrings : []string {"spec.tracing.context" },
116
+ policyValidationCtx : validCtx ,
117
+ expErrSubstrings : []string {"spec.tracing.context" },
107
118
},
108
119
{
109
120
name : "invalid span name" ,
110
121
policy : createModifiedPolicy (func (p * ngfAPI.ObservabilityPolicy ) * ngfAPI.ObservabilityPolicy {
111
122
p .Spec .Tracing .SpanName = helpers .GetPointer ("invalid$$$" )
112
123
return p
113
124
}),
114
- globalSettings : & policies. GlobalPolicySettings { NginxProxyValid : true } ,
115
- expErrSubstrings : []string {"spec.tracing.spanName" },
125
+ policyValidationCtx : validCtx ,
126
+ expErrSubstrings : []string {"spec.tracing.spanName" },
116
127
},
117
128
{
118
129
name : "invalid span attribute key" ,
119
130
policy : createModifiedPolicy (func (p * ngfAPI.ObservabilityPolicy ) * ngfAPI.ObservabilityPolicy {
120
131
p .Spec .Tracing .SpanAttributes [0 ].Key = "invalid$$$"
121
132
return p
122
133
}),
123
- globalSettings : & policies. GlobalPolicySettings { NginxProxyValid : true } ,
124
- expErrSubstrings : []string {"spec.tracing.spanAttributes.key" },
134
+ policyValidationCtx : validCtx ,
135
+ expErrSubstrings : []string {"spec.tracing.spanAttributes.key" },
125
136
},
126
137
{
127
138
name : "invalid span attribute value" ,
128
139
policy : createModifiedPolicy (func (p * ngfAPI.ObservabilityPolicy ) * ngfAPI.ObservabilityPolicy {
129
140
p .Spec .Tracing .SpanAttributes [0 ].Value = "invalid$$$"
130
141
return p
131
142
}),
132
- globalSettings : & policies. GlobalPolicySettings { NginxProxyValid : true } ,
133
- expErrSubstrings : []string {"spec.tracing.spanAttributes.value" },
143
+ policyValidationCtx : validCtx ,
144
+ expErrSubstrings : []string {"spec.tracing.spanAttributes.value" },
134
145
},
135
146
{
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 ,
140
151
},
141
152
}
142
153
@@ -146,7 +157,7 @@ func TestValidator_Validate(t *testing.T) {
146
157
t .Run (test .name , func (t * testing.T ) {
147
158
g := NewWithT (t )
148
159
149
- conds := v .Validate (test .policy , test .globalSettings )
160
+ conds := v .Validate (test .policy , test .policyValidationCtx )
150
161
151
162
if len (test .expErrSubstrings ) == 0 {
152
163
g .Expect (conds ).To (BeEmpty ())
0 commit comments