github.com/afxcn/moby@v1.13.1/client/container_diff_test.go (about)

     1  package client
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"net/http"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/docker/docker/api/types"
    13  	"golang.org/x/net/context"
    14  )
    15  
    16  func TestContainerDiffError(t *testing.T) {
    17  	client := &Client{
    18  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    19  	}
    20  	_, err := client.ContainerDiff(context.Background(), "nothing")
    21  	if err == nil || err.Error() != "Error response from daemon: Server error" {
    22  		t.Fatalf("expected a Server Error, got %v", err)
    23  	}
    24  
    25  }
    26  
    27  func TestContainerDiff(t *testing.T) {
    28  	expectedURL := "/containers/container_id/changes"
    29  	client := &Client{
    30  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    31  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    32  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    33  			}
    34  			b, err := json.Marshal([]types.ContainerChange{
    35  				{
    36  					Kind: 0,
    37  					Path: "/path/1",
    38  				},
    39  				{
    40  					Kind: 1,
    41  					Path: "/path/2",
    42  				},
    43  			})
    44  			if err != nil {
    45  				return nil, err
    46  			}
    47  			return &http.Response{
    48  				StatusCode: http.StatusOK,
    49  				Body:       ioutil.NopCloser(bytes.NewReader(b)),
    50  			}, nil
    51  		}),
    52  	}
    53  
    54  	changes, err := client.ContainerDiff(context.Background(), "container_id")
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  	if len(changes) != 2 {
    59  		t.Fatalf("expected an array of 2 changes, got %v", changes)
    60  	}
    61  }