...

Source file src/github.com/google/go-containerregistry/pkg/name/tag_test.go

Documentation: github.com/google/go-containerregistry/pkg/name

     1  // Copyright 2018 Google LLC All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package name
    16  
    17  import (
    18  	"path"
    19  	"strings"
    20  	"testing"
    21  )
    22  
    23  var goodStrictValidationTagNames = []string{
    24  	"gcr.io/g-convoy/hello-world:latest",
    25  	"gcr.io/google.com/g-convoy/hello-world:latest",
    26  	"gcr.io/project-id/with-nums:v2",
    27  	"us.gcr.io/project-id/image:with.period.in.tag",
    28  	"gcr.io/project-id/image:w1th-alpha_num3ric.PLUScaps",
    29  	"domain.with.port:9001/image:latest",
    30  }
    31  
    32  var goodWeakValidationTagNames = []string{
    33  	"namespace/pathcomponent/image",
    34  	"library/ubuntu",
    35  	"gcr.io/project-id/implicit-latest",
    36  	"www.example.test:12345/repo/path",
    37  }
    38  
    39  var badTagNames = []string{
    40  	"gcr.io/project-id/bad_chars:c@n'tuse",
    41  	"gcr.io/project-id/wrong-length:white space",
    42  	"gcr.io/project-id/too-many-chars:thisisthetagthatneverendsitgoesonandonmyfriendsomepeoplestartedtaggingitnotknowingwhatitwasandtheyllcontinuetaggingitforeverjustbecausethisisthetagthatneverends",
    43  }
    44  
    45  func TestNewTagStrictValidation(t *testing.T) {
    46  	t.Parallel()
    47  
    48  	for _, name := range goodStrictValidationTagNames {
    49  		if tag, err := NewTag(name, StrictValidation); err != nil {
    50  			t.Errorf("`%s` should be a valid Tag name, got error: %v", name, err)
    51  		} else if tag.Name() != name {
    52  			t.Errorf("`%v` .Name() should reproduce the original name. Wanted: %s Got: %s", tag, name, tag.Name())
    53  		}
    54  	}
    55  
    56  	for _, name := range append(goodWeakValidationTagNames, badTagNames...) {
    57  		if tag, err := NewTag(name, StrictValidation); err == nil {
    58  			t.Errorf("`%s` should be an invalid Tag name, got Tag: %#v", name, tag)
    59  		}
    60  	}
    61  }
    62  
    63  func TestNewTag(t *testing.T) {
    64  	t.Parallel()
    65  
    66  	for _, name := range append(goodStrictValidationTagNames, goodWeakValidationTagNames...) {
    67  		if _, err := NewTag(name, WeakValidation); err != nil {
    68  			t.Errorf("`%s` should be a valid Tag name, got error: %v", name, err)
    69  		}
    70  	}
    71  
    72  	for _, name := range badTagNames {
    73  		if tag, err := NewTag(name, WeakValidation); err == nil {
    74  			t.Errorf("`%s` should be an invalid Tag name, got Tag: %#v", name, tag)
    75  		}
    76  	}
    77  }
    78  
    79  func TestTagComponents(t *testing.T) {
    80  	t.Parallel()
    81  	testRegistry := "gcr.io"
    82  	testRepository := "project-id/image"
    83  	testTag := "latest"
    84  	fullRepo := path.Join(testRegistry, testRepository)
    85  
    86  	tagNameStr := testRegistry + "/" + testRepository + ":" + testTag
    87  	tag, err := NewTag(tagNameStr, StrictValidation)
    88  	if err != nil {
    89  		t.Fatalf("`%s` should be a valid Tag name, got error: %v", tagNameStr, err)
    90  	}
    91  
    92  	actualRegistry := tag.RegistryStr()
    93  	if actualRegistry != testRegistry {
    94  		t.Errorf("RegistryStr() was incorrect for %v. Wanted: `%s` Got: `%s`", tag, testRegistry, actualRegistry)
    95  	}
    96  	actualRepository := tag.RepositoryStr()
    97  	if actualRepository != testRepository {
    98  		t.Errorf("RepositoryStr() was incorrect for %v. Wanted: `%s` Got: `%s`", tag, testRepository, actualRepository)
    99  	}
   100  	actualTag := tag.TagStr()
   101  	if actualTag != testTag {
   102  		t.Errorf("TagStr() was incorrect for %v. Wanted: `%s` Got: `%s`", tag, testTag, actualTag)
   103  	}
   104  	if got, want := tag.Context().String(), fullRepo; got != want {
   105  		t.Errorf("Context.String() was incorrect for %v. Wanted: `%s` Got: `%s`", tag, want, got)
   106  	}
   107  	if got, want := tag.Identifier(), testTag; got != want {
   108  		t.Errorf("Identifier() was incorrect for %v. Wanted: `%s` Got: `%s`", tag, want, got)
   109  	}
   110  	if got, want := tag.String(), tagNameStr; got != want {
   111  		t.Errorf("String() was incorrect for %v. Wanted: `%s` Got: `%s`", tag, want, got)
   112  	}
   113  }
   114  
   115  func TestTagScopes(t *testing.T) {
   116  	t.Parallel()
   117  	testRegistry := "gcr.io"
   118  	testRepo := "project-id/image"
   119  	testTag := "latest"
   120  	testAction := "pull"
   121  
   122  	expectedScope := strings.Join([]string{"repository", testRepo, testAction}, ":")
   123  
   124  	tagNameStr := testRegistry + "/" + testRepo + ":" + testTag
   125  	tag, err := NewTag(tagNameStr, StrictValidation)
   126  	if err != nil {
   127  		t.Fatalf("`%s` should be a valid Tag name, got error: %v", tagNameStr, err)
   128  	}
   129  
   130  	actualScope := tag.Scope(testAction)
   131  	if actualScope != expectedScope {
   132  		t.Errorf("scope was incorrect for %v. Wanted: `%s` Got: `%s`", tag, expectedScope, actualScope)
   133  	}
   134  }
   135  
   136  func TestAllDefaults(t *testing.T) {
   137  	tagNameStr := "ubuntu"
   138  	tag, err := NewTag(tagNameStr, WeakValidation)
   139  	if err != nil {
   140  		t.Fatalf("`%s` should be a valid Tag name, got error: %v", tagNameStr, err)
   141  	}
   142  
   143  	expectedName := "index.docker.io/library/ubuntu:latest"
   144  	actualName := tag.Name()
   145  	if actualName != expectedName {
   146  		t.Errorf("Name() was incorrect for %v. Wanted: `%s` Got: `%s`", tag, expectedName, actualName)
   147  	}
   148  }
   149  
   150  func TestOverrideDefault(t *testing.T) {
   151  	tagNameStr := "ubuntu"
   152  	tag, err := NewTag(tagNameStr, WeakValidation, WithDefaultTag("other"))
   153  	if err != nil {
   154  		t.Fatalf("`%s` should be a valid Tag name, got error: %v", tagNameStr, err)
   155  	}
   156  
   157  	expectedName := "index.docker.io/library/ubuntu:other"
   158  	actualName := tag.Name()
   159  	if actualName != expectedName {
   160  		t.Errorf("Name() was incorrect for %v. Wanted: `%s` Got: `%s`", tag, expectedName, actualName)
   161  	}
   162  }
   163  

View as plain text