github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/reference/store_test.go (about)

     1  package reference
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/docker/distribution/reference"
    12  	digest "github.com/opencontainers/go-digest"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  var (
    18  	saveLoadTestCases = map[string]digest.Digest{
    19  		"registry:5000/foobar:HEAD":                                                        "sha256:470022b8af682154f57a2163d030eb369549549cba00edc69e1b99b46bb924d6",
    20  		"registry:5000/foobar:alternate":                                                   "sha256:ae300ebc4a4f00693702cfb0a5e0b7bc527b353828dc86ad09fb95c8a681b793",
    21  		"registry:5000/foobar:latest":                                                      "sha256:6153498b9ac00968d71b66cca4eac37e990b5f9eb50c26877eb8799c8847451b",
    22  		"registry:5000/foobar:master":                                                      "sha256:6c9917af4c4e05001b346421959d7ea81b6dc9d25718466a37a6add865dfd7fc",
    23  		"jess/hollywood:latest":                                                            "sha256:ae7a5519a0a55a2d4ef20ddcbd5d0ca0888a1f7ab806acc8e2a27baf46f529fe",
    24  		"registry@sha256:367eb40fd0330a7e464777121e39d2f5b3e8e23a1e159342e53ab05c9e4d94e6": "sha256:24126a56805beb9711be5f4590cc2eb55ab8d4a85ebd618eed72bb19fc50631c",
    25  		"busybox:latest": "sha256:91e54dfb11794fad694460162bf0cb0a4fa710cfa3f60979c177d920813e267c",
    26  	}
    27  
    28  	marshalledSaveLoadTestCases = []byte(`{"Repositories":{"busybox":{"busybox:latest":"sha256:91e54dfb11794fad694460162bf0cb0a4fa710cfa3f60979c177d920813e267c"},"jess/hollywood":{"jess/hollywood:latest":"sha256:ae7a5519a0a55a2d4ef20ddcbd5d0ca0888a1f7ab806acc8e2a27baf46f529fe"},"registry":{"registry@sha256:367eb40fd0330a7e464777121e39d2f5b3e8e23a1e159342e53ab05c9e4d94e6":"sha256:24126a56805beb9711be5f4590cc2eb55ab8d4a85ebd618eed72bb19fc50631c"},"registry:5000/foobar":{"registry:5000/foobar:HEAD":"sha256:470022b8af682154f57a2163d030eb369549549cba00edc69e1b99b46bb924d6","registry:5000/foobar:alternate":"sha256:ae300ebc4a4f00693702cfb0a5e0b7bc527b353828dc86ad09fb95c8a681b793","registry:5000/foobar:latest":"sha256:6153498b9ac00968d71b66cca4eac37e990b5f9eb50c26877eb8799c8847451b","registry:5000/foobar:master":"sha256:6c9917af4c4e05001b346421959d7ea81b6dc9d25718466a37a6add865dfd7fc"}}}`)
    29  )
    30  
    31  func TestLoad(t *testing.T) {
    32  	jsonFile, err := ioutil.TempFile("", "tag-store-test")
    33  	if err != nil {
    34  		t.Fatalf("error creating temp file: %v", err)
    35  	}
    36  	defer os.RemoveAll(jsonFile.Name())
    37  
    38  	// Write canned json to the temp file
    39  	_, err = jsonFile.Write(marshalledSaveLoadTestCases)
    40  	if err != nil {
    41  		t.Fatalf("error writing to temp file: %v", err)
    42  	}
    43  	jsonFile.Close()
    44  
    45  	store, err := NewReferenceStore(jsonFile.Name())
    46  	if err != nil {
    47  		t.Fatalf("error creating tag store: %v", err)
    48  	}
    49  
    50  	for refStr, expectedID := range saveLoadTestCases {
    51  		ref, err := reference.ParseNormalizedNamed(refStr)
    52  		if err != nil {
    53  			t.Fatalf("failed to parse reference: %v", err)
    54  		}
    55  		id, err := store.Get(ref)
    56  		if err != nil {
    57  			t.Fatalf("could not find reference %s: %v", refStr, err)
    58  		}
    59  		if id != expectedID {
    60  			t.Fatalf("expected %s - got %s", expectedID, id)
    61  		}
    62  	}
    63  }
    64  
    65  func TestSave(t *testing.T) {
    66  	jsonFile, err := ioutil.TempFile("", "tag-store-test")
    67  	require.NoError(t, err)
    68  
    69  	_, err = jsonFile.Write([]byte(`{}`))
    70  	require.NoError(t, err)
    71  	jsonFile.Close()
    72  	defer os.RemoveAll(jsonFile.Name())
    73  
    74  	store, err := NewReferenceStore(jsonFile.Name())
    75  	if err != nil {
    76  		t.Fatalf("error creating tag store: %v", err)
    77  	}
    78  
    79  	for refStr, id := range saveLoadTestCases {
    80  		ref, err := reference.ParseNormalizedNamed(refStr)
    81  		if err != nil {
    82  			t.Fatalf("failed to parse reference: %v", err)
    83  		}
    84  		if canonical, ok := ref.(reference.Canonical); ok {
    85  			err = store.AddDigest(canonical, id, false)
    86  			if err != nil {
    87  				t.Fatalf("could not add digest reference %s: %v", refStr, err)
    88  			}
    89  		} else {
    90  			err = store.AddTag(ref, id, false)
    91  			if err != nil {
    92  				t.Fatalf("could not add reference %s: %v", refStr, err)
    93  			}
    94  		}
    95  	}
    96  
    97  	jsonBytes, err := ioutil.ReadFile(jsonFile.Name())
    98  	if err != nil {
    99  		t.Fatalf("could not read json file: %v", err)
   100  	}
   101  
   102  	if !bytes.Equal(jsonBytes, marshalledSaveLoadTestCases) {
   103  		t.Fatalf("save output did not match expectations\nexpected:\n%s\ngot:\n%s", marshalledSaveLoadTestCases, jsonBytes)
   104  	}
   105  }
   106  
   107  func TestAddDeleteGet(t *testing.T) {
   108  	jsonFile, err := ioutil.TempFile("", "tag-store-test")
   109  	if err != nil {
   110  		t.Fatalf("error creating temp file: %v", err)
   111  	}
   112  	_, err = jsonFile.Write([]byte(`{}`))
   113  	jsonFile.Close()
   114  	defer os.RemoveAll(jsonFile.Name())
   115  
   116  	store, err := NewReferenceStore(jsonFile.Name())
   117  	if err != nil {
   118  		t.Fatalf("error creating tag store: %v", err)
   119  	}
   120  
   121  	testImageID1 := digest.Digest("sha256:9655aef5fd742a1b4e1b7b163aa9f1c76c186304bf39102283d80927c916ca9c")
   122  	testImageID2 := digest.Digest("sha256:9655aef5fd742a1b4e1b7b163aa9f1c76c186304bf39102283d80927c916ca9d")
   123  	testImageID3 := digest.Digest("sha256:9655aef5fd742a1b4e1b7b163aa9f1c76c186304bf39102283d80927c916ca9e")
   124  
   125  	// Try adding a reference with no tag or digest
   126  	nameOnly, err := reference.ParseNormalizedNamed("username/repo")
   127  	if err != nil {
   128  		t.Fatalf("could not parse reference: %v", err)
   129  	}
   130  	if err = store.AddTag(nameOnly, testImageID1, false); err != nil {
   131  		t.Fatalf("error adding to store: %v", err)
   132  	}
   133  
   134  	// Add a few references
   135  	ref1, err := reference.ParseNormalizedNamed("username/repo1:latest")
   136  	if err != nil {
   137  		t.Fatalf("could not parse reference: %v", err)
   138  	}
   139  	if err = store.AddTag(ref1, testImageID1, false); err != nil {
   140  		t.Fatalf("error adding to store: %v", err)
   141  	}
   142  
   143  	ref2, err := reference.ParseNormalizedNamed("username/repo1:old")
   144  	if err != nil {
   145  		t.Fatalf("could not parse reference: %v", err)
   146  	}
   147  	if err = store.AddTag(ref2, testImageID2, false); err != nil {
   148  		t.Fatalf("error adding to store: %v", err)
   149  	}
   150  
   151  	ref3, err := reference.ParseNormalizedNamed("username/repo1:alias")
   152  	if err != nil {
   153  		t.Fatalf("could not parse reference: %v", err)
   154  	}
   155  	if err = store.AddTag(ref3, testImageID1, false); err != nil {
   156  		t.Fatalf("error adding to store: %v", err)
   157  	}
   158  
   159  	ref4, err := reference.ParseNormalizedNamed("username/repo2:latest")
   160  	if err != nil {
   161  		t.Fatalf("could not parse reference: %v", err)
   162  	}
   163  	if err = store.AddTag(ref4, testImageID2, false); err != nil {
   164  		t.Fatalf("error adding to store: %v", err)
   165  	}
   166  
   167  	ref5, err := reference.ParseNormalizedNamed("username/repo3@sha256:58153dfb11794fad694460162bf0cb0a4fa710cfa3f60979c177d920813e267c")
   168  	if err != nil {
   169  		t.Fatalf("could not parse reference: %v", err)
   170  	}
   171  	if err = store.AddDigest(ref5.(reference.Canonical), testImageID2, false); err != nil {
   172  		t.Fatalf("error adding to store: %v", err)
   173  	}
   174  
   175  	// Attempt to overwrite with force == false
   176  	if err = store.AddTag(ref4, testImageID3, false); err == nil || !strings.HasPrefix(err.Error(), "Conflict:") {
   177  		t.Fatalf("did not get expected error on overwrite attempt - got %v", err)
   178  	}
   179  	// Repeat to overwrite with force == true
   180  	if err = store.AddTag(ref4, testImageID3, true); err != nil {
   181  		t.Fatalf("failed to force tag overwrite: %v", err)
   182  	}
   183  
   184  	// Check references so far
   185  	id, err := store.Get(nameOnly)
   186  	if err != nil {
   187  		t.Fatalf("Get returned error: %v", err)
   188  	}
   189  	if id != testImageID1 {
   190  		t.Fatalf("id mismatch: got %s instead of %s", id.String(), testImageID1.String())
   191  	}
   192  
   193  	id, err = store.Get(ref1)
   194  	if err != nil {
   195  		t.Fatalf("Get returned error: %v", err)
   196  	}
   197  	if id != testImageID1 {
   198  		t.Fatalf("id mismatch: got %s instead of %s", id.String(), testImageID1.String())
   199  	}
   200  
   201  	id, err = store.Get(ref2)
   202  	if err != nil {
   203  		t.Fatalf("Get returned error: %v", err)
   204  	}
   205  	if id != testImageID2 {
   206  		t.Fatalf("id mismatch: got %s instead of %s", id.String(), testImageID2.String())
   207  	}
   208  
   209  	id, err = store.Get(ref3)
   210  	if err != nil {
   211  		t.Fatalf("Get returned error: %v", err)
   212  	}
   213  	if id != testImageID1 {
   214  		t.Fatalf("id mismatch: got %s instead of %s", id.String(), testImageID1.String())
   215  	}
   216  
   217  	id, err = store.Get(ref4)
   218  	if err != nil {
   219  		t.Fatalf("Get returned error: %v", err)
   220  	}
   221  	if id != testImageID3 {
   222  		t.Fatalf("id mismatch: got %s instead of %s", id.String(), testImageID3.String())
   223  	}
   224  
   225  	id, err = store.Get(ref5)
   226  	if err != nil {
   227  		t.Fatalf("Get returned error: %v", err)
   228  	}
   229  	if id != testImageID2 {
   230  		t.Fatalf("id mismatch: got %s instead of %s", id.String(), testImageID3.String())
   231  	}
   232  
   233  	// Get should return ErrDoesNotExist for a nonexistent repo
   234  	nonExistRepo, err := reference.ParseNormalizedNamed("username/nonexistrepo:latest")
   235  	if err != nil {
   236  		t.Fatalf("could not parse reference: %v", err)
   237  	}
   238  	if _, err = store.Get(nonExistRepo); err != ErrDoesNotExist {
   239  		t.Fatal("Expected ErrDoesNotExist from Get")
   240  	}
   241  
   242  	// Get should return ErrDoesNotExist for a nonexistent tag
   243  	nonExistTag, err := reference.ParseNormalizedNamed("username/repo1:nonexist")
   244  	if err != nil {
   245  		t.Fatalf("could not parse reference: %v", err)
   246  	}
   247  	if _, err = store.Get(nonExistTag); err != ErrDoesNotExist {
   248  		t.Fatal("Expected ErrDoesNotExist from Get")
   249  	}
   250  
   251  	// Check References
   252  	refs := store.References(testImageID1)
   253  	if len(refs) != 3 {
   254  		t.Fatal("unexpected number of references")
   255  	}
   256  	// Looking for the references in this order verifies that they are
   257  	// returned lexically sorted.
   258  	if refs[0].String() != ref3.String() {
   259  		t.Fatalf("unexpected reference: %v", refs[0].String())
   260  	}
   261  	if refs[1].String() != ref1.String() {
   262  		t.Fatalf("unexpected reference: %v", refs[1].String())
   263  	}
   264  	if refs[2].String() != nameOnly.String()+":latest" {
   265  		t.Fatalf("unexpected reference: %v", refs[2].String())
   266  	}
   267  
   268  	// Check ReferencesByName
   269  	repoName, err := reference.ParseNormalizedNamed("username/repo1")
   270  	if err != nil {
   271  		t.Fatalf("could not parse reference: %v", err)
   272  	}
   273  	associations := store.ReferencesByName(repoName)
   274  	if len(associations) != 3 {
   275  		t.Fatal("unexpected number of associations")
   276  	}
   277  	// Looking for the associations in this order verifies that they are
   278  	// returned lexically sorted.
   279  	if associations[0].Ref.String() != ref3.String() {
   280  		t.Fatalf("unexpected reference: %v", associations[0].Ref.String())
   281  	}
   282  	if associations[0].ID != testImageID1 {
   283  		t.Fatalf("unexpected reference: %v", associations[0].Ref.String())
   284  	}
   285  	if associations[1].Ref.String() != ref1.String() {
   286  		t.Fatalf("unexpected reference: %v", associations[1].Ref.String())
   287  	}
   288  	if associations[1].ID != testImageID1 {
   289  		t.Fatalf("unexpected reference: %v", associations[1].Ref.String())
   290  	}
   291  	if associations[2].Ref.String() != ref2.String() {
   292  		t.Fatalf("unexpected reference: %v", associations[2].Ref.String())
   293  	}
   294  	if associations[2].ID != testImageID2 {
   295  		t.Fatalf("unexpected reference: %v", associations[2].Ref.String())
   296  	}
   297  
   298  	// Delete should return ErrDoesNotExist for a nonexistent repo
   299  	if _, err = store.Delete(nonExistRepo); err != ErrDoesNotExist {
   300  		t.Fatal("Expected ErrDoesNotExist from Delete")
   301  	}
   302  
   303  	// Delete should return ErrDoesNotExist for a nonexistent tag
   304  	if _, err = store.Delete(nonExistTag); err != ErrDoesNotExist {
   305  		t.Fatal("Expected ErrDoesNotExist from Delete")
   306  	}
   307  
   308  	// Delete a few references
   309  	if deleted, err := store.Delete(ref1); err != nil || !deleted {
   310  		t.Fatal("Delete failed")
   311  	}
   312  	if _, err := store.Get(ref1); err != ErrDoesNotExist {
   313  		t.Fatal("Expected ErrDoesNotExist from Get")
   314  	}
   315  	if deleted, err := store.Delete(ref5); err != nil || !deleted {
   316  		t.Fatal("Delete failed")
   317  	}
   318  	if _, err := store.Get(ref5); err != ErrDoesNotExist {
   319  		t.Fatal("Expected ErrDoesNotExist from Get")
   320  	}
   321  	if deleted, err := store.Delete(nameOnly); err != nil || !deleted {
   322  		t.Fatal("Delete failed")
   323  	}
   324  	if _, err := store.Get(nameOnly); err != ErrDoesNotExist {
   325  		t.Fatal("Expected ErrDoesNotExist from Get")
   326  	}
   327  }
   328  
   329  func TestInvalidTags(t *testing.T) {
   330  	tmpDir, err := ioutil.TempDir("", "tag-store-test")
   331  	require.NoError(t, err)
   332  	defer os.RemoveAll(tmpDir)
   333  
   334  	store, err := NewReferenceStore(filepath.Join(tmpDir, "repositories.json"))
   335  	require.NoError(t, err)
   336  	id := digest.Digest("sha256:470022b8af682154f57a2163d030eb369549549cba00edc69e1b99b46bb924d6")
   337  
   338  	// sha256 as repo name
   339  	ref, err := reference.ParseNormalizedNamed("sha256:abc")
   340  	require.NoError(t, err)
   341  	err = store.AddTag(ref, id, true)
   342  	assert.Error(t, err)
   343  
   344  	// setting digest as a tag
   345  	ref, err = reference.ParseNormalizedNamed("registry@sha256:367eb40fd0330a7e464777121e39d2f5b3e8e23a1e159342e53ab05c9e4d94e6")
   346  	require.NoError(t, err)
   347  
   348  	err = store.AddTag(ref, id, true)
   349  	assert.Error(t, err)
   350  }