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.
17 "git.curoverse.com/arvados.git/sdk/go/arvados"
18 "git.curoverse.com/arvados.git/sdk/go/ctxlog"
19 check "gopkg.in/check.v1"
22 func Test(t *testing.T) {
26 var _ = check.Suite(&Suite{})
30 func (*Suite) TestCommand(c *check.C) {
31 cf, err := ioutil.TempFile("", "cmd_test.")
32 c.Assert(err, check.IsNil)
33 defer os.Remove(cf.Name())
35 fmt.Fprintf(cf, "Clusters:\n zzzzz:\n SystemRootToken: abcde\n NodeProfiles: {\"*\": {\"arvados-controller\": {Listen: \":1234\"}}}")
37 healthCheck := make(chan bool, 1)
38 ctx, cancel := context.WithCancel(context.Background())
41 cmd := Command(arvados.ServiceNameController, func(ctx context.Context, _ *arvados.Cluster, token string) Handler {
42 c.Check(ctx.Value("foo"), check.Equals, "bar")
43 c.Check(token, check.Equals, "abcde")
44 return &testHandler{ctx: ctx, healthCheck: healthCheck}
46 cmd.(*command).ctx = context.WithValue(ctx, "foo", "bar")
48 done := make(chan bool)
49 var stdin, stdout, stderr bytes.Buffer
52 cmd.RunCommand("arvados-controller", []string{"-config", cf.Name()}, &stdin, &stdout, &stderr)
58 c.Error("command exited without health check")
61 c.Check(stdout.String(), check.Equals, "")
62 c.Check(stderr.String(), check.Matches, `(?ms).*"msg":"CheckHealth called".*`)
65 type testHandler struct {
70 func (th *testHandler) ServeHTTP(http.ResponseWriter, *http.Request) {}
71 func (th *testHandler) CheckHealth() error {
72 ctxlog.FromContext(th.ctx).Info("CheckHealth called")
74 case th.healthCheck <- true: