Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 79 additions & 4 deletions pkg/pipelineascode/pipelineascode_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pipelineascode

import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
Expand Down Expand Up @@ -35,7 +36,9 @@ import (
zapobserver "go.uber.org/zap/zaptest/observer"
"gotest.tools/v3/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
rtesting "knative.dev/pkg/reconciler/testing"
)

Expand Down Expand Up @@ -128,6 +131,7 @@ func TestRun(t *testing.T) {
concurrencyLimit int
expectedLogSnippet string
expectedPostedComment string // TODO: multiple posted comments when we need it
secretCreationError error // Error to inject for secret creation
}{
{
name: "pull request/fail-to-start-apps",
Expand Down Expand Up @@ -540,6 +544,55 @@ func TestRun(t *testing.T) {
},
tektondir: "testdata/pending_pipelinerun",
},
{
name: "pull request/secret already exists - should emit warning and continue",
runevent: info.Event{
Event: &github.PullRequestEvent{
PullRequest: &github.PullRequest{
Number: github.Ptr(666),
},
},
SHA: "fromwebhook",
Organization: "owner",
Sender: "owner",
Repository: "repo",
URL: "https://service/documentation",
HeadBranch: "press",
BaseBranch: "main",
EventType: "pull_request",
TriggerTarget: "pull_request",
PullRequestNumber: 666,
InstallationID: 1234,
},
tektondir: "testdata/pull_request",
finalStatus: "neutral",
secretCreationError: errors.NewAlreadyExists(schema.GroupResource{Group: "", Resource: "secrets"}, "test-secret"),
},
{
name: "pull request/secret creation failure - should return error",
runevent: info.Event{
Event: &github.PullRequestEvent{
PullRequest: &github.PullRequest{
Number: github.Ptr(666),
},
},
SHA: "fromwebhook",
Organization: "owner",
Sender: "owner",
Repository: "repo",
URL: "https://service/documentation",
HeadBranch: "press",
BaseBranch: "main",
EventType: "pull_request",
TriggerTarget: "pull_request",
PullRequestNumber: 666,
InstallationID: 1234,
},
tektondir: "testdata/pull_request",
finalStatus: "failure",
finalStatusText: "creating basic auth secret",
secretCreationError: fmt.Errorf("connection timeout"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -656,10 +709,22 @@ func TestRun(t *testing.T) {
ctx = info.StoreCurrentControllerName(ctx, "default")
ctx = info.StoreNS(ctx, repo.InstallNamespace)

k8int := &kitesthelper.KinterfaceTest{
ConsoleURL: "https://console.url",
ExpectedNumberofCleanups: tt.expectedNumberofCleanups,
GetSecretResult: secrets,
var k8int kubeinteraction.Interface
if tt.secretCreationError != nil {
k8int = &KinterfaceTestWithError{
KinterfaceTest: kitesthelper.KinterfaceTest{
ConsoleURL: "https://console.url",
ExpectedNumberofCleanups: tt.expectedNumberofCleanups,
GetSecretResult: secrets,
},
CreateSecretError: tt.secretCreationError,
}
} else {
k8int = &kitesthelper.KinterfaceTest{
ConsoleURL: "https://console.url",
ExpectedNumberofCleanups: tt.expectedNumberofCleanups,
GetSecretResult: secrets,
}
}
Comment on lines +712 to 728
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve maintainability and reduce code duplication, you can refactor this block. You can initialize the base kitesthelper.KinterfaceTest once, and then conditionally wrap it with KinterfaceTestWithError if an error needs to be injected. This avoids repeating the initialization logic.

			ktest := &kitesthelper.KinterfaceTest{
				ConsoleURL:               "https://console.url",
				ExpectedNumberofCleanups: tt.expectedNumberofCleanups,
				GetSecretResult:          secrets,
			}
			var k8int kubeinteraction.Interface = ktest
			if tt.secretCreationError != nil {
				k8int = &KinterfaceTestWithError{
					KinterfaceTest:    *ktest,
					CreateSecretError: tt.secretCreationError,
				}
			}


// InstallationID > 0 is used to detect if we are a GitHub APP
Expand Down Expand Up @@ -788,3 +853,13 @@ func TestGetExecutionOrderPatch(t *testing.T) {
})
}
}

// KinterfaceTestWithError extends KinterfaceTest to allow injection of specific errors.
type KinterfaceTestWithError struct {
kitesthelper.KinterfaceTest
CreateSecretError error
}

func (k *KinterfaceTestWithError) CreateSecret(_ context.Context, _ string, _ *corev1.Secret) error {
return k.CreateSecretError
}
Loading
โšก