github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/client/container_inspect_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"
    14  	"github.com/pkg/errors"
    15  )
    16  
    17  func TestContainerInspectError(t *testing.T) {
    18  	client := &Client{
    19  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    20  	}
    21  
    22  	_, err := client.ContainerInspect(context.Background(), "nothing")
    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 TestContainerInspectContainerNotFound(t *testing.T) {
    29  	client := &Client{
    30  		client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
    31  	}
    32  
    33  	_, err := client.ContainerInspect(context.Background(), "unknown")
    34  	if err == nil || !IsErrNotFound(err) {
    35  		t.Fatalf("expected a containerNotFound error, got %v", err)
    36  	}
    37  }
    38  
    39  func TestContainerInspectWithEmptyID(t *testing.T) {
    40  	client := &Client{
    41  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    42  			return nil, errors.New("should not make request")
    43  		}),
    44  	}
    45  	_, _, err := client.ContainerInspectWithRaw(context.Background(), "", true)
    46  	if !IsErrNotFound(err) {
    47  		t.Fatalf("Expected NotFoundError, got %v", err)
    48  	}
    49  }
    50  
    51  func TestContainerInspect(t *testing.T) {
    52  	expectedURL := "/containers/container_id/json"
    53  	client := &Client{
    54  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    55  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    56  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    57  			}
    58  			content, err := json.Marshal(types.ContainerJSON{
    59  				ContainerJSONBase: &types.ContainerJSONBase{
    60  					ID:    "container_id",
    61  					Image: "image",
    62  					Name:  "name",
    63  				},
    64  			})
    65  			if err != nil {
    66  				return nil, err
    67  			}
    68  			return &http.Response{
    69  				StatusCode: http.StatusOK,
    70  				Body:       ioutil.NopCloser(bytes.NewReader(content)),
    71  			}, nil
    72  		}),
    73  	}
    74  
    75  	r, err := client.ContainerInspect(context.Background(), "container_id")
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  	if r.ID != "container_id" {
    80  		t.Fatalf("expected `container_id`, got %s", r.ID)
    81  	}
    82  	if r.Image != "image" {
    83  		t.Fatalf("expected `image`, got %s", r.Image)
    84  	}
    85  	if r.Name != "name" {
    86  		t.Fatalf("expected `name`, got %s", r.Name)
    87  	}
    88  }
    89  
    90  func TestContainerInspectNode(t *testing.T) {
    91  	client := &Client{
    92  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    93  			content, err := json.Marshal(types.ContainerJSON{
    94  				ContainerJSONBase: &types.ContainerJSONBase{
    95  					ID:    "container_id",
    96  					Image: "image",
    97  					Name:  "name",
    98  					Node: &types.ContainerNode{
    99  						ID:     "container_node_id",
   100  						Addr:   "container_node",
   101  						Labels: map[string]string{"foo": "bar"},
   102  					},
   103  				},
   104  			})
   105  			if err != nil {
   106  				return nil, err
   107  			}
   108  			return &http.Response{
   109  				StatusCode: http.StatusOK,
   110  				Body:       ioutil.NopCloser(bytes.NewReader(content)),
   111  			}, nil
   112  		}),
   113  	}
   114  
   115  	r, err := client.ContainerInspect(context.Background(), "container_id")
   116  	if err != nil {
   117  		t.Fatal(err)
   118  	}
   119  	if r.ID != "container_id" {
   120  		t.Fatalf("expected `container_id`, got %s", r.ID)
   121  	}
   122  	if r.Image != "image" {
   123  		t.Fatalf("expected `image`, got %s", r.Image)
   124  	}
   125  	if r.Name != "name" {
   126  		t.Fatalf("expected `name`, got %s", r.Name)
   127  	}
   128  	if r.Node.ID != "container_node_id" {
   129  		t.Fatalf("expected `container_node_id`, got %s", r.Node.ID)
   130  	}
   131  	if r.Node.Addr != "container_node" {
   132  		t.Fatalf("expected `container_node`, got %s", r.Node.Addr)
   133  	}
   134  	foo, ok := r.Node.Labels["foo"]
   135  	if foo != "bar" || !ok {
   136  		t.Fatalf("expected `bar` for label `foo`")
   137  	}
   138  }