Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add some Vault module examples #1825

Merged
merged 9 commits into from
Nov 21, 2023
Merged
52 changes: 51 additions & 1 deletion docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,16 @@
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 @@ -1097,6 +1107,16 @@
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 @@ -1115,6 +1135,16 @@
}
}()
_ = nginxC.Start(ctx)

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

fmt.Println(state.Running)

// Output:
// true
}

func ExampleContainer_Stop() {
Expand All @@ -1132,8 +1162,18 @@
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 @@ -1157,6 +1197,16 @@
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)
}

Check failure on line 1204 in docker_test.go

View workflow job for this annotation

GitHub Actions / test (1.x, ubuntu-latest) / ./ubuntu-latest/1.x

undefined: err

Check failure on line 1204 in docker_test.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest) / ./ubuntu-latest/1.20.x

undefined: err

Check failure on line 1204 in docker_test.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest) / ./ubuntu-latest/1.20.x

undefined: err

Check failure on line 1205 in docker_test.go

View workflow job for this annotation

GitHub Actions / test (1.x, ubuntu-latest) / ./ubuntu-latest/1.x

undefined: err

Check failure on line 1205 in docker_test.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest) / ./ubuntu-latest/1.20.x

undefined: err

Check failure on line 1205 in docker_test.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest) / ./ubuntu-latest/1.20.x

undefined: err
fmt.Println(state.Running)

Check failure on line 1206 in docker_test.go

View workflow job for this annotation

GitHub Actions / test (1.x, ubuntu-latest) / ./ubuntu-latest/1.x

undefined: err

Check failure on line 1206 in docker_test.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest) / ./ubuntu-latest/1.20.x

undefined: err (typecheck)

Check failure on line 1206 in docker_test.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest) / ./ubuntu-latest/1.20.x

undefined: err) (typecheck)

// Output:
// true
}

func TestContainerCreationWithBindAndVolume(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 ExampleRunContainerWithToken() {
// 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 ExampleRunContainerWithInitCommand() {
// 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
}
10 changes: 10 additions & 0 deletions network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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