github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/client/node_remove_test.go (about)

     1  package client // import "github.com/docker/docker/client"
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"io"
     8  	"net/http"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/docker/docker/api/types"
    13  	"github.com/docker/docker/errdefs"
    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 !errdefs.IsSystem(err) {
    23  		t.Fatalf("expected a Server Error, got %[1]T: %[1]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 != http.MethodDelete {
    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:       io.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  }