github.com/uber/kraken@v0.1.4/lib/dockerregistry/testutils_test.go (about)

     1  // Copyright (c) 2016-2019 Uber Technologies, Inc.
     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  package dockerregistry
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  	"log"
    20  	"path"
    21  
    22  	"github.com/uber/kraken/core"
    23  	"github.com/uber/kraken/lib/dockerregistry/transfer"
    24  	"github.com/uber/kraken/lib/store"
    25  	"github.com/uber/kraken/utils/dockerutil"
    26  
    27  	"github.com/uber-go/tally"
    28  )
    29  
    30  const (
    31  	repoName         = "alpine"
    32  	tagName          = "latest"
    33  	hashStateContent = "this is a test hashstate"
    34  	uploadContent    = "this is a test upload"
    35  	uploadUUID       = "a20fe261-0060-467f-a44e-46eba3798d63"
    36  )
    37  
    38  // TODO(codyg): Get rid of this and all of the above constants.
    39  type testImageUploadBundle struct {
    40  	repo     string
    41  	tag      string
    42  	upload   string
    43  	manifest string
    44  	layer1   *core.BlobFixture
    45  	layer2   *core.BlobFixture
    46  }
    47  
    48  type testDriver struct {
    49  	cas        *store.CAStore
    50  	transferer transfer.ImageTransferer
    51  }
    52  
    53  func newTestDriver() (*testDriver, func()) {
    54  	cas, cleanup := store.CAStoreFixture()
    55  	transferer := transfer.NewTestTransferer(cas)
    56  	return &testDriver{cas, transferer}, cleanup
    57  }
    58  
    59  func (d *testDriver) setup() (*KrakenStorageDriver, testImageUploadBundle) {
    60  	sd := NewReadWriteStorageDriver(Config{}, d.cas, d.transferer, tally.NoopScope)
    61  
    62  	// Create upload
    63  	path := genUploadStartedAtPath(uploadUUID)
    64  	if err := sd.uploads.putContent(path, _startedat, nil); err != nil {
    65  		log.Panic(err)
    66  	}
    67  	path = genUploadHashStatesPath(uploadUUID)
    68  	if err := sd.uploads.putContent(path, _hashstates, []byte(hashStateContent)); err != nil {
    69  		log.Panic(err)
    70  	}
    71  	path = genUploadDataPath(uploadUUID)
    72  
    73  	writer, err := d.cas.GetUploadFileReadWriter(uploadUUID)
    74  	if err != nil {
    75  		log.Panic(err)
    76  	}
    77  	defer writer.Close()
    78  	writer.Write([]byte(uploadContent))
    79  
    80  	config := core.NewBlobFixture()
    81  	layer1 := core.NewBlobFixture()
    82  	layer2 := core.NewBlobFixture()
    83  
    84  	manifestDigest, manifestRaw := dockerutil.ManifestFixture(
    85  		config.Digest, layer1.Digest, layer2.Digest)
    86  
    87  	for _, blob := range []*core.BlobFixture{config, layer1, layer2} {
    88  		err := d.transferer.Upload("unused", blob.Digest, store.NewBufferFileReader(blob.Content))
    89  		if err != nil {
    90  			log.Panic(err)
    91  		}
    92  	}
    93  	err = d.transferer.Upload("unused", manifestDigest, store.NewBufferFileReader(manifestRaw))
    94  	if err != nil {
    95  		log.Panic(err)
    96  	}
    97  
    98  	if err := d.transferer.PutTag(fmt.Sprintf("%s:%s", repoName, tagName), manifestDigest); err != nil {
    99  		log.Panic(err)
   100  	}
   101  
   102  	return sd, testImageUploadBundle{
   103  		repo:     repoName,
   104  		tag:      tagName,
   105  		manifest: manifestDigest.Hex(),
   106  		layer1:   layer1,
   107  		layer2:   layer2,
   108  		upload:   uploadUUID,
   109  	}
   110  }
   111  
   112  func genLayerLinkPath(layerDigest string) string {
   113  	return fmt.Sprintf("/docker/registry/v2/repositories/alpine/_layers/sha256/%s/link", layerDigest)
   114  }
   115  
   116  func genUploadStartedAtPath(uuid string) string {
   117  	return fmt.Sprintf("/docker/registry/v2/repositories/alpine/_uploads/%s/startedat", uuid)
   118  }
   119  
   120  func genUploadHashStatesPath(uuid string) string {
   121  	return fmt.Sprintf("localstore/_uploads/%s/hashstates/sha256/1928129", uuid)
   122  }
   123  
   124  func genUploadDataPath(uuid string) string {
   125  	return fmt.Sprintf("/docker/registry/v2/repositories/alpine/_uploads/%s/data", uuid)
   126  }
   127  
   128  func genManifestTagCurrentLinkPath(repo, tag, manifest string) string {
   129  	return fmt.Sprintf("/docker/registry/v2/repositories/%s/_manifests/tags/%s/current/link", repo, tag)
   130  }
   131  
   132  func genManifestTagShaLinkPath(repo, tag, manifest string) string {
   133  	return fmt.Sprintf("/docker/registry/v2/repositories/%s/_manifests/tags/%s/index/sha256/%s/link", repo, tag, manifest)
   134  }
   135  
   136  func genManifestRevisionLinkPath(repo, manifest string) string {
   137  	return fmt.Sprintf("/docker/registry/v2/repositories/%s/_manifests/revisions/sha256/%s/link", repo, manifest)
   138  }
   139  
   140  func genBlobDataPath(digest string) string {
   141  	return fmt.Sprintf("/docker/registry/v2/blobs/sha256/%s/%s/data", string([]byte(digest)[:2]), digest)
   142  }
   143  
   144  func genManifestListPath(repo string) string {
   145  	return fmt.Sprintf("/docker/registry/v2/repositories/%s/_manifests/tags", repo)
   146  }
   147  
   148  func getShardedRelativePath(name string) string {
   149  	filePath := ""
   150  	for i := 0; i < 2 && i < len(name)/2; i++ {
   151  		// (1 byte = 2 char of file name assumming file name is in HEX)
   152  		dirName := name[i*2 : i*2+2]
   153  		filePath = path.Join(filePath, dirName)
   154  	}
   155  
   156  	return path.Join(filePath, name)
   157  }
   158  
   159  func contextFixture() context.Context {
   160  	return context.WithValue(context.Background(), "vars.name", "dummy")
   161  }