github.com/opsramp/moby@v1.13.1/client/node_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  	"golang.org/x/net/context"
    14  )
    15  
    16  func TestNodeInspectError(t *testing.T) {
    17  	client := &Client{
    18  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    19  	}
    20  
    21  	_, _, err := client.NodeInspectWithRaw(context.Background(), "nothing")
    22  	if err == nil || err.Error() != "Error response from daemon: Server error" {
    23  		t.Fatalf("expected a Server Error, got %v", err)
    24  	}
    25  }
    26  
    27  func TestNodeInspectNodeNotFound(t *testing.T) {
    28  	client := &Client{
    29  		client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
    30  	}
    31  
    32  	_, _, err := client.NodeInspectWithRaw(context.Background(), "unknown")
    33  	if err == nil || !IsErrNodeNotFound(err) {
    34  		t.Fatalf("expected an nodeNotFoundError error, got %v", err)
    35  	}
    36  }
    37  
    38  func TestNodeInspect(t *testing.T) {
    39  	expectedURL := "/nodes/node_id"
    40  	client := &Client{
    41  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    42  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    43  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    44  			}
    45  			content, err := json.Marshal(swarm.Node{
    46  				ID: "node_id",
    47  			})
    48  			if err != nil {
    49  				return nil, err
    50  			}
    51  			return &http.Response{
    52  				StatusCode: http.StatusOK,
    53  				Body:       ioutil.NopCloser(bytes.NewReader(content)),
    54  			}, nil
    55  		}),
    56  	}
    57  
    58  	nodeInspect, _, err := client.NodeInspectWithRaw(context.Background(), "node_id")
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	if nodeInspect.ID != "node_id" {
    63  		t.Fatalf("expected `node_id`, got %s", nodeInspect.ID)
    64  	}
    65  }