|
15 | 15 | package kyverno |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "os" |
| 19 | + "path/filepath" |
18 | 20 | "testing" |
19 | 21 |
|
20 | 22 | "github.com/stretchr/testify/assert" |
@@ -201,3 +203,185 @@ func TestConvertPolicySpecToYAML(t *testing.T) { |
201 | 203 | assert.NoError(t, err) |
202 | 204 | assert.Equal(t, "apiVersion: kyverno.io/v1\nkind: ClusterPolicy\nmetadata:\n name: test-policy\nspec:\n metadata:\n name: test-policy", yaml) |
203 | 205 | } |
| 206 | + |
| 207 | +func TestReadPolicyFromFileWithLabelsAndAnnotations(t *testing.T) { |
| 208 | + // Create a temporary directory and file for testing |
| 209 | + tmpDir := t.TempDir() |
| 210 | + policyContent := `apiVersion: kyverno.io/v1 |
| 211 | +kind: ClusterPolicy |
| 212 | +metadata: |
| 213 | + name: policy-with-metadata |
| 214 | + labels: |
| 215 | + app: my-app |
| 216 | + environment: production |
| 217 | + team: security |
| 218 | + annotations: |
| 219 | + description: "This is a test policy with labels and annotations" |
| 220 | + owner: platform-team |
| 221 | +spec: |
| 222 | + validationFailureAction: enforce |
| 223 | + background: true |
| 224 | + rules: |
| 225 | + - name: check-labels |
| 226 | + match: |
| 227 | + any: |
| 228 | + - resources: |
| 229 | + kinds: |
| 230 | + - Pod |
| 231 | + validate: |
| 232 | + message: "Labels are required" |
| 233 | + pattern: |
| 234 | + metadata: |
| 235 | + labels: |
| 236 | + app: "?*" |
| 237 | +` |
| 238 | + policyFile := filepath.Join(tmpDir, "policy-with-metadata.yaml") |
| 239 | + err := os.WriteFile(policyFile, []byte(policyContent), 0644) |
| 240 | + assert.NoError(t, err) |
| 241 | + |
| 242 | + policy, err := readPolicyFromFile(policyFile) |
| 243 | + assert.NoError(t, err) |
| 244 | + |
| 245 | + // Verify basic fields |
| 246 | + assert.Equal(t, "policy-with-metadata", policy.Name) |
| 247 | + assert.Equal(t, "ClusterPolicy", policy.Kind) |
| 248 | + assert.Equal(t, "kyverno.io/v1", policy.APIVersion) |
| 249 | + |
| 250 | + // Verify labels are extracted |
| 251 | + assert.NotNil(t, policy.Labels) |
| 252 | + assert.Equal(t, "my-app", policy.Labels["app"]) |
| 253 | + assert.Equal(t, "production", policy.Labels["environment"]) |
| 254 | + assert.Equal(t, "security", policy.Labels["team"]) |
| 255 | + |
| 256 | + // Verify annotations are extracted |
| 257 | + assert.NotNil(t, policy.Annotations) |
| 258 | + assert.Equal(t, "This is a test policy with labels and annotations", policy.Annotations["description"]) |
| 259 | + assert.Equal(t, "platform-team", policy.Annotations["owner"]) |
| 260 | + |
| 261 | + // Verify spec is extracted |
| 262 | + assert.NotNil(t, policy.Spec) |
| 263 | +} |
| 264 | + |
| 265 | +func TestReadPolicyFromFileWithoutLabelsAndAnnotations(t *testing.T) { |
| 266 | + policy, err := readPolicyFromFile("testdata/disallow-privileged.yaml") |
| 267 | + assert.NoError(t, err) |
| 268 | + |
| 269 | + // Verify basic fields |
| 270 | + assert.Equal(t, "disallow-privileged", policy.Name) |
| 271 | + assert.Equal(t, "Policy", policy.Kind) |
| 272 | + assert.Equal(t, "kyverno.io/v1", policy.APIVersion) |
| 273 | + |
| 274 | + // Labels and annotations should be nil or empty |
| 275 | + assert.Empty(t, policy.Labels) |
| 276 | + assert.Empty(t, policy.Annotations) |
| 277 | + |
| 278 | + // Verify spec is extracted |
| 279 | + assert.NotNil(t, policy.Spec) |
| 280 | +} |
| 281 | + |
| 282 | +func TestReadPolicyFromFileWithNamespaceAndFullMetadata(t *testing.T) { |
| 283 | + // Create a temporary directory and file for testing |
| 284 | + tmpDir := t.TempDir() |
| 285 | + policyContent := `apiVersion: kyverno.io/v1 |
| 286 | +kind: Policy |
| 287 | +metadata: |
| 288 | + name: namespaced-policy |
| 289 | + namespace: my-namespace |
| 290 | + labels: |
| 291 | + app: test-app |
| 292 | + annotations: |
| 293 | + description: "A namespaced policy" |
| 294 | + generateName: test-prefix- |
| 295 | + finalizers: |
| 296 | + - kyverno.io/finalizer |
| 297 | +spec: |
| 298 | + validationFailureAction: enforce |
| 299 | + background: true |
| 300 | + rules: |
| 301 | + - name: check-labels |
| 302 | + match: |
| 303 | + any: |
| 304 | + - resources: |
| 305 | + kinds: |
| 306 | + - Pod |
| 307 | + validate: |
| 308 | + message: "Labels are required" |
| 309 | + pattern: |
| 310 | + metadata: |
| 311 | + labels: |
| 312 | + app: "?*" |
| 313 | +` |
| 314 | + policyFile := filepath.Join(tmpDir, "namespaced-policy.yaml") |
| 315 | + err := os.WriteFile(policyFile, []byte(policyContent), 0644) |
| 316 | + assert.NoError(t, err) |
| 317 | + |
| 318 | + policy, err := readPolicyFromFile(policyFile) |
| 319 | + assert.NoError(t, err) |
| 320 | + |
| 321 | + // Verify basic fields |
| 322 | + assert.Equal(t, "namespaced-policy", policy.Name) |
| 323 | + assert.Equal(t, "Policy", policy.Kind) |
| 324 | + assert.Equal(t, "kyverno.io/v1", policy.APIVersion) |
| 325 | + |
| 326 | + // Verify namespace is extracted |
| 327 | + assert.Equal(t, "my-namespace", policy.Namespace) |
| 328 | + |
| 329 | + // Verify labels are extracted |
| 330 | + assert.NotNil(t, policy.Labels) |
| 331 | + assert.Equal(t, "test-app", policy.Labels["app"]) |
| 332 | + |
| 333 | + // Verify annotations are extracted |
| 334 | + assert.NotNil(t, policy.Annotations) |
| 335 | + assert.Equal(t, "A namespaced policy", policy.Annotations["description"]) |
| 336 | + |
| 337 | + // Verify full metadata is preserved (including generateName, finalizers, etc.) |
| 338 | + assert.NotNil(t, policy.Metadata) |
| 339 | + assert.Equal(t, "namespaced-policy", policy.Metadata["name"]) |
| 340 | + assert.Equal(t, "my-namespace", policy.Metadata["namespace"]) |
| 341 | + assert.Equal(t, "test-prefix-", policy.Metadata["generateName"]) |
| 342 | + |
| 343 | + // Check finalizers are preserved |
| 344 | + finalizers, ok := policy.Metadata["finalizers"].([]interface{}) |
| 345 | + assert.True(t, ok, "finalizers should be a slice") |
| 346 | + assert.Len(t, finalizers, 1) |
| 347 | + assert.Equal(t, "kyverno.io/finalizer", finalizers[0]) |
| 348 | + |
| 349 | + // Verify spec is extracted |
| 350 | + assert.NotNil(t, policy.Spec) |
| 351 | +} |
| 352 | + |
| 353 | +func TestToKyvernoPolicyInputPreservesAllFields(t *testing.T) { |
| 354 | + policy := KyvernoPolicy{ |
| 355 | + Name: "test-policy", |
| 356 | + Kind: "Policy", |
| 357 | + APIVersion: "kyverno.io/v1", |
| 358 | + Namespace: "test-namespace", |
| 359 | + Labels: map[string]any{ |
| 360 | + "app": "my-app", |
| 361 | + }, |
| 362 | + Annotations: map[string]any{ |
| 363 | + "description": "Test policy", |
| 364 | + }, |
| 365 | + Metadata: map[string]any{ |
| 366 | + "name": "test-policy", |
| 367 | + "namespace": "test-namespace", |
| 368 | + "generateName": "prefix-", |
| 369 | + }, |
| 370 | + Spec: map[string]any{ |
| 371 | + "validationFailureAction": "enforce", |
| 372 | + }, |
| 373 | + } |
| 374 | + |
| 375 | + input := policy.ToKyvernoPolicyInput() |
| 376 | + |
| 377 | + // Verify all fields are preserved |
| 378 | + assert.Equal(t, "test-policy", input.Name) |
| 379 | + assert.Equal(t, "Policy", input.Kind) |
| 380 | + assert.Equal(t, "kyverno.io/v1", input.APIVersion) |
| 381 | + assert.Equal(t, "test-namespace", input.Namespace) |
| 382 | + assert.Equal(t, "my-app", input.Labels["app"]) |
| 383 | + assert.Equal(t, "Test policy", input.Annotations["description"]) |
| 384 | + assert.NotNil(t, input.Metadata) |
| 385 | + assert.Equal(t, "prefix-", input.Metadata["generateName"]) |
| 386 | + assert.NotNil(t, input.Spec) |
| 387 | +} |
0 commit comments