Skip to content

Commit

Permalink
Adds unit tests for fetch and upload for OCI
Browse files Browse the repository at this point in the history
Signed-off-by: PuneetPunamiya <[email protected]>
  • Loading branch information
PuneetPunamiya committed Oct 3, 2024
1 parent f20a91e commit 706d913
Show file tree
Hide file tree
Showing 56 changed files with 21,703 additions and 0 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/google/go-containerregistry v0.20.2
github.com/moby/patternmatcher v0.6.0
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
google.golang.org/api v0.199.0
gotest.tools/v3 v3.5.1
)
Expand All @@ -38,6 +39,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/sts v1.31.3 // indirect
github.com/aws/smithy-go v1.21.0 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.14.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/cli v27.1.1+incompatible // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/docker-credential-helpers v0.7.0 // indirect
Expand All @@ -60,6 +62,7 @@ require (
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.9.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
Expand All @@ -83,4 +86,5 @@ require (
google.golang.org/grpc v1.67.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -237,6 +239,8 @@ golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.199.0 h1:aWUXClp+VFJmqE0JPvpZOK3LDQMyFKYIow4etYd9qxs=
google.golang.org/api v0.199.0/go.mod h1:ohG4qSztDJmZdjK/Ar6MhbAmb/Rpi4JHOqagsh90K28=
Expand Down
100 changes: 100 additions & 0 deletions internal/provider/oci/fetch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package oci

import (
"context"
"fmt"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"

"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/registry"
"github.com/google/go-containerregistry/pkg/v1/random"
"github.com/stretchr/testify/assert"
)

func TestFetch(t *testing.T) {
// Set up a fake registry.
s := httptest.NewServer(registry.New())
defer s.Close()

u, err := url.Parse(s.URL)
assert.NoError(t, err, "Failed to parset he url")

hash := "d98f9152dc810a4d5dcf737c72d6763c56708c6036b70fceb7947a798f628797"
target := fmt.Sprintf("%s/test/crane:{{hash}}", u.Host)
folder := t.TempDir()
insecure := false

img, err := random.Image(1024, 5)
assert.NoError(t, err, "Failed to create random image")

err = crane.Push(img, fmt.Sprintf("%s/test/crane:%s", u.Host, hash))
assert.NoError(t, err, "Failed to push the image")

err = os.MkdirAll(folder, os.ModePerm)
assert.NoError(t, err, "Error creating folder for cache")

err = Fetch(context.Background(), hash, target, folder, insecure)
assert.NoError(t, err, "Fetch should not return any error")

cacheFilePath := filepath.Join(folder, "cache.tar")
_, err = os.Stat(cacheFilePath)
assert.True(t, os.IsNotExist(err), "Cache tar file should be removed after extraction")
}

func TestFetchImageNotFound(t *testing.T) {
// Set up a fake registry.
s := httptest.NewServer(registry.New())
defer s.Close()

u, err := url.Parse(s.URL)
assert.NoError(t, err, "Failed to parset he url")

hash := "nonexistinghash"
target := fmt.Sprintf("%s/test/crane:{{hash}}", u.Host)
folder := t.TempDir()
insecure := false

err = Fetch(context.Background(), hash, target, folder, insecure)
assert.Error(t, err, "Fetch should return an error for nonexistent image")
assert.True(t,
containsAny(err.Error(), []string{"NAME_UNKNOWN", "MANIFEST_UNKNOWN"}),
"Error should indicate that the image manifest or name was not found")
}

func TestFetchInvalidFolder(t *testing.T) {
// Set up a fake registry.
s := httptest.NewServer(registry.New())
defer s.Close()

u, err := url.Parse(s.URL)
assert.NoError(t, err, "Failed to parset he url")

hash := "d98f9152dc810a4d5dcf737c72d6763c56708c6036b70fceb7947a798f628797"
target := fmt.Sprintf("%s/test/crane:{{hash}}", u.Host)
img, err := random.Image(1024, 5)
assert.NoError(t, err, "Failed to create random image")
err = crane.Push(img, fmt.Sprintf("%s/test/crane:%s", u.Host, hash))
assert.NoError(t, err, "Failed to push image to registry")

folder := "/root"
insecure := false

err = Fetch(context.Background(), hash, target, folder, insecure)

assert.Error(t, err, "Fetch should return an error when folder is not writable")
assert.Contains(t, err.Error(), "permission denied", "Error should indicate permission issues for the folder")
}

func containsAny(errMsg string, substrs []string) bool {
for _, substr := range substrs {
if contains := strings.Contains(errMsg, substr); contains {
return true
}
}
return false
}
55 changes: 55 additions & 0 deletions internal/provider/oci/upload_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package oci

import (
"context"
"fmt"
"net/http/httptest"
"net/url"
"os"
"testing"

"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/registry"
"github.com/stretchr/testify/assert"
)

func TestUpload(t *testing.T) {
// Step 1: Set up a fake registry
s := httptest.NewServer(registry.New())
defer s.Close()

u, err := url.Parse(s.URL)
assert.NoError(t, err, "Failed to parse the registry URL")

hash := "testhash"
target := fmt.Sprintf("%s/test/crane:{{hash}}", u.Host)
folder := t.TempDir() // Use a temporary directory as the source folder
insecure := false

err = os.WriteFile(fmt.Sprintf("%s/test.txt", folder), []byte("dummy content"), 0o644)
assert.NoError(t, err, "Failed to create dummy file")

err = Upload(context.Background(), hash, target, folder, insecure)
assert.NoError(t, err, "Upload should not return any error")

pulledImage, err := crane.Pull(fmt.Sprintf("%s/test/crane:testhash", u.Host), crane.Insecure)
assert.NoError(t, err, "Failed to pull the image back from the registry")

assert.NotNil(t, pulledImage, "The pulled image should not be nil")

s.Close()
}

func TestUploadFailure(t *testing.T) {
hash := "testhash"
target := "dummyhost:8000/test/crane:{{hash}}"
folder := t.TempDir() // Use a temporary directory as the source folder
insecure := false

err := os.WriteFile(fmt.Sprintf("%s/test.txt", folder), []byte("dummy content"), 0o644)
assert.NoError(t, err, "Failed to create dummy file")

err = Upload(context.Background(), hash, target, folder, insecure)
assert.Error(t, err, "Upload should not return any error")
assert.Contains(t, err.Error(), "no such host", "Error should indicate connection failure")
}
15 changes: 15 additions & 0 deletions vendor/github.com/davecgh/go-spew/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

145 changes: 145 additions & 0 deletions vendor/github.com/davecgh/go-spew/spew/bypass.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 706d913

Please sign in to comment.