-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds unit tests for fetch and upload for OCI
Signed-off-by: PuneetPunamiya <[email protected]>
- Loading branch information
1 parent
f20a91e
commit 706d913
Showing
56 changed files
with
21,703 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.