-
Notifications
You must be signed in to change notification settings - Fork 2
/
from_test.go
193 lines (154 loc) · 3.43 KB
/
from_test.go
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
package main
import (
"github.com/binxio/fromage/tag"
"github.com/google/go-containerregistry/pkg/name"
"testing"
)
func TestExtractFromStatements(t *testing.T) {
dockerfile := []byte(`
FROM golang:1.12 as builder #comment
FROM builder als runtime
`)
result := ExtractFromStatements(dockerfile)
if len(result) != 1 {
t.Fatalf("expected 1 hit, got %d", len(result))
}
if result[0] != "golang:1.12" {
t.Fatalf("expected golang:1.12, got %s", result[0])
}
}
type updateTest struct {
dockerfile []byte
reference string
updated bool
newDockerfile []byte
}
func TestUpdateFromStatements(t *testing.T) {
var tests = []updateTest{
{
[]byte(`
FROM golang@sha256:d0e79a9c39cdb3d71cc45fec929d1308d50420b79201467ec602b1b80cc314a8 as builder #comment
FROM builder als runtime
`),
"golang@sha256:d0e79a9c39cdb3d71cc45fec929d1308d50420b79201467ec602b1b80cc314a8",
false,
[]byte(`
FROM golang@sha256:d0e79a9c39cdb3d71cc45fec929d1308d50420b79201467ec602b1b80cc314a8 as builder #comment
FROM builder als runtime
`),
},
{
[]byte(`
FROM golang:1.12 as builder #comment
FROM builder as runtime
`),
"golang:1.12",
true,
[]byte(`
FROM golang:1.13 as builder #comment
FROM builder as runtime
`),
},
}
for _, test := range tests {
r, err := name.ParseReference(test.reference)
if err != nil {
t.Fatal(err)
}
reference, _ := r.(name.Tag)
nextRef, _ := tag.GetNextVersion(reference, nil, false)
result, updated := UpdateFromStatements(test.dockerfile, reference, nextRef, "./Dockerfile", true)
if updated != test.updated {
t.Fatalf("expected updated to be %v, in %s", test.updated, string(test.dockerfile))
}
if string(result) != string(test.newDockerfile) {
t.Fatalf("update did not match expected result")
}
}
}
type updateAlltest struct {
dockerfile []byte
updated bool
newDockerfile []byte
}
func TestUpdateAllFromStatements(t *testing.T) {
var tests = []updateAlltest{
{
[]byte(`
FROM golang:1.12.17 as builder #comment
FROM builder as runtime
FROM php:7.2-fpm
`),
true,
[]byte(`
FROM golang:1.13.0 as builder #comment
FROM builder as runtime
FROM php:7.3-fpm
`),
},
{
[]byte(`
FROM golang:1.12 as builder #comment
FROM builder as runtime
FROM php:7.2-fpm
`),
true,
[]byte(`
FROM golang:1.13 as builder #comment
FROM builder as runtime
FROM php:7.3-fpm
`),
},
{
[]byte(`
FROM golang:1.12.0 as builder #comment
FROM builder as runtime
FROM php:7.2-fpm
`),
true,
[]byte(`
FROM golang:1.12.1 as builder #comment
FROM builder as runtime
FROM php:7.3-fpm
`),
},
{
[]byte(`
FROM golang:latest as builder #comment
FROM builder as runtime
FROM php:latest
`),
false,
[]byte(`
FROM golang:latest as builder #comment
FROM builder as runtime
FROM php:latest
`),
},
{
[]byte(`
# update ordered to start with highest version
FROM golang:1.12
FROM golang:1.13
FROM golang:1.14
`),
true,
[]byte(`
# update ordered to start with highest version
FROM golang:1.13
FROM golang:1.14
FROM golang:1.15
`),
},
}
for _, test := range tests {
newDockerfile, updated := UpdateAllFromStatements(test.dockerfile, "./Dockerfile", nil, false, true)
if test.updated != updated {
t.Fatalf("expected updated to be %v in %s", test.updated, string(test.dockerfile))
}
if string(test.newDockerfile) != string(newDockerfile) {
t.Fatalf("expected new Dockerfile to be\n%s\ngot:\n%s\n",
string(test.newDockerfile), string(newDockerfile))
}
}
}