Skip to content

Commit 959b2bb

Browse files
author
Rajath Agasthya
committed
Randomize errand job name during tests to avoid cgroup conflicts
Tests in the integration2 package use the same `errand` job name when executing `bpm run`. When these tests are run in quick succession, it causes a race between cgroups from the previous test (with the same name) being cleaned up after `bpm run` is complete and the currently executing test. When `runc` tries to create a new container, it finds a cgroup with the same name (errand) already exists and has processes inside it, so it errors with a `runc run failed: container's cgroup is not empty: 2 process(es) found` message. This change adds a random string to the errand job's name, so cgroups do not conflict with each other.
1 parent 58bc648 commit 959b2bb

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

src/bpm/integration2/run_test.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"flag"
2222
"fmt"
23+
"math/rand"
2324
"os"
2425
"path/filepath"
2526
"strings"
@@ -30,10 +31,20 @@ import (
3031
"bpm/integration2/bpmsandbox"
3132
)
3233

34+
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
35+
3336
var runcExe = flag.String("runcExe", "/var/vcap/packages/bpm/bin/runc", "path to the runc executable")
3437
var tiniExe = flag.String("tiniExe", "/var/vcap/packages/bpm/bin/tini", "path to the tini executable")
3538
var bpmExe = flag.String("bpmExe", "", "path to bpm executable")
3639

40+
func randomString(length int) string {
41+
b := make([]byte, length)
42+
for i := range b {
43+
b[i] = charset[rand.Intn(len(charset))]
44+
}
45+
return string(b)
46+
}
47+
3748
func TestMain(m *testing.M) {
3849
flag.Parse()
3950

@@ -61,11 +72,13 @@ func TestRun(t *testing.T) {
6172
s := bpmsandbox.New(t)
6273
defer s.Cleanup()
6374

64-
s.LoadFixture("errand", "testdata/errand.yml")
75+
errandJob := fmt.Sprintf("errand-%s", randomString(6))
76+
77+
s.LoadFixture(errandJob, "testdata/errand.yml")
6578
stdoutSentinel := "stdout"
6679
stderrSentinel := "stderr"
6780

68-
cmd := s.BPMCmd("run", "errand")
81+
cmd := s.BPMCmd("run", errandJob, "-p", "errand")
6982
output, err := cmd.CombinedOutput()
7083
if err != nil {
7184
t.Fatalf("failed to run bpm: %s", output)
@@ -79,7 +92,7 @@ func TestRun(t *testing.T) {
7992
t.Errorf("stdout/stderr did not contain %q, contents: %q", stderrSentinel, combinedOutput)
8093
}
8194

82-
stdout, err := os.ReadFile(s.Path("sys", "log", "errand", "errand.stdout.log"))
95+
stdout, err := os.ReadFile(s.Path("sys", "log", errandJob, "errand.stdout.log"))
8396
if err != nil {
8497
t.Fatalf("failed to read stdout log: %v", err)
8598
}
@@ -89,7 +102,7 @@ func TestRun(t *testing.T) {
89102
t.Errorf("stdout log file did not contain %q, stdoutLog: %q", stdoutSentinel, stdoutLog)
90103
}
91104

92-
stderr, err := os.ReadFile(s.Path("sys", "log", "errand", "errand.stderr.log"))
105+
stderr, err := os.ReadFile(s.Path("sys", "log", errandJob, "errand.stderr.log"))
93106
if err != nil {
94107
t.Fatalf("failed to read stderr log: %v", err)
95108
}
@@ -98,7 +111,7 @@ func TestRun(t *testing.T) {
98111
t.Errorf("stderr log file did not contain %q, contents: %q", stderrSentinel, stderrLog)
99112
}
100113

101-
pidfile := s.Path("sys", "run", "bpm", "errand", "errand.pid")
114+
pidfile := s.Path("sys", "run", "bpm", errandJob, "errand.pid")
102115
_, err = os.Stat(pidfile)
103116
if !os.IsNotExist(err) {
104117
t.Errorf("expected %q not to exist but it did", pidfile)
@@ -110,12 +123,15 @@ func TestRunWithEnvFlags(t *testing.T) {
110123
s := bpmsandbox.New(t)
111124
defer s.Cleanup()
112125

113-
s.LoadFixture("errand", "testdata/env-flag.yml")
126+
errandJob := fmt.Sprintf("errand-%s", randomString(6))
127+
128+
s.LoadFixture(errandJob, "testdata/env-flag.yml")
114129
sentinel := "sentinel"
115130

116131
cmd := s.BPMCmd(
117132
"run",
118-
"errand",
133+
errandJob,
134+
"-p", "errand",
119135
"-e", fmt.Sprintf("ENVKEY=%s", sentinel),
120136
)
121137
output, err := cmd.CombinedOutput()
@@ -134,13 +150,16 @@ func TestRunWithVolumeFlags(t *testing.T) {
134150
s := bpmsandbox.New(t)
135151
defer s.Cleanup()
136152

137-
s.LoadFixture("errand", "testdata/volume-flag.yml")
153+
errandJob := fmt.Sprintf("errand-%s", randomString(6))
154+
155+
s.LoadFixture(errandJob, "testdata/volume-flag.yml")
138156
extraVolumeDir := s.Path("data", "extra-volume")
139157
extraVolumeFile := filepath.Join(extraVolumeDir, "data.txt")
140158

141159
cmd := s.BPMCmd(
142160
"run",
143-
"errand",
161+
errandJob,
162+
"-p", "errand",
144163
"-v", fmt.Sprintf("%s:writable,allow_executions", extraVolumeDir),
145164
"-e", fmt.Sprintf("FILE_TO_WRITE_TO=%s", extraVolumeFile),
146165
)

0 commit comments

Comments
 (0)