77 "io"
88 "os"
99 "path/filepath"
10+ "runtime"
1011 "strings"
1112 "sync"
1213 "testing"
@@ -69,6 +70,15 @@ func mustSaveStateForRepro(t *testing.T, metadata ContainerMetadata) {
6970 }
7071}
7172
73+ func repoFilePath (t * testing.T , name string ) string {
74+ t .Helper ()
75+ _ , thisFile , _ , ok := runtime .Caller (0 )
76+ if ! ok {
77+ t .Fatalf ("failed to resolve current test file path" )
78+ }
79+ return filepath .Join (filepath .Dir (thisFile ), name )
80+ }
81+
7282func TestBugExercise1_ReproduceShortContainerIDPanic (t * testing.T ) {
7383 withTempRuntimeDirs (t , func () {
7484 containerID := "abc"
@@ -90,7 +100,7 @@ func TestBugExercise1_ReproduceShortContainerIDPanic(t *testing.T) {
90100}
91101
92102func TestBugExercise2_ReproduceNilDeferClosePanic (t * testing.T ) {
93- src , err := os .ReadFile ("/home/runner/work/basic-docker-engine/basic-docker-engine/ main.go" )
103+ src , err := os .ReadFile (repoFilePath ( t , " main.go") )
94104 if err != nil {
95105 t .Fatalf ("failed to read main.go: %v" , err )
96106 }
@@ -111,8 +121,8 @@ func TestBugExercise2_ReproduceNilDeferClosePanic(t *testing.T) {
111121 if deferIdx == - 1 || errCheckIdx == - 1 {
112122 t .Fatalf ("expected defer and error-check patterns in saveLayerMetadata" )
113123 }
114- if deferIdx > errCheckIdx {
115- t .Fatalf ("expected reproduction: defer file.Close appears before os.Create error check" )
124+ if deferIdx >= errCheckIdx {
125+ t .Fatalf ("expected reproduction: defer file.Close must appear before os.Create error check" )
116126 }
117127}
118128
@@ -188,6 +198,10 @@ type reproRegistry struct {
188198 maxOpen int
189199}
190200
201+ type reproManifestLayer struct {
202+ Digest string `json:"digest"`
203+ }
204+
191205func (r * reproRegistry ) FetchManifest (repo , tag string ) (* Manifest , error ) {
192206 return & r .manifest , nil
193207}
@@ -237,17 +251,16 @@ func makeTarLayerForRepro(t *testing.T, name, content string) []byte {
237251
238252func TestBugExercise5_ReproduceDeferredCloseInLoop (t * testing.T ) {
239253 uniqueImage := fmt .Sprintf ("bug5-image-%d" , time .Now ().UnixNano ())
240- _ = os .RemoveAll (filepath .Join ("/tmp/basic-docker/images" , uniqueImage ))
241- defer os .RemoveAll (filepath .Join ("/tmp/basic-docker/images" , uniqueImage ))
254+ buggyHardcodedImagesDir := filepath .Join (os .TempDir (), "basic-docker" , "images" )
255+ _ = os .RemoveAll (filepath .Join (buggyHardcodedImagesDir , uniqueImage ))
256+ defer os .RemoveAll (filepath .Join (buggyHardcodedImagesDir , uniqueImage ))
242257
243258 r := & reproRegistry {
244259 layers : map [string ][]byte {},
245260 }
246261 digests := []string {"sha256:l1" , "sha256:l2" , "sha256:l3" }
247262 for i , d := range digests {
248- r .manifest .Layers = append (r .manifest .Layers , struct {
249- Digest string "json:\" digest\" "
250- }{Digest : d })
263+ r .manifest .Layers = append (r .manifest .Layers , reproManifestLayer {Digest : d })
251264 r .layers [d ] = makeTarLayerForRepro (t , fmt .Sprintf ("f-%d.txt" , i ), "x" )
252265 }
253266
@@ -284,14 +297,16 @@ func TestBugExercise6_ReproduceHardcodedImageDirectoryMismatch(t *testing.T) {
284297}
285298
286299func TestBugExercise7_ReproduceUnsafeExtractionLackOfValidation (t * testing.T ) {
287- src , err := os .ReadFile ("/home/runner/work/basic-docker-engine/basic-docker-engine/ image.go" )
300+ src , err := os .ReadFile (repoFilePath ( t , " image.go") )
288301 if err != nil {
289302 t .Fatalf ("failed to read image.go: %v" , err )
290303 }
291304 content := string (src )
292305
293- if ! strings .Contains (content , `exec.Command("tar", "-x", "-C", rootfs)` ) {
294- t .Fatalf ("expected tar extraction command pattern in extractLayer" )
306+ if ! strings .Contains (content , `exec.Command("tar"` ) ||
307+ ! strings .Contains (content , `"-x"` ) ||
308+ ! strings .Contains (content , `"-C", rootfs` ) {
309+ t .Fatalf ("expected tar-based extraction command pattern in extractLayer" )
295310 }
296311 if strings .Contains (content , "filepath.Clean" ) || strings .Contains (content , "Rel(" ) {
297312 t .Fatalf ("expected no explicit path-boundary validation in extractLayer/LoadImageFromTar" )
@@ -338,13 +353,13 @@ func TestBugExercise8_ReproduceIgnoredStateUpdateErrors(t *testing.T) {
338353}
339354
340355func TestBugExercise9_ReproduceGlobalMutableNetworkStateDesign (t * testing.T ) {
341- src , err := os .ReadFile ("/home/runner/work/basic-docker-engine/basic-docker-engine/ network.go" )
356+ src , err := os .ReadFile (repoFilePath ( t , " network.go") )
342357 if err != nil {
343358 t .Fatalf ("failed to read network.go: %v" , err )
344359 }
345360 content := string (src )
346361
347- if ! strings .Contains (content , "var networks = []Network{} " ) {
362+ if ! strings .Contains (content , "var networks = []Network" ) {
348363 t .Fatalf ("expected global mutable networks state declaration" )
349364 }
350365 if strings .Contains (content , "sync.Mutex" ) || strings .Contains (content , "sync.RWMutex" ) {
0 commit comments