Skip to content
This repository has been archived by the owner on Jul 19, 2018. It is now read-only.

Commit

Permalink
Merge pull request #60 from puppetlabs/DI-755/master/get-local-contai…
Browse files Browse the repository at this point in the history
…nerid-in-nested-containers

(DI-755) Update GetLocalContainerID to work in nested containers
  • Loading branch information
davejohnston authored Sep 29, 2017
2 parents e82d72d + d9068f2 commit aeabc55
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 52 deletions.
11 changes: 11 additions & 0 deletions utils/fixtures/cgroup.dind
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
2 changes: 1 addition & 1 deletion utils/getlocalcontainerid.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func GetLocalContainerID(cgroupfile string) (string, error) {
}
defer file.Close()

re := regexp.MustCompile(`^\d+:cpu.*:/docker/(\S+)$`)
re := regexp.MustCompile(`^\d+:cpu.*/docker/(\S+)$`)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
matches := re.FindStringSubmatch(scanner.Text())
Expand Down
113 changes: 62 additions & 51 deletions utils/getlocalcontainerid_test.go
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)
}
}
})
}
}

0 comments on commit aeabc55

Please sign in to comment.