mirror of
https://github.com/imjasonh/git-k8s
synced 2026-07-09 07:06:52 +00:00
Move all e2e test logic from bash steps in ci.yaml into Go test files under test/e2e/. CI now sets up infrastructure (KinD, Knative, CRDs, controllers, Gitea) then runs `go test -tags=e2e ./test/e2e/`. Benefits: - Tests run concurrently via t.Parallel() - No more bash-in-YAML maintenance burden - Each test creates its own isolated resources with cleanup - Proper Go test output with -v - Build-tagged so `go test ./...` skips them Tests: - TestPushTransaction: push main to a new branch, verify on git server - TestPushTransactionMultipleRefSpecs: push to two branches at once - TestRepoSync: create two repos, set up GitRepoSync, verify controller https://claude.ai/code/session_01QfFzqKQUsxxBsiZUhuJ3pG
77 lines
2.7 KiB
Go
77 lines
2.7 KiB
Go
//go:build e2e
|
|
|
|
package e2e
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
gitv1alpha1 "github.com/imjasonh/git-k8s/pkg/apis/git/v1alpha1"
|
|
)
|
|
|
|
func TestRepoSync(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// Create two repos on Gitea.
|
|
createGiteaRepo(t, "e2e-sync-a")
|
|
createGiteaRepo(t, "e2e-sync-b")
|
|
|
|
secretName := createCredentialsSecret(t, "e2e-sync-creds")
|
|
createGitRepository(t, "e2e-sync-repo-a", "e2e-sync-a", secretName)
|
|
createGitRepository(t, "e2e-sync-repo-b", "e2e-sync-b", secretName)
|
|
|
|
// Create GitBranch CRs and populate their headCommit via the push controller.
|
|
// First, push main to itself to get the push controller to set headCommit.
|
|
branchA := createGitBranch(t, "e2e-sync-branch-a", "e2e-sync-repo-a", "main")
|
|
branchB := createGitBranch(t, "e2e-sync-branch-b", "e2e-sync-repo-b", "main")
|
|
|
|
// We need headCommit set on both branches for the sync controller.
|
|
// Patch them with the actual commit from Gitea.
|
|
commitA := getGiteaBranchCommit(t, "e2e-sync-a", "main")
|
|
commitB := getGiteaBranchCommit(t, "e2e-sync-b", "main")
|
|
t.Logf("Repo A main commit: %s", commitA)
|
|
t.Logf("Repo B main commit: %s", commitB)
|
|
|
|
ctx := context.Background()
|
|
branchA.Status.HeadCommit = commitA
|
|
if _, err := gitClient.GitBranches(testNamespace).UpdateStatus(ctx, branchA, metav1.UpdateOptions{}); err != nil {
|
|
t.Fatalf("updating branch A status: %v", err)
|
|
}
|
|
branchB.Status.HeadCommit = commitB
|
|
if _, err := gitClient.GitBranches(testNamespace).UpdateStatus(ctx, branchB, metav1.UpdateOptions{}); err != nil {
|
|
t.Fatalf("updating branch B status: %v", err)
|
|
}
|
|
|
|
// Create a GitRepoSync between the two repos.
|
|
syncObj := &gitv1alpha1.GitRepoSync{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "e2e-sync",
|
|
Namespace: testNamespace,
|
|
},
|
|
Spec: gitv1alpha1.GitRepoSyncSpec{
|
|
RepoA: gitv1alpha1.SyncRepoRef{Name: "e2e-sync-repo-a"},
|
|
RepoB: gitv1alpha1.SyncRepoRef{Name: "e2e-sync-repo-b"},
|
|
BranchName: "main",
|
|
},
|
|
}
|
|
_, err := gitClient.GitRepoSyncs(testNamespace).Create(ctx, syncObj, metav1.CreateOptions{})
|
|
if err != nil {
|
|
t.Fatalf("creating GitRepoSync: %v", err)
|
|
}
|
|
t.Cleanup(func() {
|
|
gitClient.GitRepoSyncs(testNamespace).Delete(ctx, "e2e-sync", metav1.DeleteOptions{})
|
|
})
|
|
|
|
// Wait for the sync controller to set a phase.
|
|
result := waitForSyncPhase(t, "e2e-sync")
|
|
t.Logf("GitRepoSync phase: %s, message: %s", result.Status.Phase, result.Status.Message)
|
|
|
|
// The sync controller should have processed this.
|
|
// Since both repos are different auto_init repos, commits differ,
|
|
// so we expect Conflicted (no common ancestor) or another non-empty phase.
|
|
if result.Status.Phase == "" {
|
|
t.Fatal("sync controller did not set a phase")
|
|
}
|
|
}
|