github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/client/config_update_test.go (about) 1 package client // import "github.com/docker/docker/client" 2 3 import ( 4 "bytes" 5 "context" 6 "fmt" 7 "io/ioutil" 8 "net/http" 9 "strings" 10 "testing" 11 12 "github.com/docker/docker/api/types/swarm" 13 "gotest.tools/assert" 14 is "gotest.tools/assert/cmp" 15 ) 16 17 func TestConfigUpdateUnsupported(t *testing.T) { 18 client := &Client{ 19 version: "1.29", 20 client: &http.Client{}, 21 } 22 err := client.ConfigUpdate(context.Background(), "config_id", swarm.Version{}, swarm.ConfigSpec{}) 23 assert.Check(t, is.Error(err, `"config update" requires API version 1.30, but the Docker daemon API version is 1.29`)) 24 } 25 26 func TestConfigUpdateError(t *testing.T) { 27 client := &Client{ 28 version: "1.30", 29 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 30 } 31 32 err := client.ConfigUpdate(context.Background(), "config_id", swarm.Version{}, 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 TestConfigUpdate(t *testing.T) { 39 expectedURL := "/v1.30/configs/config_id/update" 40 41 client := &Client{ 42 version: "1.30", 43 client: newMockClient(func(req *http.Request) (*http.Response, error) { 44 if !strings.HasPrefix(req.URL.Path, expectedURL) { 45 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 46 } 47 if req.Method != "POST" { 48 return nil, fmt.Errorf("expected POST method, got %s", req.Method) 49 } 50 return &http.Response{ 51 StatusCode: http.StatusOK, 52 Body: ioutil.NopCloser(bytes.NewReader([]byte("body"))), 53 }, nil 54 }), 55 } 56 57 err := client.ConfigUpdate(context.Background(), "config_id", swarm.Version{}, swarm.ConfigSpec{}) 58 if err != nil { 59 t.Fatal(err) 60 } 61 }