github.com/kinvolk/docker@v1.13.1/client/container_rename_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  	"golang.org/x/net/context"
    12  )
    13  
    14  func TestContainerRenameError(t *testing.T) {
    15  	client := &Client{
    16  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    17  	}
    18  	err := client.ContainerRename(context.Background(), "nothing", "newNothing")
    19  	if err == nil || err.Error() != "Error response from daemon: Server error" {
    20  		t.Fatalf("expected a Server Error, got %v", err)
    21  	}
    22  }
    23  
    24  func TestContainerRename(t *testing.T) {
    25  	expectedURL := "/containers/container_id/rename"
    26  	client := &Client{
    27  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    28  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    29  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    30  			}
    31  			name := req.URL.Query().Get("name")
    32  			if name != "newName" {
    33  				return nil, fmt.Errorf("name not set in URL query properly. Expected 'newName', got %s", name)
    34  			}
    35  			return &http.Response{
    36  				StatusCode: http.StatusOK,
    37  				Body:       ioutil.NopCloser(bytes.NewReader([]byte(""))),
    38  			}, nil
    39  		}),
    40  	}
    41  
    42  	err := client.ContainerRename(context.Background(), "container_id", "newName")
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  }