1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
5 // package service provides a cmd.Handler that brings up a system service.
21 "git.arvados.org/arvados.git/sdk/go/arvados"
22 "git.arvados.org/arvados.git/sdk/go/ctxlog"
23 "github.com/prometheus/client_golang/prometheus"
24 check "gopkg.in/check.v1"
27 func Test(t *testing.T) {
31 var _ = check.Suite(&Suite{})
40 func (*Suite) TestGetListenAddress(c *check.C) {
41 // Find an available port on the testing host, so the test
42 // cases don't get confused by "already in use" errors.
43 listener, err := net.Listen("tcp", ":")
44 c.Assert(err, check.IsNil)
45 _, unusedPort, err := net.SplitHostPort(listener.Addr().String())
46 c.Assert(err, check.IsNil)
49 defer os.Unsetenv("ARVADOS_SERVICE_INTERNAL_URL")
50 for idx, trial := range []struct {
51 // internalURL => listenURL, both with trailing "/"
52 // because config loader always adds it
53 internalURLs map[string]string
55 expectErrorMatch string
56 expectLogsMatch string
61 internalURLs: map[string]string{"http://localhost:" + unusedPort + "/": ""},
62 expectListen: "http://localhost:" + unusedPort + "/",
63 expectInternal: "http://localhost:" + unusedPort + "/",
65 { // implicit port 80 in InternalURLs
66 internalURLs: map[string]string{"http://localhost/": ""},
67 expectListen: "http://localhost/",
68 expectInternal: "http://localhost/",
69 expectErrorMatch: `.*:80: bind: permission denied`,
71 { // implicit port 443 in InternalURLs
72 internalURLs: map[string]string{"https://host.example/": "http://localhost:" + unusedPort + "/"},
73 expectListen: "http://localhost:" + unusedPort + "/",
74 expectInternal: "https://host.example/",
77 internalURLs: map[string]string{"https://hostname.example/": "http://localhost:8000/"},
78 expectListen: "http://localhost:8000/",
79 expectInternal: "https://hostname.example/",
82 internalURLs: map[string]string{
83 "https://hostname1.example/": "http://localhost:12435/",
84 "https://hostname2.example/": "http://localhost:" + unusedPort + "/",
86 envVar: "https://hostname2.example", // note this works despite missing trailing "/"
87 expectListen: "http://localhost:" + unusedPort + "/",
88 expectInternal: "https://hostname2.example/",
90 { // cannot listen on any of the ListenURLs
91 internalURLs: map[string]string{
92 "https://hostname1.example/": "http://1.2.3.4:" + unusedPort + "/",
93 "https://hostname2.example/": "http://1.2.3.4:" + unusedPort + "/",
95 expectErrorMatch: "configuration does not enable the \"arvados-controller\" service on this host",
97 { // cannot listen on any of the (implied) ListenURLs
98 internalURLs: map[string]string{
99 "https://1.2.3.4/": "",
100 "https://1.2.3.5/": "",
102 expectErrorMatch: "configuration does not enable the \"arvados-controller\" service on this host",
104 { // impossible port number
105 internalURLs: map[string]string{
106 "https://host.example/": "http://0.0.0.0:1234567",
108 expectErrorMatch: `.*:1234567: listen tcp: address 1234567: invalid port`,
111 // env var URL not mentioned in config = obey env var, with warning
112 internalURLs: map[string]string{"https://hostname1.example/": "http://localhost:8000/"},
113 envVar: "https://hostname2.example",
114 expectListen: "https://hostname2.example/",
115 expectInternal: "https://hostname2.example/",
116 expectLogsMatch: `.*\Qpossible configuration error: listening on https://hostname2.example/ (from $ARVADOS_SERVICE_INTERNAL_URL) even though configuration does not have a matching InternalURLs entry\E.*\n`,
119 // env var + empty config = obey env var, with warning
120 envVar: "https://hostname.example",
121 expectListen: "https://hostname.example/",
122 expectInternal: "https://hostname.example/",
123 expectLogsMatch: `.*\Qpossible configuration error: listening on https://hostname.example/ (from $ARVADOS_SERVICE_INTERNAL_URL) even though configuration does not have a matching InternalURLs entry\E.*\n`,
126 c.Logf("trial %d %+v", idx, trial)
127 os.Setenv("ARVADOS_SERVICE_INTERNAL_URL", trial.envVar)
128 var logbuf bytes.Buffer
129 log := ctxlog.New(&logbuf, "text", "info")
130 services := arvados.Services{Controller: arvados.Service{InternalURLs: map[arvados.URL]arvados.ServiceInstance{}}}
131 for k, v := range trial.internalURLs {
132 u, err := url.Parse(k)
133 c.Assert(err, check.IsNil)
134 si := arvados.ServiceInstance{}
136 u, err := url.Parse(v)
137 c.Assert(err, check.IsNil)
138 si.ListenURL = arvados.URL(*u)
140 services.Controller.InternalURLs[arvados.URL(*u)] = si
142 listenURL, internalURL, err := getListenAddr(services, "arvados-controller", log)
143 if trial.expectLogsMatch != "" {
144 c.Check(logbuf.String(), check.Matches, trial.expectLogsMatch)
146 if trial.expectErrorMatch != "" {
147 c.Check(err, check.ErrorMatches, trial.expectErrorMatch)
150 if !c.Check(err, check.IsNil) {
153 c.Check(listenURL.String(), check.Equals, trial.expectListen)
154 c.Check(internalURL.String(), check.Equals, trial.expectInternal)
158 func (*Suite) TestCommand(c *check.C) {
159 cf, err := ioutil.TempFile("", "cmd_test.")
160 c.Assert(err, check.IsNil)
161 defer os.Remove(cf.Name())
163 fmt.Fprintf(cf, "Clusters:\n zzzzz:\n SystemRootToken: abcde\n NodeProfiles: {\"*\": {\"arvados-controller\": {Listen: \":1234\"}}}")
165 healthCheck := make(chan bool, 1)
166 ctx, cancel := context.WithCancel(context.Background())
169 cmd := Command(arvados.ServiceNameController, func(ctx context.Context, _ *arvados.Cluster, token string, reg *prometheus.Registry) Handler {
170 c.Check(ctx.Value(contextKey), check.Equals, "bar")
171 c.Check(token, check.Equals, "abcde")
172 return &testHandler{ctx: ctx, healthCheck: healthCheck}
174 cmd.(*command).ctx = context.WithValue(ctx, contextKey, "bar")
176 done := make(chan bool)
177 var stdin, stdout, stderr bytes.Buffer
180 cmd.RunCommand("arvados-controller", []string{"-config", cf.Name()}, &stdin, &stdout, &stderr)
186 c.Error("command exited without health check")
189 c.Check(stdout.String(), check.Equals, "")
190 c.Check(stderr.String(), check.Matches, `(?ms).*"msg":"CheckHealth called".*`)
193 func (*Suite) TestTLS(c *check.C) {
194 cwd, err := os.Getwd()
195 c.Assert(err, check.IsNil)
197 stdin := bytes.NewBufferString(`
200 SystemRootToken: abcde
203 ExternalURL: "https://localhost:12345"
204 InternalURLs: {"https://localhost:12345": {}}
206 Key: file://` + cwd + `/../../services/api/tmp/self-signed.key
207 Certificate: file://` + cwd + `/../../services/api/tmp/self-signed.pem
210 called := make(chan bool)
211 cmd := Command(arvados.ServiceNameController, func(ctx context.Context, _ *arvados.Cluster, token string, reg *prometheus.Registry) Handler {
212 return &testHandler{handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
213 w.Write([]byte("ok"))
218 exited := make(chan bool)
219 var stdout, stderr bytes.Buffer
221 cmd.RunCommand("arvados-controller", []string{"-config", "-"}, stdin, &stdout, &stderr)
224 got := make(chan bool)
227 client := &http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
228 for range time.NewTicker(time.Millisecond).C {
229 resp, err := client.Get("https://localhost:12345")
234 body, err := ioutil.ReadAll(resp.Body)
235 c.Check(err, check.IsNil)
236 c.Logf("status %d, body %s", resp.StatusCode, string(body))
237 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
244 c.Error("command exited without calling handler")
245 case <-time.After(time.Second):
251 c.Error("command exited before client received response")
252 case <-time.After(time.Second):
255 c.Log(stderr.String())
258 type testHandler struct {
261 healthCheck chan bool
264 func (th *testHandler) Done() <-chan struct{} { return nil }
265 func (th *testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { th.handler.ServeHTTP(w, r) }
266 func (th *testHandler) CheckHealth() error {
267 ctxlog.FromContext(th.ctx).Info("CheckHealth called")
269 case th.healthCheck <- true: