github.com/olljanat/moby@v1.13.1/client/image_load_test.go (about)

     1  package client
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"strings"
     9  	"testing"
    10  
    11  	"golang.org/x/net/context"
    12  )
    13  
    14  func TestImageLoadError(t *testing.T) {
    15  	client := &Client{
    16  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    17  	}
    18  
    19  	_, err := client.ImageLoad(context.Background(), nil, true)
    20  	if err == nil || err.Error() != "Error response from daemon: Server error" {
    21  		t.Fatalf("expected a Server Error, got %v", err)
    22  	}
    23  }
    24  
    25  func TestImageLoad(t *testing.T) {
    26  	expectedURL := "/images/load"
    27  	expectedInput := "inputBody"
    28  	expectedOutput := "outputBody"
    29  	loadCases := []struct {
    30  		quiet                bool
    31  		responseContentType  string
    32  		expectedResponseJSON bool
    33  		expectedQueryParams  map[string]string
    34  	}{
    35  		{
    36  			quiet:                false,
    37  			responseContentType:  "text/plain",
    38  			expectedResponseJSON: false,
    39  			expectedQueryParams: map[string]string{
    40  				"quiet": "0",
    41  			},
    42  		},
    43  		{
    44  			quiet:                true,
    45  			responseContentType:  "application/json",
    46  			expectedResponseJSON: true,
    47  			expectedQueryParams: map[string]string{
    48  				"quiet": "1",
    49  			},
    50  		},
    51  	}
    52  	for _, loadCase := range loadCases {
    53  		client := &Client{
    54  			client: newMockClient(func(req *http.Request) (*http.Response, error) {
    55  				if !strings.HasPrefix(req.URL.Path, expectedURL) {
    56  					return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    57  				}
    58  				contentType := req.Header.Get("Content-Type")
    59  				if contentType != "application/x-tar" {
    60  					return nil, fmt.Errorf("content-type not set in URL headers properly. Expected 'application/x-tar', got %s", contentType)
    61  				}
    62  				query := req.URL.Query()
    63  				for key, expected := range loadCase.expectedQueryParams {
    64  					actual := query.Get(key)
    65  					if actual != expected {
    66  						return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
    67  					}
    68  				}
    69  				headers := http.Header{}
    70  				headers.Add("Content-Type", loadCase.responseContentType)
    71  				return &http.Response{
    72  					StatusCode: http.StatusOK,
    73  					Body:       ioutil.NopCloser(bytes.NewReader([]byte(expectedOutput))),
    74  					Header:     headers,
    75  				}, nil
    76  			}),
    77  		}
    78  
    79  		input := bytes.NewReader([]byte(expectedInput))
    80  		imageLoadResponse, err := client.ImageLoad(context.Background(), input, loadCase.quiet)
    81  		if err != nil {
    82  			t.Fatal(err)
    83  		}
    84  		if imageLoadResponse.JSON != loadCase.expectedResponseJSON {
    85  			t.Fatalf("expected a JSON response, was not.")
    86  		}
    87  		body, err := ioutil.ReadAll(imageLoadResponse.Body)
    88  		if err != nil {
    89  			t.Fatal(err)
    90  		}
    91  		if string(body) != expectedOutput {
    92  			t.Fatalf("expected %s, got %s", expectedOutput, string(body))
    93  		}
    94  	}
    95  }