github.com/uber/kraken@v0.1.4/lib/dockerregistry/transfer/testing.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 transfer
    15  
    16  import (
    17  	"errors"
    18  	"fmt"
    19  	"path"
    20  	"strings"
    21  
    22  	"github.com/uber/kraken/core"
    23  	"github.com/uber/kraken/lib/backend/namepath"
    24  	"github.com/uber/kraken/lib/store"
    25  )
    26  
    27  type testTransferer struct {
    28  	tagPather namepath.Pather
    29  	tags      map[string]core.Digest
    30  	cas       *store.CAStore
    31  }
    32  
    33  // NewTestTransferer creates a Transferer which stores blobs in cas and tags in
    34  // memory for testing purposes.
    35  func NewTestTransferer(cas *store.CAStore) ImageTransferer {
    36  	tagPather, err := namepath.New("", namepath.DockerTag)
    37  	if err != nil {
    38  		panic(err)
    39  	}
    40  	return &testTransferer{
    41  		tagPather: tagPather,
    42  		tags:      make(map[string]core.Digest),
    43  		cas:       cas,
    44  	}
    45  }
    46  
    47  // Stat returns blob info from local cache.
    48  func (t *testTransferer) Stat(namespace string, d core.Digest) (*core.BlobInfo, error) {
    49  	fi, err := t.cas.GetCacheFileStat(d.Hex())
    50  	if err != nil {
    51  		return nil, fmt.Errorf("stat cache file: %s", err)
    52  	}
    53  	return core.NewBlobInfo(fi.Size()), nil
    54  }
    55  
    56  func (t *testTransferer) Download(namespace string, d core.Digest) (store.FileReader, error) {
    57  	return t.cas.GetCacheFileReader(d.Hex())
    58  }
    59  
    60  func (t *testTransferer) Upload(namespace string, d core.Digest, blob store.FileReader) error {
    61  	return t.cas.CreateCacheFile(d.Hex(), blob)
    62  }
    63  
    64  func (t *testTransferer) GetTag(tag string) (core.Digest, error) {
    65  	p, err := t.tagPather.BlobPath(tag)
    66  	if err != nil {
    67  		return core.Digest{}, err
    68  	}
    69  	d, ok := t.tags[p]
    70  	if !ok {
    71  		return core.Digest{}, errors.New("tag not found")
    72  	}
    73  	return d, nil
    74  }
    75  
    76  func (t *testTransferer) PutTag(tag string, d core.Digest) error {
    77  	p, err := t.tagPather.BlobPath(tag)
    78  	if err != nil {
    79  		return err
    80  	}
    81  	t.tags[p] = d
    82  	return nil
    83  }
    84  
    85  func (t *testTransferer) ListTags(prefix string) ([]string, error) {
    86  	prefix = path.Join(t.tagPather.BasePath(), prefix)
    87  	var tags []string
    88  	for path := range t.tags {
    89  		if strings.HasPrefix(path, prefix) {
    90  			tag, err := t.tagPather.NameFromBlobPath(path)
    91  			if err != nil {
    92  				return nil, fmt.Errorf("invalid tag path %s: %s", path, err)
    93  			}
    94  			tags = append(tags, tag)
    95  		}
    96  	}
    97  	return tags, nil
    98  }