Skip to content

Commit

Permalink
docs: add some Vault module examples (#1825)
Browse files Browse the repository at this point in the history
* docs: show examples at pkg.go.dev

* docs: add some Vault module examples

* fix: improve asserts

* Update docker_test.go

Co-authored-by: Manuel de la Peña <social.mdelapenya@gmail.com>

* fix: variable assignment

* fix: start the container in example

* chore: rename network

* fix: testable example lint

---------

Co-authored-by: Manuel de la Peña <social.mdelapenya@gmail.com>
Co-authored-by: Manuel de la Peña <mdelapenya@gmail.com>
  • Loading branch information
3 people authored Nov 21, 2023
1 parent 4e6fbdc commit 2869eab
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 6 deletions.
52 changes: 51 additions & 1 deletion docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,16 @@ func ExampleDockerProvider_CreateContainer() {
log.Fatalf("failed to terminate container: %s", err)
}
}()

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

fmt.Println(state.Running)

// Output:
// true
}

func ExampleContainer_Host() {
Expand All @@ -1134,6 +1144,16 @@ func ExampleContainer_Host() {
ip, _ := nginxC.Host(ctx)
// }
println(ip)

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

fmt.Println(state.Running)

// Output:
// true
}

func ExampleContainer_Start() {
Expand All @@ -1152,6 +1172,16 @@ func ExampleContainer_Start() {
}
}()
_ = nginxC.Start(ctx)

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

fmt.Println(state.Running)

// Output:
// true
}

func ExampleContainer_Stop() {
Expand All @@ -1169,8 +1199,18 @@ func ExampleContainer_Stop() {
log.Fatalf("failed to terminate container: %s", err)
}
}()
fmt.Println("Container has been started")
timeout := 10 * time.Second
_ = nginxC.Stop(ctx, &timeout)
err := nginxC.Stop(ctx, &timeout)
if err != nil {
panic(err)
}

fmt.Println("Container has been stopped")

// Output:
// Container has been started
// Container has been stopped
}

func ExampleContainer_MappedPort() {
Expand All @@ -1194,6 +1234,16 @@ func ExampleContainer_MappedPort() {
port, _ := nginxC.MappedPort(ctx, "80")
_, _ = http.Get(fmt.Sprintf("http://%s:%s", ip, port.Port()))
// }

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

fmt.Println(state.Running)

// Output:
// true
}

func TestContainerCreationWithVolumeAndFileWritingToIt(t *testing.T) {
Expand Down
74 changes: 74 additions & 0 deletions modules/vault/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/testcontainers/testcontainers-go/exec"
"github.com/testcontainers/testcontainers-go/modules/vault"
)

Expand Down Expand Up @@ -34,3 +35,76 @@ func ExampleRunContainer() {
// Output:
// true
}

func ExampleRunContainer_withToken() {
// runVaultContainerWithToken {
ctx := context.Background()

vaultContainer, err := vault.RunContainer(ctx, vault.WithToken("MyToKeN"))
if err != nil {
panic(err)
}

// Clean up the container
defer func() {
if err := vaultContainer.Terminate(ctx); err != nil {
panic(err)
}
}()
// }

state, err := vaultContainer.State(ctx)
if err != nil {
panic(err)
}

fmt.Println(state.Running)

cmds := []string{
"vault", "kv", "put", "secret/test", "value=123",
}
exitCode, _, err := vaultContainer.Exec(ctx, cmds, exec.Multiplexed())
if err != nil {
panic(err)
}

fmt.Println(exitCode)

// Output:
// true
// 0
}

func ExampleRunContainer_withInitCommand() {
// runVaultContainerWithInitCommand {
ctx := context.Background()

vaultContainer, err := vault.RunContainer(ctx, vault.WithToken("MyToKeN"), vault.WithInitCommand(
"auth enable approle", // Enable the approle auth method
"secrets disable secret", // Disable the default secret engine
"secrets enable -version=1 -path=secret kv", // Enable the kv secret engine at version 1
"write --force auth/approle/role/myrole", // Create a role
"write secret/testing top_secret=password123", // Create a secret
))
if err != nil {
panic(err)
}

// Clean up the container
defer func() {
if err := vaultContainer.Terminate(ctx); err != nil {
panic(err)
}
}()
// }

state, err := vaultContainer.State(ctx)
if err != nil {
panic(err)
}

fmt.Println(state.Running)

// Output:
// true
}
13 changes: 12 additions & 1 deletion network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func ExampleNetworkProvider_CreateNetwork() {
// createNetwork {
ctx := context.Background()
networkName := "new-network"
networkName := "new-generic-network"
net, _ := GenericNetwork(ctx, GenericNetworkRequest{
NetworkRequest: NetworkRequest{
Name: networkName,
Expand All @@ -41,6 +41,7 @@ func ExampleNetworkProvider_CreateNetwork() {
networkName,
},
},
Started: true,
})
defer func() {
if err := nginxC.Terminate(ctx); err != nil {
Expand All @@ -49,6 +50,16 @@ func ExampleNetworkProvider_CreateNetwork() {
}()

nginxC.GetContainerID()

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

fmt.Println(state.Running)

// Output:
// true
}

func Test_NetworkWithIPAM(t *testing.T) {
Expand Down
11 changes: 10 additions & 1 deletion wait/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"errors"
"fmt"
"io"
"log"
"testing"
Expand Down Expand Up @@ -38,7 +39,15 @@ func ExampleExecStrategy() {
}
}()

// Here you have a running container
state, err := localstack.State(ctx)
if err != nil {
panic(err)
}

fmt.Println(state.Running)

// Output:
// true
}

type mockExecTarget struct {
Expand Down
30 changes: 27 additions & 3 deletions wait/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,15 @@ func ExampleHTTPStrategy() {
}
}()

// Here you have a running container
state, err := gogs.State(ctx)
if err != nil {
panic(err)
}

fmt.Println(state.Running)

// Output:
// true
}

func ExampleHTTPStrategy_WithPort() {
Expand All @@ -74,7 +82,15 @@ func ExampleHTTPStrategy_WithPort() {
}
}()

// Here you have a running container
state, err := gogs.State(ctx)
if err != nil {
panic(err)
}

fmt.Println(state.Running)

// Output:
// true
}

func ExampleHTTPStrategy_WithBasicAuth() {
Expand All @@ -101,7 +117,15 @@ func ExampleHTTPStrategy_WithBasicAuth() {
}
}()

// Here you have a running container
state, err := gogs.State(ctx)
if err != nil {
panic(err)
}

fmt.Println(state.Running)

// Output:
// true
}

func TestHTTPStrategyWaitUntilReady(t *testing.T) {
Expand Down

0 comments on commit 2869eab

Please sign in to comment.