Skip to content

Commit dc868e2

Browse files
committed
feat: allow overriding the session ID
1 parent 9f1aa62 commit dc868e2

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

internal/config/config.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ type Config struct {
5252
// Environment variable: TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX
5353
HubImageNamePrefix string `properties:"hub.image.name.prefix,default="`
5454

55+
// SessionID is the ID of the testing session.
56+
// Setting this value will preclude runs from creating more than one reaper. Therefore,
57+
// changes to ryuk settings past its creation will be ignored.
58+
//
59+
// Environment variable: TESTCONTAINERS_SESSION_ID
60+
SessionID string `properties:"session.id,default="`
61+
5562
// RyukDisabled is a flag to enable or disable the Garbage Collector.
5663
// Setting this to true will prevent testcontainers from automatically cleaning up
5764
// resources, which is particularly important in tests which timeout as they
@@ -121,6 +128,11 @@ func read() Config {
121128
config.HubImageNamePrefix = hubImageNamePrefix
122129
}
123130

131+
sessionID := os.Getenv("TESTCONTAINERS_SESSION_ID")
132+
if sessionID != "" {
133+
config.SessionID = sessionID
134+
}
135+
124136
ryukPrivilegedEnv := os.Getenv("TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED")
125137
if parseBool(ryukPrivilegedEnv) {
126138
config.RyukPrivileged = ryukPrivilegedEnv == "true"

internal/config/config_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const (
2121
func resetTestEnv(t *testing.T) {
2222
t.Helper()
2323
t.Setenv("TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX", "")
24+
t.Setenv("TESTCONTAINERS_SESSION_ID", "")
2425
t.Setenv("TESTCONTAINERS_RYUK_DISABLED", "")
2526
t.Setenv("TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED", "")
2627
t.Setenv("RYUK_VERBOSE", "")
@@ -76,6 +77,7 @@ func TestReadTCConfig(t *testing.T) {
7677
t.Setenv("USERPROFILE", "") // Windows support
7778
t.Setenv("TESTCONTAINERS_RYUK_DISABLED", "true")
7879
t.Setenv("TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX", defaultHubPrefix)
80+
t.Setenv("TESTCONTAINERS_SESSION_ID", "foo")
7981
t.Setenv("TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED", "true")
8082
t.Setenv("RYUK_RECONNECTION_TIMEOUT", "13s")
8183
t.Setenv("RYUK_CONNECTION_TIMEOUT", "12s")
@@ -84,6 +86,7 @@ func TestReadTCConfig(t *testing.T) {
8486

8587
expected := Config{
8688
HubImageNamePrefix: defaultHubPrefix,
89+
SessionID: "foo",
8790
RyukDisabled: true,
8891
RyukPrivileged: true,
8992
Host: "", // docker socket is empty at the properties file
@@ -124,6 +127,7 @@ func TestReadTCConfig(t *testing.T) {
124127
t.Setenv("USERPROFILE", tmpDir) // Windows support
125128
t.Setenv("TESTCONTAINERS_RYUK_DISABLED", "true")
126129
t.Setenv("TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX", defaultHubPrefix)
130+
t.Setenv("TESTCONTAINERS_SESSION_ID", "foo")
127131
t.Setenv("TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED", "true")
128132
t.Setenv("RYUK_VERBOSE", "true")
129133
t.Setenv("RYUK_RECONNECTION_TIMEOUT", "13s")
@@ -132,6 +136,7 @@ func TestReadTCConfig(t *testing.T) {
132136
config := read()
133137
expected := Config{
134138
HubImageNamePrefix: defaultHubPrefix,
139+
SessionID: "foo",
135140
RyukDisabled: true,
136141
RyukPrivileged: true,
137142
RyukVerbose: true,

internal/core/bootstrap.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/google/uuid"
1010
"github.com/shirou/gopsutil/v4/process"
11+
"github.com/testcontainers/testcontainers-go/internal/config"
1112
)
1213

1314
// sessionID returns a unique session ID for the current test session. Because each Go package
@@ -45,6 +46,11 @@ var processID string
4546
const sessionIDPlaceholder = "testcontainers-go:%d:%d"
4647

4748
func init() {
49+
cfg := config.Read()
50+
if cfg.SessionID != "" {
51+
sessionID = cfg.SessionID
52+
}
53+
4854
processID = uuid.New().String()
4955

5056
parentPid := os.Getppid()
@@ -57,7 +63,9 @@ func init() {
5763

5864
processes, err := process.Processes()
5965
if err != nil {
60-
sessionID = uuid.New().String()
66+
if sessionID == "" {
67+
sessionID = uuid.New().String()
68+
}
6169
projectPath = fallbackCwd
6270
return
6371
}
@@ -75,22 +83,26 @@ func init() {
7583

7684
t, err := p.CreateTime()
7785
if err != nil {
78-
sessionID = uuid.New().String()
86+
if sessionID == "" {
87+
sessionID = uuid.New().String()
88+
}
7989
return
8090
}
8191

8292
createTime = t
8393
break
8494
}
8595

86-
hasher := sha256.New()
87-
_, err = fmt.Fprintf(hasher, sessionIDPlaceholder, parentPid, createTime)
88-
if err != nil {
89-
sessionID = uuid.New().String()
90-
return
91-
}
96+
if sessionID == "" {
97+
hasher := sha256.New()
98+
_, err = fmt.Fprintf(hasher, sessionIDPlaceholder, parentPid, createTime)
99+
if err != nil {
100+
sessionID = uuid.New().String()
101+
return
102+
}
92103

93-
sessionID = hex.EncodeToString(hasher.Sum(nil))
104+
sessionID = hex.EncodeToString(hasher.Sum(nil))
105+
}
94106
}
95107

96108
func ProcessID() string {

0 commit comments

Comments
 (0)