Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
| ... | ... |
@@ -18,21 +18,19 @@ import ( |
| 18 | 18 |
) |
| 19 | 19 |
|
| 20 | 20 |
func TestTaskInspectError(t *testing.T) {
|
| 21 |
- client := &Client{
|
|
| 22 |
- client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), |
|
| 23 |
- } |
|
| 21 |
+ client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error"))) |
|
| 22 |
+ assert.NilError(t, err) |
|
| 24 | 23 |
|
| 25 |
- _, _, err := client.TaskInspectWithRaw(context.Background(), "nothing") |
|
| 24 |
+ _, _, err = client.TaskInspectWithRaw(context.Background(), "nothing") |
|
| 26 | 25 |
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal)) |
| 27 | 26 |
} |
| 28 | 27 |
|
| 29 | 28 |
func TestTaskInspectWithEmptyID(t *testing.T) {
|
| 30 |
- client := &Client{
|
|
| 31 |
- client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
|
| 32 |
- return nil, errors.New("should not make request")
|
|
| 33 |
- }), |
|
| 34 |
- } |
|
| 35 |
- _, _, err := client.TaskInspectWithRaw(context.Background(), "") |
|
| 29 |
+ client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
|
|
| 30 |
+ return nil, errors.New("should not make request")
|
|
| 31 |
+ })) |
|
| 32 |
+ assert.NilError(t, err) |
|
| 33 |
+ _, _, err = client.TaskInspectWithRaw(context.Background(), "") |
|
| 36 | 34 |
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument)) |
| 37 | 35 |
assert.Check(t, is.ErrorContains(err, "value is empty")) |
| 38 | 36 |
|
| ... | ... |
@@ -43,23 +41,22 @@ func TestTaskInspectWithEmptyID(t *testing.T) {
|
| 43 | 43 |
|
| 44 | 44 |
func TestTaskInspect(t *testing.T) {
|
| 45 | 45 |
expectedURL := "/tasks/task_id" |
| 46 |
- client := &Client{
|
|
| 47 |
- client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
|
| 48 |
- if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
|
| 49 |
- return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
|
| 50 |
- } |
|
| 51 |
- content, err := json.Marshal(swarm.Task{
|
|
| 52 |
- ID: "task_id", |
|
| 53 |
- }) |
|
| 54 |
- if err != nil {
|
|
| 55 |
- return nil, err |
|
| 56 |
- } |
|
| 57 |
- return &http.Response{
|
|
| 58 |
- StatusCode: http.StatusOK, |
|
| 59 |
- Body: io.NopCloser(bytes.NewReader(content)), |
|
| 60 |
- }, nil |
|
| 61 |
- }), |
|
| 62 |
- } |
|
| 46 |
+ client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
|
|
| 47 |
+ if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
|
| 48 |
+ return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
|
| 49 |
+ } |
|
| 50 |
+ content, err := json.Marshal(swarm.Task{
|
|
| 51 |
+ ID: "task_id", |
|
| 52 |
+ }) |
|
| 53 |
+ if err != nil {
|
|
| 54 |
+ return nil, err |
|
| 55 |
+ } |
|
| 56 |
+ return &http.Response{
|
|
| 57 |
+ StatusCode: http.StatusOK, |
|
| 58 |
+ Body: io.NopCloser(bytes.NewReader(content)), |
|
| 59 |
+ }, nil |
|
| 60 |
+ })) |
|
| 61 |
+ assert.NilError(t, err) |
|
| 63 | 62 |
|
| 64 | 63 |
taskInspect, _, err := client.TaskInspectWithRaw(context.Background(), "task_id") |
| 65 | 64 |
assert.NilError(t, err) |
| ... | ... |
@@ -18,11 +18,10 @@ import ( |
| 18 | 18 |
) |
| 19 | 19 |
|
| 20 | 20 |
func TestTaskListError(t *testing.T) {
|
| 21 |
- client := &Client{
|
|
| 22 |
- client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), |
|
| 23 |
- } |
|
| 21 |
+ client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error"))) |
|
| 22 |
+ assert.NilError(t, err) |
|
| 24 | 23 |
|
| 25 |
- _, err := client.TaskList(context.Background(), TaskListOptions{})
|
|
| 24 |
+ _, err = client.TaskList(context.Background(), TaskListOptions{})
|
|
| 26 | 25 |
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal)) |
| 27 | 26 |
} |
| 28 | 27 |
|
| ... | ... |
@@ -52,35 +51,34 @@ func TestTaskList(t *testing.T) {
|
| 52 | 52 |
}, |
| 53 | 53 |
} |
| 54 | 54 |
for _, listCase := range listCases {
|
| 55 |
- client := &Client{
|
|
| 56 |
- client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
|
| 57 |
- if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
|
| 58 |
- return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
|
| 59 |
- } |
|
| 60 |
- query := req.URL.Query() |
|
| 61 |
- for key, expected := range listCase.expectedQueryParams {
|
|
| 62 |
- actual := query.Get(key) |
|
| 63 |
- if actual != expected {
|
|
| 64 |
- return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
|
|
| 65 |
- } |
|
| 55 |
+ client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
|
|
| 56 |
+ if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
|
| 57 |
+ return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
|
| 58 |
+ } |
|
| 59 |
+ query := req.URL.Query() |
|
| 60 |
+ for key, expected := range listCase.expectedQueryParams {
|
|
| 61 |
+ actual := query.Get(key) |
|
| 62 |
+ if actual != expected {
|
|
| 63 |
+ return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
|
|
| 66 | 64 |
} |
| 67 |
- content, err := json.Marshal([]swarm.Task{
|
|
| 68 |
- {
|
|
| 69 |
- ID: "task_id1", |
|
| 70 |
- }, |
|
| 71 |
- {
|
|
| 72 |
- ID: "task_id2", |
|
| 73 |
- }, |
|
| 74 |
- }) |
|
| 75 |
- if err != nil {
|
|
| 76 |
- return nil, err |
|
| 77 |
- } |
|
| 78 |
- return &http.Response{
|
|
| 79 |
- StatusCode: http.StatusOK, |
|
| 80 |
- Body: io.NopCloser(bytes.NewReader(content)), |
|
| 81 |
- }, nil |
|
| 82 |
- }), |
|
| 83 |
- } |
|
| 65 |
+ } |
|
| 66 |
+ content, err := json.Marshal([]swarm.Task{
|
|
| 67 |
+ {
|
|
| 68 |
+ ID: "task_id1", |
|
| 69 |
+ }, |
|
| 70 |
+ {
|
|
| 71 |
+ ID: "task_id2", |
|
| 72 |
+ }, |
|
| 73 |
+ }) |
|
| 74 |
+ if err != nil {
|
|
| 75 |
+ return nil, err |
|
| 76 |
+ } |
|
| 77 |
+ return &http.Response{
|
|
| 78 |
+ StatusCode: http.StatusOK, |
|
| 79 |
+ Body: io.NopCloser(bytes.NewReader(content)), |
|
| 80 |
+ }, nil |
|
| 81 |
+ })) |
|
| 82 |
+ assert.NilError(t, err) |
|
| 84 | 83 |
|
| 85 | 84 |
tasks, err := client.TaskList(context.Background(), listCase.options) |
| 86 | 85 |
assert.NilError(t, err) |