github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/client/container_update_test.go (about)

     1  package client // import "github.com/docker/docker/client"
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/json"
     7  	"fmt"
     8  	"io/ioutil"
     9  	"net/http"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/docker/docker/api/types/container"
    14  	"github.com/docker/docker/errdefs"
    15  )
    16  
    17  func TestContainerUpdateError(t *testing.T) {
    18  	client := &Client{
    19  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    20  	}
    21  	_, err := client.ContainerUpdate(context.Background(), "nothing", container.UpdateConfig{})
    22  	if !errdefs.IsSystem(err) {
    23  		t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
    24  	}
    25  }
    26  
    27  func TestContainerUpdate(t *testing.T) {
    28  	expectedURL := "/containers/container_id/update"
    29  
    30  	client := &Client{
    31  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    32  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    33  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    34  			}
    35  
    36  			b, err := json.Marshal(container.ContainerUpdateOKBody{})
    37  			if err != nil {
    38  				return nil, err
    39  			}
    40  
    41  			return &http.Response{
    42  				StatusCode: http.StatusOK,
    43  				Body:       ioutil.NopCloser(bytes.NewReader(b)),
    44  			}, nil
    45  		}),
    46  	}
    47  
    48  	_, err := client.ContainerUpdate(context.Background(), "container_id", container.UpdateConfig{
    49  		Resources: container.Resources{
    50  			CPUPeriod: 1,
    51  		},
    52  		RestartPolicy: container.RestartPolicy{
    53  			Name: "always",
    54  		},
    55  	})
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  }