11 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
12 "git.curoverse.com/arvados.git/sdk/go/keepclient"
17 // Gocheck boilerplate
18 func Test(t *testing.T) {
22 // Gocheck boilerplate
23 var _ = Suite(&ServerRequiredSuite{})
25 // Tests that require the Keep server running
26 type ServerRequiredSuite struct{}
28 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
31 func (s *ServerRequiredSuite) SetUpTest(c *C) {
32 arvadostest.ResetEnv()
33 srcKeepServicesJSON = ""
34 dstKeepServicesJSON = ""
37 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
38 arvadostest.StopKeep()
42 // Testing keep-rsync needs two sets of keep services: src and dst.
43 // The test setup hence tweaks keep-rsync initialization to achieve this.
44 // First invoke initializeKeepRsync and then invoke StartKeepWithParams
45 // to create the keep servers to be used as destination.
46 func setupRsync(c *C, enforcePermissions bool) {
48 srcConfig.APIHost = os.Getenv("ARVADOS_API_HOST")
49 srcConfig.APIToken = os.Getenv("ARVADOS_API_TOKEN")
50 srcConfig.APIHostInsecure = matchTrue.MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE"))
53 dstConfig.APIHost = os.Getenv("ARVADOS_API_HOST")
54 dstConfig.APIToken = os.Getenv("ARVADOS_API_TOKEN")
55 dstConfig.APIHostInsecure = matchTrue.MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE"))
58 if enforcePermissions {
59 blobSigningKey = "zfhgfenhffzltr9dixws36j1yhksjoll2grmku38mi7yxd66h5j4q9w4jzanezacp8s6q0ro3hxakfye02152hncy6zml2ed0uc"
62 // Start API and Keep servers
63 arvadostest.StartAPI()
64 arvadostest.StartKeepWithParams(false, enforcePermissions)
66 // initialize keep-rsync
67 err := initializeKeepRsync()
68 c.Assert(err, Equals, nil)
70 // Create two more keep servers to be used as destination
71 arvadostest.StartKeepWithParams(true, enforcePermissions)
74 kcDst, err = keepclient.MakeKeepClient(&arvDst)
75 c.Assert(err, Equals, nil)
76 kcDst.Want_replicas = 1
79 // Test readConfigFromFile method
80 func (s *ServerRequiredSuite) TestReadConfigFromFile(c *C) {
81 // Setup a test config file
82 file, err := ioutil.TempFile(os.TempDir(), "config")
83 c.Assert(err, Equals, nil)
84 defer os.Remove(file.Name())
86 fileContent := "ARVADOS_API_HOST=testhost\n"
87 fileContent += "ARVADOS_API_TOKEN=testtoken\n"
88 fileContent += "ARVADOS_API_HOST_INSECURE=true\n"
89 fileContent += "ARVADOS_BLOB_SIGNING_KEY=abcdefg"
91 _, err = file.Write([]byte(fileContent))
93 // Invoke readConfigFromFile method with this test filename
94 config, err := readConfigFromFile(file.Name())
95 c.Assert(err, Equals, nil)
96 c.Assert(config.APIHost, Equals, "testhost")
97 c.Assert(config.APIToken, Equals, "testtoken")
98 c.Assert(config.APIHostInsecure, Equals, true)
99 c.Assert(config.ExternalClient, Equals, false)
100 c.Assert(blobSigningKey, Equals, "abcdefg")
103 // Test keep-rsync initialization, with src and dst keep servers.
104 // Do a Put and Get in src, both of which should succeed.
105 // Do a Put and Get in dst, both of which should succeed.
106 // Do a Get in dst for the src hash, which should raise block not found error.
107 // Do a Get in src for the dst hash, which should raise block not found error.
108 func (s *ServerRequiredSuite) TestRsyncPutInOne_GetFromOtherShouldFail(c *C) {
111 // Put a block in src using kcSrc and Get it
112 srcData := []byte("test-data1")
113 locatorInSrc := fmt.Sprintf("%x", md5.Sum(srcData))
115 hash, rep, err := kcSrc.PutB(srcData)
116 c.Check(hash, Matches, fmt.Sprintf(`^%s\+10(\+.+)?$`, locatorInSrc))
117 c.Check(rep, Equals, 2)
118 c.Check(err, Equals, nil)
120 reader, blocklen, _, err := kcSrc.Get(locatorInSrc)
121 c.Assert(err, Equals, nil)
122 c.Check(blocklen, Equals, int64(10))
123 all, err := ioutil.ReadAll(reader)
124 c.Check(all, DeepEquals, srcData)
126 // Put a different block in src using kcSrc and Get it
127 dstData := []byte("test-data2")
128 locatorInDst := fmt.Sprintf("%x", md5.Sum(dstData))
130 hash, rep, err = kcDst.PutB(dstData)
131 c.Check(hash, Matches, fmt.Sprintf(`^%s\+10(\+.+)?$`, locatorInDst))
132 c.Check(rep, Equals, 1)
133 c.Check(err, Equals, nil)
135 reader, blocklen, _, err = kcDst.Get(locatorInDst)
136 c.Assert(err, Equals, nil)
137 c.Check(blocklen, Equals, int64(10))
138 all, err = ioutil.ReadAll(reader)
139 c.Check(all, DeepEquals, dstData)
141 // Get srcLocator using kcDst should fail with Not Found error
142 _, _, _, err = kcDst.Get(locatorInSrc)
143 c.Assert(err.Error(), Equals, "Block not found")
145 // Get dstLocator using kcSrc should fail with Not Found error
146 _, _, _, err = kcSrc.Get(locatorInDst)
147 c.Assert(err.Error(), Equals, "Block not found")
150 // Test keep-rsync initialization, with srcKeepServicesJSON
151 func (s *ServerRequiredSuite) TestRsyncInitializeWithKeepServicesJSON(c *C) {
152 srcKeepServicesJSON = "{ \"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 }"
156 localRoots := kcSrc.LocalRoots()
157 c.Check(localRoots != nil, Equals, true)
160 for k := range localRoots {
161 if k == "zzzzz-bi6l4-123456789012340" {
165 c.Check(foundIt, Equals, true)
168 for k := range localRoots {
169 if k == "zzzzz-bi6l4-123456789012341" {
173 c.Check(foundIt, Equals, true)
176 // Test keep-rsync initialization, with src and dst keep servers with blobSigningKey.
177 // Do a Put and Get in src, both of which should succeed.
178 // Do a Put and Get in dst, both of which should succeed.
179 // Do a Get in dst for the src hash, which should raise block not found error.
180 // Do a Get in src for the dst hash, which should raise block not found error.
181 func (s *ServerRequiredSuite) TestRsyncWithBlobSigning_PutInOne_GetFromOtherShouldFail(c *C) {
184 // Put a block in src using kcSrc and Get it
185 srcData := []byte("test-data1")
186 locatorInSrc := fmt.Sprintf("%x", md5.Sum(srcData))
188 hash, rep, err := kcSrc.PutB(srcData)
189 c.Check(hash, Matches, fmt.Sprintf(`^%s\+10(\+.+)?$`, locatorInSrc))
190 c.Check(rep, Equals, 2)
191 c.Check(err, Equals, nil)
193 tomorrow := time.Now().AddDate(0, 0, 1)
194 signedLocator := keepclient.SignLocator(locatorInSrc, arvSrc.ApiToken, tomorrow, []byte(blobSigningKey))
196 reader, blocklen, _, err := kcSrc.Get(signedLocator)
197 c.Assert(err, Equals, nil)
198 c.Check(blocklen, Equals, int64(10))
199 all, err := ioutil.ReadAll(reader)
200 c.Check(all, DeepEquals, srcData)
202 // Put a different block in src using kcSrc and Get it
203 dstData := []byte("test-data2")
204 locatorInDst := fmt.Sprintf("%x", md5.Sum(dstData))
206 hash, rep, err = kcDst.PutB(dstData)
207 c.Check(hash, Matches, fmt.Sprintf(`^%s\+10(\+.+)?$`, locatorInDst))
208 c.Check(rep, Equals, 1)
209 c.Check(err, Equals, nil)
211 signedLocator = keepclient.SignLocator(locatorInDst, arvDst.ApiToken, tomorrow, []byte(blobSigningKey))
213 reader, blocklen, _, err = kcDst.Get(signedLocator)
214 c.Assert(err, Equals, nil)
215 c.Check(blocklen, Equals, int64(10))
216 all, err = ioutil.ReadAll(reader)
217 c.Check(all, DeepEquals, dstData)
219 // Get srcLocator using kcDst should fail with Not Found error
220 signedLocator = keepclient.SignLocator(locatorInSrc, arvDst.ApiToken, tomorrow, []byte(blobSigningKey))
221 _, _, _, err = kcDst.Get(locatorInSrc)
222 c.Assert(err.Error(), Equals, "Block not found")
224 // Get dstLocator using kcSrc should fail with Not Found error
225 signedLocator = keepclient.SignLocator(locatorInDst, arvSrc.ApiToken, tomorrow, []byte(blobSigningKey))
226 _, _, _, err = kcSrc.Get(locatorInDst)
227 c.Assert(err.Error(), Equals, "Block not found")
230 // Put some blocks in Src and some more in Dst
231 // And copy missing blocks from Src to Dst
232 func (s *ServerRequiredSuite) TestKeepRsync(c *C) {
233 testKeepRsync(c, false)
236 // Put some blocks in Src and some more in Dst with blob signing enabled.
237 // And copy missing blocks from Src to Dst
238 func (s *ServerRequiredSuite) TestKeepRsync_WithBlobSigning(c *C) {
239 testKeepRsync(c, true)
242 // Put 5 blocks in src. Put 2 of those blocks in dst
243 // Hence there are 3 additional blocks in src
244 // Also, put 2 extra blocks in dst; they are hence only in dst
245 // Run rsync and verify that those 7 blocks are now available in dst
246 func testKeepRsync(c *C, enforcePermissions bool) {
247 setupRsync(c, enforcePermissions)
249 tomorrow := time.Now().AddDate(0, 0, 1)
251 // Put a few blocks in src using kcSrc
252 var srcLocators []string
253 for i := 0; i < 5; i++ {
254 data := []byte(fmt.Sprintf("test-data-%d", i))
255 hash := fmt.Sprintf("%x", md5.Sum(data))
257 hash2, rep, err := kcSrc.PutB(data)
258 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+11(\+.+)?$`, hash))
259 c.Check(rep, Equals, 2)
260 c.Check(err, Equals, nil)
263 if enforcePermissions {
264 getLocator = keepclient.SignLocator(getLocator, arvSrc.ApiToken, tomorrow, []byte(blobSigningKey))
267 reader, blocklen, _, err := kcSrc.Get(getLocator)
268 c.Assert(err, Equals, nil)
269 c.Check(blocklen, Equals, int64(11))
270 all, err := ioutil.ReadAll(reader)
271 c.Check(all, DeepEquals, data)
273 srcLocators = append(srcLocators, fmt.Sprintf("%s+%d", hash, blocklen))
276 // Put just two of those blocks in dst using kcDst
277 var dstLocators []string
278 for i := 0; i < 2; i++ {
279 data := []byte(fmt.Sprintf("test-data-%d", i))
280 hash := fmt.Sprintf("%x", md5.Sum(data))
282 hash2, rep, err := kcDst.PutB(data)
283 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+11(\+.+)?$`, hash))
284 c.Check(rep, Equals, 1)
285 c.Check(err, Equals, nil)
288 if enforcePermissions {
289 getLocator = keepclient.SignLocator(getLocator, arvDst.ApiToken, tomorrow, []byte(blobSigningKey))
292 reader, blocklen, _, err := kcDst.Get(getLocator)
293 c.Assert(err, Equals, nil)
294 c.Check(blocklen, Equals, int64(11))
295 all, err := ioutil.ReadAll(reader)
296 c.Check(all, DeepEquals, data)
298 dstLocators = append(dstLocators, fmt.Sprintf("%s+%d", hash, blocklen))
301 // Put two more blocks in dst; they are not in src at all
302 var extraDstLocators []string
303 for i := 0; i < 2; i++ {
304 data := []byte(fmt.Sprintf("other-data-%d", i))
305 hash := fmt.Sprintf("%x", md5.Sum(data))
307 hash2, rep, err := kcDst.PutB(data)
308 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+12(\+.+)?$`, hash))
309 c.Check(rep, Equals, 1)
310 c.Check(err, Equals, nil)
313 if enforcePermissions {
314 getLocator = keepclient.SignLocator(getLocator, arvDst.ApiToken, tomorrow, []byte(blobSigningKey))
317 reader, blocklen, _, err := kcDst.Get(getLocator)
318 c.Assert(err, Equals, nil)
319 c.Check(blocklen, Equals, int64(12))
320 all, err := ioutil.ReadAll(reader)
321 c.Check(all, DeepEquals, data)
323 extraDstLocators = append(extraDstLocators, fmt.Sprintf("%s+%d", hash, blocklen))
326 err := performKeepRsync()
327 c.Check(err, Equals, nil)
329 // Now GetIndex from dst and verify that all 5 from src and the 2 extra blocks are found
330 dstIndex, err := getUniqueLocators(kcDst, "")
331 c.Check(err, Equals, nil)
332 for _, locator := range srcLocators {
333 _, ok := dstIndex[locator]
334 c.Assert(ok, Equals, true)
336 for _, locator := range extraDstLocators {
337 _, ok := dstIndex[locator]
338 c.Assert(ok, Equals, true)