github.com/uber/kraken@v0.1.4/core/fixtures.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 core
    15  
    16  import (
    17  	"bytes"
    18  	"fmt"
    19  
    20  	"github.com/uber/kraken/utils/randutil"
    21  )
    22  
    23  // BlobFixture joins all information associated with a blob for testing convenience.
    24  type BlobFixture struct {
    25  	Content  []byte
    26  	Digest   Digest
    27  	MetaInfo *MetaInfo
    28  }
    29  
    30  // Length returns the length of the blob.
    31  func (f *BlobFixture) Length() int64 {
    32  	return int64(len(f.Content))
    33  }
    34  
    35  // Info returns a BlobInfo for f.
    36  func (f *BlobFixture) Info() *BlobInfo {
    37  	return NewBlobInfo(f.Length())
    38  }
    39  
    40  // CustomBlobFixture creates a BlobFixture with custom fields.
    41  func CustomBlobFixture(content []byte, digest Digest, mi *MetaInfo) *BlobFixture {
    42  	return &BlobFixture{content, digest, mi}
    43  }
    44  
    45  // SizedBlobFixture creates a randomly generated BlobFixture of given size with given piece lengths.
    46  func SizedBlobFixture(size uint64, pieceLength uint64) *BlobFixture {
    47  	b := randutil.Text(size)
    48  	d, err := NewDigester().FromBytes(b)
    49  	if err != nil {
    50  		panic(err)
    51  	}
    52  	mi, err := NewMetaInfo(d, bytes.NewReader(b), int64(pieceLength))
    53  	if err != nil {
    54  		panic(err)
    55  	}
    56  	return &BlobFixture{
    57  		Content:  b,
    58  		Digest:   d,
    59  		MetaInfo: mi,
    60  	}
    61  }
    62  
    63  // NewBlobFixture creates a randomly generated BlobFixture.
    64  func NewBlobFixture() *BlobFixture {
    65  	return SizedBlobFixture(256, 8)
    66  }
    67  
    68  // PeerIDFixture returns a randomly generated PeerID.
    69  func PeerIDFixture() PeerID {
    70  	p, err := RandomPeerID()
    71  	if err != nil {
    72  		panic(err)
    73  	}
    74  	return p
    75  }
    76  
    77  // PeerInfoFixture returns a randomly generated PeerInfo.
    78  func PeerInfoFixture() *PeerInfo {
    79  	return NewPeerInfo(PeerIDFixture(), randutil.IP(), randutil.Port(), false, false)
    80  }
    81  
    82  // OriginPeerInfoFixture returns a randomly generated PeerInfo for an origin.
    83  func OriginPeerInfoFixture() *PeerInfo {
    84  	return NewPeerInfo(PeerIDFixture(), randutil.IP(), randutil.Port(), true, true)
    85  }
    86  
    87  // MetaInfoFixture returns a randomly generated MetaInfo.
    88  func MetaInfoFixture() *MetaInfo {
    89  	return NewBlobFixture().MetaInfo
    90  }
    91  
    92  // InfoHashFixture returns a randomly generated InfoHash.
    93  func InfoHashFixture() InfoHash {
    94  	return MetaInfoFixture().InfoHash()
    95  }
    96  
    97  // DigestFixture returns a random Digest.
    98  func DigestFixture() Digest {
    99  	return NewBlobFixture().Digest
   100  }
   101  
   102  // DigestListFixture returns a list of random Digests.
   103  func DigestListFixture(n int) []Digest {
   104  	var l DigestList
   105  	for i := 0; i < n; i++ {
   106  		l = append(l, DigestFixture())
   107  	}
   108  	return l
   109  }
   110  
   111  // PeerContextFixture returns a randomly generated PeerContext.
   112  func PeerContextFixture() PeerContext {
   113  	pctx, err := NewPeerContext(
   114  		RandomPeerIDFactory,
   115  		"zone1",
   116  		"test01-zone1",
   117  		randutil.IP(),
   118  		randutil.Port(),
   119  		false)
   120  	if err != nil {
   121  		panic(err)
   122  	}
   123  	return pctx
   124  }
   125  
   126  // OriginContextFixture returns a randomly generated origin PeerContext.
   127  func OriginContextFixture() PeerContext {
   128  	octx := PeerContextFixture()
   129  	octx.Origin = true
   130  	return octx
   131  }
   132  
   133  // TagFixture creates a random tag for service repo-bar.
   134  func TagFixture() string {
   135  	return fmt.Sprintf("namespace-foo/repo-bar:%s", randutil.Text(8))
   136  }
   137  
   138  // NamespaceFixture creates a random namespace.
   139  func NamespaceFixture() string {
   140  	return fmt.Sprintf("namespace-foo/repo-bar-%s", randutil.Text(8))
   141  }