daemon_unix_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // +build !windows
  2. package daemon // import "github.com/docker/docker/daemon"
  3. import (
  4. "errors"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "testing"
  9. "github.com/docker/docker/api/types/blkiodev"
  10. containertypes "github.com/docker/docker/api/types/container"
  11. "github.com/docker/docker/container"
  12. "github.com/docker/docker/daemon/config"
  13. "golang.org/x/sys/unix"
  14. "gotest.tools/assert"
  15. is "gotest.tools/assert/cmp"
  16. )
  17. type fakeContainerGetter struct {
  18. containers map[string]*container.Container
  19. }
  20. func (f *fakeContainerGetter) GetContainer(cid string) (*container.Container, error) {
  21. container, ok := f.containers[cid]
  22. if !ok {
  23. return nil, errors.New("container not found")
  24. }
  25. return container, nil
  26. }
  27. // Unix test as uses settings which are not available on Windows
  28. func TestAdjustSharedNamespaceContainerName(t *testing.T) {
  29. fakeID := "abcdef1234567890"
  30. hostConfig := &containertypes.HostConfig{
  31. IpcMode: containertypes.IpcMode("container:base"),
  32. PidMode: containertypes.PidMode("container:base"),
  33. NetworkMode: containertypes.NetworkMode("container:base"),
  34. }
  35. containerStore := &fakeContainerGetter{}
  36. containerStore.containers = make(map[string]*container.Container)
  37. containerStore.containers["base"] = &container.Container{
  38. ID: fakeID,
  39. }
  40. adaptSharedNamespaceContainer(containerStore, hostConfig)
  41. if hostConfig.IpcMode != containertypes.IpcMode("container:"+fakeID) {
  42. t.Errorf("Expected IpcMode to be container:%s", fakeID)
  43. }
  44. if hostConfig.PidMode != containertypes.PidMode("container:"+fakeID) {
  45. t.Errorf("Expected PidMode to be container:%s", fakeID)
  46. }
  47. if hostConfig.NetworkMode != containertypes.NetworkMode("container:"+fakeID) {
  48. t.Errorf("Expected NetworkMode to be container:%s", fakeID)
  49. }
  50. }
  51. // Unix test as uses settings which are not available on Windows
  52. func TestAdjustCPUShares(t *testing.T) {
  53. tmp, err := ioutil.TempDir("", "docker-daemon-unix-test-")
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. defer os.RemoveAll(tmp)
  58. daemon := &Daemon{
  59. repository: tmp,
  60. root: tmp,
  61. }
  62. hostConfig := &containertypes.HostConfig{
  63. Resources: containertypes.Resources{CPUShares: linuxMinCPUShares - 1},
  64. }
  65. daemon.adaptContainerSettings(hostConfig, true)
  66. if hostConfig.CPUShares != linuxMinCPUShares {
  67. t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares)
  68. }
  69. hostConfig.CPUShares = linuxMaxCPUShares + 1
  70. daemon.adaptContainerSettings(hostConfig, true)
  71. if hostConfig.CPUShares != linuxMaxCPUShares {
  72. t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares)
  73. }
  74. hostConfig.CPUShares = 0
  75. daemon.adaptContainerSettings(hostConfig, true)
  76. if hostConfig.CPUShares != 0 {
  77. t.Error("Expected CPUShares to be unchanged")
  78. }
  79. hostConfig.CPUShares = 1024
  80. daemon.adaptContainerSettings(hostConfig, true)
  81. if hostConfig.CPUShares != 1024 {
  82. t.Error("Expected CPUShares to be unchanged")
  83. }
  84. }
  85. // Unix test as uses settings which are not available on Windows
  86. func TestAdjustCPUSharesNoAdjustment(t *testing.T) {
  87. tmp, err := ioutil.TempDir("", "docker-daemon-unix-test-")
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. defer os.RemoveAll(tmp)
  92. daemon := &Daemon{
  93. repository: tmp,
  94. root: tmp,
  95. }
  96. hostConfig := &containertypes.HostConfig{
  97. Resources: containertypes.Resources{CPUShares: linuxMinCPUShares - 1},
  98. }
  99. daemon.adaptContainerSettings(hostConfig, false)
  100. if hostConfig.CPUShares != linuxMinCPUShares-1 {
  101. t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares-1)
  102. }
  103. hostConfig.CPUShares = linuxMaxCPUShares + 1
  104. daemon.adaptContainerSettings(hostConfig, false)
  105. if hostConfig.CPUShares != linuxMaxCPUShares+1 {
  106. t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares+1)
  107. }
  108. hostConfig.CPUShares = 0
  109. daemon.adaptContainerSettings(hostConfig, false)
  110. if hostConfig.CPUShares != 0 {
  111. t.Error("Expected CPUShares to be unchanged")
  112. }
  113. hostConfig.CPUShares = 1024
  114. daemon.adaptContainerSettings(hostConfig, false)
  115. if hostConfig.CPUShares != 1024 {
  116. t.Error("Expected CPUShares to be unchanged")
  117. }
  118. }
  119. // Unix test as uses settings which are not available on Windows
  120. func TestParseSecurityOptWithDeprecatedColon(t *testing.T) {
  121. container := &container.Container{}
  122. config := &containertypes.HostConfig{}
  123. // test apparmor
  124. config.SecurityOpt = []string{"apparmor=test_profile"}
  125. if err := parseSecurityOpt(container, config); err != nil {
  126. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  127. }
  128. if container.AppArmorProfile != "test_profile" {
  129. t.Fatalf("Unexpected AppArmorProfile, expected: \"test_profile\", got %q", container.AppArmorProfile)
  130. }
  131. // test seccomp
  132. sp := "/path/to/seccomp_test.json"
  133. config.SecurityOpt = []string{"seccomp=" + sp}
  134. if err := parseSecurityOpt(container, config); err != nil {
  135. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  136. }
  137. if container.SeccompProfile != sp {
  138. t.Fatalf("Unexpected AppArmorProfile, expected: %q, got %q", sp, container.SeccompProfile)
  139. }
  140. // test valid label
  141. config.SecurityOpt = []string{"label=user:USER"}
  142. if err := parseSecurityOpt(container, config); err != nil {
  143. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  144. }
  145. // test invalid label
  146. config.SecurityOpt = []string{"label"}
  147. if err := parseSecurityOpt(container, config); err == nil {
  148. t.Fatal("Expected parseSecurityOpt error, got nil")
  149. }
  150. // test invalid opt
  151. config.SecurityOpt = []string{"test"}
  152. if err := parseSecurityOpt(container, config); err == nil {
  153. t.Fatal("Expected parseSecurityOpt error, got nil")
  154. }
  155. }
  156. func TestParseSecurityOpt(t *testing.T) {
  157. container := &container.Container{}
  158. config := &containertypes.HostConfig{}
  159. // test apparmor
  160. config.SecurityOpt = []string{"apparmor=test_profile"}
  161. if err := parseSecurityOpt(container, config); err != nil {
  162. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  163. }
  164. if container.AppArmorProfile != "test_profile" {
  165. t.Fatalf("Unexpected AppArmorProfile, expected: \"test_profile\", got %q", container.AppArmorProfile)
  166. }
  167. // test seccomp
  168. sp := "/path/to/seccomp_test.json"
  169. config.SecurityOpt = []string{"seccomp=" + sp}
  170. if err := parseSecurityOpt(container, config); err != nil {
  171. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  172. }
  173. if container.SeccompProfile != sp {
  174. t.Fatalf("Unexpected SeccompProfile, expected: %q, got %q", sp, container.SeccompProfile)
  175. }
  176. // test valid label
  177. config.SecurityOpt = []string{"label=user:USER"}
  178. if err := parseSecurityOpt(container, config); err != nil {
  179. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  180. }
  181. // test invalid label
  182. config.SecurityOpt = []string{"label"}
  183. if err := parseSecurityOpt(container, config); err == nil {
  184. t.Fatal("Expected parseSecurityOpt error, got nil")
  185. }
  186. // test invalid opt
  187. config.SecurityOpt = []string{"test"}
  188. if err := parseSecurityOpt(container, config); err == nil {
  189. t.Fatal("Expected parseSecurityOpt error, got nil")
  190. }
  191. }
  192. func TestParseNNPSecurityOptions(t *testing.T) {
  193. daemon := &Daemon{
  194. configStore: &config.Config{NoNewPrivileges: true},
  195. }
  196. container := &container.Container{}
  197. config := &containertypes.HostConfig{}
  198. // test NNP when "daemon:true" and "no-new-privileges=false""
  199. config.SecurityOpt = []string{"no-new-privileges=false"}
  200. if err := daemon.parseSecurityOpt(container, config); err != nil {
  201. t.Fatalf("Unexpected daemon.parseSecurityOpt error: %v", err)
  202. }
  203. if container.NoNewPrivileges {
  204. t.Fatalf("container.NoNewPrivileges should be FALSE: %v", container.NoNewPrivileges)
  205. }
  206. // test NNP when "daemon:false" and "no-new-privileges=true""
  207. daemon.configStore.NoNewPrivileges = false
  208. config.SecurityOpt = []string{"no-new-privileges=true"}
  209. if err := daemon.parseSecurityOpt(container, config); err != nil {
  210. t.Fatalf("Unexpected daemon.parseSecurityOpt error: %v", err)
  211. }
  212. if !container.NoNewPrivileges {
  213. t.Fatalf("container.NoNewPrivileges should be TRUE: %v", container.NoNewPrivileges)
  214. }
  215. }
  216. func TestNetworkOptions(t *testing.T) {
  217. daemon := &Daemon{}
  218. dconfigCorrect := &config.Config{
  219. CommonConfig: config.CommonConfig{
  220. ClusterStore: "consul://localhost:8500",
  221. ClusterAdvertise: "192.168.0.1:8000",
  222. },
  223. }
  224. if _, err := daemon.networkOptions(dconfigCorrect, nil, nil); err != nil {
  225. t.Fatalf("Expect networkOptions success, got error: %v", err)
  226. }
  227. dconfigWrong := &config.Config{
  228. CommonConfig: config.CommonConfig{
  229. ClusterStore: "consul://localhost:8500://test://bbb",
  230. },
  231. }
  232. if _, err := daemon.networkOptions(dconfigWrong, nil, nil); err == nil {
  233. t.Fatal("Expected networkOptions error, got nil")
  234. }
  235. }
  236. const (
  237. // prepare major 0x1FD(509 in decimal) and minor 0x130(304)
  238. DEVNO = 0x11FD30
  239. MAJOR = 509
  240. MINOR = 304
  241. WEIGHT = 1024
  242. )
  243. func deviceTypeMock(t *testing.T, testAndCheck func(string)) {
  244. if os.Getuid() != 0 {
  245. t.Skip("root required") // for mknod
  246. }
  247. t.Parallel()
  248. tempDir, err := ioutil.TempDir("", "tempDevDir"+t.Name())
  249. assert.NilError(t, err, "create temp file")
  250. tempFile := filepath.Join(tempDir, "dev")
  251. defer os.RemoveAll(tempDir)
  252. if err = unix.Mknod(tempFile, unix.S_IFCHR, DEVNO); err != nil {
  253. t.Fatalf("mknod error %s(%x): %v", tempFile, DEVNO, err)
  254. }
  255. testAndCheck(tempFile)
  256. }
  257. func TestGetBlkioWeightDevices(t *testing.T) {
  258. deviceTypeMock(t, func(tempFile string) {
  259. mockResource := containertypes.Resources{
  260. BlkioWeightDevice: []*blkiodev.WeightDevice{{Path: tempFile, Weight: WEIGHT}},
  261. }
  262. weightDevs, err := getBlkioWeightDevices(mockResource)
  263. assert.NilError(t, err, "getBlkioWeightDevices")
  264. assert.Check(t, is.Len(weightDevs, 1), "getBlkioWeightDevices")
  265. assert.Check(t, weightDevs[0].Major == MAJOR, "get major device type")
  266. assert.Check(t, weightDevs[0].Minor == MINOR, "get minor device type")
  267. assert.Check(t, *weightDevs[0].Weight == WEIGHT, "get device weight")
  268. })
  269. }
  270. func TestGetBlkioThrottleDevices(t *testing.T) {
  271. deviceTypeMock(t, func(tempFile string) {
  272. mockDevs := []*blkiodev.ThrottleDevice{{Path: tempFile, Rate: WEIGHT}}
  273. retDevs, err := getBlkioThrottleDevices(mockDevs)
  274. assert.NilError(t, err, "getBlkioThrottleDevices")
  275. assert.Check(t, is.Len(retDevs, 1), "getBlkioThrottleDevices")
  276. assert.Check(t, retDevs[0].Major == MAJOR, "get major device type")
  277. assert.Check(t, retDevs[0].Minor == MINOR, "get minor device type")
  278. assert.Check(t, retDevs[0].Rate == WEIGHT, "get device rate")
  279. })
  280. }