github.com/kinvolk/docker@v1.13.1/client/container_update_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/container" 13 "golang.org/x/net/context" 14 ) 15 16 func TestContainerUpdateError(t *testing.T) { 17 client := &Client{ 18 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 19 } 20 _, err := client.ContainerUpdate(context.Background(), "nothing", container.UpdateConfig{}) 21 if err == nil || err.Error() != "Error response from daemon: Server error" { 22 t.Fatalf("expected a Server Error, got %v", err) 23 } 24 } 25 26 func TestContainerUpdate(t *testing.T) { 27 expectedURL := "/containers/container_id/update" 28 29 client := &Client{ 30 client: newMockClient(func(req *http.Request) (*http.Response, error) { 31 if !strings.HasPrefix(req.URL.Path, expectedURL) { 32 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 33 } 34 35 b, err := json.Marshal(container.ContainerUpdateOKBody{}) 36 if err != nil { 37 return nil, err 38 } 39 40 return &http.Response{ 41 StatusCode: http.StatusOK, 42 Body: ioutil.NopCloser(bytes.NewReader(b)), 43 }, nil 44 }), 45 } 46 47 _, err := client.ContainerUpdate(context.Background(), "container_id", container.UpdateConfig{ 48 Resources: container.Resources{ 49 CPUPeriod: 1, 50 }, 51 RestartPolicy: container.RestartPolicy{ 52 Name: "always", 53 }, 54 }) 55 if err != nil { 56 t.Fatal(err) 57 } 58 }