get.porter.sh/porter@v1.3.0/pkg/cnab/cnab-to-oci/registry_test.go (about)

     1  package cnabtooci
     2  
     3  import (
     4  	"net/http"
     5  	"testing"
     6  
     7  	"get.porter.sh/porter/pkg/cnab"
     8  	"github.com/docker/docker/api/types/image"
     9  	"github.com/google/go-containerregistry/pkg/v1/remote/transport"
    10  	"github.com/opencontainers/go-digest"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestImageSummary(t *testing.T) {
    16  	type expectedOutput struct {
    17  		imageRef   string
    18  		digest     string
    19  		hasInitErr bool
    20  	}
    21  
    22  	testcases := []struct {
    23  		name         string
    24  		imgRef       string
    25  		imageSummary image.InspectResponse
    26  		expected     expectedOutput
    27  		expectedErr  string
    28  	}{
    29  		{
    30  			name:         "successful initialization",
    31  			imgRef:       "test/image:latest",
    32  			imageSummary: image.InspectResponse{ID: "test", RepoDigests: []string{"test/image@sha256:6b5a28ccbb76f12ce771a23757880c6083234255c5ba191fca1c5db1f71c1687"}},
    33  			expected:     expectedOutput{imageRef: "test/image:latest", digest: "sha256:6b5a28ccbb76f12ce771a23757880c6083234255c5ba191fca1c5db1f71c1687"},
    34  			expectedErr:  "",
    35  		},
    36  		{
    37  			name:         "empty repo digests",
    38  			imgRef:       "test/image:latest",
    39  			imageSummary: image.InspectResponse{ID: "test", RepoDigests: []string{}},
    40  			expectedErr:  "failed to get digest",
    41  			expected: expectedOutput{
    42  				imageRef: "test/image:latest",
    43  			},
    44  		},
    45  		{
    46  			name:         "failed to find valid digest",
    47  			imgRef:       "test/image:latest",
    48  			imageSummary: image.InspectResponse{ID: "test", RepoDigests: []string{"test/image-another-repo@sha256:6b5a28ccbb76f12ce771a23757880c6083234255c5ba191fca1c5db1f71c1687"}},
    49  			expectedErr:  "cannot find image digest for desired repo",
    50  			expected: expectedOutput{
    51  				imageRef: "test/image:latest",
    52  			},
    53  		},
    54  	}
    55  
    56  	for _, tt := range testcases {
    57  		tt := tt
    58  		t.Run(tt.name, func(t *testing.T) {
    59  			sum, err := NewImageSummaryFromInspect(cnab.MustParseOCIReference(tt.imgRef), tt.imageSummary)
    60  			if tt.expected.hasInitErr {
    61  				require.ErrorContains(t, err, tt.expectedErr)
    62  				return
    63  			}
    64  			require.Equal(t, sum.Reference.String(), tt.expected.imageRef)
    65  			digest, err := sum.GetRepositoryDigest()
    66  			if tt.expected.digest == "" {
    67  				require.ErrorContains(t, err, tt.expectedErr)
    68  				return
    69  			}
    70  			require.Equal(t, tt.expected.digest, digest.String())
    71  		})
    72  	}
    73  }
    74  
    75  func TestNewImageSummaryFromDescriptor(t *testing.T) {
    76  	ref := cnab.MustParseOCIReference("localhost:5000/whalesayd:latest")
    77  	origRepoDigest := digest.Digest("sha256:499f71eec2e3bd78f26c268bbf5b2a65f73b96216fac4a89b86b5ebf115527b6")
    78  
    79  	s, err := NewImageSummaryFromDigest(ref, origRepoDigest)
    80  	require.NoError(t, err, "NewImageSummaryFromDigest failed")
    81  
    82  	// Locate the repository digest associated with the reference and validate that it matches what we input
    83  	repoDigest, err := s.GetRepositoryDigest()
    84  	require.NoError(t, err, "failed to get repository digest for image summary")
    85  	assert.Equal(t, origRepoDigest.String(), repoDigest.String())
    86  }
    87  
    88  func TestAsNotFoundError(t *testing.T) {
    89  	ref := cnab.MustParseOCIReference("example.com/mybuns:v1.2.3")
    90  	t.Run("404", func(t *testing.T) {
    91  		srcErr := &transport.Error{
    92  			StatusCode: http.StatusNotFound,
    93  		}
    94  		result := asNotFoundError(srcErr, ref)
    95  		require.NotNil(t, result)
    96  		require.Equal(t, ErrNotFound{Reference: ref}, result)
    97  	})
    98  	t.Run("401", func(t *testing.T) {
    99  		srcErr := &transport.Error{
   100  			StatusCode: http.StatusUnauthorized,
   101  		}
   102  		result := asNotFoundError(srcErr, ref)
   103  		require.Nil(t, result)
   104  	})
   105  }