github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/client/image_tag_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 TestImageTagError(t *testing.T) {
    15  	client := &Client{
    16  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    17  	}
    18  
    19  	err := client.ImageTag(context.Background(), "image_id", "repo:tag")
    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  // Note: this is not testing all the InvalidReference as it's the responsibility
    26  // of distribution/reference package.
    27  func TestImageTagInvalidReference(t *testing.T) {
    28  	client := &Client{
    29  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    30  	}
    31  
    32  	err := client.ImageTag(context.Background(), "image_id", "aa/asdf$$^/aa")
    33  	if err == nil || err.Error() != `Error parsing reference: "aa/asdf$$^/aa" is not a valid repository/tag: invalid reference format` {
    34  		t.Fatalf("expected ErrReferenceInvalidFormat, got %v", err)
    35  	}
    36  }
    37  
    38  func TestImageTagInvalidSourceImageName(t *testing.T) {
    39  	client := &Client{
    40  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    41  	}
    42  
    43  	err := client.ImageTag(context.Background(), "invalid_source_image_name_", "repo:tag")
    44  	if err == nil || err.Error() != "Error parsing reference: \"invalid_source_image_name_\" is not a valid repository/tag: invalid reference format" {
    45  		t.Fatalf("expected Parsing Reference Error, got %v", err)
    46  	}
    47  }
    48  
    49  func TestImageTagHexSource(t *testing.T) {
    50  	client := &Client{
    51  		client: newMockClient(errorMock(http.StatusOK, "OK")),
    52  	}
    53  
    54  	err := client.ImageTag(context.Background(), "0d409d33b27e47423b049f7f863faa08655a8c901749c2b25b93ca67d01a470d", "repo:tag")
    55  	if err != nil {
    56  		t.Fatalf("got error: %v", err)
    57  	}
    58  }
    59  
    60  func TestImageTag(t *testing.T) {
    61  	expectedURL := "/images/image_id/tag"
    62  	tagCases := []struct {
    63  		reference           string
    64  		expectedQueryParams map[string]string
    65  	}{
    66  		{
    67  			reference: "repository:tag1",
    68  			expectedQueryParams: map[string]string{
    69  				"repo": "repository",
    70  				"tag":  "tag1",
    71  			},
    72  		}, {
    73  			reference: "another_repository:latest",
    74  			expectedQueryParams: map[string]string{
    75  				"repo": "another_repository",
    76  				"tag":  "latest",
    77  			},
    78  		}, {
    79  			reference: "another_repository",
    80  			expectedQueryParams: map[string]string{
    81  				"repo": "another_repository",
    82  				"tag":  "latest",
    83  			},
    84  		}, {
    85  			reference: "test/another_repository",
    86  			expectedQueryParams: map[string]string{
    87  				"repo": "test/another_repository",
    88  				"tag":  "latest",
    89  			},
    90  		}, {
    91  			reference: "test/another_repository:tag1",
    92  			expectedQueryParams: map[string]string{
    93  				"repo": "test/another_repository",
    94  				"tag":  "tag1",
    95  			},
    96  		}, {
    97  			reference: "test/test/another_repository:tag1",
    98  			expectedQueryParams: map[string]string{
    99  				"repo": "test/test/another_repository",
   100  				"tag":  "tag1",
   101  			},
   102  		}, {
   103  			reference: "test:5000/test/another_repository:tag1",
   104  			expectedQueryParams: map[string]string{
   105  				"repo": "test:5000/test/another_repository",
   106  				"tag":  "tag1",
   107  			},
   108  		}, {
   109  			reference: "test:5000/test/another_repository",
   110  			expectedQueryParams: map[string]string{
   111  				"repo": "test:5000/test/another_repository",
   112  				"tag":  "latest",
   113  			},
   114  		},
   115  	}
   116  	for _, tagCase := range tagCases {
   117  		client := &Client{
   118  			client: newMockClient(func(req *http.Request) (*http.Response, error) {
   119  				if !strings.HasPrefix(req.URL.Path, expectedURL) {
   120  					return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL)
   121  				}
   122  				if req.Method != "POST" {
   123  					return nil, fmt.Errorf("expected POST method, got %s", req.Method)
   124  				}
   125  				query := req.URL.Query()
   126  				for key, expected := range tagCase.expectedQueryParams {
   127  					actual := query.Get(key)
   128  					if actual != expected {
   129  						return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
   130  					}
   131  				}
   132  				return &http.Response{
   133  					StatusCode: http.StatusOK,
   134  					Body:       ioutil.NopCloser(bytes.NewReader([]byte(""))),
   135  				}, nil
   136  			}),
   137  		}
   138  		err := client.ImageTag(context.Background(), "image_id", tagCase.reference)
   139  		if err != nil {
   140  			t.Fatal(err)
   141  		}
   142  	}
   143  }