github.com/rawahars/moby@v24.0.4+incompatible/client/container_diff_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"
     9  	"net/http"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/docker/docker/api/types/container"
    14  	"github.com/docker/docker/errdefs"
    15  	"gotest.tools/v3/assert"
    16  	is "gotest.tools/v3/assert/cmp"
    17  )
    18  
    19  func TestContainerDiffError(t *testing.T) {
    20  	client := &Client{
    21  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    22  	}
    23  	_, err := client.ContainerDiff(context.Background(), "nothing")
    24  	assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
    25  }
    26  
    27  func TestContainerDiff(t *testing.T) {
    28  	const expectedURL = "/containers/container_id/changes"
    29  
    30  	expected := []container.FilesystemChange{
    31  		{
    32  			Kind: container.ChangeModify,
    33  			Path: "/path/1",
    34  		},
    35  		{
    36  			Kind: container.ChangeAdd,
    37  			Path: "/path/2",
    38  		},
    39  		{
    40  			Kind: container.ChangeDelete,
    41  			Path: "/path/3",
    42  		},
    43  	}
    44  
    45  	client := &Client{
    46  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    47  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    48  				return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL)
    49  			}
    50  			b, err := json.Marshal(expected)
    51  			if err != nil {
    52  				return nil, err
    53  			}
    54  			return &http.Response{
    55  				StatusCode: http.StatusOK,
    56  				Body:       io.NopCloser(bytes.NewReader(b)),
    57  			}, nil
    58  		}),
    59  	}
    60  
    61  	changes, err := client.ContainerDiff(context.Background(), "container_id")
    62  	assert.Check(t, err)
    63  	assert.Check(t, is.DeepEqual(changes, expected))
    64  }