1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
16 "git.arvados.org/arvados.git/sdk/go/arvadosclient"
17 "git.arvados.org/arvados.git/sdk/go/arvadostest"
18 "git.arvados.org/arvados.git/sdk/go/keepclient"
23 var kcSrc, kcDst *keepclient.KeepClient
24 var srcKeepServicesJSON, dstKeepServicesJSON, blobSigningKey string
25 var blobSignatureTTL = time.Duration(2*7*24) * time.Hour
29 srcKeepServicesJSON = ""
30 dstKeepServicesJSON = ""
35 // Gocheck boilerplate
36 func Test(t *testing.T) {
40 var _ = Suite(&ServerRequiredSuite{})
41 var _ = Suite(&ServerNotRequiredSuite{})
42 var _ = Suite(&DoMainTestSuite{})
44 type ServerRequiredSuite struct{}
46 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
47 arvadostest.ResetEnv()
50 func (s *ServerRequiredSuite) SetUpTest(c *C) {
54 func (s *ServerRequiredSuite) TearDownTest(c *C) {
55 arvadostest.StopKeep(3)
58 func (s *ServerNotRequiredSuite) SetUpTest(c *C) {
62 type ServerNotRequiredSuite struct{}
64 type DoMainTestSuite struct {
68 func (s *DoMainTestSuite) SetUpTest(c *C) {
69 s.initialArgs = os.Args
70 os.Args = []string{"keep-rsync"}
74 func (s *DoMainTestSuite) TearDownTest(c *C) {
75 os.Args = s.initialArgs
78 var testKeepServicesJSON = "{ \"kind\":\"arvados#keepServiceList\", \"etag\":\"\", \"self_link\":\"\", \"offset\":null, \"limit\":null, \"items\":[ { \"href\":\"/keep_services/zzzzz-bi6l4-123456789012340\", \"kind\":\"arvados#keepService\", \"etag\":\"641234567890enhj7hzx432e5\", \"uuid\":\"zzzzz-bi6l4-123456789012340\", \"owner_uuid\":\"zzzzz-tpzed-123456789012345\", \"service_host\":\"keep0.zzzzz.arvadosapi.com\", \"service_port\":25107, \"service_ssl_flag\":false, \"service_type\":\"disk\", \"read_only\":false }, { \"href\":\"/keep_services/zzzzz-bi6l4-123456789012341\", \"kind\":\"arvados#keepService\", \"etag\":\"641234567890enhj7hzx432e5\", \"uuid\":\"zzzzz-bi6l4-123456789012341\", \"owner_uuid\":\"zzzzz-tpzed-123456789012345\", \"service_host\":\"keep0.zzzzz.arvadosapi.com\", \"service_port\":25108, \"service_ssl_flag\":false, \"service_type\":\"disk\", \"read_only\":false } ], \"items_available\":2 }"
80 // Testing keep-rsync needs two sets of keep services: src and dst.
81 // The test setup hence creates 3 servers instead of the default 2,
82 // and uses the first 2 as src and the 3rd as dst keep servers.
83 func setupRsync(c *C, enforcePermissions bool, replications int) {
85 var srcConfig apiConfig
86 srcConfig.APIHost = os.Getenv("ARVADOS_API_HOST")
87 srcConfig.APIToken = arvadostest.SystemRootToken
88 srcConfig.APIHostInsecure = arvadosclient.StringBool(os.Getenv("ARVADOS_API_HOST_INSECURE"))
91 var dstConfig apiConfig
92 dstConfig.APIHost = os.Getenv("ARVADOS_API_HOST")
93 dstConfig.APIToken = arvadostest.SystemRootToken
94 dstConfig.APIHostInsecure = arvadosclient.StringBool(os.Getenv("ARVADOS_API_HOST_INSECURE"))
96 if enforcePermissions {
97 blobSigningKey = arvadostest.BlobSigningKey
100 // Start Keep servers
101 arvadostest.StartKeep(3, enforcePermissions)
102 keepclient.RefreshServiceDiscovery()
106 kcSrc, _, err = setupKeepClient(srcConfig, srcKeepServicesJSON, false, 0, blobSignatureTTL)
109 kcDst, _, err = setupKeepClient(dstConfig, dstKeepServicesJSON, true, replications, 0)
112 srcRoots := map[string]string{}
113 dstRoots := map[string]string{}
114 for uuid, root := range kcSrc.LocalRoots() {
115 if strings.HasSuffix(uuid, "02") {
116 dstRoots[uuid] = root
118 srcRoots[uuid] = root
121 if srcKeepServicesJSON == "" {
122 kcSrc.SetServiceRoots(srcRoots, srcRoots, srcRoots)
124 if dstKeepServicesJSON == "" {
125 kcDst.SetServiceRoots(dstRoots, dstRoots, dstRoots)
128 if replications == 0 {
129 // Must have got default replications value of 2 from dst discovery document
130 c.Assert(kcDst.Want_replicas, Equals, 2)
132 // Since replications value is provided, it is used
133 c.Assert(kcDst.Want_replicas, Equals, replications)
137 func (s *ServerRequiredSuite) TestRsyncPutInOne_GetFromOtherShouldFail(c *C) {
138 setupRsync(c, false, 1)
140 // Put a block in src and verify that it is not found in dst
141 testNoCrosstalk(c, "test-data-1", kcSrc, kcDst)
143 // Put a block in dst and verify that it is not found in src
144 testNoCrosstalk(c, "test-data-2", kcDst, kcSrc)
147 func (s *ServerRequiredSuite) TestRsyncWithBlobSigning_PutInOne_GetFromOtherShouldFail(c *C) {
148 setupRsync(c, true, 1)
150 // Put a block in src and verify that it is not found in dst
151 testNoCrosstalk(c, "test-data-1", kcSrc, kcDst)
153 // Put a block in dst and verify that it is not found in src
154 testNoCrosstalk(c, "test-data-2", kcDst, kcSrc)
157 // Do a Put in the first and Get from the second,
158 // which should raise block not found error.
159 func testNoCrosstalk(c *C, testData string, kc1, kc2 *keepclient.KeepClient) {
160 // Put a block using kc1
161 locator, _, err := kc1.PutB([]byte(testData))
162 c.Assert(err, Equals, nil)
164 locator = strings.Split(locator, "+")[0]
165 _, _, _, err = kc2.Get(keepclient.SignLocator(locator, kc2.Arvados.ApiToken, time.Now().AddDate(0, 0, 1), blobSignatureTTL, []byte(blobSigningKey)))
166 c.Assert(err, NotNil)
167 c.Check(err.Error(), Equals, "Block not found")
170 // Test keep-rsync initialization, with srcKeepServicesJSON
171 func (s *ServerRequiredSuite) TestRsyncInitializeWithKeepServicesJSON(c *C) {
172 srcKeepServicesJSON = testKeepServicesJSON
174 setupRsync(c, false, 1)
176 localRoots := kcSrc.LocalRoots()
177 c.Check(localRoots, NotNil)
178 c.Check(localRoots["zzzzz-bi6l4-123456789012340"], Not(Equals), "")
179 c.Check(localRoots["zzzzz-bi6l4-123456789012341"], Not(Equals), "")
182 // Test keep-rsync initialization with default replications count
183 func (s *ServerRequiredSuite) TestInitializeRsyncDefaultReplicationsCount(c *C) {
184 setupRsync(c, false, 0)
187 // Test keep-rsync initialization with replications count argument
188 func (s *ServerRequiredSuite) TestInitializeRsyncReplicationsCount(c *C) {
189 setupRsync(c, false, 3)
192 // Put some blocks in Src and some more in Dst
193 // And copy missing blocks from Src to Dst
194 func (s *ServerRequiredSuite) TestKeepRsync(c *C) {
195 testKeepRsync(c, false, "")
198 // Put some blocks in Src and some more in Dst with blob signing enabled.
199 // And copy missing blocks from Src to Dst
200 func (s *ServerRequiredSuite) TestKeepRsync_WithBlobSigning(c *C) {
201 testKeepRsync(c, true, "")
204 // Put some blocks in Src and some more in Dst
205 // Use prefix while doing rsync
206 // And copy missing blocks from Src to Dst
207 func (s *ServerRequiredSuite) TestKeepRsync_WithPrefix(c *C) {
208 data := []byte("test-data-4")
209 hash := fmt.Sprintf("%x", md5.Sum(data))
211 testKeepRsync(c, false, hash[0:3])
212 c.Check(len(dstIndex) > len(dstLocators), Equals, true)
215 // Put some blocks in Src and some more in Dst
216 // Use prefix not in src while doing rsync
217 // And copy missing blocks from Src to Dst
218 func (s *ServerRequiredSuite) TestKeepRsync_WithNoSuchPrefixInSrc(c *C) {
219 testKeepRsync(c, false, "999")
220 c.Check(len(dstIndex), Equals, len(dstLocators))
223 // Put 5 blocks in src. Put 2 of those blocks in dst
224 // Hence there are 3 additional blocks in src
225 // Also, put 2 extra blocks in dst; they are hence only in dst
226 // Run rsync and verify that those 7 blocks are now available in dst
227 func testKeepRsync(c *C, enforcePermissions bool, prefix string) {
228 setupRsync(c, enforcePermissions, 1)
231 setupTestData(c, prefix)
233 err := performKeepRsync(kcSrc, kcDst, blobSignatureTTL, blobSigningKey, prefix)
236 // Now GetIndex from dst and verify that all 5 from src and the 2 extra blocks are found
237 dstIndex, err = getUniqueLocators(kcDst, "")
240 for _, locator := range srcLocatorsMatchingPrefix {
241 _, ok := dstIndex[locator]
242 c.Assert(ok, Equals, true)
245 for _, locator := range extraDstLocators {
246 _, ok := dstIndex[locator]
247 c.Assert(ok, Equals, true)
251 // all blocks from src and the two extra blocks
252 c.Assert(len(dstIndex), Equals, len(srcLocators)+len(extraDstLocators))
254 // 1 matching prefix and copied over, 2 that were initially copied into dst along with src, and the 2 extra blocks
255 c.Assert(len(dstIndex), Equals, len(srcLocatorsMatchingPrefix)+len(extraDstLocators)+2)
259 // Setup test data in src and dst.
260 var srcLocators, srcLocatorsMatchingPrefix, dstLocators, extraDstLocators []string
261 var dstIndex map[string]bool
263 func setupTestData(c *C, indexPrefix string) {
264 srcLocators = []string{}
265 srcLocatorsMatchingPrefix = []string{}
266 dstLocators = []string{}
267 extraDstLocators = []string{}
268 dstIndex = make(map[string]bool)
270 // Put a few blocks in src using kcSrc
271 for i := 0; i < 5; i++ {
272 hash, _, err := kcSrc.PutB([]byte(fmt.Sprintf("test-data-%d", i)))
275 srcLocators = append(srcLocators, strings.Split(hash, "+A")[0])
276 if strings.HasPrefix(hash, indexPrefix) {
277 srcLocatorsMatchingPrefix = append(srcLocatorsMatchingPrefix, strings.Split(hash, "+A")[0])
281 // Put first two of those src blocks in dst using kcDst
282 for i := 0; i < 2; i++ {
283 hash, _, err := kcDst.PutB([]byte(fmt.Sprintf("test-data-%d", i)))
285 dstLocators = append(dstLocators, strings.Split(hash, "+A")[0])
288 // Put two more blocks in dst; they are not in src at all
289 for i := 0; i < 2; i++ {
290 hash, _, err := kcDst.PutB([]byte(fmt.Sprintf("other-data-%d", i)))
292 dstLocators = append(dstLocators, strings.Split(hash, "+A")[0])
293 extraDstLocators = append(extraDstLocators, strings.Split(hash, "+A")[0])
297 // Setup rsync using srcKeepServicesJSON with fake keepservers.
298 // Expect error during performKeepRsync due to unreachable src keepservers.
299 func (s *ServerRequiredSuite) TestErrorDuringRsync_FakeSrcKeepservers(c *C) {
300 srcKeepServicesJSON = testKeepServicesJSON
302 setupRsync(c, false, 1)
304 err := performKeepRsync(kcSrc, kcDst, blobSignatureTTL, "", "")
305 c.Assert(err, NotNil)
306 c.Check(err.Error(), Matches, ".*no such host.*")
309 // Setup rsync using dstKeepServicesJSON with fake keepservers.
310 // Expect error during performKeepRsync due to unreachable dst keepservers.
311 func (s *ServerRequiredSuite) TestErrorDuringRsync_FakeDstKeepservers(c *C) {
312 dstKeepServicesJSON = testKeepServicesJSON
314 setupRsync(c, false, 1)
316 err := performKeepRsync(kcSrc, kcDst, blobSignatureTTL, "", "")
317 c.Assert(err, NotNil)
318 c.Check(err.Error(), Matches, ".*no such host.*")
321 // Test rsync with signature error during Get from src.
322 func (s *ServerRequiredSuite) TestErrorDuringRsync_ErrorGettingBlockFromSrc(c *C) {
323 setupRsync(c, true, 1)
325 // put some blocks in src and dst
328 // Change blob signing key to a fake key, so that Get from src fails
329 blobSigningKey = "thisisfakeblobsigningkey"
331 err := performKeepRsync(kcSrc, kcDst, blobSignatureTTL, blobSigningKey, "")
332 c.Assert(err, NotNil)
333 c.Check(err.Error(), Matches, ".*HTTP 403 \"Forbidden\".*")
336 // Test rsync with error during Put to src.
337 func (s *ServerRequiredSuite) TestErrorDuringRsync_ErrorPuttingBlockInDst(c *C) {
338 setupRsync(c, false, 1)
340 // put some blocks in src and dst
343 // Increase Want_replicas on dst to result in insufficient replicas error during Put
344 kcDst.Want_replicas = 2
346 err := performKeepRsync(kcSrc, kcDst, blobSignatureTTL, blobSigningKey, "")
347 c.Assert(err, NotNil)
348 c.Check(err.Error(), Matches, ".*Could not write sufficient replicas.*")
351 // Test loadConfig func
352 func (s *ServerNotRequiredSuite) TestLoadConfig(c *C) {
353 // Setup a src config file
354 srcFile := setupConfigFile(c, "src-config")
355 defer os.Remove(srcFile.Name())
356 srcConfigFile := srcFile.Name()
358 // Setup a dst config file
359 dstFile := setupConfigFile(c, "dst-config")
360 defer os.Remove(dstFile.Name())
361 dstConfigFile := dstFile.Name()
363 // load configuration from those files
364 srcConfig, srcBlobSigningKey, err := loadConfig(srcConfigFile)
367 c.Assert(srcConfig.APIHost, Equals, os.Getenv("ARVADOS_API_HOST"))
368 c.Assert(srcConfig.APIToken, Equals, arvadostest.SystemRootToken)
369 c.Assert(srcConfig.APIHostInsecure, Equals, arvadosclient.StringBool(os.Getenv("ARVADOS_API_HOST_INSECURE")))
370 c.Assert(srcConfig.ExternalClient, Equals, false)
372 dstConfig, _, err := loadConfig(dstConfigFile)
375 c.Assert(dstConfig.APIHost, Equals, os.Getenv("ARVADOS_API_HOST"))
376 c.Assert(dstConfig.APIToken, Equals, arvadostest.SystemRootToken)
377 c.Assert(dstConfig.APIHostInsecure, Equals, arvadosclient.StringBool(os.Getenv("ARVADOS_API_HOST_INSECURE")))
378 c.Assert(dstConfig.ExternalClient, Equals, false)
380 c.Assert(srcBlobSigningKey, Equals, "abcdefg")
383 // Test loadConfig func without setting up the config files
384 func (s *ServerNotRequiredSuite) TestLoadConfig_MissingSrcConfig(c *C) {
385 _, _, err := loadConfig("")
386 c.Assert(err.Error(), Equals, "config file not specified")
389 // Test loadConfig func - error reading config
390 func (s *ServerNotRequiredSuite) TestLoadConfig_ErrorLoadingSrcConfig(c *C) {
391 _, _, err := loadConfig("no-such-config-file")
392 c.Assert(err, NotNil)
393 c.Check(err.Error(), Matches, ".*no such file or directory.*")
396 func (s *ServerNotRequiredSuite) TestSetupKeepClient_NoBlobSignatureTTL(c *C) {
397 var srcConfig apiConfig
398 srcConfig.APIHost = os.Getenv("ARVADOS_API_HOST")
399 srcConfig.APIToken = arvadostest.SystemRootToken
400 srcConfig.APIHostInsecure = arvadosclient.StringBool(os.Getenv("ARVADOS_API_HOST_INSECURE"))
402 _, ttl, err := setupKeepClient(srcConfig, srcKeepServicesJSON, false, 0, 0)
404 c.Assert(ttl, Equals, blobSignatureTTL)
407 func setupConfigFile(c *C, name string) *os.File {
408 // Setup a config file
409 file, err := ioutil.TempFile(os.TempDir(), name)
412 fileContent := "ARVADOS_API_HOST=" + os.Getenv("ARVADOS_API_HOST") + "\n"
413 fileContent += "ARVADOS_API_TOKEN=" + arvadostest.SystemRootToken + "\n"
414 fileContent += "ARVADOS_API_HOST_INSECURE=" + os.Getenv("ARVADOS_API_HOST_INSECURE") + "\n"
415 fileContent += "ARVADOS_EXTERNAL_CLIENT=false\n"
416 fileContent += "ARVADOS_BLOB_SIGNING_KEY=abcdefg"
418 _, err = file.Write([]byte(fileContent))
424 func (s *DoMainTestSuite) Test_doMain_NoSrcConfig(c *C) {
426 c.Assert(err, NotNil)
427 c.Assert(err.Error(), Equals, "Error loading src configuration from file: config file not specified")
430 func (s *DoMainTestSuite) Test_doMain_SrcButNoDstConfig(c *C) {
431 srcConfig := setupConfigFile(c, "src")
432 args := []string{"-replications", "3", "-src", srcConfig.Name()}
433 os.Args = append(os.Args, args...)
435 c.Assert(err, NotNil)
436 c.Assert(err.Error(), Equals, "Error loading dst configuration from file: config file not specified")
439 func (s *DoMainTestSuite) Test_doMain_BadSrcConfig(c *C) {
440 args := []string{"-src", "abcd"}
441 os.Args = append(os.Args, args...)
443 c.Assert(err, NotNil)
444 c.Assert(err.Error(), Matches, "Error loading src configuration from file: Error reading config file.*")
447 func (s *DoMainTestSuite) Test_doMain_WithReplicationsButNoSrcConfig(c *C) {
448 args := []string{"-replications", "3"}
449 os.Args = append(os.Args, args...)
452 c.Assert(err.Error(), Equals, "Error loading src configuration from file: config file not specified")
455 func (s *DoMainTestSuite) Test_doMainWithSrcAndDstConfig(c *C) {
456 srcConfig := setupConfigFile(c, "src")
457 dstConfig := setupConfigFile(c, "dst")
458 args := []string{"-src", srcConfig.Name(), "-dst", dstConfig.Name()}
459 os.Args = append(os.Args, args...)
461 // Start keepservers. Since we are not doing any tweaking as
462 // in setupRsync func, kcSrc and kcDst will be the same and no
463 // actual copying to dst will happen, but that's ok.
464 arvadostest.StartKeep(2, false)
465 defer arvadostest.StopKeep(2)
466 keepclient.RefreshServiceDiscovery()