Skip to content

Commit b988b22

Browse files
committed
test: add unit tests for duplicate secret reuse scenario
Signed-off-by: ab-ghosh <abghosh@redhat.com>
1 parent a8e6b9c commit b988b22

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

pkg/pipelineascode/pipelineascode_test.go

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package pipelineascode
22

33
import (
4+
"context"
45
"crypto/hmac"
56
"crypto/sha256"
67
"encoding/hex"
@@ -35,7 +36,9 @@ import (
3536
zapobserver "go.uber.org/zap/zaptest/observer"
3637
"gotest.tools/v3/assert"
3738
corev1 "k8s.io/api/core/v1"
39+
"k8s.io/apimachinery/pkg/api/errors"
3840
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
41+
"k8s.io/apimachinery/pkg/runtime/schema"
3942
rtesting "knative.dev/pkg/reconciler/testing"
4043
)
4144

@@ -96,6 +99,28 @@ func testSetupCommonGhReplies(t *testing.T, mux *http.ServeMux, runevent info.Ev
9699
})
97100
}
98101

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+
99124
func TestRun(t *testing.T) {
100125
var hubCatalogs sync.Map
101126
hubCatalogs.Store(
@@ -128,6 +153,7 @@ func TestRun(t *testing.T) {
128153
concurrencyLimit int
129154
expectedLogSnippet string
130155
expectedPostedComment string // TODO: multiple posted comments when we need it
156+
secretCreationError error // Error to inject for secret creation
131157
}{
132158
{
133159
name: "pull request/fail-to-start-apps",
@@ -540,6 +566,21 @@ func TestRun(t *testing.T) {
540566
},
541567
tektondir: "testdata/pending_pipelinerun",
542568
},
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+
},
543584
}
544585
for _, tt := range tests {
545586
t.Run(tt.name, func(t *testing.T) {
@@ -656,12 +697,22 @@ func TestRun(t *testing.T) {
656697
ctx = info.StoreCurrentControllerName(ctx, "default")
657698
ctx = info.StoreNS(ctx, repo.InstallNamespace)
658699

659-
k8int := &kitesthelper.KinterfaceTest{
700+
kintTest := kitesthelper.KinterfaceTest{
660701
ConsoleURL: "https://console.url",
661702
ExpectedNumberofCleanups: tt.expectedNumberofCleanups,
662703
GetSecretResult: secrets,
663704
}
664705

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+
665716
// InstallationID > 0 is used to detect if we are a GitHub APP
666717
tt.runevent.InstallationID = 12345
667718
if tt.ProviderInfoFromRepo {
@@ -788,3 +839,13 @@ func TestGetExecutionOrderPatch(t *testing.T) {
788839
})
789840
}
790841
}
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

Comments
 (0)