github.com/rish1988/moby@v25.0.2+incompatible/client/container_rename_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  	is "gotest.tools/v3/assert/cmp"
    15  )
    16  
    17  func TestContainerRenameError(t *testing.T) {
    18  	client := &Client{
    19  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    20  	}
    21  	err := client.ContainerRename(context.Background(), "nothing", "newNothing")
    22  	assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
    23  }
    24  
    25  func TestContainerRename(t *testing.T) {
    26  	expectedURL := "/containers/container_id/rename"
    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  			name := req.URL.Query().Get("name")
    33  			if name != "newName" {
    34  				return nil, fmt.Errorf("name not set in URL query properly. Expected 'newName', got %s", name)
    35  			}
    36  			return &http.Response{
    37  				StatusCode: http.StatusOK,
    38  				Body:       io.NopCloser(bytes.NewReader([]byte(""))),
    39  			}, nil
    40  		}),
    41  	}
    42  
    43  	err := client.ContainerRename(context.Background(), "container_id", "newName")
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  }