Skip to content

Commit

Permalink
The underlying issue is apparently more complex. I think, that partia…
Browse files Browse the repository at this point in the history
…lly it also was that unlike container, network was not removed explicitly.

I've rectified this in the latest commit by modifying the way network is created and removed.
  • Loading branch information
ikolomiyets committed Apr 6, 2020
1 parent 533329b commit 8d295a6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 33 deletions.
9 changes: 4 additions & 5 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
26 changes: 18 additions & 8 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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,
}
Expand All @@ -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)
}
Expand Down
20 changes: 20 additions & 0 deletions generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
36 changes: 16 additions & 20 deletions network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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()
}
Expand All @@ -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)
}
Expand Down

0 comments on commit 8d295a6

Please sign in to comment.