github.com/rish1988/moby@v25.0.2+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"
     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  	assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
    27  }
    28  
    29  func TestConfigInspectWithEmptyID(t *testing.T) {
    30  	client := &Client{
    31  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    32  			return nil, errors.New("should not make request")
    33  		}),
    34  	}
    35  	_, _, err := client.ConfigInspectWithRaw(context.Background(), "")
    36  	assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
    37  }
    38  
    39  func TestConfigInspectUnsupported(t *testing.T) {
    40  	client := &Client{
    41  		version: "1.29",
    42  		client:  &http.Client{},
    43  	}
    44  	_, _, err := client.ConfigInspectWithRaw(context.Background(), "nothing")
    45  	assert.Check(t, is.Error(err, `"config inspect" requires API version 1.30, but the Docker daemon API version is 1.29`))
    46  }
    47  
    48  func TestConfigInspectError(t *testing.T) {
    49  	client := &Client{
    50  		version: "1.30",
    51  		client:  newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    52  	}
    53  
    54  	_, _, err := client.ConfigInspectWithRaw(context.Background(), "nothing")
    55  	assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
    56  }
    57  
    58  func TestConfigInspectConfigNotFound(t *testing.T) {
    59  	client := &Client{
    60  		version: "1.30",
    61  		client:  newMockClient(errorMock(http.StatusNotFound, "Server error")),
    62  	}
    63  
    64  	_, _, err := client.ConfigInspectWithRaw(context.Background(), "unknown")
    65  	assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
    66  }
    67  
    68  func TestConfigInspect(t *testing.T) {
    69  	expectedURL := "/v1.30/configs/config_id"
    70  	client := &Client{
    71  		version: "1.30",
    72  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    73  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    74  				return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL)
    75  			}
    76  			content, err := json.Marshal(swarm.Config{
    77  				ID: "config_id",
    78  			})
    79  			if err != nil {
    80  				return nil, err
    81  			}
    82  			return &http.Response{
    83  				StatusCode: http.StatusOK,
    84  				Body:       io.NopCloser(bytes.NewReader(content)),
    85  			}, nil
    86  		}),
    87  	}
    88  
    89  	configInspect, _, err := client.ConfigInspectWithRaw(context.Background(), "config_id")
    90  	if err != nil {
    91  		t.Fatal(err)
    92  	}
    93  	if configInspect.ID != "config_id" {
    94  		t.Fatalf("expected `config_id`, got %s", configInspect.ID)
    95  	}
    96  }