github.com/adityamillind98/moby@v23.0.0-rc.4+incompatible/client/service_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/errdefs"
    13  	"gotest.tools/v3/assert"
    14  )
    15  
    16  func TestServiceRemoveError(t *testing.T) {
    17  	client := &Client{
    18  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    19  	}
    20  
    21  	err := client.ServiceRemove(context.Background(), "service_id")
    22  	if !errdefs.IsSystem(err) {
    23  		t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
    24  	}
    25  }
    26  
    27  func TestServiceRemoveNotFoundError(t *testing.T) {
    28  	client := &Client{
    29  		client: newMockClient(errorMock(http.StatusNotFound, "no such service: service_id")),
    30  	}
    31  
    32  	err := client.ServiceRemove(context.Background(), "service_id")
    33  	assert.ErrorContains(t, err, "no such service: service_id")
    34  	assert.Check(t, IsErrNotFound(err))
    35  }
    36  
    37  func TestServiceRemove(t *testing.T) {
    38  	expectedURL := "/services/service_id"
    39  
    40  	client := &Client{
    41  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    42  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    43  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    44  			}
    45  			if req.Method != http.MethodDelete {
    46  				return nil, fmt.Errorf("expected DELETE method, got %s", req.Method)
    47  			}
    48  			return &http.Response{
    49  				StatusCode: http.StatusOK,
    50  				Body:       io.NopCloser(bytes.NewReader([]byte("body"))),
    51  			}, nil
    52  		}),
    53  	}
    54  
    55  	err := client.ServiceRemove(context.Background(), "service_id")
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  }