Merge branch '16745-keep-web-cache'
[arvados.git] / lib / config / deprecated_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package config
6
7 import (
8         "flag"
9         "fmt"
10         "io/ioutil"
11         "os"
12         "time"
13
14         "git.arvados.org/arvados.git/sdk/go/arvados"
15         check "gopkg.in/check.v1"
16 )
17
18 // Configured at: sdk/python/tests/run_test_server.py
19 const TestServerManagementToken = "e687950a23c3a9bceec28c6223a06c79"
20
21 func testLoadLegacyConfig(content []byte, mungeFlag string, c *check.C) (*arvados.Cluster, error) {
22         tmpfile, err := ioutil.TempFile("", "example")
23         if err != nil {
24                 return nil, err
25         }
26         defer os.Remove(tmpfile.Name())
27
28         if _, err := tmpfile.Write(content); err != nil {
29                 return nil, err
30         }
31         if err := tmpfile.Close(); err != nil {
32                 return nil, err
33         }
34         flags := flag.NewFlagSet("test", flag.ExitOnError)
35         ldr := testLoader(c, "Clusters: {zzzzz: {}}", nil)
36         ldr.SetupFlags(flags)
37         args := ldr.MungeLegacyConfigArgs(ldr.Logger, []string{"-config", tmpfile.Name()}, mungeFlag)
38         flags.Parse(args)
39         cfg, err := ldr.Load()
40         if err != nil {
41                 return nil, err
42         }
43         cluster, err := cfg.GetCluster("")
44         if err != nil {
45                 return nil, err
46         }
47         return cluster, nil
48 }
49
50 func (s *LoadSuite) TestDeprecatedNodeProfilesToServices(c *check.C) {
51         hostname, err := os.Hostname()
52         c.Assert(err, check.IsNil)
53         checkEquivalent(c, `
54 Clusters:
55  z1111:
56   NodeProfiles:
57    "*":
58     arvados-controller:
59      listen: ":9004"
60    `+hostname+`:
61     arvados-api-server:
62      listen: ":8000"
63    dispatch-host:
64     arvados-dispatch-cloud:
65      listen: ":9006"
66 `, `
67 Clusters:
68  z1111:
69   Services:
70    RailsAPI:
71     InternalURLs:
72      "http://localhost:8000": {}
73    Controller:
74     InternalURLs:
75      "http://localhost:9004": {}
76    DispatchCloud:
77     InternalURLs:
78      "http://dispatch-host:9006": {}
79   NodeProfiles:
80    "*":
81     arvados-controller:
82      listen: ":9004"
83    `+hostname+`:
84     arvados-api-server:
85      listen: ":8000"
86    dispatch-host:
87     arvados-dispatch-cloud:
88      listen: ":9006"
89 `)
90 }
91
92 func (s *LoadSuite) TestDeprecatedLoginBackend(c *check.C) {
93         checkEquivalent(c, `
94 Clusters:
95  z1111:
96   Login:
97    GoogleClientID: aaaa
98    GoogleClientSecret: bbbb
99    GoogleAlternateEmailAddresses: true
100 `, `
101 Clusters:
102  z1111:
103   Login:
104    Google:
105     Enable: true
106     ClientID: aaaa
107     ClientSecret: bbbb
108     AlternateEmailAddresses: true
109 `)
110         checkEquivalent(c, `
111 Clusters:
112  z1111:
113   Login:
114    ProviderAppID: aaaa
115    ProviderAppSecret: bbbb
116 `, `
117 Clusters:
118  z1111:
119   Login:
120    SSO:
121     Enable: true
122     ProviderAppID: aaaa
123     ProviderAppSecret: bbbb
124 `)
125 }
126
127 func (s *LoadSuite) TestLegacyKeepWebConfig(c *check.C) {
128         content := []byte(`
129 {
130         "Client": {
131                 "Scheme": "",
132                 "APIHost": "example.com",
133                 "AuthToken": "abcdefg",
134         },
135         "Listen": ":80",
136         "AnonymousTokens": [
137                 "anonusertoken"
138         ],
139         "AttachmentOnlyHost": "download.example.com",
140         "TrustAllContent": true,
141         "Cache": {
142                 "TTL": "1m",
143                 "UUIDTTL": "1s",
144                 "MaxCollectionEntries": 42,
145                 "MaxCollectionBytes": 1234567890,
146                 "MaxPermissionEntries": 100,
147                 "MaxUUIDEntries": 100
148         },
149         "ManagementToken": "xyzzy"
150 }
151 `)
152         cluster, err := testLoadLegacyConfig(content, "-legacy-keepweb-config", c)
153         c.Assert(err, check.IsNil)
154
155         c.Check(cluster.Services.Controller.ExternalURL, check.Equals, arvados.URL{Scheme: "https", Host: "example.com", Path: "/"})
156         c.Check(cluster.SystemRootToken, check.Equals, "abcdefg")
157
158         c.Check(cluster.Collections.WebDAVCache.TTL, check.Equals, arvados.Duration(60*time.Second))
159         c.Check(cluster.Collections.WebDAVCache.UUIDTTL, check.Equals, arvados.Duration(time.Second))
160         c.Check(cluster.Collections.WebDAVCache.MaxCollectionEntries, check.Equals, 42)
161         c.Check(cluster.Collections.WebDAVCache.MaxCollectionBytes, check.Equals, int64(1234567890))
162         c.Check(cluster.Collections.WebDAVCache.MaxPermissionEntries, check.Equals, 100)
163         c.Check(cluster.Collections.WebDAVCache.MaxUUIDEntries, check.Equals, 100)
164
165         c.Check(cluster.Services.WebDAVDownload.ExternalURL, check.Equals, arvados.URL{Host: "download.example.com", Path: "/"})
166         c.Check(cluster.Services.WebDAVDownload.InternalURLs[arvados.URL{Host: ":80"}], check.NotNil)
167         c.Check(cluster.Services.WebDAV.InternalURLs[arvados.URL{Host: ":80"}], check.NotNil)
168
169         c.Check(cluster.Collections.TrustAllContent, check.Equals, true)
170         c.Check(cluster.Users.AnonymousUserToken, check.Equals, "anonusertoken")
171         c.Check(cluster.ManagementToken, check.Equals, "xyzzy")
172 }
173
174 // Tests fix for https://dev.arvados.org/issues/15642
175 func (s *LoadSuite) TestLegacyKeepWebConfigDoesntDisableMissingItems(c *check.C) {
176         content := []byte(`
177 {
178         "Client": {
179                 "Scheme": "",
180                 "APIHost": "example.com",
181                 "AuthToken": "abcdefg",
182         }
183 }
184 `)
185         cluster, err := testLoadLegacyConfig(content, "-legacy-keepweb-config", c)
186         c.Assert(err, check.IsNil)
187         // The resulting ManagementToken should be the one set up on the test server.
188         c.Check(cluster.ManagementToken, check.Equals, TestServerManagementToken)
189 }
190
191 func (s *LoadSuite) TestLegacyKeepproxyConfig(c *check.C) {
192         f := "-legacy-keepproxy-config"
193         content := []byte(fmtKeepproxyConfig("", true))
194         cluster, err := testLoadLegacyConfig(content, f, c)
195
196         c.Assert(err, check.IsNil)
197         c.Assert(cluster, check.NotNil)
198         c.Check(cluster.Services.Controller.ExternalURL, check.Equals, arvados.URL{Scheme: "https", Host: "example.com", Path: "/"})
199         c.Check(cluster.SystemRootToken, check.Equals, "abcdefg")
200         c.Check(cluster.ManagementToken, check.Equals, "xyzzy")
201         c.Check(cluster.Services.Keepproxy.InternalURLs[arvados.URL{Host: ":80"}], check.Equals, arvados.ServiceInstance{})
202         c.Check(cluster.Collections.DefaultReplication, check.Equals, 0)
203         c.Check(cluster.API.KeepServiceRequestTimeout.String(), check.Equals, "15s")
204         c.Check(cluster.SystemLogs.LogLevel, check.Equals, "debug")
205
206         content = []byte(fmtKeepproxyConfig("", false))
207         cluster, err = testLoadLegacyConfig(content, f, c)
208         c.Check(err, check.IsNil)
209         c.Check(cluster.SystemLogs.LogLevel, check.Equals, "info")
210
211         content = []byte(fmtKeepproxyConfig(`"DisableGet": true,`, true))
212         _, err = testLoadLegacyConfig(content, f, c)
213         c.Check(err, check.NotNil)
214
215         content = []byte(fmtKeepproxyConfig(`"DisablePut": true,`, true))
216         _, err = testLoadLegacyConfig(content, f, c)
217         c.Check(err, check.NotNil)
218
219         content = []byte(fmtKeepproxyConfig(`"PIDFile": "test",`, true))
220         _, err = testLoadLegacyConfig(content, f, c)
221         c.Check(err, check.NotNil)
222
223         content = []byte(fmtKeepproxyConfig(`"DisableGet": false, "DisablePut": false, "PIDFile": "",`, true))
224         _, err = testLoadLegacyConfig(content, f, c)
225         c.Check(err, check.IsNil)
226 }
227
228 func fmtKeepproxyConfig(param string, debugLog bool) string {
229         return fmt.Sprintf(`
230 {
231         "Client": {
232                 "Scheme": "",
233                 "APIHost": "example.com",
234                 "AuthToken": "abcdefg",
235                 "Insecure": false
236         },
237         "Listen": ":80",
238         "DefaultReplicas": 0,
239         "Timeout": "15s",
240         "Debug": %t,
241         %s
242         "ManagementToken": "xyzzy"
243 }
244 `, debugLog, param)
245 }
246
247 func (s *LoadSuite) TestLegacyArvGitHttpdConfig(c *check.C) {
248         content := []byte(`
249 {
250         "Client": {
251                 "Scheme": "",
252                 "APIHost": "example.com",
253                 "AuthToken": "abcdefg",
254         },
255         "Listen": ":9000",
256         "GitCommand": "/test/git",
257         "GitoliteHome": "/test/gitolite",
258         "RepoRoot": "/test/reporoot",
259         "ManagementToken": "xyzzy"
260 }
261 `)
262         f := "-legacy-git-httpd-config"
263         cluster, err := testLoadLegacyConfig(content, f, c)
264
265         c.Assert(err, check.IsNil)
266         c.Assert(cluster, check.NotNil)
267         c.Check(cluster.Services.Controller.ExternalURL, check.Equals, arvados.URL{Scheme: "https", Host: "example.com", Path: "/"})
268         c.Check(cluster.SystemRootToken, check.Equals, "abcdefg")
269         c.Check(cluster.ManagementToken, check.Equals, "xyzzy")
270         c.Check(cluster.Git.GitCommand, check.Equals, "/test/git")
271         c.Check(cluster.Git.GitoliteHome, check.Equals, "/test/gitolite")
272         c.Check(cluster.Git.Repositories, check.Equals, "/test/reporoot")
273         c.Check(cluster.Services.Keepproxy.InternalURLs[arvados.URL{Host: ":9000"}], check.Equals, arvados.ServiceInstance{})
274 }
275
276 // Tests fix for https://dev.arvados.org/issues/15642
277 func (s *LoadSuite) TestLegacyArvGitHttpdConfigDoesntDisableMissingItems(c *check.C) {
278         content := []byte(`
279 {
280         "Client": {
281                 "Scheme": "",
282                 "APIHost": "example.com",
283                 "AuthToken": "abcdefg",
284         }
285 }
286 `)
287         cluster, err := testLoadLegacyConfig(content, "-legacy-git-httpd-config", c)
288         c.Assert(err, check.IsNil)
289         // The resulting ManagementToken should be the one set up on the test server.
290         c.Check(cluster.ManagementToken, check.Equals, TestServerManagementToken)
291 }
292
293 func (s *LoadSuite) TestLegacyKeepBalanceConfig(c *check.C) {
294         f := "-legacy-keepbalance-config"
295         content := []byte(fmtKeepBalanceConfig(""))
296         cluster, err := testLoadLegacyConfig(content, f, c)
297
298         c.Assert(err, check.IsNil)
299         c.Assert(cluster, check.NotNil)
300         c.Check(cluster.ManagementToken, check.Equals, "xyzzy")
301         c.Check(cluster.Services.Keepbalance.InternalURLs[arvados.URL{Host: ":80"}], check.Equals, arvados.ServiceInstance{})
302         c.Check(cluster.Collections.BalanceCollectionBuffers, check.Equals, 1000)
303         c.Check(cluster.Collections.BalanceCollectionBatch, check.Equals, 100000)
304         c.Check(cluster.Collections.BalancePeriod.String(), check.Equals, "10m")
305         c.Check(cluster.Collections.BlobMissingReport, check.Equals, "testfile")
306         c.Check(cluster.API.KeepServiceRequestTimeout.String(), check.Equals, "30m")
307
308         content = []byte(fmtKeepBalanceConfig(`"KeepServiceTypes":["disk"],`))
309         _, err = testLoadLegacyConfig(content, f, c)
310         c.Check(err, check.IsNil)
311
312         content = []byte(fmtKeepBalanceConfig(`"KeepServiceTypes":[],`))
313         _, err = testLoadLegacyConfig(content, f, c)
314         c.Check(err, check.IsNil)
315
316         content = []byte(fmtKeepBalanceConfig(`"KeepServiceTypes":["proxy"],`))
317         _, err = testLoadLegacyConfig(content, f, c)
318         c.Check(err, check.NotNil)
319
320         content = []byte(fmtKeepBalanceConfig(`"KeepServiceTypes":["disk", "proxy"],`))
321         _, err = testLoadLegacyConfig(content, f, c)
322         c.Check(err, check.NotNil)
323
324         content = []byte(fmtKeepBalanceConfig(`"KeepServiceList":{},`))
325         _, err = testLoadLegacyConfig(content, f, c)
326         c.Check(err, check.NotNil)
327 }
328
329 func fmtKeepBalanceConfig(param string) string {
330         return fmt.Sprintf(`
331 {
332         "Client": {
333                 "Scheme": "",
334                 "APIHost": "example.com",
335                 "AuthToken": "abcdefg",
336                 "Insecure": false
337         },
338         "Listen": ":80",
339         %s
340         "RunPeriod": "10m",
341         "CollectionBatchSize": 100000,
342         "CollectionBuffers": 1000,
343         "RequestTimeout": "30m",
344         "ManagementToken": "xyzzy",
345         "LostBlocksFile": "testfile"
346 }
347 `, param)
348 }