libgit2: refactor tests to use managed and unmanaged transport cleanly
Refactors libgit2 checkout tests to test managed and unmanaged transport by making sure the tests requiring unmanaged transport are run before, any tests that require managed transport (since disabling managed transport isn't possible). This is done via arranging the tests carefully in alphabetically sorted names, i.e. the tests with unmanaged transport go in `checkout_test.go`, which forces golang to run the tests in that file before any other tests. Signed-off-by: Sanskar Jaiswal <jaiswalsanskar078@gmail.com>
This commit is contained in:
parent
1faa547dad
commit
4ce31319ee
|
|
@ -37,6 +37,7 @@ import (
|
||||||
ctrl "sigs.k8s.io/controller-runtime"
|
ctrl "sigs.k8s.io/controller-runtime"
|
||||||
|
|
||||||
"github.com/fluxcd/pkg/runtime/controller"
|
"github.com/fluxcd/pkg/runtime/controller"
|
||||||
|
feathelper "github.com/fluxcd/pkg/runtime/features"
|
||||||
"github.com/fluxcd/pkg/runtime/testenv"
|
"github.com/fluxcd/pkg/runtime/testenv"
|
||||||
"github.com/fluxcd/pkg/testserver"
|
"github.com/fluxcd/pkg/testserver"
|
||||||
"github.com/go-logr/logr"
|
"github.com/go-logr/logr"
|
||||||
|
|
@ -206,6 +207,8 @@ func TestMain(m *testing.M) {
|
||||||
panic(fmt.Sprintf("Failed to create a test registry server: %v", err))
|
panic(fmt.Sprintf("Failed to create a test registry server: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fg := feathelper.FeatureGates{}
|
||||||
|
fg.SupportedFeatures(features.FeatureGates())
|
||||||
managed.InitManagedTransport(logr.Discard())
|
managed.InitManagedTransport(logr.Discard())
|
||||||
|
|
||||||
if err := (&GitRepositoryReconciler{
|
if err := (&GitRepositoryReconciler{
|
||||||
|
|
|
||||||
|
|
@ -25,40 +25,49 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/fluxcd/source-controller/pkg/git"
|
"github.com/fluxcd/pkg/gittestserver"
|
||||||
git2go "github.com/libgit2/git2go/v33"
|
git2go "github.com/libgit2/git2go/v33"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
|
|
||||||
|
"github.com/fluxcd/source-controller/pkg/git"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCheckoutBranch_checkoutUnmanaged(t *testing.T) {
|
func TestCheckoutBranch_unmanaged(t *testing.T) {
|
||||||
repo, err := initBareRepo(t)
|
checkoutBranch(t, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkoutBranch is a test helper function which runs the tests for checking out
|
||||||
|
// via CheckoutBranch.
|
||||||
|
func checkoutBranch(t *testing.T, managed bool) {
|
||||||
|
// we use a HTTP Git server instead of a bare repo (for all tests in this
|
||||||
|
// package), because our managed transports don't support the file protocol,
|
||||||
|
// so we wouldn't actually be using our custom transports, if we used a bare
|
||||||
|
// repo.
|
||||||
|
server, err := gittestserver.NewTempGitServer()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(server.Root())
|
||||||
|
|
||||||
|
err = server.StartHTTP()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer server.StopHTTP()
|
||||||
|
|
||||||
|
repoPath := "test.git"
|
||||||
|
err = server.InitRepo("../testdata/git/repo", git.DefaultBranch, repoPath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
repo, err := git2go.OpenRepository(filepath.Join(server.Root(), repoPath))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer repo.Free()
|
defer repo.Free()
|
||||||
|
|
||||||
cfg, err := git2go.OpenDefault()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ignores the error here because it can be defaulted
|
|
||||||
// https://github.blog/2020-07-27-highlights-from-git-2-28/#introducing-init-defaultbranch
|
|
||||||
defaultBranch := "master"
|
defaultBranch := "master"
|
||||||
iter, err := cfg.NewIterator()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
for {
|
|
||||||
val, e := iter.Next()
|
|
||||||
if e != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if val.Name == "init.defaultbranch" {
|
|
||||||
defaultBranch = val.Value
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
firstCommit, err := commitFile(repo, "branch", "init", time.Now())
|
firstCommit, err := commitFile(repo, "branch", "init", time.Now())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -75,31 +84,52 @@ func TestCheckoutBranch_checkoutUnmanaged(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
repoURL := server.HTTPAddress() + "/" + repoPath
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
branch string
|
branch string
|
||||||
filesCreated map[string]string
|
filesCreated map[string]string
|
||||||
lastRevision string
|
lastRevision string
|
||||||
expectedCommit string
|
expectedCommit string
|
||||||
expectedErr string
|
expectedConcreteCommit bool
|
||||||
|
expectedErr string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Default branch",
|
name: "Default branch",
|
||||||
branch: defaultBranch,
|
branch: defaultBranch,
|
||||||
filesCreated: map[string]string{"branch": "second"},
|
filesCreated: map[string]string{"branch": "second"},
|
||||||
expectedCommit: secondCommit.String(),
|
expectedCommit: secondCommit.String(),
|
||||||
|
expectedConcreteCommit: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Other branch",
|
name: "Other branch",
|
||||||
branch: "test",
|
branch: "test",
|
||||||
filesCreated: map[string]string{"branch": "init"},
|
filesCreated: map[string]string{"branch": "init"},
|
||||||
expectedCommit: firstCommit.String(),
|
expectedCommit: firstCommit.String(),
|
||||||
|
expectedConcreteCommit: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Non existing branch",
|
name: "Non existing branch",
|
||||||
branch: "invalid",
|
branch: "invalid",
|
||||||
expectedErr: "reference 'refs/remotes/origin/invalid' not found",
|
expectedErr: "reference 'refs/remotes/origin/invalid' not found",
|
||||||
|
expectedConcreteCommit: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "skip clone - lastRevision hasn't changed",
|
||||||
|
branch: defaultBranch,
|
||||||
|
filesCreated: map[string]string{"branch": "second"},
|
||||||
|
lastRevision: fmt.Sprintf("%s/%s", defaultBranch, secondCommit.String()),
|
||||||
|
expectedCommit: secondCommit.String(),
|
||||||
|
expectedConcreteCommit: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "lastRevision is different",
|
||||||
|
branch: defaultBranch,
|
||||||
|
filesCreated: map[string]string{"branch": "second"},
|
||||||
|
lastRevision: fmt.Sprintf("%s/%s", defaultBranch, firstCommit.String()),
|
||||||
|
expectedCommit: secondCommit.String(),
|
||||||
|
expectedConcreteCommit: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -111,9 +141,13 @@ func TestCheckoutBranch_checkoutUnmanaged(t *testing.T) {
|
||||||
Branch: tt.branch,
|
Branch: tt.branch,
|
||||||
LastRevision: tt.lastRevision,
|
LastRevision: tt.lastRevision,
|
||||||
}
|
}
|
||||||
tmpDir := t.TempDir()
|
|
||||||
|
|
||||||
cc, err := branch.Checkout(context.TODO(), tmpDir, repo.Path(), nil)
|
tmpDir := t.TempDir()
|
||||||
|
authOpts := git.AuthOptions{
|
||||||
|
TransportOptionsURL: getTransportOptionsURL(git.HTTP),
|
||||||
|
}
|
||||||
|
|
||||||
|
cc, err := branch.Checkout(context.TODO(), tmpDir, repoURL, &authOpts)
|
||||||
if tt.expectedErr != "" {
|
if tt.expectedErr != "" {
|
||||||
g.Expect(err).To(HaveOccurred())
|
g.Expect(err).To(HaveOccurred())
|
||||||
g.Expect(err.Error()).To(ContainSubstring(tt.expectedErr))
|
g.Expect(err.Error()).To(ContainSubstring(tt.expectedErr))
|
||||||
|
|
@ -122,31 +156,51 @@ func TestCheckoutBranch_checkoutUnmanaged(t *testing.T) {
|
||||||
}
|
}
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
g.Expect(err).ToNot(HaveOccurred())
|
||||||
g.Expect(cc.String()).To(Equal(tt.branch + "/" + tt.expectedCommit))
|
g.Expect(cc.String()).To(Equal(tt.branch + "/" + tt.expectedCommit))
|
||||||
|
if managed {
|
||||||
|
g.Expect(git.IsConcreteCommit(*cc)).To(Equal(tt.expectedConcreteCommit))
|
||||||
|
}
|
||||||
|
|
||||||
|
if tt.expectedConcreteCommit {
|
||||||
|
for k, v := range tt.filesCreated {
|
||||||
|
g.Expect(filepath.Join(tmpDir, k)).To(BeARegularFile())
|
||||||
|
g.Expect(os.ReadFile(filepath.Join(tmpDir, k))).To(BeEquivalentTo(v))
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCheckoutTag_checkoutUnmanaged(t *testing.T) {
|
func TestCheckoutTag_unmanaged(t *testing.T) {
|
||||||
|
checkoutTag(t, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkoutTag is a test helper function which runs the tests for checking out
|
||||||
|
// via CheckoutTag.
|
||||||
|
func checkoutTag(t *testing.T, managed bool) {
|
||||||
type testTag struct {
|
type testTag struct {
|
||||||
name string
|
name string
|
||||||
annotated bool
|
annotated bool
|
||||||
}
|
}
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
tagsInRepo []testTag
|
tagsInRepo []testTag
|
||||||
checkoutTag string
|
checkoutTag string
|
||||||
expectErr string
|
lastRevTag string
|
||||||
|
expectErr string
|
||||||
|
expectConcreteCommit bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Tag",
|
name: "Tag",
|
||||||
tagsInRepo: []testTag{{"tag-1", false}},
|
tagsInRepo: []testTag{{"tag-1", false}},
|
||||||
checkoutTag: "tag-1",
|
checkoutTag: "tag-1",
|
||||||
|
expectConcreteCommit: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Annotated",
|
name: "Annotated",
|
||||||
tagsInRepo: []testTag{{"annotated", true}},
|
tagsInRepo: []testTag{{"annotated", true}},
|
||||||
checkoutTag: "annotated",
|
checkoutTag: "annotated",
|
||||||
|
expectConcreteCommit: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Non existing tag",
|
name: "Non existing tag",
|
||||||
|
|
@ -154,29 +208,46 @@ func TestCheckoutTag_checkoutUnmanaged(t *testing.T) {
|
||||||
expectErr: "unable to find 'invalid': no reference found for shorthand 'invalid'",
|
expectErr: "unable to find 'invalid': no reference found for shorthand 'invalid'",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Skip clone - last revision unchanged",
|
name: "Skip clone - last revision unchanged",
|
||||||
tagsInRepo: []testTag{{"tag-1", false}},
|
tagsInRepo: []testTag{{"tag-1", false}},
|
||||||
checkoutTag: "tag-1",
|
checkoutTag: "tag-1",
|
||||||
|
lastRevTag: "tag-1",
|
||||||
|
expectConcreteCommit: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Last revision changed",
|
name: "Last revision changed",
|
||||||
tagsInRepo: []testTag{{"tag-1", false}, {"tag-2", false}},
|
tagsInRepo: []testTag{{"tag-1", false}, {"tag-2", false}},
|
||||||
checkoutTag: "tag-2",
|
checkoutTag: "tag-2",
|
||||||
|
lastRevTag: "tag-1",
|
||||||
|
expectConcreteCommit: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
g := NewWithT(t)
|
g := NewWithT(t)
|
||||||
|
|
||||||
repo, err := initBareRepo(t)
|
server, err := gittestserver.NewTempGitServer()
|
||||||
if err != nil {
|
g.Expect(err).ToNot(HaveOccurred())
|
||||||
t.Fatal(err)
|
defer os.RemoveAll(server.Root())
|
||||||
}
|
|
||||||
|
err = server.StartHTTP()
|
||||||
|
g.Expect(err).ToNot(HaveOccurred())
|
||||||
|
defer server.StopHTTP()
|
||||||
|
|
||||||
|
repoPath := "test.git"
|
||||||
|
err = server.InitRepo("../testdata/git/repo", git.DefaultBranch, repoPath)
|
||||||
|
g.Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
|
repo, err := git2go.OpenRepository(filepath.Join(server.Root(), repoPath))
|
||||||
|
g.Expect(err).ToNot(HaveOccurred())
|
||||||
defer repo.Free()
|
defer repo.Free()
|
||||||
|
|
||||||
// Collect tags and their associated commit for later reference.
|
// Collect tags and their associated commit for later reference.
|
||||||
tagCommits := map[string]*git2go.Commit{}
|
tagCommits := map[string]*git2go.Commit{}
|
||||||
|
|
||||||
|
repoURL := server.HTTPAddress() + "/" + repoPath
|
||||||
|
|
||||||
// Populate the repo with commits and tags.
|
// Populate the repo with commits and tags.
|
||||||
if tt.tagsInRepo != nil {
|
if tt.tagsInRepo != nil {
|
||||||
for _, tr := range tt.tagsInRepo {
|
for _, tr := range tt.tagsInRepo {
|
||||||
|
|
@ -199,9 +270,18 @@ func TestCheckoutTag_checkoutUnmanaged(t *testing.T) {
|
||||||
checkoutTag := CheckoutTag{
|
checkoutTag := CheckoutTag{
|
||||||
Tag: tt.checkoutTag,
|
Tag: tt.checkoutTag,
|
||||||
}
|
}
|
||||||
|
// If last revision is provided, configure it.
|
||||||
|
if tt.lastRevTag != "" {
|
||||||
|
lc := tagCommits[tt.lastRevTag]
|
||||||
|
checkoutTag.LastRevision = fmt.Sprintf("%s/%s", tt.lastRevTag, lc.Id().String())
|
||||||
|
}
|
||||||
|
|
||||||
tmpDir := t.TempDir()
|
tmpDir := t.TempDir()
|
||||||
|
|
||||||
cc, err := checkoutTag.Checkout(context.TODO(), tmpDir, repo.Path(), nil)
|
authOpts := git.AuthOptions{
|
||||||
|
TransportOptionsURL: getTransportOptionsURL(git.HTTP),
|
||||||
|
}
|
||||||
|
cc, err := checkoutTag.Checkout(context.TODO(), tmpDir, repoURL, &authOpts)
|
||||||
if tt.expectErr != "" {
|
if tt.expectErr != "" {
|
||||||
g.Expect(err).To(HaveOccurred())
|
g.Expect(err).To(HaveOccurred())
|
||||||
g.Expect(err.Error()).To(ContainSubstring(tt.expectErr))
|
g.Expect(err.Error()).To(ContainSubstring(tt.expectErr))
|
||||||
|
|
@ -213,17 +293,48 @@ func TestCheckoutTag_checkoutUnmanaged(t *testing.T) {
|
||||||
targetTagCommit := tagCommits[tt.checkoutTag]
|
targetTagCommit := tagCommits[tt.checkoutTag]
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
g.Expect(err).ToNot(HaveOccurred())
|
||||||
g.Expect(cc.String()).To(Equal(tt.checkoutTag + "/" + targetTagCommit.Id().String()))
|
g.Expect(cc.String()).To(Equal(tt.checkoutTag + "/" + targetTagCommit.Id().String()))
|
||||||
|
if managed {
|
||||||
|
g.Expect(git.IsConcreteCommit(*cc)).To(Equal(tt.expectConcreteCommit))
|
||||||
|
|
||||||
g.Expect(filepath.Join(tmpDir, "tag")).To(BeARegularFile())
|
}
|
||||||
g.Expect(os.ReadFile(filepath.Join(tmpDir, "tag"))).To(BeEquivalentTo(tt.checkoutTag))
|
|
||||||
|
// Check file content only when there's an actual checkout.
|
||||||
|
if tt.lastRevTag != tt.checkoutTag {
|
||||||
|
g.Expect(filepath.Join(tmpDir, "tag")).To(BeARegularFile())
|
||||||
|
g.Expect(os.ReadFile(filepath.Join(tmpDir, "tag"))).To(BeEquivalentTo(tt.checkoutTag))
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCheckoutCommit_Checkout(t *testing.T) {
|
func TestCheckoutCommit_unmanaged(t *testing.T) {
|
||||||
|
checkoutCommit(t, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkoutCommit is a test helper function which runs the tests for checking out
|
||||||
|
// via CheckoutCommit.
|
||||||
|
func checkoutCommit(t *testing.T, managed bool) {
|
||||||
g := NewWithT(t)
|
g := NewWithT(t)
|
||||||
|
|
||||||
repo, err := initBareRepo(t)
|
server, err := gittestserver.NewTempGitServer()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(server.Root())
|
||||||
|
|
||||||
|
err = server.StartHTTP()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer server.StopHTTP()
|
||||||
|
|
||||||
|
repoPath := "test.git"
|
||||||
|
err = server.InitRepo("../testdata/git/repo", git.DefaultBranch, repoPath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
repo, err := git2go.OpenRepository(filepath.Join(server.Root(), repoPath))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
@ -236,13 +347,17 @@ func TestCheckoutCommit_Checkout(t *testing.T) {
|
||||||
if _, err = commitFile(repo, "commit", "second", time.Now()); err != nil {
|
if _, err = commitFile(repo, "commit", "second", time.Now()); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
tmpDir := t.TempDir()
|
||||||
|
authOpts := git.AuthOptions{
|
||||||
|
TransportOptionsURL: getTransportOptionsURL(git.HTTP),
|
||||||
|
}
|
||||||
|
repoURL := server.HTTPAddress() + "/" + repoPath
|
||||||
|
|
||||||
commit := CheckoutCommit{
|
commit := CheckoutCommit{
|
||||||
Commit: c.String(),
|
Commit: c.String(),
|
||||||
}
|
}
|
||||||
tmpDir := t.TempDir()
|
|
||||||
|
|
||||||
cc, err := commit.Checkout(context.TODO(), tmpDir, repo.Path(), nil)
|
cc, err := commit.Checkout(context.TODO(), tmpDir, repoURL, &authOpts)
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
g.Expect(err).ToNot(HaveOccurred())
|
||||||
g.Expect(cc).ToNot(BeNil())
|
g.Expect(cc).ToNot(BeNil())
|
||||||
g.Expect(cc.String()).To(Equal("HEAD/" + c.String()))
|
g.Expect(cc.String()).To(Equal("HEAD/" + c.String()))
|
||||||
|
|
@ -254,13 +369,19 @@ func TestCheckoutCommit_Checkout(t *testing.T) {
|
||||||
}
|
}
|
||||||
tmpDir2 := t.TempDir()
|
tmpDir2 := t.TempDir()
|
||||||
|
|
||||||
cc, err = commit.Checkout(context.TODO(), tmpDir2, repo.Path(), nil)
|
cc, err = commit.Checkout(context.TODO(), tmpDir2, repoURL, &authOpts)
|
||||||
g.Expect(err).To(HaveOccurred())
|
g.Expect(err).To(HaveOccurred())
|
||||||
g.Expect(err.Error()).To(HavePrefix("git checkout error: git commit '4dc3185c5fc94eb75048376edeb44571cece25f4' not found:"))
|
g.Expect(err.Error()).To(HavePrefix("git checkout error: git commit '4dc3185c5fc94eb75048376edeb44571cece25f4' not found:"))
|
||||||
g.Expect(cc).To(BeNil())
|
g.Expect(cc).To(BeNil())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCheckoutTagSemVer_Checkout(t *testing.T) {
|
func TestCheckoutTagSemVer_unmanaged(t *testing.T) {
|
||||||
|
checkoutSemVer(t, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkoutSemVer is a test helper function which runs the tests for checking out
|
||||||
|
// via CheckoutSemVer.
|
||||||
|
func checkoutSemVer(t *testing.T, managed bool) {
|
||||||
g := NewWithT(t)
|
g := NewWithT(t)
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
|
||||||
|
|
@ -322,11 +443,30 @@ func TestCheckoutTagSemVer_Checkout(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
repo, err := initBareRepo(t)
|
server, err := gittestserver.NewTempGitServer()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(server.Root())
|
||||||
|
|
||||||
|
err = server.StartHTTP()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer server.StopHTTP()
|
||||||
|
|
||||||
|
repoPath := "test.git"
|
||||||
|
err = server.InitRepo("../testdata/git/repo", git.DefaultBranch, repoPath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
repo, err := git2go.OpenRepository(filepath.Join(server.Root(), repoPath))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer repo.Free()
|
defer repo.Free()
|
||||||
|
repoURL := server.HTTPAddress() + "/" + repoPath
|
||||||
|
|
||||||
refs := make(map[string]string, len(tags))
|
refs := make(map[string]string, len(tags))
|
||||||
for _, tt := range tags {
|
for _, tt := range tags {
|
||||||
|
|
@ -357,9 +497,13 @@ func TestCheckoutTagSemVer_Checkout(t *testing.T) {
|
||||||
semVer := CheckoutSemVer{
|
semVer := CheckoutSemVer{
|
||||||
SemVer: tt.constraint,
|
SemVer: tt.constraint,
|
||||||
}
|
}
|
||||||
tmpDir := t.TempDir()
|
|
||||||
|
|
||||||
cc, err := semVer.Checkout(context.TODO(), tmpDir, repo.Path(), nil)
|
tmpDir := t.TempDir()
|
||||||
|
authOpts := git.AuthOptions{
|
||||||
|
TransportOptionsURL: getTransportOptionsURL(git.HTTP),
|
||||||
|
}
|
||||||
|
|
||||||
|
cc, err := semVer.Checkout(context.TODO(), tmpDir, repoURL, &authOpts)
|
||||||
if tt.expectErr != nil {
|
if tt.expectErr != nil {
|
||||||
g.Expect(err).To(Equal(tt.expectErr))
|
g.Expect(err).To(Equal(tt.expectErr))
|
||||||
g.Expect(cc).To(BeNil())
|
g.Expect(cc).To(BeNil())
|
||||||
|
|
@ -376,7 +520,7 @@ func TestCheckoutTagSemVer_Checkout(t *testing.T) {
|
||||||
|
|
||||||
func initBareRepo(t *testing.T) (*git2go.Repository, error) {
|
func initBareRepo(t *testing.T) (*git2go.Repository, error) {
|
||||||
tmpDir := t.TempDir()
|
tmpDir := t.TempDir()
|
||||||
repo, err := git2go.InitRepository(tmpDir, false)
|
repo, err := git2go.InitRepository(tmpDir, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,46 @@
|
||||||
|
/*
|
||||||
|
Copyright 2022 The Flux authors
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// This file is named `managed_checkout_test.go` on purpose to make sure that
|
||||||
|
// tests needing to use unmanaged transports run before the tests that use managed
|
||||||
|
// transports do, since the the former are present in `checkout_test.go`. `checkout_test.go`
|
||||||
|
// comes first in this package (alphabetically speaking), which makes golang run the tests
|
||||||
|
// in that file first.
|
||||||
|
package libgit2
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCheckoutBranch_CheckoutManaged(t *testing.T) {
|
||||||
|
enableManagedTransport()
|
||||||
|
checkoutBranch(t, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCheckoutTag_CheckoutManaged(t *testing.T) {
|
||||||
|
enableManagedTransport()
|
||||||
|
checkoutTag(t, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCheckoutCommit_CheckoutManaged(t *testing.T) {
|
||||||
|
enableManagedTransport()
|
||||||
|
checkoutCommit(t, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCheckoutTagSemVer_CheckoutManaged(t *testing.T) {
|
||||||
|
enableManagedTransport()
|
||||||
|
checkoutSemVer(t, true)
|
||||||
|
}
|
||||||
|
|
@ -30,22 +30,23 @@ import (
|
||||||
"github.com/fluxcd/gitkit"
|
"github.com/fluxcd/gitkit"
|
||||||
"github.com/fluxcd/pkg/gittestserver"
|
"github.com/fluxcd/pkg/gittestserver"
|
||||||
"github.com/fluxcd/pkg/ssh"
|
"github.com/fluxcd/pkg/ssh"
|
||||||
|
"github.com/go-logr/logr"
|
||||||
|
|
||||||
|
feathelper "github.com/fluxcd/pkg/runtime/features"
|
||||||
|
. "github.com/onsi/gomega"
|
||||||
|
cryptossh "golang.org/x/crypto/ssh"
|
||||||
|
|
||||||
|
"github.com/fluxcd/source-controller/internal/features"
|
||||||
"github.com/fluxcd/source-controller/pkg/git"
|
"github.com/fluxcd/source-controller/pkg/git"
|
||||||
"github.com/fluxcd/source-controller/pkg/git/libgit2/managed"
|
"github.com/fluxcd/source-controller/pkg/git/libgit2/managed"
|
||||||
|
|
||||||
"github.com/go-logr/logr"
|
|
||||||
. "github.com/onsi/gomega"
|
|
||||||
|
|
||||||
git2go "github.com/libgit2/git2go/v33"
|
|
||||||
cryptossh "golang.org/x/crypto/ssh"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const testRepositoryPath = "../testdata/git/repo"
|
const testRepositoryPath = "../testdata/git/repo"
|
||||||
|
|
||||||
// Test_ManagedSSH_KeyTypes assures support for the different
|
// Test_managedSSH_KeyTypes assures support for the different
|
||||||
// types of keys for SSH Authentication supported by Flux.
|
// types of keys for SSH Authentication supported by Flux.
|
||||||
func Test_ManagedSSH_KeyTypes(t *testing.T) {
|
func Test_managedSSH_KeyTypes(t *testing.T) {
|
||||||
managed.InitManagedTransport(logr.Discard())
|
enableManagedTransport()
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
|
@ -171,10 +172,10 @@ func Test_ManagedSSH_KeyTypes(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test_ManagedSSH_KeyExchangeAlgos assures support for the different
|
// Test_managedSSH_KeyExchangeAlgos assures support for the different
|
||||||
// types of SSH key exchange algorithms supported by Flux.
|
// types of SSH key exchange algorithms supported by Flux.
|
||||||
func Test_ManagedSSH_KeyExchangeAlgos(t *testing.T) {
|
func Test_managedSSH_KeyExchangeAlgos(t *testing.T) {
|
||||||
managed.InitManagedTransport(logr.Discard())
|
enableManagedTransport()
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
|
@ -294,10 +295,10 @@ func Test_ManagedSSH_KeyExchangeAlgos(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test_ManagedSSH_HostKeyAlgos assures support for the different
|
// Test_managedSSH_HostKeyAlgos assures support for the different
|
||||||
// types of SSH Host Key algorithms supported by Flux.
|
// types of SSH Host Key algorithms supported by Flux.
|
||||||
func Test_ManagedSSH_HostKeyAlgos(t *testing.T) {
|
func Test_managedSSH_HostKeyAlgos(t *testing.T) {
|
||||||
managed.InitManagedTransport(logr.Discard())
|
enableManagedTransport()
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
|
@ -458,185 +459,6 @@ func Test_ManagedSSH_HostKeyAlgos(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_ManagedHTTPCheckout(t *testing.T) {
|
|
||||||
managed.InitManagedTransport(logr.Discard())
|
|
||||||
g := NewWithT(t)
|
|
||||||
|
|
||||||
timeout := 5 * time.Second
|
|
||||||
server, err := gittestserver.NewTempGitServer()
|
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
|
||||||
defer os.RemoveAll(server.Root())
|
|
||||||
|
|
||||||
user := "test-user"
|
|
||||||
pwd := "test-pswd"
|
|
||||||
server.Auth(user, pwd)
|
|
||||||
|
|
||||||
err = server.StartHTTP()
|
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
|
||||||
defer server.StopHTTP()
|
|
||||||
|
|
||||||
repoPath := "test.git"
|
|
||||||
err = server.InitRepo("../testdata/git/repo", git.DefaultBranch, repoPath)
|
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
|
||||||
|
|
||||||
authOpts := &git.AuthOptions{
|
|
||||||
Username: "test-user",
|
|
||||||
Password: "test-pswd",
|
|
||||||
}
|
|
||||||
authOpts.TransportOptionsURL = getTransportOptionsURL(git.HTTP)
|
|
||||||
|
|
||||||
// Prepare for checkout.
|
|
||||||
branchCheckoutStrat := &CheckoutBranch{Branch: git.DefaultBranch}
|
|
||||||
tmpDir := t.TempDir()
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.TODO(), timeout)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
repoURL := server.HTTPAddress() + "/" + repoPath
|
|
||||||
// Checkout the repo.
|
|
||||||
_, err = branchCheckoutStrat.Checkout(ctx, tmpDir, repoURL, authOpts)
|
|
||||||
g.Expect(err).Error().ShouldNot(HaveOccurred())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestManagedCheckoutBranch_Checkout(t *testing.T) {
|
|
||||||
managed.InitManagedTransport(logr.Discard())
|
|
||||||
g := NewWithT(t)
|
|
||||||
|
|
||||||
timeout := 5 * time.Second
|
|
||||||
server, err := gittestserver.NewTempGitServer()
|
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
|
||||||
defer os.RemoveAll(server.Root())
|
|
||||||
|
|
||||||
err = server.StartHTTP()
|
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
|
||||||
defer server.StopHTTP()
|
|
||||||
|
|
||||||
repoPath := "test.git"
|
|
||||||
err = server.InitRepo("../testdata/git/repo", git.DefaultBranch, repoPath)
|
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
|
||||||
|
|
||||||
repo, err := git2go.OpenRepository(filepath.Join(server.Root(), repoPath))
|
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
|
||||||
defer repo.Free()
|
|
||||||
|
|
||||||
branchRef, err := repo.References.Lookup(fmt.Sprintf("refs/heads/%s", git.DefaultBranch))
|
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
|
||||||
defer branchRef.Free()
|
|
||||||
|
|
||||||
commit, err := repo.LookupCommit(branchRef.Target())
|
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
|
||||||
defer commit.Free()
|
|
||||||
|
|
||||||
authOpts := &git.AuthOptions{
|
|
||||||
TransportOptionsURL: getTransportOptionsURL(git.HTTP),
|
|
||||||
}
|
|
||||||
|
|
||||||
tmpDir := t.TempDir()
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.TODO(), timeout)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
repoURL := server.HTTPAddress() + "/" + repoPath
|
|
||||||
branch := CheckoutBranch{
|
|
||||||
Branch: git.DefaultBranch,
|
|
||||||
// Set last revision to HEAD commit, to force a no-op clone.
|
|
||||||
LastRevision: fmt.Sprintf("%s/%s", git.DefaultBranch, commit.Id().String()),
|
|
||||||
}
|
|
||||||
|
|
||||||
cc, err := branch.Checkout(ctx, tmpDir, repoURL, authOpts)
|
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
|
||||||
g.Expect(cc.String()).To(Equal(git.DefaultBranch + "/" + commit.Id().String()))
|
|
||||||
g.Expect(git.IsConcreteCommit(*cc)).To(Equal(false))
|
|
||||||
|
|
||||||
// Set last revision to a fake commit to force a full clone.
|
|
||||||
branch.LastRevision = fmt.Sprintf("%s/non-existent-commit", git.DefaultBranch)
|
|
||||||
cc, err = branch.Checkout(ctx, tmpDir, repoURL, authOpts)
|
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
|
||||||
g.Expect(cc.String()).To(Equal(git.DefaultBranch + "/" + commit.Id().String()))
|
|
||||||
g.Expect(git.IsConcreteCommit(*cc)).To(Equal(true))
|
|
||||||
|
|
||||||
// Create a new branch and push it.
|
|
||||||
err = createBranch(repo, "test", nil)
|
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
|
||||||
transportOptsURL := getTransportOptionsURL(git.HTTP)
|
|
||||||
managed.AddTransportOptions(transportOptsURL, managed.TransportOptions{
|
|
||||||
TargetURL: repoURL,
|
|
||||||
})
|
|
||||||
defer managed.RemoveTransportOptions(transportOptsURL)
|
|
||||||
origin, err := repo.Remotes.Create("origin", transportOptsURL)
|
|
||||||
defer origin.Free()
|
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
|
||||||
err = origin.Push([]string{"refs/heads/test:refs/heads/test"}, &git2go.PushOptions{})
|
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
|
||||||
|
|
||||||
branch.Branch = "test"
|
|
||||||
tmpDir2 := t.TempDir()
|
|
||||||
cc, err = branch.Checkout(ctx, tmpDir2, repoURL, authOpts)
|
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
|
||||||
|
|
||||||
// Check if the repo HEAD points to the branch.
|
|
||||||
repo, err = git2go.OpenRepository(tmpDir2)
|
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
|
||||||
head, err := repo.Head()
|
|
||||||
defer head.Free()
|
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
|
||||||
g.Expect(head.Branch().Name()).To(Equal("test"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestManagedCheckoutTag_Checkout(t *testing.T) {
|
|
||||||
managed.InitManagedTransport(logr.Discard())
|
|
||||||
g := NewWithT(t)
|
|
||||||
|
|
||||||
timeout := 5 * time.Second
|
|
||||||
server, err := gittestserver.NewTempGitServer()
|
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
|
||||||
defer os.RemoveAll(server.Root())
|
|
||||||
|
|
||||||
err = server.StartHTTP()
|
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
|
||||||
defer server.StopHTTP()
|
|
||||||
|
|
||||||
repoPath := "test.git"
|
|
||||||
err = server.InitRepo("../testdata/git/repo", git.DefaultBranch, repoPath)
|
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
|
||||||
|
|
||||||
repo, err := git2go.OpenRepository(filepath.Join(server.Root(), repoPath))
|
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
|
||||||
defer repo.Free()
|
|
||||||
|
|
||||||
branchRef, err := repo.References.Lookup(fmt.Sprintf("refs/heads/%s", git.DefaultBranch))
|
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
|
||||||
defer branchRef.Free()
|
|
||||||
|
|
||||||
commit, err := repo.LookupCommit(branchRef.Target())
|
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
|
||||||
defer commit.Free()
|
|
||||||
_, err = tag(repo, commit.Id(), false, "tag-1", time.Now())
|
|
||||||
|
|
||||||
checkoutTag := CheckoutTag{
|
|
||||||
Tag: "tag-1",
|
|
||||||
}
|
|
||||||
authOpts := &git.AuthOptions{
|
|
||||||
TransportOptionsURL: getTransportOptionsURL(git.HTTP),
|
|
||||||
}
|
|
||||||
repoURL := server.HTTPAddress() + "/" + repoPath
|
|
||||||
tmpDir := t.TempDir()
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.TODO(), timeout)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
cc, err := checkoutTag.Checkout(ctx, tmpDir, repoURL, authOpts)
|
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
|
||||||
g.Expect(cc.String()).To(Equal("tag-1" + "/" + commit.Id().String()))
|
|
||||||
g.Expect(git.IsConcreteCommit(*cc)).To(Equal(true))
|
|
||||||
|
|
||||||
checkoutTag.LastRevision = "tag-1" + "/" + commit.Id().String()
|
|
||||||
cc, err = checkoutTag.Checkout(ctx, tmpDir, repoURL, authOpts)
|
|
||||||
g.Expect(err).ToNot(HaveOccurred())
|
|
||||||
g.Expect(cc.String()).To(Equal("tag-1" + "/" + commit.Id().String()))
|
|
||||||
g.Expect(git.IsConcreteCommit(*cc)).To(Equal(false))
|
|
||||||
}
|
|
||||||
|
|
||||||
func getTransportOptionsURL(transport git.TransportType) string {
|
func getTransportOptionsURL(transport git.TransportType) string {
|
||||||
letterRunes := []rune("abcdefghijklmnopqrstuvwxyz1234567890")
|
letterRunes := []rune("abcdefghijklmnopqrstuvwxyz1234567890")
|
||||||
b := make([]rune, 10)
|
b := make([]rune, 10)
|
||||||
|
|
@ -645,3 +467,9 @@ func getTransportOptionsURL(transport git.TransportType) string {
|
||||||
}
|
}
|
||||||
return string(transport) + "://" + string(b)
|
return string(transport) + "://" + string(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func enableManagedTransport() {
|
||||||
|
fg := feathelper.FeatureGates{}
|
||||||
|
fg.SupportedFeatures(features.FeatureGates())
|
||||||
|
managed.InitManagedTransport(logr.Discard())
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,9 +29,11 @@ import (
|
||||||
|
|
||||||
"github.com/elazarl/goproxy"
|
"github.com/elazarl/goproxy"
|
||||||
"github.com/fluxcd/pkg/gittestserver"
|
"github.com/fluxcd/pkg/gittestserver"
|
||||||
|
feathelper "github.com/fluxcd/pkg/runtime/features"
|
||||||
"github.com/go-logr/logr"
|
"github.com/go-logr/logr"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
|
|
||||||
|
"github.com/fluxcd/source-controller/internal/features"
|
||||||
"github.com/fluxcd/source-controller/pkg/git"
|
"github.com/fluxcd/source-controller/pkg/git"
|
||||||
"github.com/fluxcd/source-controller/pkg/git/gogit"
|
"github.com/fluxcd/source-controller/pkg/git/gogit"
|
||||||
"github.com/fluxcd/source-controller/pkg/git/libgit2"
|
"github.com/fluxcd/source-controller/pkg/git/libgit2"
|
||||||
|
|
@ -45,6 +47,9 @@ func TestCheckoutStrategyForImplementation_Proxied(t *testing.T) {
|
||||||
// for libgit2 we are only testing for managed transport,
|
// for libgit2 we are only testing for managed transport,
|
||||||
// as unmanaged is sunsetting.
|
// as unmanaged is sunsetting.
|
||||||
// Unmanaged transport does not support HTTP_PROXY.
|
// Unmanaged transport does not support HTTP_PROXY.
|
||||||
|
fg := feathelper.FeatureGates{}
|
||||||
|
fg.SupportedFeatures(features.FeatureGates())
|
||||||
|
|
||||||
managed.InitManagedTransport(logr.Discard())
|
managed.InitManagedTransport(logr.Discard())
|
||||||
|
|
||||||
type cleanupFunc func()
|
type cleanupFunc func()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue