2018-02-05 16:05:59 -05:00
|
|
|
package xfer // import "github.com/docker/docker/distribution/xfer"
|
2015-11-13 19:59:01 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2015-11-13 19:59:01 -05:00
|
|
|
"errors"
|
2016-04-21 12:08:37 -04:00
|
|
|
"fmt"
|
2015-11-13 19:59:01 -05:00
|
|
|
"io"
|
2016-02-28 23:08:34 -05:00
|
|
|
"runtime"
|
2015-11-13 19:59:01 -05:00
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-05-25 22:11:51 -04:00
|
|
|
"github.com/docker/distribution"
|
2015-11-13 19:59:01 -05:00
|
|
|
"github.com/docker/docker/image"
|
|
|
|
"github.com/docker/docker/layer"
|
|
|
|
"github.com/docker/docker/pkg/progress"
|
2022-03-04 08:49:42 -05:00
|
|
|
"github.com/opencontainers/go-digest"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2015-11-13 19:59:01 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const maxDownloadConcurrency = 3
|
|
|
|
|
|
|
|
type mockLayer struct {
|
|
|
|
layerData bytes.Buffer
|
|
|
|
diffID layer.DiffID
|
|
|
|
chainID layer.ChainID
|
|
|
|
parent layer.Layer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *mockLayer) TarStream() (io.ReadCloser, error) {
|
2021-08-24 06:10:50 -04:00
|
|
|
return io.NopCloser(bytes.NewBuffer(ml.layerData.Bytes())), nil
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
|
2016-04-21 12:08:37 -04:00
|
|
|
func (ml *mockLayer) TarStreamFrom(layer.ChainID) (io.ReadCloser, error) {
|
|
|
|
return nil, fmt.Errorf("not implemented")
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:59:01 -05:00
|
|
|
func (ml *mockLayer) ChainID() layer.ChainID {
|
|
|
|
return ml.chainID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *mockLayer) DiffID() layer.DiffID {
|
|
|
|
return ml.diffID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *mockLayer) Parent() layer.Layer {
|
|
|
|
return ml.parent
|
|
|
|
}
|
|
|
|
|
2022-01-21 13:01:34 -05:00
|
|
|
func (ml *mockLayer) Size() int64 {
|
|
|
|
return 0
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
|
2022-01-21 13:01:34 -05:00
|
|
|
func (ml *mockLayer) DiffSize() int64 {
|
|
|
|
return 0
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *mockLayer) Metadata() (map[string]string, error) {
|
|
|
|
return make(map[string]string), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockLayerStore struct {
|
|
|
|
layers map[layer.ChainID]*mockLayer
|
|
|
|
}
|
|
|
|
|
|
|
|
func createChainIDFromParent(parent layer.ChainID, dgsts ...layer.DiffID) layer.ChainID {
|
|
|
|
if len(dgsts) == 0 {
|
|
|
|
return parent
|
|
|
|
}
|
|
|
|
if parent == "" {
|
|
|
|
return createChainIDFromParent(layer.ChainID(dgsts[0]), dgsts[1:]...)
|
|
|
|
}
|
|
|
|
// H = "H(n-1) SHA256(n)"
|
2015-12-08 14:14:02 -05:00
|
|
|
dgst := digest.FromBytes([]byte(string(parent) + " " + string(dgsts[0])))
|
2015-11-13 19:59:01 -05:00
|
|
|
return createChainIDFromParent(layer.ChainID(dgst), dgsts[1:]...)
|
|
|
|
}
|
|
|
|
|
2016-08-23 19:08:43 -04:00
|
|
|
func (ls *mockLayerStore) Map() map[layer.ChainID]layer.Layer {
|
|
|
|
layers := map[layer.ChainID]layer.Layer{}
|
|
|
|
|
|
|
|
for k, v := range ls.layers {
|
|
|
|
layers[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return layers
|
|
|
|
}
|
|
|
|
|
2017-09-19 15:14:46 -04:00
|
|
|
func (ls *mockLayerStore) Register(reader io.Reader, parentID layer.ChainID) (layer.Layer, error) {
|
2016-06-06 20:49:34 -04:00
|
|
|
return ls.RegisterWithDescriptor(reader, parentID, distribution.Descriptor{})
|
2016-05-25 22:11:51 -04:00
|
|
|
}
|
|
|
|
|
2016-06-06 20:49:34 -04:00
|
|
|
func (ls *mockLayerStore) RegisterWithDescriptor(reader io.Reader, parentID layer.ChainID, _ distribution.Descriptor) (layer.Layer, error) {
|
2015-11-13 19:59:01 -05:00
|
|
|
var (
|
|
|
|
parent layer.Layer
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if parentID != "" {
|
|
|
|
parent, err = ls.Get(parentID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
l := &mockLayer{parent: parent}
|
|
|
|
_, err = l.layerData.ReadFrom(reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-12-08 14:14:02 -05:00
|
|
|
l.diffID = layer.DiffID(digest.FromBytes(l.layerData.Bytes()))
|
2015-11-13 19:59:01 -05:00
|
|
|
l.chainID = createChainIDFromParent(parentID, l.diffID)
|
|
|
|
|
|
|
|
ls.layers[l.chainID] = l
|
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ls *mockLayerStore) Get(chainID layer.ChainID) (layer.Layer, error) {
|
|
|
|
l, ok := ls.layers[chainID]
|
|
|
|
if !ok {
|
|
|
|
return nil, layer.ErrLayerDoesNotExist
|
|
|
|
}
|
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ls *mockLayerStore) Release(l layer.Layer) ([]layer.Metadata, error) {
|
|
|
|
return []layer.Metadata{}, nil
|
|
|
|
}
|
2017-09-19 15:14:46 -04:00
|
|
|
func (ls *mockLayerStore) CreateRWLayer(string, layer.ChainID, *layer.CreateRWLayerOpts) (layer.RWLayer, error) {
|
2015-11-13 19:59:01 -05:00
|
|
|
return nil, errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2015-12-16 17:13:50 -05:00
|
|
|
func (ls *mockLayerStore) GetRWLayer(string) (layer.RWLayer, error) {
|
2015-11-13 19:59:01 -05:00
|
|
|
return nil, errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2015-12-16 17:13:50 -05:00
|
|
|
func (ls *mockLayerStore) ReleaseRWLayer(layer.RWLayer) ([]layer.Metadata, error) {
|
2015-12-14 19:52:15 -05:00
|
|
|
return nil, errors.New("not implemented")
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
|
|
|
func (ls *mockLayerStore) GetMountID(string) (string, error) {
|
|
|
|
return "", errors.New("not implemented")
|
2015-12-14 19:52:15 -05:00
|
|
|
}
|
|
|
|
|
2015-12-16 15:32:16 -05:00
|
|
|
func (ls *mockLayerStore) Cleanup() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-19 15:14:46 -04:00
|
|
|
func (ls *mockLayerStore) DriverStatus() [][2]string {
|
2015-12-16 15:32:16 -05:00
|
|
|
return [][2]string{}
|
|
|
|
}
|
|
|
|
|
2017-09-19 15:14:46 -04:00
|
|
|
func (ls *mockLayerStore) DriverName() string {
|
2015-12-16 15:32:16 -05:00
|
|
|
return "mock"
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:59:01 -05:00
|
|
|
type mockDownloadDescriptor struct {
|
|
|
|
currentDownloads *int32
|
|
|
|
id string
|
|
|
|
diffID layer.DiffID
|
|
|
|
registeredDiffID layer.DiffID
|
|
|
|
expectedDiffID layer.DiffID
|
|
|
|
simulateRetries int
|
2019-06-25 09:26:36 -04:00
|
|
|
retries int
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Key returns the key used to deduplicate downloads.
|
|
|
|
func (d *mockDownloadDescriptor) Key() string {
|
|
|
|
return d.id
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns the ID for display purposes.
|
|
|
|
func (d *mockDownloadDescriptor) ID() string {
|
|
|
|
return d.id
|
|
|
|
}
|
|
|
|
|
|
|
|
// DiffID should return the DiffID for this layer, or an error
|
|
|
|
// if it is unknown (for example, if it has not been downloaded
|
|
|
|
// before).
|
|
|
|
func (d *mockDownloadDescriptor) DiffID() (layer.DiffID, error) {
|
|
|
|
if d.diffID != "" {
|
|
|
|
return d.diffID, nil
|
|
|
|
}
|
|
|
|
return "", errors.New("no diffID available")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *mockDownloadDescriptor) Registered(diffID layer.DiffID) {
|
|
|
|
d.registeredDiffID = diffID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *mockDownloadDescriptor) mockTarStream() io.ReadCloser {
|
|
|
|
// The mock implementation returns the ID repeated 5 times as a tar
|
|
|
|
// stream instead of actual tar data. The data is ignored except for
|
|
|
|
// computing IDs.
|
2021-08-24 06:10:50 -04:00
|
|
|
return io.NopCloser(bytes.NewBuffer([]byte(d.id + d.id + d.id + d.id + d.id)))
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Download is called to perform the download.
|
|
|
|
func (d *mockDownloadDescriptor) Download(ctx context.Context, progressOutput progress.Output) (io.ReadCloser, int64, error) {
|
|
|
|
if d.currentDownloads != nil {
|
|
|
|
defer atomic.AddInt32(d.currentDownloads, -1)
|
|
|
|
|
|
|
|
if atomic.AddInt32(d.currentDownloads, 1) > maxDownloadConcurrency {
|
|
|
|
return nil, 0, errors.New("concurrency limit exceeded")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sleep a bit to simulate a time-consuming download.
|
|
|
|
for i := int64(0); i <= 10; i++ {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, 0, ctx.Err()
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
|
|
|
progressOutput.WriteProgress(progress.Progress{ID: d.ID(), Action: "Downloading", Current: i, Total: 10})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-25 09:26:36 -04:00
|
|
|
if d.retries < d.simulateRetries {
|
|
|
|
d.retries++
|
|
|
|
return nil, 0, fmt.Errorf("simulating download attempt %d/%d", d.retries, d.simulateRetries)
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return d.mockTarStream(), 0, nil
|
|
|
|
}
|
|
|
|
|
2016-01-25 21:20:18 -05:00
|
|
|
func (d *mockDownloadDescriptor) Close() {
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:59:01 -05:00
|
|
|
func downloadDescriptors(currentDownloads *int32) []DownloadDescriptor {
|
|
|
|
return []DownloadDescriptor{
|
|
|
|
&mockDownloadDescriptor{
|
|
|
|
currentDownloads: currentDownloads,
|
|
|
|
id: "id1",
|
|
|
|
expectedDiffID: layer.DiffID("sha256:68e2c75dc5c78ea9240689c60d7599766c213ae210434c53af18470ae8c53ec1"),
|
|
|
|
},
|
|
|
|
&mockDownloadDescriptor{
|
|
|
|
currentDownloads: currentDownloads,
|
|
|
|
id: "id2",
|
|
|
|
expectedDiffID: layer.DiffID("sha256:64a636223116aa837973a5d9c2bdd17d9b204e4f95ac423e20e65dfbb3655473"),
|
|
|
|
},
|
|
|
|
&mockDownloadDescriptor{
|
|
|
|
currentDownloads: currentDownloads,
|
|
|
|
id: "id3",
|
|
|
|
expectedDiffID: layer.DiffID("sha256:58745a8bbd669c25213e9de578c4da5c8ee1c836b3581432c2b50e38a6753300"),
|
|
|
|
},
|
|
|
|
&mockDownloadDescriptor{
|
|
|
|
currentDownloads: currentDownloads,
|
|
|
|
id: "id2",
|
|
|
|
expectedDiffID: layer.DiffID("sha256:64a636223116aa837973a5d9c2bdd17d9b204e4f95ac423e20e65dfbb3655473"),
|
|
|
|
},
|
|
|
|
&mockDownloadDescriptor{
|
|
|
|
currentDownloads: currentDownloads,
|
|
|
|
id: "id4",
|
|
|
|
expectedDiffID: layer.DiffID("sha256:0dfb5b9577716cc173e95af7c10289322c29a6453a1718addc00c0c5b1330936"),
|
|
|
|
simulateRetries: 1,
|
|
|
|
},
|
|
|
|
&mockDownloadDescriptor{
|
|
|
|
currentDownloads: currentDownloads,
|
|
|
|
id: "id5",
|
|
|
|
expectedDiffID: layer.DiffID("sha256:0a5f25fa1acbc647f6112a6276735d0fa01e4ee2aa7ec33015e337350e1ea23d"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSuccessfulDownload(t *testing.T) {
|
2016-02-28 23:08:34 -05:00
|
|
|
// TODO Windows: Fix this unit text
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("Needs fixing on Windows")
|
|
|
|
}
|
2016-12-23 15:09:29 -05:00
|
|
|
|
2015-11-13 19:59:01 -05:00
|
|
|
layerStore := &mockLayerStore{make(map[layer.ChainID]*mockLayer)}
|
2021-03-19 10:34:08 -04:00
|
|
|
ldm := NewLayerDownloadManager(layerStore, maxDownloadConcurrency, func(m *LayerDownloadManager) { m.waitDuration = time.Millisecond })
|
2015-11-13 19:59:01 -05:00
|
|
|
|
|
|
|
progressChan := make(chan progress.Progress)
|
|
|
|
progressDone := make(chan struct{})
|
2016-01-11 12:46:34 -05:00
|
|
|
receivedProgress := make(map[string]progress.Progress)
|
2015-11-13 19:59:01 -05:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for p := range progressChan {
|
2016-01-11 12:46:34 -05:00
|
|
|
receivedProgress[p.ID] = p
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
close(progressDone)
|
|
|
|
}()
|
|
|
|
|
|
|
|
var currentDownloads int32
|
|
|
|
descriptors := downloadDescriptors(¤tDownloads)
|
|
|
|
|
|
|
|
firstDescriptor := descriptors[0].(*mockDownloadDescriptor)
|
|
|
|
|
|
|
|
// Pre-register the first layer to simulate an already-existing layer
|
2017-09-19 15:14:46 -04:00
|
|
|
l, err := layerStore.Register(firstDescriptor.mockTarStream(), "")
|
2015-11-13 19:59:01 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
firstDescriptor.diffID = l.DiffID()
|
|
|
|
|
2022-01-21 07:34:37 -05:00
|
|
|
rootFS, releaseFunc, err := ldm.Download(context.Background(), *image.NewRootFS(), descriptors, progress.ChanOutput(progressChan))
|
2015-11-13 19:59:01 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("download error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
releaseFunc()
|
|
|
|
|
|
|
|
close(progressChan)
|
|
|
|
<-progressDone
|
|
|
|
|
|
|
|
if len(rootFS.DiffIDs) != len(descriptors) {
|
|
|
|
t.Fatal("got wrong number of diffIDs in rootfs")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, d := range descriptors {
|
|
|
|
descriptor := d.(*mockDownloadDescriptor)
|
|
|
|
|
|
|
|
if descriptor.diffID != "" {
|
2016-01-11 12:46:34 -05:00
|
|
|
if receivedProgress[d.ID()].Action != "Already exists" {
|
|
|
|
t.Fatalf("did not get 'Already exists' message for %v", d.ID())
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
2016-01-11 12:46:34 -05:00
|
|
|
} else if receivedProgress[d.ID()].Action != "Pull complete" {
|
|
|
|
t.Fatalf("did not get 'Pull complete' message for %v", d.ID())
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if rootFS.DiffIDs[i] != descriptor.expectedDiffID {
|
|
|
|
t.Fatalf("rootFS item %d has the wrong diffID (expected: %v got: %v)", i, descriptor.expectedDiffID, rootFS.DiffIDs[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
if descriptor.diffID == "" && descriptor.registeredDiffID != rootFS.DiffIDs[i] {
|
|
|
|
t.Fatal("diffID mismatch between rootFS and Registered callback")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCancelledDownload(t *testing.T) {
|
2017-04-25 19:45:42 -04:00
|
|
|
layerStore := &mockLayerStore{make(map[layer.ChainID]*mockLayer)}
|
2021-03-19 10:34:08 -04:00
|
|
|
ldm := NewLayerDownloadManager(layerStore, maxDownloadConcurrency, func(m *LayerDownloadManager) { m.waitDuration = time.Millisecond })
|
2015-11-13 19:59:01 -05:00
|
|
|
progressChan := make(chan progress.Progress)
|
|
|
|
progressDone := make(chan struct{})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for range progressChan {
|
|
|
|
}
|
|
|
|
close(progressDone)
|
|
|
|
}()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-time.After(time.Millisecond)
|
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
|
|
|
|
descriptors := downloadDescriptors(nil)
|
2022-01-21 07:34:37 -05:00
|
|
|
_, _, err := ldm.Download(ctx, *image.NewRootFS(), descriptors, progress.ChanOutput(progressChan))
|
2015-11-13 19:59:01 -05:00
|
|
|
if err != context.Canceled {
|
2022-01-07 23:28:18 -05:00
|
|
|
close(progressChan)
|
2015-11-13 19:59:01 -05:00
|
|
|
t.Fatal("expected download to be cancelled")
|
|
|
|
}
|
|
|
|
|
|
|
|
close(progressChan)
|
|
|
|
<-progressDone
|
|
|
|
}
|
2019-06-25 09:26:36 -04:00
|
|
|
|
|
|
|
func TestMaxDownloadAttempts(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
simulateRetries int
|
|
|
|
maxDownloadAttempts int
|
|
|
|
expectedErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "max-attempts=5, succeed at 2nd attempt",
|
|
|
|
simulateRetries: 2,
|
|
|
|
maxDownloadAttempts: 5,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "max-attempts=5, succeed at 5th attempt",
|
|
|
|
simulateRetries: 5,
|
|
|
|
maxDownloadAttempts: 5,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "max-attempts=5, fail at 6th attempt",
|
|
|
|
simulateRetries: 6,
|
|
|
|
maxDownloadAttempts: 5,
|
|
|
|
expectedErr: "simulating download attempt 5/6",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "max-attempts=0, fail after 1 attempt",
|
|
|
|
simulateRetries: 1,
|
|
|
|
maxDownloadAttempts: 0,
|
|
|
|
expectedErr: "simulating download attempt 1/1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
layerStore := &mockLayerStore{make(map[layer.ChainID]*mockLayer)}
|
|
|
|
ldm := NewLayerDownloadManager(
|
2021-03-19 10:34:08 -04:00
|
|
|
layerStore,
|
2019-06-25 09:26:36 -04:00
|
|
|
maxDownloadConcurrency,
|
|
|
|
func(m *LayerDownloadManager) {
|
|
|
|
m.waitDuration = time.Millisecond
|
|
|
|
m.maxDownloadAttempts = tc.maxDownloadAttempts
|
|
|
|
})
|
|
|
|
|
|
|
|
progressChan := make(chan progress.Progress)
|
|
|
|
progressDone := make(chan struct{})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for range progressChan {
|
|
|
|
}
|
|
|
|
close(progressDone)
|
|
|
|
}()
|
|
|
|
|
|
|
|
var currentDownloads int32
|
|
|
|
descriptors := downloadDescriptors(¤tDownloads)
|
|
|
|
descriptors[4].(*mockDownloadDescriptor).simulateRetries = tc.simulateRetries
|
|
|
|
|
2022-01-21 07:34:37 -05:00
|
|
|
_, _, err := ldm.Download(context.Background(), *image.NewRootFS(), descriptors, progress.ChanOutput(progressChan))
|
2019-06-25 09:26:36 -04:00
|
|
|
if tc.expectedErr == "" {
|
|
|
|
assert.NilError(t, err)
|
|
|
|
} else {
|
|
|
|
assert.Error(t, err, tc.expectedErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|