lxc_template_unit_test.go 8.4 KB

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