forked from testcontainers/testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples_test.go
More file actions
128 lines (112 loc) · 2.82 KB
/
examples_test.go
File metadata and controls
128 lines (112 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package testcontainers_test
import (
"context"
"fmt"
"io"
"strings"
"time"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/log"
"github.com/testcontainers/testcontainers-go/network"
"github.com/testcontainers/testcontainers-go/wait"
)
func ExampleRun() {
ctx := context.Background()
nw, err := network.New(ctx)
if err != nil {
log.Printf("failed to create network: %s", err)
return
}
defer func() {
if err := nw.Remove(ctx); err != nil {
log.Printf("failed to remove network: %s", err)
}
}()
testFileContent := "Hello from file!"
ctr, err := testcontainers.Run(
ctx,
"nginx:alpine",
network.WithNetwork([]string{"nginx-alias"}, nw),
testcontainers.WithFiles(testcontainers.ContainerFile{
Reader: strings.NewReader(testFileContent),
ContainerFilePath: "/tmp/file.txt",
FileMode: 0o644,
}),
testcontainers.WithTmpfs(map[string]string{
"/tmp": "rw",
}),
testcontainers.WithLabels(map[string]string{
"testcontainers.label": "true",
}),
testcontainers.WithEnv(map[string]string{
"TEST": "true",
}),
testcontainers.WithExposedPorts("80/tcp"),
testcontainers.WithAfterReadyCommand(testcontainers.NewRawCommand([]string{"echo", "hello", "world"})),
testcontainers.WithWaitStrategy(wait.ForListeningPort("80/tcp").WithStartupTimeout(time.Second*5)),
)
defer func() {
if err := testcontainers.TerminateContainer(ctr); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Printf("failed to start container: %s", err)
return
}
state, err := ctr.State(ctx)
if err != nil {
log.Printf("failed to get container state: %s", err)
return
}
fmt.Println(state.Running)
cli, err := testcontainers.NewDockerClientWithOpts(ctx)
if err != nil {
log.Printf("failed to create docker client: %s", err)
return
}
ctrResp, err := cli.ContainerInspect(ctx, ctr.GetContainerID())
if err != nil {
log.Printf("failed to inspect container: %s", err)
return
}
// networks
respNw, ok := ctrResp.NetworkSettings.Networks[nw.Name]
if !ok {
log.Printf("network not found")
return
}
fmt.Println(respNw.Aliases)
// env
fmt.Println(ctrResp.Config.Env[0])
// tmpfs
tmpfs, ok := ctrResp.HostConfig.Tmpfs["/tmp"]
if !ok {
log.Printf("tmpfs not found")
return
}
fmt.Println(tmpfs)
// labels
fmt.Println(ctrResp.Config.Labels["testcontainers.label"])
// files
// copyFileFromContainer {
f, err := ctr.CopyFileFromContainer(context.Background(), "/tmp/file.txt")
if err != nil {
log.Printf("failed to copy file from container: %s", err)
return
}
content, err := io.ReadAll(f)
if err != nil {
log.Printf("failed to read file: %s", err)
return
}
// }
fmt.Println(string(content))
// Output:
// true
// [nginx-alias]
// TEST=true
// rw
// true
// Hello from file!
}