github.com/moby/docker@v26.1.3+incompatible/client/config_update_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/api/types/swarm"
    13  	"github.com/docker/docker/errdefs"
    14  	"gotest.tools/v3/assert"
    15  	is "gotest.tools/v3/assert/cmp"
    16  )
    17  
    18  func TestConfigUpdateUnsupported(t *testing.T) {
    19  	client := &Client{
    20  		version: "1.29",
    21  		client:  &http.Client{},
    22  	}
    23  	err := client.ConfigUpdate(context.Background(), "config_id", swarm.Version{}, swarm.ConfigSpec{})
    24  	assert.Check(t, is.Error(err, `"config update" requires API version 1.30, but the Docker daemon API version is 1.29`))
    25  }
    26  
    27  func TestConfigUpdateError(t *testing.T) {
    28  	client := &Client{
    29  		version: "1.30",
    30  		client:  newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    31  	}
    32  
    33  	err := client.ConfigUpdate(context.Background(), "config_id", swarm.Version{}, swarm.ConfigSpec{})
    34  	assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
    35  }
    36  
    37  func TestConfigUpdate(t *testing.T) {
    38  	expectedURL := "/v1.30/configs/config_id/update"
    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 != http.MethodPost {
    47  				return nil, fmt.Errorf("expected POST method, got %s", req.Method)
    48  			}
    49  			return &http.Response{
    50  				StatusCode: http.StatusOK,
    51  				Body:       io.NopCloser(bytes.NewReader([]byte("body"))),
    52  			}, nil
    53  		}),
    54  	}
    55  
    56  	err := client.ConfigUpdate(context.Background(), "config_id", swarm.Version{}, swarm.ConfigSpec{})
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  }