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