Skip to content

Commit d6c875d

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. Signed-off-by: Nishad Mathur <nishad.mathur@broadcom.com>
1 parent 58bc648 commit d6c875d

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

src/bpm/integration2/run_test.go

Lines changed: 29 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,12 @@ func TestRun(t *testing.T) {
6172
s := bpmsandbox.New(t)
6273
defer s.Cleanup()
6374

64-
s.LoadFixture("errand", "testdata/errand.yml")
75+
errandJob := generateJobName("errand")
76+
s.LoadFixture(errandJob, "testdata/errand.yml")
6577
stdoutSentinel := "stdout"
6678
stderrSentinel := "stderr"
6779

68-
cmd := s.BPMCmd("run", "errand")
80+
cmd := s.BPMCmd("run", errandJob, "-p", "errand")
6981
output, err := cmd.CombinedOutput()
7082
if err != nil {
7183
t.Fatalf("failed to run bpm: %s", output)
@@ -79,7 +91,7 @@ func TestRun(t *testing.T) {
7991
t.Errorf("stdout/stderr did not contain %q, contents: %q", stderrSentinel, combinedOutput)
8092
}
8193

82-
stdout, err := os.ReadFile(s.Path("sys", "log", "errand", "errand.stdout.log"))
94+
stdout, err := os.ReadFile(s.Path("sys", "log", errandJob, "errand.stdout.log"))
8395
if err != nil {
8496
t.Fatalf("failed to read stdout log: %v", err)
8597
}
@@ -89,7 +101,7 @@ func TestRun(t *testing.T) {
89101
t.Errorf("stdout log file did not contain %q, stdoutLog: %q", stdoutSentinel, stdoutLog)
90102
}
91103

92-
stderr, err := os.ReadFile(s.Path("sys", "log", "errand", "errand.stderr.log"))
104+
stderr, err := os.ReadFile(s.Path("sys", "log", errandJob, "errand.stderr.log"))
93105
if err != nil {
94106
t.Fatalf("failed to read stderr log: %v", err)
95107
}
@@ -98,24 +110,30 @@ func TestRun(t *testing.T) {
98110
t.Errorf("stderr log file did not contain %q, contents: %q", stderrSentinel, stderrLog)
99111
}
100112

101-
pidfile := s.Path("sys", "run", "bpm", "errand", "errand.pid")
113+
pidfile := s.Path("sys", "run", "bpm", errandJob, "errand.pid")
102114
_, err = os.Stat(pidfile)
103115
if !os.IsNotExist(err) {
104116
t.Errorf("expected %q not to exist but it did", pidfile)
105117
}
106118
}
107119

120+
func generateJobName(prefix string) string {
121+
return fmt.Sprintf("%s-%s", prefix, randomString(6))
122+
}
123+
108124
func TestRunWithEnvFlags(t *testing.T) {
109125
t.Parallel()
110126
s := bpmsandbox.New(t)
111127
defer s.Cleanup()
112128

113-
s.LoadFixture("errand", "testdata/env-flag.yml")
129+
errandJob := generateJobName("errand")
130+
s.LoadFixture(errandJob, "testdata/env-flag.yml")
114131
sentinel := "sentinel"
115132

116133
cmd := s.BPMCmd(
117134
"run",
118-
"errand",
135+
errandJob,
136+
"-p", "errand",
119137
"-e", fmt.Sprintf("ENVKEY=%s", sentinel),
120138
)
121139
output, err := cmd.CombinedOutput()
@@ -134,13 +152,15 @@ func TestRunWithVolumeFlags(t *testing.T) {
134152
s := bpmsandbox.New(t)
135153
defer s.Cleanup()
136154

137-
s.LoadFixture("errand", "testdata/volume-flag.yml")
155+
errandJob := generateJobName("errand")
156+
s.LoadFixture(errandJob, "testdata/volume-flag.yml")
138157
extraVolumeDir := s.Path("data", "extra-volume")
139158
extraVolumeFile := filepath.Join(extraVolumeDir, "data.txt")
140159

141160
cmd := s.BPMCmd(
142161
"run",
143-
"errand",
162+
errandJob,
163+
"-p", "errand",
144164
"-v", fmt.Sprintf("%s:writable,allow_executions", extraVolumeDir),
145165
"-e", fmt.Sprintf("FILE_TO_WRITE_TO=%s", extraVolumeFile),
146166
)

0 commit comments

Comments
 (0)