diff --git a/docker.go b/docker.go index 38256366d0..8bb03ce439 100644 --- a/docker.go +++ b/docker.go @@ -383,11 +383,9 @@ type DockerNetwork struct { } // Remove is used to remove the network. It is usually triggered by as defer function. -func (n *DockerNetwork) Remove(_ context.Context) error { - if n.terminationSignal != nil { - n.terminationSignal <- true - } - return nil +func (n *DockerNetwork) Remove(ctx context.Context) error { + + return n.provider.client.NetworkRemove(ctx, n.ID) } // DockerProvider implements the ContainerProvider interface @@ -704,6 +702,7 @@ func (p *DockerProvider) CreateNetwork(ctx context.Context, req NetworkRequest) Driver: req.Driver, Name: req.Name, terminationSignal: termSignal, + provider: p, } return n, nil diff --git a/docker_test.go b/docker_test.go index afdde1eeb5..d0b71cb5d1 100644 --- a/docker_test.go +++ b/docker_test.go @@ -46,12 +46,13 @@ func TestContainerAttachedToNewNetwork(t *testing.T) { }, } - provider, err := gcr.ProviderType.GetProvider() - - newNetwork, err := provider.CreateNetwork(ctx, NetworkRequest{ - Name: networkName, - CheckDuplicate: true, + newNetwork, err := GenericNetwork(ctx, GenericNetworkRequest{ + NetworkRequest: NetworkRequest{ + Name: networkName, + CheckDuplicate: true, + }, }) + if err != nil { t.Fatal(err) } @@ -98,8 +99,12 @@ func TestContainerWithHostNetworkOptions(t *testing.T) { gcr := GenericContainerRequest{ ContainerRequest: ContainerRequest{ Image: "nginx", + Privileged: true, SkipReaper: true, NetworkMode: "host", + ExposedPorts: []string{ + "80/tcp", + }, }, Started: true, } @@ -111,12 +116,17 @@ func TestContainerWithHostNetworkOptions(t *testing.T) { defer nginxC.Terminate(ctx) - host, err := nginxC.Host(ctx) + //host, err := nginxC.Host(ctx) + //if err != nil { + // t.Errorf("Expected host %s. Got '%d'.", host, err) + //} + // + endpoint, err := nginxC.Endpoint(ctx, "http") if err != nil { - t.Errorf("Expected host %s. Got '%d'.", host, err) + t.Errorf("Expected server endpoint. Got '%v'.", err) } - _, err = http.Get("http://" + host + ":80") + _, err = http.Get(endpoint) if err != nil { t.Errorf("Expected OK response. Got '%d'.", err) } diff --git a/generic.go b/generic.go index cf39b8c1a0..5c611e0905 100644 --- a/generic.go +++ b/generic.go @@ -13,6 +13,26 @@ type GenericContainerRequest struct { ProviderType ProviderType // which provider to use, Docker if empty } +// GenericNetworkRequest represents parameters to a generic network +type GenericNetworkRequest struct { + NetworkRequest // embedded request for provider + ProviderType ProviderType // which provider to use, Docker if empty +} + +// GenericNetwork creates a generic network with parameters +func GenericNetwork(ctx context.Context, req GenericNetworkRequest) (Network, error) { + provider, err := req.ProviderType.GetProvider() + if err != nil { + return nil, err + } + network, err := provider.CreateNetwork(ctx, req.NetworkRequest) + if err != nil { + return nil, errors.Wrap(err, "failed to create network") + } + + return network, nil +} + // GenericContainer creates a generic container with parameters func GenericContainer(ctx context.Context, req GenericContainerRequest) (Container, error) { provider, err := req.ProviderType.GetProvider() diff --git a/network_test.go b/network_test.go index 41a5b33f9a..43791de3f9 100644 --- a/network_test.go +++ b/network_test.go @@ -12,7 +12,15 @@ import ( func ExampleNetworkProvider_CreateNetwork() { ctx := context.Background() networkName := "new-network" - gcr := GenericContainerRequest{ + net, _ := GenericNetwork(ctx, GenericNetworkRequest{ + NetworkRequest: NetworkRequest{ + Name: networkName, + CheckDuplicate: true, + }, + }) + defer net.Remove(ctx) + + nginxC, _ := GenericContainer(ctx, GenericContainerRequest{ ContainerRequest: ContainerRequest{ Image: "nginx", ExposedPorts: []string{ @@ -22,15 +30,7 @@ func ExampleNetworkProvider_CreateNetwork() { networkName, }, }, - } - provider, _ := gcr.ProviderType.GetProvider() - net, _ := provider.CreateNetwork(ctx, NetworkRequest{ - Name: networkName, - CheckDuplicate: true, }) - defer net.Remove(ctx) - - nginxC, _ := GenericContainer(ctx, gcr) defer nginxC.Terminate(ctx) nginxC.GetContainerID() } @@ -57,24 +57,20 @@ func Test_MultipleContainersInTheNewNetwork(t *testing.T) { Networks: []string{networkName}, } - gcr := GenericContainerRequest{ - ContainerRequest: dbContainerRequest, - Started: true, - } - - provider, err := gcr.ProviderType.GetProvider() - if err != nil { - t.Fatal("cannot get provider") - } + net, err := GenericNetwork(ctx, GenericNetworkRequest{ + NetworkRequest: networkRequest, + }) - net, err := provider.CreateNetwork(ctx, networkRequest) if err != nil { t.Fatal("cannot create network") } defer net.Remove(ctx) - postgres, err := GenericContainer(ctx, gcr) + postgres, err := GenericContainer(ctx, GenericContainerRequest{ + ContainerRequest: dbContainerRequest, + Started: true, + }) if err != nil { t.Fatal(err) }