Add tests for api client creation
This commit is contained in:
@@ -238,7 +238,7 @@ func TestConfigureTaskWhenTaskExists(t *testing.T) {
|
|||||||
|
|
||||||
func TestCreateJobSetsAllGivenArguments(t *testing.T) {
|
func TestCreateJobSetsAllGivenArguments(t *testing.T) {
|
||||||
testJob, base := createTestJob()
|
testJob, base := createTestJob()
|
||||||
apiClient := ApiClient{&nomadApi.Client{}, *base}
|
apiClient := ApiClient{&nomadApiClient{}, &nomadApi.Client{}, *base}
|
||||||
job := apiClient.createJob(
|
job := apiClient.createJob(
|
||||||
*testJob.ID,
|
*testJob.ID,
|
||||||
uint(*testJob.TaskGroups[0].Count),
|
uint(*testJob.TaskGroups[0].Count),
|
||||||
|
@@ -44,9 +44,9 @@ func (apiClient *ApiClient) init(nomadURL *url.URL) (err error) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadRunners loads the allocations of the specified job.
|
// LoadAvailableRunners loads the allocations of the specified job.
|
||||||
func (apiClient *ApiClient) LoadRunners(jobId string) (runnerIds []string, err error) {
|
func (apiClient *ApiClient) LoadAvailableRunners(jobId string) (runnerIds []string, err error) {
|
||||||
list, _, err := apiClient.client.Jobs().Allocations(jobId, true, nil)
|
list, err := apiClient.loadRunners(jobId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@@ -3,8 +3,11 @@ package nomad
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
nomadApi "github.com/hashicorp/nomad/api"
|
nomadApi "github.com/hashicorp/nomad/api"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/mock"
|
"github.com/stretchr/testify/mock"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
|
"net/url"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -112,3 +115,34 @@ func (suite *LoadAvailableRunnersTestSuite) TestReturnsAllAvailableRunners() {
|
|||||||
suite.Contains(returnedIds, suite.availableRunner.ID)
|
suite.Contains(returnedIds, suite.availableRunner.ID)
|
||||||
suite.Contains(returnedIds, suite.anotherAvailableRunner.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)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user