5 "git.curoverse.com/arvados.git/sdk/go/arvados"
6 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
7 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
8 "git.curoverse.com/arvados.git/sdk/go/dispatch"
21 // Gocheck boilerplate
22 func Test(t *testing.T) {
26 var _ = Suite(&TestSuite{})
27 var _ = Suite(&MockArvadosServerSuite{})
29 type TestSuite struct{}
30 type MockArvadosServerSuite struct{}
32 var initialArgs []string
34 func (s *TestSuite) SetUpSuite(c *C) {
36 arvadostest.StartAPI()
37 runningCmds = make(map[string]*exec.Cmd)
40 func (s *TestSuite) TearDownSuite(c *C) {
44 func (s *TestSuite) SetUpTest(c *C) {
45 args := []string{"crunch-dispatch-local"}
49 func (s *TestSuite) TearDownTest(c *C) {
50 arvadostest.ResetEnv()
54 func (s *MockArvadosServerSuite) TearDownTest(c *C) {
55 arvadostest.ResetEnv()
58 func (s *TestSuite) TestIntegration(c *C) {
59 arv, err := arvadosclient.MakeArvadosClient()
63 crunchRunCommand = &echo
65 doneProcessing := make(chan struct{})
66 dispatcher := dispatch.Dispatcher{
68 PollInterval: time.Second,
69 RunContainer: func(dispatcher *dispatch.Dispatcher,
70 container arvados.Container,
71 status chan arvados.Container) {
72 run(dispatcher, container, status)
73 doneProcessing <- struct{}{}
75 DoneProcessing: doneProcessing}
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.RunDispatcher()
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 doneProcessing := make(chan struct{})
170 dispatcher := dispatch.Dispatcher{
172 PollInterval: time.Duration(1) * time.Second,
173 RunContainer: func(dispatcher *dispatch.Dispatcher,
174 container arvados.Container,
175 status chan arvados.Container) {
176 run(dispatcher, container, status)
177 doneProcessing <- struct{}{}
179 DoneProcessing: doneProcessing}
181 startCmd = func(container arvados.Container, cmd *exec.Cmd) error {
182 dispatcher.UpdateState(container.UUID, "Running")
183 dispatcher.UpdateState(container.UUID, "Complete")
188 for i := 0; i < 80 && !strings.Contains(buf.String(), expected); i++ {
189 time.Sleep(100 * time.Millisecond)
191 dispatcher.DoneProcessing <- struct{}{}
194 err := dispatcher.RunDispatcher()
197 // Wait for all running crunch jobs to complete / terminate
200 c.Check(buf.String(), Matches, `(?ms).*`+expected+`.*`)