summaryrefslogtreecommitdiffstats
path: root/pkg/v1/daemon/write_test.go
blob: 0e5495cdbf4df83c1dc8b4455173041f37812e2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2018 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package daemon

import (
	"context"
	"errors"
	"fmt"
	"io"
	"strings"
	"testing"

	"github.com/docker/docker/api/types"

	"github.com/google/go-containerregistry/pkg/name"
	"github.com/google/go-containerregistry/pkg/v1/empty"
	"github.com/google/go-containerregistry/pkg/v1/tarball"
)

type errReader struct {
	err error
}

func (r *errReader) Read(p []byte) (int, error) {
	return 0, r.err
}

func (m *MockClient) ImageLoad(ctx context.Context, r io.Reader, _ bool) (types.ImageLoadResponse, error) {
	if !m.negotiated {
		return types.ImageLoadResponse{}, errors.New("you forgot to call NegotiateAPIVersion before calling ImageLoad")
	}
	if m.wantCtx != nil && m.wantCtx != ctx {
		return types.ImageLoadResponse{}, fmt.Errorf("ImageLoad: wrong context")
	}

	_, _ = io.Copy(io.Discard, r)
	return types.ImageLoadResponse{
		Body: m.loadBody,
	}, m.loadErr
}

func (m *MockClient) ImageTag(ctx context.Context, source, target string) error {
	if !m.negotiated {
		return errors.New("you forgot to call NegotiateAPIVersion before calling ImageTag")
	}
	if m.wantCtx != nil && m.wantCtx != ctx {
		return fmt.Errorf("ImageTag: wrong context")
	}
	return nil
}

func TestWriteImage(t *testing.T) {
	for _, tc := range []struct {
		name         string
		client       *MockClient
		wantResponse string
		wantErr      string
	}{{
		name: "success",
		client: &MockClient{
			loadBody: io.NopCloser(strings.NewReader("Loaded")),
		},
		wantResponse: "Loaded",
	}, {
		name: "load err",
		client: &MockClient{
			loadBody: io.NopCloser(strings.NewReader("Loaded")),
			loadErr:  fmt.Errorf("locked and loaded"),
		},
		wantErr: "locked and loaded",
	}, {
		name: "read err",
		client: &MockClient{
			loadBody: io.NopCloser(&errReader{fmt.Errorf("goodbye, world")}),
		},
		wantErr: "goodbye, world",
	}} {
		t.Run(tc.name, func(t *testing.T) {
			image, err := tarball.ImageFromPath("../tarball/testdata/test_image_1.tar", nil)
			if err != nil {
				t.Errorf("Error loading image: %v", err.Error())
			}
			tag, err := name.NewTag("test_image_2:latest")
			if err != nil {
				t.Fatal(err)
			}
			response, err := Write(tag, image, WithClient(tc.client))
			if tc.wantErr == "" {
				if err != nil {
					t.Errorf("Error writing image tar: %s", err.Error())
				}
			} else {
				if err == nil {
					t.Errorf("expected err")
				} else if !strings.Contains(err.Error(), tc.wantErr) {
					t.Errorf("Error writing image tar: wanted %s to contain %s", err.Error(), tc.wantErr)
				}
			}
			if !strings.Contains(response, tc.wantResponse) {
				t.Errorf("Error loading image. Response: %s", response)
			}

			dst, err := name.NewTag("hello:world")
			if err != nil {
				t.Fatal(err)
			}
			if err := Tag(tag, dst, WithClient(tc.client)); err != nil {
				t.Errorf("Error tagging image: %v", err)
			}
		})
	}
}

func TestWriteDefaultClient(t *testing.T) {
	wantErr := fmt.Errorf("bad client")
	defaultClient = func() (Client, error) {
		return nil, wantErr
	}

	tag, err := name.NewTag("test_image_2:latest")
	if err != nil {
		t.Fatal(err)
	}

	if _, err := Write(tag, empty.Image); !errors.Is(err, wantErr) {
		t.Errorf("Write(): want %v; got %v", wantErr, err)
	}

	if err := Tag(tag, tag); !errors.Is(err, wantErr) {
		t.Errorf("Tag(): want %v; got %v", wantErr, err)
	}

	// Cover default client init and ctx use as well.
	ctx := context.TODO()
	defaultClient = func() (Client, error) {
		return &MockClient{
			loadBody: io.NopCloser(strings.NewReader("Loaded")),
			wantCtx:  ctx,
		}, nil
	}
	if err := Tag(tag, tag, WithContext(ctx)); err != nil {
		t.Fatal(err)
	}
	if _, err := Write(tag, empty.Image, WithContext(ctx)); err != nil {
		t.Fatal(err)
	}
}