Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: Improve unit tests for pkg/registry/client.go #865

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 112 additions & 1 deletion pkg/registry/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
"testing"
"time"

"github.com/distribution/distribution/v3/manifest"
"github.com/distribution/distribution/v3/manifest/manifestlist"
"github.com/distribution/distribution/v3/manifest/ocischema"
"github.com/distribution/distribution/v3/manifest/schema2"

"github.com/distribution/distribution/v3"
"github.com/distribution/distribution/v3/manifest/schema1" //nolint:staticcheck
v1 "github.com/opencontainers/image-spec/specs-go/v1"
Expand Down Expand Up @@ -444,7 +449,7 @@ func Test_TagMetadata(t *testing.T) {

})

t.Run("Check for correct error handling when time stamp cannot be parsed", func(t *testing.T) {
t.Run("Check for invalid/valid timestamp and non-match platforms", func(t *testing.T) {
ts := "invalid"
meta1 := &schema1.SignedManifest{ //nolint:staticcheck
Manifest: schema1.Manifest{ //nolint:staticcheck
Expand All @@ -461,6 +466,103 @@ func Test_TagMetadata(t *testing.T) {
require.NoError(t, err)
_, err = client.TagMetadata(meta1, &options.ManifestOptions{})
require.Error(t, err)

ts = time.Now().Format(time.RFC3339Nano)
opts := &options.ManifestOptions{}
meta1.Manifest.History[0].V1Compatibility = `{"created":"` + ts + `"}`
tagInfo, _ := client.TagMetadata(meta1, opts)
assert.Equal(t, ts, tagInfo.CreatedAt.Format(time.RFC3339Nano))

opts.WithPlatform("testOS", "testArch", "testVariant")
tagInfo, err = client.TagMetadata(meta1, opts)
assert.Nil(t, tagInfo)
assert.Nil(t, err)
})
}

func Test_TagMetadata_2(t *testing.T) {
t.Run("ocischema DeserializedManifest invalid digest format", func(t *testing.T) {
meta1 := &ocischema.DeserializedManifest{
Manifest: ocischema.Manifest{
Versioned: manifest.Versioned{
SchemaVersion: 1,
MediaType: "",
},
},
}
ep, err := GetRegistryEndpoint("")
require.NoError(t, err)
client, err := NewClient(ep, "", "")

require.NoError(t, err)
err = client.NewRepository("test/test")
require.NoError(t, err)
_, err = client.TagMetadata(meta1, &options.ManifestOptions{})
require.Error(t, err) // invalid digest format
})
t.Run("schema2 DeserializedManifest invalid digest format", func(t *testing.T) {
meta1 := &schema2.DeserializedManifest{
Manifest: schema2.Manifest{
Versioned: manifest.Versioned{
SchemaVersion: 1,
MediaType: "",
},
Config: distribution.Descriptor{
MediaType: "",
Digest: "sha256:abc",
},
},
}
ep, err := GetRegistryEndpoint("")
require.NoError(t, err)
client, err := NewClient(ep, "", "")

require.NoError(t, err)
err = client.NewRepository("test/test")
require.NoError(t, err)
_, err = client.TagMetadata(meta1, &options.ManifestOptions{})
require.Error(t, err) // invalid digest format
})
t.Run("ocischema DeserializedImageIndex empty index not supported", func(t *testing.T) {
meta1 := &ocischema.DeserializedImageIndex{
ImageIndex: ocischema.ImageIndex{
Versioned: manifest.Versioned{
SchemaVersion: 1,
MediaType: "",
},
Manifests: nil,
Annotations: nil,
},
}
ep, err := GetRegistryEndpoint("")
require.NoError(t, err)
client, err := NewClient(ep, "", "")

require.NoError(t, err)
err = client.NewRepository("test/test")
require.NoError(t, err)
_, err = client.TagMetadata(meta1, &options.ManifestOptions{})
require.Error(t, err) // empty index not supported
})
t.Run("ocischema DeserializedImageIndex empty manifestlist not supported", func(t *testing.T) {
meta1 := &manifestlist.DeserializedManifestList{
ManifestList: manifestlist.ManifestList{
Versioned: manifest.Versioned{
SchemaVersion: 1,
MediaType: "",
},
Manifests: nil,
},
}
ep, err := GetRegistryEndpoint("")
require.NoError(t, err)
client, err := NewClient(ep, "", "")

require.NoError(t, err)
err = client.NewRepository("test/test")
require.NoError(t, err)
_, err = client.TagMetadata(meta1, &options.ManifestOptions{})
require.Error(t, err) // empty manifestlist not supported
})
}

Expand Down Expand Up @@ -495,4 +597,13 @@ func TestPing(t *testing.T) {
assert.ErrorContains(t, err, "does not seem to be a valid v2 Docker Registry API")
})

t.Run("Empty Registry API", func(t *testing.T) {
mockManager := new(mocks.Manager)
ep := &RegistryEndpoint{RegistryAPI: ""}
mockManager.On("AddResponse", mock.Anything).Return(nil)
_, err := ping(mockManager, ep, "")
require.Error(t, err)
assert.ErrorContains(t, err, "unsupported protocol scheme")
})

}