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