github.com/jwhonce/docker@v0.6.7-0.20190327063223-da823cf3a5a3/client/container_wait_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/ioutil" 9 "log" 10 "net/http" 11 "strings" 12 "testing" 13 "time" 14 15 "github.com/docker/docker/api/types/container" 16 "github.com/docker/docker/errdefs" 17 ) 18 19 func TestContainerWaitError(t *testing.T) { 20 client := &Client{ 21 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 22 } 23 resultC, errC := client.ContainerWait(context.Background(), "nothing", "") 24 select { 25 case result := <-resultC: 26 t.Fatalf("expected to not get a wait result, got %d", result.StatusCode) 27 case err := <-errC: 28 if err.Error() != "Error response from daemon: Server error" { 29 t.Fatalf("expected a Server Error, got %v", err) 30 } 31 if !errdefs.IsSystem(err) { 32 t.Fatalf("expected a Server Error, got %T", err) 33 } 34 } 35 } 36 37 func TestContainerWait(t *testing.T) { 38 expectedURL := "/containers/container_id/wait" 39 client := &Client{ 40 client: newMockClient(func(req *http.Request) (*http.Response, error) { 41 if !strings.HasPrefix(req.URL.Path, expectedURL) { 42 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 43 } 44 b, err := json.Marshal(container.ContainerWaitOKBody{ 45 StatusCode: 15, 46 }) 47 if err != nil { 48 return nil, err 49 } 50 return &http.Response{ 51 StatusCode: http.StatusOK, 52 Body: ioutil.NopCloser(bytes.NewReader(b)), 53 }, nil 54 }), 55 } 56 57 resultC, errC := client.ContainerWait(context.Background(), "container_id", "") 58 select { 59 case err := <-errC: 60 t.Fatal(err) 61 case result := <-resultC: 62 if result.StatusCode != 15 { 63 t.Fatalf("expected a status code equal to '15', got %d", result.StatusCode) 64 } 65 } 66 } 67 68 func ExampleClient_ContainerWait_withTimeout() { 69 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 70 defer cancel() 71 72 client, _ := NewClientWithOpts(FromEnv) 73 _, errC := client.ContainerWait(ctx, "container_id", "") 74 if err := <-errC; err != nil { 75 log.Fatal(err) 76 } 77 }