Skip to content

Commit

Permalink
Fixing - these tests should push and pull from a local registry to av…
Browse files Browse the repository at this point in the history
…oid getting rate limited

Signed-off-by: Juan Bustamante <[email protected]>
  • Loading branch information
jjbustamante committed Apr 23, 2024
1 parent 4aedd4c commit c2871d0
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 26 deletions.
4 changes: 2 additions & 2 deletions acceptance/reproducibility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ func testReproducibility(t *testing.T, _ spec.G, it spec.S) {
it.Before(func() {
dockerClient = h.DockerCli(t)

daemonInfo, err := dockerClient.Info(context.TODO())
daemonInfo, err := dockerClient.ServerVersion(context.TODO())
h.AssertNil(t, err)

daemonOS := daemonInfo.OSType
daemonOS := daemonInfo.Os

runnableBaseImageName = h.RunnableBaseImage(daemonOS)
h.PullIfMissing(t, dockerClient, runnableBaseImageName)
Expand Down
78 changes: 55 additions & 23 deletions remote/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import (
"testing"

"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/random"
remote2 "github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"

Expand All @@ -15,32 +18,46 @@ import (
)

func TestRemoteNewIndex(t *testing.T) {
dockerConfigDir, err := os.MkdirTemp("", "test.docker.config.remote.index.dir")
h.AssertNil(t, err)
defer os.RemoveAll(dockerConfigDir)

dockerRegistry = h.NewDockerRegistry(h.WithAuth(dockerConfigDir))
dockerRegistry.Start(t)
defer dockerRegistry.Stop(t)
os.Setenv("DOCKER_CONFIG", dockerConfigDir)
defer os.Unsetenv("DOCKER_CONFIG")

spec.Run(t, "RemoteNewIndex", testNewIndex, spec.Parallel(), spec.Report(report.Terminal{}))
}

// FIXME: these tests should push and pull from a local registry to avoid getting rate limited
const numberOfManifests = 2

func testNewIndex(t *testing.T, when spec.G, it spec.S) {
var (
idx imgutil.ImageIndex
xdgPath string
err error
idx imgutil.ImageIndex
manifests []v1.Hash
remoteIndexRepoName string
xdgPath string
err error
)

it.Before(func() {
// creates the directory to save all the OCI images on disk
xdgPath, err = os.MkdirTemp("", "image-indexes")
h.AssertNil(t, err)
remoteIndexRepoName = newTestImageIndexName("random")
randomIndex := setUpRandomRemoteIndex(t, remoteIndexRepoName, 1, numberOfManifests)
manifests = h.DigestsFromImageIndex(t, randomIndex)
})

it.After(func() {
err := os.RemoveAll(xdgPath)
err = os.RemoveAll(xdgPath)
h.AssertNil(t, err)
})

when("#NewIndex", func() {
it("should have expected indexOptions", func() {
idx, err = remote.NewIndex(
"busybox:1.36-musl",
"some-index",
imgutil.WithInsecure(),
imgutil.WithKeychain(authn.DefaultKeychain),
imgutil.WithXDGRuntimePath(xdgPath),
Expand All @@ -50,14 +67,14 @@ func testNewIndex(t *testing.T, when spec.G, it spec.S) {
imgIx, ok := idx.(*imgutil.CNBIndex)
h.AssertEq(t, ok, true)
h.AssertEq(t, imgIx.XdgPath, xdgPath)
h.AssertEq(t, imgIx.RepoName, "busybox:1.36-musl")
h.AssertEq(t, imgIx.RepoName, "some-index")
})

it("should return an error when index with the given repoName doesn't exists", func() {
_, err = remote.NewIndex(
"my-index",
imgutil.WithKeychain(authn.DefaultKeychain),
imgutil.FromBaseIndex("some-not-exist-index"),
imgutil.FromBaseIndex("some-none-existing-index"),
)
h.AssertNotEq(t, err, nil)
})
Expand All @@ -66,7 +83,7 @@ func testNewIndex(t *testing.T, when spec.G, it spec.S) {
idx, err = remote.NewIndex(
"my-index",
imgutil.WithKeychain(authn.DefaultKeychain),
imgutil.FromBaseIndex("busybox:1.36-musl"),
imgutil.FromBaseIndex(remoteIndexRepoName),
)
h.AssertNil(t, err)

Expand All @@ -75,50 +92,65 @@ func testNewIndex(t *testing.T, when spec.G, it spec.S) {

mfest, err := imgIx.IndexManifest()
h.AssertNil(t, err)
h.AssertNotEq(t, mfest, nil)
h.AssertEq(t, len(mfest.Manifests), 8)
h.AssertNotNil(t, mfest)
h.AssertEq(t, len(mfest.Manifests), numberOfManifests)
})

it("should able to call #ImageIndex", func() {
idx, err = remote.NewIndex(
"my-index",
imgutil.WithKeychain(authn.DefaultKeychain),
imgutil.FromBaseIndex("busybox:1.36-musl"),
imgutil.FromBaseIndex(remoteIndexRepoName),
)
h.AssertNil(t, err)

imgIx, ok := idx.(*imgutil.CNBIndex)
h.AssertEq(t, ok, true)

// linux/amd64
// some none existing hash
hash1, err := v1.NewHash(
"sha256:b9d056b83bb6446fee29e89a7fcf10203c562c1f59586a6e2f39c903597bda34",
)
h.AssertNil(t, err)

_, err = imgIx.ImageIndex.ImageIndex(hash1)
// err is "no child with digest"
h.AssertNotEq(t, err.Error(), "empty index")
})

it("should able to call #Image", func() {
idx, err = remote.NewIndex(
"my-index",
imgutil.WithKeychain(authn.DefaultKeychain),
imgutil.FromBaseIndex("busybox:1.36-musl"),
imgutil.FromBaseIndex(remoteIndexRepoName),
)
h.AssertNil(t, err)

imgIdx, ok := idx.(*imgutil.CNBIndex)
h.AssertEq(t, ok, true)

// linux/amd64
hash1, err := v1.NewHash(
"sha256:b9d056b83bb6446fee29e89a7fcf10203c562c1f59586a6e2f39c903597bda34",
)
h.AssertNil(t, err)

_, err = imgIdx.Image(hash1)
// select one valid digest from the index
_, err = imgIdx.Image(manifests[0])
h.AssertNil(t, err)
})
})
}

func newTestImageIndexName(name string) string {
return dockerRegistry.RepoName(name + "-" + h.RandString(10))
}

// setUpRandomRemoteIndex creates a random image index with the provided (count) number of manifest
// each manifest will have the provided number of layers
func setUpRandomRemoteIndex(t *testing.T, repoName string, layers, count int64) v1.ImageIndex {
ref, err := name.ParseReference(repoName, name.WeakValidation)
h.AssertNil(t, err)

randomIndex, err := random.Index(1024, layers, count)
h.AssertNil(t, err)

err = remote2.WriteIndex(ref, randomIndex, remote2.WithAuthFromKeychain(authn.DefaultKeychain))
h.AssertNil(t, err)

return randomIndex
}
12 changes: 11 additions & 1 deletion testhelpers/testhelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,6 @@ func RemoteImage(t *testing.T, testImageName string, opts []remote.Option) v1.Im

testImage, err := remote.Image(r, opts...)
AssertNil(t, err)

return testImage
}

Expand Down Expand Up @@ -592,6 +591,17 @@ func ReadImageIndex(t *testing.T, path string) v1.ImageIndex {
return localIndex
}

func DigestsFromImageIndex(t *testing.T, index v1.ImageIndex) []v1.Hash {
manifests, err := index.IndexManifest()
AssertNil(t, err)

var hashes []v1.Hash
for _, manifest := range manifests.Manifests {
hashes = append(hashes, manifest.Digest)
}
return hashes
}

func ReadIndexManifest(t *testing.T, path string) *v1.IndexManifest {
indexPath := filepath.Join(path, "index.json")
AssertPathExists(t, filepath.Join(path, "oci-layout"))
Expand Down

0 comments on commit c2871d0

Please sign in to comment.