gopkg.in/docker/docker.v20@v20.10.27/client/config_create_test.go (about)

     1  package client // import "github.com/docker/docker/client"
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/json"
     7  	"fmt"
     8  	"io"
     9  	"net/http"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/docker/docker/api/types"
    14  	"github.com/docker/docker/api/types/swarm"
    15  	"github.com/docker/docker/errdefs"
    16  	"gotest.tools/v3/assert"
    17  	is "gotest.tools/v3/assert/cmp"
    18  )
    19  
    20  func TestConfigCreateUnsupported(t *testing.T) {
    21  	client := &Client{
    22  		version: "1.29",
    23  		client:  &http.Client{},
    24  	}
    25  	_, err := client.ConfigCreate(context.Background(), swarm.ConfigSpec{})
    26  	assert.Check(t, is.Error(err, `"config create" requires API version 1.30, but the Docker daemon API version is 1.29`))
    27  }
    28  
    29  func TestConfigCreateError(t *testing.T) {
    30  	client := &Client{
    31  		version: "1.30",
    32  		client:  newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    33  	}
    34  	_, err := client.ConfigCreate(context.Background(), swarm.ConfigSpec{})
    35  	if !errdefs.IsSystem(err) {
    36  		t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
    37  	}
    38  }
    39  
    40  func TestConfigCreate(t *testing.T) {
    41  	expectedURL := "/v1.30/configs/create"
    42  	client := &Client{
    43  		version: "1.30",
    44  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    45  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    46  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    47  			}
    48  			if req.Method != http.MethodPost {
    49  				return nil, fmt.Errorf("expected POST method, got %s", req.Method)
    50  			}
    51  			b, err := json.Marshal(types.ConfigCreateResponse{
    52  				ID: "test_config",
    53  			})
    54  			if err != nil {
    55  				return nil, err
    56  			}
    57  			return &http.Response{
    58  				StatusCode: http.StatusCreated,
    59  				Body:       io.NopCloser(bytes.NewReader(b)),
    60  			}, nil
    61  		}),
    62  	}
    63  
    64  	r, err := client.ConfigCreate(context.Background(), swarm.ConfigSpec{})
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  	if r.ID != "test_config" {
    69  		t.Fatalf("expected `test_config`, got %s", r.ID)
    70  	}
    71  }