github.com/rumpl/bof@v23.0.0-rc.2+incompatible/client/container_kill_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  )
    14  
    15  func TestContainerKillError(t *testing.T) {
    16  	client := &Client{
    17  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    18  	}
    19  	err := client.ContainerKill(context.Background(), "nothing", "SIGKILL")
    20  	if !errdefs.IsSystem(err) {
    21  		t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
    22  	}
    23  }
    24  
    25  func TestContainerKill(t *testing.T) {
    26  	expectedURL := "/containers/container_id/kill"
    27  	client := &Client{
    28  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    29  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    30  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    31  			}
    32  			signal := req.URL.Query().Get("signal")
    33  			if signal != "SIGKILL" {
    34  				return nil, fmt.Errorf("signal not set in URL query properly. Expected 'SIGKILL', got %s", signal)
    35  			}
    36  			return &http.Response{
    37  				StatusCode: http.StatusOK,
    38  				Body:       io.NopCloser(bytes.NewReader([]byte(""))),
    39  			}, nil
    40  		}),
    41  	}
    42  
    43  	err := client.ContainerKill(context.Background(), "container_id", "SIGKILL")
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  }