Skip to content

Commit 4ab219e

Browse files
author
Paulo Gomes
committed
Fix tests after upgrading to libgit2 1.3.0
Signed-off-by: Paulo Gomes <[email protected]>
1 parent b898759 commit 4ab219e

File tree

3 files changed

+119
-10
lines changed

3 files changed

+119
-10
lines changed

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,19 @@ update-attributions:
271271
./hack/update-attributions.sh
272272

273273
verify: update-attributions fmt
274+
ifneq ($(shell grep -o 'LIBGIT2_IMG ?= \w.*' Makefile | cut -d ' ' -f 3):$(shell grep -o 'LIBGIT2_TAG ?= \w.*' Makefile | cut -d ' ' -f 3), \
275+
$(shell grep -o "LIBGIT2_IMG=\w.*" Dockerfile | cut -d'=' -f2):$(shell grep -o "LIBGIT2_TAG=\w.*" Dockerfile | cut -d'=' -f2))
276+
@{ \
277+
echo "LIBGIT2_IMG and LIBGIT2_TAG must match in both Makefile and Dockerfile"; \
278+
exit 1; \
279+
}
280+
endif
281+
ifneq ($(shell grep -o 'LIBGIT2_TAG ?= \w.*' Makefile | cut -d ' ' -f 3), $(shell grep -o "LIBGIT2_TAG=.*" tests/fuzz/oss_fuzz_build.sh | sed 's;LIBGIT2_TAG="$${LIBGIT2_TAG:-;;g' | sed 's;}";;g'))
282+
@{ \
283+
echo "LIBGIT2_TAG must match in both Makefile and tests/fuzz/oss_fuzz_build.sh"; \
284+
exit 1; \
285+
}
286+
endif
274287
ifneq (, $(shell git status --porcelain --untracked-files=no))
275288
@{ \
276289
echo "working directory is dirty:"; \

controllers/update_test.go

Lines changed: 105 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ import (
2828
"strings"
2929
"time"
3030

31-
"github.com/ProtonMail/go-crypto/openpgp"
32-
"github.com/ProtonMail/go-crypto/openpgp/armor"
3331
"github.com/fluxcd/pkg/apis/acl"
32+
git2go "github.com/libgit2/git2go/v33"
33+
"golang.org/x/crypto/openpgp"
34+
"golang.org/x/crypto/openpgp/armor"
35+
3436
"github.com/go-git/go-billy/v5/memfs"
3537
"github.com/go-git/go-git/v5"
3638
"github.com/go-git/go-git/v5/config"
@@ -61,9 +63,15 @@ import (
6163

6264
const timeout = 10 * time.Second
6365

64-
// Copied from
65-
// https://github.com/fluxcd/source-controller/blob/master/controllers/suite_test.go
66-
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz1234567890")
66+
var (
67+
// Copied from
68+
// https://github.com/fluxcd/source-controller/blob/master/controllers/suite_test.go
69+
letterRunes = []rune("abcdefghijklmnopqrstuvwxyz1234567890")
70+
71+
gitServer *gittestserver.GitServer
72+
73+
repositoryPath string
74+
)
6775

6876
func randStringRunes(n int) string {
6977
b := make([]rune, n)
@@ -76,10 +84,8 @@ func randStringRunes(n int) string {
7684
var _ = Describe("ImageUpdateAutomation", func() {
7785
var (
7886
branch string
79-
repositoryPath string
8087
namespace *corev1.Namespace
8188
username, password string
82-
gitServer *gittestserver.GitServer
8389
)
8490

8591
// Start the git server
@@ -782,7 +788,7 @@ Images:
782788
// verify commit
783789
ent, err := commit.Verify(b.String())
784790
Expect(err).ToNot(HaveOccurred())
785-
Expect(ent.PrimaryKey.Fingerprint).To(Equal(pgpEntity.PrimaryKey.Fingerprint))
791+
Expect(ent.PrimaryKey.Fingerprint).To(Equal(pgpEntity.PrimaryKey.Fingerprint[:]))
786792
})
787793
})
788794

@@ -1353,11 +1359,27 @@ func initGitRepo(gitServer *gittestserver.GitServer, fixture, branch, repository
13531359
return err
13541360
}
13551361

1356-
return remote.Push(&git.PushOptions{
1362+
err = remote.Push(&git.PushOptions{
13571363
RefSpecs: []config.RefSpec{
13581364
config.RefSpec(fmt.Sprintf("refs/heads/%s:refs/heads/%s", branch, branch)),
13591365
},
13601366
})
1367+
if err != nil {
1368+
return err
1369+
}
1370+
1371+
// Adhoc fix to a bug in which most clone operations during test fail.
1372+
// This started with the upgrade from libgit2 1.1.1 to 1.3.0, and
1373+
// requires further investigation as to why repositories created and
1374+
// amended by go-git stopped working for libgit2 clone operations.
1375+
repo2, err := git2go.OpenRepository(gitServer.Root() + repositoryPath)
1376+
if err != nil {
1377+
return err
1378+
}
1379+
defer repo2.Free()
1380+
_, err = commitFile(repo2, "branch", "init", time.Now())
1381+
1382+
return err
13611383
}
13621384

13631385
func checkoutBranch(repo *git.Repository, branch string) error {
@@ -1413,3 +1435,77 @@ func mergeBranchIntoHead(repo *git.Repository, pushBranch string) {
14131435
})
14141436
Expect(err).NotTo(HaveOccurred())
14151437
}
1438+
1439+
// copied from source-controller/pkg/git/libgit2/checkout.go
1440+
func commitFile(repo *git2go.Repository, path, content string, time time.Time) (*git2go.Oid, error) {
1441+
var parentC []*git2go.Commit
1442+
head, err := headCommit(repo)
1443+
if err == nil {
1444+
defer head.Free()
1445+
parentC = append(parentC, head)
1446+
}
1447+
1448+
index, err := repo.Index()
1449+
if err != nil {
1450+
return nil, err
1451+
}
1452+
defer index.Free()
1453+
1454+
blobOID, err := repo.CreateBlobFromBuffer([]byte(content))
1455+
if err != nil {
1456+
return nil, err
1457+
}
1458+
1459+
entry := &git2go.IndexEntry{
1460+
Mode: git2go.FilemodeBlob,
1461+
Id: blobOID,
1462+
Path: path,
1463+
}
1464+
1465+
if err := index.Add(entry); err != nil {
1466+
return nil, err
1467+
}
1468+
if err := index.Write(); err != nil {
1469+
return nil, err
1470+
}
1471+
1472+
treeID, err := index.WriteTree()
1473+
if err != nil {
1474+
return nil, err
1475+
}
1476+
1477+
tree, err := repo.LookupTree(treeID)
1478+
if err != nil {
1479+
return nil, err
1480+
}
1481+
defer tree.Free()
1482+
1483+
c, err := repo.CreateCommit("HEAD", mockSignature(time), mockSignature(time), "Committing "+path, tree, parentC...)
1484+
if err != nil {
1485+
return nil, err
1486+
}
1487+
return c, nil
1488+
}
1489+
1490+
// copied from source-controller/pkg/git/libgit2/checkout.go
1491+
func mockSignature(time time.Time) *git2go.Signature {
1492+
return &git2go.Signature{
1493+
Name: "Jane Doe",
1494+
1495+
When: time,
1496+
}
1497+
}
1498+
1499+
// copied from source-controller/pkg/git/libgit2/checkout.go
1500+
func headCommit(repo *git2go.Repository) (*git2go.Commit, error) {
1501+
head, err := repo.Head()
1502+
if err != nil {
1503+
return nil, err
1504+
}
1505+
defer head.Free()
1506+
c, err := repo.LookupCommit(head.Target())
1507+
if err != nil {
1508+
return nil, err
1509+
}
1510+
return c, nil
1511+
}

tests/fuzz/oss_fuzz_build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
set -euxo pipefail
1818

19-
LIBGIT2_TAG="${LIBGIT2_TAG:-libgit2-1.1.1-7}"
19+
LIBGIT2_TAG="${LIBGIT2_TAG:-libgit2-1.3.0-2}"
2020
GOPATH="${GOPATH:-/root/go}"
2121
GO_SRC="${GOPATH}/src"
2222
PROJECT_PATH="github.com/fluxcd/image-automation-controller"

0 commit comments

Comments
 (0)