@@ -11,17 +11,96 @@ import (
11
11
12
12
"github.com/google/go-containerregistry/pkg/name"
13
13
"github.com/google/go-containerregistry/pkg/registry"
14
+ cranev1 "github.com/google/go-containerregistry/pkg/v1"
14
15
"github.com/google/go-containerregistry/pkg/v1/daemon"
15
16
"github.com/google/go-containerregistry/pkg/v1/remote"
16
17
)
17
18
19
+ func filesInLayer (layer cranev1.Layer ) (map [string ]string , error ) {
20
+ rc , err := layer .Uncompressed ()
21
+ if err != nil {
22
+ return nil , fmt .Errorf ("uncompress layer: %w" , err )
23
+ }
24
+ defer rc .Close ()
25
+
26
+ tr := tar .NewReader (rc )
27
+
28
+ files := make (map [string ]string )
29
+
30
+ for {
31
+ hdr , err := tr .Next ()
32
+ if err == io .EOF {
33
+ break
34
+ }
35
+ if err != nil {
36
+ return nil , fmt .Errorf ("read tar header: %w" , err )
37
+ }
38
+
39
+ if hdr .FileInfo ().IsDir () {
40
+ continue
41
+ }
42
+
43
+ content , err := io .ReadAll (tr )
44
+ if err != nil {
45
+ return nil , fmt .Errorf ("read tar content: %w" , err )
46
+ }
47
+
48
+ files [hdr .Name ] = string (content )
49
+ }
50
+
51
+ return files , nil
52
+ }
53
+
54
+ const worldDotTxt = "This is my world!"
55
+
56
+ func TestForgeLocal (t * testing.T ) {
57
+ config := & ForgeConfig {WorkDir : "testdata" }
58
+
59
+ if err := Build ("testdata/localbase.wanda.yaml" , config ); err != nil {
60
+ t .Fatalf ("build base: %v" , err )
61
+ }
62
+
63
+ if err := Build ("testdata/local.wanda.yaml" , config ); err != nil {
64
+ t .Fatalf ("build: %v" , err )
65
+ }
66
+
67
+ const resultRef = "cr.ray.io/rayproject/local"
68
+ ref , err := name .ParseReference (resultRef )
69
+ if err != nil {
70
+ t .Fatalf ("parse reference: %v" , err )
71
+ }
72
+
73
+ img , err := daemon .Image (ref )
74
+ if err != nil {
75
+ t .Fatalf ("read image: %v" , err )
76
+ }
77
+
78
+ layers , err := img .Layers ()
79
+ if err != nil {
80
+ t .Fatalf ("read layers: %v" , err )
81
+ }
82
+
83
+ if len (layers ) != 1 {
84
+ t .Fatalf ("got %d layers, want 2" , len (layers ))
85
+ }
86
+
87
+ files , err := filesInLayer (layers [0 ])
88
+ if err != nil {
89
+ t .Fatalf ("read layer: %v" , err )
90
+ }
91
+
92
+ if _ , ok := files ["opt/app/Dockerfile" ]; ! ok {
93
+ t .Errorf ("Dockerfile not in image" )
94
+ }
95
+ }
96
+
18
97
func TestForge (t * testing.T ) {
19
98
config := & ForgeConfig {
20
99
WorkDir : "testdata" ,
21
100
NamePrefix : "cr.ray.io/rayproject/" ,
22
101
}
23
102
24
- if err := Build ("testdata/hello.spec .yaml" , config ); err != nil {
103
+ if err := Build ("testdata/hello.wanda .yaml" , config ); err != nil {
25
104
t .Fatalf ("build: %v" , err )
26
105
}
27
106
@@ -45,6 +124,39 @@ func TestForge(t *testing.T) {
45
124
if len (layers ) != 1 {
46
125
t .Fatalf ("got %d layers, want 1" , len (layers ))
47
126
}
127
+
128
+ if err := Build ("testdata/world.wanda.yaml" , config ); err != nil {
129
+ t .Fatalf ("build world: %v" , err )
130
+ }
131
+
132
+ world := "cr.ray.io/rayproject/world"
133
+ ref2 , err := name .ParseReference (world )
134
+ if err != nil {
135
+ t .Fatalf ("parse world reference: %v" , err )
136
+ }
137
+
138
+ img2 , err := daemon .Image (ref2 )
139
+ if err != nil {
140
+ t .Fatalf ("read world image: %v" , err )
141
+ }
142
+
143
+ layers2 , err := img2 .Layers ()
144
+ if err != nil {
145
+ t .Fatalf ("read world layers: %v" , err )
146
+ }
147
+
148
+ if len (layers2 ) != 2 {
149
+ t .Fatalf ("got %d world layers, want 1" , len (layers2 ))
150
+ }
151
+
152
+ files , err := filesInLayer (layers2 [1 ])
153
+ if err != nil {
154
+ t .Fatalf ("read world layer files: %v" , err )
155
+ }
156
+
157
+ if got := files ["opt/app/world.txt" ]; got != worldDotTxt {
158
+ t .Errorf ("world.txt in image, got %q, want %q" , got , worldDotTxt )
159
+ }
48
160
}
49
161
50
162
func TestForgeWithWorkRepo (t * testing.T ) {
@@ -67,11 +179,11 @@ func TestForgeWithWorkRepo(t *testing.T) {
67
179
BuildID : "abc123" ,
68
180
}
69
181
70
- if err := Build ("testdata/hello.spec .yaml" , config ); err != nil {
182
+ if err := Build ("testdata/hello.wanda .yaml" , config ); err != nil {
71
183
t .Fatalf ("build hello: %v" , err )
72
184
}
73
185
74
- if err := Build ("testdata/world.spec .yaml" , config ); err != nil {
186
+ if err := Build ("testdata/world.wanda .yaml" , config ); err != nil {
75
187
t .Fatalf ("build world: %v" , err )
76
188
}
77
189
@@ -94,39 +206,11 @@ func TestForgeWithWorkRepo(t *testing.T) {
94
206
if len (layers ) != 2 {
95
207
t .Fatalf ("got %d layers, want 2" , len (layers ))
96
208
}
97
-
98
- layer , err := layers [1 ].Uncompressed ()
209
+ files , err := filesInLayer (layers [1 ])
99
210
if err != nil {
100
- t .Fatalf ("uncompress layer: %v" , err )
211
+ t .Fatalf ("read layer: %v" , err )
101
212
}
102
-
103
- tr := tar .NewReader (layer )
104
-
105
- files := make (map [string ]string )
106
-
107
- for {
108
- hdr , err := tr .Next ()
109
- if err == io .EOF {
110
- break
111
- }
112
- if err != nil {
113
- t .Fatalf ("read tar header: %v" , err )
114
- }
115
-
116
- if hdr .FileInfo ().IsDir () {
117
- continue
118
- }
119
-
120
- content , err := io .ReadAll (tr )
121
- if err != nil {
122
- t .Fatalf ("read tar content: %v" , err )
123
- }
124
-
125
- t .Log (hdr .Name )
126
- files [hdr .Name ] = string (content )
127
- }
128
-
129
- if got , want := files ["opt/app/world.txt" ], "This is my world!" ; got != want {
130
- t .Errorf ("world.txt in image, got %q, want %q" , got , want )
213
+ if got := files ["opt/app/world.txt" ]; got != worldDotTxt {
214
+ t .Errorf ("world.txt in image, got %q, want %q" , got , worldDotTxt )
131
215
}
132
216
}
0 commit comments