X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/3a0aa1db801154916f50b1b299d5100945a3e1df..ae92d144610446849eb568247a44f02ae985c281:/services/crunch-dispatch-local/crunch-dispatch-local_test.go diff --git a/services/crunch-dispatch-local/crunch-dispatch-local_test.go b/services/crunch-dispatch-local/crunch-dispatch-local_test.go index 17f9d671a7..e5ce5c66c5 100644 --- a/services/crunch-dispatch-local/crunch-dispatch-local_test.go +++ b/services/crunch-dispatch-local/crunch-dispatch-local_test.go @@ -1,21 +1,27 @@ +// Copyright (C) The Arvados Authors. All rights reserved. +// +// SPDX-License-Identifier: AGPL-3.0 + package main import ( "bytes" - "git.curoverse.com/arvados.git/sdk/go/arvados" - "git.curoverse.com/arvados.git/sdk/go/arvadosclient" - "git.curoverse.com/arvados.git/sdk/go/arvadostest" - "git.curoverse.com/arvados.git/sdk/go/dispatch" - . "gopkg.in/check.v1" + "context" "io" - "log" "net/http" "net/http/httptest" "os" "os/exec" - "strings" + "regexp" "testing" "time" + + "git.arvados.org/arvados.git/sdk/go/arvados" + "git.arvados.org/arvados.git/sdk/go/arvadosclient" + "git.arvados.org/arvados.git/sdk/go/arvadostest" + "git.arvados.org/arvados.git/sdk/go/ctxlog" + "git.arvados.org/arvados.git/sdk/go/dispatch" + . "gopkg.in/check.v1" ) // Gocheck boilerplate @@ -33,21 +39,19 @@ var initialArgs []string func (s *TestSuite) SetUpSuite(c *C) { initialArgs = os.Args - arvadostest.StartAPI() runningCmds = make(map[string]*exec.Cmd) } -func (s *TestSuite) TearDownSuite(c *C) { - arvadostest.StopAPI() -} - func (s *TestSuite) SetUpTest(c *C) { + arvadostest.ResetDB(c) + arvadostest.ResetEnv() args := []string{"crunch-dispatch-local"} os.Args = args } func (s *TestSuite) TearDownTest(c *C) { arvadostest.ResetEnv() + arvadostest.ResetDB(c) os.Args = initialArgs } @@ -60,28 +64,29 @@ func (s *TestSuite) TestIntegration(c *C) { c.Assert(err, IsNil) echo := "echo" - crunchRunCommand = &echo + crunchRunCommand = echo - doneProcessing := make(chan struct{}) + ctx, cancel := context.WithCancel(ctxlog.Context(context.Background(), ctxlog.TestLogger(c))) dispatcher := dispatch.Dispatcher{ - Arv: arv, - PollInterval: time.Second, - RunContainer: func(dispatcher *dispatch.Dispatcher, - container arvados.Container, - status chan arvados.Container) { - run(dispatcher, container, status) - doneProcessing <- struct{}{} - }, - DoneProcessing: doneProcessing} - - startCmd = func(container arvados.Container, cmd *exec.Cmd) error { + Arv: arv, + PollPeriod: time.Second, + } + + startCmd := func(container arvados.Container, cmd *exec.Cmd) error { dispatcher.UpdateState(container.UUID, "Running") dispatcher.UpdateState(container.UUID, "Complete") return cmd.Start() } - err = dispatcher.RunDispatcher() - c.Assert(err, IsNil) + cl := arvados.Cluster{Containers: arvados.ContainersConfig{RuntimeEngine: "docker"}} + + dispatcher.RunContainer = func(d *dispatch.Dispatcher, c arvados.Container, s <-chan arvados.Container) error { + defer cancel() + return (&LocalRun{startCmd, make(chan bool, 8), ctx, &cl}).run(d, c, s) + } + + err = dispatcher.Run(ctx) + c.Assert(err, Equals, context.Canceled) // Wait for all running crunch jobs to complete / terminate waitGroup.Wait() @@ -106,7 +111,7 @@ func (s *MockArvadosServerSuite) Test_APIErrorGettingContainers(c *C) { apiStubResponses := make(map[string]arvadostest.StubResponse) apiStubResponses["/arvados/v1/containers"] = arvadostest.StubResponse{500, string(`{}`)} - testWithServerStub(c, apiStubResponses, "echo", "Error getting list of containers") + testWithServerStub(c, apiStubResponses, "echo", "error getting count of containers") } func (s *MockArvadosServerSuite) Test_APIErrorUpdatingContainerState(c *C) { @@ -116,18 +121,20 @@ func (s *MockArvadosServerSuite) Test_APIErrorUpdatingContainerState(c *C) { apiStubResponses["/arvados/v1/containers/zzzzz-dz642-xxxxxxxxxxxxxx1"] = arvadostest.StubResponse{500, string(`{}`)} - testWithServerStub(c, apiStubResponses, "echo", "Error updating container zzzzz-dz642-xxxxxxxxxxxxxx1 to state \"Locked\"") + testWithServerStub(c, apiStubResponses, "echo", "error locking container zzzzz-dz642-xxxxxxxxxxxxxx1") } func (s *MockArvadosServerSuite) Test_ContainerStillInRunningAfterRun(c *C) { apiStubResponses := make(map[string]arvadostest.StubResponse) apiStubResponses["/arvados/v1/containers"] = arvadostest.StubResponse{200, string(`{"items_available":1, "items":[{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx2","State":"Queued","Priority":1}]}`)} + apiStubResponses["/arvados/v1/containers/zzzzz-dz642-xxxxxxxxxxxxxx2/lock"] = + arvadostest.StubResponse{200, string(`{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx2", "state":"Locked", "priority":1, "locked_by_uuid": "` + arvadostest.Dispatch1AuthUUID + `"}`)} apiStubResponses["/arvados/v1/containers/zzzzz-dz642-xxxxxxxxxxxxxx2"] = arvadostest.StubResponse{200, string(`{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx2", "state":"Running", "priority":1, "locked_by_uuid": "` + arvadostest.Dispatch1AuthUUID + `"}`)} testWithServerStub(c, apiStubResponses, "echo", - `After echo process termination, container state for Running is "zzzzz-dz642-xxxxxxxxxxxxxx2". Updating it to "Cancelled"`) + `after \\"echo\\" process termination, container state for zzzzz-dz642-xxxxxxxxxxxxxx2 is \\"Running\\"; updating it to \\"Cancelled\\"`) } func (s *MockArvadosServerSuite) Test_ErrorRunningContainer(c *C) { @@ -135,10 +142,10 @@ func (s *MockArvadosServerSuite) Test_ErrorRunningContainer(c *C) { apiStubResponses["/arvados/v1/containers"] = arvadostest.StubResponse{200, string(`{"items_available":1, "items":[{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx3","State":"Queued","Priority":1}]}`)} - apiStubResponses["/arvados/v1/containers/zzzzz-dz642-xxxxxxxxxxxxxx3"] = - arvadostest.StubResponse{200, string(`{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx3", "state":"Running", "priority":1}`)} + apiStubResponses["/arvados/v1/containers/zzzzz-dz642-xxxxxxxxxxxxxx3/lock"] = + arvadostest.StubResponse{200, string(`{"uuid":"zzzzz-dz642-xxxxxxxxxxxxxx3", "state":"Locked", "priority":1}`)} - testWithServerStub(c, apiStubResponses, "nosuchcommand", "Error starting nosuchcommand for zzzzz-dz642-xxxxxxxxxxxxxx3") + testWithServerStub(c, apiStubResponses, "nosuchcommand", `error starting \\"nosuchcommand\\" for zzzzz-dz642-xxxxxxxxxxxxxx3`) } func testWithServerStub(c *C, apiStubResponses map[string]arvadostest.StubResponse, crunchCmd string, expected string) { @@ -150,7 +157,7 @@ func testWithServerStub(c *C, apiStubResponses map[string]arvadostest.StubRespon api := httptest.NewServer(&apiStub) defer api.Close() - arv := arvadosclient.ArvadosClient{ + arv := &arvadosclient.ArvadosClient{ Scheme: "http", ApiServer: api.URL[7:], ApiToken: "abc123", @@ -159,41 +166,44 @@ func testWithServerStub(c *C, apiStubResponses map[string]arvadostest.StubRespon } buf := bytes.NewBuffer(nil) - log.SetOutput(io.MultiWriter(buf, os.Stderr)) - defer log.SetOutput(os.Stderr) + logger := ctxlog.TestLogger(c) + logger.SetOutput(io.MultiWriter(buf, logger.Out)) - *crunchRunCommand = crunchCmd + crunchRunCommand = crunchCmd - doneProcessing := make(chan struct{}) + ctx, cancel := context.WithCancel(ctxlog.Context(context.Background(), logger)) + defer cancel() dispatcher := dispatch.Dispatcher{ - Arv: arv, - PollInterval: time.Duration(1) * time.Second, - RunContainer: func(dispatcher *dispatch.Dispatcher, - container arvados.Container, - status chan arvados.Container) { - run(dispatcher, container, status) - doneProcessing <- struct{}{} - }, - DoneProcessing: doneProcessing} - - startCmd = func(container arvados.Container, cmd *exec.Cmd) error { + Logger: logger, + Arv: arv, + PollPeriod: time.Second, + } + + startCmd := func(container arvados.Container, cmd *exec.Cmd) error { dispatcher.UpdateState(container.UUID, "Running") dispatcher.UpdateState(container.UUID, "Complete") return cmd.Start() } + cl := arvados.Cluster{Containers: arvados.ContainersConfig{RuntimeEngine: "docker"}} + + dispatcher.RunContainer = func(d *dispatch.Dispatcher, c arvados.Container, s <-chan arvados.Container) error { + defer cancel() + return (&LocalRun{startCmd, make(chan bool, 8), ctx, &cl}).run(d, c, s) + } + + re := regexp.MustCompile(`(?ms).*` + expected + `.*`) go func() { - for i := 0; i < 80 && !strings.Contains(buf.String(), expected); i++ { + for i := 0; i < 80 && !re.MatchString(buf.String()); i++ { time.Sleep(100 * time.Millisecond) } - dispatcher.DoneProcessing <- struct{}{} + cancel() }() - err := dispatcher.RunDispatcher() - c.Assert(err, IsNil) + err := dispatcher.Run(ctx) + c.Assert(err, Equals, context.Canceled) + c.Check(buf.String(), Matches, `(?ms).*`+expected+`.*`) - // Wait for all running crunch jobs to complete / terminate + c.Logf("test finished, waiting for running crunch jobs to complete / terminate") waitGroup.Wait() - - c.Check(buf.String(), Matches, `(?ms).*`+expected+`.*`) }