parse_test.go 4.4 KB

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