github.com/opsramp/moby@v1.13.1/client/image_create_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  	"github.com/docker/docker/api/types"
    14  )
    15  
    16  func TestImageCreateError(t *testing.T) {
    17  	client := &Client{
    18  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    19  	}
    20  	_, err := client.ImageCreate(context.Background(), "reference", types.ImageCreateOptions{})
    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  func TestImageCreate(t *testing.T) {
    27  	expectedURL := "/images/create"
    28  	expectedImage := "test:5000/my_image"
    29  	expectedTag := "sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
    30  	expectedReference := fmt.Sprintf("%s@%s", expectedImage, expectedTag)
    31  	expectedRegistryAuth := "eyJodHRwczovL2luZGV4LmRvY2tlci5pby92MS8iOnsiYXV0aCI6ImRHOTBid289IiwiZW1haWwiOiJqb2huQGRvZS5jb20ifX0="
    32  	client := &Client{
    33  		client: newMockClient(func(r *http.Request) (*http.Response, error) {
    34  			if !strings.HasPrefix(r.URL.Path, expectedURL) {
    35  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
    36  			}
    37  			registryAuth := r.Header.Get("X-Registry-Auth")
    38  			if registryAuth != expectedRegistryAuth {
    39  				return nil, fmt.Errorf("X-Registry-Auth header not properly set in the request. Expected '%s', got %s", expectedRegistryAuth, registryAuth)
    40  			}
    41  
    42  			query := r.URL.Query()
    43  			fromImage := query.Get("fromImage")
    44  			if fromImage != expectedImage {
    45  				return nil, fmt.Errorf("fromImage not set in URL query properly. Expected '%s', got %s", expectedImage, fromImage)
    46  			}
    47  
    48  			tag := query.Get("tag")
    49  			if tag != expectedTag {
    50  				return nil, fmt.Errorf("tag not set in URL query properly. Expected '%s', got %s", expectedTag, tag)
    51  			}
    52  
    53  			return &http.Response{
    54  				StatusCode: http.StatusOK,
    55  				Body:       ioutil.NopCloser(bytes.NewReader([]byte("body"))),
    56  			}, nil
    57  		}),
    58  	}
    59  
    60  	createResponse, err := client.ImageCreate(context.Background(), expectedReference, types.ImageCreateOptions{
    61  		RegistryAuth: expectedRegistryAuth,
    62  	})
    63  	if err != nil {
    64  		t.Fatal(err)
    65  	}
    66  	response, err := ioutil.ReadAll(createResponse)
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  	if err = createResponse.Close(); err != nil {
    71  		t.Fatal(err)
    72  	}
    73  	if string(response) != "body" {
    74  		t.Fatalf("expected Body to contain 'body' string, got %s", response)
    75  	}
    76  }