1
1
package dib_test
2
2
3
3
import (
4
+ "fmt"
4
5
"os"
5
6
"os/exec"
6
7
"path"
@@ -12,13 +13,16 @@ import (
12
13
"github.com/stretchr/testify/require"
13
14
)
14
15
15
- const fixtureDir = "../../test/fixtures/docker"
16
+ const (
17
+ buildPath1 = "../../test/fixtures/docker"
18
+ buildPath2 = "../../test/fixtures/docker-duplicates"
19
+ registryPrefix = "eu.gcr.io/my-test-repository"
20
+ )
16
21
17
22
//nolint:paralleltest
18
23
func TestGenerateDAG (t * testing.T ) {
19
24
t .Run ("basic tests" , func (t * testing.T ) {
20
- graph , err := dib .GenerateDAG (fixtureDir ,
21
- "eu.gcr.io/my-test-repository" , "" , map [string ]string {})
25
+ graph , err := dib .GenerateDAG (buildPath1 , registryPrefix , "" , nil )
22
26
require .NoError (t , err )
23
27
24
28
nodes := flattenNodes (graph )
@@ -27,7 +31,7 @@ func TestGenerateDAG(t *testing.T) {
27
31
multistageNode := nodes ["multistage" ]
28
32
29
33
rootImage := rootNode .Image
30
- assert .Equal (t , "eu.gcr.io/my-test-repository/ bullseye" , rootImage .Name )
34
+ assert .Equal (t , path . Join ( registryPrefix , " bullseye") , rootImage .Name )
31
35
assert .Equal (t , "bullseye" , rootImage .ShortName )
32
36
assert .Empty (t , rootNode .Parents ())
33
37
assert .Len (t , rootNode .Children (), 3 )
@@ -37,10 +41,9 @@ func TestGenerateDAG(t *testing.T) {
37
41
})
38
42
39
43
t .Run ("modifying the root node should change all hashes" , func (t * testing.T ) {
40
- tmpDir := copyFixtures (t )
44
+ buildPath := copyFixtures (t , buildPath1 )
41
45
42
- graph0 , err := dib .GenerateDAG (tmpDir ,
43
- "eu.gcr.io/my-test-repository" , "" , map [string ]string {})
46
+ graph0 , err := dib .GenerateDAG (buildPath , registryPrefix , "" , nil )
44
47
require .NoError (t , err )
45
48
46
49
nodes0 := flattenNodes (graph0 )
@@ -50,13 +53,12 @@ func TestGenerateDAG(t *testing.T) {
50
53
51
54
// When I add a new file in bullseye/ (root node)
52
55
require .NoError (t , os .WriteFile (
53
- path .Join (tmpDir , "bullseye/newfile" ),
56
+ path .Join (buildPath , "bullseye/newfile" ),
54
57
[]byte ("any content" ),
55
58
os .ModePerm ))
56
59
57
60
// Then ONLY the hash of the child node bullseye/multistage should have changed
58
- graph1 , err := dib .GenerateDAG (tmpDir ,
59
- "eu.gcr.io/my-test-repository" , "" , map [string ]string {})
61
+ graph1 , err := dib .GenerateDAG (buildPath , registryPrefix , "" , nil )
60
62
require .NoError (t , err )
61
63
62
64
nodes1 := flattenNodes (graph1 )
@@ -70,10 +72,9 @@ func TestGenerateDAG(t *testing.T) {
70
72
})
71
73
72
74
t .Run ("modifying a child node should change only its hash" , func (t * testing.T ) {
73
- tmpDir := copyFixtures (t )
75
+ buildPath := copyFixtures (t , buildPath1 )
74
76
75
- graph0 , err := dib .GenerateDAG (tmpDir ,
76
- "eu.gcr.io/my-test-repository" , "" , map [string ]string {})
77
+ graph0 , err := dib .GenerateDAG (buildPath , registryPrefix , "" , nil )
77
78
require .NoError (t , err )
78
79
79
80
nodes0 := flattenNodes (graph0 )
@@ -83,13 +84,12 @@ func TestGenerateDAG(t *testing.T) {
83
84
84
85
// When I add a new file in bullseye/multistage/ (child node)
85
86
require .NoError (t , os .WriteFile (
86
- path .Join (tmpDir , "bullseye/multistage/newfile" ),
87
+ path .Join (buildPath , "bullseye/multistage/newfile" ),
87
88
[]byte ("file contents" ),
88
89
os .ModePerm ))
89
90
90
91
// Then ONLY the hash of the child node bullseye/multistage should have changed
91
- graph1 , err := dib .GenerateDAG (tmpDir ,
92
- "eu.gcr.io/my-test-repository" , "" , map [string ]string {})
92
+ graph1 , err := dib .GenerateDAG (buildPath , registryPrefix , "" , nil )
93
93
require .NoError (t , err )
94
94
95
95
nodes1 := flattenNodes (graph1 )
@@ -103,14 +103,11 @@ func TestGenerateDAG(t *testing.T) {
103
103
})
104
104
105
105
t .Run ("using custom hash list should change only hashes of nodes with custom label" , func (t * testing.T ) {
106
- graph0 , err := dib .GenerateDAG (fixtureDir ,
107
- "eu.gcr.io/my-test-repository" , "" , map [string ]string {})
106
+ graph0 , err := dib .GenerateDAG (buildPath1 , registryPrefix , "" , nil )
108
107
require .NoError (t , err )
109
108
110
- graph1 , err := dib .GenerateDAG (fixtureDir ,
111
- "eu.gcr.io/my-test-repository" ,
112
- "../../test/fixtures/dib/valid_wordlist.txt" ,
113
- map [string ]string {})
109
+ graph1 , err := dib .GenerateDAG (buildPath1 , registryPrefix ,
110
+ "../../test/fixtures/dib/valid_wordlist.txt" , nil )
114
111
require .NoError (t , err )
115
112
116
113
nodes0 := flattenNodes (graph0 )
@@ -126,13 +123,10 @@ func TestGenerateDAG(t *testing.T) {
126
123
})
127
124
128
125
t .Run ("using arg used in root node should change all hashes" , func (t * testing.T ) {
129
- graph0 , err := dib .GenerateDAG (fixtureDir ,
130
- "eu.gcr.io/my-test-repository" , "" ,
131
- map [string ]string {})
126
+ graph0 , err := dib .GenerateDAG (buildPath1 , registryPrefix , "" , nil )
132
127
require .NoError (t , err )
133
128
134
- graph1 , err := dib .GenerateDAG (fixtureDir ,
135
- "eu.gcr.io/my-test-repository" , "" ,
129
+ graph1 , err := dib .GenerateDAG (buildPath1 , registryPrefix , "" ,
136
130
map [string ]string {
137
131
"HELLO" : "world" ,
138
132
})
@@ -145,14 +139,24 @@ func TestGenerateDAG(t *testing.T) {
145
139
146
140
assert .NotEqual (t , rootNode1 .Image .Hash , rootNode0 .Image .Hash )
147
141
})
142
+
143
+ t .Run ("duplicates" , func (t * testing.T ) {
144
+ graph , err := dib .GenerateDAG (buildPath2 , registryPrefix , "" , nil )
145
+ require .Error (t , err )
146
+ require .Nil (t , graph )
147
+ require .EqualError (t , err ,
148
+ fmt .Sprintf (
149
+ "duplicate image name \" %s/duplicate\" found while reading file \" %s/bullseye/duplicate2/Dockerfile\" : previous file was \" %s/bullseye/duplicate1/Dockerfile\" " , //nolint:lll
150
+ registryPrefix , buildPath2 , buildPath2 ))
151
+ })
148
152
}
149
153
150
- // copyFixtures copies the directory fixtureDir into a temporary one to be free to edit files.
151
- func copyFixtures (t * testing.T ) string {
154
+ // copyFixtures copies the buildPath directory into a temporary one to be free to edit files.
155
+ func copyFixtures (t * testing.T , buildPath string ) string {
152
156
t .Helper ()
153
157
cwd , err := os .Getwd ()
154
158
require .NoError (t , err )
155
- src := path .Join (cwd , fixtureDir )
159
+ src := path .Join (cwd , buildPath )
156
160
dest := t .TempDir ()
157
161
cmd := exec .Command ("cp" , "-r" , src , dest )
158
162
require .NoError (t , cmd .Run ())
0 commit comments