|
1 | 1 | package pipelineascode |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "crypto/hmac" |
5 | 6 | "crypto/sha256" |
6 | 7 | "encoding/hex" |
@@ -35,7 +36,9 @@ import ( |
35 | 36 | zapobserver "go.uber.org/zap/zaptest/observer" |
36 | 37 | "gotest.tools/v3/assert" |
37 | 38 | corev1 "k8s.io/api/core/v1" |
| 39 | + "k8s.io/apimachinery/pkg/api/errors" |
38 | 40 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 41 | + "k8s.io/apimachinery/pkg/runtime/schema" |
39 | 42 | rtesting "knative.dev/pkg/reconciler/testing" |
40 | 43 | ) |
41 | 44 |
|
@@ -128,6 +131,7 @@ func TestRun(t *testing.T) { |
128 | 131 | concurrencyLimit int |
129 | 132 | expectedLogSnippet string |
130 | 133 | expectedPostedComment string // TODO: multiple posted comments when we need it |
| 134 | + secretCreationError error // Error to inject for secret creation |
131 | 135 | }{ |
132 | 136 | { |
133 | 137 | name: "pull request/fail-to-start-apps", |
@@ -540,6 +544,55 @@ func TestRun(t *testing.T) { |
540 | 544 | }, |
541 | 545 | tektondir: "testdata/pending_pipelinerun", |
542 | 546 | }, |
| 547 | + { |
| 548 | + name: "pull request/secret already exists - should emit warning and continue", |
| 549 | + runevent: info.Event{ |
| 550 | + Event: &github.PullRequestEvent{ |
| 551 | + PullRequest: &github.PullRequest{ |
| 552 | + Number: github.Ptr(666), |
| 553 | + }, |
| 554 | + }, |
| 555 | + SHA: "fromwebhook", |
| 556 | + Organization: "owner", |
| 557 | + Sender: "owner", |
| 558 | + Repository: "repo", |
| 559 | + URL: "https://service/documentation", |
| 560 | + HeadBranch: "press", |
| 561 | + BaseBranch: "main", |
| 562 | + EventType: "pull_request", |
| 563 | + TriggerTarget: "pull_request", |
| 564 | + PullRequestNumber: 666, |
| 565 | + InstallationID: 1234, |
| 566 | + }, |
| 567 | + tektondir: "testdata/pull_request", |
| 568 | + finalStatus: "neutral", |
| 569 | + secretCreationError: errors.NewAlreadyExists(schema.GroupResource{Group: "", Resource: "secrets"}, "test-secret"), |
| 570 | + }, |
| 571 | + { |
| 572 | + name: "pull request/secret creation failure - should return error", |
| 573 | + runevent: info.Event{ |
| 574 | + Event: &github.PullRequestEvent{ |
| 575 | + PullRequest: &github.PullRequest{ |
| 576 | + Number: github.Ptr(666), |
| 577 | + }, |
| 578 | + }, |
| 579 | + SHA: "fromwebhook", |
| 580 | + Organization: "owner", |
| 581 | + Sender: "owner", |
| 582 | + Repository: "repo", |
| 583 | + URL: "https://service/documentation", |
| 584 | + HeadBranch: "press", |
| 585 | + BaseBranch: "main", |
| 586 | + EventType: "pull_request", |
| 587 | + TriggerTarget: "pull_request", |
| 588 | + PullRequestNumber: 666, |
| 589 | + InstallationID: 1234, |
| 590 | + }, |
| 591 | + tektondir: "testdata/pull_request", |
| 592 | + finalStatus: "failure", |
| 593 | + finalStatusText: "creating basic auth secret", |
| 594 | + secretCreationError: fmt.Errorf("connection timeout"), |
| 595 | + }, |
543 | 596 | } |
544 | 597 | for _, tt := range tests { |
545 | 598 | t.Run(tt.name, func(t *testing.T) { |
@@ -656,10 +709,22 @@ func TestRun(t *testing.T) { |
656 | 709 | ctx = info.StoreCurrentControllerName(ctx, "default") |
657 | 710 | ctx = info.StoreNS(ctx, repo.InstallNamespace) |
658 | 711 |
|
659 | | - k8int := &kitesthelper.KinterfaceTest{ |
660 | | - ConsoleURL: "https://console.url", |
661 | | - ExpectedNumberofCleanups: tt.expectedNumberofCleanups, |
662 | | - GetSecretResult: secrets, |
| 712 | + var k8int kubeinteraction.Interface |
| 713 | + if tt.secretCreationError != nil { |
| 714 | + k8int = &KinterfaceTestWithError{ |
| 715 | + KinterfaceTest: kitesthelper.KinterfaceTest{ |
| 716 | + ConsoleURL: "https://console.url", |
| 717 | + ExpectedNumberofCleanups: tt.expectedNumberofCleanups, |
| 718 | + GetSecretResult: secrets, |
| 719 | + }, |
| 720 | + CreateSecretError: tt.secretCreationError, |
| 721 | + } |
| 722 | + } else { |
| 723 | + k8int = &kitesthelper.KinterfaceTest{ |
| 724 | + ConsoleURL: "https://console.url", |
| 725 | + ExpectedNumberofCleanups: tt.expectedNumberofCleanups, |
| 726 | + GetSecretResult: secrets, |
| 727 | + } |
663 | 728 | } |
664 | 729 |
|
665 | 730 | // InstallationID > 0 is used to detect if we are a GitHub APP |
@@ -788,3 +853,13 @@ func TestGetExecutionOrderPatch(t *testing.T) { |
788 | 853 | }) |
789 | 854 | } |
790 | 855 | } |
| 856 | + |
| 857 | +// KinterfaceTestWithError extends KinterfaceTest to allow injection of specific errors. |
| 858 | +type KinterfaceTestWithError struct { |
| 859 | + kitesthelper.KinterfaceTest |
| 860 | + CreateSecretError error |
| 861 | +} |
| 862 | + |
| 863 | +func (k *KinterfaceTestWithError) CreateSecret(_ context.Context, _ string, _ *corev1.Secret) error { |
| 864 | + return k.CreateSecretError |
| 865 | +} |
0 commit comments