Skip to content

Commit

Permalink
tests: Add test for getMemory
Browse files Browse the repository at this point in the history
Add a new test for `getMemory()`. This necessitated making
the memory file a `var` to allow the tests to modify it.

Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
  • Loading branch information
jodh-intel committed Jul 18, 2019
1 parent cd2f994 commit c92715f
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 1 deletion.
3 changes: 2 additions & 1 deletion agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ import (

const (
procCgroups = "/proc/cgroups"
meminfo = "/proc/meminfo"

bashPath = "/bin/bash"
shPath = "/bin/sh"
debugConsolePath = "/dev/console"
)

var (
meminfo = "/proc/meminfo"

// cgroup fs is mounted at /sys/fs when systemd is the init process
sysfsDir = "/sys"
cgroupPath = sysfsDir + "/fs/cgroup"
Expand Down
134 changes: 134 additions & 0 deletions agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package main

import (
"fmt"
"io/ioutil"
"os"
"path"
Expand All @@ -25,8 +26,17 @@ import (
const (
testExecID = "testExecID"
testContainerID = "testContainerID"
testFileMode = os.FileMode(0640)
)

func createFileWithPerms(file, contents string, perms os.FileMode) error {
return ioutil.WriteFile(file, []byte(contents), perms)
}

func createFile(file, contents string) error {
return createFileWithPerms(file, contents, testFileMode)
}

func skipUnlessRoot(t *testing.T) {
if os.Getuid() != 0 {
t.Skip("Test disabled as requires root user")
Expand Down Expand Up @@ -487,3 +497,127 @@ func TestAddGuestHooks(t *testing.T) {
assert.True(len(spec.Hooks.Poststop) == 1)
assert.True(strings.Contains(spec.Hooks.Poststop[0].Path, "executable"))
}

func TestGetMemory(t *testing.T) {
assert := assert.New(t)

dir, err := ioutil.TempDir("", "")
assert.NoError(err)
defer os.RemoveAll(dir)

file := filepath.Join(dir, "meminfo")

savedMeminfo := meminfo
defer func() {
meminfo = savedMeminfo
}()

// Override the file
meminfo = file

type testData struct {
contents string
expectedResult string
createFile bool
expectError bool
}

memKB := 13
memKBStr := fmt.Sprintf("%d", memKB)

entry := fmt.Sprintf("MemTotal: %d\n", memKB)
tooManyFieldsEntry := fmt.Sprintf("MemTotal: foo: %d\n", memKB)
noNumericEntry := fmt.Sprintf("MemTotal: \n")

data := []testData{
{
"",
"",
false,
true,
},
{
"",
"",
true,
true,
},
{
"hello",
"",
true,
true,
},
{
"hello:",
"",
true,
true,
},
{
"hello: world",
"",
true,
true,
},
{
"hello: world:",
"",
true,
true,
},
{
"MemTotal: ",
"",
true,
true,
},
{
"MemTotal:",
"",
true,
true,
},
{
tooManyFieldsEntry,
"",
true,
true,
},
{
noNumericEntry,
"",
true,
true,
},
{
entry,
memKBStr,
true,
false,
},
}

for i, d := range data {
msg := fmt.Sprintf("test[%d]: %+v\n", i, d)

if d.createFile {
err := createFile(file, d.contents)
assert.NoError(err, msg)
defer os.Remove(file)
} else {
// Ensure it does not exist
os.Remove(file)
}

mem, err := getMemory()
if d.expectError {
assert.Error(err, msg)
continue
}

assert.NoError(err, msg)

assert.Equal(d.expectedResult, mem, msg)
}
}

0 comments on commit c92715f

Please sign in to comment.