12 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
13 "git.curoverse.com/arvados.git/sdk/go/keepclient"
18 // Gocheck boilerplate
19 func Test(t *testing.T) {
23 // Gocheck boilerplate
24 var _ = Suite(&ServerRequiredSuite{})
26 // Tests that require the Keep server running
27 type ServerRequiredSuite struct{}
29 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
32 var kcSrc, kcDst *keepclient.KeepClient
33 var srcKeepServicesJSON, dstKeepServicesJSON, blobSigningKey string
35 func (s *ServerRequiredSuite) SetUpTest(c *C) {
36 arvadostest.ResetEnv()
38 // reset all variables between tests
40 srcKeepServicesJSON = ""
41 dstKeepServicesJSON = ""
42 kcSrc = &keepclient.KeepClient{}
43 kcDst = &keepclient.KeepClient{}
46 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
47 arvadostest.StopKeepServers(3)
51 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 }"
53 // Testing keep-rsync needs two sets of keep services: src and dst.
54 // The test setup hence creates 3 servers instead of the default 2,
55 // and uses the first 2 as src and the 3rd as dst keep servers.
56 func setupRsync(c *C, enforcePermissions, updateDstReplications bool, replications int) {
58 var srcConfig apiConfig
59 srcConfig.APIHost = os.Getenv("ARVADOS_API_HOST")
60 srcConfig.APIToken = os.Getenv("ARVADOS_API_TOKEN")
61 srcConfig.APIHostInsecure = matchTrue.MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE"))
64 var dstConfig apiConfig
65 dstConfig.APIHost = os.Getenv("ARVADOS_API_HOST")
66 dstConfig.APIToken = os.Getenv("ARVADOS_API_TOKEN")
67 dstConfig.APIHostInsecure = matchTrue.MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE"))
69 if enforcePermissions {
70 blobSigningKey = "zfhgfenhffzltr9dixws36j1yhksjoll2grmku38mi7yxd66h5j4q9w4jzanezacp8s6q0ro3hxakfye02152hncy6zml2ed0uc"
73 // Start API and Keep servers
74 arvadostest.StartAPI()
75 arvadostest.StartKeepWithParams(3, enforcePermissions)
79 kcSrc, err = setupKeepClient(srcConfig, srcKeepServicesJSON, false, 0)
82 kcDst, err = setupKeepClient(dstConfig, dstKeepServicesJSON, true, replications)
85 for uuid := range kcSrc.LocalRoots() {
86 if strings.HasSuffix(uuid, "02") {
87 delete(kcSrc.LocalRoots(), uuid)
90 for uuid := range kcSrc.GatewayRoots() {
91 if strings.HasSuffix(uuid, "02") {
92 delete(kcSrc.GatewayRoots(), uuid)
95 for uuid := range kcSrc.WritableLocalRoots() {
96 if strings.HasSuffix(uuid, "02") {
97 delete(kcSrc.WritableLocalRoots(), uuid)
101 for uuid := range kcDst.LocalRoots() {
102 if strings.HasSuffix(uuid, "00") || strings.HasSuffix(uuid, "01") {
103 delete(kcDst.LocalRoots(), uuid)
106 for uuid := range kcDst.GatewayRoots() {
107 if strings.HasSuffix(uuid, "00") || strings.HasSuffix(uuid, "01") {
108 delete(kcDst.GatewayRoots(), uuid)
111 for uuid := range kcDst.WritableLocalRoots() {
112 if strings.HasSuffix(uuid, "00") || strings.HasSuffix(uuid, "01") {
113 delete(kcDst.WritableLocalRoots(), uuid)
117 if updateDstReplications {
118 kcDst.Want_replicas = replications
122 // Test keep-rsync initialization, with src and dst keep servers.
123 // Do a Put and Get in src, both of which should succeed.
124 // Do a Put and Get in dst, both of which should succeed.
125 // Do a Get in dst for the src hash, which should raise block not found error.
126 // Do a Get in src for the dst hash, which should raise block not found error.
127 func (s *ServerRequiredSuite) TestRsyncPutInOne_GetFromOtherShouldFail(c *C) {
128 setupRsync(c, false, true, 1)
130 // Put a block in src using kcSrc and Get it
131 srcData := []byte("test-data1")
132 locatorInSrc := fmt.Sprintf("%x", md5.Sum(srcData))
134 hash, rep, err := kcSrc.PutB(srcData)
135 c.Check(hash, Matches, fmt.Sprintf(`^%s\+10(\+.+)?$`, locatorInSrc))
136 c.Check(rep, Equals, 2)
137 c.Check(err, Equals, nil)
139 reader, blocklen, _, err := kcSrc.Get(locatorInSrc)
141 c.Check(blocklen, Equals, int64(10))
142 all, err := ioutil.ReadAll(reader)
143 c.Check(all, DeepEquals, srcData)
145 // Put a different block in src using kcSrc and Get it
146 dstData := []byte("test-data2")
147 locatorInDst := fmt.Sprintf("%x", md5.Sum(dstData))
149 hash, rep, err = kcDst.PutB(dstData)
150 c.Check(hash, Matches, fmt.Sprintf(`^%s\+10(\+.+)?$`, locatorInDst))
151 c.Check(rep, Equals, 1)
152 c.Check(err, Equals, nil)
154 reader, blocklen, _, err = kcDst.Get(locatorInDst)
156 c.Check(blocklen, Equals, int64(10))
157 all, err = ioutil.ReadAll(reader)
158 c.Check(all, DeepEquals, dstData)
160 // Get srcLocator using kcDst should fail with Not Found error
161 _, _, _, err = kcDst.Get(locatorInSrc)
162 c.Assert(err.Error(), Equals, "Block not found")
164 // Get dstLocator using kcSrc should fail with Not Found error
165 _, _, _, err = kcSrc.Get(locatorInDst)
166 c.Assert(err.Error(), Equals, "Block not found")
169 // Test keep-rsync initialization, with srcKeepServicesJSON
170 func (s *ServerRequiredSuite) TestRsyncInitializeWithKeepServicesJSON(c *C) {
171 srcKeepServicesJSON = testKeepServicesJSON
173 setupRsync(c, false, true, 1)
175 localRoots := kcSrc.LocalRoots()
176 c.Check(localRoots, NotNil)
179 for k := range localRoots {
180 if k == "zzzzz-bi6l4-123456789012340" {
184 c.Check(foundIt, Equals, true)
187 for k := range localRoots {
188 if k == "zzzzz-bi6l4-123456789012341" {
192 c.Check(foundIt, Equals, true)
195 // Test keep-rsync initialization, with src and dst keep servers with blobSigningKey.
196 // Do a Put and Get in src, both of which should succeed.
197 // Do a Put and Get in dst, both of which should succeed.
198 // Do a Get in dst for the src hash, which should raise block not found error.
199 // Do a Get in src for the dst hash, which should raise block not found error.
200 func (s *ServerRequiredSuite) TestRsyncWithBlobSigning_PutInOne_GetFromOtherShouldFail(c *C) {
201 setupRsync(c, true, true, 1)
203 // Put a block in src using kcSrc and Get it
204 srcData := []byte("test-data1")
205 locatorInSrc := fmt.Sprintf("%x", md5.Sum(srcData))
207 hash, rep, err := kcSrc.PutB(srcData)
208 c.Check(hash, Matches, fmt.Sprintf(`^%s\+10(\+.+)?$`, locatorInSrc))
209 c.Check(rep, Equals, 2)
210 c.Check(err, Equals, nil)
212 tomorrow := time.Now().AddDate(0, 0, 1)
213 signedLocator := keepclient.SignLocator(locatorInSrc, kcSrc.Arvados.ApiToken, tomorrow, []byte(blobSigningKey))
215 reader, blocklen, _, err := kcSrc.Get(signedLocator)
217 c.Check(blocklen, Equals, int64(10))
218 all, err := ioutil.ReadAll(reader)
219 c.Check(all, DeepEquals, srcData)
221 // Put a different block in src using kcSrc and Get it
222 dstData := []byte("test-data2")
223 locatorInDst := fmt.Sprintf("%x", md5.Sum(dstData))
225 hash, rep, err = kcDst.PutB(dstData)
226 c.Check(hash, Matches, fmt.Sprintf(`^%s\+10(\+.+)?$`, locatorInDst))
227 c.Check(rep, Equals, 1)
228 c.Check(err, Equals, nil)
230 signedLocator = keepclient.SignLocator(locatorInDst, kcDst.Arvados.ApiToken, tomorrow, []byte(blobSigningKey))
232 reader, blocklen, _, err = kcDst.Get(signedLocator)
234 c.Check(blocklen, Equals, int64(10))
235 all, err = ioutil.ReadAll(reader)
236 c.Check(all, DeepEquals, dstData)
238 // Get srcLocator using kcDst should fail with Not Found error
239 signedLocator = keepclient.SignLocator(locatorInSrc, kcDst.Arvados.ApiToken, tomorrow, []byte(blobSigningKey))
240 _, _, _, err = kcDst.Get(locatorInSrc)
241 c.Assert(err.Error(), Equals, "Block not found")
243 // Get dstLocator using kcSrc should fail with Not Found error
244 signedLocator = keepclient.SignLocator(locatorInDst, kcSrc.Arvados.ApiToken, tomorrow, []byte(blobSigningKey))
245 _, _, _, err = kcSrc.Get(locatorInDst)
246 c.Assert(err.Error(), Equals, "Block not found")
249 // Test keep-rsync initialization with default replications count
250 func (s *ServerRequiredSuite) TestInitializeRsyncDefaultReplicationsCount(c *C) {
251 setupRsync(c, false, false, 0)
253 // Must have got default replications value as 2 from dst discovery document
254 c.Assert(kcDst.Want_replicas, Equals, 2)
257 // Test keep-rsync initialization with replications count argument
258 func (s *ServerRequiredSuite) TestInitializeRsyncReplicationsCount(c *C) {
259 setupRsync(c, false, false, 3)
261 // Since replications value is provided, default is not used
262 c.Assert(kcDst.Want_replicas, Equals, 3)
265 // Put some blocks in Src and some more in Dst
266 // And copy missing blocks from Src to Dst
267 func (s *ServerRequiredSuite) TestKeepRsync(c *C) {
268 testKeepRsync(c, false, "")
271 // Put some blocks in Src and some more in Dst with blob signing enabled.
272 // And copy missing blocks from Src to Dst
273 func (s *ServerRequiredSuite) TestKeepRsync_WithBlobSigning(c *C) {
274 testKeepRsync(c, true, "")
277 // Put some blocks in Src and some more in Dst
278 // Use prefix while doing rsync
279 // And copy missing blocks from Src to Dst
280 func (s *ServerRequiredSuite) TestKeepRsync_WithPrefix(c *C) {
281 data := []byte("test-data-4")
282 hash := fmt.Sprintf("%x", md5.Sum(data))
284 testKeepRsync(c, false, hash[0:3])
287 // Put some blocks in Src and some more in Dst
288 // Use prefix not in src while doing rsync
289 // And copy missing blocks from Src to Dst
290 func (s *ServerRequiredSuite) TestKeepRsync_WithNoSuchPrefixInSrc(c *C) {
291 testKeepRsync(c, false, "999")
294 // Put 5 blocks in src. Put 2 of those blocks in dst
295 // Hence there are 3 additional blocks in src
296 // Also, put 2 extra blocks in dst; they are hence only in dst
297 // Run rsync and verify that those 7 blocks are now available in dst
298 func testKeepRsync(c *C, enforcePermissions bool, prefix string) {
299 setupRsync(c, enforcePermissions, true, 1)
302 setupTestData(c, prefix)
304 err := performKeepRsync(kcSrc, kcDst, blobSigningKey, prefix)
307 // Now GetIndex from dst and verify that all 5 from src and the 2 extra blocks are found
308 dstIndex, err := getUniqueLocators(kcDst, "")
312 for _, locator := range srcLocators {
313 _, ok := dstIndex[locator]
314 c.Assert(ok, Equals, true)
317 for _, locator := range srcLocatorsMatchingPrefix {
318 _, ok := dstIndex[locator]
319 c.Assert(ok, Equals, true)
323 for _, locator := range extraDstLocators {
324 _, ok := dstIndex[locator]
325 c.Assert(ok, Equals, true)
329 // all blocks from src and the two extra blocks
330 c.Assert(len(dstIndex), Equals, len(srcLocators)+len(extraDstLocators))
332 // one matching prefix, 2 that were initially copied into dst along with src, and the extra blocks
333 c.Assert(len(dstIndex), Equals, len(srcLocatorsMatchingPrefix)+len(extraDstLocators)+2)
337 // Setup test data in src and dst.
338 var srcLocators, srcLocatorsMatchingPrefix, dstLocators, extraDstLocators []string
340 func setupTestData(c *C, indexPrefix string) {
341 srcLocators = []string{}
342 srcLocatorsMatchingPrefix = []string{}
343 dstLocators = []string{}
344 extraDstLocators = []string{}
346 // Put a few blocks in src using kcSrc
347 for i := 0; i < 5; i++ {
348 hash, _, err := kcSrc.PutB([]byte(fmt.Sprintf("test-data-%d", i)))
351 srcLocators = append(srcLocators, strings.Split(hash, "+A")[0])
352 if strings.HasPrefix(hash, indexPrefix) {
353 srcLocatorsMatchingPrefix = append(srcLocatorsMatchingPrefix, strings.Split(hash, "+A")[0])
357 // Put first two of those src blocks in dst using kcDst
358 for i := 0; i < 2; i++ {
359 hash, _, err := kcDst.PutB([]byte(fmt.Sprintf("test-data-%d", i)))
361 dstLocators = append(dstLocators, strings.Split(hash, "+A")[0])
364 // Put two more blocks in dst; they are not in src at all
365 for i := 0; i < 2; i++ {
366 hash, _, err := kcDst.PutB([]byte(fmt.Sprintf("other-data-%d", i)))
368 dstLocators = append(dstLocators, strings.Split(hash, "+A")[0])
369 extraDstLocators = append(extraDstLocators, strings.Split(hash, "+A")[0])
373 // Setup rsync using srcKeepServicesJSON with fake keepservers.
374 // Expect error during performKeepRsync due to unreachable src keepservers.
375 func (s *ServerRequiredSuite) TestErrorDuringRsync_FakeSrcKeepservers(c *C) {
376 srcKeepServicesJSON = testKeepServicesJSON
378 setupRsync(c, false, false, 1)
380 err := performKeepRsync(kcSrc, kcDst, "", "")
381 c.Check(strings.HasSuffix(err.Error(), "no such host"), Equals, true)
384 // Setup rsync using dstKeepServicesJSON with fake keepservers.
385 // Expect error during performKeepRsync due to unreachable dst keepservers.
386 func (s *ServerRequiredSuite) TestErrorDuringRsync_FakeDstKeepservers(c *C) {
387 dstKeepServicesJSON = testKeepServicesJSON
389 setupRsync(c, false, false, 1)
391 err := performKeepRsync(kcSrc, kcDst, "", "")
392 c.Check(strings.HasSuffix(err.Error(), "no such host"), Equals, true)
395 // Test rsync with signature error during Get from src.
396 func (s *ServerRequiredSuite) TestErrorDuringRsync_ErrorGettingBlockFromSrc(c *C) {
397 setupRsync(c, true, true, 1)
399 // put some blocks in src and dst
402 // Change blob signing key to a fake key, so that Get from src fails
403 blobSigningKey = "thisisfakeblobsigningkey"
405 err := performKeepRsync(kcSrc, kcDst, blobSigningKey, "")
406 c.Check(strings.HasSuffix(err.Error(), "Block not found"), Equals, true)
409 // Test rsync with error during Put to src.
410 func (s *ServerRequiredSuite) TestErrorDuringRsync_ErrorPuttingBlockInDst(c *C) {
411 setupRsync(c, false, true, 1)
413 // put some blocks in src and dst
416 // Increase Want_replicas on dst to result in insufficient replicas error during Put
417 kcDst.Want_replicas = 2
419 err := performKeepRsync(kcSrc, kcDst, blobSigningKey, "")
420 c.Check(strings.HasSuffix(err.Error(), "Could not write sufficient replicas"), Equals, true)
423 // Test loadConfig func
424 func (s *ServerRequiredSuite) TestLoadConfig(c *C) {
425 // Setup a src config file
426 srcFile := setupConfigFile(c, "src-config")
427 defer os.Remove(srcFile.Name())
428 srcConfigFile := srcFile.Name()
430 // Setup a dst config file
431 dstFile := setupConfigFile(c, "dst-config")
432 defer os.Remove(dstFile.Name())
433 dstConfigFile := dstFile.Name()
435 // load configuration from those files
436 srcConfig, srcBlobSigningKey, err := loadConfig(srcConfigFile)
439 c.Assert(srcConfig.APIHost, Equals, "testhost")
440 c.Assert(srcConfig.APIToken, Equals, "testtoken")
441 c.Assert(srcConfig.APIHostInsecure, Equals, true)
442 c.Assert(srcConfig.ExternalClient, Equals, false)
444 dstConfig, _, err := loadConfig(dstConfigFile)
447 c.Assert(dstConfig.APIHost, Equals, "testhost")
448 c.Assert(dstConfig.APIToken, Equals, "testtoken")
449 c.Assert(dstConfig.APIHostInsecure, Equals, true)
450 c.Assert(dstConfig.ExternalClient, Equals, false)
452 c.Assert(srcBlobSigningKey, Equals, "abcdefg")
455 // Test loadConfig func without setting up the config files
456 func (s *ServerRequiredSuite) TestLoadConfig_MissingSrcConfig(c *C) {
457 _, _, err := loadConfig("")
458 c.Assert(err.Error(), Equals, "config file not specified")
461 // Test loadConfig func - error reading config
462 func (s *ServerRequiredSuite) TestLoadConfig_ErrorLoadingSrcConfig(c *C) {
463 _, _, err := loadConfig("no-such-config-file")
464 c.Assert(strings.HasSuffix(err.Error(), "no such file or directory"), Equals, true)
467 func setupConfigFile(c *C, name string) *os.File {
468 // Setup a config file
469 file, err := ioutil.TempFile(os.TempDir(), name)
472 fileContent := "ARVADOS_API_HOST=testhost\n"
473 fileContent += "ARVADOS_API_TOKEN=testtoken\n"
474 fileContent += "ARVADOS_API_HOST_INSECURE=true\n"
475 fileContent += "ARVADOS_BLOB_SIGNING_KEY=abcdefg"
477 _, err = file.Write([]byte(fileContent))