github.com/docker/app@v0.9.1-beta3.0.20210611140623-a48f773ab002/internal/commands/image/tag_test.go (about) 1 package image 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/docker/app/internal/image" 8 9 "gotest.tools/assert" 10 11 "github.com/docker/distribution/reference" 12 ) 13 14 func parseRefOrDie(t *testing.T, ref string) reference.Named { 15 t.Helper() 16 named, err := reference.ParseNormalizedNamed(ref) 17 assert.NilError(t, err) 18 return named 19 } 20 21 type imageStoreStub struct { 22 ReadBundle *image.AppImage 23 ReadError error 24 StoredBundle string 25 StoredError error 26 StoredID reference.Digested 27 LookUpRef reference.Reference 28 LookUpError error 29 } 30 31 func (b *imageStoreStub) Store(img *image.AppImage, ref reference.Reference) (reference.Digested, error) { 32 defer func() { 33 b.StoredError = nil 34 }() 35 36 b.StoredBundle = ref.String() 37 38 return b.StoredID, b.StoredError 39 } 40 41 func (b *imageStoreStub) Read(ref reference.Reference) (*image.AppImage, error) { 42 defer func() { 43 b.ReadBundle = nil 44 b.ReadError = nil 45 }() 46 return b.ReadBundle, b.ReadError 47 } 48 49 func (b *imageStoreStub) List() ([]reference.Reference, error) { 50 return nil, nil 51 } 52 53 func (b *imageStoreStub) Remove(ref reference.Reference, force bool) error { 54 return nil 55 } 56 57 func (b *imageStoreStub) LookUp(refOrID string) (reference.Reference, error) { 58 defer func() { 59 b.LookUpRef = nil 60 b.LookUpError = nil 61 }() 62 return b.LookUpRef, b.LookUpError 63 } 64 65 var mockedImageStore = &imageStoreStub{} 66 67 func TestInvalidSourceReference(t *testing.T) { 68 // given a bad source image reference 69 const badRef = "b@d reference" 70 // and given bundle store will return an error on LookUp 71 mockedImageStore.LookUpError = fmt.Errorf("error from imageStore.LookUp") 72 73 err := runTag(mockedImageStore, badRef, "") 74 75 assert.Assert(t, err != nil) 76 } 77 78 func TestUnexistingSource(t *testing.T) { 79 // given a well formatted source image reference 80 const unexistingRef = "unexisting" 81 // and given bundle store will return an error on Read 82 mockedImageStore.ReadError = fmt.Errorf("error from imageStore.Read") 83 84 err := runTag(mockedImageStore, unexistingRef, "dest") 85 86 assert.Assert(t, err != nil) 87 } 88 89 func TestInvalidDestinationReference(t *testing.T) { 90 // given a reference and a bundle is returned by imageStore.LookUp and imageStore.Read 91 mockedImageStore.LookUpRef = parseRefOrDie(t, "ref") 92 mockedImageStore.ReadBundle = &image.AppImage{} 93 // and given a bad destination reference 94 const badRef = "b@d reference" 95 96 err := runTag(mockedImageStore, "ref", badRef) 97 98 assert.ErrorContains(t, err, "invalid reference format") 99 } 100 101 func TestBundleNotStored(t *testing.T) { 102 // given a reference and a bundle is returned by imageStore.LookUp and imageStore.Read 103 mockedImageStore.LookUpRef = parseRefOrDie(t, "src-app") 104 mockedImageStore.ReadBundle = &image.AppImage{} 105 // and given imageStore.Store will return an error 106 mockedImageStore.StoredError = fmt.Errorf("error from imageStore.Store") 107 108 err := runTag(mockedImageStore, "src-app", "dest-app") 109 110 assert.Assert(t, err != nil) 111 } 112 113 func TestSuccessfulyTag(t *testing.T) { 114 // given a reference and a bundle is returned by imageStore.LookUp and imageStore.Read 115 mockedImageStore.LookUpRef = parseRefOrDie(t, "src-app") 116 mockedImageStore.ReadBundle = &image.AppImage{} 117 // and given valid source and output references 118 const ( 119 srcRef = "src-app" 120 destRef = "dest-app" 121 normalizedDestRef = "docker.io/library/dest-app:latest" 122 ) 123 124 err := runTag(mockedImageStore, srcRef, destRef) 125 126 assert.NilError(t, err) 127 assert.Equal(t, mockedImageStore.StoredBundle, normalizedDestRef) 128 }