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) SetUpSuite(c *C) {
47 arvadostest.StartAPI()
50 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
52 arvadostest.ResetEnv()
55 func (s *ServerRequiredSuite) SetUpTest(c *C) {
59 func (s *ServerRequiredSuite) TearDownTest(c *C) {
60 arvadostest.StopKeep(3)
63 func (s *ServerNotRequiredSuite) SetUpTest(c *C) {
67 type ServerNotRequiredSuite struct{}
69 type DoMainTestSuite struct {
73 func (s *DoMainTestSuite) SetUpTest(c *C) {
74 s.initialArgs = os.Args
75 os.Args = []string{"keep-rsync"}
79 func (s *DoMainTestSuite) TearDownTest(c *C) {
80 os.Args = s.initialArgs
83 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 }"
85 // Testing keep-rsync needs two sets of keep services: src and dst.
86 // The test setup hence creates 3 servers instead of the default 2,
87 // and uses the first 2 as src and the 3rd as dst keep servers.
88 func setupRsync(c *C, enforcePermissions bool, replications int) {
90 var srcConfig apiConfig
91 srcConfig.APIHost = os.Getenv("ARVADOS_API_HOST")
92 srcConfig.APIToken = arvadostest.SystemRootToken
93 srcConfig.APIHostInsecure = arvadosclient.StringBool(os.Getenv("ARVADOS_API_HOST_INSECURE"))
96 var dstConfig apiConfig
97 dstConfig.APIHost = os.Getenv("ARVADOS_API_HOST")
98 dstConfig.APIToken = arvadostest.SystemRootToken
99 dstConfig.APIHostInsecure = arvadosclient.StringBool(os.Getenv("ARVADOS_API_HOST_INSECURE"))
101 if enforcePermissions {
102 blobSigningKey = arvadostest.BlobSigningKey
105 // Start Keep servers
106 arvadostest.StartKeep(3, enforcePermissions)
107 keepclient.RefreshServiceDiscovery()
111 kcSrc, _, err = setupKeepClient(srcConfig, srcKeepServicesJSON, false, 0, blobSignatureTTL)
114 kcDst, _, err = setupKeepClient(dstConfig, dstKeepServicesJSON, true, replications, 0)
117 srcRoots := map[string]string{}
118 dstRoots := map[string]string{}
119 for uuid, root := range kcSrc.LocalRoots() {
120 if strings.HasSuffix(uuid, "02") {
121 dstRoots[uuid] = root
123 srcRoots[uuid] = root
126 if srcKeepServicesJSON == "" {
127 kcSrc.SetServiceRoots(srcRoots, srcRoots, srcRoots)
129 if dstKeepServicesJSON == "" {
130 kcDst.SetServiceRoots(dstRoots, dstRoots, dstRoots)
133 if replications == 0 {
134 // Must have got default replications value of 2 from dst discovery document
135 c.Assert(kcDst.Want_replicas, Equals, 2)
137 // Since replications value is provided, it is used
138 c.Assert(kcDst.Want_replicas, Equals, replications)
142 func (s *ServerRequiredSuite) TestRsyncPutInOne_GetFromOtherShouldFail(c *C) {
143 setupRsync(c, false, 1)
145 // Put a block in src and verify that it is not found in dst
146 testNoCrosstalk(c, "test-data-1", kcSrc, kcDst)
148 // Put a block in dst and verify that it is not found in src
149 testNoCrosstalk(c, "test-data-2", kcDst, kcSrc)
152 func (s *ServerRequiredSuite) TestRsyncWithBlobSigning_PutInOne_GetFromOtherShouldFail(c *C) {
153 setupRsync(c, true, 1)
155 // Put a block in src and verify that it is not found in dst
156 testNoCrosstalk(c, "test-data-1", kcSrc, kcDst)
158 // Put a block in dst and verify that it is not found in src
159 testNoCrosstalk(c, "test-data-2", kcDst, kcSrc)
162 // Do a Put in the first and Get from the second,
163 // which should raise block not found error.
164 func testNoCrosstalk(c *C, testData string, kc1, kc2 *keepclient.KeepClient) {
165 // Put a block using kc1
166 locator, _, err := kc1.PutB([]byte(testData))
167 c.Assert(err, Equals, nil)
169 locator = strings.Split(locator, "+")[0]
170 _, _, _, err = kc2.Get(keepclient.SignLocator(locator, kc2.Arvados.ApiToken, time.Now().AddDate(0, 0, 1), blobSignatureTTL, []byte(blobSigningKey)))
171 c.Assert(err, NotNil)
172 c.Check(err.Error(), Equals, "Block not found")
175 // Test keep-rsync initialization, with srcKeepServicesJSON
176 func (s *ServerRequiredSuite) TestRsyncInitializeWithKeepServicesJSON(c *C) {
177 srcKeepServicesJSON = testKeepServicesJSON
179 setupRsync(c, false, 1)
181 localRoots := kcSrc.LocalRoots()
182 c.Check(localRoots, NotNil)
183 c.Check(localRoots["zzzzz-bi6l4-123456789012340"], Not(Equals), "")
184 c.Check(localRoots["zzzzz-bi6l4-123456789012341"], Not(Equals), "")
187 // Test keep-rsync initialization with default replications count
188 func (s *ServerRequiredSuite) TestInitializeRsyncDefaultReplicationsCount(c *C) {
189 setupRsync(c, false, 0)
192 // Test keep-rsync initialization with replications count argument
193 func (s *ServerRequiredSuite) TestInitializeRsyncReplicationsCount(c *C) {
194 setupRsync(c, false, 3)
197 // Put some blocks in Src and some more in Dst
198 // And copy missing blocks from Src to Dst
199 func (s *ServerRequiredSuite) TestKeepRsync(c *C) {
200 testKeepRsync(c, false, "")
203 // Put some blocks in Src and some more in Dst with blob signing enabled.
204 // And copy missing blocks from Src to Dst
205 func (s *ServerRequiredSuite) TestKeepRsync_WithBlobSigning(c *C) {
206 testKeepRsync(c, true, "")
209 // Put some blocks in Src and some more in Dst
210 // Use prefix while doing rsync
211 // And copy missing blocks from Src to Dst
212 func (s *ServerRequiredSuite) TestKeepRsync_WithPrefix(c *C) {
213 data := []byte("test-data-4")
214 hash := fmt.Sprintf("%x", md5.Sum(data))
216 testKeepRsync(c, false, hash[0:3])
217 c.Check(len(dstIndex) > len(dstLocators), Equals, true)
220 // Put some blocks in Src and some more in Dst
221 // Use prefix not in src while doing rsync
222 // And copy missing blocks from Src to Dst
223 func (s *ServerRequiredSuite) TestKeepRsync_WithNoSuchPrefixInSrc(c *C) {
224 testKeepRsync(c, false, "999")
225 c.Check(len(dstIndex), Equals, len(dstLocators))
228 // Put 5 blocks in src. Put 2 of those blocks in dst
229 // Hence there are 3 additional blocks in src
230 // Also, put 2 extra blocks in dst; they are hence only in dst
231 // Run rsync and verify that those 7 blocks are now available in dst
232 func testKeepRsync(c *C, enforcePermissions bool, prefix string) {
233 setupRsync(c, enforcePermissions, 1)
236 setupTestData(c, prefix)
238 err := performKeepRsync(kcSrc, kcDst, blobSignatureTTL, blobSigningKey, prefix)
241 // Now GetIndex from dst and verify that all 5 from src and the 2 extra blocks are found
242 dstIndex, err = getUniqueLocators(kcDst, "")
245 for _, locator := range srcLocatorsMatchingPrefix {
246 _, ok := dstIndex[locator]
247 c.Assert(ok, Equals, true)
250 for _, locator := range extraDstLocators {
251 _, ok := dstIndex[locator]
252 c.Assert(ok, Equals, true)
256 // all blocks from src and the two extra blocks
257 c.Assert(len(dstIndex), Equals, len(srcLocators)+len(extraDstLocators))
259 // 1 matching prefix and copied over, 2 that were initially copied into dst along with src, and the 2 extra blocks
260 c.Assert(len(dstIndex), Equals, len(srcLocatorsMatchingPrefix)+len(extraDstLocators)+2)
264 // Setup test data in src and dst.
265 var srcLocators, srcLocatorsMatchingPrefix, dstLocators, extraDstLocators []string
266 var dstIndex map[string]bool
268 func setupTestData(c *C, indexPrefix string) {
269 srcLocators = []string{}
270 srcLocatorsMatchingPrefix = []string{}
271 dstLocators = []string{}
272 extraDstLocators = []string{}
273 dstIndex = make(map[string]bool)
275 // Put a few blocks in src using kcSrc
276 for i := 0; i < 5; i++ {
277 hash, _, err := kcSrc.PutB([]byte(fmt.Sprintf("test-data-%d", i)))
280 srcLocators = append(srcLocators, strings.Split(hash, "+A")[0])
281 if strings.HasPrefix(hash, indexPrefix) {
282 srcLocatorsMatchingPrefix = append(srcLocatorsMatchingPrefix, strings.Split(hash, "+A")[0])
286 // Put first two of those src blocks in dst using kcDst
287 for i := 0; i < 2; i++ {
288 hash, _, err := kcDst.PutB([]byte(fmt.Sprintf("test-data-%d", i)))
290 dstLocators = append(dstLocators, strings.Split(hash, "+A")[0])
293 // Put two more blocks in dst; they are not in src at all
294 for i := 0; i < 2; i++ {
295 hash, _, err := kcDst.PutB([]byte(fmt.Sprintf("other-data-%d", i)))
297 dstLocators = append(dstLocators, strings.Split(hash, "+A")[0])
298 extraDstLocators = append(extraDstLocators, strings.Split(hash, "+A")[0])
302 // Setup rsync using srcKeepServicesJSON with fake keepservers.
303 // Expect error during performKeepRsync due to unreachable src keepservers.
304 func (s *ServerRequiredSuite) TestErrorDuringRsync_FakeSrcKeepservers(c *C) {
305 srcKeepServicesJSON = testKeepServicesJSON
307 setupRsync(c, false, 1)
309 err := performKeepRsync(kcSrc, kcDst, blobSignatureTTL, "", "")
310 c.Assert(err, NotNil)
311 c.Check(err.Error(), Matches, ".*no such host.*")
314 // Setup rsync using dstKeepServicesJSON with fake keepservers.
315 // Expect error during performKeepRsync due to unreachable dst keepservers.
316 func (s *ServerRequiredSuite) TestErrorDuringRsync_FakeDstKeepservers(c *C) {
317 dstKeepServicesJSON = testKeepServicesJSON
319 setupRsync(c, false, 1)
321 err := performKeepRsync(kcSrc, kcDst, blobSignatureTTL, "", "")
322 c.Assert(err, NotNil)
323 c.Check(err.Error(), Matches, ".*no such host.*")
326 // Test rsync with signature error during Get from src.
327 func (s *ServerRequiredSuite) TestErrorDuringRsync_ErrorGettingBlockFromSrc(c *C) {
328 setupRsync(c, true, 1)
330 // put some blocks in src and dst
333 // Change blob signing key to a fake key, so that Get from src fails
334 blobSigningKey = "thisisfakeblobsigningkey"
336 err := performKeepRsync(kcSrc, kcDst, blobSignatureTTL, blobSigningKey, "")
337 c.Assert(err, NotNil)
338 c.Check(err.Error(), Matches, ".*HTTP 403 \"Forbidden\".*")
341 // Test rsync with error during Put to src.
342 func (s *ServerRequiredSuite) TestErrorDuringRsync_ErrorPuttingBlockInDst(c *C) {
343 setupRsync(c, false, 1)
345 // put some blocks in src and dst
348 // Increase Want_replicas on dst to result in insufficient replicas error during Put
349 kcDst.Want_replicas = 2
351 err := performKeepRsync(kcSrc, kcDst, blobSignatureTTL, blobSigningKey, "")
352 c.Assert(err, NotNil)
353 c.Check(err.Error(), Matches, ".*Could not write sufficient replicas.*")
356 // Test loadConfig func
357 func (s *ServerNotRequiredSuite) TestLoadConfig(c *C) {
358 // Setup a src config file
359 srcFile := setupConfigFile(c, "src-config")
360 defer os.Remove(srcFile.Name())
361 srcConfigFile := srcFile.Name()
363 // Setup a dst config file
364 dstFile := setupConfigFile(c, "dst-config")
365 defer os.Remove(dstFile.Name())
366 dstConfigFile := dstFile.Name()
368 // load configuration from those files
369 srcConfig, srcBlobSigningKey, err := loadConfig(srcConfigFile)
372 c.Assert(srcConfig.APIHost, Equals, os.Getenv("ARVADOS_API_HOST"))
373 c.Assert(srcConfig.APIToken, Equals, arvadostest.SystemRootToken)
374 c.Assert(srcConfig.APIHostInsecure, Equals, arvadosclient.StringBool(os.Getenv("ARVADOS_API_HOST_INSECURE")))
375 c.Assert(srcConfig.ExternalClient, Equals, false)
377 dstConfig, _, err := loadConfig(dstConfigFile)
380 c.Assert(dstConfig.APIHost, Equals, os.Getenv("ARVADOS_API_HOST"))
381 c.Assert(dstConfig.APIToken, Equals, arvadostest.SystemRootToken)
382 c.Assert(dstConfig.APIHostInsecure, Equals, arvadosclient.StringBool(os.Getenv("ARVADOS_API_HOST_INSECURE")))
383 c.Assert(dstConfig.ExternalClient, Equals, false)
385 c.Assert(srcBlobSigningKey, Equals, "abcdefg")
388 // Test loadConfig func without setting up the config files
389 func (s *ServerNotRequiredSuite) TestLoadConfig_MissingSrcConfig(c *C) {
390 _, _, err := loadConfig("")
391 c.Assert(err.Error(), Equals, "config file not specified")
394 // Test loadConfig func - error reading config
395 func (s *ServerNotRequiredSuite) TestLoadConfig_ErrorLoadingSrcConfig(c *C) {
396 _, _, err := loadConfig("no-such-config-file")
397 c.Assert(err, NotNil)
398 c.Check(err.Error(), Matches, ".*no such file or directory.*")
401 func (s *ServerNotRequiredSuite) TestSetupKeepClient_NoBlobSignatureTTL(c *C) {
402 var srcConfig apiConfig
403 srcConfig.APIHost = os.Getenv("ARVADOS_API_HOST")
404 srcConfig.APIToken = arvadostest.SystemRootToken
405 srcConfig.APIHostInsecure = arvadosclient.StringBool(os.Getenv("ARVADOS_API_HOST_INSECURE"))
407 _, ttl, err := setupKeepClient(srcConfig, srcKeepServicesJSON, false, 0, 0)
409 c.Assert(ttl, Equals, blobSignatureTTL)
412 func setupConfigFile(c *C, name string) *os.File {
413 // Setup a config file
414 file, err := ioutil.TempFile(os.TempDir(), name)
417 fileContent := "ARVADOS_API_HOST=" + os.Getenv("ARVADOS_API_HOST") + "\n"
418 fileContent += "ARVADOS_API_TOKEN=" + arvadostest.SystemRootToken + "\n"
419 fileContent += "ARVADOS_API_HOST_INSECURE=" + os.Getenv("ARVADOS_API_HOST_INSECURE") + "\n"
420 fileContent += "ARVADOS_EXTERNAL_CLIENT=false\n"
421 fileContent += "ARVADOS_BLOB_SIGNING_KEY=abcdefg"
423 _, err = file.Write([]byte(fileContent))
429 func (s *DoMainTestSuite) Test_doMain_NoSrcConfig(c *C) {
431 c.Assert(err, NotNil)
432 c.Assert(err.Error(), Equals, "Error loading src configuration from file: config file not specified")
435 func (s *DoMainTestSuite) Test_doMain_SrcButNoDstConfig(c *C) {
436 srcConfig := setupConfigFile(c, "src")
437 args := []string{"-replications", "3", "-src", srcConfig.Name()}
438 os.Args = append(os.Args, args...)
440 c.Assert(err, NotNil)
441 c.Assert(err.Error(), Equals, "Error loading dst configuration from file: config file not specified")
444 func (s *DoMainTestSuite) Test_doMain_BadSrcConfig(c *C) {
445 args := []string{"-src", "abcd"}
446 os.Args = append(os.Args, args...)
448 c.Assert(err, NotNil)
449 c.Assert(err.Error(), Matches, "Error loading src configuration from file: Error reading config file.*")
452 func (s *DoMainTestSuite) Test_doMain_WithReplicationsButNoSrcConfig(c *C) {
453 args := []string{"-replications", "3"}
454 os.Args = append(os.Args, args...)
457 c.Assert(err.Error(), Equals, "Error loading src configuration from file: config file not specified")
460 func (s *DoMainTestSuite) Test_doMainWithSrcAndDstConfig(c *C) {
461 srcConfig := setupConfigFile(c, "src")
462 dstConfig := setupConfigFile(c, "dst")
463 args := []string{"-src", srcConfig.Name(), "-dst", dstConfig.Name()}
464 os.Args = append(os.Args, args...)
466 // Start keepservers. Since we are not doing any tweaking as
467 // in setupRsync func, kcSrc and kcDst will be the same and no
468 // actual copying to dst will happen, but that's ok.
469 arvadostest.StartKeep(2, false)
470 defer arvadostest.StopKeep(2)
471 keepclient.RefreshServiceDiscovery()