4 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
5 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
20 // Gocheck boilerplate
21 func Test(t *testing.T) {
25 var _ = Suite(&TestSuite{})
26 var _ = Suite(&MockArvadosServerSuite{})
28 type TestSuite struct{}
29 type MockArvadosServerSuite struct{}
31 var initialArgs []string
33 func (s *TestSuite) SetUpSuite(c *C) {
35 arvadostest.StartAPI()
38 func (s *TestSuite) TearDownSuite(c *C) {
42 func (s *TestSuite) SetUpTest(c *C) {
43 args := []string{"crunch-dispatch-local"}
47 arv, err = arvadosclient.MakeArvadosClient()
49 c.Fatalf("Error making arvados client: %s", err)
53 func (s *TestSuite) TearDownTest(c *C) {
54 arvadostest.ResetEnv()
58 func (s *MockArvadosServerSuite) TearDownTest(c *C) {
59 arvadostest.ResetEnv()
62 func (s *TestSuite) Test_doMain(c *C) {
63 args := []string{"-poll-interval", "2", "-container-priority-poll-interval", "1", "-crunch-run-command", "echo"}
64 os.Args = append(os.Args, args...)
67 time.Sleep(5 * time.Second)
68 sigChan <- syscall.SIGINT
74 // There should be no queued containers now
75 params := arvadosclient.Dict{
76 "filters": [][]string{[]string{"state", "=", "Queued"}},
78 var containers ContainerList
79 err = arv.List("containers", params, &containers)
81 c.Assert(len(containers.Items), Equals, 0)
83 // Previously "Queued" container should now be in "Complete" state
84 var container Container
85 err = arv.Get("containers", "zzzzz-dz642-queuedcontainer", nil, &container)
87 c.Check(container.State, Equals, "Complete")
90 func (s *MockArvadosServerSuite) Test_APIErrorGettingContainers(c *C) {
91 apiStubResponses := make(map[string]arvadostest.StubResponse)
92 apiStubResponses["/arvados/v1/containers"] = arvadostest.StubResponse{500, string(`{}`)}
94 testWithServerStub(c, apiStubResponses, "echo", "Error getting list of queued containers")
97 func (s *MockArvadosServerSuite) Test_APIErrorUpdatingContainerState(c *C) {
98 apiStubResponses := make(map[string]arvadostest.StubResponse)
99 apiStubResponses["/arvados/v1/containers"] =
100 arvadostest.StubResponse{200, string(`{"items_available":1, "items":[{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx1"}]}`)}
101 apiStubResponses["/arvados/v1/containers/zzzzz-dz642-xxxxxxxxxxxxxx1"] =
102 arvadostest.StubResponse{500, string(`{}`)}
104 testWithServerStub(c, apiStubResponses, "echo", "Error updating container state")
107 func (s *MockArvadosServerSuite) Test_ContainerStillInRunningAfterRun(c *C) {
108 apiStubResponses := make(map[string]arvadostest.StubResponse)
109 apiStubResponses["/arvados/v1/containers"] =
110 arvadostest.StubResponse{200, string(`{"items_available":1, "items":[{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx2"}]}`)}
111 apiStubResponses["/arvados/v1/containers/zzzzz-dz642-xxxxxxxxxxxxxx2"] =
112 arvadostest.StubResponse{200, string(`{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx2", "state":"Running", "priority":1}`)}
114 testWithServerStub(c, apiStubResponses, "echo",
115 "After crunch-run process termination, the state is still 'Running' for zzzzz-dz642-xxxxxxxxxxxxxx2")
118 func (s *MockArvadosServerSuite) Test_ErrorRunningContainer(c *C) {
119 apiStubResponses := make(map[string]arvadostest.StubResponse)
120 apiStubResponses["/arvados/v1/containers"] =
121 arvadostest.StubResponse{200, string(`{"items_available":1, "items":[{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx3"}]}`)}
122 apiStubResponses["/arvados/v1/containers/zzzzz-dz642-xxxxxxxxxxxxxx3"] =
123 arvadostest.StubResponse{200, string(`{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx3", "state":"Running", "priority":1}`)}
125 testWithServerStub(c, apiStubResponses, "nosuchcommand", "Error running container for zzzzz-dz642-xxxxxxxxxxxxxx3")
128 func testWithServerStub(c *C, apiStubResponses map[string]arvadostest.StubResponse, crunchCmd string, expected string) {
129 apiStub := arvadostest.ServerStub{apiStubResponses}
131 api := httptest.NewServer(&apiStub)
134 arv = arvadosclient.ArvadosClient{
136 ApiServer: api.URL[7:],
138 Client: &http.Client{Transport: &http.Transport{}},
142 tempfile, err := ioutil.TempFile(os.TempDir(), "temp-log-file")
144 defer os.Remove(tempfile.Name())
145 log.SetOutput(tempfile)
148 time.Sleep(2 * time.Second)
149 sigChan <- syscall.SIGTERM
152 runQueuedContainers(1, 1, crunchCmd)
154 // Wait for all running crunch jobs to complete / terminate
157 buf, _ := ioutil.ReadFile(tempfile.Name())
158 c.Check(strings.Contains(string(buf), expected), Equals, true)