4 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
5 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
19 // Gocheck boilerplate
20 func Test(t *testing.T) {
24 var _ = Suite(&TestSuite{})
25 var _ = Suite(&MockArvadosServerSuite{})
27 type TestSuite struct{}
28 type MockArvadosServerSuite struct{}
30 var initialArgs []string
32 func (s *TestSuite) SetUpSuite(c *C) {
34 arvadostest.StartAPI()
37 func (s *TestSuite) TearDownSuite(c *C) {
41 func (s *TestSuite) SetUpTest(c *C) {
42 args := []string{"crunch-dispatch-local"}
46 arv, err = arvadosclient.MakeArvadosClient()
48 c.Fatalf("Error making arvados client: %s", err)
52 func (s *TestSuite) TearDownTest(c *C) {
53 arvadostest.ResetEnv()
57 func (s *MockArvadosServerSuite) TearDownTest(c *C) {
58 arvadostest.ResetEnv()
61 func (s *TestSuite) Test_doMain(c *C) {
62 args := []string{"-poll-interval", "2", "-container-priority-poll-interval", "1", "-crunch-run-command", "echo"}
63 os.Args = append(os.Args, args...)
66 time.Sleep(2 * time.Second)
67 doneProcessing <- true
73 // Give some time for run goroutine to complete
74 time.Sleep(1 * time.Second)
76 // There should be no queued containers now
77 params := arvadosclient.Dict{
78 "filters": [][]string{[]string{"state", "=", "Queued"}},
80 var containers ContainerList
81 err = arv.List("containers", params, &containers)
83 c.Assert(len(containers.Items), Equals, 0)
85 // Previously "Queued" container should now be in "Complete" state
86 var container Container
87 err = arv.Get("containers", "zzzzz-dz642-queuedcontainer", nil, &container)
89 c.Check(container.State, Equals, "Complete")
92 func (s *MockArvadosServerSuite) Test_APIErrorGettingContainers(c *C) {
93 apiStubResponses := make(map[string]arvadostest.StubResponse)
94 apiStubResponses["/arvados/v1/containers"] = arvadostest.StubResponse{500, string(`{}`)}
96 testWithServerStub(c, apiStubResponses, "echo", "Error getting list of queued containers")
99 func (s *MockArvadosServerSuite) Test_APIErrorUpdatingContainerState(c *C) {
100 apiStubResponses := make(map[string]arvadostest.StubResponse)
101 apiStubResponses["/arvados/v1/containers"] =
102 arvadostest.StubResponse{200, string(`{"items_available":1, "items":[{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx1"}]}`)}
103 apiStubResponses["/arvados/v1/containers/zzzzz-dz642-xxxxxxxxxxxxxx1"] =
104 arvadostest.StubResponse{500, string(`{}`)}
106 testWithServerStub(c, apiStubResponses, "echo", "Error updating container state")
109 func (s *MockArvadosServerSuite) Test_ContainerStillInRunningAfterRun(c *C) {
110 apiStubResponses := make(map[string]arvadostest.StubResponse)
111 apiStubResponses["/arvados/v1/containers"] =
112 arvadostest.StubResponse{200, string(`{"items_available":1, "items":[{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx2"}]}`)}
113 apiStubResponses["/arvados/v1/containers/zzzzz-dz642-xxxxxxxxxxxxxx2"] =
114 arvadostest.StubResponse{200, string(`{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx2", "state":"Running", "priority":1}`)}
116 testWithServerStub(c, apiStubResponses, "echo",
117 "After crunch-run process termination, the state is still 'Running' for zzzzz-dz642-xxxxxxxxxxxxxx2")
120 func (s *MockArvadosServerSuite) Test_ErrorRunningContainer(c *C) {
121 apiStubResponses := make(map[string]arvadostest.StubResponse)
122 apiStubResponses["/arvados/v1/containers"] =
123 arvadostest.StubResponse{200, string(`{"items_available":1, "items":[{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx3"}]}`)}
124 apiStubResponses["/arvados/v1/containers/zzzzz-dz642-xxxxxxxxxxxxxx3"] =
125 arvadostest.StubResponse{200, string(`{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx3", "state":"Running", "priority":1}`)}
127 testWithServerStub(c, apiStubResponses, "nosuchcommand", "Error running container for zzzzz-dz642-xxxxxxxxxxxxxx3")
130 func testWithServerStub(c *C, apiStubResponses map[string]arvadostest.StubResponse, crunchCmd string, expected string) {
131 apiStub := arvadostest.ServerStub{apiStubResponses}
133 api := httptest.NewServer(&apiStub)
136 arv = arvadosclient.ArvadosClient{
138 ApiServer: api.URL[7:],
140 Client: &http.Client{Transport: &http.Transport{}},
144 tempfile, err := ioutil.TempFile(os.TempDir(), "temp-log-file")
146 defer os.Remove(tempfile.Name())
147 log.SetOutput(tempfile)
150 time.Sleep(1 * time.Second)
151 doneProcessing <- true
154 runQueuedContainers(1, 1, crunchCmd)
156 // Give some time for run goroutine to complete
157 time.Sleep(5 * time.Second)
159 buf, _ := ioutil.ReadFile(tempfile.Name())
160 c.Check(strings.Contains(string(buf), expected), Equals, true)