From a2a9ed367ade6b454b1cc3ac1308658591adfdbe Mon Sep 17 00:00:00 2001 From: sirkrypt0 <22522058+sirkrypt0@users.noreply.github.com> Date: Mon, 10 May 2021 11:58:28 +0200 Subject: [PATCH] Add tests for api client creation --- nomad/job_test.go | 2 +- nomad/nomad.go | 6 +++--- nomad/nomad_test.go | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/nomad/job_test.go b/nomad/job_test.go index 0dd3cab..e1447a3 100644 --- a/nomad/job_test.go +++ b/nomad/job_test.go @@ -238,7 +238,7 @@ func TestConfigureTaskWhenTaskExists(t *testing.T) { func TestCreateJobSetsAllGivenArguments(t *testing.T) { testJob, base := createTestJob() - apiClient := ApiClient{&nomadApi.Client{}, *base} + apiClient := ApiClient{&nomadApiClient{}, &nomadApi.Client{}, *base} job := apiClient.createJob( *testJob.ID, uint(*testJob.TaskGroups[0].Count), diff --git a/nomad/nomad.go b/nomad/nomad.go index 73bb94d..2af2cfc 100644 --- a/nomad/nomad.go +++ b/nomad/nomad.go @@ -44,9 +44,9 @@ func (apiClient *ApiClient) init(nomadURL *url.URL) (err error) { return nil } -// LoadRunners loads the allocations of the specified job. -func (apiClient *ApiClient) LoadRunners(jobId string) (runnerIds []string, err error) { - list, _, err := apiClient.client.Jobs().Allocations(jobId, true, nil) +// LoadAvailableRunners loads the allocations of the specified job. +func (apiClient *ApiClient) LoadAvailableRunners(jobId string) (runnerIds []string, err error) { + list, err := apiClient.loadRunners(jobId) if err != nil { return nil, err } diff --git a/nomad/nomad_test.go b/nomad/nomad_test.go index 714015f..de7fd42 100644 --- a/nomad/nomad_test.go +++ b/nomad/nomad_test.go @@ -3,8 +3,11 @@ package nomad import ( "errors" nomadApi "github.com/hashicorp/nomad/api" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" + "net/url" "testing" ) @@ -112,3 +115,34 @@ func (suite *LoadAvailableRunnersTestSuite) TestReturnsAllAvailableRunners() { suite.Contains(returnedIds, suite.availableRunner.ID) suite.Contains(returnedIds, suite.anotherAvailableRunner.ID) } + +var TestURL = url.URL{ + Scheme: "http", + Host: "127.0.0.1:4646", +} + +func TestApiClient_init(t *testing.T) { + client := &ApiClient{} + defaultJob := parseJob(defaultJobHCL) + err := client.init(&TestURL) + require.Nil(t, err) + assert.Equal(t, *defaultJob, client.defaultJob) +} + +func TestApiClientCanNotBeInitializedWithInvalidUrl(t *testing.T) { + client := &ApiClient{} + err := client.init(&url.URL{ + Scheme: "http", + Host: "http://127.0.0.1:4646", + }) + assert.NotNil(t, err) +} + +func TestNewExecutorApiCanBeCreatedWithoutError(t *testing.T) { + expectedClient := &ApiClient{} + err := expectedClient.init(&TestURL) + require.Nil(t, err) + + _, err = NewExecutorApi(&TestURL) + require.Nil(t, err) +}