...

Source file src/github.com/google/go-containerregistry/pkg/name/repository_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  	"errors"
    19  	"strings"
    20  	"testing"
    21  )
    22  
    23  var goodStrictValidationRepositoryNames = []string{
    24  	"gcr.io/g-convoy/hello-world",
    25  	"gcr.io/google.com/project-id/hello-world",
    26  	"us.gcr.io/project-id/sub-repo",
    27  	"example.text/foo/bar",
    28  	"mirror.gcr.io/ubuntu",
    29  	"index.docker.io/library/ubuntu",
    30  }
    31  
    32  var goodWeakValidationRepositoryNames = []string{
    33  	"namespace/pathcomponent/image",
    34  	"library/ubuntu",
    35  	"ubuntu",
    36  }
    37  
    38  var badRepositoryNames = []string{
    39  	"white space",
    40  	"b@char/image",
    41  	"",
    42  }
    43  
    44  func TestNewRepositoryStrictValidation(t *testing.T) {
    45  	t.Parallel()
    46  
    47  	for _, name := range goodStrictValidationRepositoryNames {
    48  		if repository, err := NewRepository(name, StrictValidation); err != nil {
    49  			t.Errorf("`%s` should be a valid Repository name, got error: %v", name, err)
    50  		} else if repository.Name() != name {
    51  			t.Errorf("`%v` .Name() should reproduce the original name. Wanted: %s Got: %s", repository, name, repository.Name())
    52  		}
    53  	}
    54  
    55  	for _, name := range append(goodWeakValidationRepositoryNames, badRepositoryNames...) {
    56  		if repo, err := NewRepository(name, StrictValidation); err == nil {
    57  			t.Errorf("`%s` should be an invalid repository name, got Repository: %#v", name, repo)
    58  		}
    59  	}
    60  }
    61  
    62  func TestNewRepository(t *testing.T) {
    63  	t.Parallel()
    64  
    65  	for _, name := range append(goodStrictValidationRepositoryNames, goodWeakValidationRepositoryNames...) {
    66  		if _, err := NewRepository(name, WeakValidation); err != nil {
    67  			t.Errorf("`%s` should be a valid repository name, got error: %v", name, err)
    68  		}
    69  	}
    70  
    71  	for _, name := range badRepositoryNames {
    72  		if repo, err := NewRepository(name, WeakValidation); err == nil {
    73  			t.Errorf("`%s` should be an invalid repository name, got Repository: %#v", name, repo)
    74  		}
    75  	}
    76  }
    77  
    78  func TestRepositoryComponents(t *testing.T) {
    79  	t.Parallel()
    80  	testRegistry := "gcr.io"
    81  	testRepository := "project-id/image"
    82  
    83  	repositoryNameStr := testRegistry + "/" + testRepository
    84  	repository, err := NewRepository(repositoryNameStr, StrictValidation)
    85  	if err != nil {
    86  		t.Fatalf("`%s` should be a valid Repository name, got error: %v", repositoryNameStr, err)
    87  	}
    88  
    89  	actualRegistry := repository.RegistryStr()
    90  	if actualRegistry != testRegistry {
    91  		t.Errorf("RegistryStr() was incorrect for %v. Wanted: `%s` Got: `%s`", repository, testRegistry, actualRegistry)
    92  	}
    93  	actualRepository := repository.RepositoryStr()
    94  	if actualRepository != testRepository {
    95  		t.Errorf("RepositoryStr() was incorrect for %v. Wanted: `%s` Got: `%s`", repository, testRepository, actualRepository)
    96  	}
    97  }
    98  
    99  func TestRepositoryScopes(t *testing.T) {
   100  	t.Parallel()
   101  	testRegistry := "gcr.io"
   102  	testRepo := "project-id/image"
   103  	testAction := "pull"
   104  
   105  	expectedScope := strings.Join([]string{"repository", testRepo, testAction}, ":")
   106  
   107  	repositoryNameStr := testRegistry + "/" + testRepo
   108  	repository, err := NewRepository(repositoryNameStr, StrictValidation)
   109  	if err != nil {
   110  		t.Fatalf("`%s` should be a valid Repository name, got error: %v", repositoryNameStr, err)
   111  	}
   112  
   113  	actualScope := repository.Scope(testAction)
   114  	if actualScope != expectedScope {
   115  		t.Errorf("scope was incorrect for %v. Wanted: `%s` Got: `%s`", repository, expectedScope, actualScope)
   116  	}
   117  }
   118  
   119  func TestRepositoryBadDefaulting(t *testing.T) {
   120  	var berr *ErrBadName
   121  	if _, err := NewRepository("index.docker.io/foo", StrictValidation); !errors.As(err, &berr) {
   122  		t.Errorf("Not an ErrBadName: %v", err)
   123  	}
   124  }
   125  
   126  func TestRepositoryChildren(t *testing.T) {
   127  	repo, err := NewRepository("example.com/repo", Insecure)
   128  	if err != nil {
   129  		t.Fatal(err)
   130  	}
   131  	tag := repo.Tag("foo")
   132  	if got, want := tag.Scheme(), "http"; got != want {
   133  		t.Errorf("tag.Scheme(): got %s want %s", got, want)
   134  	}
   135  	if got, want := tag.String(), "example.com/repo:foo"; got != want {
   136  		t.Errorf("tag.String(): got %s want %s", got, want)
   137  	}
   138  	digest := repo.Digest("badf00d")
   139  	if got, want := digest.Scheme(), "http"; got != want {
   140  		t.Errorf("digest.Scheme(): got %s want %s", got, want)
   141  	}
   142  	if got, want := digest.String(), "example.com/repo@badf00d"; got != want {
   143  		t.Errorf("digest.String(): got %s want %s", got, want)
   144  	}
   145  }
   146  

View as plain text