1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
19 "git.curoverse.com/arvados.git/sdk/go/arvados"
20 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
21 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
22 "git.curoverse.com/arvados.git/sdk/go/dispatch"
23 "github.com/sirupsen/logrus"
27 // Gocheck boilerplate
28 func Test(t *testing.T) {
32 var _ = Suite(&TestSuite{})
33 var _ = Suite(&MockArvadosServerSuite{})
35 type TestSuite struct{}
36 type MockArvadosServerSuite struct{}
38 var initialArgs []string
40 func (s *TestSuite) SetUpSuite(c *C) {
42 arvadostest.StartAPI()
43 runningCmds = make(map[string]*exec.Cmd)
44 logrus.SetFormatter(&logrus.TextFormatter{DisableColors: true})
47 func (s *TestSuite) TearDownSuite(c *C) {
51 func (s *TestSuite) SetUpTest(c *C) {
52 args := []string{"crunch-dispatch-local"}
56 func (s *TestSuite) TearDownTest(c *C) {
57 arvadostest.ResetEnv()
61 func (s *MockArvadosServerSuite) TearDownTest(c *C) {
62 arvadostest.ResetEnv()
65 func (s *TestSuite) TestIntegration(c *C) {
66 arv, err := arvadosclient.MakeArvadosClient()
70 crunchRunCommand = &echo
72 ctx, cancel := context.WithCancel(context.Background())
73 dispatcher := dispatch.Dispatcher{
75 PollPeriod: time.Second,
76 RunContainer: func(d *dispatch.Dispatcher, c arvados.Container, s <-chan arvados.Container) {
82 startCmd = func(container arvados.Container, cmd *exec.Cmd) error {
83 dispatcher.UpdateState(container.UUID, "Running")
84 dispatcher.UpdateState(container.UUID, "Complete")
88 err = dispatcher.Run(ctx)
89 c.Assert(err, Equals, context.Canceled)
91 // Wait for all running crunch jobs to complete / terminate
94 // There should be no queued containers now
95 params := arvadosclient.Dict{
96 "filters": [][]string{{"state", "=", "Queued"}},
98 var containers arvados.ContainerList
99 err = arv.List("containers", params, &containers)
101 c.Assert(len(containers.Items), Equals, 0)
103 // Previously "Queued" container should now be in "Complete" state
104 var container arvados.Container
105 err = arv.Get("containers", "zzzzz-dz642-queuedcontainer", nil, &container)
107 c.Check(string(container.State), Equals, "Complete")
110 func (s *MockArvadosServerSuite) Test_APIErrorGettingContainers(c *C) {
111 apiStubResponses := make(map[string]arvadostest.StubResponse)
112 apiStubResponses["/arvados/v1/containers"] = arvadostest.StubResponse{500, string(`{}`)}
114 testWithServerStub(c, apiStubResponses, "echo", "error getting count of containers")
117 func (s *MockArvadosServerSuite) Test_APIErrorUpdatingContainerState(c *C) {
118 apiStubResponses := make(map[string]arvadostest.StubResponse)
119 apiStubResponses["/arvados/v1/containers"] =
120 arvadostest.StubResponse{200, string(`{"items_available":1, "items":[{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx1","State":"Queued","Priority":1}]}`)}
121 apiStubResponses["/arvados/v1/containers/zzzzz-dz642-xxxxxxxxxxxxxx1"] =
122 arvadostest.StubResponse{500, string(`{}`)}
124 testWithServerStub(c, apiStubResponses, "echo", "error locking container zzzzz-dz642-xxxxxxxxxxxxxx1")
127 func (s *MockArvadosServerSuite) Test_ContainerStillInRunningAfterRun(c *C) {
128 apiStubResponses := make(map[string]arvadostest.StubResponse)
129 apiStubResponses["/arvados/v1/containers"] =
130 arvadostest.StubResponse{200, string(`{"items_available":1, "items":[{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx2","State":"Queued","Priority":1}]}`)}
131 apiStubResponses["/arvados/v1/containers/zzzzz-dz642-xxxxxxxxxxxxxx2/lock"] =
132 arvadostest.StubResponse{200, string(`{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx2", "state":"Locked", "priority":1, "locked_by_uuid": "` + arvadostest.Dispatch1AuthUUID + `"}`)}
133 apiStubResponses["/arvados/v1/containers/zzzzz-dz642-xxxxxxxxxxxxxx2"] =
134 arvadostest.StubResponse{200, string(`{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx2", "state":"Running", "priority":1, "locked_by_uuid": "` + arvadostest.Dispatch1AuthUUID + `"}`)}
136 testWithServerStub(c, apiStubResponses, "echo",
137 `after \\"echo\\" process termination, container state for zzzzz-dz642-xxxxxxxxxxxxxx2 is \\"Running\\"; updating it to \\"Cancelled\\"`)
140 func (s *MockArvadosServerSuite) Test_ErrorRunningContainer(c *C) {
141 apiStubResponses := make(map[string]arvadostest.StubResponse)
142 apiStubResponses["/arvados/v1/containers"] =
143 arvadostest.StubResponse{200, string(`{"items_available":1, "items":[{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx3","State":"Queued","Priority":1}]}`)}
145 apiStubResponses["/arvados/v1/containers/zzzzz-dz642-xxxxxxxxxxxxxx3/lock"] =
146 arvadostest.StubResponse{200, string(`{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx3", "state":"Locked", "priority":1}`)}
148 testWithServerStub(c, apiStubResponses, "nosuchcommand", `error starting \\"nosuchcommand\\" for zzzzz-dz642-xxxxxxxxxxxxxx3`)
151 func testWithServerStub(c *C, apiStubResponses map[string]arvadostest.StubResponse, crunchCmd string, expected string) {
152 apiStubResponses["/arvados/v1/api_client_authorizations/current"] =
153 arvadostest.StubResponse{200, string(`{"uuid": "` + arvadostest.Dispatch1AuthUUID + `", "api_token": "xyz"}`)}
155 apiStub := arvadostest.ServerStub{apiStubResponses}
157 api := httptest.NewServer(&apiStub)
160 arv := &arvadosclient.ArvadosClient{
162 ApiServer: api.URL[7:],
164 Client: &http.Client{Transport: &http.Transport{}},
168 buf := bytes.NewBuffer(nil)
169 logrus.SetOutput(io.MultiWriter(buf, os.Stderr))
170 defer logrus.SetOutput(os.Stderr)
172 *crunchRunCommand = crunchCmd
174 ctx, cancel := context.WithCancel(context.Background())
175 dispatcher := dispatch.Dispatcher{
177 PollPeriod: time.Second / 20,
178 RunContainer: func(d *dispatch.Dispatcher, c arvados.Container, s <-chan arvados.Container) {
184 startCmd = func(container arvados.Container, cmd *exec.Cmd) error {
185 dispatcher.UpdateState(container.UUID, "Running")
186 dispatcher.UpdateState(container.UUID, "Complete")
190 re := regexp.MustCompile(`(?ms).*` + expected + `.*`)
192 for i := 0; i < 80 && !re.MatchString(buf.String()); i++ {
193 time.Sleep(100 * time.Millisecond)
198 err := dispatcher.Run(ctx)
199 c.Assert(err, Equals, context.Canceled)
201 // Wait for all running crunch jobs to complete / terminate
204 c.Check(buf.String(), Matches, `(?ms).*`+expected+`.*`)