7167: add --keep-enforce-permissions to run_test_servers.py
[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.StartKeepWithParams(true, false)
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 }