@@ -16,6 +16,7 @@ import (
16
16
ifakes "github.com/buildpacks/imgutil/fakes"
17
17
"github.com/buildpacks/lifecycle/api"
18
18
"github.com/buildpacks/lifecycle/platform/files"
19
+ "github.com/docker/docker/api/types"
19
20
"github.com/docker/docker/api/types/container"
20
21
"github.com/docker/docker/client"
21
22
"github.com/google/go-containerregistry/pkg/authn"
@@ -275,7 +276,7 @@ func testLifecycleExecution(t *testing.T, when spec.G, it spec.S) {
275
276
fakeBuilder * fakes.FakeBuilder
276
277
outBuf bytes.Buffer
277
278
logger * logging.LogWithWriters
278
- docker * client. Client
279
+ docker * fakeDockerClient
279
280
fakeTermui * fakes.FakeTermui
280
281
)
281
282
@@ -289,7 +290,7 @@ func testLifecycleExecution(t *testing.T, when spec.G, it spec.S) {
289
290
fakeBuilder , err = fakes .NewFakeBuilder (fakes .WithSupportedPlatformAPIs ([]* api.Version {api .MustParse ("0.3" )}))
290
291
h .AssertNil (t , err )
291
292
logger = logging .NewLogWithWriters (& outBuf , & outBuf )
292
- docker , err = client . NewClientWithOpts ( client . FromEnv , client . WithVersion ( "1.38" ))
293
+ docker = & fakeDockerClient {}
293
294
h .AssertNil (t , err )
294
295
fakePhaseFactory = fakes .NewFakePhaseFactory ()
295
296
})
@@ -780,6 +781,46 @@ func testLifecycleExecution(t *testing.T, when spec.G, it spec.S) {
780
781
})
781
782
})
782
783
784
+ when ("network is not provided" , func () {
785
+ it ("creates an ephemeral bridge network" , func () {
786
+ beforeNetworks := func () int {
787
+ networks , err := docker .NetworkList (context .Background (), types.NetworkListOptions {})
788
+ h .AssertNil (t , err )
789
+ return len (networks )
790
+ }()
791
+
792
+ opts := build.LifecycleOptions {
793
+ Image : imageName ,
794
+ Builder : fakeBuilder ,
795
+ Termui : fakeTermui ,
796
+ }
797
+
798
+ lifecycle , err := build .NewLifecycleExecution (logger , docker , "some-temp-dir" , opts )
799
+ h .AssertNil (t , err )
800
+
801
+ err = lifecycle .Run (context .Background (), func (execution * build.LifecycleExecution ) build.PhaseFactory {
802
+ return fakePhaseFactory
803
+ })
804
+ h .AssertNil (t , err )
805
+
806
+ for _ , entry := range fakePhaseFactory .NewCalledWithProvider {
807
+ h .AssertContains (t , string (entry .HostConfig ().NetworkMode ), "pack.local/network/" )
808
+ h .AssertEq (t , entry .HostConfig ().NetworkMode .IsDefault (), false )
809
+ h .AssertEq (t , entry .HostConfig ().NetworkMode .IsHost (), false )
810
+ h .AssertEq (t , entry .HostConfig ().NetworkMode .IsNone (), false )
811
+ h .AssertEq (t , entry .HostConfig ().NetworkMode .IsPrivate (), true )
812
+ h .AssertEq (t , entry .HostConfig ().NetworkMode .IsUserDefined (), true )
813
+ }
814
+
815
+ afterNetworks := func () int {
816
+ networks , err := docker .NetworkList (context .Background (), types.NetworkListOptions {})
817
+ h .AssertNil (t , err )
818
+ return len (networks )
819
+ }()
820
+ h .AssertEq (t , beforeNetworks , afterNetworks )
821
+ })
822
+ })
823
+
783
824
when ("Error cases" , func () {
784
825
when ("passed invalid" , func () {
785
826
it ("fails for cache-image" , func () {
@@ -2657,6 +2698,26 @@ func (f *fakeImageFetcher) fetchRunImage(name string) error {
2657
2698
return nil
2658
2699
}
2659
2700
2701
+ type fakeDockerClient struct {
2702
+ nNetworks int
2703
+ build.DockerClient
2704
+ }
2705
+
2706
+ func (f * fakeDockerClient ) NetworkList (ctx context.Context , opts types.NetworkListOptions ) ([]types.NetworkResource , error ) {
2707
+ ret := make ([]types.NetworkResource , f .nNetworks )
2708
+ return ret , nil
2709
+ }
2710
+
2711
+ func (f * fakeDockerClient ) NetworkCreate (ctx context.Context , name string , options types.NetworkCreate ) (types.NetworkCreateResponse , error ) {
2712
+ f .nNetworks ++
2713
+ return types.NetworkCreateResponse {}, nil
2714
+ }
2715
+
2716
+ func (f * fakeDockerClient ) NetworkRemove (ctx context.Context , network string ) error {
2717
+ f .nNetworks --
2718
+ return nil
2719
+ }
2720
+
2660
2721
func newTestLifecycleExecErr (t * testing.T , logVerbose bool , tmpDir string , ops ... func (* build.LifecycleOptions )) (* build.LifecycleExecution , error ) {
2661
2722
docker , err := client .NewClientWithOpts (client .FromEnv , client .WithVersion ("1.38" ))
2662
2723
h .AssertNil (t , err )
0 commit comments