Skip to content

Commit

Permalink
docs: add consistent snippets for network creation (#2703)
Browse files Browse the repository at this point in the history
* docs: add consistent snippets for network creation

* fix: lint
  • Loading branch information
mdelapenya authored Aug 8, 2024
1 parent e4ce806 commit 150a48c
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 40 deletions.
4 changes: 2 additions & 2 deletions docs/features/creating_networks.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ It's important to mention that the name of the network is automatically generate
## Usage example

<!--codeinclude-->
[Creating a network](../../network/network_test.go) inside_block:createNetwork
[Creating a network with options](../../network/network_test.go) inside_block:newNetworkWithOptions
[Creating a network](../../network/examples_test.go) inside_block:createNetwork
[Creating a network with options](../../network/examples_test.go) inside_block:newNetworkWithOptions
<!--/codeinclude-->
76 changes: 76 additions & 0 deletions network/examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package network_test

import (
"context"
"fmt"
"log"

dockernetwork "github.com/docker/docker/api/types/network"

"github.com/testcontainers/testcontainers-go/network"
)

func ExampleNew() {
// createNetwork {
ctx := context.Background()

net, err := network.New(ctx)
if err != nil {
fmt.Println(err)
return
}
defer func() {
if err := net.Remove(ctx); err != nil {
log.Fatalf("failed to remove network: %s", err)
}
}()
// }

fmt.Println(net.ID != "")
fmt.Println(net.Driver)

// Output:
// true
// bridge
}

func ExampleNew_withOptions() {
// newNetworkWithOptions {
ctx := context.Background()

// dockernetwork is the alias used for github.com/docker/docker/api/types/network
ipamConfig := dockernetwork.IPAM{
Driver: "default",
Config: []dockernetwork.IPAMConfig{
{
Subnet: "10.1.1.0/24",
Gateway: "10.1.1.254",
},
},
Options: map[string]string{
"driver": "host-local",
},
}
net, err := network.New(ctx,
network.WithIPAM(&ipamConfig),
network.WithAttachable(),
network.WithDriver("bridge"),
)
if err != nil {
fmt.Println(err)
return
}
defer func() {
if err := net.Remove(ctx); err != nil {
log.Fatalf("failed to remove network: %s", err)
}
}()
// }

fmt.Println(net.ID != "")
fmt.Println(net.Driver)

// Output:
// true
// bridge
}
49 changes: 11 additions & 38 deletions network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package network_test

import (
"context"
"fmt"
"log"
"testing"
"time"

Expand All @@ -23,9 +21,7 @@ const (
nginxDefaultPort = "80/tcp"
)

// Create a network.
func ExampleNew() {
// createNetwork {
func TestNew(t *testing.T) {
ctx := context.Background()

net, err := network.New(ctx,
Expand All @@ -35,18 +31,14 @@ func ExampleNew() {
network.WithInternal(),
network.WithLabels(map[string]string{"this-is-a-test": "value"}),
)
if err != nil {
fmt.Println(err)
return
}
require.NoError(t, err)
defer func() {
if err := net.Remove(ctx); err != nil {
log.Fatalf("failed to remove network: %s", err)
t.Fatalf("failed to remove network: %s", err)
}
}()

networkName := net.Name
// }

nginxC, _ := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Expand All @@ -62,49 +54,30 @@ func ExampleNew() {
})
defer func() {
if err := nginxC.Terminate(ctx); err != nil {
log.Fatalf("failed to terminate container: %s", err)
t.Fatalf("failed to terminate container: %s", err)
}
}()

client, err := testcontainers.NewDockerClientWithOpts(context.Background())
if err != nil {
fmt.Println(err)
return
}
require.NoError(t, err)

resources, err := client.NetworkList(context.Background(), dockernetwork.ListOptions{
Filters: filters.NewArgs(filters.Arg("name", networkName)),
})
if err != nil {
fmt.Println(err)
return
}
require.NoError(t, err)

fmt.Println(len(resources))
assert.Len(t, resources, 1)

newNetwork := resources[0]

expectedLabels := testcontainers.GenericLabels()
expectedLabels["this-is-a-test"] = "true"

fmt.Println(newNetwork.Attachable)
fmt.Println(newNetwork.Internal)
fmt.Println(newNetwork.Labels["this-is-a-test"])

state, err := nginxC.State(ctx)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(state.Running)
assert.True(t, newNetwork.Attachable)
assert.True(t, newNetwork.Internal)
assert.Equal(t, "value", newNetwork.Labels["this-is-a-test"])

// Output:
// 1
// true
// true
// value
// true
require.NoError(t, err)
}

// testNetworkAliases {
Expand Down

0 comments on commit 150a48c

Please sign in to comment.