github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/client/container_exec_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 "golang.org/x/net/context" 13 14 "github.com/docker/docker/api/types" 15 ) 16 17 func TestContainerExecCreateError(t *testing.T) { 18 client := &Client{ 19 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 20 } 21 _, err := client.ContainerExecCreate(context.Background(), "container_id", types.ExecConfig{}) 22 if err == nil || err.Error() != "Error response from daemon: Server error" { 23 t.Fatalf("expected a Server Error, got %v", err) 24 } 25 } 26 27 func TestContainerExecCreate(t *testing.T) { 28 expectedURL := "/containers/container_id/exec" 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 if req.Method != "POST" { 35 return nil, fmt.Errorf("expected POST method, got %s", req.Method) 36 } 37 // FIXME validate the content is the given ExecConfig ? 38 if err := req.ParseForm(); err != nil { 39 return nil, err 40 } 41 execConfig := &types.ExecConfig{} 42 if err := json.NewDecoder(req.Body).Decode(execConfig); err != nil { 43 return nil, err 44 } 45 if execConfig.User != "user" { 46 return nil, fmt.Errorf("expected an execConfig with User == 'user', got %v", execConfig) 47 } 48 b, err := json.Marshal(types.IDResponse{ 49 ID: "exec_id", 50 }) 51 if err != nil { 52 return nil, err 53 } 54 return &http.Response{ 55 StatusCode: http.StatusOK, 56 Body: ioutil.NopCloser(bytes.NewReader(b)), 57 }, nil 58 }), 59 } 60 61 r, err := client.ContainerExecCreate(context.Background(), "container_id", types.ExecConfig{ 62 User: "user", 63 }) 64 if err != nil { 65 t.Fatal(err) 66 } 67 if r.ID != "exec_id" { 68 t.Fatalf("expected `exec_id`, got %s", r.ID) 69 } 70 } 71 72 func TestContainerExecStartError(t *testing.T) { 73 client := &Client{ 74 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 75 } 76 err := client.ContainerExecStart(context.Background(), "nothing", types.ExecStartCheck{}) 77 if err == nil || err.Error() != "Error response from daemon: Server error" { 78 t.Fatalf("expected a Server Error, got %v", err) 79 } 80 } 81 82 func TestContainerExecStart(t *testing.T) { 83 expectedURL := "/exec/exec_id/start" 84 client := &Client{ 85 client: newMockClient(func(req *http.Request) (*http.Response, error) { 86 if !strings.HasPrefix(req.URL.Path, expectedURL) { 87 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 88 } 89 if err := req.ParseForm(); err != nil { 90 return nil, err 91 } 92 execStartCheck := &types.ExecStartCheck{} 93 if err := json.NewDecoder(req.Body).Decode(execStartCheck); err != nil { 94 return nil, err 95 } 96 if execStartCheck.Tty || !execStartCheck.Detach { 97 return nil, fmt.Errorf("expected execStartCheck{Detach:true,Tty:false}, got %v", execStartCheck) 98 } 99 100 return &http.Response{ 101 StatusCode: http.StatusOK, 102 Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), 103 }, nil 104 }), 105 } 106 107 err := client.ContainerExecStart(context.Background(), "exec_id", types.ExecStartCheck{ 108 Detach: true, 109 Tty: false, 110 }) 111 if err != nil { 112 t.Fatal(err) 113 } 114 } 115 116 func TestContainerExecInspectError(t *testing.T) { 117 client := &Client{ 118 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 119 } 120 _, err := client.ContainerExecInspect(context.Background(), "nothing") 121 if err == nil || err.Error() != "Error response from daemon: Server error" { 122 t.Fatalf("expected a Server Error, got %v", err) 123 } 124 } 125 126 func TestContainerExecInspect(t *testing.T) { 127 expectedURL := "/exec/exec_id/json" 128 client := &Client{ 129 client: newMockClient(func(req *http.Request) (*http.Response, error) { 130 if !strings.HasPrefix(req.URL.Path, expectedURL) { 131 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 132 } 133 b, err := json.Marshal(types.ContainerExecInspect{ 134 ExecID: "exec_id", 135 ContainerID: "container_id", 136 }) 137 if err != nil { 138 return nil, err 139 } 140 return &http.Response{ 141 StatusCode: http.StatusOK, 142 Body: ioutil.NopCloser(bytes.NewReader(b)), 143 }, nil 144 }), 145 } 146 147 inspect, err := client.ContainerExecInspect(context.Background(), "exec_id") 148 if err != nil { 149 t.Fatal(err) 150 } 151 if inspect.ExecID != "exec_id" { 152 t.Fatalf("expected ExecID to be `exec_id`, got %s", inspect.ExecID) 153 } 154 if inspect.ContainerID != "container_id" { 155 t.Fatalf("expected ContainerID `container_id`, got %s", inspect.ContainerID) 156 } 157 }