parse_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package configuration
  2. import (
  3. "testing"
  4. "github.com/dotcloud/docker/daemon/execdriver/native/template"
  5. )
  6. func TestSetReadonlyRootFs(t *testing.T) {
  7. var (
  8. container = template.New()
  9. opts = []string{
  10. "fs.readonly=true",
  11. }
  12. )
  13. if container.ReadonlyFs {
  14. t.Fatal("container should not have a readonly rootfs by default")
  15. }
  16. if err := ParseConfiguration(container, nil, opts); err != nil {
  17. t.Fatal(err)
  18. }
  19. if !container.ReadonlyFs {
  20. t.Fatal("container should have a readonly rootfs")
  21. }
  22. }
  23. func TestConfigurationsDoNotConflict(t *testing.T) {
  24. var (
  25. container1 = template.New()
  26. container2 = template.New()
  27. opts = []string{
  28. "cap.add=NET_ADMIN",
  29. }
  30. )
  31. if err := ParseConfiguration(container1, nil, opts); err != nil {
  32. t.Fatal(err)
  33. }
  34. if !container1.CapabilitiesMask["NET_ADMIN"] {
  35. t.Fatal("container one should have NET_ADMIN enabled")
  36. }
  37. if container2.CapabilitiesMask["NET_ADMIN"] {
  38. t.Fatal("container two should not have NET_ADMIN enabled")
  39. }
  40. }
  41. func TestCpusetCpus(t *testing.T) {
  42. var (
  43. container = template.New()
  44. opts = []string{
  45. "cgroups.cpuset.cpus=1,2",
  46. }
  47. )
  48. if err := ParseConfiguration(container, nil, opts); err != nil {
  49. t.Fatal(err)
  50. }
  51. if expected := "1,2"; container.Cgroups.CpusetCpus != expected {
  52. t.Fatalf("expected %s got %s for cpuset.cpus", expected, container.Cgroups.CpusetCpus)
  53. }
  54. }
  55. func TestAppArmorProfile(t *testing.T) {
  56. var (
  57. container = template.New()
  58. opts = []string{
  59. "apparmor_profile=koye-the-protector",
  60. }
  61. )
  62. if err := ParseConfiguration(container, nil, opts); err != nil {
  63. t.Fatal(err)
  64. }
  65. if expected := "koye-the-protector"; container.Context["apparmor_profile"] != expected {
  66. t.Fatalf("expected profile %s got %s", expected, container.Context["apparmor_profile"])
  67. }
  68. }
  69. func TestCpuShares(t *testing.T) {
  70. var (
  71. container = template.New()
  72. opts = []string{
  73. "cgroups.cpu_shares=1048",
  74. }
  75. )
  76. if err := ParseConfiguration(container, nil, opts); err != nil {
  77. t.Fatal(err)
  78. }
  79. if expected := int64(1048); container.Cgroups.CpuShares != expected {
  80. t.Fatalf("expected cpu shares %d got %d", expected, container.Cgroups.CpuShares)
  81. }
  82. }
  83. func TestMemory(t *testing.T) {
  84. var (
  85. container = template.New()
  86. opts = []string{
  87. "cgroups.memory=500m",
  88. }
  89. )
  90. if err := ParseConfiguration(container, nil, opts); err != nil {
  91. t.Fatal(err)
  92. }
  93. if expected := int64(500 * 1024 * 1024); container.Cgroups.Memory != expected {
  94. t.Fatalf("expected memory %d got %d", expected, container.Cgroups.Memory)
  95. }
  96. }
  97. func TestMemoryReservation(t *testing.T) {
  98. var (
  99. container = template.New()
  100. opts = []string{
  101. "cgroups.memory_reservation=500m",
  102. }
  103. )
  104. if err := ParseConfiguration(container, nil, opts); err != nil {
  105. t.Fatal(err)
  106. }
  107. if expected := int64(500 * 1024 * 1024); container.Cgroups.MemoryReservation != expected {
  108. t.Fatalf("expected memory reservation %d got %d", expected, container.Cgroups.MemoryReservation)
  109. }
  110. }
  111. func TestAddCap(t *testing.T) {
  112. var (
  113. container = template.New()
  114. opts = []string{
  115. "cap.add=MKNOD",
  116. "cap.add=SYS_ADMIN",
  117. }
  118. )
  119. if err := ParseConfiguration(container, nil, opts); err != nil {
  120. t.Fatal(err)
  121. }
  122. if !container.CapabilitiesMask["MKNOD"] {
  123. t.Fatal("container should have MKNOD enabled")
  124. }
  125. if !container.CapabilitiesMask["SYS_ADMIN"] {
  126. t.Fatal("container should have SYS_ADMIN enabled")
  127. }
  128. }
  129. func TestDropCap(t *testing.T) {
  130. var (
  131. container = template.New()
  132. opts = []string{
  133. "cap.drop=MKNOD",
  134. }
  135. )
  136. // enabled all caps like in privileged mode
  137. for key := range container.CapabilitiesMask {
  138. container.CapabilitiesMask[key] = true
  139. }
  140. if err := ParseConfiguration(container, nil, opts); err != nil {
  141. t.Fatal(err)
  142. }
  143. if container.CapabilitiesMask["MKNOD"] {
  144. t.Fatal("container should not have MKNOD enabled")
  145. }
  146. }
  147. func TestDropNamespace(t *testing.T) {
  148. var (
  149. container = template.New()
  150. opts = []string{
  151. "ns.drop=NEWNET",
  152. }
  153. )
  154. if err := ParseConfiguration(container, nil, opts); err != nil {
  155. t.Fatal(err)
  156. }
  157. if container.Namespaces["NEWNET"] {
  158. t.Fatal("container should not have NEWNET enabled")
  159. }
  160. }