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{})
25 var _ = Suite(&DoMainTestSuite{})
27 type ServerRequiredSuite struct{}
28 type DoMainTestSuite struct{}
30 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
31 arvadostest.StartAPI()
34 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
36 arvadostest.ResetEnv()
39 func (s *ServerRequiredSuite) SetUpTest(c *C) {
43 tempfile, err := ioutil.TempFile(os.TempDir(), "temp-log-file")
45 log.SetOutput(tempfile)
46 tempLogFileName = tempfile.Name()
49 func (s *ServerRequiredSuite) TearDownTest(c *C) {
50 arvadostest.StopKeep(2)
51 os.Remove(tempLogFileName)
54 var tempLogFileName = ""
55 var initialArgs []string
56 var kc *keepclient.KeepClient
57 var keepServicesJSON, blobSigningKey string
59 func (s *DoMainTestSuite) SetUpSuite(c *C) {
63 func (s *DoMainTestSuite) SetUpTest(c *C) {
67 args := []string{"keep-block-check"}
70 tempfile, err := ioutil.TempFile(os.TempDir(), "temp-log-file")
72 log.SetOutput(tempfile)
73 tempLogFileName = tempfile.Name()
76 func (s *DoMainTestSuite) TearDownTest(c *C) {
77 os.Remove(tempLogFileName)
81 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 }"
83 var TestHash = "aaaa09c290d0fb1ca068ffaddf22cbd0"
84 var TestHash2 = "aaaac516f788aec4f30932ffb6395c39"
86 func setupKeepBlockCheck(c *C, enforcePermissions bool) {
88 config.APIHost = os.Getenv("ARVADOS_API_HOST")
89 config.APIToken = arvadostest.DataManagerToken
90 config.APIHostInsecure = matchTrue.MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE"))
91 if enforcePermissions {
92 blobSigningKey = "zfhgfenhffzltr9dixws36j1yhksjoll2grmku38mi7yxd66h5j4q9w4jzanezacp8s6q0ro3hxakfye02152hncy6zml2ed0uc"
96 arvadostest.StartKeep(2, enforcePermissions)
100 kc, err = setupKeepClient(config, keepServicesJSON)
105 var allLocators []string
107 func setupTestData(c *C) {
108 allLocators = []string{}
111 for i := 0; i < 5; i++ {
112 hash, _, err := kc.PutB([]byte(fmt.Sprintf("keep-block-check-test-data-%d", i)))
114 allLocators = append(allLocators, strings.Split(hash, "+A")[0])
118 func setupConfigFile(c *C, fileName string) string {
119 // Setup a config file
120 file, err := ioutil.TempFile(os.TempDir(), fileName)
123 // Add config to file. While at it, throw some extra white space
124 fileContent := "ARVADOS_API_HOST=" + os.Getenv("ARVADOS_API_HOST") + "\n"
125 fileContent += "ARVADOS_API_TOKEN=" + arvadostest.DataManagerToken + "\n"
127 fileContent += "ARVADOS_API_HOST_INSECURE=" + os.Getenv("ARVADOS_API_HOST_INSECURE") + "\n"
128 fileContent += " ARVADOS_EXTERNAL_CLIENT = false \n"
129 fileContent += "ARVADOS_BLOB_SIGNING_KEY=abcdefg\n"
131 _, err = file.Write([]byte(fileContent))
137 func setupBlockHashFile(c *C, name string, blocks []string) string {
138 // Setup a block hash file
139 file, err := ioutil.TempFile(os.TempDir(), name)
142 // Add the hashes to the file. While at it, throw some extra white space
144 for _, hash := range blocks {
145 fileContent += fmt.Sprintf(" %s \n", hash)
148 _, err = file.Write([]byte(fileContent))
154 func checkErrorLog(c *C, blocks []string, prefix, suffix string) {
155 buf, _ := ioutil.ReadFile(tempLogFileName)
156 if len(blocks) == 0 {
157 expected := prefix + `.*` + suffix
158 match, _ := regexp.MatchString(expected, string(buf))
159 c.Assert(match, Equals, false)
162 for _, hash := range blocks {
163 expected := prefix + `.*` + hash + `.*` + suffix
164 match, _ := regexp.MatchString(expected, string(buf))
165 c.Assert(match, Equals, true)
169 func (s *ServerRequiredSuite) TestBlockCheck(c *C) {
170 setupKeepBlockCheck(c, false)
172 err := performKeepBlockCheck(kc, blobSigningKey, "", allLocators)
174 checkErrorLog(c, []string{}, "head", "Block not found") // no errors
177 func (s *ServerRequiredSuite) TestBlockCheckWithBlobSigning(c *C) {
178 setupKeepBlockCheck(c, true)
180 err := performKeepBlockCheck(kc, blobSigningKey, "", allLocators)
182 checkErrorLog(c, []string{}, "head", "Block not found") // no errors
185 func (s *ServerRequiredSuite) TestBlockCheck_NoSuchBlock(c *C) {
186 setupKeepBlockCheck(c, false)
188 allLocators = append(allLocators, TestHash)
189 allLocators = append(allLocators, TestHash2)
190 err := performKeepBlockCheck(kc, blobSigningKey, "", allLocators)
192 c.Assert(err.Error(), Equals, "Head information not found for 2 out of 7 blocks with matching prefix.")
193 checkErrorLog(c, []string{TestHash, TestHash2}, "head", "Block not found")
196 func (s *ServerRequiredSuite) TestBlockCheck_NoSuchBlock_WithMatchingPrefix(c *C) {
197 setupKeepBlockCheck(c, false)
199 allLocators = append(allLocators, TestHash)
200 allLocators = append(allLocators, TestHash2)
201 err := performKeepBlockCheck(kc, blobSigningKey, "aaa", allLocators)
203 // Of the 7 blocks given, only two match the prefix and hence only those are checked
204 c.Assert(err.Error(), Equals, "Head information not found for 2 out of 2 blocks with matching prefix.")
205 checkErrorLog(c, []string{TestHash, TestHash2}, "head", "Block not found")
208 func (s *ServerRequiredSuite) TestBlockCheck_NoSuchBlock_WithPrefixMismatch(c *C) {
209 setupKeepBlockCheck(c, false)
211 allLocators = append(allLocators, TestHash)
212 allLocators = append(allLocators, TestHash2)
213 err := performKeepBlockCheck(kc, blobSigningKey, "999", allLocators)
215 checkErrorLog(c, []string{}, "head", "Block not found") // no errors
218 // Setup block-check using keepServicesJSON with fake keepservers.
219 // Expect error during performKeepBlockCheck due to unreachable keepservers.
220 func (s *ServerRequiredSuite) TestErrorDuringKeepBlockCheck_FakeKeepservers(c *C) {
221 keepServicesJSON = testKeepServicesJSON
222 setupKeepBlockCheck(c, false)
223 err := performKeepBlockCheck(kc, blobSigningKey, "", []string{TestHash, TestHash2})
224 c.Assert(err.Error(), Equals, "Head information not found for 2 out of 2 blocks with matching prefix.")
225 checkErrorLog(c, []string{TestHash, TestHash2}, "head", "no such host")
228 func (s *ServerRequiredSuite) TestBlockCheck_BadSignature(c *C) {
229 setupKeepBlockCheck(c, true)
231 err := performKeepBlockCheck(kc, "badblobsigningkey", "", []string{TestHash, TestHash2})
232 c.Assert(err.Error(), Equals, "Head information not found for 2 out of 2 blocks with matching prefix.")
233 checkErrorLog(c, []string{TestHash, TestHash2}, "head", "HTTP 403")
236 // Test keep-block-check initialization with keepServicesJSON
237 func (s *ServerRequiredSuite) TestKeepBlockCheck_InitializeWithKeepServicesJSON(c *C) {
238 keepServicesJSON = testKeepServicesJSON
239 setupKeepBlockCheck(c, false)
241 for k := range kc.LocalRoots() {
242 if k == "zzzzz-bi6l4-123456789012340" || k == "zzzzz-bi6l4-123456789012341" {
246 c.Check(found, Equals, 2)
249 // Test loadConfig func
250 func (s *ServerRequiredSuite) TestLoadConfig(c *C) {
252 configFile := setupConfigFile(c, "config")
253 defer os.Remove(configFile)
255 // load configuration from the file
256 config, blobSigningKey, err := loadConfig(configFile)
259 c.Assert(config.APIHost, Equals, os.Getenv("ARVADOS_API_HOST"))
260 c.Assert(config.APIToken, Equals, arvadostest.DataManagerToken)
261 c.Assert(config.APIHostInsecure, Equals, matchTrue.MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE")))
262 c.Assert(config.ExternalClient, Equals, false)
263 c.Assert(blobSigningKey, Equals, "abcdefg")
266 func (s *DoMainTestSuite) Test_doMain_WithNoConfig(c *C) {
267 args := []string{"-prefix", "a"}
268 os.Args = append(os.Args, args...)
271 c.Assert(strings.Contains(err.Error(), "config file not specified"), Equals, true)
274 func (s *DoMainTestSuite) Test_doMain_WithNoSuchConfigFile(c *C) {
275 args := []string{"-config", "no-such-file"}
276 os.Args = append(os.Args, args...)
279 c.Assert(strings.Contains(err.Error(), "no such file or directory"), Equals, true)
282 func (s *DoMainTestSuite) Test_doMain_WithNoBlockHashFile(c *C) {
283 config := setupConfigFile(c, "config")
284 defer os.Remove(config)
286 args := []string{"-config", config}
287 os.Args = append(os.Args, args...)
289 // Start keepservers.
290 arvadostest.StartKeep(2, false)
291 defer arvadostest.StopKeep(2)
294 c.Assert(strings.Contains(err.Error(), "block-hash-file not specified"), Equals, true)
297 func (s *DoMainTestSuite) Test_doMain_WithNoSuchBlockHashFile(c *C) {
298 config := setupConfigFile(c, "config")
299 defer os.Remove(config)
301 args := []string{"-config", config, "-block-hash-file", "no-such-file"}
302 os.Args = append(os.Args, args...)
304 // Start keepservers.
305 arvadostest.StartKeep(2, false)
306 defer arvadostest.StopKeep(2)
309 c.Assert(strings.Contains(err.Error(), "no such file or directory"), Equals, true)
312 func (s *DoMainTestSuite) Test_doMain(c *C) {
313 // Start keepservers.
314 arvadostest.StartKeep(2, false)
315 defer arvadostest.StopKeep(2)
317 config := setupConfigFile(c, "config")
318 defer os.Remove(config)
320 locatorFile := setupBlockHashFile(c, "block-hash", []string{TestHash, TestHash2})
321 defer os.Remove(locatorFile)
323 args := []string{"-config", config, "-block-hash-file", locatorFile}
324 os.Args = append(os.Args, args...)
328 c.Assert(err.Error(), Equals, "Head information not found for 2 out of 2 blocks with matching prefix.")
329 checkErrorLog(c, []string{TestHash, TestHash2}, "head", "Block not found")