Skip to content

Commit

Permalink
fixes bug in libostree list refs that caused checksum to be returned …
Browse files Browse the repository at this point in the history
…as ref_name

updates tests to prove it works
  • Loading branch information
kishie committed Jan 26, 2024
1 parent b7c7e43 commit f3c8668
Show file tree
Hide file tree
Showing 27 changed files with 28 additions and 26 deletions.
6 changes: 3 additions & 3 deletions internal/plugins/ostree/pkg/libostree/generate-testdata.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ ostree --repo=testdata/repo init --mode=archive
echo "Test file in a simple ostree repo - branch test1" > ./testdata/tree/testfile.txt
ostree --repo=testdata/repo commit --branch=test1 ./testdata/tree/

echo "Test file in a simple ostree repo - branch test2" > ./testdata/tree/testfile.txt
ostree --repo=testdata/repo commit --branch=test2 ./testdata/tree/
echo "Test file in a simple ostree repo - branch long/branch/name/test2" > ./testdata/tree/testfile.txt
ostree --repo=testdata/repo commit --branch=long/branch/name/test2 ./testdata/tree/

echo "Another test file" > ./testdata/tree/another_testfile.txt
ostree --repo=testdata/repo commit --branch=test2 ./testdata/tree/
ostree --repo=testdata/repo commit --branch=long/branch/name/test2 ./testdata/tree/

ostree --repo=testdata/repo summary --update

Expand Down
36 changes: 18 additions & 18 deletions internal/plugins/ostree/pkg/libostree/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -154,36 +155,35 @@ func TestRepo_Pull(t *testing.T) {
})

t.Run("should list refs from original repo", func(t *testing.T) {
expectedChecksums := map[string]bool{}
test1Data, err := os.ReadFile("testdata/repo/refs/heads/test1")
test2Data, err := os.ReadFile("testdata/repo/refs/heads/test2")
repoHeadsPrefix := "testdata/repo/refs/heads"
branch1Name := "test1"
branch2Name := "long/branch/name/test2"
branch1Data, err := os.ReadFile(filepath.Join(repoHeadsPrefix, branch1Name))
branch2Data, err := os.ReadFile(filepath.Join(repoHeadsPrefix, branch2Name))
if err != nil {
t.Errorf("failed to read refs file: %s", err.Error())
}

// Update in case of changes to testdata
expectedChecksums[strings.TrimRight(string(test1Data), "\n")] = false
expectedChecksums[strings.TrimRight(string(test2Data), "\n")] = false
expectedBranches := map[string]string{
branch1Name: strings.TrimRight(string(branch1Data), "\n"),
branch2Name: strings.TrimRight(string(branch2Data), "\n"),
}

refs, err := repo.ListRefsExt(ListRefsExtFlagsNone)
assert.NoError(t, err)
if err != nil {
assert.Failf(t, "failed to list refs", "err: %s", err.Error())
}
assert.NotEmpty(t, refs)
assert.Len(t, refs, 2)

// This could be a static list of assert statements but the loop captures unexpected extra refs.
for _, ref := range refs {
checksum := ref.Checksum
assert.NotEmpty(t, checksum)
for sum := range expectedChecksums {
if sum == checksum {
expectedChecksums[sum] = true
}
}
}
assert.NotEmpty(t, ref.Name)
assert.NotEmpty(t, ref.Checksum)

for sum, exists := range expectedChecksums {
assert.True(t, exists, "checksum %s not found", sum)
// Ensure the ref is one of the expected branches
expectedChecksum, exists := expectedBranches[ref.Name]
assert.True(t, exists, "unexpected ref: %s", ref.Name)
assert.Equal(t, expectedChecksum, ref.Checksum)
}
})

Expand Down
8 changes: 5 additions & 3 deletions internal/plugins/ostree/pkg/libostree/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,17 +286,19 @@ func (r *Repo) ListRefsExt(flags ListRefsExtFlags, prefix ...string) ([]Ref, err
var iter C.GHashTableIter
C.g_hash_table_iter_init(&iter, outAllRefs)

var cRef, cChecksum C.gpointer
var cRef C.gpointer
var cChecksum C.gpointer
var ret []Ref
for C.g_hash_table_iter_next(&iter, &cRef, &cChecksum) == C.gboolean(1) {
if cRef == nil {
break
}

ref := (*C.OstreeCollectionRef)(unsafe.Pointer(&cRef))
// GHashTable
//ref := (*C.OstreeCollectionRef)(unsafe.Pointer(&cRef))

ret = append(ret, Ref{
Name: C.GoString(ref.ref_name),
Name: C.GoString((*C.char)(cRef)), //C.GoString(ref.ref_name),
Checksum: C.GoString((*C.char)(cChecksum)),
})
}
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
e7f3faeb246374c3cc23053bee0bd027c40ca43c3846a78acd0a3e0fe56a07b9
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0e1518ee5f0421ad34685958b662c4947ea18a96be03b406bc8eb9ccf913ff22
6b6824da30f8f46e3087d60a9d5393f14637ccaa668e810be8ad62f7e5b28ab4

This file was deleted.

Binary file modified internal/plugins/ostree/pkg/libostree/testdata/repo/summary
Binary file not shown.

0 comments on commit f3c8668

Please sign in to comment.