@@ -2,8 +2,12 @@ package emulator_test
22
33import (
44 "context"
5+ "crypto/sha256"
6+ "encoding/hex"
57 "errors"
68 "net/http"
9+ "os"
10+ "path/filepath"
711 "testing"
812
913 "github.com/stretchr/testify/assert"
@@ -26,13 +30,64 @@ func TestTheEmulatorOfAnArm64ImageIsTakenFromThePinnedPackage(t *testing.T) {
2630 store := download .Open (dir , http .DefaultClient )
2731
2832 // act
29- found , err := emulator .ReadyFrom (context .Background (), store , download.Pin {URL : "https://snapshot.invalid/qemu-user.deb" , Digest : digest }, "arm64" )
33+ found , err := emulator .ReadyFrom (context .Background (), store , download.Pin {URL : "https://snapshot.invalid/qemu-user.deb" , Digest : digest }, "arm64" , digestOf ( "the emulator" ) )
3034
3135 // assert
3236 require .NoError (t , err )
3337 assert .Equal (t , []byte ("the emulator" ), found )
3438}
3539
40+ func TestAnEmulatorTheStoreKeepsIsReadWithoutThePackage (t * testing.T ) {
41+ // arrange
42+ dir := t .TempDir ()
43+ digest := downloadtest .Kept (t , dir , []byte ("the emulator" ))
44+ refuse := roundTrip (func (* http.Request ) (* http.Response , error ) {
45+ return nil , errors .New ("offline" )
46+ })
47+ store := download .Open (dir , & http.Client {Transport : refuse })
48+
49+ // act
50+ found , err := emulator .ReadyFrom (context .Background (), store , download.Pin {URL : "https://snapshot.invalid/qemu-user.deb" , Digest : "sha256:notkept" }, "arm64" , digest )
51+
52+ // assert
53+ require .NoError (t , err )
54+ assert .Equal (t , []byte ("the emulator" ), found )
55+ }
56+
57+ func TestAnEmulatorTakenFromThePackageIsKeptUnderItsDigest (t * testing.T ) {
58+ // arrange
59+ pkg := debtest .Package (t , map [string ]string {"./usr/bin/qemu-aarch64" : "the emulator" })
60+ dir := t .TempDir ()
61+ digest := downloadtest .Kept (t , dir , pkg )
62+ store := download .Open (dir , http .DefaultClient )
63+ program := digestOf ("the emulator" )
64+
65+ // act
66+ _ , err := emulator .ReadyFrom (context .Background (), store , download.Pin {URL : "https://snapshot.invalid/qemu-user.deb" , Digest : digest }, "arm64" , program )
67+
68+ // assert
69+ require .NoError (t , err )
70+ kept , err := os .ReadFile (store .Path (program ))
71+ require .NoError (t , err )
72+ assert .Equal (t , []byte ("the emulator" ), kept )
73+ }
74+
75+ func TestAnEmulatorOtherThanItsPinIsRefusedAndNotKept (t * testing.T ) {
76+ // arrange
77+ pkg := debtest .Package (t , map [string ]string {"./usr/bin/qemu-aarch64" : "another emulator" })
78+ dir := t .TempDir ()
79+ digest := downloadtest .Kept (t , dir , pkg )
80+ store := download .Open (dir , http .DefaultClient )
81+ pinned := digestOf ("the emulator" )
82+
83+ // act
84+ _ , err := emulator .ReadyFrom (context .Background (), store , download.Pin {URL : "https://snapshot.invalid/qemu-user.deb" , Digest : digest }, "arm64" , pinned )
85+
86+ // assert
87+ require .ErrorContains (t , err , pinned )
88+ assert .NoFileExists (t , store .Path (digestOf ("another emulator" )))
89+ }
90+
3691func TestAnArm64BuilderRunsAnAmd64ImageWithQEMUOfItsOwnArchitecture (t * testing.T ) {
3792 // arrange
3893 var asked string
@@ -51,6 +106,44 @@ func TestAnArm64BuilderRunsAnAmd64ImageWithQEMUOfItsOwnArchitecture(t *testing.T
51106 assert .Contains (t , asked , "/qemu-user_10.0.13%2Bds-0%2Bdeb13u1_arm64.deb" )
52107}
53108
109+ func TestAnArm64BuilderReadsTheAmd64EmulatorItKeptWithoutThePackage (t * testing.T ) {
110+ // arrange
111+ dir := t .TempDir ()
112+ refuse := roundTrip (func (* http.Request ) (* http.Response , error ) {
113+ return nil , errors .New ("offline" )
114+ })
115+ store := download .Open (dir , & http.Client {Transport : refuse })
116+ kept := store .Path ("sha256:9af86352fd463ad9ec25bdd6a4b891ac6b6a7f3d46a7db3da6f23e2c09094a59" )
117+ require .NoError (t , os .MkdirAll (filepath .Dir (kept ), 0o750 ))
118+ require .NoError (t , os .WriteFile (kept , []byte ("the emulator" ), 0o600 ))
119+
120+ // act
121+ found , err := emulator .Ready (context .Background (), store , "arm64" , "amd64" )
122+
123+ // assert
124+ require .NoError (t , err )
125+ assert .Equal (t , []byte ("the emulator" ), found )
126+ }
127+
128+ func TestAnAmd64BuilderReadsTheArm64EmulatorItKeptWithoutThePackage (t * testing.T ) {
129+ // arrange
130+ dir := t .TempDir ()
131+ refuse := roundTrip (func (* http.Request ) (* http.Response , error ) {
132+ return nil , errors .New ("offline" )
133+ })
134+ store := download .Open (dir , & http.Client {Transport : refuse })
135+ kept := store .Path ("sha256:73cc2584f119f9e74c85c0a8549dbb6fd104ae3897a06a7500797f9e5b4d0943" )
136+ require .NoError (t , os .MkdirAll (filepath .Dir (kept ), 0o750 ))
137+ require .NoError (t , os .WriteFile (kept , []byte ("the emulator" ), 0o600 ))
138+
139+ // act
140+ found , err := emulator .Ready (context .Background (), store , "amd64" , "arm64" )
141+
142+ // assert
143+ require .NoError (t , err )
144+ assert .Equal (t , []byte ("the emulator" ), found )
145+ }
146+
54147func TestAnEmulatorForABuilderMisoHasNoQEMUForIsRefusedNamingIt (t * testing.T ) {
55148 // arrange
56149 store := download .Open (t .TempDir (), http .DefaultClient )
@@ -86,3 +179,9 @@ type roundTrip func(*http.Request) (*http.Response, error)
86179func (f roundTrip ) RoundTrip (request * http.Request ) (* http.Response , error ) {
87180 return f (request )
88181}
182+
183+ func digestOf (content string ) string {
184+ sum := sha256 .Sum256 ([]byte (content ))
185+
186+ return "sha256:" + hex .EncodeToString (sum [:])
187+ }
0 commit comments