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