This repository has been archived by the owner on Jul 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from puppetlabs/DI-755/master/get-local-contai…
…nerid-in-nested-containers (DI-755) Update GetLocalContainerID to work in nested containers
- Loading branch information
Showing
3 changed files
with
74 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
11:devices:/docker/5462b001b4b4253ac2ee52b27b04a821e48f369ed2c923b958995e6e51c496b4/docker/2ea749928409f83bd7af82f2d520f56345c99a4fcf5d0642cc99bd5cbc8168d5 | ||
10:memory:/docker/5462b001b4b4253ac2ee52b27b04a821e48f369ed2c923b958995e6e51c496b4/docker/2ea749928409f83bd7af82f2d520f56345c99a4fcf5d0642cc99bd5cbc8168d5 | ||
9:hugetlb:/docker/5462b001b4b4253ac2ee52b27b04a821e48f369ed2c923b958995e6e51c496b4/docker/2ea749928409f83bd7af82f2d520f56345c99a4fcf5d0642cc99bd5cbc8168d5 | ||
8:cpuset:/docker/5462b001b4b4253ac2ee52b27b04a821e48f369ed2c923b958995e6e51c496b4/docker/2ea749928409f83bd7af82f2d520f56345c99a4fcf5d0642cc99bd5cbc8168d5 | ||
7:net_prio,net_cls:/docker/5462b001b4b4253ac2ee52b27b04a821e48f369ed2c923b958995e6e51c496b4/docker/2ea749928409f83bd7af82f2d520f56345c99a4fcf5d0642cc99bd5cbc8168d5 | ||
6:perf_event:/docker/5462b001b4b4253ac2ee52b27b04a821e48f369ed2c923b958995e6e51c496b4/docker/2ea749928409f83bd7af82f2d520f56345c99a4fcf5d0642cc99bd5cbc8168d5 | ||
5:blkio:/docker/5462b001b4b4253ac2ee52b27b04a821e48f369ed2c923b958995e6e51c496b4/docker/2ea749928409f83bd7af82f2d520f56345c99a4fcf5d0642cc99bd5cbc8168d5 | ||
4:pids:/docker/5462b001b4b4253ac2ee52b27b04a821e48f369ed2c923b958995e6e51c496b4/docker/2ea749928409f83bd7af82f2d520f56345c99a4fcf5d0642cc99bd5cbc8168d5 | ||
3:freezer:/docker/5462b001b4b4253ac2ee52b27b04a821e48f369ed2c923b958995e6e51c496b4/docker/2ea749928409f83bd7af82f2d520f56345c99a4fcf5d0642cc99bd5cbc8168d5 | ||
2:cpuacct,cpu:/docker/5462b001b4b4253ac2ee52b27b04a821e48f369ed2c923b958995e6e51c496b4/docker/2ea749928409f83bd7af82f2d520f56345c99a4fcf5d0642cc99bd5cbc8168d5 | ||
1:name=systemd:/docker/5462b001b4b4253ac2ee52b27b04a821e48f369ed2c923b958995e6e51c496b4/docker/2ea749928409f83bd7af82f2d520f56345c99a4fcf5d0642cc99bd5cbc8168d5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,72 @@ | ||
package utils | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func Test_GetLocalContainerID(t *testing.T) { | ||
cgroupfile := "./fixtures/cgroup.valid" | ||
expectedContainerID := "364b47437b435e097489faaafe9e896c20096fa555dae80d569e0cfa078a6d55" | ||
actualContainerID, err := GetLocalContainerID(cgroupfile) | ||
if err != nil { | ||
t.Errorf("Unexpected error returned [%s], expected containerID [%s]", err, expectedContainerID) | ||
} | ||
if strings.Compare(expectedContainerID, actualContainerID) != 0 { | ||
t.Errorf("Extracted containerID [%s] does not match expected value [%s]", actualContainerID, expectedContainerID) | ||
} | ||
} | ||
func Test_GetLocalContainerID_cgroupCpuMultipleKeys(t *testing.T) { | ||
cgroupfile := "./fixtures/cgroup.multi" | ||
expectedContainerID := "c7acb1eb27bb163d07c2ce0a3e7deb36a698824472f21074662e605bf57c7521" | ||
actualContainerID, err := GetLocalContainerID(cgroupfile) | ||
if err != nil { | ||
t.Errorf("Unexpected error returned [%s], expected containerID [%s]", err, expectedContainerID) | ||
} | ||
if strings.Compare(expectedContainerID, actualContainerID) != 0 { | ||
t.Errorf("Extracted containerID [%s] does not match expected value [%s]", actualContainerID, expectedContainerID) | ||
} | ||
} | ||
|
||
func Test_GetLocalContainerID_missingCgroupFile(t *testing.T) { | ||
cgroupfile := "./fixtures/cgroup.doesnotexist" | ||
_, err := GetLocalContainerID(cgroupfile) | ||
if err == nil { | ||
t.Errorf("Expected error not thrown") | ||
} | ||
} | ||
|
||
func Test_GetLocalContainerID_emptyCgroupFile(t *testing.T) { | ||
cgroupfile := "./fixtures/cgroup.empty" | ||
_, err := GetLocalContainerID(cgroupfile) | ||
if err == nil { | ||
t.Errorf("Expected error not thrown") | ||
} | ||
var getLocalContainerIDTests = []struct { | ||
title string | ||
cgroupfile string | ||
expectedContainerID string | ||
expectError bool | ||
}{ | ||
{ | ||
title: "Simple success case", | ||
cgroupfile: "./fixtures/cgroup.valid", | ||
expectedContainerID: "364b47437b435e097489faaafe9e896c20096fa555dae80d569e0cfa078a6d55", | ||
expectError: false, | ||
}, | ||
{ | ||
title: "cgroupCpuMultipleKeys", | ||
cgroupfile: "./fixtures/cgroup.multi", | ||
expectedContainerID: "c7acb1eb27bb163d07c2ce0a3e7deb36a698824472f21074662e605bf57c7521", | ||
expectError: false, | ||
}, | ||
{ | ||
title: "nested container running in Docker-in-Docker", | ||
cgroupfile: "./fixtures/cgroup.dind", | ||
expectedContainerID: "2ea749928409f83bd7af82f2d520f56345c99a4fcf5d0642cc99bd5cbc8168d5", | ||
expectError: false, | ||
}, | ||
{ | ||
title: "missingCgroupFile", | ||
cgroupfile: "./fixtures/cgroup.doesnotexist", | ||
expectedContainerID: "n/a", | ||
expectError: true, | ||
}, | ||
{ | ||
title: "emptyCgroupFile", | ||
cgroupfile: "./fixtures/cgroup.empty", | ||
expectedContainerID: "n/a", | ||
expectError: true, | ||
}, | ||
{ | ||
title: "cgroupCpuMissing", | ||
cgroupfile: "./fixtures/cgroup.nocpu", | ||
expectedContainerID: "n/a", | ||
expectError: true, | ||
}, | ||
{ | ||
title: "cgroupCpuNoId", | ||
cgroupfile: "./fixtures/cgroup.emptycpu", | ||
expectedContainerID: "n/a", | ||
expectError: true, | ||
}, | ||
} | ||
|
||
func Test_GetLocalContainerID_cgroupCpuMissing(t *testing.T) { | ||
cgroupfile := "./fixtures/cgroup.nocpu" | ||
_, err := GetLocalContainerID(cgroupfile) | ||
if err == nil { | ||
t.Errorf("Expected error not thrown") | ||
} | ||
} | ||
|
||
func Test_GetLocalContainerID_cgroupCpuNoId(t *testing.T) { | ||
cgroupfile := "./fixtures/cgroup.emptycpu" | ||
_, err := GetLocalContainerID(cgroupfile) | ||
if err == nil { | ||
t.Errorf("Expected error not thrown") | ||
func Test_GetLocalContainerID(t *testing.T) { | ||
for _, test := range getLocalContainerIDTests { | ||
t.Run(test.title, func(t *testing.T) { | ||
actualContainerID, err := GetLocalContainerID(test.cgroupfile) | ||
if err != nil { | ||
if !test.expectError { | ||
t.Errorf("Test [%s] threw unexpected error [%s]", test.title, err) | ||
} | ||
} else { | ||
if actualContainerID != test.expectedContainerID { | ||
t.Errorf("Extracted containerID [%s] does not match expected value [%s]", actualContainerID, test.expectedContainerID) | ||
} | ||
} | ||
}) | ||
} | ||
} |