github.com/uber/kraken@v0.1.4/tracker/trackerserver/metainfo_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 trackerserver
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/uber/kraken/core"
    20  	"github.com/uber/kraken/lib/hashring"
    21  	"github.com/uber/kraken/lib/hostlist"
    22  	"github.com/uber/kraken/tracker/metainfoclient"
    23  	"github.com/uber/kraken/utils/httputil"
    24  	"github.com/uber/kraken/utils/testutil"
    25  
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func newMetaInfoClient(addr string) metainfoclient.Client {
    30  	return metainfoclient.New(hashring.NoopPassiveRing(hostlist.Fixture(addr)), nil)
    31  }
    32  
    33  func TestGetMetaInfoHandlerFetchesFromOrigin(t *testing.T) {
    34  	require := require.New(t)
    35  
    36  	mocks, cleanup := newServerMocks(t, Config{})
    37  	defer cleanup()
    38  
    39  	addr, stop := testutil.StartServer(mocks.handler())
    40  	defer stop()
    41  
    42  	namespace := core.TagFixture()
    43  	mi := core.MetaInfoFixture()
    44  
    45  	mocks.originCluster.EXPECT().GetMetaInfo(namespace, mi.Digest()).Return(mi, nil)
    46  
    47  	client := newMetaInfoClient(addr)
    48  
    49  	result, err := client.Download(namespace, mi.Digest())
    50  	require.NoError(err)
    51  	require.Equal(mi, result)
    52  }
    53  
    54  func TestGetMetaInfoHandlerPropagatesOriginError(t *testing.T) {
    55  	require := require.New(t)
    56  
    57  	mocks, cleanup := newServerMocks(t, Config{})
    58  	defer cleanup()
    59  
    60  	addr, stop := testutil.StartServer(mocks.handler())
    61  	defer stop()
    62  
    63  	namespace := core.TagFixture()
    64  	mi := core.MetaInfoFixture()
    65  
    66  	mocks.originCluster.EXPECT().GetMetaInfo(
    67  		namespace, mi.Digest()).Return(nil, httputil.StatusError{Status: 599}).MinTimes(1)
    68  
    69  	client := newMetaInfoClient(addr)
    70  
    71  	_, err := client.Download(namespace, mi.Digest())
    72  	require.Error(err)
    73  	require.True(httputil.IsStatus(err, 599))
    74  }