@@ -3,7 +3,6 @@ package testcontainers
3
3
import (
4
4
"archive/tar"
5
5
"bufio"
6
- "bytes"
7
6
"context"
8
7
"encoding/base64"
9
8
"encoding/binary"
@@ -12,6 +11,7 @@ import (
12
11
"fmt"
13
12
"io"
14
13
"io/fs"
14
+ "net"
15
15
"net/url"
16
16
"os"
17
17
"path/filepath"
@@ -156,7 +156,7 @@ func (c *DockerContainer) PortEndpoint(ctx context.Context, port nat.Port, proto
156
156
157
157
// Host gets host (ip or name) of the docker daemon where the container port is exposed
158
158
// Warning: this is based on your Docker host setting. Will fail if using an SSH tunnel
159
- // You can use the "TC_HOST " env variable to set this yourself
159
+ // You can use the "TESTCONTAINERS_HOST_OVERRIDE " env variable to set this yourself
160
160
func (c * DockerContainer ) Host (ctx context.Context ) (string , error ) {
161
161
host , err := c .provider .DaemonHost (ctx )
162
162
if err != nil {
@@ -507,7 +507,7 @@ func (c *DockerContainer) Exec(ctx context.Context, cmd []string, options ...tce
507
507
return 0 , nil , err
508
508
}
509
509
510
- hijack , err := cli .ContainerExecAttach (ctx , response .ID , types. ExecStartCheck {})
510
+ hijack , err := cli .ContainerExecAttach (ctx , response .ID , container. ExecAttachOptions {})
511
511
if err != nil {
512
512
return 0 , nil , err
513
513
}
@@ -596,7 +596,7 @@ func (c *DockerContainer) CopyDirToContainer(ctx context.Context, hostDirPath st
596
596
// create the directory under its parent
597
597
parent := filepath .Dir (containerParentPath )
598
598
599
- err = c .provider .client .CopyToContainer (ctx , c .ID , parent , buff , types .CopyToContainerOptions {})
599
+ err = c .provider .client .CopyToContainer (ctx , c .ID , parent , buff , container .CopyToContainerOptions {})
600
600
if err != nil {
601
601
return err
602
602
}
@@ -654,7 +654,7 @@ func (c *DockerContainer) copyToContainer(ctx context.Context, fileContent func(
654
654
return err
655
655
}
656
656
657
- err = c .provider .client .CopyToContainer (ctx , c .ID , "/" , buffer , types .CopyToContainerOptions {})
657
+ err = c .provider .client .CopyToContainer (ctx , c .ID , "/" , buffer , container .CopyToContainerOptions {})
658
658
if err != nil {
659
659
return err
660
660
}
@@ -742,57 +742,59 @@ func (c *DockerContainer) startLogProduction(ctx context.Context, opts ...LogPro
742
742
c .logProductionError <- r .Close ()
743
743
return
744
744
default :
745
- h := make ([]byte , 8 )
746
- _ , err := io .ReadFull (r , h )
747
- if err != nil {
748
- // proper type matching requires https://go-review.googlesource.com/c/go/+/250357/ (go 1.16)
749
- if strings .Contains (err .Error (), "use of closed network connection" ) {
750
- now := time .Now ()
751
- since = fmt .Sprintf ("%d.%09d" , now .Unix (), int64 (now .Nanosecond ()))
752
- goto BEGIN
753
- }
754
- if errors .Is (err , context .DeadlineExceeded ) || errors .Is (err , context .Canceled ) {
755
- // Probably safe to continue here
756
- continue
757
- }
745
+ }
746
+ h := make ([]byte , 8 )
747
+ _ , err := io .ReadFull (r , h )
748
+ if err != nil {
749
+ switch {
750
+ case err == io .EOF :
751
+ // No more logs coming
752
+ case errors .Is (err , net .ErrClosed ):
753
+ now := time .Now ()
754
+ since = fmt .Sprintf ("%d.%09d" , now .Unix (), int64 (now .Nanosecond ()))
755
+ goto BEGIN
756
+ case errors .Is (err , context .DeadlineExceeded ) || errors .Is (err , context .Canceled ):
757
+ // Probably safe to continue here
758
+ continue
759
+ default :
758
760
_ , _ = fmt .Fprintf (os .Stderr , "container log error: %+v. %s" , err , logStoppedForOutOfSyncMessage )
759
761
// if we would continue here, the next header-read will result into random data...
760
- return
761
762
}
763
+ return
764
+ }
762
765
763
- count := binary .BigEndian .Uint32 (h [4 :])
764
- if count == 0 {
765
- continue
766
- }
767
- logType := h [0 ]
768
- if logType > 2 {
769
- _ , _ = fmt .Fprintf (os .Stderr , "received invalid log type: %d" , logType )
770
- // sometimes docker returns logType = 3 which is an undocumented log type, so treat it as stdout
771
- logType = 1
772
- }
766
+ count := binary .BigEndian .Uint32 (h [4 :])
767
+ if count == 0 {
768
+ continue
769
+ }
770
+ logType := h [0 ]
771
+ if logType > 2 {
772
+ _ , _ = fmt .Fprintf (os .Stderr , "received invalid log type: %d" , logType )
773
+ // sometimes docker returns logType = 3 which is an undocumented log type, so treat it as stdout
774
+ logType = 1
775
+ }
773
776
774
- // a map of the log type --> int representation in the header, notice the first is blank, this is stdin, but the go docker client doesn't allow following that in logs
775
- logTypes := []string {"" , StdoutLog , StderrLog }
777
+ // a map of the log type --> int representation in the header, notice the first is blank, this is stdin, but the go docker client doesn't allow following that in logs
778
+ logTypes := []string {"" , StdoutLog , StderrLog }
776
779
777
- b := make ([]byte , count )
778
- _ , err = io .ReadFull (r , b )
779
- if err != nil {
780
- // TODO: add-logger: use logger to log out this error
781
- _ , _ = fmt .Fprintf (os .Stderr , "error occurred reading log with known length %s" , err .Error ())
782
- if errors .Is (err , context .DeadlineExceeded ) || errors .Is (err , context .Canceled ) {
783
- // Probably safe to continue here
784
- continue
785
- }
786
- // we can not continue here as the next read most likely will not be the next header
787
- _ , _ = fmt .Fprintln (os .Stderr , logStoppedForOutOfSyncMessage )
788
- return
789
- }
790
- for _ , c := range c .consumers {
791
- c .Accept (Log {
792
- LogType : logTypes [logType ],
793
- Content : b ,
794
- })
780
+ b := make ([]byte , count )
781
+ _ , err = io .ReadFull (r , b )
782
+ if err != nil {
783
+ // TODO: add-logger: use logger to log out this error
784
+ _ , _ = fmt .Fprintf (os .Stderr , "error occurred reading log with known length %s" , err .Error ())
785
+ if errors .Is (err , context .DeadlineExceeded ) || errors .Is (err , context .Canceled ) {
786
+ // Probably safe to continue here
787
+ continue
795
788
}
789
+ // we can not continue here as the next read most likely will not be the next header
790
+ _ , _ = fmt .Fprintln (os .Stderr , logStoppedForOutOfSyncMessage )
791
+ return
792
+ }
793
+ for _ , c := range c .consumers {
794
+ c .Accept (Log {
795
+ LogType : logTypes [logType ],
796
+ Content : b ,
797
+ })
796
798
}
797
799
}
798
800
}()
@@ -881,6 +883,9 @@ var _ ContainerProvider = (*DockerProvider)(nil)
881
883
// BuildImage will build and image from context and Dockerfile, then return the tag
882
884
func (p * DockerProvider ) BuildImage (ctx context.Context , img ImageBuildInfo ) (string , error ) {
883
885
buildOptions , err := img .BuildOptions ()
886
+ if err != nil {
887
+ return "" , err
888
+ }
884
889
885
890
var buildError error
886
891
var resp types.ImageBuildResponse
@@ -912,8 +917,7 @@ func (p *DockerProvider) BuildImage(ctx context.Context, img ImageBuildInfo) (st
912
917
913
918
// need to read the response from Docker, I think otherwise the image
914
919
// might not finish building before continuing to execute here
915
- buf := new (bytes.Buffer )
916
- _ , err = buf .ReadFrom (resp .Body )
920
+ _ , err = io .Copy (io .Discard , resp .Body )
917
921
if err != nil {
918
922
return "" , err
919
923
}
@@ -1337,7 +1341,7 @@ func (p *DockerProvider) Config() TestcontainersConfig {
1337
1341
1338
1342
// DaemonHost gets the host or ip of the Docker daemon where ports are exposed on
1339
1343
// Warning: this is based on your Docker host setting. Will fail if using an SSH tunnel
1340
- // You can use the "TC_HOST " env variable to set this yourself
1344
+ // You can use the "TESTCONTAINERS_HOST_OVERRIDE " env variable to set this yourself
1341
1345
func (p * DockerProvider ) DaemonHost (ctx context.Context ) (string , error ) {
1342
1346
return daemonHost (ctx , p )
1343
1347
}
@@ -1347,7 +1351,7 @@ func daemonHost(ctx context.Context, p *DockerProvider) (string, error) {
1347
1351
return p .hostCache , nil
1348
1352
}
1349
1353
1350
- host , exists := os .LookupEnv ("TC_HOST " )
1354
+ host , exists := os .LookupEnv ("TESTCONTAINERS_HOST_OVERRIDE " )
1351
1355
if exists {
1352
1356
p .hostCache = host
1353
1357
return p .hostCache , nil
@@ -1405,7 +1409,7 @@ func (p *DockerProvider) CreateNetwork(ctx context.Context, req NetworkRequest)
1405
1409
1406
1410
tcConfig := p .Config ().Config
1407
1411
1408
- nc := types. NetworkCreate {
1412
+ nc := network. CreateOptions {
1409
1413
Driver : req .Driver ,
1410
1414
Internal : req .Internal ,
1411
1415
EnableIPv6 : req .EnableIPv6 ,
@@ -1460,12 +1464,12 @@ func (p *DockerProvider) CreateNetwork(ctx context.Context, req NetworkRequest)
1460
1464
}
1461
1465
1462
1466
// GetNetwork returns the object representing the network identified by its name
1463
- func (p * DockerProvider ) GetNetwork (ctx context.Context , req NetworkRequest ) (types. NetworkResource , error ) {
1464
- networkResource , err := p .client .NetworkInspect (ctx , req .Name , types. NetworkInspectOptions {
1467
+ func (p * DockerProvider ) GetNetwork (ctx context.Context , req NetworkRequest ) (network. Inspect , error ) {
1468
+ networkResource , err := p .client .NetworkInspect (ctx , req .Name , network. InspectOptions {
1465
1469
Verbose : true ,
1466
1470
})
1467
1471
if err != nil {
1468
- return types. NetworkResource {}, err
1472
+ return network. Inspect {}, err
1469
1473
}
1470
1474
1471
1475
return networkResource , err
@@ -1501,7 +1505,7 @@ func (p *DockerProvider) GetGatewayIP(ctx context.Context) (string, error) {
1501
1505
1502
1506
func (p * DockerProvider ) getDefaultNetwork (ctx context.Context , cli client.APIClient ) (string , error ) {
1503
1507
// Get list of available networks
1504
- networkResources , err := cli .NetworkList (ctx , types. NetworkListOptions {})
1508
+ networkResources , err := cli .NetworkList (ctx , network. ListOptions {})
1505
1509
if err != nil {
1506
1510
return "" , err
1507
1511
}
@@ -1522,7 +1526,7 @@ func (p *DockerProvider) getDefaultNetwork(ctx context.Context, cli client.APICl
1522
1526
1523
1527
// Create a bridge network for the container communications
1524
1528
if ! reaperNetworkExists {
1525
- _ , err = cli .NetworkCreate (ctx , reaperNetwork , types. NetworkCreate {
1529
+ _ , err = cli .NetworkCreate (ctx , reaperNetwork , network. CreateOptions {
1526
1530
Driver : Bridge ,
1527
1531
Attachable : true ,
1528
1532
Labels : core .DefaultLabels (core .SessionID ()),
0 commit comments