github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/client/swarm_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/ioutil"
     9  	"net/http"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/docker/docker/api/types/swarm"
    14  	"github.com/docker/docker/errdefs"
    15  )
    16  
    17  func TestSwarmInspectError(t *testing.T) {
    18  	client := &Client{
    19  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    20  	}
    21  
    22  	_, err := client.SwarmInspect(context.Background())
    23  	if !errdefs.IsSystem(err) {
    24  		t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
    25  	}
    26  }
    27  
    28  func TestSwarmInspect(t *testing.T) {
    29  	expectedURL := "/swarm"
    30  	client := &Client{
    31  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    32  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    33  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    34  			}
    35  			content, err := json.Marshal(swarm.Swarm{
    36  				ClusterInfo: swarm.ClusterInfo{
    37  					ID: "swarm_id",
    38  				},
    39  			})
    40  			if err != nil {
    41  				return nil, err
    42  			}
    43  			return &http.Response{
    44  				StatusCode: http.StatusOK,
    45  				Body:       ioutil.NopCloser(bytes.NewReader(content)),
    46  			}, nil
    47  		}),
    48  	}
    49  
    50  	swarmInspect, err := client.SwarmInspect(context.Background())
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	if swarmInspect.ID != "swarm_id" {
    55  		t.Fatalf("expected `swarm_id`, got %s", swarmInspect.ID)
    56  	}
    57  }