github.com/olljanat/moby@v1.13.1/client/swarm_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 TestSwarmInspectError(t *testing.T) {
    17  	client := &Client{
    18  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    19  	}
    20  
    21  	_, err := client.SwarmInspect(context.Background())
    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 TestSwarmInspect(t *testing.T) {
    28  	expectedURL := "/swarm"
    29  	client := &Client{
    30  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    31  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    32  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    33  			}
    34  			content, err := json.Marshal(swarm.Swarm{
    35  				ClusterInfo: swarm.ClusterInfo{
    36  					ID: "swarm_id",
    37  				},
    38  			})
    39  			if err != nil {
    40  				return nil, err
    41  			}
    42  			return &http.Response{
    43  				StatusCode: http.StatusOK,
    44  				Body:       ioutil.NopCloser(bytes.NewReader(content)),
    45  			}, nil
    46  		}),
    47  	}
    48  
    49  	swarmInspect, err := client.SwarmInspect(context.Background())
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  	if swarmInspect.ID != "swarm_id" {
    54  		t.Fatalf("expected `swarm_id`, got %s", swarmInspect.ID)
    55  	}
    56  }