github.com/uber/kraken@v0.1.4/lib/backend/registrybackend/blobclient_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  	"testing"
    22  
    23  	"github.com/pressly/chi"
    24  	"github.com/stretchr/testify/require"
    25  	"github.com/uber/kraken/core"
    26  	"github.com/uber/kraken/lib/backend/backenderrors"
    27  	"github.com/uber/kraken/utils/memsize"
    28  	"github.com/uber/kraken/utils/randutil"
    29  	"github.com/uber/kraken/utils/testutil"
    30  )
    31  
    32  func TestClientFactory(t *testing.T) {
    33  	require := require.New(t)
    34  
    35  	config := Config{}
    36  	f := blobClientFactory{}
    37  	_, err := f.Create(config, nil)
    38  	require.NoError(err)
    39  }
    40  
    41  func TestBlobDownloadBlobSuccess(t *testing.T) {
    42  	require := require.New(t)
    43  
    44  	blob := randutil.Blob(32 * memsize.KB)
    45  	namespace := core.NamespaceFixture()
    46  
    47  	r := chi.NewRouter()
    48  	r.Get(fmt.Sprintf("/v2/%s/blobs/{blob}", namespace), func(w http.ResponseWriter, req *http.Request) {
    49  		_, err := io.Copy(w, bytes.NewReader(blob))
    50  		require.NoError(err)
    51  	})
    52  	r.Head(fmt.Sprintf("/v2/%s/blobs/{blob}", namespace), func(w http.ResponseWriter, req *http.Request) {
    53  		w.Header().Set("Content-Length", fmt.Sprintf("%d", len(blob)))
    54  	})
    55  	addr, stop := testutil.StartServer(r)
    56  	defer stop()
    57  
    58  	config := newTestConfig(addr)
    59  	client, err := NewBlobClient(config)
    60  	require.NoError(err)
    61  
    62  	info, err := client.Stat(namespace, "data")
    63  	require.NoError(err)
    64  	require.Equal(int64(len(blob)), info.Size)
    65  
    66  	var b bytes.Buffer
    67  	require.NoError(client.Download(namespace, "data", &b))
    68  	require.Equal(blob, b.Bytes())
    69  }
    70  
    71  func TestBlobDownloadManifestSuccess(t *testing.T) {
    72  	require := require.New(t)
    73  
    74  	blob := randutil.Blob(32 * memsize.KB)
    75  	namespace := core.NamespaceFixture()
    76  
    77  	r := chi.NewRouter()
    78  	r.Get(fmt.Sprintf("/v2/%s/manifests/{blob}", namespace), func(w http.ResponseWriter, req *http.Request) {
    79  		_, err := io.Copy(w, bytes.NewReader(blob))
    80  		require.NoError(err)
    81  	})
    82  	r.Head(fmt.Sprintf("/v2/%s/manifests/{blob}", namespace), func(w http.ResponseWriter, req *http.Request) {
    83  		w.Header().Set("Content-Length", fmt.Sprintf("%d", len(blob)))
    84  	})
    85  	addr, stop := testutil.StartServer(r)
    86  	defer stop()
    87  
    88  	config := newTestConfig(addr)
    89  	client, err := NewBlobClient(config)
    90  	require.NoError(err)
    91  
    92  	info, err := client.Stat(namespace, "data")
    93  	require.NoError(err)
    94  	require.Equal(int64(len(blob)), info.Size)
    95  
    96  	var b bytes.Buffer
    97  	require.NoError(client.Download(namespace, "data", &b))
    98  	require.Equal(blob, b.Bytes())
    99  }
   100  
   101  func TestBlobDownloadFileNotFound(t *testing.T) {
   102  	require := require.New(t)
   103  
   104  	namespace := core.NamespaceFixture()
   105  
   106  	r := chi.NewRouter()
   107  	r.Get(fmt.Sprintf("/v2/%s/blobs/{blob}", namespace), func(w http.ResponseWriter, req *http.Request) {
   108  		w.WriteHeader(http.StatusNotFound)
   109  		w.Write([]byte("file not found"))
   110  	})
   111  	r.Head(fmt.Sprintf("/v2/%s/blobs/{blob}", namespace), func(w http.ResponseWriter, req *http.Request) {
   112  		w.WriteHeader(http.StatusNotFound)
   113  		w.Write([]byte("file not found"))
   114  	})
   115  	addr, stop := testutil.StartServer(r)
   116  	defer stop()
   117  
   118  	config := newTestConfig(addr)
   119  	client, err := NewBlobClient(config)
   120  	require.NoError(err)
   121  
   122  	_, err = client.Stat(namespace, "data")
   123  	require.Equal(backenderrors.ErrBlobNotFound, err)
   124  
   125  	var b bytes.Buffer
   126  	require.Equal(backenderrors.ErrBlobNotFound, client.Download(namespace, "data", &b))
   127  }