Skip to content

Commit

Permalink
test: pkg/image/oci to 90% coverage
Browse files Browse the repository at this point in the history
Signed-off-by: cpendery <[email protected]>
  • Loading branch information
cpendery committed Jun 15, 2022
1 parent 9e3b40f commit 4911c51
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 0 deletions.
53 changes: 53 additions & 0 deletions pkg/image/oci/directory_provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package oci

import (
"testing"

"github.com/anchore/stereoscope/pkg/file"
"github.com/stretchr/testify/assert"
)

func Test_NewProviderFromPath(t *testing.T) {
//GIVEN
path := "path"
generator := file.TempDirGenerator{}

//WHEN
provider := NewProviderFromPath(path, &generator)

//THEN
assert.NotNil(t, provider.path)
assert.NotNil(t, provider.tmpDirGen)
}

func Test_Directory_Provide(t *testing.T) {
//GIVEN
tests := []struct {
name string
path string
expectedErr bool
}{
{"fails to read from path", "", true},
{"reads invalid oci manifest", "test-fixtures/invalid_file", true},
{"reads valid oci manifest with no images", "test-fixtures/no_manifests", true},
{"reads a fully correct manifest", "test-fixtures/valid_manifest", false},
}

for _, tc := range tests {
provider := NewProviderFromPath(tc.path, file.NewTempDirGenerator("tempDir"))
t.Run(tc.name, func(t *testing.T) {
//WHEN
image, err := provider.Provide(nil)

//THEN
if tc.expectedErr {
assert.Error(t, err)
assert.Nil(t, image)
} else {
assert.NoError(t, err)
assert.NotNil(t, image)
}

})
}
}
85 changes: 85 additions & 0 deletions pkg/image/oci/registry_provider_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,99 @@
package oci

import (
"context"
"reflect"
"testing"

"github.com/anchore/stereoscope/pkg/file"
"github.com/anchore/stereoscope/pkg/image"
"github.com/google/go-containerregistry/pkg/name"
"github.com/stretchr/testify/assert"
)

func Test_NewProviderFromRegistry(t *testing.T) {
//GIVEN
imageStr := "image"
generator := file.TempDirGenerator{}
options := image.RegistryOptions{}
platform := &image.Platform{}

//WHEN
provider := NewProviderFromRegistry(imageStr, &generator, options, platform)

//THEN
assert.NotNil(t, provider.imageStr)
assert.NotNil(t, provider.tmpDirGen)
assert.NotNil(t, provider.registryOptions)
assert.NotNil(t, provider.platform)
}

func Test_Registry_Provide_FailsUnauthorized(t *testing.T) {
//GIVEN
imageStr := "image"
generator := file.TempDirGenerator{}
options := image.RegistryOptions{
InsecureSkipTLSVerify: true,
Credentials: []image.RegistryCredentials{
{
Authority: "index.docker.io",
Token: "token",
},
},
}
platform := &image.Platform{}
provider := NewProviderFromRegistry(imageStr, &generator, options, platform)
ctx := context.Background()

//WHEN
result, err := provider.Provide(ctx)

//THEN
assert.Nil(t, result)
assert.NoError(t, err)
}

func Test_Registry_Provide_FailsImageMissingPlatform(t *testing.T) {
//GIVEN
imageStr := "docker.io/golang:1.18"
generator := file.TempDirGenerator{}
options := image.RegistryOptions{
InsecureSkipTLSVerify: true,
}
platform := &image.Platform{}
provider := NewProviderFromRegistry(imageStr, &generator, options, platform)
ctx := context.Background()

//WHEN
result, err := provider.Provide(ctx)

//THEN
assert.Nil(t, result)
assert.Error(t, err)
}

func Test_Registry_Provide(t *testing.T) {
//GIVEN
imageStr := "golang:1.18"
generator := file.TempDirGenerator{}
options := image.RegistryOptions{
InsecureSkipTLSVerify: true,
}
platform := &image.Platform{
OS: "linux",
Architecture: "amd64",
}
provider := NewProviderFromRegistry(imageStr, &generator, options, platform)
ctx := context.Background()

//WHEN
result, err := provider.Provide(ctx)

//THEN
assert.NotNil(t, result)
assert.NoError(t, err)
}

func Test_prepareReferenceOptions(t *testing.T) {
tests := []struct {
name string
Expand Down
45 changes: 45 additions & 0 deletions pkg/image/oci/tarball_provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package oci

import (
"testing"

"github.com/anchore/stereoscope/pkg/file"
"github.com/stretchr/testify/assert"
)

func Test_NewProviderFromTarball(t *testing.T) {
//GIVEN
path := "path"
generator := file.TempDirGenerator{}

//WHEN
provider := NewProviderFromTarball(path, &generator)

//THEN
assert.NotNil(t, provider.path)
assert.NotNil(t, provider.tmpDirGen)
}

func Test_TarballProvide(t *testing.T) {
//GIVEN
provider := NewProviderFromTarball("test-fixtures/file.tar", file.NewTempDirGenerator("tempDir"))

//WHEN
image, err := provider.Provide(nil)

//THEN
assert.NoError(t, err)
assert.NotNil(t, image)
}

func Test_TarballProvide_Fails(t *testing.T) {
//GIVEN
provider := NewProviderFromTarball("", file.NewTempDirGenerator("tempDir"))

//WHEN
image, err := provider.Provide(nil)

//THEN
assert.Error(t, err)
assert.Nil(t, image)
}
Binary file added pkg/image/oci/test-fixtures/file.tar
Binary file not shown.
Empty file.
16 changes: 16 additions & 0 deletions pkg/image/oci/test-fixtures/no_manifests/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"schemaVersion": 2,
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"config": {
"mediaType": "application/vnd.docker.container.image.v1+json",
"size": 1520,
"digest": "sha256:1815c82652c03bfd8644afda26fb184f2ed891d921b20a0703b46768f9755c57"
},
"layers": [
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 972,
"digest": "sha256:b04784fba78d739b526e27edc02a5a8cd07b1052e9283f5fc155828f4b614c28"
}
]
}
15 changes: 15 additions & 0 deletions pkg/image/oci/test-fixtures/valid_manifest/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"schemaVersion": 2,
"mediaType": "application/vnd.docker.distribution.manifest.list.v2+json",
"manifests": [
{
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"size": 424,
"digest": "sha256:f67dcc5fc786f04f0743abfe0ee5dae9bd8caf8efa6c8144f7f2a43889dc513b",
"platform": {
"architecture": "arm",
"os": "linux"
}
}
]
}

0 comments on commit 4911c51

Please sign in to comment.