github.com/skf/moby@v1.13.1/client/container_copy_test.go (about)

     1  package client
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/base64"
     6  	"encoding/json"
     7  	"fmt"
     8  	"io/ioutil"
     9  	"net/http"
    10  	"strings"
    11  	"testing"
    12  
    13  	"golang.org/x/net/context"
    14  
    15  	"github.com/docker/docker/api/types"
    16  )
    17  
    18  func TestContainerStatPathError(t *testing.T) {
    19  	client := &Client{
    20  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    21  	}
    22  	_, err := client.ContainerStatPath(context.Background(), "container_id", "path")
    23  	if err == nil || err.Error() != "Error response from daemon: Server error" {
    24  		t.Fatalf("expected a Server error, got %v", err)
    25  	}
    26  }
    27  
    28  func TestContainerStatPathNoHeaderError(t *testing.T) {
    29  	client := &Client{
    30  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    31  			return &http.Response{
    32  				StatusCode: http.StatusOK,
    33  				Body:       ioutil.NopCloser(bytes.NewReader([]byte(""))),
    34  			}, nil
    35  		}),
    36  	}
    37  	_, err := client.ContainerStatPath(context.Background(), "container_id", "path/to/file")
    38  	if err == nil {
    39  		t.Fatalf("expected an error, got nothing")
    40  	}
    41  }
    42  
    43  func TestContainerStatPath(t *testing.T) {
    44  	expectedURL := "/containers/container_id/archive"
    45  	expectedPath := "path/to/file"
    46  	client := &Client{
    47  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    48  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    49  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    50  			}
    51  			if req.Method != "HEAD" {
    52  				return nil, fmt.Errorf("expected HEAD method, got %s", req.Method)
    53  			}
    54  			query := req.URL.Query()
    55  			path := query.Get("path")
    56  			if path != expectedPath {
    57  				return nil, fmt.Errorf("path not set in URL query properly")
    58  			}
    59  			content, err := json.Marshal(types.ContainerPathStat{
    60  				Name: "name",
    61  				Mode: 0700,
    62  			})
    63  			if err != nil {
    64  				return nil, err
    65  			}
    66  			base64PathStat := base64.StdEncoding.EncodeToString(content)
    67  			return &http.Response{
    68  				StatusCode: http.StatusOK,
    69  				Body:       ioutil.NopCloser(bytes.NewReader([]byte(""))),
    70  				Header: http.Header{
    71  					"X-Docker-Container-Path-Stat": []string{base64PathStat},
    72  				},
    73  			}, nil
    74  		}),
    75  	}
    76  	stat, err := client.ContainerStatPath(context.Background(), "container_id", expectedPath)
    77  	if err != nil {
    78  		t.Fatal(err)
    79  	}
    80  	if stat.Name != "name" {
    81  		t.Fatalf("expected container path stat name to be 'name', got '%s'", stat.Name)
    82  	}
    83  	if stat.Mode != 0700 {
    84  		t.Fatalf("expected container path stat mode to be 0700, got '%v'", stat.Mode)
    85  	}
    86  }
    87  
    88  func TestCopyToContainerError(t *testing.T) {
    89  	client := &Client{
    90  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    91  	}
    92  	err := client.CopyToContainer(context.Background(), "container_id", "path/to/file", bytes.NewReader([]byte("")), types.CopyToContainerOptions{})
    93  	if err == nil || err.Error() != "Error response from daemon: Server error" {
    94  		t.Fatalf("expected a Server error, got %v", err)
    95  	}
    96  }
    97  
    98  func TestCopyToContainerNotStatusOKError(t *testing.T) {
    99  	client := &Client{
   100  		client: newMockClient(errorMock(http.StatusNoContent, "No content")),
   101  	}
   102  	err := client.CopyToContainer(context.Background(), "container_id", "path/to/file", bytes.NewReader([]byte("")), types.CopyToContainerOptions{})
   103  	if err == nil || err.Error() != "unexpected status code from daemon: 204" {
   104  		t.Fatalf("expected an unexpected status code error, got %v", err)
   105  	}
   106  }
   107  
   108  func TestCopyToContainer(t *testing.T) {
   109  	expectedURL := "/containers/container_id/archive"
   110  	expectedPath := "path/to/file"
   111  	client := &Client{
   112  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
   113  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
   114  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
   115  			}
   116  			if req.Method != "PUT" {
   117  				return nil, fmt.Errorf("expected PUT method, got %s", req.Method)
   118  			}
   119  			query := req.URL.Query()
   120  			path := query.Get("path")
   121  			if path != expectedPath {
   122  				return nil, fmt.Errorf("path not set in URL query properly, expected '%s', got %s", expectedPath, path)
   123  			}
   124  			noOverwriteDirNonDir := query.Get("noOverwriteDirNonDir")
   125  			if noOverwriteDirNonDir != "true" {
   126  				return nil, fmt.Errorf("noOverwriteDirNonDir not set in URL query properly, expected true, got %s", noOverwriteDirNonDir)
   127  			}
   128  
   129  			content, err := ioutil.ReadAll(req.Body)
   130  			if err != nil {
   131  				return nil, err
   132  			}
   133  			if err := req.Body.Close(); err != nil {
   134  				return nil, err
   135  			}
   136  			if string(content) != "content" {
   137  				return nil, fmt.Errorf("expected content to be 'content', got %s", string(content))
   138  			}
   139  
   140  			return &http.Response{
   141  				StatusCode: http.StatusOK,
   142  				Body:       ioutil.NopCloser(bytes.NewReader([]byte(""))),
   143  			}, nil
   144  		}),
   145  	}
   146  	err := client.CopyToContainer(context.Background(), "container_id", expectedPath, bytes.NewReader([]byte("content")), types.CopyToContainerOptions{
   147  		AllowOverwriteDirWithFile: false,
   148  	})
   149  	if err != nil {
   150  		t.Fatal(err)
   151  	}
   152  }
   153  
   154  func TestCopyFromContainerError(t *testing.T) {
   155  	client := &Client{
   156  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
   157  	}
   158  	_, _, err := client.CopyFromContainer(context.Background(), "container_id", "path/to/file")
   159  	if err == nil || err.Error() != "Error response from daemon: Server error" {
   160  		t.Fatalf("expected a Server error, got %v", err)
   161  	}
   162  }
   163  
   164  func TestCopyFromContainerNotStatusOKError(t *testing.T) {
   165  	client := &Client{
   166  		client: newMockClient(errorMock(http.StatusNoContent, "No content")),
   167  	}
   168  	_, _, err := client.CopyFromContainer(context.Background(), "container_id", "path/to/file")
   169  	if err == nil || err.Error() != "unexpected status code from daemon: 204" {
   170  		t.Fatalf("expected an unexpected status code error, got %v", err)
   171  	}
   172  }
   173  
   174  func TestCopyFromContainerNoHeaderError(t *testing.T) {
   175  	client := &Client{
   176  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
   177  			return &http.Response{
   178  				StatusCode: http.StatusOK,
   179  				Body:       ioutil.NopCloser(bytes.NewReader([]byte(""))),
   180  			}, nil
   181  		}),
   182  	}
   183  	_, _, err := client.CopyFromContainer(context.Background(), "container_id", "path/to/file")
   184  	if err == nil {
   185  		t.Fatalf("expected an error, got nothing")
   186  	}
   187  }
   188  
   189  func TestCopyFromContainer(t *testing.T) {
   190  	expectedURL := "/containers/container_id/archive"
   191  	expectedPath := "path/to/file"
   192  	client := &Client{
   193  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
   194  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
   195  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
   196  			}
   197  			if req.Method != "GET" {
   198  				return nil, fmt.Errorf("expected PUT method, got %s", req.Method)
   199  			}
   200  			query := req.URL.Query()
   201  			path := query.Get("path")
   202  			if path != expectedPath {
   203  				return nil, fmt.Errorf("path not set in URL query properly, expected '%s', got %s", expectedPath, path)
   204  			}
   205  
   206  			headercontent, err := json.Marshal(types.ContainerPathStat{
   207  				Name: "name",
   208  				Mode: 0700,
   209  			})
   210  			if err != nil {
   211  				return nil, err
   212  			}
   213  			base64PathStat := base64.StdEncoding.EncodeToString(headercontent)
   214  
   215  			return &http.Response{
   216  				StatusCode: http.StatusOK,
   217  				Body:       ioutil.NopCloser(bytes.NewReader([]byte("content"))),
   218  				Header: http.Header{
   219  					"X-Docker-Container-Path-Stat": []string{base64PathStat},
   220  				},
   221  			}, nil
   222  		}),
   223  	}
   224  	r, stat, err := client.CopyFromContainer(context.Background(), "container_id", expectedPath)
   225  	if err != nil {
   226  		t.Fatal(err)
   227  	}
   228  	if stat.Name != "name" {
   229  		t.Fatalf("expected container path stat name to be 'name', got '%s'", stat.Name)
   230  	}
   231  	if stat.Mode != 0700 {
   232  		t.Fatalf("expected container path stat mode to be 0700, got '%v'", stat.Mode)
   233  	}
   234  	content, err := ioutil.ReadAll(r)
   235  	if err != nil {
   236  		t.Fatal(err)
   237  	}
   238  	if err := r.Close(); err != nil {
   239  		t.Fatal(err)
   240  	}
   241  	if string(content) != "content" {
   242  		t.Fatalf("expected content to be 'content', got %s", string(content))
   243  	}
   244  }