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 fileContent := "ARVADOS_API_HOST=" + os.Getenv("ARVADOS_API_HOST") + "\n"
124 fileContent += "ARVADOS_API_TOKEN=" + arvadostest.DataManagerToken + "\n"
125 fileContent += "ARVADOS_API_HOST_INSECURE=" + os.Getenv("ARVADOS_API_HOST_INSECURE") + "\n"
126 fileContent += "ARVADOS_EXTERNAL_CLIENT=false\n"
127 fileContent += "ARVADOS_BLOB_SIGNING_KEY=abcdefg"
129 _, err = file.Write([]byte(fileContent))
135 func setupBlockHashFile(c *C, name string, blocks []string) string {
136 // Setup a block hash file
137 file, err := ioutil.TempFile(os.TempDir(), name)
141 for _, hash := range blocks {
142 fileContent += fmt.Sprintf("%s\n", hash)
144 _, err = file.Write([]byte(fileContent))
150 func checkErrorLog(c *C, blocks []string, prefix, suffix string) {
151 buf, _ := ioutil.ReadFile(tempLogFileName)
152 if len(blocks) == 0 {
153 expected := prefix + `.*` + suffix
154 match, _ := regexp.MatchString(expected, string(buf))
155 c.Assert(match, Equals, false)
158 for _, hash := range blocks {
159 expected := prefix + `.*` + hash + `.*` + suffix
160 match, _ := regexp.MatchString(expected, string(buf))
161 c.Assert(match, Equals, true)
165 func (s *ServerRequiredSuite) TestBlockCheck(c *C) {
166 setupKeepBlockCheck(c, false)
168 performKeepBlockCheck(kc, blobSigningKey, "", allLocators)
169 checkErrorLog(c, []string{}, "head", "Block not found") // no errors
172 func (s *ServerRequiredSuite) TestBlockCheckWithBlobSigning(c *C) {
173 setupKeepBlockCheck(c, true)
175 performKeepBlockCheck(kc, blobSigningKey, "", allLocators)
176 checkErrorLog(c, []string{}, "head", "Block not found") // no errors
179 func (s *ServerRequiredSuite) TestBlockCheck_NoSuchBlock(c *C) {
180 setupKeepBlockCheck(c, false)
182 performKeepBlockCheck(kc, blobSigningKey, "", []string{TestHash, TestHash2})
183 checkErrorLog(c, []string{TestHash, TestHash2}, "head", "Block not found")
186 func (s *ServerRequiredSuite) TestBlockCheck_NoSuchBlock_WithMatchingPrefix(c *C) {
187 setupKeepBlockCheck(c, false)
189 performKeepBlockCheck(kc, blobSigningKey, "aaa", []string{TestHash, TestHash2})
190 checkErrorLog(c, []string{TestHash, TestHash2}, "head", "Block not found")
193 func (s *ServerRequiredSuite) TestBlockCheck_NoSuchBlock_WithPrefixMismatch(c *C) {
194 setupKeepBlockCheck(c, false)
196 performKeepBlockCheck(kc, blobSigningKey, "999", []string{TestHash, TestHash2})
197 checkErrorLog(c, []string{}, "head", "Block not found") // no errors
200 // Setup block-check using keepServicesJSON with fake keepservers.
201 // Expect error during performKeepBlockCheck due to unreachable keepservers.
202 func (s *ServerRequiredSuite) TestErrorDuringKeepBlockCheck_FakeKeepservers(c *C) {
203 keepServicesJSON = testKeepServicesJSON
204 setupKeepBlockCheck(c, false)
205 performKeepBlockCheck(kc, blobSigningKey, "", []string{TestHash, TestHash2})
206 checkErrorLog(c, []string{TestHash, TestHash2}, "head", "no such host")
209 func (s *ServerRequiredSuite) TestBlockCheck_BadSignature(c *C) {
210 setupKeepBlockCheck(c, true)
212 performKeepBlockCheck(kc, "badblobsigningkey", "", []string{TestHash, TestHash2})
213 checkErrorLog(c, []string{TestHash, TestHash2}, "head", "HTTP 403")
216 // Test keep-block-check initialization with keepServicesJSON
217 func (s *ServerRequiredSuite) TestKeepBlockCheck_InitializeWithKeepServicesJSON(c *C) {
218 keepServicesJSON = testKeepServicesJSON
219 setupKeepBlockCheck(c, false)
221 for k := range kc.LocalRoots() {
222 if k == "zzzzz-bi6l4-123456789012340" || k == "zzzzz-bi6l4-123456789012341" {
226 c.Check(found, Equals, 2)
229 // Test loadConfig func
230 func (s *ServerRequiredSuite) TestLoadConfig(c *C) {
232 configFile := setupConfigFile(c, "config")
233 defer os.Remove(configFile)
235 // load configuration from the file
236 config, blobSigningKey, err := loadConfig(configFile)
239 c.Assert(config.APIHost, Equals, os.Getenv("ARVADOS_API_HOST"))
240 c.Assert(config.APIToken, Equals, arvadostest.DataManagerToken)
241 c.Assert(config.APIHostInsecure, Equals, matchTrue.MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE")))
242 c.Assert(config.ExternalClient, Equals, false)
243 c.Assert(blobSigningKey, Equals, "abcdefg")
246 func (s *DoMainTestSuite) Test_doMain_WithNoConfig(c *C) {
247 args := []string{"-prefix", "a"}
248 os.Args = append(os.Args, args...)
251 c.Assert(strings.Contains(err.Error(), "config file not specified"), Equals, true)
254 func (s *DoMainTestSuite) Test_doMain_WithNoSuchConfigFile(c *C) {
255 args := []string{"-config", "no-such-file"}
256 os.Args = append(os.Args, args...)
259 c.Assert(strings.Contains(err.Error(), "no such file or directory"), Equals, true)
262 func (s *DoMainTestSuite) Test_doMain_WithNoBlockHashFile(c *C) {
263 config := setupConfigFile(c, "config")
264 defer os.Remove(config)
266 args := []string{"-config", config}
267 os.Args = append(os.Args, args...)
269 // Start keepservers.
270 arvadostest.StartKeep(2, false)
271 defer arvadostest.StopKeep(2)
274 c.Assert(strings.Contains(err.Error(), "block-hash-file not specified"), Equals, true)
277 func (s *DoMainTestSuite) Test_doMain_WithNoSuchBlockHashFile(c *C) {
278 config := setupConfigFile(c, "config")
279 defer os.Remove(config)
281 args := []string{"-config", config, "-block-hash-file", "no-such-file"}
282 os.Args = append(os.Args, args...)
284 // Start keepservers.
285 arvadostest.StartKeep(2, false)
286 defer arvadostest.StopKeep(2)
289 c.Assert(strings.Contains(err.Error(), "no such file or directory"), Equals, true)
292 func (s *DoMainTestSuite) Test_doMain(c *C) {
293 config := setupConfigFile(c, "config")
294 defer os.Remove(config)
296 locatorFile := setupBlockHashFile(c, "block-hash", []string{TestHash, TestHash2})
297 defer os.Remove(locatorFile)
299 // Start keepservers.
300 arvadostest.StartKeep(2, false)
301 defer arvadostest.StopKeep(2)
303 args := []string{"-config", config, "-block-hash-file", locatorFile}
304 os.Args = append(os.Args, args...)
308 checkErrorLog(c, []string{TestHash, TestHash2}, "head", "Block not found")