github.com/uber/kraken@v0.1.4/lib/blobrefresh/refresher_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 blobrefresh 15 16 import ( 17 "io/ioutil" 18 "os" 19 "testing" 20 "time" 21 22 "github.com/uber/kraken/core" 23 "github.com/uber/kraken/lib/backend" 24 "github.com/uber/kraken/lib/metainfogen" 25 "github.com/uber/kraken/lib/store" 26 "github.com/uber/kraken/lib/store/metadata" 27 "github.com/uber/kraken/mocks/lib/backend" 28 "github.com/uber/kraken/utils/mockutil" 29 "github.com/uber/kraken/utils/testutil" 30 31 "github.com/golang/mock/gomock" 32 "github.com/stretchr/testify/require" 33 "github.com/uber-go/tally" 34 ) 35 36 const _testPieceLength = 10 37 38 type refresherMocks struct { 39 ctrl *gomock.Controller 40 cas *store.CAStore 41 backends *backend.Manager 42 config Config 43 } 44 45 func newRefresherMocks(t *testing.T) (*refresherMocks, func()) { 46 var cleanup testutil.Cleanup 47 defer cleanup.Recover() 48 49 cas, c := store.CAStoreFixture() 50 cleanup.Add(c) 51 52 ctrl := gomock.NewController(t) 53 cleanup.Add(ctrl.Finish) 54 55 backends := backend.ManagerFixture() 56 57 return &refresherMocks{ctrl, cas, backends, Config{}}, cleanup.Run 58 } 59 60 func (m *refresherMocks) new() *Refresher { 61 return New(m.config, tally.NoopScope, m.cas, m.backends, metainfogen.Fixture(m.cas, _testPieceLength)) 62 } 63 64 func (m *refresherMocks) newClient(namespace string) *mockbackend.MockClient { 65 client := mockbackend.NewMockClient(m.ctrl) 66 m.backends.Register(namespace, client) 67 return client 68 } 69 70 func TestRefresh(t *testing.T) { 71 require := require.New(t) 72 73 mocks, cleanup := newRefresherMocks(t) 74 defer cleanup() 75 76 refresher := mocks.new() 77 78 namespace := core.TagFixture() 79 client := mocks.newClient(namespace) 80 81 blob := core.SizedBlobFixture(100, uint64(_testPieceLength)) 82 83 client.EXPECT().Stat(namespace, blob.Digest.Hex()).Return(core.NewBlobInfo(int64(len(blob.Content))), nil) 84 client.EXPECT().Download(namespace, blob.Digest.Hex(), mockutil.MatchWriter(blob.Content)).Return(nil) 85 86 require.NoError(refresher.Refresh(namespace, blob.Digest)) 87 88 require.NoError(testutil.PollUntilTrue(5*time.Second, func() bool { 89 _, err := mocks.cas.GetCacheFileStat(blob.Digest.Hex()) 90 return !os.IsNotExist(err) 91 })) 92 93 f, err := mocks.cas.GetCacheFileReader(blob.Digest.Hex()) 94 require.NoError(err) 95 result, err := ioutil.ReadAll(f) 96 require.Equal(string(blob.Content), string(result)) 97 98 var tm metadata.TorrentMeta 99 require.NoError(mocks.cas.GetCacheFileMetadata(blob.Digest.Hex(), &tm)) 100 require.Equal(blob.MetaInfo, tm.MetaInfo) 101 } 102 103 func TestRefreshSizeLimitError(t *testing.T) { 104 require := require.New(t) 105 106 mocks, cleanup := newRefresherMocks(t) 107 defer cleanup() 108 109 mocks.config.SizeLimit = 99 110 111 refresher := mocks.new() 112 113 namespace := core.TagFixture() 114 client := mocks.newClient(namespace) 115 116 blob := core.SizedBlobFixture(100, uint64(_testPieceLength)) 117 118 client.EXPECT().Stat(namespace, blob.Digest.Hex()).Return(core.NewBlobInfo(int64(len(blob.Content))), nil) 119 120 require.Error(refresher.Refresh(namespace, blob.Digest)) 121 } 122 123 func TestRefreshSizeLimitWithValidSize(t *testing.T) { 124 require := require.New(t) 125 126 mocks, cleanup := newRefresherMocks(t) 127 defer cleanup() 128 129 mocks.config.SizeLimit = 100 130 131 refresher := mocks.new() 132 133 namespace := core.TagFixture() 134 client := mocks.newClient(namespace) 135 136 blob := core.SizedBlobFixture(100, uint64(_testPieceLength)) 137 138 client.EXPECT().Stat(namespace, blob.Digest.Hex()).Return(core.NewBlobInfo(int64(len(blob.Content))), nil) 139 client.EXPECT().Download(namespace, blob.Digest.Hex(), mockutil.MatchWriter(blob.Content)).Return(nil) 140 141 require.NoError(refresher.Refresh(namespace, blob.Digest)) 142 143 require.NoError(testutil.PollUntilTrue(5*time.Second, func() bool { 144 _, err := mocks.cas.GetCacheFileStat(blob.Digest.Hex()) 145 return !os.IsNotExist(err) 146 })) 147 }