github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/client/config_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/swarm"
    14  	"github.com/pkg/errors"
    15  	"gotest.tools/assert"
    16  	is "gotest.tools/assert/cmp"
    17  )
    18  
    19  func TestConfigInspectNotFound(t *testing.T) {
    20  	client := &Client{
    21  		client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
    22  	}
    23  
    24  	_, _, err := client.ConfigInspectWithRaw(context.Background(), "unknown")
    25  	if err == nil || !IsErrNotFound(err) {
    26  		t.Fatalf("expected a NotFoundError error, got %v", err)
    27  	}
    28  }
    29  
    30  func TestConfigInspectWithEmptyID(t *testing.T) {
    31  	client := &Client{
    32  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    33  			return nil, errors.New("should not make request")
    34  		}),
    35  	}
    36  	_, _, err := client.ConfigInspectWithRaw(context.Background(), "")
    37  	if !IsErrNotFound(err) {
    38  		t.Fatalf("Expected NotFoundError, got %v", err)
    39  	}
    40  }
    41  
    42  func TestConfigInspectUnsupported(t *testing.T) {
    43  	client := &Client{
    44  		version: "1.29",
    45  		client:  &http.Client{},
    46  	}
    47  	_, _, err := client.ConfigInspectWithRaw(context.Background(), "nothing")
    48  	assert.Check(t, is.Error(err, `"config inspect" requires API version 1.30, but the Docker daemon API version is 1.29`))
    49  }
    50  
    51  func TestConfigInspectError(t *testing.T) {
    52  	client := &Client{
    53  		version: "1.30",
    54  		client:  newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    55  	}
    56  
    57  	_, _, err := client.ConfigInspectWithRaw(context.Background(), "nothing")
    58  	if err == nil || err.Error() != "Error response from daemon: Server error" {
    59  		t.Fatalf("expected a Server Error, got %v", err)
    60  	}
    61  }
    62  
    63  func TestConfigInspectConfigNotFound(t *testing.T) {
    64  	client := &Client{
    65  		version: "1.30",
    66  		client:  newMockClient(errorMock(http.StatusNotFound, "Server error")),
    67  	}
    68  
    69  	_, _, err := client.ConfigInspectWithRaw(context.Background(), "unknown")
    70  	if err == nil || !IsErrNotFound(err) {
    71  		t.Fatalf("expected a configNotFoundError error, got %v", err)
    72  	}
    73  }
    74  
    75  func TestConfigInspect(t *testing.T) {
    76  	expectedURL := "/v1.30/configs/config_id"
    77  	client := &Client{
    78  		version: "1.30",
    79  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    80  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    81  				return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL)
    82  			}
    83  			content, err := json.Marshal(swarm.Config{
    84  				ID: "config_id",
    85  			})
    86  			if err != nil {
    87  				return nil, err
    88  			}
    89  			return &http.Response{
    90  				StatusCode: http.StatusOK,
    91  				Body:       ioutil.NopCloser(bytes.NewReader(content)),
    92  			}, nil
    93  		}),
    94  	}
    95  
    96  	configInspect, _, err := client.ConfigInspectWithRaw(context.Background(), "config_id")
    97  	if err != nil {
    98  		t.Fatal(err)
    99  	}
   100  	if configInspect.ID != "config_id" {
   101  		t.Fatalf("expected `config_id`, got %s", configInspect.ID)
   102  	}
   103  }