github.com/uber/kraken@v0.1.4/lib/backend/registrybackend/tagclient_test.go (about) 1 // Copyright (c) 2016-2019 Uber Technologies, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package registrybackend 15 16 import ( 17 "bytes" 18 "fmt" 19 "io" 20 "net/http" 21 "strings" 22 "testing" 23 24 "github.com/pressly/chi" 25 "github.com/stretchr/testify/require" 26 "github.com/uber/kraken/core" 27 "github.com/uber/kraken/lib/backend/backenderrors" 28 "github.com/uber/kraken/utils/dockerutil" 29 "github.com/uber/kraken/utils/testutil" 30 ) 31 32 func TestTagDownloadSuccess(t *testing.T) { 33 require := require.New(t) 34 35 imageConfig := core.NewBlobFixture() 36 layer1 := core.NewBlobFixture() 37 layer2 := core.NewBlobFixture() 38 digest, manifest := dockerutil.ManifestFixture( 39 imageConfig.Digest, layer1.Digest, layer2.Digest) 40 41 tag := core.TagFixture() 42 namespace := strings.Split(tag, ":")[0] 43 44 r := chi.NewRouter() 45 r.Get(fmt.Sprintf("/v2/%s/manifests/{tag}", namespace), func(w http.ResponseWriter, req *http.Request) { 46 w.Header().Set("Content-Length", fmt.Sprintf("%d", len(manifest))) 47 w.Header().Set("Docker-Content-Digest", digest.String()) 48 _, err := io.Copy(w, bytes.NewReader(manifest)) 49 require.NoError(err) 50 }) 51 r.Head(fmt.Sprintf("/v2/%s/manifests/{tag}", namespace), func(w http.ResponseWriter, req *http.Request) { 52 w.Header().Set("Content-Length", fmt.Sprintf("%d", len(manifest))) 53 w.Header().Set("Docker-Content-Digest", digest.String()) 54 _, err := io.Copy(w, bytes.NewReader(manifest)) 55 require.NoError(err) 56 }) 57 addr, stop := testutil.StartServer(r) 58 defer stop() 59 60 config := newTestConfig(addr) 61 client, err := NewTagClient(config) 62 require.NoError(err) 63 64 info, err := client.Stat(tag, tag) 65 require.NoError(err) 66 require.Equal(int64(len(manifest)), info.Size) 67 68 var b bytes.Buffer 69 require.NoError(client.Download(tag, tag, &b)) 70 require.Equal(digest.String(), string(b.Bytes())) 71 } 72 73 func TestTagDownloadFileNotFound(t *testing.T) { 74 require := require.New(t) 75 76 tag := core.TagFixture() 77 namespace := strings.Split(tag, ":")[0] 78 79 r := chi.NewRouter() 80 r.Get(fmt.Sprintf("/v2/%s/manifests/{tag}", namespace), func(w http.ResponseWriter, req *http.Request) { 81 w.WriteHeader(http.StatusNotFound) 82 w.Write([]byte("file not found")) 83 }) 84 r.Head(fmt.Sprintf("/v2/%s/manifests/{tag}", namespace), func(w http.ResponseWriter, req *http.Request) { 85 w.WriteHeader(http.StatusNotFound) 86 }) 87 addr, stop := testutil.StartServer(r) 88 defer stop() 89 90 config := newTestConfig(addr) 91 client, err := NewTagClient(config) 92 require.NoError(err) 93 94 _, err = client.Stat(tag, tag) 95 require.Equal(backenderrors.ErrBlobNotFound, err) 96 97 var b bytes.Buffer 98 require.Equal(backenderrors.ErrBlobNotFound, client.Download(tag, tag, &b)) 99 }