github.com/MaximeAubanel/moby@v1.13.1/client/node_remove_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  	"github.com/docker/docker/api/types"
    12  
    13  	"golang.org/x/net/context"
    14  )
    15  
    16  func TestNodeRemoveError(t *testing.T) {
    17  	client := &Client{
    18  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    19  	}
    20  
    21  	err := client.NodeRemove(context.Background(), "node_id", types.NodeRemoveOptions{Force: false})
    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 TestNodeRemove(t *testing.T) {
    28  	expectedURL := "/nodes/node_id"
    29  
    30  	removeCases := []struct {
    31  		force         bool
    32  		expectedForce string
    33  	}{
    34  		{
    35  			expectedForce: "",
    36  		},
    37  		{
    38  			force:         true,
    39  			expectedForce: "1",
    40  		},
    41  	}
    42  
    43  	for _, removeCase := range removeCases {
    44  		client := &Client{
    45  			client: newMockClient(func(req *http.Request) (*http.Response, error) {
    46  				if !strings.HasPrefix(req.URL.Path, expectedURL) {
    47  					return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    48  				}
    49  				if req.Method != "DELETE" {
    50  					return nil, fmt.Errorf("expected DELETE method, got %s", req.Method)
    51  				}
    52  				force := req.URL.Query().Get("force")
    53  				if force != removeCase.expectedForce {
    54  					return nil, fmt.Errorf("force not set in URL query properly. expected '%s', got %s", removeCase.expectedForce, force)
    55  				}
    56  
    57  				return &http.Response{
    58  					StatusCode: http.StatusOK,
    59  					Body:       ioutil.NopCloser(bytes.NewReader([]byte("body"))),
    60  				}, nil
    61  			}),
    62  		}
    63  
    64  		err := client.NodeRemove(context.Background(), "node_id", types.NodeRemoveOptions{Force: removeCase.force})
    65  		if err != nil {
    66  			t.Fatal(err)
    67  		}
    68  	}
    69  }