github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/client/swarm_unlock_test.go (about)

     1  package client
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"strings"
     9  	"testing"
    10  
    11  	"golang.org/x/net/context"
    12  
    13  	"github.com/docker/docker/api/types/swarm"
    14  )
    15  
    16  func TestSwarmUnlockError(t *testing.T) {
    17  	client := &Client{
    18  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    19  	}
    20  
    21  	err := client.SwarmUnlock(context.Background(), swarm.UnlockRequest{"SWMKEY-1-y6guTZNTwpQeTL5RhUfOsdBdXoQjiB2GADHSRJvbXeU"})
    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 TestSwarmUnlock(t *testing.T) {
    28  	expectedURL := "/swarm/unlock"
    29  
    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  			if req.Method != "POST" {
    36  				return nil, fmt.Errorf("expected POST method, got %s", req.Method)
    37  			}
    38  			return &http.Response{
    39  				StatusCode: http.StatusOK,
    40  				Body:       ioutil.NopCloser(bytes.NewReader([]byte(""))),
    41  			}, nil
    42  		}),
    43  	}
    44  
    45  	err := client.SwarmUnlock(context.Background(), swarm.UnlockRequest{"SWMKEY-1-y6guTZNTwpQeTL5RhUfOsdBdXoQjiB2GADHSRJvbXeU"})
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  }