From 2869eabf6525c1bff061eb7e0987af88a273e208 Mon Sep 17 00:00:00 2001 From: Ivan Fernandez Calvo Date: Tue, 21 Nov 2023 11:11:04 +0100 Subject: [PATCH] docs: add some Vault module examples (#1825) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 * fix: variable assignment * fix: start the container in example * chore: rename network * fix: testable example lint --------- Co-authored-by: Manuel de la Peña Co-authored-by: Manuel de la Peña --- docker_test.go | 52 +++++++++++++++++++++++- modules/vault/examples_test.go | 74 ++++++++++++++++++++++++++++++++++ network_test.go | 13 +++++- wait/exec_test.go | 11 ++++- wait/http_test.go | 30 ++++++++++++-- 5 files changed, 174 insertions(+), 6 deletions(-) diff --git a/docker_test.go b/docker_test.go index 6d234cd4c7..ee1bd92216 100644 --- a/docker_test.go +++ b/docker_test.go @@ -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() { @@ -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() { @@ -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() { @@ -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() { @@ -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) { diff --git a/modules/vault/examples_test.go b/modules/vault/examples_test.go index 6140b8fa16..e8638269ba 100644 --- a/modules/vault/examples_test.go +++ b/modules/vault/examples_test.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/testcontainers/testcontainers-go/exec" "github.com/testcontainers/testcontainers-go/modules/vault" ) @@ -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 +} diff --git a/network_test.go b/network_test.go index 22ea225227..4b2c1e6e28 100644 --- a/network_test.go +++ b/network_test.go @@ -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, @@ -41,6 +41,7 @@ func ExampleNetworkProvider_CreateNetwork() { networkName, }, }, + Started: true, }) defer func() { if err := nginxC.Terminate(ctx); err != nil { @@ -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) { diff --git a/wait/exec_test.go b/wait/exec_test.go index 6c91b3a487..d8bbf2ea72 100644 --- a/wait/exec_test.go +++ b/wait/exec_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "errors" + "fmt" "io" "log" "testing" @@ -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 { diff --git a/wait/http_test.go b/wait/http_test.go index 8ae6075451..f173ac7f27 100644 --- a/wait/http_test.go +++ b/wait/http_test.go @@ -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() { @@ -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() { @@ -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) {