...

Source file src/github.com/google/go-containerregistry/pkg/name/digest_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  const validDigest = "sha256:deadb33fdeadb33fdeadb33fdeadb33fdeadb33fdeadb33fdeadb33fdeadb33f"
    24  
    25  var goodStrictValidationDigestNames = []string{
    26  	"gcr.io/g-convoy/hello-world@" + validDigest,
    27  	"gcr.io/google.com/project-id/hello-world@" + validDigest,
    28  	"us.gcr.io/project-id/sub-repo@" + validDigest,
    29  	"example.text/foo/bar@" + validDigest,
    30  }
    31  
    32  var goodStrictValidationTagDigestNames = []string{
    33  	"example.text/foo/bar:latest@" + validDigest,
    34  	"example.text:8443/foo/bar:latest@" + validDigest,
    35  	"example.text/foo/bar:v1.0.0-alpine@" + validDigest,
    36  }
    37  
    38  var goodWeakValidationDigestNames = []string{
    39  	"namespace/pathcomponent/image@" + validDigest,
    40  	"library/ubuntu@" + validDigest,
    41  }
    42  
    43  var goodWeakValidationTagDigestNames = []string{
    44  	"nginx:latest@" + validDigest,
    45  	"library/nginx:latest@" + validDigest,
    46  }
    47  
    48  var badDigestNames = []string{
    49  	"gcr.io/project-id/unknown-alg@unknown:abc123",
    50  	"gcr.io/project-id/wrong-length@sha256:d34db33fd34db33f",
    51  	"gcr.io/project-id/missing-digest@",
    52  	// https://github.com/google/go-containerregistry/issues/1394
    53  	"repo@sha256:" + strings.Repeat(":", 64),
    54  	"repo@sha256:" + strings.Repeat("sh", 32),
    55  	"repo@sha256:" + validDigest + "@" + validDigest,
    56  }
    57  
    58  func TestNewDigestStrictValidation(t *testing.T) {
    59  	t.Parallel()
    60  
    61  	for _, name := range goodStrictValidationDigestNames {
    62  		if digest, err := NewDigest(name, StrictValidation); err != nil {
    63  			t.Errorf("`%s` should be a valid Digest name, got error: %v", name, err)
    64  		} else if digest.Name() != name {
    65  			t.Errorf("`%v` .Name() should reproduce the original name. Wanted: %s Got: %s", digest, name, digest.Name())
    66  		}
    67  	}
    68  
    69  	for _, name := range goodStrictValidationTagDigestNames {
    70  		if _, err := NewDigest(name, StrictValidation); err != nil {
    71  			t.Errorf("`%s` should be a valid Digest name, got error: %v", name, err)
    72  		}
    73  	}
    74  
    75  	for _, name := range append(goodWeakValidationDigestNames, badDigestNames...) {
    76  		if repo, err := NewDigest(name, StrictValidation); err == nil {
    77  			t.Errorf("`%s` should be an invalid Digest name, got Digest: %#v", name, repo)
    78  		}
    79  	}
    80  }
    81  
    82  func TestNewDigest(t *testing.T) {
    83  	t.Parallel()
    84  
    85  	for _, name := range append(goodStrictValidationDigestNames, append(goodWeakValidationDigestNames, goodWeakValidationTagDigestNames...)...) {
    86  		if _, err := NewDigest(name, WeakValidation); err != nil {
    87  			t.Errorf("`%s` should be a valid Digest name, got error: %v", name, err)
    88  		}
    89  	}
    90  
    91  	for _, name := range badDigestNames {
    92  		if repo, err := NewDigest(name, WeakValidation); err == nil {
    93  			t.Errorf("`%s` should be an invalid Digest name, got Digest: %#v", name, repo)
    94  		}
    95  	}
    96  }
    97  
    98  func TestDigestComponents(t *testing.T) {
    99  	t.Parallel()
   100  	testRegistry := "gcr.io"
   101  	testRepository := "project-id/image"
   102  	fullRepo := path.Join(testRegistry, testRepository)
   103  
   104  	digestNameStr := testRegistry + "/" + testRepository + "@" + validDigest
   105  	digest, err := NewDigest(digestNameStr, StrictValidation)
   106  	if err != nil {
   107  		t.Fatalf("`%s` should be a valid Digest name, got error: %v", digestNameStr, err)
   108  	}
   109  
   110  	if got := digest.String(); got != digestNameStr {
   111  		t.Errorf("String() was incorrect for %v. Wanted: `%s` Got: `%s`", digest, digestNameStr, got)
   112  	}
   113  	if got := digest.Identifier(); got != validDigest {
   114  		t.Errorf("Identifier() was incorrect for %v. Wanted: `%s` Got: `%s`", digest, validDigest, got)
   115  	}
   116  	actualRegistry := digest.RegistryStr()
   117  	if actualRegistry != testRegistry {
   118  		t.Errorf("RegistryStr() was incorrect for %v. Wanted: `%s` Got: `%s`", digest, testRegistry, actualRegistry)
   119  	}
   120  	actualRepository := digest.RepositoryStr()
   121  	if actualRepository != testRepository {
   122  		t.Errorf("RepositoryStr() was incorrect for %v. Wanted: `%s` Got: `%s`", digest, testRepository, actualRepository)
   123  	}
   124  	contextRepo := digest.Context().String()
   125  	if contextRepo != fullRepo {
   126  		t.Errorf("Context().String() was incorrect for %v. Wanted: `%s` Got: `%s`", digest, fullRepo, contextRepo)
   127  	}
   128  	actualDigest := digest.DigestStr()
   129  	if actualDigest != validDigest {
   130  		t.Errorf("DigestStr() was incorrect for %v. Wanted: `%s` Got: `%s`", digest, validDigest, actualDigest)
   131  	}
   132  }
   133  
   134  func TestDigestScopes(t *testing.T) {
   135  	t.Parallel()
   136  	testRegistry := "gcr.io"
   137  	testRepo := "project-id/image"
   138  	testAction := "pull"
   139  
   140  	expectedScope := strings.Join([]string{"repository", testRepo, testAction}, ":")
   141  
   142  	digestNameStr := testRegistry + "/" + testRepo + "@" + validDigest
   143  	digest, err := NewDigest(digestNameStr, StrictValidation)
   144  	if err != nil {
   145  		t.Fatalf("`%s` should be a valid Digest name, got error: %v", digestNameStr, err)
   146  	}
   147  
   148  	actualScope := digest.Scope(testAction)
   149  	if actualScope != expectedScope {
   150  		t.Errorf("scope was incorrect for %v. Wanted: `%s` Got: `%s`", digest, expectedScope, actualScope)
   151  	}
   152  }
   153  

View as plain text