lxc_template_unit_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package docker
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io/ioutil"
  6. "math/rand"
  7. "os"
  8. "strings"
  9. "testing"
  10. "time"
  11. )
  12. func TestLXCConfig(t *testing.T) {
  13. root, err := ioutil.TempDir("", "TestLXCConfig")
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. defer os.RemoveAll(root)
  18. // Memory is allocated randomly for testing
  19. rand.Seed(time.Now().UTC().UnixNano())
  20. memMin := 33554432
  21. memMax := 536870912
  22. mem := memMin + rand.Intn(memMax-memMin)
  23. // CPU shares as well
  24. cpuMin := 100
  25. cpuMax := 10000
  26. cpu := cpuMin + rand.Intn(cpuMax-cpuMin)
  27. container := &Container{
  28. root: root,
  29. Config: &Config{
  30. Hostname: "foobar",
  31. Memory: int64(mem),
  32. CpuShares: int64(cpu),
  33. NetworkDisabled: true,
  34. },
  35. hostConfig: &HostConfig{
  36. Privileged: false,
  37. },
  38. }
  39. if err := container.generateLXCConfig(); err != nil {
  40. t.Fatal(err)
  41. }
  42. grepFile(t, container.lxcConfigPath(), "lxc.utsname = foobar")
  43. grepFile(t, container.lxcConfigPath(),
  44. fmt.Sprintf("lxc.cgroup.memory.limit_in_bytes = %d", mem))
  45. grepFile(t, container.lxcConfigPath(),
  46. fmt.Sprintf("lxc.cgroup.memory.memsw.limit_in_bytes = %d", mem*2))
  47. }
  48. func TestCustomLxcConfig(t *testing.T) {
  49. root, err := ioutil.TempDir("", "TestCustomLxcConfig")
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. defer os.RemoveAll(root)
  54. container := &Container{
  55. root: root,
  56. Config: &Config{
  57. Hostname: "foobar",
  58. NetworkDisabled: true,
  59. },
  60. hostConfig: &HostConfig{
  61. Privileged: false,
  62. LxcConf: []KeyValuePair{
  63. {
  64. Key: "lxc.utsname",
  65. Value: "docker",
  66. },
  67. {
  68. Key: "lxc.cgroup.cpuset.cpus",
  69. Value: "0,1",
  70. },
  71. },
  72. },
  73. }
  74. if err := container.generateLXCConfig(); err != nil {
  75. t.Fatal(err)
  76. }
  77. grepFile(t, container.lxcConfigPath(), "lxc.utsname = docker")
  78. grepFile(t, container.lxcConfigPath(), "lxc.cgroup.cpuset.cpus = 0,1")
  79. }
  80. func grepFile(t *testing.T, path string, pattern string) {
  81. f, err := os.Open(path)
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. defer f.Close()
  86. r := bufio.NewReader(f)
  87. var (
  88. line string
  89. )
  90. err = nil
  91. for err == nil {
  92. line, err = r.ReadString('\n')
  93. if strings.Contains(line, pattern) == true {
  94. return
  95. }
  96. }
  97. t.Fatalf("grepFile: pattern \"%s\" not found in \"%s\"", pattern, path)
  98. }
  99. func TestEscapeFstabSpaces(t *testing.T) {
  100. var testInputs = map[string]string{
  101. " ": "\\040",
  102. "": "",
  103. "/double space": "/double\\040\\040space",
  104. "/some long test string": "/some\\040long\\040test\\040string",
  105. "/var/lib/docker": "/var/lib/docker",
  106. " leading": "\\040leading",
  107. "trailing ": "trailing\\040",
  108. }
  109. for in, exp := range testInputs {
  110. if out := escapeFstabSpaces(in); exp != out {
  111. t.Logf("Expected %s got %s", exp, out)
  112. t.Fail()
  113. }
  114. }
  115. }