7167: merge test setup branch
[arvados.git] / tools / keep-rsync / keep-rsync_test.go
1 package main
2
3 import (
4         "crypto/md5"
5         "fmt"
6         "io/ioutil"
7         "os"
8         "testing"
9
10         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
11         "git.curoverse.com/arvados.git/sdk/go/keepclient"
12
13         . "gopkg.in/check.v1"
14 )
15
16 // Gocheck boilerplate
17 func Test(t *testing.T) {
18         TestingT(t)
19 }
20
21 // Gocheck boilerplate
22 var _ = Suite(&ServerRequiredSuite{})
23
24 // Tests that require the Keep server running
25 type ServerRequiredSuite struct{}
26
27 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
28 }
29
30 func (s *ServerRequiredSuite) SetUpTest(c *C) {
31         arvadostest.ResetEnv()
32         srcKeepServicesJSON = ""
33         dstKeepServicesJSON = ""
34 }
35
36 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
37         arvadostest.StopKeep()
38         arvadostest.StopAPI()
39 }
40
41 // Testing keep-rsync needs two sets of keep services: src and dst.
42 // The test setup hence tweaks keep-rsync initialzation to achieve this.
43 // First invoke initializeKeepRsync and then invoke StartKeepAdditional
44 // to create the keep servers to be used as destination.
45 func setupRsync(c *C) {
46         // srcConfig
47         srcConfig.APIHost = os.Getenv("ARVADOS_API_HOST")
48         srcConfig.APIToken = os.Getenv("ARVADOS_API_TOKEN")
49         srcConfig.APIHostInsecure = matchTrue.MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE"))
50
51         // dstConfig
52         dstConfig.APIHost = os.Getenv("ARVADOS_API_HOST")
53         dstConfig.APIToken = os.Getenv("ARVADOS_API_TOKEN")
54         dstConfig.APIHostInsecure = matchTrue.MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE"))
55
56         replications = 1
57
58         // Start API and Keep servers
59         arvadostest.StartAPI()
60         arvadostest.StartKeep()
61
62         // initialize keep-rsync
63         err := initializeKeepRsync()
64         c.Assert(err, Equals, nil)
65
66         // Create two more keep servers to be used as destination
67         arvadostest.StartKeepAdditional(true)
68
69         // load kcDst
70         kcDst, err = keepclient.MakeKeepClient(&arvDst)
71         c.Assert(err, Equals, nil)
72         kcDst.Want_replicas = 1
73 }
74
75 // Test readConfigFromFile method
76 func (s *ServerRequiredSuite) TestReadConfigFromFile(c *C) {
77         // Setup a test config file
78         file, err := ioutil.TempFile(os.TempDir(), "config")
79         c.Assert(err, Equals, nil)
80         defer os.Remove(file.Name())
81
82         fileContent := "ARVADOS_API_HOST=testhost\n"
83         fileContent += "ARVADOS_API_TOKEN=testtoken\n"
84         fileContent += "ARVADOS_API_HOST_INSECURE=true"
85
86         _, err = file.Write([]byte(fileContent))
87
88         // Invoke readConfigFromFile method with this test filename
89         config, err := readConfigFromFile(file.Name())
90         c.Assert(err, Equals, nil)
91         c.Assert(config.APIHost, Equals, "testhost")
92         c.Assert(config.APIToken, Equals, "testtoken")
93         c.Assert(config.APIHostInsecure, Equals, true)
94         c.Assert(config.ExternalClient, Equals, false)
95 }
96
97 // Test keep-rsync initialization, with src and dst keep servers.
98 // Do a Put and Get in src, both of which should succeed.
99 // Do a Put and Get in dst, both of which should succeed.
100 // Do a Get in dst for the src hash, which should raise block not found error.
101 // Do a Get in src for the dst hash, which should raise block not found error.
102 func (s *ServerRequiredSuite) TestRsyncPutInOne_GetFromOtherShouldFail(c *C) {
103         setupRsync(c)
104
105         // Put a block in src using kcSrc and Get it
106         srcData := []byte("test-data1")
107         locatorInSrc := fmt.Sprintf("%x", md5.Sum(srcData))
108
109         hash, rep, err := kcSrc.PutB(srcData)
110         c.Check(hash, Matches, fmt.Sprintf(`^%s\+10(\+.+)?$`, locatorInSrc))
111         c.Check(rep, Equals, 2)
112         c.Check(err, Equals, nil)
113
114         reader, blocklen, _, err := kcSrc.Get(locatorInSrc)
115         c.Assert(err, Equals, nil)
116         c.Check(blocklen, Equals, int64(10))
117         all, err := ioutil.ReadAll(reader)
118         c.Check(all, DeepEquals, srcData)
119
120         // Put a different block in src using kcSrc and Get it
121         dstData := []byte("test-data2")
122         locatorInDst := fmt.Sprintf("%x", md5.Sum(dstData))
123
124         hash, rep, err = kcDst.PutB(dstData)
125         c.Check(hash, Matches, fmt.Sprintf(`^%s\+10(\+.+)?$`, locatorInDst))
126         c.Check(rep, Equals, 1)
127         c.Check(err, Equals, nil)
128
129         reader, blocklen, _, err = kcDst.Get(locatorInDst)
130         c.Assert(err, Equals, nil)
131         c.Check(blocklen, Equals, int64(10))
132         all, err = ioutil.ReadAll(reader)
133         c.Check(all, DeepEquals, dstData)
134
135         // Get srcLocator using kcDst should fail with NotFound error
136         _, _, _, err = kcDst.Get(locatorInSrc)
137         c.Assert(err.Error(), Equals, "Block not found")
138
139         // Get dstLocator using kcSrc should fail with NotFound error
140         _, _, _, err = kcSrc.Get(locatorInDst)
141         c.Assert(err.Error(), Equals, "Block not found")
142 }
143
144 // Test keep-rsync initialization, with srcKeepServicesJSON
145 func (s *ServerRequiredSuite) TestRsyncInitializeWithKeepServicesJSON(c *C) {
146         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 }"
147
148         setupRsync(c)
149
150         localRoots := kcSrc.LocalRoots()
151         c.Check(localRoots != nil, Equals, true)
152
153         foundIt := false
154         for k := range localRoots {
155                 if k == "zzzzz-bi6l4-123456789012340" {
156                         foundIt = true
157                 }
158         }
159         c.Check(foundIt, Equals, true)
160
161         foundIt = false
162         for k := range localRoots {
163                 if k == "zzzzz-bi6l4-123456789012341" {
164                         foundIt = true
165                 }
166         }
167         c.Check(foundIt, Equals, true)
168 }
169
170 // Put 5 blocks in src. Put 2 of those blocks in dst
171 // Hence there are 3 additional blocks in src
172 // Also, put 2 extra blocks in dts; they are hence only in dst
173 // Run rsync and verify that those 7 blocks are now available in dst
174 func (s *ServerRequiredSuite) TestKeepRsync(c *C) {
175         setupRsync(c)
176
177         // Put a few blocks in src using kcSrc
178         var srcLocators []string
179         for i := 0; i < 5; i++ {
180                 data := []byte(fmt.Sprintf("test-data-%d", i))
181                 hash := fmt.Sprintf("%x", md5.Sum(data))
182
183                 hash2, rep, err := kcSrc.PutB(data)
184                 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+11(\+.+)?$`, hash))
185                 c.Check(rep, Equals, 2)
186                 c.Check(err, Equals, nil)
187
188                 reader, blocklen, _, err := kcSrc.Get(hash)
189                 c.Assert(err, Equals, nil)
190                 c.Check(blocklen, Equals, int64(11))
191                 all, err := ioutil.ReadAll(reader)
192                 c.Check(all, DeepEquals, data)
193
194                 srcLocators = append(srcLocators, fmt.Sprintf("%s+%d", hash, blocklen))
195         }
196
197         // Put just two of those blocks in dst using kcDst
198         var dstLocators []string
199         for i := 0; i < 2; i++ {
200                 data := []byte(fmt.Sprintf("test-data-%d", i))
201                 hash := fmt.Sprintf("%x", md5.Sum(data))
202
203                 hash2, rep, err := kcDst.PutB(data)
204                 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+11(\+.+)?$`, hash))
205                 c.Check(rep, Equals, 1)
206                 c.Check(err, Equals, nil)
207
208                 reader, blocklen, _, err := kcDst.Get(hash)
209                 c.Assert(err, Equals, nil)
210                 c.Check(blocklen, Equals, int64(11))
211                 all, err := ioutil.ReadAll(reader)
212                 c.Check(all, DeepEquals, data)
213
214                 dstLocators = append(dstLocators, fmt.Sprintf("%s+%d", hash, blocklen))
215         }
216
217         // Put two more blocks in dst; they are not in src at all
218         var extraDstLocators []string
219         for i := 0; i < 2; i++ {
220                 data := []byte(fmt.Sprintf("other-data-%d", i))
221                 hash := fmt.Sprintf("%x", md5.Sum(data))
222
223                 hash2, rep, err := kcDst.PutB(data)
224                 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+12(\+.+)?$`, hash))
225                 c.Check(rep, Equals, 1)
226                 c.Check(err, Equals, nil)
227
228                 reader, blocklen, _, err := kcDst.Get(hash)
229                 c.Assert(err, Equals, nil)
230                 c.Check(blocklen, Equals, int64(12))
231                 all, err := ioutil.ReadAll(reader)
232                 c.Check(all, DeepEquals, data)
233
234                 extraDstLocators = append(extraDstLocators, fmt.Sprintf("%s+%d", hash, blocklen))
235         }
236
237         err := performKeepRsync()
238         c.Check(err, Equals, nil)
239
240         // Now GetIndex from dst and verify that all 5 from src and the 2 extra blocks are found
241         dstIndex, err := getUniqueLocators(kcDst, "")
242         c.Check(err, Equals, nil)
243         for _, locator := range srcLocators {
244                 _, ok := dstIndex[locator]
245                 c.Assert(ok, Equals, true)
246         }
247         for _, locator := range extraDstLocators {
248                 _, ok := dstIndex[locator]
249                 c.Assert(ok, Equals, true)
250         }
251 }