lxc_template_unit_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. Memory: int64(mem),
  31. CpuShares: int64(cpu),
  32. NetworkDisabled: true,
  33. },
  34. hostConfig: &HostConfig{
  35. Privileged: false,
  36. },
  37. }
  38. if err := container.generateLXCConfig(); err != nil {
  39. t.Fatal(err)
  40. }
  41. grepFile(t, container.lxcConfigPath(),
  42. fmt.Sprintf("lxc.cgroup.memory.limit_in_bytes = %d", mem))
  43. grepFile(t, container.lxcConfigPath(),
  44. fmt.Sprintf("lxc.cgroup.memory.memsw.limit_in_bytes = %d", mem*2))
  45. }
  46. func TestCustomLxcConfig(t *testing.T) {
  47. root, err := ioutil.TempDir("", "TestCustomLxcConfig")
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. defer os.RemoveAll(root)
  52. container := &Container{
  53. root: root,
  54. Config: &Config{
  55. Hostname: "foobar",
  56. NetworkDisabled: true,
  57. },
  58. hostConfig: &HostConfig{
  59. Privileged: false,
  60. LxcConf: []KeyValuePair{
  61. {
  62. Key: "lxc.utsname",
  63. Value: "docker",
  64. },
  65. {
  66. Key: "lxc.cgroup.cpuset.cpus",
  67. Value: "0,1",
  68. },
  69. },
  70. },
  71. }
  72. if err := container.generateLXCConfig(); err != nil {
  73. t.Fatal(err)
  74. }
  75. grepFile(t, container.lxcConfigPath(), "lxc.utsname = docker")
  76. grepFile(t, container.lxcConfigPath(), "lxc.cgroup.cpuset.cpus = 0,1")
  77. }
  78. func grepFile(t *testing.T, path string, pattern string) {
  79. f, err := os.Open(path)
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. defer f.Close()
  84. r := bufio.NewReader(f)
  85. var (
  86. line string
  87. )
  88. err = nil
  89. for err == nil {
  90. line, err = r.ReadString('\n')
  91. if strings.Contains(line, pattern) == true {
  92. return
  93. }
  94. }
  95. t.Fatalf("grepFile: pattern \"%s\" not found in \"%s\"", pattern, path)
  96. }
  97. func TestEscapeFstabSpaces(t *testing.T) {
  98. var testInputs = map[string]string{
  99. " ": "\\040",
  100. "": "",
  101. "/double space": "/double\\040\\040space",
  102. "/some long test string": "/some\\040long\\040test\\040string",
  103. "/var/lib/docker": "/var/lib/docker",
  104. " leading": "\\040leading",
  105. "trailing ": "trailing\\040",
  106. }
  107. for in, exp := range testInputs {
  108. if out := escapeFstabSpaces(in); exp != out {
  109. t.Logf("Expected %s got %s", exp, out)
  110. t.Fail()
  111. }
  112. }
  113. }