1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
19 "git.arvados.org/arvados.git/sdk/go/arvados"
20 "git.arvados.org/arvados.git/sdk/go/arvadosclient"
21 "git.arvados.org/arvados.git/sdk/go/arvadostest"
22 "git.arvados.org/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 runningCmds = make(map[string]*exec.Cmd)
43 logrus.SetFormatter(&logrus.TextFormatter{DisableColors: true})
46 func (s *TestSuite) SetUpTest(c *C) {
47 args := []string{"crunch-dispatch-local"}
51 func (s *TestSuite) TearDownTest(c *C) {
52 arvadostest.ResetEnv()
56 func (s *MockArvadosServerSuite) TearDownTest(c *C) {
57 arvadostest.ResetEnv()
60 func (s *TestSuite) TestIntegration(c *C) {
61 arv, err := arvadosclient.MakeArvadosClient()
65 crunchRunCommand = &echo
67 ctx, cancel := context.WithCancel(context.Background())
68 dispatcher := dispatch.Dispatcher{
70 PollPeriod: time.Second,
73 startCmd := func(container arvados.Container, cmd *exec.Cmd) error {
74 dispatcher.UpdateState(container.UUID, "Running")
75 dispatcher.UpdateState(container.UUID, "Complete")
79 cl := arvados.Cluster{Containers: arvados.ContainersConfig{RuntimeEngine: "docker"}}
81 dispatcher.RunContainer = func(d *dispatch.Dispatcher, c arvados.Container, s <-chan arvados.Container) error {
83 return (&LocalRun{startCmd, make(chan bool, 8), ctx, &cl}).run(d, c, s)
86 err = dispatcher.Run(ctx)
87 c.Assert(err, Equals, context.Canceled)
89 // Wait for all running crunch jobs to complete / terminate
92 // There should be no queued containers now
93 params := arvadosclient.Dict{
94 "filters": [][]string{{"state", "=", "Queued"}},
96 var containers arvados.ContainerList
97 err = arv.List("containers", params, &containers)
99 c.Assert(len(containers.Items), Equals, 0)
101 // Previously "Queued" container should now be in "Complete" state
102 var container arvados.Container
103 err = arv.Get("containers", "zzzzz-dz642-queuedcontainer", nil, &container)
105 c.Check(string(container.State), Equals, "Complete")
108 func (s *MockArvadosServerSuite) Test_APIErrorGettingContainers(c *C) {
109 apiStubResponses := make(map[string]arvadostest.StubResponse)
110 apiStubResponses["/arvados/v1/containers"] = arvadostest.StubResponse{500, string(`{}`)}
112 testWithServerStub(c, apiStubResponses, "echo", "error getting count of containers")
115 func (s *MockArvadosServerSuite) Test_APIErrorUpdatingContainerState(c *C) {
116 apiStubResponses := make(map[string]arvadostest.StubResponse)
117 apiStubResponses["/arvados/v1/containers"] =
118 arvadostest.StubResponse{200, string(`{"items_available":1, "items":[{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx1","State":"Queued","Priority":1}]}`)}
119 apiStubResponses["/arvados/v1/containers/zzzzz-dz642-xxxxxxxxxxxxxx1"] =
120 arvadostest.StubResponse{500, string(`{}`)}
122 testWithServerStub(c, apiStubResponses, "echo", "error locking container zzzzz-dz642-xxxxxxxxxxxxxx1")
125 func (s *MockArvadosServerSuite) Test_ContainerStillInRunningAfterRun(c *C) {
126 apiStubResponses := make(map[string]arvadostest.StubResponse)
127 apiStubResponses["/arvados/v1/containers"] =
128 arvadostest.StubResponse{200, string(`{"items_available":1, "items":[{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx2","State":"Queued","Priority":1}]}`)}
129 apiStubResponses["/arvados/v1/containers/zzzzz-dz642-xxxxxxxxxxxxxx2/lock"] =
130 arvadostest.StubResponse{200, string(`{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx2", "state":"Locked", "priority":1, "locked_by_uuid": "` + arvadostest.Dispatch1AuthUUID + `"}`)}
131 apiStubResponses["/arvados/v1/containers/zzzzz-dz642-xxxxxxxxxxxxxx2"] =
132 arvadostest.StubResponse{200, string(`{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx2", "state":"Running", "priority":1, "locked_by_uuid": "` + arvadostest.Dispatch1AuthUUID + `"}`)}
134 testWithServerStub(c, apiStubResponses, "echo",
135 `after \\"echo\\" process termination, container state for zzzzz-dz642-xxxxxxxxxxxxxx2 is \\"Running\\"; updating it to \\"Cancelled\\"`)
138 func (s *MockArvadosServerSuite) Test_ErrorRunningContainer(c *C) {
139 apiStubResponses := make(map[string]arvadostest.StubResponse)
140 apiStubResponses["/arvados/v1/containers"] =
141 arvadostest.StubResponse{200, string(`{"items_available":1, "items":[{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx3","State":"Queued","Priority":1}]}`)}
143 apiStubResponses["/arvados/v1/containers/zzzzz-dz642-xxxxxxxxxxxxxx3/lock"] =
144 arvadostest.StubResponse{200, string(`{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx3", "state":"Locked", "priority":1}`)}
146 testWithServerStub(c, apiStubResponses, "nosuchcommand", `error starting \\"nosuchcommand\\" for zzzzz-dz642-xxxxxxxxxxxxxx3`)
149 func testWithServerStub(c *C, apiStubResponses map[string]arvadostest.StubResponse, crunchCmd string, expected string) {
150 apiStubResponses["/arvados/v1/api_client_authorizations/current"] =
151 arvadostest.StubResponse{200, string(`{"uuid": "` + arvadostest.Dispatch1AuthUUID + `", "api_token": "xyz"}`)}
153 apiStub := arvadostest.ServerStub{apiStubResponses}
155 api := httptest.NewServer(&apiStub)
158 arv := &arvadosclient.ArvadosClient{
160 ApiServer: api.URL[7:],
162 Client: &http.Client{Transport: &http.Transport{}},
166 buf := bytes.NewBuffer(nil)
167 logrus.SetOutput(io.MultiWriter(buf, os.Stderr))
168 defer logrus.SetOutput(os.Stderr)
170 *crunchRunCommand = crunchCmd
172 ctx, cancel := context.WithCancel(context.Background())
173 dispatcher := dispatch.Dispatcher{
175 PollPeriod: time.Second / 20,
178 startCmd := func(container arvados.Container, cmd *exec.Cmd) error {
179 dispatcher.UpdateState(container.UUID, "Running")
180 dispatcher.UpdateState(container.UUID, "Complete")
184 cl := arvados.Cluster{Containers: arvados.ContainersConfig{RuntimeEngine: "docker"}}
186 dispatcher.RunContainer = func(d *dispatch.Dispatcher, c arvados.Container, s <-chan arvados.Container) error {
188 return (&LocalRun{startCmd, make(chan bool, 8), ctx, &cl}).run(d, c, s)
191 re := regexp.MustCompile(`(?ms).*` + expected + `.*`)
193 for i := 0; i < 80 && !re.MatchString(buf.String()); i++ {
194 time.Sleep(100 * time.Millisecond)
199 err := dispatcher.Run(ctx)
200 c.Assert(err, Equals, context.Canceled)
202 // Wait for all running crunch jobs to complete / terminate
205 c.Check(buf.String(), Matches, `(?ms).*`+expected+`.*`)