2014-01-15 20:26:04 -05:00
|
|
|
package lxc
|
2013-11-15 14:09:29 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2014-01-15 20:26:04 -05:00
|
|
|
"github.com/dotcloud/docker/execdriver"
|
2013-11-15 14:09:29 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
2014-01-15 20:26:04 -05:00
|
|
|
"path"
|
2013-11-15 14:09:29 -05:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLXCConfig(t *testing.T) {
|
|
|
|
root, err := ioutil.TempDir("", "TestLXCConfig")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(root)
|
2014-01-15 20:26:04 -05:00
|
|
|
|
|
|
|
os.MkdirAll(path.Join(root, "containers", "1"), 0777)
|
|
|
|
|
2013-11-15 14:09:29 -05:00
|
|
|
// Memory is allocated randomly for testing
|
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
2014-01-15 20:26:04 -05:00
|
|
|
var (
|
|
|
|
memMin = 33554432
|
|
|
|
memMax = 536870912
|
|
|
|
mem = memMin + rand.Intn(memMax-memMin)
|
|
|
|
cpuMin = 100
|
|
|
|
cpuMax = 10000
|
|
|
|
cpu = cpuMin + rand.Intn(cpuMax-cpuMin)
|
|
|
|
)
|
|
|
|
|
|
|
|
driver, err := NewDriver(root, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-01-20 19:05:07 -05:00
|
|
|
command := &execdriver.Command{
|
2014-01-15 20:26:04 -05:00
|
|
|
ID: "1",
|
2014-01-20 16:23:02 -05:00
|
|
|
Resources: &execdriver.Resources{
|
2014-01-15 20:26:04 -05:00
|
|
|
Memory: int64(mem),
|
|
|
|
CpuShares: int64(cpu),
|
2013-11-15 14:09:29 -05:00
|
|
|
},
|
|
|
|
}
|
2014-01-20 19:05:07 -05:00
|
|
|
p, err := driver.generateLXCConfig(command)
|
2014-01-15 20:26:04 -05:00
|
|
|
if err != nil {
|
2013-11-15 14:09:29 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-01-15 20:26:04 -05:00
|
|
|
grepFile(t, p,
|
2013-11-15 14:09:29 -05:00
|
|
|
fmt.Sprintf("lxc.cgroup.memory.limit_in_bytes = %d", mem))
|
2014-01-15 20:26:04 -05:00
|
|
|
|
|
|
|
grepFile(t, p,
|
2013-11-15 14:09:29 -05:00
|
|
|
fmt.Sprintf("lxc.cgroup.memory.memsw.limit_in_bytes = %d", mem*2))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCustomLxcConfig(t *testing.T) {
|
|
|
|
root, err := ioutil.TempDir("", "TestCustomLxcConfig")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(root)
|
2014-01-15 20:26:04 -05:00
|
|
|
|
|
|
|
os.MkdirAll(path.Join(root, "containers", "1"), 0777)
|
|
|
|
|
|
|
|
driver, err := NewDriver(root, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-01-20 19:05:07 -05:00
|
|
|
command := &execdriver.Command{
|
2014-01-15 20:26:04 -05:00
|
|
|
ID: "1",
|
|
|
|
Privileged: false,
|
|
|
|
Config: []string{
|
|
|
|
"lxc.utsname = docker",
|
|
|
|
"lxc.cgroup.cpuset.cpus = 0,1",
|
2013-11-15 14:09:29 -05:00
|
|
|
},
|
|
|
|
}
|
2014-01-15 20:26:04 -05:00
|
|
|
|
2014-01-20 19:05:07 -05:00
|
|
|
p, err := driver.generateLXCConfig(command)
|
2014-01-15 20:26:04 -05:00
|
|
|
if err != nil {
|
2013-11-15 14:09:29 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-01-15 20:26:04 -05:00
|
|
|
|
|
|
|
grepFile(t, p, "lxc.utsname = docker")
|
|
|
|
grepFile(t, p, "lxc.cgroup.cpuset.cpus = 0,1")
|
2013-11-15 14:09:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func grepFile(t *testing.T, path string, pattern string) {
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
r := bufio.NewReader(f)
|
|
|
|
var (
|
|
|
|
line string
|
|
|
|
)
|
|
|
|
err = nil
|
|
|
|
for err == nil {
|
|
|
|
line, err = r.ReadString('\n')
|
|
|
|
if strings.Contains(line, pattern) == true {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Fatalf("grepFile: pattern \"%s\" not found in \"%s\"", pattern, path)
|
|
|
|
}
|
2013-11-26 02:07:59 -05:00
|
|
|
|
|
|
|
func TestEscapeFstabSpaces(t *testing.T) {
|
|
|
|
var testInputs = map[string]string{
|
|
|
|
" ": "\\040",
|
|
|
|
"": "",
|
|
|
|
"/double space": "/double\\040\\040space",
|
|
|
|
"/some long test string": "/some\\040long\\040test\\040string",
|
|
|
|
"/var/lib/docker": "/var/lib/docker",
|
|
|
|
" leading": "\\040leading",
|
|
|
|
"trailing ": "trailing\\040",
|
|
|
|
}
|
|
|
|
for in, exp := range testInputs {
|
|
|
|
if out := escapeFstabSpaces(in); exp != out {
|
|
|
|
t.Logf("Expected %s got %s", exp, out)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|