github.com/dhaiducek/policy-generator-plugin@v1.99.99/internal/ordering_test.go (about) 1 package internal 2 3 import ( 4 "bytes" 5 "embed" 6 "testing" 7 "text/template" 8 ) 9 10 func mockProcess(filePath, generator string) ([]byte, error) { 11 p := Plugin{} 12 13 tmpl, err := template.New("generator").Parse(generator) 14 if err != nil { 15 return nil, err 16 } 17 18 genBytes := &bytes.Buffer{} 19 20 templateVars := struct { 21 Dir string 22 }{ 23 Dir: filePath, 24 } 25 26 err = tmpl.Execute(genBytes, templateVars) 27 if err != nil { 28 return nil, err 29 } 30 31 err = p.Config(genBytes.Bytes(), filePath) 32 if err != nil { 33 return nil, err 34 } 35 36 return p.Generate() 37 } 38 39 type genOutTest struct { 40 tmpDir string 41 generator string 42 wantFile string 43 wantErr string 44 } 45 46 func (g genOutTest) run(t *testing.T) { 47 t.Parallel() 48 49 gotVal, gotErr := mockProcess(g.tmpDir, g.generator) 50 if gotErr != nil { 51 if g.wantErr != gotErr.Error() { 52 t.Fatalf("expected err %v, got %v", g.wantErr, gotErr) 53 } else { 54 return 55 } 56 } 57 58 want, err := wantedOutputs.ReadFile(g.wantFile) 59 if err != nil { 60 t.Fatalf("could not read wanted test output %v, err: %v", g.wantFile, err) 61 } 62 63 assertEqualYaml(t, want, gotVal) 64 } 65 66 //go:embed testdata/ordering/* 67 var wantedOutputs embed.FS 68 69 func TestOrderPolicies(t *testing.T) { 70 t.Parallel() 71 tmpDir := t.TempDir() 72 createConfigMap(t, tmpDir, "configmap.yaml") 73 74 tests := map[string]genOutTest{ 75 "one ordered policy": { 76 tmpDir: tmpDir, 77 generator: ` 78 apiVersion: policy.open-cluster-management.io/v1 79 kind: PolicyGenerator 80 metadata: 81 name: test 82 policyDefaults: 83 orderPolicies: true 84 namespace: my-policies 85 policies: 86 - name: one 87 manifests: 88 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 89 `, 90 wantFile: "testdata/ordering/one-ordered-policy.yaml", 91 wantErr: "", 92 }, 93 "two ordered policies": { 94 tmpDir: tmpDir, 95 generator: ` 96 apiVersion: policy.open-cluster-management.io/v1 97 kind: PolicyGenerator 98 metadata: 99 name: test 100 policyDefaults: 101 orderPolicies: true 102 namespace: my-policies 103 policies: 104 - name: one 105 manifests: 106 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 107 - name: two 108 manifests: 109 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 110 `, 111 wantFile: "testdata/ordering/two-ordered-policies.yaml", 112 wantErr: "", 113 }, 114 "policyDefaults dependencies and orderPolicies": { 115 tmpDir: tmpDir, 116 generator: ` 117 apiVersion: policy.open-cluster-management.io/v1 118 kind: PolicyGenerator 119 metadata: 120 name: test 121 policyDefaults: 122 orderPolicies: true 123 dependencies: 124 - name: foo 125 namespace: my-policies 126 policies: 127 - name: one 128 manifests: 129 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 130 - name: two 131 manifests: 132 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 133 `, 134 wantFile: "", 135 wantErr: "policyDefaults must specify only one of dependencies or orderPolicies", 136 }, 137 "policy dependencies and orderPolicies": { 138 tmpDir: tmpDir, 139 generator: ` 140 apiVersion: policy.open-cluster-management.io/v1 141 kind: PolicyGenerator 142 metadata: 143 name: test 144 policyDefaults: 145 orderPolicies: true 146 namespace: my-policies 147 policies: 148 - name: one 149 dependencies: 150 - name: foo 151 manifests: 152 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 153 - name: two 154 manifests: 155 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 156 `, 157 wantFile: "", 158 wantErr: "dependencies may not be set in policy one when policyDefaults.orderPolicies is true", 159 }, 160 } 161 162 for name := range tests { 163 t.Run(name, tests[name].run) 164 } 165 } 166 167 func TestDependencies(t *testing.T) { 168 t.Parallel() 169 tmpDir := t.TempDir() 170 createConfigMap(t, tmpDir, "configmap.yaml") 171 172 tests := map[string]genOutTest{ 173 "policyDefaults go in both policies": { 174 tmpDir: tmpDir, 175 generator: ` 176 apiVersion: policy.open-cluster-management.io/v1 177 kind: PolicyGenerator 178 metadata: 179 name: test 180 policyDefaults: 181 dependencies: 182 - name: foo 183 namespace: my-policies 184 policies: 185 - name: one 186 manifests: 187 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 188 - name: two 189 manifests: 190 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 191 `, 192 wantFile: "testdata/ordering/default-deps-propagated.yaml", 193 wantErr: "", 194 }, 195 "additional dependency details are configurable": { 196 tmpDir: tmpDir, 197 generator: ` 198 apiVersion: policy.open-cluster-management.io/v1 199 kind: PolicyGenerator 200 metadata: 201 name: test 202 policyDefaults: 203 dependencies: 204 - apiVersion: fake.test.io/v2 205 compliance: Pending 206 kind: FakeThing 207 name: foo 208 namespace: bar 209 namespace: my-policies 210 policies: 211 - name: one 212 manifests: 213 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 214 `, 215 wantFile: "testdata/ordering/dependency-details-config.yaml", 216 wantErr: "", 217 }, 218 "one policy can override policyDefaults": { 219 tmpDir: tmpDir, 220 generator: ` 221 apiVersion: policy.open-cluster-management.io/v1 222 kind: PolicyGenerator 223 metadata: 224 name: test 225 policyDefaults: 226 dependencies: 227 - name: foo 228 namespace: my-policies 229 policies: 230 - name: one 231 dependencies: 232 - name: bar 233 compliance: NonCompliant 234 manifests: 235 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 236 - name: two 237 manifests: 238 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 239 `, 240 wantFile: "testdata/ordering/override-dependencies.yaml", 241 wantErr: "", 242 }, 243 "dependencies are configurable at the policy level": { 244 tmpDir: tmpDir, 245 generator: ` 246 apiVersion: policy.open-cluster-management.io/v1 247 kind: PolicyGenerator 248 metadata: 249 name: test 250 policyDefaults: 251 namespace: my-policies 252 policies: 253 - name: one 254 dependencies: 255 - name: baz 256 namespace: default 257 manifests: 258 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 259 `, 260 wantFile: "testdata/ordering/policy-level-dependencies.yaml", 261 wantErr: "", 262 }, 263 } 264 265 for name := range tests { 266 t.Run(name, tests[name].run) 267 } 268 } 269 270 func TestIgnorePending(t *testing.T) { 271 t.Parallel() 272 tmpDir := t.TempDir() 273 createConfigMap(t, tmpDir, "configmap.yaml") 274 275 tests := map[string]genOutTest{ 276 "policyDefaults.ignorePending is propagated to all manifests": { 277 tmpDir: tmpDir, 278 generator: ` 279 apiVersion: policy.open-cluster-management.io/v1 280 kind: PolicyGenerator 281 metadata: 282 name: test 283 policyDefaults: 284 consolidateManifests: false 285 ignorePending: true 286 namespace: my-policies 287 policies: 288 - name: one 289 manifests: 290 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 291 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 292 - name: two 293 manifests: 294 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 295 `, 296 wantFile: "testdata/ordering/ignore-pending-propagation.yaml", 297 wantErr: "", 298 }, 299 "policyDefaults.ignorePending is propagated with consolidated manifests": { 300 tmpDir: tmpDir, 301 generator: ` 302 apiVersion: policy.open-cluster-management.io/v1 303 kind: PolicyGenerator 304 metadata: 305 name: test 306 policyDefaults: 307 consolidateManifests: true 308 ignorePending: true 309 namespace: my-policies 310 policies: 311 - name: one 312 manifests: 313 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 314 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 315 - name: two 316 ignorePending: false 317 manifests: 318 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 319 `, 320 wantFile: "testdata/ordering/ignore-pending-policy-consolidated.yaml", 321 wantErr: "", 322 }, 323 "policyDefaults.ignorePending can be overridden at policy level": { 324 tmpDir: tmpDir, 325 generator: ` 326 apiVersion: policy.open-cluster-management.io/v1 327 kind: PolicyGenerator 328 metadata: 329 name: test 330 policyDefaults: 331 consolidateManifests: false 332 ignorePending: true 333 namespace: my-policies 334 policies: 335 - name: one 336 manifests: 337 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 338 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 339 - name: two 340 ignorePending: false 341 manifests: 342 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 343 `, 344 wantFile: "testdata/ordering/ignore-pending-policy-override.yaml", 345 wantErr: "", 346 }, 347 "policyDefaults.ignorePending can be overridden at manifest level": { 348 tmpDir: tmpDir, 349 generator: ` 350 apiVersion: policy.open-cluster-management.io/v1 351 kind: PolicyGenerator 352 metadata: 353 name: test 354 policyDefaults: 355 consolidateManifests: false 356 namespace: my-policies 357 policies: 358 - name: one 359 manifests: 360 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 361 ignorePending: true 362 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 363 - name: two 364 ignorePending: true 365 manifests: 366 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 367 `, 368 wantFile: "testdata/ordering/ignore-pending-manifest-override.yaml", 369 wantErr: "", 370 }, 371 } 372 373 for name := range tests { 374 t.Run(name, tests[name].run) 375 } 376 } 377 378 func TestOrderManifests(t *testing.T) { 379 t.Parallel() 380 tmpDir := t.TempDir() 381 createConfigMap(t, tmpDir, "configmap.yaml") 382 383 tests := map[string]genOutTest{ 384 "orderManifests from policyDefaults": { 385 tmpDir: tmpDir, 386 generator: ` 387 apiVersion: policy.open-cluster-management.io/v1 388 kind: PolicyGenerator 389 metadata: 390 name: test 391 policyDefaults: 392 consolidateManifests: false 393 orderManifests: true 394 namespace: my-policies 395 policies: 396 - name: one 397 manifests: 398 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 399 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 400 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 401 `, 402 wantFile: "testdata/ordering/three-ordered-manifests.yaml", 403 wantErr: "", 404 }, 405 "orderManifests from policy setting": { 406 tmpDir: tmpDir, 407 generator: ` 408 apiVersion: policy.open-cluster-management.io/v1 409 kind: PolicyGenerator 410 metadata: 411 name: test 412 policyDefaults: 413 consolidateManifests: false 414 namespace: my-policies 415 policies: 416 - name: one 417 orderManifests: true 418 manifests: 419 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 420 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 421 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 422 `, 423 wantFile: "testdata/ordering/three-ordered-manifests.yaml", 424 wantErr: "", 425 }, 426 "orderManifests and extraDependencies in policyDefaults": { 427 tmpDir: tmpDir, 428 generator: ` 429 apiVersion: policy.open-cluster-management.io/v1 430 kind: PolicyGenerator 431 metadata: 432 name: test 433 policyDefaults: 434 consolidateManifests: false 435 orderManifests: true 436 extraDependencies: 437 - name: foo 438 namespace: my-policies 439 policies: 440 - name: one 441 manifests: 442 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 443 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 444 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 445 `, 446 wantFile: "", 447 wantErr: "policyDefaults may not specify both extraDependencies and orderManifests", 448 }, 449 "orderManifests in policyDefaults and extraDependencies in policy": { 450 tmpDir: tmpDir, 451 generator: ` 452 apiVersion: policy.open-cluster-management.io/v1 453 kind: PolicyGenerator 454 metadata: 455 name: test 456 policyDefaults: 457 consolidateManifests: false 458 orderManifests: true 459 namespace: my-policies 460 policies: 461 - name: one 462 extraDependencies: 463 - name: foo 464 manifests: 465 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 466 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 467 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 468 `, 469 wantFile: "", 470 wantErr: "extraDependencies may not be set in policy one when orderManifests is true", 471 }, 472 "orderManifests in policyDefaults and extraDependencies in manifest": { 473 tmpDir: tmpDir, 474 generator: ` 475 apiVersion: policy.open-cluster-management.io/v1 476 kind: PolicyGenerator 477 metadata: 478 name: test 479 policyDefaults: 480 consolidateManifests: false 481 orderManifests: true 482 namespace: my-policies 483 policies: 484 - name: one 485 manifests: 486 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 487 extraDependencies: 488 - name: foo 489 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 490 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 491 `, 492 wantFile: "", 493 wantErr: "extraDependencies may not be set in policy one manifest[0] because orderManifests is set", 494 }, 495 "orderManifests and consolidateManifests is false": { 496 tmpDir: tmpDir, 497 generator: ` 498 apiVersion: policy.open-cluster-management.io/v1 499 kind: PolicyGenerator 500 metadata: 501 name: test 502 policyDefaults: 503 orderManifests: true 504 namespace: my-policies 505 policies: 506 - name: one 507 manifests: 508 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 509 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 510 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 511 `, 512 wantFile: "", 513 wantErr: "policyDefaults may not specify both consolidateManifests and orderManifests", 514 }, 515 "orderManifests in policy and consolidateManifests is false": { 516 tmpDir: tmpDir, 517 generator: ` 518 apiVersion: policy.open-cluster-management.io/v1 519 kind: PolicyGenerator 520 metadata: 521 name: test 522 policyDefaults: 523 namespace: my-policies 524 policies: 525 - name: one 526 orderManifests: true 527 manifests: 528 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 529 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 530 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 531 `, 532 wantFile: "", 533 wantErr: "policy one may not set orderManifests when consolidateManifests is true", 534 }, 535 } 536 537 for name := range tests { 538 t.Run(name, tests[name].run) 539 } 540 } 541 542 func TestExtraDependencies(t *testing.T) { 543 t.Parallel() 544 tmpDir := t.TempDir() 545 createConfigMap(t, tmpDir, "configmap.yaml") 546 547 tests := map[string]genOutTest{ 548 "policyDefaults.extraDependencies are propagated to all manifests": { 549 tmpDir: tmpDir, 550 generator: ` 551 apiVersion: policy.open-cluster-management.io/v1 552 kind: PolicyGenerator 553 metadata: 554 name: test 555 policyDefaults: 556 consolidateManifests: false 557 namespace: my-policies 558 extraDependencies: 559 - name: extrafoo 560 policies: 561 - name: one 562 manifests: 563 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 564 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 565 - name: two 566 manifests: 567 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 568 `, 569 wantFile: "testdata/ordering/default-extradeps-propagated.yaml", 570 wantErr: "", 571 }, 572 "policyDefaults.extraDependencies is propagated with consolidated manifests": { 573 tmpDir: tmpDir, 574 generator: ` 575 apiVersion: policy.open-cluster-management.io/v1 576 kind: PolicyGenerator 577 metadata: 578 name: test 579 policyDefaults: 580 consolidateManifests: true 581 namespace: my-policies 582 extraDependencies: 583 - name: extrafoo 584 policies: 585 - name: one 586 manifests: 587 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 588 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 589 - name: two 590 manifests: 591 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 592 `, 593 wantFile: "testdata/ordering/default-extradeps-consolidated.yaml", 594 wantErr: "", 595 }, 596 "policy extraDependencies are propagated": { 597 tmpDir: tmpDir, 598 generator: ` 599 apiVersion: policy.open-cluster-management.io/v1 600 kind: PolicyGenerator 601 metadata: 602 name: test 603 policyDefaults: 604 consolidateManifests: false 605 namespace: my-policies 606 policies: 607 - name: one 608 extraDependencies: 609 - name: myextrafoo 610 manifests: 611 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 612 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 613 - name: two 614 manifests: 615 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 616 `, 617 wantFile: "testdata/ordering/policy-extradeps.yaml", 618 wantErr: "", 619 }, 620 "manifest extraDependencies are handled": { 621 tmpDir: tmpDir, 622 generator: ` 623 apiVersion: policy.open-cluster-management.io/v1 624 kind: PolicyGenerator 625 metadata: 626 name: test 627 policyDefaults: 628 consolidateManifests: false 629 namespace: my-policies 630 policies: 631 - name: one 632 manifests: 633 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 634 extraDependencies: 635 - name: manifestextra 636 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 637 - name: two 638 manifests: 639 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 640 `, 641 wantFile: "testdata/ordering/manifest-extradeps.yaml", 642 wantErr: "", 643 }, 644 "extraDependencies defaults can be overwritten": { 645 tmpDir: tmpDir, 646 generator: ` 647 apiVersion: policy.open-cluster-management.io/v1 648 kind: PolicyGenerator 649 metadata: 650 name: test 651 policyDefaults: 652 consolidateManifests: false 653 namespace: my-policies 654 extraDependencies: 655 - name: defaultextradep 656 policies: 657 - name: one 658 manifests: 659 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 660 extraDependencies: 661 - name: manifestextra 662 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 663 - name: two 664 extraDependencies: 665 - name: policyextra 666 manifests: 667 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 668 `, 669 wantFile: "testdata/ordering/default-extradeps-override.yaml", 670 wantErr: "", 671 }, 672 "extraDependencies default fields can be overwritten": { 673 tmpDir: tmpDir, 674 generator: ` 675 apiVersion: policy.open-cluster-management.io/v1 676 kind: PolicyGenerator 677 metadata: 678 name: test 679 policyDefaults: 680 consolidateManifests: false 681 namespace: my-policies 682 extraDependencies: 683 - apiVersion: fake.test.io/v2 684 compliance: Pending 685 kind: FakeThing 686 name: foo 687 namespace: bar 688 policies: 689 - name: one 690 manifests: 691 - path: {{printf "%v/%v" .Dir "configmap.yaml"}} 692 `, 693 wantFile: "testdata/ordering/extradeps-overrides.yaml", 694 wantErr: "", 695 }, 696 } 697 698 for name := range tests { 699 t.Run(name, tests[name].run) 700 } 701 }