-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #373 from aryan9600/managed-transport-default
- Loading branch information
Showing
4 changed files
with
265 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,8 @@ import ( | |
|
||
"github.com/go-logr/logr" | ||
libgit2 "github.com/libgit2/git2go/v33" | ||
. "github.com/onsi/gomega" | ||
"k8s.io/apimachinery/pkg/types" | ||
|
||
"github.com/fluxcd/pkg/gittestserver" | ||
) | ||
|
@@ -127,22 +129,32 @@ func TestPushRejected(t *testing.T) { | |
t.Fatal(err) | ||
} | ||
|
||
// this is currently defined in update_test.go, but handy right here .. | ||
if err = initGitRepo(gitServer, "testdata/appconfig", "main", "/appconfig.git"); err != nil { | ||
// We use "test" as the branch to init repo, to avoid potential conflicts | ||
// with the default branch(main/master) of the system this test is running | ||
// on. If, for e.g., we used main as the branch and the default branch is | ||
// supposed to be main, this will fail as this would try to create a branch | ||
// named main explicitly. | ||
if err = initGitRepo(gitServer, "testdata/appconfig", "test", "/appconfig.git"); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
repoURL := gitServer.HTTPAddressWithCredentials() + "/appconfig.git" | ||
repo, err := clone(repoURL, "origin", "main") | ||
cloneCtx, cancel := context.WithTimeout(ctx, time.Second*10) | ||
defer cancel() | ||
repo, err := clone(cloneCtx, repoURL, "test") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer repo.Free() | ||
|
||
cleanup, err := configureTransportOptsForRepo(repo) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer cleanup() | ||
|
||
// This is here to guard against push in general being broken | ||
err = push(context.TODO(), repo.Workdir(), "main", repoAccess{ | ||
url: repoURL, | ||
auth: nil, | ||
}) | ||
err = push(context.TODO(), repo.Workdir(), "test", repoAccess{}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
@@ -154,11 +166,104 @@ func TestPushRejected(t *testing.T) { | |
|
||
// This is supposed to fail, because the hook rejects the branch | ||
// pushed to. | ||
err = push(context.TODO(), repo.Workdir(), branch, repoAccess{ | ||
url: repoURL, | ||
auth: nil, | ||
}) | ||
err = push(context.TODO(), repo.Workdir(), branch, repoAccess{}) | ||
if err == nil { | ||
t.Error("push to a forbidden branch is expected to fail, but succeeded") | ||
} | ||
} | ||
|
||
func Test_switchToBranch(t *testing.T) { | ||
g := NewWithT(t) | ||
gitServer, err := gittestserver.NewTempGitServer() | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
gitServer.AutoCreate() | ||
g.Expect(gitServer.StartHTTP()).To(Succeed()) | ||
|
||
// We use "test" as the branch to init repo, to avoid potential conflicts | ||
// with the default branch(main/master) of the system this test is running | ||
// on. If, for e.g., we used main as the branch and the default branch is | ||
// supposed to be main, this will fail as this would try to create a branch | ||
// named main explicitly. | ||
branch := "test" | ||
g.Expect(initGitRepo(gitServer, "testdata/appconfig", branch, "/appconfig.git")).To(Succeed()) | ||
|
||
repoURL := gitServer.HTTPAddressWithCredentials() + "/appconfig.git" | ||
cloneCtx, cancel := context.WithTimeout(ctx, time.Second*10) | ||
defer cancel() | ||
repo, err := clone(cloneCtx, repoURL, branch) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
defer repo.Free() | ||
|
||
head, err := repo.Head() | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
defer head.Free() | ||
target := head.Target() | ||
|
||
// register transport options and update remote to transport url | ||
cleanup, err := configureTransportOptsForRepo(repo) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer cleanup() | ||
|
||
// calling switchToBranch with a branch that doesn't exist on origin | ||
// should result in the branch being created and switched to. | ||
branch = "not-on-origin" | ||
switchToBranch(repo, context.TODO(), branch, repoAccess{}) | ||
|
||
head, err = repo.Head() | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
name, err := head.Branch().Name() | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
g.Expect(name).To(Equal(branch)) | ||
|
||
cc, err := repo.LookupCommit(head.Target()) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
defer cc.Free() | ||
g.Expect(cc.Id().String()).To(Equal(target.String())) | ||
|
||
// create a branch with the HEAD commit and push it to origin | ||
branch = "exists-on-origin" | ||
_, err = repo.CreateBranch(branch, cc, false) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
origin, err := repo.Remotes.Lookup(originRemote) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
defer origin.Free() | ||
|
||
g.Expect(origin.Push( | ||
[]string{fmt.Sprintf("refs/heads/%s:refs/heads/%s", branch, branch)}, &libgit2.PushOptions{}, | ||
)).To(Succeed()) | ||
|
||
// push a new commit to the branch. this is done to test whether we properly | ||
// sync our local branch with the remote branch, before switching. | ||
policyKey := types.NamespacedName{ | ||
Name: "policy", | ||
Namespace: "ns", | ||
} | ||
commitID := commitInRepo(g, repoURL, branch, "Install setter marker", func(tmp string) { | ||
g.Expect(replaceMarker(tmp, policyKey)).To(Succeed()) | ||
}) | ||
|
||
// calling switchToBranch with a branch that exists should make sure to fetch latest | ||
// for that branch from origin, and then switch to it. | ||
switchToBranch(repo, context.TODO(), branch, repoAccess{}) | ||
head, err = repo.Head() | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
name, err = head.Branch().Name() | ||
|
||
g.Expect(err).ToNot(HaveOccurred()) | ||
g.Expect(name).To(Equal(branch)) | ||
g.Expect(head.Target().String()).To(Equal(commitID.String())) | ||
|
||
// push a commit after switching to the branch, to check if the local | ||
// branch is synced with origin. | ||
replaceMarker(repo.Workdir(), policyKey) | ||
sig := &libgit2.Signature{ | ||
Name: "Testbot", | ||
Email: "[email protected]", | ||
When: time.Now(), | ||
} | ||
_, err = commitWorkDir(repo, branch, "update policy", sig) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
g.Expect(push(context.TODO(), repo.Workdir(), branch, repoAccess{})).To(Succeed()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.