github.com/shishir-a412ed/docker@v1.3.2-0.20180103180333-fda904911d87/client/config_inspect_test.go (about)

     1  package client
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"net/http"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/docker/docker/api/types/swarm"
    13  	"github.com/stretchr/testify/assert"
    14  	"golang.org/x/net/context"
    15  )
    16  
    17  func TestConfigInspectUnsupported(t *testing.T) {
    18  	client := &Client{
    19  		version: "1.29",
    20  		client:  &http.Client{},
    21  	}
    22  	_, _, err := client.ConfigInspectWithRaw(context.Background(), "nothing")
    23  	assert.EqualError(t, err, `"config inspect" requires API version 1.30, but the Docker daemon API version is 1.29`)
    24  }
    25  
    26  func TestConfigInspectError(t *testing.T) {
    27  	client := &Client{
    28  		version: "1.30",
    29  		client:  newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    30  	}
    31  
    32  	_, _, err := client.ConfigInspectWithRaw(context.Background(), "nothing")
    33  	if err == nil || err.Error() != "Error response from daemon: Server error" {
    34  		t.Fatalf("expected a Server Error, got %v", err)
    35  	}
    36  }
    37  
    38  func TestConfigInspectConfigNotFound(t *testing.T) {
    39  	client := &Client{
    40  		version: "1.30",
    41  		client:  newMockClient(errorMock(http.StatusNotFound, "Server error")),
    42  	}
    43  
    44  	_, _, err := client.ConfigInspectWithRaw(context.Background(), "unknown")
    45  	if err == nil || !IsErrNotFound(err) {
    46  		t.Fatalf("expected a configNotFoundError error, got %v", err)
    47  	}
    48  }
    49  
    50  func TestConfigInspect(t *testing.T) {
    51  	expectedURL := "/v1.30/configs/config_id"
    52  	client := &Client{
    53  		version: "1.30",
    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(swarm.Config{
    59  				ID: "config_id",
    60  			})
    61  			if err != nil {
    62  				return nil, err
    63  			}
    64  			return &http.Response{
    65  				StatusCode: http.StatusOK,
    66  				Body:       ioutil.NopCloser(bytes.NewReader(content)),
    67  			}, nil
    68  		}),
    69  	}
    70  
    71  	configInspect, _, err := client.ConfigInspectWithRaw(context.Background(), "config_id")
    72  	if err != nil {
    73  		t.Fatal(err)
    74  	}
    75  	if configInspect.ID != "config_id" {
    76  		t.Fatalf("expected `config_id`, got %s", configInspect.ID)
    77  	}
    78  }