lxc_template_unit_test.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. // +build linux
  2. package lxc
  3. import (
  4. "bufio"
  5. "fmt"
  6. "io/ioutil"
  7. "math/rand"
  8. "os"
  9. "path"
  10. "strings"
  11. "testing"
  12. "time"
  13. "github.com/docker/docker/daemon/execdriver"
  14. nativeTemplate "github.com/docker/docker/daemon/execdriver/native/template"
  15. "github.com/docker/libcontainer/configs"
  16. "github.com/syndtr/gocapability/capability"
  17. )
  18. func TestLXCConfig(t *testing.T) {
  19. root, err := ioutil.TempDir("", "TestLXCConfig")
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. defer os.RemoveAll(root)
  24. os.MkdirAll(path.Join(root, "containers", "1"), 0777)
  25. // Memory is allocated randomly for testing
  26. rand.Seed(time.Now().UTC().UnixNano())
  27. var (
  28. memMin = 33554432
  29. memMax = 536870912
  30. mem = memMin + rand.Intn(memMax-memMin)
  31. cpuMin = 100
  32. cpuMax = 10000
  33. cpu = cpuMin + rand.Intn(cpuMax-cpuMin)
  34. )
  35. driver, err := NewDriver(root, "", false)
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. command := &execdriver.Command{
  40. ID: "1",
  41. Resources: &execdriver.Resources{
  42. Memory: int64(mem),
  43. CpuShares: int64(cpu),
  44. },
  45. Network: &execdriver.Network{
  46. Mtu: 1500,
  47. Interface: nil,
  48. },
  49. AllowedDevices: make([]*configs.Device, 0),
  50. ProcessConfig: execdriver.ProcessConfig{},
  51. }
  52. p, err := driver.generateLXCConfig(command)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. grepFile(t, p,
  57. fmt.Sprintf("lxc.cgroup.memory.limit_in_bytes = %d", mem))
  58. grepFile(t, p,
  59. fmt.Sprintf("lxc.cgroup.memory.memsw.limit_in_bytes = %d", mem*2))
  60. }
  61. func TestCustomLxcConfig(t *testing.T) {
  62. root, err := ioutil.TempDir("", "TestCustomLxcConfig")
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. defer os.RemoveAll(root)
  67. os.MkdirAll(path.Join(root, "containers", "1"), 0777)
  68. driver, err := NewDriver(root, "", false)
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. processConfig := execdriver.ProcessConfig{
  73. Privileged: false,
  74. }
  75. command := &execdriver.Command{
  76. ID: "1",
  77. LxcConfig: []string{
  78. "lxc.utsname = docker",
  79. "lxc.cgroup.cpuset.cpus = 0,1",
  80. },
  81. Network: &execdriver.Network{
  82. Mtu: 1500,
  83. Interface: nil,
  84. },
  85. ProcessConfig: processConfig,
  86. }
  87. p, err := driver.generateLXCConfig(command)
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. grepFile(t, p, "lxc.utsname = docker")
  92. grepFile(t, p, "lxc.cgroup.cpuset.cpus = 0,1")
  93. }
  94. func grepFile(t *testing.T, path string, pattern string) {
  95. grepFileWithReverse(t, path, pattern, false)
  96. }
  97. func grepFileWithReverse(t *testing.T, path string, pattern string, inverseGrep bool) {
  98. f, err := os.Open(path)
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. defer f.Close()
  103. r := bufio.NewReader(f)
  104. var (
  105. line string
  106. )
  107. err = nil
  108. for err == nil {
  109. line, err = r.ReadString('\n')
  110. if strings.Contains(line, pattern) == true {
  111. if inverseGrep {
  112. t.Fatalf("grepFile: pattern \"%s\" found in \"%s\"", pattern, path)
  113. }
  114. return
  115. }
  116. }
  117. if inverseGrep {
  118. return
  119. }
  120. t.Fatalf("grepFile: pattern \"%s\" not found in \"%s\"", pattern, path)
  121. }
  122. func TestEscapeFstabSpaces(t *testing.T) {
  123. var testInputs = map[string]string{
  124. " ": "\\040",
  125. "": "",
  126. "/double space": "/double\\040\\040space",
  127. "/some long test string": "/some\\040long\\040test\\040string",
  128. "/var/lib/docker": "/var/lib/docker",
  129. " leading": "\\040leading",
  130. "trailing ": "trailing\\040",
  131. }
  132. for in, exp := range testInputs {
  133. if out := escapeFstabSpaces(in); exp != out {
  134. t.Logf("Expected %s got %s", exp, out)
  135. t.Fail()
  136. }
  137. }
  138. }
  139. func TestIsDirectory(t *testing.T) {
  140. tempDir, err := ioutil.TempDir("", "TestIsDir")
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. defer os.RemoveAll(tempDir)
  145. tempFile, err := ioutil.TempFile(tempDir, "TestIsDirFile")
  146. if err != nil {
  147. t.Fatal(err)
  148. }
  149. if isDirectory(tempDir) != "dir" {
  150. t.Logf("Could not identify %s as a directory", tempDir)
  151. t.Fail()
  152. }
  153. if isDirectory(tempFile.Name()) != "file" {
  154. t.Logf("Could not identify %s as a file", tempFile.Name())
  155. t.Fail()
  156. }
  157. }
  158. func TestCustomLxcConfigMounts(t *testing.T) {
  159. root, err := ioutil.TempDir("", "TestCustomLxcConfig")
  160. if err != nil {
  161. t.Fatal(err)
  162. }
  163. defer os.RemoveAll(root)
  164. tempDir, err := ioutil.TempDir("", "TestIsDir")
  165. if err != nil {
  166. t.Fatal(err)
  167. }
  168. defer os.RemoveAll(tempDir)
  169. tempFile, err := ioutil.TempFile(tempDir, "TestIsDirFile")
  170. if err != nil {
  171. t.Fatal(err)
  172. }
  173. os.MkdirAll(path.Join(root, "containers", "1"), 0777)
  174. driver, err := NewDriver(root, "", false)
  175. if err != nil {
  176. t.Fatal(err)
  177. }
  178. processConfig := execdriver.ProcessConfig{
  179. Privileged: false,
  180. }
  181. mounts := []execdriver.Mount{
  182. {
  183. Source: tempDir,
  184. Destination: tempDir,
  185. Writable: false,
  186. Private: true,
  187. },
  188. {
  189. Source: tempFile.Name(),
  190. Destination: tempFile.Name(),
  191. Writable: true,
  192. Private: true,
  193. },
  194. }
  195. command := &execdriver.Command{
  196. ID: "1",
  197. LxcConfig: []string{
  198. "lxc.utsname = docker",
  199. "lxc.cgroup.cpuset.cpus = 0,1",
  200. },
  201. Network: &execdriver.Network{
  202. Mtu: 1500,
  203. Interface: nil,
  204. },
  205. Mounts: mounts,
  206. ProcessConfig: processConfig,
  207. }
  208. p, err := driver.generateLXCConfig(command)
  209. if err != nil {
  210. t.Fatal(err)
  211. }
  212. grepFile(t, p, "lxc.utsname = docker")
  213. grepFile(t, p, "lxc.cgroup.cpuset.cpus = 0,1")
  214. grepFile(t, p, fmt.Sprintf("lxc.mount.entry = %s %s none rbind,ro,create=%s 0 0", tempDir, "/"+tempDir, "dir"))
  215. grepFile(t, p, fmt.Sprintf("lxc.mount.entry = %s %s none rbind,rw,create=%s 0 0", tempFile.Name(), "/"+tempFile.Name(), "file"))
  216. }
  217. func TestCustomLxcConfigMisc(t *testing.T) {
  218. root, err := ioutil.TempDir("", "TestCustomLxcConfig")
  219. if err != nil {
  220. t.Fatal(err)
  221. }
  222. defer os.RemoveAll(root)
  223. os.MkdirAll(path.Join(root, "containers", "1"), 0777)
  224. driver, err := NewDriver(root, "", true)
  225. if err != nil {
  226. t.Fatal(err)
  227. }
  228. processConfig := execdriver.ProcessConfig{
  229. Privileged: false,
  230. }
  231. processConfig.Env = []string{"HOSTNAME=testhost"}
  232. command := &execdriver.Command{
  233. ID: "1",
  234. LxcConfig: []string{
  235. "lxc.cgroup.cpuset.cpus = 0,1",
  236. },
  237. Network: &execdriver.Network{
  238. Mtu: 1500,
  239. Interface: &execdriver.NetworkInterface{
  240. Gateway: "10.10.10.1",
  241. IPAddress: "10.10.10.10",
  242. IPPrefixLen: 24,
  243. Bridge: "docker0",
  244. },
  245. },
  246. ProcessConfig: processConfig,
  247. CapAdd: []string{"net_admin", "syslog"},
  248. CapDrop: []string{"kill", "mknod"},
  249. AppArmorProfile: "lxc-container-default-with-nesting",
  250. }
  251. p, err := driver.generateLXCConfig(command)
  252. if err != nil {
  253. t.Fatal(err)
  254. }
  255. // network
  256. grepFile(t, p, "lxc.network.type = veth")
  257. grepFile(t, p, "lxc.network.link = docker0")
  258. grepFile(t, p, "lxc.network.name = eth0")
  259. grepFile(t, p, "lxc.network.ipv4 = 10.10.10.10/24")
  260. grepFile(t, p, "lxc.network.ipv4.gateway = 10.10.10.1")
  261. grepFile(t, p, "lxc.network.flags = up")
  262. grepFile(t, p, "lxc.aa_profile = lxc-container-default-with-nesting")
  263. // hostname
  264. grepFile(t, p, "lxc.utsname = testhost")
  265. grepFile(t, p, "lxc.cgroup.cpuset.cpus = 0,1")
  266. container := nativeTemplate.New()
  267. for _, cap := range container.Capabilities {
  268. realCap := execdriver.GetCapability(cap)
  269. numCap := fmt.Sprintf("%d", realCap.Value)
  270. if cap != "MKNOD" && cap != "KILL" {
  271. grepFile(t, p, fmt.Sprintf("lxc.cap.keep = %s", numCap))
  272. }
  273. }
  274. grepFileWithReverse(t, p, fmt.Sprintf("lxc.cap.keep = %d", capability.CAP_KILL), true)
  275. grepFileWithReverse(t, p, fmt.Sprintf("lxc.cap.keep = %d", capability.CAP_MKNOD), true)
  276. }
  277. func TestCustomLxcConfigMiscOverride(t *testing.T) {
  278. root, err := ioutil.TempDir("", "TestCustomLxcConfig")
  279. if err != nil {
  280. t.Fatal(err)
  281. }
  282. defer os.RemoveAll(root)
  283. os.MkdirAll(path.Join(root, "containers", "1"), 0777)
  284. driver, err := NewDriver(root, "", false)
  285. if err != nil {
  286. t.Fatal(err)
  287. }
  288. processConfig := execdriver.ProcessConfig{
  289. Privileged: false,
  290. }
  291. processConfig.Env = []string{"HOSTNAME=testhost"}
  292. command := &execdriver.Command{
  293. ID: "1",
  294. LxcConfig: []string{
  295. "lxc.cgroup.cpuset.cpus = 0,1",
  296. "lxc.network.ipv4 = 172.0.0.1",
  297. },
  298. Network: &execdriver.Network{
  299. Mtu: 1500,
  300. Interface: &execdriver.NetworkInterface{
  301. Gateway: "10.10.10.1",
  302. IPAddress: "10.10.10.10",
  303. IPPrefixLen: 24,
  304. Bridge: "docker0",
  305. },
  306. },
  307. ProcessConfig: processConfig,
  308. CapAdd: []string{"NET_ADMIN", "SYSLOG"},
  309. CapDrop: []string{"KILL", "MKNOD"},
  310. }
  311. p, err := driver.generateLXCConfig(command)
  312. if err != nil {
  313. t.Fatal(err)
  314. }
  315. // network
  316. grepFile(t, p, "lxc.network.type = veth")
  317. grepFile(t, p, "lxc.network.link = docker0")
  318. grepFile(t, p, "lxc.network.name = eth0")
  319. grepFile(t, p, "lxc.network.ipv4 = 172.0.0.1")
  320. grepFile(t, p, "lxc.network.ipv4.gateway = 10.10.10.1")
  321. grepFile(t, p, "lxc.network.flags = up")
  322. // hostname
  323. grepFile(t, p, "lxc.utsname = testhost")
  324. grepFile(t, p, "lxc.cgroup.cpuset.cpus = 0,1")
  325. container := nativeTemplate.New()
  326. for _, cap := range container.Capabilities {
  327. realCap := execdriver.GetCapability(cap)
  328. numCap := fmt.Sprintf("%d", realCap.Value)
  329. if cap != "MKNOD" && cap != "KILL" {
  330. grepFile(t, p, fmt.Sprintf("lxc.cap.keep = %s", numCap))
  331. }
  332. }
  333. grepFileWithReverse(t, p, fmt.Sprintf("lxc.cap.keep = %d", capability.CAP_KILL), true)
  334. grepFileWithReverse(t, p, fmt.Sprintf("lxc.cap.keep = %d", capability.CAP_MKNOD), true)
  335. }