forked from goodwithtech/deckoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_test.go
More file actions
200 lines (192 loc) · 4.59 KB
/
image_test.go
File metadata and controls
200 lines (192 loc) · 4.59 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package image
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"github.com/SpazioDati/deckoder/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewImage(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case strings.Contains(r.URL.String(), "unknown"):
w.WriteHeader(404)
return
case strings.Contains(r.URL.String(), "invalid_json"):
w.Header().Set("Content-Type", "application/vnd.docker.distribution.manifest.v2+json")
w.Write([]byte(`{invalid}`))
default:
b, _ := ioutil.ReadFile("testdata/manifest.json")
w.Header().Set("Content-Type", "application/vnd.docker.distribution.manifest.v2+json")
w.Write(b)
}
}))
defer ts.Close()
tsurl := strings.TrimPrefix(ts.URL, "http://")
type args struct {
image Reference
transports []string
option types.DockerOption
}
type image struct {
name string
isFile bool
transports []string
}
tests := []struct {
name string
args args
wantImage image
wantErr string
}{
{
name: "happy path",
args: args{
image: Reference{
Name: fmt.Sprintf("%s/foobar", tsurl),
IsFile: false,
},
transports: []string{"docker://"},
option: types.DockerOption{
SkipPing: true,
InsecureSkipTLSVerify: true,
},
},
wantImage: image{
name: fmt.Sprintf("%s/foobar", tsurl),
isFile: false,
transports: []string{"docker://"},
},
},
{
name: "happy path without latest tag",
args: args{
image: Reference{
Name: fmt.Sprintf("%s/foobar", tsurl),
IsFile: false,
},
transports: []string{"docker://"},
option: types.DockerOption{
InsecureSkipTLSVerify: true,
},
},
wantImage: image{
name: fmt.Sprintf("%s/foobar", tsurl),
isFile: false,
transports: []string{"docker://"},
},
},
{
name: "happy path with a tar file",
args: args{
image: Reference{
Name: "testdata/alpine-310.tar.gz",
IsFile: true,
},
transports: []string{"docker-archive:"},
},
wantImage: image{
name: "testdata/alpine-310.tar.gz",
isFile: true,
transports: []string{"docker-archive:"},
},
},
{
name: "sad path: invalid image name",
args: args{
image: Reference{
Name: "ALPINE",
IsFile: false,
},
transports: []string{"docker-archive:"},
},
wantErr: "invalid image name",
},
{
name: "sad path: invalid image name",
args: args{
image: Reference{
Name: "alpine:3.10",
IsFile: false,
},
transports: []string{"invalid:"},
},
wantErr: `unknown transport "invalid"`,
},
{
name: "sad path: non-existed image name",
args: args{
image: Reference{
Name: fmt.Sprintf("%s/unknown:3.10", tsurl),
IsFile: false,
},
transports: []string{"docker://"},
option: types.DockerOption{
SkipPing: true,
InsecureSkipTLSVerify: true,
},
},
wantErr: `failed to initialize source`,
},
{
name: "sad path: invalid manifest JSON",
args: args{
image: Reference{
Name: fmt.Sprintf("%s/invalid_json:3.10", tsurl),
IsFile: false,
},
transports: []string{"docker://"},
option: types.DockerOption{
SkipPing: true,
InsecureSkipTLSVerify: true,
},
},
wantErr: `failed to initialize: invalid character`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
got, err := NewImage(ctx, tt.args.image, tt.args.transports, tt.args.option)
if tt.wantErr != "" {
require.NotNil(t, err, tt.name)
require.Contains(t, err.Error(), tt.wantErr, tt.name)
return
} else {
require.NoError(t, err, tt.name)
}
assert.Equal(t, tt.wantImage.name, got.name, tt.name)
})
}
}
func TestRealImage_LayerIDs(t *testing.T) {
tests := []struct {
name string
imageFile string
want []string
}{
{
name: "happy path",
imageFile: "testdata/alpine-310.tar.gz",
want: []string{"sha256:531743b7098cb2aaf615641007a129173f63ed86ca32fe7b5a246a1c47286028"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
img, err := NewImage(context.Background(), Reference{
Name: tt.imageFile,
IsFile: true,
}, []string{"docker-archive:"}, types.DockerOption{})
require.NoError(t, err, tt.name)
if got := img.LayerIDs(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("LayerIDs() = %v, want %v", got, tt.want)
}
})
}
}