github.com/rish1988/moby@v25.0.2+incompatible/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 assert.Check(t, is.ErrorType(err, errdefs.IsSystem)) 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 != http.MethodPost { 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: io.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 }