github.com/rish1988/moby@v25.0.2+incompatible/client/node_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 TestNodeInspectError(t *testing.T) {
    21  	client := &Client{
    22  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    23  	}
    24  
    25  	_, _, err := client.NodeInspectWithRaw(context.Background(), "nothing")
    26  	assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
    27  }
    28  
    29  func TestNodeInspectNodeNotFound(t *testing.T) {
    30  	client := &Client{
    31  		client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
    32  	}
    33  
    34  	_, _, err := client.NodeInspectWithRaw(context.Background(), "unknown")
    35  	assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
    36  }
    37  
    38  func TestNodeInspectWithEmptyID(t *testing.T) {
    39  	client := &Client{
    40  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    41  			return nil, errors.New("should not make request")
    42  		}),
    43  	}
    44  	_, _, err := client.NodeInspectWithRaw(context.Background(), "")
    45  	assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
    46  }
    47  
    48  func TestNodeInspect(t *testing.T) {
    49  	expectedURL := "/nodes/node_id"
    50  	client := &Client{
    51  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    52  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    53  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    54  			}
    55  			content, err := json.Marshal(swarm.Node{
    56  				ID: "node_id",
    57  			})
    58  			if err != nil {
    59  				return nil, err
    60  			}
    61  			return &http.Response{
    62  				StatusCode: http.StatusOK,
    63  				Body:       io.NopCloser(bytes.NewReader(content)),
    64  			}, nil
    65  		}),
    66  	}
    67  
    68  	nodeInspect, _, err := client.NodeInspectWithRaw(context.Background(), "node_id")
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  	if nodeInspect.ID != "node_id" {
    73  		t.Fatalf("expected `node_id`, got %s", nodeInspect.ID)
    74  	}
    75  }