summaryrefslogtreecommitdiff
path: root/vendor/github.com/testcontainers/testcontainers-go/internal/core/bootstrap.go
blob: d249d9be3113b6bfadde316e3c91e3da2a6ff5a8 (plain)
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
package core

import (
	"crypto/sha256"
	"encoding/hex"
	"fmt"
	"os"

	"github.com/google/uuid"
	"github.com/shirou/gopsutil/v4/process"
)

// sessionID returns a unique session ID for the current test session. Because each Go package
// will be run in a separate process, we need a way to identify the current test session.
// By test session, we mean:
//   - a single "go test" invocation (including flags)
//   - a single "go test ./..." invocation (including flags)
//   - the execution of a single test or a set of tests using the IDE
//
// As a consequence, with the sole goal of aggregating test execution across multiple
// packages, this function will use the parent process ID (pid) of the current process
// and its creation date, to use it to generate a unique session ID. We are using the parent pid because
// the current process will be a child process of:
//   - the process that is running the tests, e.g.: "go test";
//   - the process that is running the application in development mode, e.g. "go run main.go -tags dev";
//   - the process that is running the tests in the IDE, e.g.: "go test ./...".
//
// Finally, we will hash the combination of the "testcontainers-go:" string with the parent pid
// and the creation date of that parent process to generate a unique session ID.
//
// This sessionID will be used to:
//   - identify the test session, aggregating the test execution of multiple packages in the same test session.
//   - tag the containers created by testcontainers-go, adding a label to the container with the session ID.
var sessionID string

// projectPath returns the current working directory of the parent test process running Testcontainers for Go.
// If it's not possible to get that directory, the library will use the current working directory. If again
// it's not possible to get the current working directory, the library will use a temporary directory.
var projectPath string

// processID returns a unique ID for the current test process. Because each Go package will be run in a separate process,
// we need a way to identify the current test process, in the form of a UUID
var processID string

const sessionIDPlaceholder = "testcontainers-go:%d:%d"

func init() {
	processID = uuid.New().String()

	parentPid := os.Getppid()
	var createTime int64
	fallbackCwd, err := os.Getwd()
	if err != nil {
		// very unlikely to fail, but if it does, we will use a temp dir
		fallbackCwd = os.TempDir()
	}

	processes, err := process.Processes()
	if err != nil {
		sessionID = uuid.New().String()
		projectPath = fallbackCwd
		return
	}

	for _, p := range processes {
		if int(p.Pid) != parentPid {
			continue
		}

		cwd, err := p.Cwd()
		if err != nil {
			cwd = fallbackCwd
		}
		projectPath = cwd

		t, err := p.CreateTime()
		if err != nil {
			sessionID = uuid.New().String()
			return
		}

		createTime = t
		break
	}

	hasher := sha256.New()
	_, err = fmt.Fprintf(hasher, sessionIDPlaceholder, parentPid, createTime)
	if err != nil {
		sessionID = uuid.New().String()
		return
	}

	sessionID = hex.EncodeToString(hasher.Sum(nil))
}

func ProcessID() string {
	return processID
}

func ProjectPath() string {
	return projectPath
}

func SessionID() string {
	return sessionID
}