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.
19 "git.curoverse.com/arvados.git/sdk/go/arvados"
20 "git.curoverse.com/arvados.git/sdk/go/ctxlog"
21 "github.com/prometheus/client_golang/prometheus"
22 check "gopkg.in/check.v1"
25 func Test(t *testing.T) {
29 var _ = check.Suite(&Suite{})
33 func (*Suite) TestCommand(c *check.C) {
34 cf, err := ioutil.TempFile("", "cmd_test.")
35 c.Assert(err, check.IsNil)
36 defer os.Remove(cf.Name())
38 fmt.Fprintf(cf, "Clusters:\n zzzzz:\n SystemRootToken: abcde\n NodeProfiles: {\"*\": {\"arvados-controller\": {Listen: \":1234\"}}}")
40 healthCheck := make(chan bool, 1)
41 ctx, cancel := context.WithCancel(context.Background())
44 cmd := Command(arvados.ServiceNameController, func(ctx context.Context, _ *arvados.Cluster, token string, reg *prometheus.Registry) Handler {
45 c.Check(ctx.Value("foo"), check.Equals, "bar")
46 c.Check(token, check.Equals, "abcde")
47 return &testHandler{ctx: ctx, healthCheck: healthCheck}
49 cmd.(*command).ctx = context.WithValue(ctx, "foo", "bar")
51 done := make(chan bool)
52 var stdin, stdout, stderr bytes.Buffer
55 cmd.RunCommand("arvados-controller", []string{"-config", cf.Name()}, &stdin, &stdout, &stderr)
61 c.Error("command exited without health check")
64 c.Check(stdout.String(), check.Equals, "")
65 c.Check(stderr.String(), check.Matches, `(?ms).*"msg":"CheckHealth called".*`)
68 func (*Suite) TestTLS(c *check.C) {
69 cwd, err := os.Getwd()
70 c.Assert(err, check.IsNil)
72 stdin := bytes.NewBufferString(`
75 SystemRootToken: abcde
78 ExternalURL: "https://localhost:12345"
79 InternalURLs: {"https://localhost:12345": {}}
81 Key: file://` + cwd + `/../../services/api/tmp/self-signed.key
82 Certificate: file://` + cwd + `/../../services/api/tmp/self-signed.pem
85 called := make(chan bool)
86 cmd := Command(arvados.ServiceNameController, func(ctx context.Context, _ *arvados.Cluster, token string, reg *prometheus.Registry) Handler {
87 return &testHandler{handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
93 exited := make(chan bool)
94 var stdout, stderr bytes.Buffer
96 cmd.RunCommand("arvados-controller", []string{"-config", "-"}, stdin, &stdout, &stderr)
99 got := make(chan bool)
102 client := &http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
103 for range time.NewTicker(time.Millisecond).C {
104 resp, err := client.Get("https://localhost:12345")
109 body, err := ioutil.ReadAll(resp.Body)
110 c.Logf("status %d, body %s", resp.StatusCode, string(body))
111 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
118 c.Error("command exited without calling handler")
119 case <-time.After(time.Second):
125 c.Error("command exited before client received response")
126 case <-time.After(time.Second):
129 c.Log(stderr.String())
132 type testHandler struct {
135 healthCheck chan bool
138 func (th *testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { th.handler.ServeHTTP(w, r) }
139 func (th *testHandler) CheckHealth() error {
140 ctxlog.FromContext(th.ctx).Info("CheckHealth called")
142 case th.healthCheck <- true: