@@ -8,23 +8,24 @@ package test
88
99import (
1010 "bytes"
11+ "context"
1112 "fmt"
1213 "io"
1314 "net/http"
1415 "testing"
15- "time"
1616
17- "github.com/hyperledger/fabric-private-chaincode/samples/demos/irb/pkg/container"
1817 "github.com/hyperledger/fabric-private-chaincode/samples/demos/irb/pkg/crypto"
1918 pb "github.com/hyperledger/fabric-private-chaincode/samples/demos/irb/pkg/protos"
2019 "github.com/hyperledger/fabric-private-chaincode/samples/demos/irb/pkg/storage"
21- "github.com/hyperledger/fabric-private-chaincode/samples/demos/irb/pkg/utils"
2220 "github.com/pkg/errors"
21+ "github.com/stretchr/testify/require"
22+ "github.com/testcontainers/testcontainers-go"
23+ "github.com/testcontainers/testcontainers-go/network"
24+ "github.com/testcontainers/testcontainers-go/wait"
2325 "google.golang.org/protobuf/proto"
2426)
2527
2628func requestAttestation () ([]byte , error ) {
27-
2829 resp , err := http .Get ("http://localhost:5000/attestation" )
2930 if err != nil {
3031 return nil , err
@@ -57,7 +58,6 @@ func requestAttestation() ([]byte, error) {
5758}
5859
5960func submitEvaluationPack (pk []byte , req * pb.RegisterDataRequest ) error {
60-
6161 epm := & pb.EvaluationPackMessage {}
6262 epm .RegisteredData = []* pb.RegisterDataRequest {req }
6363
@@ -134,7 +134,7 @@ func upload() (*pb.RegisterDataRequest, error) {
134134 PublicKey : []byte ("some verification key" ),
135135 }
136136
137- //build request
137+ // build request
138138 registerDataRequest := & pb.RegisterDataRequest {
139139 Participant : & userIdentity ,
140140 DecryptionKey : sk ,
@@ -148,70 +148,66 @@ func upload() (*pb.RegisterDataRequest, error) {
148148const networkID = "mytestnetwork"
149149
150150func TestWorker (t * testing.T ) {
151-
152- network := & container.Network {Name : networkID }
153- err := network .Create ()
154- defer network .Remove ()
155- if err != nil {
156- panic (err )
157- }
158-
159- // setup redis
160- redis := & container.Container {
161- Image : "redis" ,
162- Name : "redis-container" ,
163- HostIP : "localhost" ,
164- HostPort : "6379" ,
165- Network : networkID ,
166- }
167- err = redis .Start ()
168- defer redis .Stop ()
169- if err != nil {
170- panic (err )
171- }
172-
173- // setup experiment container
174- experiment := & container.Container {
175- Image : "irb-experimenter-worker" ,
176- Name : "experiment-container" ,
177- HostIP : "localhost" ,
178- HostPort : "5000" ,
179- Env : []string {"REDIS_HOST=redis-container" },
180- Network : networkID ,
181- }
182- err = experiment .Start ()
183- defer experiment .Stop ()
184- if err != nil {
185- panic (err )
186- }
187-
188- // let's wait until experiment service is up an running
189- err = utils .Retry (func () bool {
190- resp , err := http .Get ("http://localhost:5000/info" )
191- if err != nil {
192- return false
193- }
194- return resp .StatusCode == 200
195- }, 5 , 60 * time .Second , 2 * time .Second )
196- if err != nil {
197- panic (err )
198- }
151+ ctx := context .Background ()
152+
153+ // network
154+ net , err := network .New (ctx )
155+ require .NoError (t , err )
156+ defer func () {
157+ err := net .Remove (ctx )
158+ require .NoError (t , err )
159+ }()
160+ networkName := net .Name
161+
162+ // redis
163+ redisImageName := "redis"
164+ redis , err := testcontainers .GenericContainer (ctx , testcontainers.GenericContainerRequest {
165+ ContainerRequest : testcontainers.ContainerRequest {
166+ Image : redisImageName ,
167+ ExposedPorts : []string {fmt .Sprintf ("%d/tcp" , storage .DefaultRedisPort )},
168+ Networks : []string {networkName },
169+ WaitingFor : wait .ForLog ("* Ready to accept connections" ),
170+ },
171+ Started : true ,
172+ })
173+ require .NoError (t , err )
174+ defer func () {
175+ err := redis .Terminate (ctx )
176+ require .NoError (t , err )
177+ }()
178+
179+ redisHost , err := redis .Host (ctx )
180+ require .NoError (t , err )
181+
182+ // experiment
183+ experimentImageName := "irb-experimenter-worker"
184+ experimentPort := "5000/tcp"
185+ experiment , err := testcontainers .GenericContainer (ctx , testcontainers.GenericContainerRequest {
186+ ContainerRequest : testcontainers.ContainerRequest {
187+ Image : experimentImageName ,
188+ ExposedPorts : []string {experimentPort },
189+ Networks : []string {networkName },
190+ Env : map [string ]string {"REDIS_HOST" : redisHost },
191+ WaitingFor : wait .ForExposedPort (),
192+ },
193+ Started : true ,
194+ })
195+ require .NoError (t , err )
196+ defer func () {
197+ err := experiment .Terminate (ctx )
198+ require .NoError (t , err )
199+ }()
199200
200201 req , err := upload ()
201- if err != nil {
202- panic (err )
203- }
202+ require .NoError (t , err )
204203
205204 fmt .Println ("Testing attestation..." )
206205 pk , err := requestAttestation ()
207- if err != nil {
208- panic (err )
209- }
206+ require .NoError (t , err )
210207
211208 fmt .Println ("Testing evaluation pack..." )
212- if err := submitEvaluationPack (pk , req ); err != nil {
213- panic (err )
214- }
209+ err = submitEvaluationPack (pk , req )
210+ require .NoError (t , err )
215211
216212 fmt .Println ("Test done." )
217213}
0 commit comments