2014-11-19 23:10:23 +00:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
mrand "math/rand"
|
|
|
|
"time"
|
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
"github.com/distribution/distribution/v3"
|
|
|
|
"github.com/distribution/distribution/v3/context"
|
2016-12-17 00:28:34 +00:00
|
|
|
"github.com/opencontainers/go-digest"
|
2014-11-19 23:10:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// CreateRandomTarFile creates a random tarfile, returning it as an
|
2015-12-16 01:18:13 +00:00
|
|
|
// io.ReadSeeker along with its digest. An error is returned if there is a
|
2014-11-19 23:10:23 +00:00
|
|
|
// problem generating valid content.
|
2015-12-16 01:18:13 +00:00
|
|
|
func CreateRandomTarFile() (rs io.ReadSeeker, dgst digest.Digest, err error) {
|
2014-11-19 23:10:23 +00:00
|
|
|
nFiles := mrand.Intn(10) + 10
|
|
|
|
target := &bytes.Buffer{}
|
|
|
|
wr := tar.NewWriter(target)
|
|
|
|
|
|
|
|
// Perturb this on each iteration of the loop below.
|
|
|
|
header := &tar.Header{
|
2022-11-02 21:05:45 +00:00
|
|
|
Mode: 0o644,
|
2014-11-19 23:10:23 +00:00
|
|
|
ModTime: time.Now(),
|
|
|
|
Typeflag: tar.TypeReg,
|
|
|
|
Uname: "randocalrissian",
|
|
|
|
Gname: "cloudcity",
|
|
|
|
AccessTime: time.Now(),
|
|
|
|
ChangeTime: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for fileNumber := 0; fileNumber < nFiles; fileNumber++ {
|
|
|
|
fileSize := mrand.Int63n(1<<20) + 1<<20
|
|
|
|
|
|
|
|
header.Name = fmt.Sprint(fileNumber)
|
|
|
|
header.Size = fileSize
|
|
|
|
|
|
|
|
if err := wr.WriteHeader(header); err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
randomData := make([]byte, fileSize)
|
|
|
|
|
|
|
|
// Fill up the buffer with some random data.
|
2016-08-09 00:16:23 +00:00
|
|
|
n, err := mrand.Read(randomData)
|
2014-11-19 23:10:23 +00:00
|
|
|
|
|
|
|
if n != len(randomData) {
|
|
|
|
return nil, "", fmt.Errorf("short read creating random reader: %v bytes != %v bytes", n, len(randomData))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
nn, err := io.Copy(wr, bytes.NewReader(randomData))
|
|
|
|
if nn != fileSize {
|
|
|
|
return nil, "", fmt.Errorf("short copy writing random file to tar")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := wr.Flush(); err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := wr.Close(); err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
2015-12-16 01:18:13 +00:00
|
|
|
dgst = digest.FromBytes(target.Bytes())
|
2014-11-19 23:10:23 +00:00
|
|
|
|
2015-12-16 01:18:13 +00:00
|
|
|
return bytes.NewReader(target.Bytes()), dgst, nil
|
2014-11-19 23:10:23 +00:00
|
|
|
}
|
2016-01-19 22:26:15 +00:00
|
|
|
|
|
|
|
// CreateRandomLayers returns a map of n digests. We don't particularly care
|
|
|
|
// about the order of said digests (since they're all random anyway).
|
|
|
|
func CreateRandomLayers(n int) (map[digest.Digest]io.ReadSeeker, error) {
|
|
|
|
digestMap := map[digest.Digest]io.ReadSeeker{}
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
rs, ds, err := CreateRandomTarFile()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unexpected error generating test layer file: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-08-06 21:34:15 +00:00
|
|
|
digestMap[ds] = rs
|
2016-01-19 22:26:15 +00:00
|
|
|
}
|
|
|
|
return digestMap, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UploadBlobs lets you upload blobs to a repository
|
|
|
|
func UploadBlobs(repository distribution.Repository, layers map[digest.Digest]io.ReadSeeker) error {
|
|
|
|
ctx := context.Background()
|
2022-11-26 12:34:11 +00:00
|
|
|
for dgst, rs := range layers {
|
2016-01-19 22:26:15 +00:00
|
|
|
wr, err := repository.Blobs(ctx).Create(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unexpected error creating upload: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := io.Copy(wr, rs); err != nil {
|
|
|
|
return fmt.Errorf("unexpected error copying to upload: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-11-26 12:34:11 +00:00
|
|
|
if _, err := wr.Commit(ctx, distribution.Descriptor{Digest: dgst}); err != nil {
|
2016-01-19 22:26:15 +00:00
|
|
|
return fmt.Errorf("unexpected error committinng upload: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|