diff --git a/api/websocket_test.go b/api/websocket_test.go index b0a1f38..40559ee 100644 --- a/api/websocket_test.go +++ b/api/websocket_test.go @@ -370,9 +370,9 @@ func TestCodeOceanToRawReaderReturnsOnlyAfterOneByteWasReadFromConnection(t *tes // --- Test suite specific test helpers --- -func newNomadAllocationWithMockedAPIClient(runnerID string) (r runner.Runner, mock *nomad.ExecutorAPIMock) { - mock = &nomad.ExecutorAPIMock{} - r = runner.NewNomadJob(runnerID, nil, mock, nil) +func newNomadAllocationWithMockedAPIClient(runnerID string) (r runner.Runner, executorAPIMock *nomad.ExecutorAPIMock) { + executorAPIMock = &nomad.ExecutorAPIMock{} + r = runner.NewNomadJob(runnerID, nil, executorAPIMock, nil) return } diff --git a/nomad/nomad_test.go b/nomad/nomad_test.go index 65cfe77..17deb3d 100644 --- a/nomad/nomad_test.go +++ b/nomad/nomad_test.go @@ -755,3 +755,35 @@ func (s *ExecuteCommandTestSuite) mockExecute(command interface{}, exitCode int, Run(runFunc). Return(exitCode, err) } + +func TestAPIClient_LoadRunnerPortMappings(t *testing.T) { + apiMock := &apiQuerierMock{} + mockedCall := apiMock.On("allocation", tests.DefaultRunnerID) + nomadAPIClient := APIClient{apiQuerier: apiMock} + + t.Run("should return error when API query fails", func(t *testing.T) { + mockedCall.Return(nil, tests.ErrDefault) + portMappings, err := nomadAPIClient.LoadRunnerPortMappings(tests.DefaultRunnerID) + assert.Nil(t, portMappings) + assert.ErrorIs(t, err, tests.ErrDefault) + }) + + t.Run("should return error when AllocatedResources is nil", func(t *testing.T) { + mockedCall.Return(&nomadApi.Allocation{AllocatedResources: nil}, nil) + portMappings, err := nomadAPIClient.LoadRunnerPortMappings(tests.DefaultRunnerID) + assert.ErrorIs(t, err, ErrorNoAllocatedResourcesFound) + assert.Nil(t, portMappings) + }) + + t.Run("should correctly return ports", func(t *testing.T) { + allocation := &nomadApi.Allocation{ + AllocatedResources: &nomadApi.AllocatedResources{ + Shared: nomadApi.AllocatedSharedResources{Ports: tests.DefaultPortMappings}, + }, + } + mockedCall.Return(allocation, nil) + portMappings, err := nomadAPIClient.LoadRunnerPortMappings(tests.DefaultRunnerID) + assert.NoError(t, err) + assert.Equal(t, tests.DefaultPortMappings, portMappings) + }) +} diff --git a/runner/manager_test.go b/runner/manager_test.go index 1e481d0..20265b2 100644 --- a/runner/manager_test.go +++ b/runner/manager_test.go @@ -284,3 +284,64 @@ func modifyMockedCall(apiMock *nomad.ExecutorAPIMock, method string, modifier fu } } } + +func (s *ManagerTestSuite) TestOnAllocationAdded() { + s.registerDefaultEnvironment() + s.Run("does not add environment template id job", func() { + alloc := &nomadApi.Allocation{JobID: TemplateJobID(tests.DefaultEnvironmentIDAsInteger)} + s.nomadRunnerManager.onAllocationAdded(alloc) + job, ok := s.nomadRunnerManager.environments.Get(tests.DefaultEnvironmentIDAsInteger) + s.True(ok) + s.Zero(job.idleRunners.Length()) + }) + s.Run("does not panic when environment id cannot be parsed", func() { + alloc := &nomadApi.Allocation{JobID: ""} + s.NotPanics(func() { + s.nomadRunnerManager.onAllocationAdded(alloc) + }) + }) + s.Run("does not panic when environment does not exist", func() { + nonExistentEnvironment := EnvironmentID(1234) + _, ok := s.nomadRunnerManager.environments.Get(nonExistentEnvironment) + s.Require().False(ok) + + alloc := &nomadApi.Allocation{JobID: RunnerJobID(nonExistentEnvironment, "1-1-1-1")} + s.NotPanics(func() { + s.nomadRunnerManager.onAllocationAdded(alloc) + }) + }) + s.Run("adds correct job", func() { + s.Run("without allocated resources", func() { + alloc := &nomadApi.Allocation{ + JobID: tests.DefaultJobID, + AllocatedResources: nil, + } + s.nomadRunnerManager.onAllocationAdded(alloc) + job, ok := s.nomadRunnerManager.environments.Get(tests.DefaultEnvironmentIDAsInteger) + s.True(ok) + runner, ok := job.idleRunners.Get(tests.DefaultJobID) + s.True(ok) + nomadJob, ok := runner.(*NomadJob) + s.True(ok) + s.Equal(nomadJob.id, tests.DefaultJobID) + s.Empty(nomadJob.portMappings) + }) + s.Run("with mapped ports", func() { + alloc := &nomadApi.Allocation{ + JobID: tests.DefaultJobID, + AllocatedResources: &nomadApi.AllocatedResources{ + Shared: nomadApi.AllocatedSharedResources{Ports: tests.DefaultPortMappings}, + }, + } + s.nomadRunnerManager.onAllocationAdded(alloc) + job, ok := s.nomadRunnerManager.environments.Get(tests.DefaultEnvironmentIDAsInteger) + s.True(ok) + runner, ok := job.idleRunners.Get(tests.DefaultJobID) + s.True(ok) + nomadJob, ok := runner.(*NomadJob) + s.True(ok) + s.Equal(nomadJob.id, tests.DefaultJobID) + s.Equal(nomadJob.portMappings, tests.DefaultPortMappings) + }) + }) +} diff --git a/runner/runner_test.go b/runner/runner_test.go index be8b6f3..db957a3 100644 --- a/runner/runner_test.go +++ b/runner/runner_test.go @@ -25,6 +25,14 @@ func TestIdIsStored(t *testing.T) { assert.Equal(t, tests.DefaultJobID, runner.ID()) } +func TestMappedPortsAreStoredCorrectly(t *testing.T) { + runner := NewNomadJob(tests.DefaultJobID, tests.DefaultPortMappings, nil, nil) + assert.Equal(t, tests.DefaultMappedPorts, runner.MappedPorts()) + + runner = NewNomadJob(tests.DefaultJobID, nil, nil, nil) + assert.Empty(t, runner.MappedPorts()) +} + func TestMarshalRunner(t *testing.T) { runner := NewNomadJob(tests.DefaultJobID, nil, nil, nil) marshal, err := json.Marshal(runner) diff --git a/tests/constants.go b/tests/constants.go index 52a139e..40bc963 100644 --- a/tests/constants.go +++ b/tests/constants.go @@ -2,6 +2,8 @@ package tests import ( "errors" + nomadApi "github.com/hashicorp/nomad/api" + "gitlab.hpi.de/codeocean/codemoon/poseidon/api/dto" "time" ) @@ -28,5 +30,7 @@ const ( ) var ( - ErrDefault = errors.New("an error occurred") + ErrDefault = errors.New("an error occurred") + DefaultPortMappings = []nomadApi.PortMapping{{To: 42, Value: 1337, Label: "lit", HostIP: "127.0.0.1"}} + DefaultMappedPorts = []*dto.MappedPort{{ExposedPort: 42, HostAddress: "127.0.0.1:1337"}} )