add tests for oci client
Signed-off-by: Meredith Lancaster <malancas@github.com>
This commit is contained in:
parent
f055517baa
commit
38adc439cc
2 changed files with 84 additions and 51 deletions
|
|
@ -6,7 +6,7 @@ import (
|
|||
|
||||
"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"
|
||||
"github.com/google/go-containerregistry/pkg/v1/remote"
|
||||
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
|
||||
)
|
||||
|
|
@ -62,53 +62,3 @@ func NewLiveClient() *LiveClient {
|
|||
Get: remote.Get,
|
||||
}
|
||||
}
|
||||
|
||||
// func NewMockClient() *Client {
|
||||
// return &Client{
|
||||
// ParseReference: func(string, ...name.Option) (name.Reference, error) {
|
||||
// return name.Tag{}, nil
|
||||
// },
|
||||
// Get: func(name.Reference, ...remote.Option) (*remote.Descriptor, error) {
|
||||
// d := remote.Descriptor{}
|
||||
// d.Digest = v1.Hash{
|
||||
// Hex: "1234567890abcdef",
|
||||
// Algorithm: "sha256",
|
||||
// }
|
||||
|
||||
// return &d, nil
|
||||
// },
|
||||
// }
|
||||
// }
|
||||
|
||||
// func NewReferenceFailClient() *Client {
|
||||
// return &Client{
|
||||
// ParseReference: func(string, ...name.Option) (name.Reference, error) {
|
||||
// return nil, fmt.Errorf("failed to parse reference")
|
||||
// },
|
||||
// Get: func(name.Reference, ...remote.Option) (*remote.Descriptor, error) {
|
||||
// return nil, nil
|
||||
// },
|
||||
// }
|
||||
// }
|
||||
|
||||
// func NewAuthFailClient() *Client {
|
||||
// return &Client{
|
||||
// ParseReference: func(string, ...name.Option) (name.Reference, error) {
|
||||
// return name.Tag{}, nil
|
||||
// },
|
||||
// Get: func(name.Reference, ...remote.Option) (*remote.Descriptor, error) {
|
||||
// return nil, &transport.Error{Errors: []transport.Diagnostic{{Code: transport.UnauthorizedErrorCode}}}
|
||||
// },
|
||||
// }
|
||||
// }
|
||||
|
||||
// func NewDeniedClient() *Client {
|
||||
// return &Client{
|
||||
// ParseReference: func(string, ...name.Option) (name.Reference, error) {
|
||||
// return name.Tag{}, nil
|
||||
// },
|
||||
// Get: func(name.Reference, ...remote.Option) (*remote.Descriptor, error) {
|
||||
// return nil, &transport.Error{Errors: []transport.Diagnostic{{Code: transport.DeniedErrorCode}}}
|
||||
// },
|
||||
// }
|
||||
// }
|
||||
|
|
|
|||
83
pkg/cmd/attestation/artifact/oci/oci_test.go
Normal file
83
pkg/cmd/attestation/artifact/oci/oci_test.go
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
package oci
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-containerregistry/pkg/name"
|
||||
"github.com/google/go-containerregistry/pkg/v1"
|
||||
"github.com/google/go-containerregistry/pkg/v1/remote"
|
||||
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestGetImageDigest_Success(t *testing.T) {
|
||||
expectedDigest := v1.Hash{
|
||||
Hex: "1234567890abcdef",
|
||||
Algorithm: "sha256",
|
||||
}
|
||||
|
||||
c := LiveClient{
|
||||
ParseReference: func(string, ...name.Option) (name.Reference, error) {
|
||||
return name.Tag{}, nil
|
||||
},
|
||||
Get: func(name.Reference, ...remote.Option) (*remote.Descriptor, error) {
|
||||
d := remote.Descriptor{}
|
||||
d.Digest = expectedDigest
|
||||
|
||||
return &d, nil
|
||||
},
|
||||
}
|
||||
|
||||
digest, err := c.GetImageDigest("test")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, &expectedDigest, digest)
|
||||
}
|
||||
|
||||
func TestGetImageDigest_ReferenceFail(t *testing.T) {
|
||||
c := LiveClient{
|
||||
ParseReference: func(string, ...name.Option) (name.Reference, error) {
|
||||
return nil, fmt.Errorf("failed to parse reference")
|
||||
},
|
||||
Get: func(name.Reference, ...remote.Option) (*remote.Descriptor, error) {
|
||||
return nil, nil
|
||||
},
|
||||
}
|
||||
|
||||
digest, err := c.GetImageDigest("test")
|
||||
require.Error(t, err)
|
||||
require.Nil(t, digest)
|
||||
}
|
||||
|
||||
func TestGetImageDigest_AuthFail(t *testing.T) {
|
||||
c := LiveClient{
|
||||
ParseReference: func(string, ...name.Option) (name.Reference, error) {
|
||||
return name.Tag{}, nil
|
||||
},
|
||||
Get: func(name.Reference, ...remote.Option) (*remote.Descriptor, error) {
|
||||
return nil, &transport.Error{Errors: []transport.Diagnostic{{Code: transport.UnauthorizedErrorCode}}}
|
||||
},
|
||||
}
|
||||
|
||||
digest, err := c.GetImageDigest("test")
|
||||
require.Error(t, err)
|
||||
require.ErrorIs(t, err, ErrRegistryAuthz)
|
||||
require.Nil(t, digest)
|
||||
}
|
||||
|
||||
func TestGetImageDigest_Denied(t *testing.T) {
|
||||
c := LiveClient{
|
||||
ParseReference: func(string, ...name.Option) (name.Reference, error) {
|
||||
return name.Tag{}, nil
|
||||
},
|
||||
Get: func(name.Reference, ...remote.Option) (*remote.Descriptor, error) {
|
||||
return nil, &transport.Error{Errors: []transport.Diagnostic{{Code: transport.DeniedErrorCode}}}
|
||||
},
|
||||
}
|
||||
|
||||
digest, err := c.GetImageDigest("test")
|
||||
require.Error(t, err)
|
||||
require.ErrorIs(t, err, ErrDenied)
|
||||
require.Nil(t, digest)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue