github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/client/container_create_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  	"net/http"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/docker/docker/api/types/container"
    14  	"github.com/docker/docker/errdefs"
    15  )
    16  
    17  func TestContainerCreateError(t *testing.T) {
    18  	client := &Client{
    19  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    20  	}
    21  	_, err := client.ContainerCreate(context.Background(), nil, nil, nil, nil, "nothing")
    22  	if !errdefs.IsSystem(err) {
    23  		t.Fatalf("expected a Server Error while testing StatusInternalServerError, got %T", err)
    24  	}
    25  
    26  	// 404 doesn't automatically means an unknown image
    27  	client = &Client{
    28  		client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
    29  	}
    30  	_, err = client.ContainerCreate(context.Background(), nil, nil, nil, nil, "nothing")
    31  	if err == nil || !IsErrNotFound(err) {
    32  		t.Fatalf("expected a Server Error while testing StatusNotFound, got %T", err)
    33  	}
    34  }
    35  
    36  func TestContainerCreateImageNotFound(t *testing.T) {
    37  	client := &Client{
    38  		client: newMockClient(errorMock(http.StatusNotFound, "No such image")),
    39  	}
    40  	_, err := client.ContainerCreate(context.Background(), &container.Config{Image: "unknown_image"}, nil, nil, nil, "unknown")
    41  	if err == nil || !IsErrNotFound(err) {
    42  		t.Fatalf("expected an imageNotFound error, got %v, %T", err, err)
    43  	}
    44  }
    45  
    46  func TestContainerCreateWithName(t *testing.T) {
    47  	expectedURL := "/containers/create"
    48  	client := &Client{
    49  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    50  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    51  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    52  			}
    53  			name := req.URL.Query().Get("name")
    54  			if name != "container_name" {
    55  				return nil, fmt.Errorf("container name not set in URL query properly. Expected `container_name`, got %s", name)
    56  			}
    57  			b, err := json.Marshal(container.ContainerCreateCreatedBody{
    58  				ID: "container_id",
    59  			})
    60  			if err != nil {
    61  				return nil, err
    62  			}
    63  			return &http.Response{
    64  				StatusCode: http.StatusOK,
    65  				Body:       ioutil.NopCloser(bytes.NewReader(b)),
    66  			}, nil
    67  		}),
    68  	}
    69  
    70  	r, err := client.ContainerCreate(context.Background(), nil, nil, nil, nil, "container_name")
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  	if r.ID != "container_id" {
    75  		t.Fatalf("expected `container_id`, got %s", r.ID)
    76  	}
    77  }
    78  
    79  // TestContainerCreateAutoRemove validates that a client using API 1.24 always disables AutoRemove. When using API 1.25
    80  // or up, AutoRemove should not be disabled.
    81  func TestContainerCreateAutoRemove(t *testing.T) {
    82  	autoRemoveValidator := func(expectedValue bool) func(req *http.Request) (*http.Response, error) {
    83  		return func(req *http.Request) (*http.Response, error) {
    84  			var config configWrapper
    85  
    86  			if err := json.NewDecoder(req.Body).Decode(&config); err != nil {
    87  				return nil, err
    88  			}
    89  			if config.HostConfig.AutoRemove != expectedValue {
    90  				return nil, fmt.Errorf("expected AutoRemove to be %v, got %v", expectedValue, config.HostConfig.AutoRemove)
    91  			}
    92  			b, err := json.Marshal(container.ContainerCreateCreatedBody{
    93  				ID: "container_id",
    94  			})
    95  			if err != nil {
    96  				return nil, err
    97  			}
    98  			return &http.Response{
    99  				StatusCode: http.StatusOK,
   100  				Body:       ioutil.NopCloser(bytes.NewReader(b)),
   101  			}, nil
   102  		}
   103  	}
   104  
   105  	client := &Client{
   106  		client:  newMockClient(autoRemoveValidator(false)),
   107  		version: "1.24",
   108  	}
   109  	if _, err := client.ContainerCreate(context.Background(), nil, &container.HostConfig{AutoRemove: true}, nil, nil, ""); err != nil {
   110  		t.Fatal(err)
   111  	}
   112  	client = &Client{
   113  		client:  newMockClient(autoRemoveValidator(true)),
   114  		version: "1.25",
   115  	}
   116  	if _, err := client.ContainerCreate(context.Background(), nil, &container.HostConfig{AutoRemove: true}, nil, nil, ""); err != nil {
   117  		t.Fatal(err)
   118  	}
   119  }