|
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 |
|
@@ -96,6 +99,28 @@ func testSetupCommonGhReplies(t *testing.T, mux *http.ServeMux, runevent info.Ev |
96 | 99 | }) |
97 | 100 | } |
98 | 101 |
|
| 102 | +// getSecretTestRunEvent returns a common runevent for secret-related test cases. |
| 103 | +func getSecretTestRunEvent() info.Event { |
| 104 | + return info.Event{ |
| 105 | + Event: &github.PullRequestEvent{ |
| 106 | + PullRequest: &github.PullRequest{ |
| 107 | + Number: github.Ptr(666), |
| 108 | + }, |
| 109 | + }, |
| 110 | + SHA: "fromwebhook", |
| 111 | + Organization: "owner", |
| 112 | + Sender: "owner", |
| 113 | + Repository: "repo", |
| 114 | + URL: "https://service/documentation", |
| 115 | + HeadBranch: "press", |
| 116 | + BaseBranch: "main", |
| 117 | + EventType: "pull_request", |
| 118 | + TriggerTarget: "pull_request", |
| 119 | + PullRequestNumber: 666, |
| 120 | + InstallationID: 1234, |
| 121 | + } |
| 122 | +} |
| 123 | + |
99 | 124 | func TestRun(t *testing.T) { |
100 | 125 | var hubCatalogs sync.Map |
101 | 126 | hubCatalogs.Store( |
@@ -128,6 +153,7 @@ func TestRun(t *testing.T) { |
128 | 153 | concurrencyLimit int |
129 | 154 | expectedLogSnippet string |
130 | 155 | expectedPostedComment string // TODO: multiple posted comments when we need it |
| 156 | + secretCreationError error // Error to inject for secret creation |
131 | 157 | }{ |
132 | 158 | { |
133 | 159 | name: "pull request/fail-to-start-apps", |
@@ -540,6 +566,21 @@ func TestRun(t *testing.T) { |
540 | 566 | }, |
541 | 567 | tektondir: "testdata/pending_pipelinerun", |
542 | 568 | }, |
| 569 | + { |
| 570 | + name: "pull request/secret already exists - should emit warning and continue", |
| 571 | + runevent: getSecretTestRunEvent(), |
| 572 | + tektondir: "testdata/pull_request", |
| 573 | + finalStatus: "neutral", |
| 574 | + secretCreationError: errors.NewAlreadyExists(schema.GroupResource{Group: "", Resource: "secrets"}, "test-secret"), |
| 575 | + }, |
| 576 | + { |
| 577 | + name: "pull request/secret creation failure - should return error", |
| 578 | + runevent: getSecretTestRunEvent(), |
| 579 | + tektondir: "testdata/pull_request", |
| 580 | + finalStatus: "failure", |
| 581 | + finalStatusText: "creating basic auth secret", |
| 582 | + secretCreationError: fmt.Errorf("connection timeout"), |
| 583 | + }, |
543 | 584 | } |
544 | 585 | for _, tt := range tests { |
545 | 586 | t.Run(tt.name, func(t *testing.T) { |
@@ -656,12 +697,22 @@ func TestRun(t *testing.T) { |
656 | 697 | ctx = info.StoreCurrentControllerName(ctx, "default") |
657 | 698 | ctx = info.StoreNS(ctx, repo.InstallNamespace) |
658 | 699 |
|
659 | | - k8int := &kitesthelper.KinterfaceTest{ |
| 700 | + kintTest := kitesthelper.KinterfaceTest{ |
660 | 701 | ConsoleURL: "https://console.url", |
661 | 702 | ExpectedNumberofCleanups: tt.expectedNumberofCleanups, |
662 | 703 | GetSecretResult: secrets, |
663 | 704 | } |
664 | 705 |
|
| 706 | + var k8int kubeinteraction.Interface |
| 707 | + if tt.secretCreationError != nil { |
| 708 | + k8int = &KinterfaceTestWithError{ |
| 709 | + KinterfaceTest: kintTest, |
| 710 | + CreateSecretError: tt.secretCreationError, |
| 711 | + } |
| 712 | + } else { |
| 713 | + k8int = &kintTest |
| 714 | + } |
| 715 | + |
665 | 716 | // InstallationID > 0 is used to detect if we are a GitHub APP |
666 | 717 | tt.runevent.InstallationID = 12345 |
667 | 718 | if tt.ProviderInfoFromRepo { |
@@ -788,3 +839,13 @@ func TestGetExecutionOrderPatch(t *testing.T) { |
788 | 839 | }) |
789 | 840 | } |
790 | 841 | } |
| 842 | + |
| 843 | +// KinterfaceTestWithError extends KinterfaceTest to allow injection of specific errors. |
| 844 | +type KinterfaceTestWithError struct { |
| 845 | + kitesthelper.KinterfaceTest |
| 846 | + CreateSecretError error |
| 847 | +} |
| 848 | + |
| 849 | +func (k *KinterfaceTestWithError) CreateSecret(_ context.Context, _ string, _ *corev1.Secret) error { |
| 850 | + return k.CreateSecretError |
| 851 | +} |
0 commit comments