github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/client/config_remove_test.go (about)

     1  package client // import "github.com/docker/docker/client"
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"net/http"
     9  	"strings"
    10  	"testing"
    11  
    12  	"gotest.tools/assert"
    13  	is "gotest.tools/assert/cmp"
    14  )
    15  
    16  func TestConfigRemoveUnsupported(t *testing.T) {
    17  	client := &Client{
    18  		version: "1.29",
    19  		client:  &http.Client{},
    20  	}
    21  	err := client.ConfigRemove(context.Background(), "config_id")
    22  	assert.Check(t, is.Error(err, `"config remove" requires API version 1.30, but the Docker daemon API version is 1.29`))
    23  }
    24  
    25  func TestConfigRemoveError(t *testing.T) {
    26  	client := &Client{
    27  		version: "1.30",
    28  		client:  newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    29  	}
    30  
    31  	err := client.ConfigRemove(context.Background(), "config_id")
    32  	if err == nil || err.Error() != "Error response from daemon: Server error" {
    33  		t.Fatalf("expected a Server Error, got %v", err)
    34  	}
    35  }
    36  
    37  func TestConfigRemove(t *testing.T) {
    38  	expectedURL := "/v1.30/configs/config_id"
    39  
    40  	client := &Client{
    41  		version: "1.30",
    42  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    43  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    44  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    45  			}
    46  			if req.Method != "DELETE" {
    47  				return nil, fmt.Errorf("expected DELETE method, got %s", req.Method)
    48  			}
    49  			return &http.Response{
    50  				StatusCode: http.StatusOK,
    51  				Body:       ioutil.NopCloser(bytes.NewReader([]byte("body"))),
    52  			}, nil
    53  		}),
    54  	}
    55  
    56  	err := client.ConfigRemove(context.Background(), "config_id")
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  }