Skip to content

Commit 6994ff2

Browse files
author
Mrunal Patel
authored
Merge pull request #1967 from cyphar/integration-factory-fixup
integration: fix mis-use of libcontainer.Factory
2 parents 8011af4 + 565325f commit 6994ff2

File tree

3 files changed

+36
-97
lines changed

3 files changed

+36
-97
lines changed

libcontainer/integration/exec_test.go

+15-73
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,14 @@ func TestEnter(t *testing.T) {
206206
if testing.Short() {
207207
return
208208
}
209-
root, err := newTestRoot()
210-
ok(t, err)
211-
defer os.RemoveAll(root)
212209

213210
rootfs, err := newRootfs()
214211
ok(t, err)
215212
defer remove(rootfs)
216213

217214
config := newTemplateConfig(rootfs)
218215

219-
container, err := factory.Create("test", config)
216+
container, err := newContainerWithName("test", config)
220217
ok(t, err)
221218
defer container.Destroy()
222219

@@ -296,17 +293,14 @@ func TestProcessEnv(t *testing.T) {
296293
if testing.Short() {
297294
return
298295
}
299-
root, err := newTestRoot()
300-
ok(t, err)
301-
defer os.RemoveAll(root)
302296

303297
rootfs, err := newRootfs()
304298
ok(t, err)
305299
defer remove(rootfs)
306300

307301
config := newTemplateConfig(rootfs)
308302

309-
container, err := factory.Create("test", config)
303+
container, err := newContainerWithName("test", config)
310304
ok(t, err)
311305
defer container.Destroy()
312306

@@ -347,9 +341,6 @@ func TestProcessEmptyCaps(t *testing.T) {
347341
if testing.Short() {
348342
return
349343
}
350-
root, err := newTestRoot()
351-
ok(t, err)
352-
defer os.RemoveAll(root)
353344

354345
rootfs, err := newRootfs()
355346
ok(t, err)
@@ -358,7 +349,7 @@ func TestProcessEmptyCaps(t *testing.T) {
358349
config := newTemplateConfig(rootfs)
359350
config.Capabilities = nil
360351

361-
container, err := factory.Create("test", config)
352+
container, err := newContainerWithName("test", config)
362353
ok(t, err)
363354
defer container.Destroy()
364355

@@ -399,17 +390,14 @@ func TestProcessCaps(t *testing.T) {
399390
if testing.Short() {
400391
return
401392
}
402-
root, err := newTestRoot()
403-
ok(t, err)
404-
defer os.RemoveAll(root)
405393

406394
rootfs, err := newRootfs()
407395
ok(t, err)
408396
defer remove(rootfs)
409397

410398
config := newTemplateConfig(rootfs)
411399

412-
container, err := factory.Create("test", config)
400+
container, err := newContainerWithName("test", config)
413401
ok(t, err)
414402
defer container.Destroy()
415403

@@ -471,20 +459,13 @@ func TestAdditionalGroups(t *testing.T) {
471459
if testing.Short() {
472460
return
473461
}
474-
root, err := newTestRoot()
475-
ok(t, err)
476-
defer os.RemoveAll(root)
477-
478462
rootfs, err := newRootfs()
479463
ok(t, err)
480464
defer remove(rootfs)
481465

482466
config := newTemplateConfig(rootfs)
483467

484-
factory, err := libcontainer.New(root, libcontainer.Cgroupfs)
485-
ok(t, err)
486-
487-
container, err := factory.Create("test", config)
468+
container, err := newContainerWithName("test", config)
488469
ok(t, err)
489470
defer container.Destroy()
490471

@@ -531,21 +512,13 @@ func testFreeze(t *testing.T, systemd bool) {
531512
if testing.Short() {
532513
return
533514
}
534-
root, err := newTestRoot()
535-
ok(t, err)
536-
defer os.RemoveAll(root)
537515

538516
rootfs, err := newRootfs()
539517
ok(t, err)
540518
defer remove(rootfs)
541519

542520
config := newTemplateConfig(rootfs)
543-
f := factory
544-
if systemd {
545-
f = systemdFactory
546-
}
547-
548-
container, err := f.Create("test", config)
521+
container, err := newContainerWithName("test", config)
549522
ok(t, err)
550523
defer container.Destroy()
551524

@@ -727,11 +700,6 @@ func TestContainerState(t *testing.T) {
727700
if testing.Short() {
728701
return
729702
}
730-
root, err := newTestRoot()
731-
if err != nil {
732-
t.Fatal(err)
733-
}
734-
defer os.RemoveAll(root)
735703

736704
rootfs, err := newRootfs()
737705
if err != nil {
@@ -754,7 +722,7 @@ func TestContainerState(t *testing.T) {
754722
{Type: configs.NEWNET},
755723
})
756724

757-
container, err := factory.Create("test", config)
725+
container, err := newContainerWithName("test", config)
758726
if err != nil {
759727
t.Fatal(err)
760728
}
@@ -807,7 +775,7 @@ func TestPassExtraFiles(t *testing.T) {
807775

808776
config := newTemplateConfig(rootfs)
809777

810-
container, err := factory.Create("test", config)
778+
container, err := newContainerWithName("test", config)
811779
if err != nil {
812780
t.Fatal(err)
813781
}
@@ -867,11 +835,6 @@ func TestMountCmds(t *testing.T) {
867835
if testing.Short() {
868836
return
869837
}
870-
root, err := newTestRoot()
871-
if err != nil {
872-
t.Fatal(err)
873-
}
874-
defer os.RemoveAll(root)
875838

876839
rootfs, err := newRootfs()
877840
if err != nil {
@@ -901,7 +864,7 @@ func TestMountCmds(t *testing.T) {
901864
},
902865
})
903866

904-
container, err := factory.Create("test", config)
867+
container, err := newContainerWithName("test", config)
905868
if err != nil {
906869
t.Fatal(err)
907870
}
@@ -937,9 +900,6 @@ func TestSysctl(t *testing.T) {
937900
if testing.Short() {
938901
return
939902
}
940-
root, err := newTestRoot()
941-
ok(t, err)
942-
defer os.RemoveAll(root)
943903

944904
rootfs, err := newRootfs()
945905
ok(t, err)
@@ -950,7 +910,7 @@ func TestSysctl(t *testing.T) {
950910
"kernel.shmmni": "8192",
951911
}
952912

953-
container, err := factory.Create("test", config)
913+
container, err := newContainerWithName("test", config)
954914
ok(t, err)
955915
defer container.Destroy()
956916

@@ -1077,9 +1037,6 @@ func TestOomScoreAdj(t *testing.T) {
10771037
if testing.Short() {
10781038
return
10791039
}
1080-
root, err := newTestRoot()
1081-
ok(t, err)
1082-
defer os.RemoveAll(root)
10831040

10841041
rootfs, err := newRootfs()
10851042
ok(t, err)
@@ -1088,10 +1045,7 @@ func TestOomScoreAdj(t *testing.T) {
10881045
config := newTemplateConfig(rootfs)
10891046
config.OomScoreAdj = ptrInt(200)
10901047

1091-
factory, err := libcontainer.New(root, libcontainer.Cgroupfs)
1092-
ok(t, err)
1093-
1094-
container, err := factory.Create("test", config)
1048+
container, err := newContainerWithName("test", config)
10951049
ok(t, err)
10961050
defer container.Destroy()
10971051

@@ -1198,7 +1152,7 @@ func TestHook(t *testing.T) {
11981152
ok(t, err)
11991153
ok(t, json.NewEncoder(f).Encode(config))
12001154

1201-
container, err := factory.Create("test", config)
1155+
container, err := newContainerWithName("test", config)
12021156
ok(t, err)
12031157

12041158
var stdout bytes.Buffer
@@ -1307,10 +1261,7 @@ func TestRootfsPropagationSlaveMount(t *testing.T) {
13071261
Device: "bind",
13081262
Flags: unix.MS_BIND | unix.MS_REC})
13091263

1310-
// TODO: systemd specific processing
1311-
f := factory
1312-
1313-
container, err := f.Create("testSlaveMount", config)
1264+
container, err := newContainerWithName("testSlaveMount", config)
13141265
ok(t, err)
13151266
defer container.Destroy()
13161267

@@ -1425,10 +1376,7 @@ func TestRootfsPropagationSharedMount(t *testing.T) {
14251376
Device: "bind",
14261377
Flags: unix.MS_BIND | unix.MS_REC})
14271378

1428-
// TODO: systemd specific processing
1429-
f := factory
1430-
1431-
container, err := f.Create("testSharedMount", config)
1379+
container, err := newContainerWithName("testSharedMount", config)
14321380
ok(t, err)
14331381
defer container.Destroy()
14341382

@@ -1729,9 +1677,6 @@ func TestTmpfsCopyUp(t *testing.T) {
17291677
if testing.Short() {
17301678
return
17311679
}
1732-
root, err := newTestRoot()
1733-
ok(t, err)
1734-
defer os.RemoveAll(root)
17351680

17361681
rootfs, err := newRootfs()
17371682
ok(t, err)
@@ -1746,10 +1691,7 @@ func TestTmpfsCopyUp(t *testing.T) {
17461691
Extensions: configs.EXT_COPYUP,
17471692
})
17481693

1749-
factory, err := libcontainer.New(root, libcontainer.Cgroupfs)
1750-
ok(t, err)
1751-
1752-
container, err := factory.Create("test", config)
1694+
container, err := newContainerWithName("test", config)
17531695
ok(t, err)
17541696
defer container.Destroy()
17551697

libcontainer/integration/init_test.go

+7-22
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"testing"
77

88
"github.com/opencontainers/runc/libcontainer"
9-
"github.com/opencontainers/runc/libcontainer/cgroups/systemd"
109
_ "github.com/opencontainers/runc/libcontainer/nsenter"
1110

1211
"github.com/sirupsen/logrus"
@@ -29,33 +28,19 @@ func init() {
2928
}
3029
}
3130

32-
var (
33-
factory libcontainer.Factory
34-
systemdFactory libcontainer.Factory
35-
)
31+
var testRoots []string
3632

3733
func TestMain(m *testing.M) {
38-
var (
39-
err error
40-
ret int
41-
)
42-
4334
logrus.SetOutput(os.Stderr)
4435
logrus.SetLevel(logrus.InfoLevel)
4536

46-
factory, err = libcontainer.New("/run/libctTests", libcontainer.Cgroupfs)
47-
if err != nil {
48-
logrus.Error(err)
49-
os.Exit(1)
50-
}
51-
if systemd.UseSystemd() {
52-
systemdFactory, err = libcontainer.New("/run/libctTests", libcontainer.SystemdCgroups)
53-
if err != nil {
54-
logrus.Error(err)
55-
os.Exit(1)
37+
// Clean up roots after running everything.
38+
defer func() {
39+
for _, root := range testRoots {
40+
os.RemoveAll(root)
5641
}
57-
}
42+
}()
5843

59-
ret = m.Run()
44+
ret := m.Run()
6045
os.Exit(ret)
6146
}

libcontainer/integration/utils_test.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func newTestRoot() (string, error) {
7777
if err := os.MkdirAll(dir, 0700); err != nil {
7878
return "", err
7979
}
80+
testRoots = append(testRoots, dir)
8081
return dir, nil
8182
}
8283

@@ -127,9 +128,20 @@ func newContainer(config *configs.Config) (libcontainer.Container, error) {
127128
}
128129

129130
func newContainerWithName(name string, config *configs.Config) (libcontainer.Container, error) {
130-
f := factory
131+
root, err := newTestRoot()
132+
if err != nil {
133+
return nil, err
134+
}
135+
136+
f, err := libcontainer.New(root, libcontainer.Cgroupfs)
137+
if err != nil {
138+
return nil, err
139+
}
131140
if config.Cgroups != nil && config.Cgroups.Parent == "system.slice" {
132-
f = systemdFactory
141+
f, err = libcontainer.New(root, libcontainer.SystemdCgroups)
142+
if err != nil {
143+
return nil, err
144+
}
133145
}
134146
return f.Create(name, config)
135147
}

0 commit comments

Comments
 (0)