16 "git.curoverse.com/arvados.git/sdk/go/arvados"
17 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
18 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
19 "git.curoverse.com/arvados.git/sdk/go/dispatch"
23 // Gocheck boilerplate
24 func Test(t *testing.T) {
28 var _ = Suite(&TestSuite{})
29 var _ = Suite(&MockArvadosServerSuite{})
31 type TestSuite struct{}
32 type MockArvadosServerSuite struct{}
34 var initialArgs []string
36 func (s *TestSuite) SetUpSuite(c *C) {
38 arvadostest.StartAPI()
39 runningCmds = make(map[string]*exec.Cmd)
42 func (s *TestSuite) TearDownSuite(c *C) {
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,
71 RunContainer: func(d *dispatch.Dispatcher, c arvados.Container, s <-chan arvados.Container) {
77 startCmd = func(container arvados.Container, cmd *exec.Cmd) error {
78 dispatcher.UpdateState(container.UUID, "Running")
79 dispatcher.UpdateState(container.UUID, "Complete")
83 err = dispatcher.Run(ctx)
84 c.Assert(err, Equals, context.Canceled)
86 // Wait for all running crunch jobs to complete / terminate
89 // There should be no queued containers now
90 params := arvadosclient.Dict{
91 "filters": [][]string{{"state", "=", "Queued"}},
93 var containers arvados.ContainerList
94 err = arv.List("containers", params, &containers)
96 c.Assert(len(containers.Items), Equals, 0)
98 // Previously "Queued" container should now be in "Complete" state
99 var container arvados.Container
100 err = arv.Get("containers", "zzzzz-dz642-queuedcontainer", nil, &container)
102 c.Check(string(container.State), Equals, "Complete")
105 func (s *MockArvadosServerSuite) Test_APIErrorGettingContainers(c *C) {
106 apiStubResponses := make(map[string]arvadostest.StubResponse)
107 apiStubResponses["/arvados/v1/containers"] = arvadostest.StubResponse{500, string(`{}`)}
109 testWithServerStub(c, apiStubResponses, "echo", "Error getting list of containers")
112 func (s *MockArvadosServerSuite) Test_APIErrorUpdatingContainerState(c *C) {
113 apiStubResponses := make(map[string]arvadostest.StubResponse)
114 apiStubResponses["/arvados/v1/containers"] =
115 arvadostest.StubResponse{200, string(`{"items_available":1, "items":[{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx1","State":"Queued","Priority":1}]}`)}
116 apiStubResponses["/arvados/v1/containers/zzzzz-dz642-xxxxxxxxxxxxxx1"] =
117 arvadostest.StubResponse{500, string(`{}`)}
119 testWithServerStub(c, apiStubResponses, "echo", "error locking container zzzzz-dz642-xxxxxxxxxxxxxx1")
122 func (s *MockArvadosServerSuite) Test_ContainerStillInRunningAfterRun(c *C) {
123 apiStubResponses := make(map[string]arvadostest.StubResponse)
124 apiStubResponses["/arvados/v1/containers"] =
125 arvadostest.StubResponse{200, string(`{"items_available":1, "items":[{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx2","State":"Queued","Priority":1}]}`)}
126 apiStubResponses["/arvados/v1/containers/zzzzz-dz642-xxxxxxxxxxxxxx2/lock"] =
127 arvadostest.StubResponse{200, string(`{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx2", "state":"Locked", "priority":1, "locked_by_uuid": "` + arvadostest.Dispatch1AuthUUID + `"}`)}
128 apiStubResponses["/arvados/v1/containers/zzzzz-dz642-xxxxxxxxxxxxxx2"] =
129 arvadostest.StubResponse{200, string(`{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx2", "state":"Running", "priority":1, "locked_by_uuid": "` + arvadostest.Dispatch1AuthUUID + `"}`)}
131 testWithServerStub(c, apiStubResponses, "echo",
132 `After echo process termination, container state for Running is "zzzzz-dz642-xxxxxxxxxxxxxx2". Updating it to "Cancelled"`)
135 func (s *MockArvadosServerSuite) Test_ErrorRunningContainer(c *C) {
136 apiStubResponses := make(map[string]arvadostest.StubResponse)
137 apiStubResponses["/arvados/v1/containers"] =
138 arvadostest.StubResponse{200, string(`{"items_available":1, "items":[{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx3","State":"Queued","Priority":1}]}`)}
140 apiStubResponses["/arvados/v1/containers/zzzzz-dz642-xxxxxxxxxxxxxx3/lock"] =
141 arvadostest.StubResponse{200, string(`{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx3", "state":"Locked", "priority":1}`)}
143 testWithServerStub(c, apiStubResponses, "nosuchcommand", "Error starting nosuchcommand for zzzzz-dz642-xxxxxxxxxxxxxx3")
146 func testWithServerStub(c *C, apiStubResponses map[string]arvadostest.StubResponse, crunchCmd string, expected string) {
147 apiStubResponses["/arvados/v1/api_client_authorizations/current"] =
148 arvadostest.StubResponse{200, string(`{"uuid": "` + arvadostest.Dispatch1AuthUUID + `", "api_token": "xyz"}`)}
150 apiStub := arvadostest.ServerStub{apiStubResponses}
152 api := httptest.NewServer(&apiStub)
155 arv := &arvadosclient.ArvadosClient{
157 ApiServer: api.URL[7:],
159 Client: &http.Client{Transport: &http.Transport{}},
163 buf := bytes.NewBuffer(nil)
164 log.SetOutput(io.MultiWriter(buf, os.Stderr))
165 defer log.SetOutput(os.Stderr)
167 *crunchRunCommand = crunchCmd
169 ctx, cancel := context.WithCancel(context.Background())
170 dispatcher := dispatch.Dispatcher{
172 PollPeriod: time.Duration(1) * time.Second,
173 RunContainer: func(d *dispatch.Dispatcher, c arvados.Container, s <-chan arvados.Container) {
179 startCmd = func(container arvados.Container, cmd *exec.Cmd) error {
180 dispatcher.UpdateState(container.UUID, "Running")
181 dispatcher.UpdateState(container.UUID, "Complete")
186 for i := 0; i < 80 && !strings.Contains(buf.String(), expected); i++ {
187 time.Sleep(100 * time.Millisecond)
192 err := dispatcher.Run(ctx)
193 c.Assert(err, Equals, context.Canceled)
195 // Wait for all running crunch jobs to complete / terminate
198 c.Check(buf.String(), Matches, `(?ms).*`+expected+`.*`)