@@ -1175,6 +1175,138 @@ func TestInitializeChainLayers(t *testing.T) {
1175
1175
}
1176
1176
}
1177
1177
1178
+ func TestTopFS (t * testing.T ) {
1179
+ tests := []struct {
1180
+ name string
1181
+ image * Image
1182
+ wantFilesFromFS []string
1183
+ wantErr bool
1184
+ }{
1185
+ {
1186
+ name : "no chain layers" ,
1187
+ image : & Image {
1188
+ chainLayers : []* chainLayer {},
1189
+ },
1190
+ wantErr : true ,
1191
+ },
1192
+ {
1193
+ name : "single chain layer" ,
1194
+ image : & Image {
1195
+ chainLayers : []* chainLayer {
1196
+ {
1197
+ fileNodeTree : func () * pathtree.Node [fileNode ] {
1198
+ root := pathtree .NewNode [fileNode ]()
1199
+ _ = root .Insert ("/" , & fileNode {
1200
+ virtualPath : "/" ,
1201
+ isWhiteout : false ,
1202
+ mode : fs .ModeDir | dirPermission ,
1203
+ })
1204
+ _ = root .Insert ("/foo.txt" , & fileNode {
1205
+ virtualPath : "/foo.txt" ,
1206
+ mode : filePermission ,
1207
+ })
1208
+ return root
1209
+ }(),
1210
+ index : 0 ,
1211
+ latestLayer : & Layer {
1212
+ buildCommand : "" ,
1213
+ diffID : "sha256:123" ,
1214
+ isEmpty : false ,
1215
+ },
1216
+ },
1217
+ },
1218
+ },
1219
+ wantFilesFromFS : []string {"/foo.txt" },
1220
+ },
1221
+ {
1222
+ name : "multiple chain layers" ,
1223
+ image : & Image {
1224
+ chainLayers : []* chainLayer {
1225
+ {
1226
+ fileNodeTree : func () * pathtree.Node [fileNode ] {
1227
+ root := pathtree .NewNode [fileNode ]()
1228
+ _ = root .Insert ("/" , & fileNode {
1229
+ virtualPath : "/" ,
1230
+ isWhiteout : false ,
1231
+ mode : fs .ModeDir | dirPermission ,
1232
+ })
1233
+ _ = root .Insert ("/foo.txt" , & fileNode {
1234
+ virtualPath : "/foo.txt" ,
1235
+ mode : filePermission ,
1236
+ })
1237
+ return root
1238
+ }(),
1239
+ index : 0 ,
1240
+ latestLayer : & Layer {
1241
+ buildCommand : "" ,
1242
+ diffID : "sha256:123" ,
1243
+ isEmpty : false ,
1244
+ },
1245
+ },
1246
+ {
1247
+ fileNodeTree : func () * pathtree.Node [fileNode ] {
1248
+ root := pathtree .NewNode [fileNode ]()
1249
+ _ = root .Insert ("/" , & fileNode {
1250
+ extractDir : "" ,
1251
+ layerDir : "" ,
1252
+ virtualPath : "/" ,
1253
+ isWhiteout : false ,
1254
+ mode : fs .ModeDir | dirPermission ,
1255
+ })
1256
+ _ = root .Insert ("/foo.txt" , & fileNode {
1257
+ virtualPath : "/foo.txt" ,
1258
+ mode : filePermission ,
1259
+ })
1260
+ _ = root .Insert ("/bar.txt" , & fileNode {
1261
+ virtualPath : "/bar.txt" ,
1262
+ mode : filePermission ,
1263
+ })
1264
+ return root
1265
+ }(),
1266
+ index : 0 ,
1267
+ latestLayer : & Layer {
1268
+ buildCommand : "" ,
1269
+ diffID : "sha256:123" ,
1270
+ isEmpty : false ,
1271
+ },
1272
+ },
1273
+ },
1274
+ },
1275
+ wantFilesFromFS : []string {"/foo.txt" , "/bar.txt" },
1276
+ },
1277
+ }
1278
+
1279
+ for _ , tc := range tests {
1280
+ t .Run (tc .name , func (t * testing.T ) {
1281
+ gotFS , err := tc .image .TopFS ()
1282
+ if tc .wantErr {
1283
+ if err == nil {
1284
+ t .Fatalf ("TopFS() returned nil error, but want non-nil error" )
1285
+ }
1286
+ return
1287
+ }
1288
+
1289
+ var gotPaths []string
1290
+ err = fs .WalkDir (gotFS , "/" , func (path string , d fs.DirEntry , err error ) error {
1291
+ if err != nil || d .IsDir () {
1292
+ return err
1293
+ }
1294
+
1295
+ gotPaths = append (gotPaths , path )
1296
+ return nil
1297
+ })
1298
+
1299
+ if err != nil {
1300
+ t .Fatalf ("WalkDir() returned error: %v" , err )
1301
+ }
1302
+
1303
+ if diff := cmp .Diff (tc .wantFilesFromFS , gotPaths , cmpopts .SortSlices (func (a , b string ) bool { return a < b })); diff != "" {
1304
+ t .Errorf ("TopFS() returned incorrect files: got %v, want %v" , gotPaths , tc .wantFilesFromFS )
1305
+ }
1306
+ })
1307
+ }
1308
+ }
1309
+
1178
1310
// tarEntry represents a single entry in a tarball. It contains the header and data for the entry.
1179
1311
// If the data is nil, the entry will be written without any content.
1180
1312
type tarEntry struct {
0 commit comments