github.com/opsramp/moby@v1.13.1/client/image_save_test.go (about)

     1  package client
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"reflect"
     9  	"testing"
    10  
    11  	"golang.org/x/net/context"
    12  
    13  	"strings"
    14  )
    15  
    16  func TestImageSaveError(t *testing.T) {
    17  	client := &Client{
    18  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    19  	}
    20  	_, err := client.ImageSave(context.Background(), []string{"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  func TestImageSave(t *testing.T) {
    27  	expectedURL := "/images/get"
    28  	client := &Client{
    29  		client: newMockClient(func(r *http.Request) (*http.Response, error) {
    30  			if !strings.HasPrefix(r.URL.Path, expectedURL) {
    31  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
    32  			}
    33  			query := r.URL.Query()
    34  			names := query["names"]
    35  			expectedNames := []string{"image_id1", "image_id2"}
    36  			if !reflect.DeepEqual(names, expectedNames) {
    37  				return nil, fmt.Errorf("names not set in URL query properly. Expected %v, got %v", names, expectedNames)
    38  			}
    39  
    40  			return &http.Response{
    41  				StatusCode: http.StatusOK,
    42  				Body:       ioutil.NopCloser(bytes.NewReader([]byte("response"))),
    43  			}, nil
    44  		}),
    45  	}
    46  	saveResponse, err := client.ImageSave(context.Background(), []string{"image_id1", "image_id2"})
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  	response, err := ioutil.ReadAll(saveResponse)
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	saveResponse.Close()
    55  	if string(response) != "response" {
    56  		t.Fatalf("expected response to contain 'response', got %s", string(response))
    57  	}
    58  }