13 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
14 "git.curoverse.com/arvados.git/sdk/go/keepclient"
21 srcKeepServicesJSON = ""
22 dstKeepServicesJSON = ""
27 // Gocheck boilerplate
28 func Test(t *testing.T) {
32 // Gocheck boilerplate
33 var _ = Suite(&ServerRequiredSuite{})
34 var _ = Suite(&ServerNotRequiredSuite{})
35 var _ = Suite(&DoMainTestSuite{})
37 // Tests that require the Keep server running
38 type ServerRequiredSuite struct{}
39 type ServerNotRequiredSuite struct{}
40 type DoMainTestSuite struct{}
42 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
44 arvadostest.StartAPI()
47 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
49 arvadostest.ResetEnv()
52 var initialArgs []string
54 func (s *DoMainTestSuite) SetUpSuite(c *C) {
58 var kcSrc, kcDst *keepclient.KeepClient
59 var srcKeepServicesJSON, dstKeepServicesJSON, blobSigningKey string
60 var blobSignatureTTL = time.Duration(2*7*24) * time.Hour
62 func (s *ServerRequiredSuite) SetUpTest(c *C) {
66 func (s *ServerRequiredSuite) TearDownTest(c *C) {
67 arvadostest.StopKeep(3)
70 func (s *DoMainTestSuite) SetUpTest(c *C) {
71 keepclient.ClearCache()
72 args := []string{"keep-rsync"}
76 func (s *DoMainTestSuite) TearDownTest(c *C) {
80 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 }"
82 // Testing keep-rsync needs two sets of keep services: src and dst.
83 // The test setup hence creates 3 servers instead of the default 2,
84 // and uses the first 2 as src and the 3rd as dst keep servers.
85 func setupRsync(c *C, enforcePermissions bool, replications int) {
87 var srcConfig apiConfig
88 srcConfig.APIHost = os.Getenv("ARVADOS_API_HOST")
89 srcConfig.APIToken = arvadostest.DataManagerToken
90 srcConfig.APIHostInsecure = matchTrue.MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE"))
93 var dstConfig apiConfig
94 dstConfig.APIHost = os.Getenv("ARVADOS_API_HOST")
95 dstConfig.APIToken = arvadostest.DataManagerToken
96 dstConfig.APIHostInsecure = matchTrue.MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE"))
98 if enforcePermissions {
99 blobSigningKey = arvadostest.BlobSigningKey
102 // Start Keep servers
103 arvadostest.StartKeep(3, enforcePermissions)
104 keepclient.ClearCache()
108 kcSrc, _, err = setupKeepClient(srcConfig, srcKeepServicesJSON, false, 0, blobSignatureTTL)
111 kcDst, _, err = setupKeepClient(dstConfig, dstKeepServicesJSON, true, replications, 0)
114 srcRoots := map[string]string{}
115 dstRoots := map[string]string{}
116 for uuid, root := range kcSrc.LocalRoots() {
117 if strings.HasSuffix(uuid, "02") {
118 dstRoots[uuid] = root
120 srcRoots[uuid] = root
123 if srcKeepServicesJSON == "" {
124 kcSrc.SetServiceRoots(srcRoots, srcRoots, srcRoots)
126 if dstKeepServicesJSON == "" {
127 kcDst.SetServiceRoots(dstRoots, dstRoots, dstRoots)
130 if replications == 0 {
131 // Must have got default replications value of 2 from dst discovery document
132 c.Assert(kcDst.Want_replicas, Equals, 2)
134 // Since replications value is provided, it is used
135 c.Assert(kcDst.Want_replicas, Equals, replications)
139 func (s *ServerRequiredSuite) TestRsyncPutInOne_GetFromOtherShouldFail(c *C) {
140 setupRsync(c, false, 1)
142 // Put a block in src and verify that it is not found in dst
143 testNoCrosstalk(c, "test-data-1", kcSrc, kcDst)
145 // Put a block in dst and verify that it is not found in src
146 testNoCrosstalk(c, "test-data-2", kcDst, kcSrc)
149 func (s *ServerRequiredSuite) TestRsyncWithBlobSigning_PutInOne_GetFromOtherShouldFail(c *C) {
150 setupRsync(c, true, 1)
152 // Put a block in src and verify that it is not found in dst
153 testNoCrosstalk(c, "test-data-1", kcSrc, kcDst)
155 // Put a block in dst and verify that it is not found in src
156 testNoCrosstalk(c, "test-data-2", kcDst, kcSrc)
159 // Do a Put in the first and Get from the second,
160 // which should raise block not found error.
161 func testNoCrosstalk(c *C, testData string, kc1, kc2 *keepclient.KeepClient) {
162 // Put a block using kc1
163 locator, _, err := kc1.PutB([]byte(testData))
164 c.Assert(err, Equals, nil)
166 locator = strings.Split(locator, "+")[0]
167 _, _, _, err = kc2.Get(keepclient.SignLocator(locator, kc2.Arvados.ApiToken, time.Now().AddDate(0, 0, 1), blobSignatureTTL, []byte(blobSigningKey)))
168 c.Assert(err, NotNil)
169 c.Check(err.Error(), Equals, "Block not found")
172 // Test keep-rsync initialization, with srcKeepServicesJSON
173 func (s *ServerRequiredSuite) TestRsyncInitializeWithKeepServicesJSON(c *C) {
174 srcKeepServicesJSON = testKeepServicesJSON
176 setupRsync(c, false, 1)
178 localRoots := kcSrc.LocalRoots()
179 c.Check(localRoots, NotNil)
182 for k := range localRoots {
183 if k == "zzzzz-bi6l4-123456789012340" {
187 c.Check(foundIt, Equals, true)
190 for k := range localRoots {
191 if k == "zzzzz-bi6l4-123456789012341" {
195 c.Check(foundIt, Equals, true)
198 // Test keep-rsync initialization with default replications count
199 func (s *ServerRequiredSuite) TestInitializeRsyncDefaultReplicationsCount(c *C) {
200 setupRsync(c, false, 0)
203 // Test keep-rsync initialization with replications count argument
204 func (s *ServerRequiredSuite) TestInitializeRsyncReplicationsCount(c *C) {
205 setupRsync(c, false, 3)
208 // Put some blocks in Src and some more in Dst
209 // And copy missing blocks from Src to Dst
210 func (s *ServerRequiredSuite) TestKeepRsync(c *C) {
211 testKeepRsync(c, false, "")
214 // Put some blocks in Src and some more in Dst with blob signing enabled.
215 // And copy missing blocks from Src to Dst
216 func (s *ServerRequiredSuite) TestKeepRsync_WithBlobSigning(c *C) {
217 testKeepRsync(c, true, "")
220 // Put some blocks in Src and some more in Dst
221 // Use prefix while doing rsync
222 // And copy missing blocks from Src to Dst
223 func (s *ServerRequiredSuite) TestKeepRsync_WithPrefix(c *C) {
224 data := []byte("test-data-4")
225 hash := fmt.Sprintf("%x", md5.Sum(data))
227 testKeepRsync(c, false, hash[0:3])
228 c.Check(len(dstIndex) > len(dstLocators), Equals, true)
231 // Put some blocks in Src and some more in Dst
232 // Use prefix not in src while doing rsync
233 // And copy missing blocks from Src to Dst
234 func (s *ServerRequiredSuite) TestKeepRsync_WithNoSuchPrefixInSrc(c *C) {
235 testKeepRsync(c, false, "999")
236 c.Check(len(dstIndex), Equals, len(dstLocators))
239 // Put 5 blocks in src. Put 2 of those blocks in dst
240 // Hence there are 3 additional blocks in src
241 // Also, put 2 extra blocks in dst; they are hence only in dst
242 // Run rsync and verify that those 7 blocks are now available in dst
243 func testKeepRsync(c *C, enforcePermissions bool, prefix string) {
244 setupRsync(c, enforcePermissions, 1)
247 setupTestData(c, prefix)
249 err := performKeepRsync(kcSrc, kcDst, blobSignatureTTL, blobSigningKey, prefix)
252 // Now GetIndex from dst and verify that all 5 from src and the 2 extra blocks are found
253 dstIndex, err = getUniqueLocators(kcDst, "")
256 for _, locator := range srcLocatorsMatchingPrefix {
257 _, ok := dstIndex[locator]
258 c.Assert(ok, Equals, true)
261 for _, locator := range extraDstLocators {
262 _, ok := dstIndex[locator]
263 c.Assert(ok, Equals, true)
267 // all blocks from src and the two extra blocks
268 c.Assert(len(dstIndex), Equals, len(srcLocators)+len(extraDstLocators))
270 // 1 matching prefix and copied over, 2 that were initially copied into dst along with src, and the 2 extra blocks
271 c.Assert(len(dstIndex), Equals, len(srcLocatorsMatchingPrefix)+len(extraDstLocators)+2)
275 // Setup test data in src and dst.
276 var srcLocators, srcLocatorsMatchingPrefix, dstLocators, extraDstLocators []string
277 var dstIndex map[string]bool
279 func setupTestData(c *C, indexPrefix string) {
280 srcLocators = []string{}
281 srcLocatorsMatchingPrefix = []string{}
282 dstLocators = []string{}
283 extraDstLocators = []string{}
284 dstIndex = make(map[string]bool)
286 // Put a few blocks in src using kcSrc
287 for i := 0; i < 5; i++ {
288 hash, _, err := kcSrc.PutB([]byte(fmt.Sprintf("test-data-%d", i)))
291 srcLocators = append(srcLocators, strings.Split(hash, "+A")[0])
292 if strings.HasPrefix(hash, indexPrefix) {
293 srcLocatorsMatchingPrefix = append(srcLocatorsMatchingPrefix, strings.Split(hash, "+A")[0])
297 // Put first two of those src blocks in dst using kcDst
298 for i := 0; i < 2; i++ {
299 hash, _, err := kcDst.PutB([]byte(fmt.Sprintf("test-data-%d", i)))
301 dstLocators = append(dstLocators, strings.Split(hash, "+A")[0])
304 // Put two more blocks in dst; they are not in src at all
305 for i := 0; i < 2; i++ {
306 hash, _, err := kcDst.PutB([]byte(fmt.Sprintf("other-data-%d", i)))
308 dstLocators = append(dstLocators, strings.Split(hash, "+A")[0])
309 extraDstLocators = append(extraDstLocators, strings.Split(hash, "+A")[0])
313 // Setup rsync using srcKeepServicesJSON with fake keepservers.
314 // Expect error during performKeepRsync due to unreachable src keepservers.
315 func (s *ServerRequiredSuite) TestErrorDuringRsync_FakeSrcKeepservers(c *C) {
316 srcKeepServicesJSON = testKeepServicesJSON
318 setupRsync(c, false, 1)
320 err := performKeepRsync(kcSrc, kcDst, blobSignatureTTL, "", "")
321 log.Printf("Err = %v", err)
322 c.Check(strings.Contains(err.Error(), "no such host"), Equals, true)
325 // Setup rsync using dstKeepServicesJSON with fake keepservers.
326 // Expect error during performKeepRsync due to unreachable dst keepservers.
327 func (s *ServerRequiredSuite) TestErrorDuringRsync_FakeDstKeepservers(c *C) {
328 dstKeepServicesJSON = testKeepServicesJSON
330 setupRsync(c, false, 1)
332 err := performKeepRsync(kcSrc, kcDst, blobSignatureTTL, "", "")
333 log.Printf("Err = %v", err)
334 c.Check(strings.Contains(err.Error(), "no such host"), Equals, true)
337 // Test rsync with signature error during Get from src.
338 func (s *ServerRequiredSuite) TestErrorDuringRsync_ErrorGettingBlockFromSrc(c *C) {
339 setupRsync(c, true, 1)
341 // put some blocks in src and dst
344 // Change blob signing key to a fake key, so that Get from src fails
345 blobSigningKey = "thisisfakeblobsigningkey"
347 err := performKeepRsync(kcSrc, kcDst, blobSignatureTTL, blobSigningKey, "")
348 c.Check(strings.Contains(err.Error(), "HTTP 403 \"Forbidden\""), Equals, true)
351 // Test rsync with error during Put to src.
352 func (s *ServerRequiredSuite) TestErrorDuringRsync_ErrorPuttingBlockInDst(c *C) {
353 setupRsync(c, false, 1)
355 // put some blocks in src and dst
358 // Increase Want_replicas on dst to result in insufficient replicas error during Put
359 kcDst.Want_replicas = 2
361 err := performKeepRsync(kcSrc, kcDst, blobSignatureTTL, blobSigningKey, "")
362 c.Check(strings.Contains(err.Error(), "Could not write sufficient replicas"), Equals, true)
365 // Test loadConfig func
366 func (s *ServerNotRequiredSuite) TestLoadConfig(c *C) {
367 // Setup a src config file
368 srcFile := setupConfigFile(c, "src-config")
369 defer os.Remove(srcFile.Name())
370 srcConfigFile := srcFile.Name()
372 // Setup a dst config file
373 dstFile := setupConfigFile(c, "dst-config")
374 defer os.Remove(dstFile.Name())
375 dstConfigFile := dstFile.Name()
377 // load configuration from those files
378 srcConfig, srcBlobSigningKey, err := loadConfig(srcConfigFile)
381 c.Assert(srcConfig.APIHost, Equals, os.Getenv("ARVADOS_API_HOST"))
382 c.Assert(srcConfig.APIToken, Equals, arvadostest.DataManagerToken)
383 c.Assert(srcConfig.APIHostInsecure, Equals, matchTrue.MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE")))
384 c.Assert(srcConfig.ExternalClient, Equals, false)
386 dstConfig, _, err := loadConfig(dstConfigFile)
389 c.Assert(dstConfig.APIHost, Equals, os.Getenv("ARVADOS_API_HOST"))
390 c.Assert(dstConfig.APIToken, Equals, arvadostest.DataManagerToken)
391 c.Assert(dstConfig.APIHostInsecure, Equals, matchTrue.MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE")))
392 c.Assert(dstConfig.ExternalClient, Equals, false)
394 c.Assert(srcBlobSigningKey, Equals, "abcdefg")
397 // Test loadConfig func without setting up the config files
398 func (s *ServerNotRequiredSuite) TestLoadConfig_MissingSrcConfig(c *C) {
399 _, _, err := loadConfig("")
400 c.Assert(err.Error(), Equals, "config file not specified")
403 // Test loadConfig func - error reading config
404 func (s *ServerNotRequiredSuite) TestLoadConfig_ErrorLoadingSrcConfig(c *C) {
405 _, _, err := loadConfig("no-such-config-file")
406 c.Assert(strings.Contains(err.Error(), "no such file or directory"), Equals, true)
409 func (s *ServerNotRequiredSuite) TestSetupKeepClient_NoBlobSignatureTTL(c *C) {
410 var srcConfig apiConfig
411 srcConfig.APIHost = os.Getenv("ARVADOS_API_HOST")
412 srcConfig.APIToken = arvadostest.DataManagerToken
413 srcConfig.APIHostInsecure = matchTrue.MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE"))
415 _, ttl, err := setupKeepClient(srcConfig, srcKeepServicesJSON, false, 0, 0)
417 c.Assert(ttl, Equals, blobSignatureTTL)
420 func setupConfigFile(c *C, name string) *os.File {
421 // Setup a config file
422 file, err := ioutil.TempFile(os.TempDir(), name)
425 fileContent := "ARVADOS_API_HOST=" + os.Getenv("ARVADOS_API_HOST") + "\n"
426 fileContent += "ARVADOS_API_TOKEN=" + arvadostest.DataManagerToken + "\n"
427 fileContent += "ARVADOS_API_HOST_INSECURE=" + os.Getenv("ARVADOS_API_HOST_INSECURE") + "\n"
428 fileContent += "ARVADOS_EXTERNAL_CLIENT=false\n"
429 fileContent += "ARVADOS_BLOB_SIGNING_KEY=abcdefg"
431 _, err = file.Write([]byte(fileContent))
437 func (s *DoMainTestSuite) Test_doMain_NoSrcConfig(c *C) {
440 c.Assert(err.Error(), Equals, "Error loading src configuration from file: config file not specified")
443 func (s *DoMainTestSuite) Test_doMain_SrcButNoDstConfig(c *C) {
444 srcConfig := setupConfigFile(c, "src")
445 args := []string{"-replications", "3", "-src", srcConfig.Name()}
446 os.Args = append(os.Args, args...)
449 c.Assert(err.Error(), Equals, "Error loading dst configuration from file: config file not specified")
452 func (s *DoMainTestSuite) Test_doMain_BadSrcConfig(c *C) {
453 args := []string{"-src", "abcd"}
454 os.Args = append(os.Args, args...)
457 c.Assert(strings.HasPrefix(err.Error(), "Error loading src configuration from file: Error reading config file"), Equals, true)
460 func (s *DoMainTestSuite) Test_doMain_WithReplicationsButNoSrcConfig(c *C) {
461 args := []string{"-replications", "3"}
462 os.Args = append(os.Args, args...)
465 c.Assert(err.Error(), Equals, "Error loading src configuration from file: config file not specified")
468 func (s *DoMainTestSuite) Test_doMainWithSrcAndDstConfig(c *C) {
469 srcConfig := setupConfigFile(c, "src")
470 dstConfig := setupConfigFile(c, "dst")
471 args := []string{"-src", srcConfig.Name(), "-dst", dstConfig.Name()}
472 os.Args = append(os.Args, args...)
474 // Start keepservers. Since we are not doing any tweaking as
475 // in setupRsync func, kcSrc and kcDst will be the same and no
476 // actual copying to dst will happen, but that's ok.
477 arvadostest.StartKeep(2, false)
478 defer arvadostest.StopKeep(2)
479 keepclient.ClearCache()