daemon_unix_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // +build !windows
  2. package daemon
  3. import (
  4. "errors"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "testing"
  9. containertypes "github.com/docker/docker/api/types/container"
  10. "github.com/docker/docker/container"
  11. "github.com/docker/docker/daemon/config"
  12. "github.com/docker/docker/pkg/idtools"
  13. "github.com/docker/docker/volume"
  14. "github.com/docker/docker/volume/drivers"
  15. "github.com/docker/docker/volume/local"
  16. "github.com/docker/docker/volume/store"
  17. "github.com/stretchr/testify/require"
  18. )
  19. type fakeContainerGetter struct {
  20. containers map[string]*container.Container
  21. }
  22. func (f *fakeContainerGetter) GetContainer(cid string) (*container.Container, error) {
  23. container, ok := f.containers[cid]
  24. if !ok {
  25. return nil, errors.New("container not found")
  26. }
  27. return container, nil
  28. }
  29. // Unix test as uses settings which are not available on Windows
  30. func TestAdjustSharedNamespaceContainerName(t *testing.T) {
  31. fakeID := "abcdef1234567890"
  32. hostConfig := &containertypes.HostConfig{
  33. IpcMode: containertypes.IpcMode("container:base"),
  34. PidMode: containertypes.PidMode("container:base"),
  35. NetworkMode: containertypes.NetworkMode("container:base"),
  36. }
  37. containerStore := &fakeContainerGetter{}
  38. containerStore.containers = make(map[string]*container.Container)
  39. containerStore.containers["base"] = &container.Container{
  40. ID: fakeID,
  41. }
  42. adaptSharedNamespaceContainer(containerStore, hostConfig)
  43. if hostConfig.IpcMode != containertypes.IpcMode("container:"+fakeID) {
  44. t.Errorf("Expected IpcMode to be container:%s", fakeID)
  45. }
  46. if hostConfig.PidMode != containertypes.PidMode("container:"+fakeID) {
  47. t.Errorf("Expected PidMode to be container:%s", fakeID)
  48. }
  49. if hostConfig.NetworkMode != containertypes.NetworkMode("container:"+fakeID) {
  50. t.Errorf("Expected NetworkMode to be container:%s", fakeID)
  51. }
  52. }
  53. // Unix test as uses settings which are not available on Windows
  54. func TestAdjustCPUShares(t *testing.T) {
  55. tmp, err := ioutil.TempDir("", "docker-daemon-unix-test-")
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. defer os.RemoveAll(tmp)
  60. daemon := &Daemon{
  61. repository: tmp,
  62. root: tmp,
  63. }
  64. hostConfig := &containertypes.HostConfig{
  65. Resources: containertypes.Resources{CPUShares: linuxMinCPUShares - 1},
  66. }
  67. daemon.adaptContainerSettings(hostConfig, true)
  68. if hostConfig.CPUShares != linuxMinCPUShares {
  69. t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares)
  70. }
  71. hostConfig.CPUShares = linuxMaxCPUShares + 1
  72. daemon.adaptContainerSettings(hostConfig, true)
  73. if hostConfig.CPUShares != linuxMaxCPUShares {
  74. t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares)
  75. }
  76. hostConfig.CPUShares = 0
  77. daemon.adaptContainerSettings(hostConfig, true)
  78. if hostConfig.CPUShares != 0 {
  79. t.Error("Expected CPUShares to be unchanged")
  80. }
  81. hostConfig.CPUShares = 1024
  82. daemon.adaptContainerSettings(hostConfig, true)
  83. if hostConfig.CPUShares != 1024 {
  84. t.Error("Expected CPUShares to be unchanged")
  85. }
  86. }
  87. // Unix test as uses settings which are not available on Windows
  88. func TestAdjustCPUSharesNoAdjustment(t *testing.T) {
  89. tmp, err := ioutil.TempDir("", "docker-daemon-unix-test-")
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. defer os.RemoveAll(tmp)
  94. daemon := &Daemon{
  95. repository: tmp,
  96. root: tmp,
  97. }
  98. hostConfig := &containertypes.HostConfig{
  99. Resources: containertypes.Resources{CPUShares: linuxMinCPUShares - 1},
  100. }
  101. daemon.adaptContainerSettings(hostConfig, false)
  102. if hostConfig.CPUShares != linuxMinCPUShares-1 {
  103. t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares-1)
  104. }
  105. hostConfig.CPUShares = linuxMaxCPUShares + 1
  106. daemon.adaptContainerSettings(hostConfig, false)
  107. if hostConfig.CPUShares != linuxMaxCPUShares+1 {
  108. t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares+1)
  109. }
  110. hostConfig.CPUShares = 0
  111. daemon.adaptContainerSettings(hostConfig, false)
  112. if hostConfig.CPUShares != 0 {
  113. t.Error("Expected CPUShares to be unchanged")
  114. }
  115. hostConfig.CPUShares = 1024
  116. daemon.adaptContainerSettings(hostConfig, false)
  117. if hostConfig.CPUShares != 1024 {
  118. t.Error("Expected CPUShares to be unchanged")
  119. }
  120. }
  121. // Unix test as uses settings which are not available on Windows
  122. func TestParseSecurityOptWithDeprecatedColon(t *testing.T) {
  123. container := &container.Container{}
  124. config := &containertypes.HostConfig{}
  125. // test apparmor
  126. config.SecurityOpt = []string{"apparmor=test_profile"}
  127. if err := parseSecurityOpt(container, config); err != nil {
  128. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  129. }
  130. if container.AppArmorProfile != "test_profile" {
  131. t.Fatalf("Unexpected AppArmorProfile, expected: \"test_profile\", got %q", container.AppArmorProfile)
  132. }
  133. // test seccomp
  134. sp := "/path/to/seccomp_test.json"
  135. config.SecurityOpt = []string{"seccomp=" + sp}
  136. if err := parseSecurityOpt(container, config); err != nil {
  137. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  138. }
  139. if container.SeccompProfile != sp {
  140. t.Fatalf("Unexpected AppArmorProfile, expected: %q, got %q", sp, container.SeccompProfile)
  141. }
  142. // test valid label
  143. config.SecurityOpt = []string{"label=user:USER"}
  144. if err := parseSecurityOpt(container, config); err != nil {
  145. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  146. }
  147. // test invalid label
  148. config.SecurityOpt = []string{"label"}
  149. if err := parseSecurityOpt(container, config); err == nil {
  150. t.Fatal("Expected parseSecurityOpt error, got nil")
  151. }
  152. // test invalid opt
  153. config.SecurityOpt = []string{"test"}
  154. if err := parseSecurityOpt(container, config); err == nil {
  155. t.Fatal("Expected parseSecurityOpt error, got nil")
  156. }
  157. }
  158. func TestParseSecurityOpt(t *testing.T) {
  159. container := &container.Container{}
  160. config := &containertypes.HostConfig{}
  161. // test apparmor
  162. config.SecurityOpt = []string{"apparmor=test_profile"}
  163. if err := parseSecurityOpt(container, config); err != nil {
  164. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  165. }
  166. if container.AppArmorProfile != "test_profile" {
  167. t.Fatalf("Unexpected AppArmorProfile, expected: \"test_profile\", got %q", container.AppArmorProfile)
  168. }
  169. // test seccomp
  170. sp := "/path/to/seccomp_test.json"
  171. config.SecurityOpt = []string{"seccomp=" + sp}
  172. if err := parseSecurityOpt(container, config); err != nil {
  173. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  174. }
  175. if container.SeccompProfile != sp {
  176. t.Fatalf("Unexpected SeccompProfile, expected: %q, got %q", sp, container.SeccompProfile)
  177. }
  178. // test valid label
  179. config.SecurityOpt = []string{"label=user:USER"}
  180. if err := parseSecurityOpt(container, config); err != nil {
  181. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  182. }
  183. // test invalid label
  184. config.SecurityOpt = []string{"label"}
  185. if err := parseSecurityOpt(container, config); err == nil {
  186. t.Fatal("Expected parseSecurityOpt error, got nil")
  187. }
  188. // test invalid opt
  189. config.SecurityOpt = []string{"test"}
  190. if err := parseSecurityOpt(container, config); err == nil {
  191. t.Fatal("Expected parseSecurityOpt error, got nil")
  192. }
  193. }
  194. func TestParseNNPSecurityOptions(t *testing.T) {
  195. daemon := &Daemon{
  196. configStore: &config.Config{NoNewPrivileges: true},
  197. }
  198. container := &container.Container{}
  199. config := &containertypes.HostConfig{}
  200. // test NNP when "daemon:true" and "no-new-privileges=false""
  201. config.SecurityOpt = []string{"no-new-privileges=false"}
  202. if err := daemon.parseSecurityOpt(container, config); err != nil {
  203. t.Fatalf("Unexpected daemon.parseSecurityOpt error: %v", err)
  204. }
  205. if container.NoNewPrivileges {
  206. t.Fatalf("container.NoNewPrivileges should be FALSE: %v", container.NoNewPrivileges)
  207. }
  208. // test NNP when "daemon:false" and "no-new-privileges=true""
  209. daemon.configStore.NoNewPrivileges = false
  210. config.SecurityOpt = []string{"no-new-privileges=true"}
  211. if err := daemon.parseSecurityOpt(container, config); err != nil {
  212. t.Fatalf("Unexpected daemon.parseSecurityOpt error: %v", err)
  213. }
  214. if !container.NoNewPrivileges {
  215. t.Fatalf("container.NoNewPrivileges should be TRUE: %v", container.NoNewPrivileges)
  216. }
  217. }
  218. func TestNetworkOptions(t *testing.T) {
  219. daemon := &Daemon{}
  220. dconfigCorrect := &config.Config{
  221. CommonConfig: config.CommonConfig{
  222. ClusterStore: "consul://localhost:8500",
  223. ClusterAdvertise: "192.168.0.1:8000",
  224. },
  225. }
  226. if _, err := daemon.networkOptions(dconfigCorrect, nil, nil); err != nil {
  227. t.Fatalf("Expect networkOptions success, got error: %v", err)
  228. }
  229. dconfigWrong := &config.Config{
  230. CommonConfig: config.CommonConfig{
  231. ClusterStore: "consul://localhost:8500://test://bbb",
  232. },
  233. }
  234. if _, err := daemon.networkOptions(dconfigWrong, nil, nil); err == nil {
  235. t.Fatal("Expected networkOptions error, got nil")
  236. }
  237. }
  238. func TestMigratePre17Volumes(t *testing.T) {
  239. rootDir, err := ioutil.TempDir("", "test-daemon-volumes")
  240. if err != nil {
  241. t.Fatal(err)
  242. }
  243. defer os.RemoveAll(rootDir)
  244. volumeRoot := filepath.Join(rootDir, "volumes")
  245. err = os.MkdirAll(volumeRoot, 0755)
  246. if err != nil {
  247. t.Fatal(err)
  248. }
  249. containerRoot := filepath.Join(rootDir, "containers")
  250. cid := "1234"
  251. err = os.MkdirAll(filepath.Join(containerRoot, cid), 0755)
  252. require.NoError(t, err)
  253. vid := "5678"
  254. vfsPath := filepath.Join(rootDir, "vfs", "dir", vid)
  255. err = os.MkdirAll(vfsPath, 0755)
  256. require.NoError(t, err)
  257. config := []byte(`
  258. {
  259. "ID": "` + cid + `",
  260. "Volumes": {
  261. "/foo": "` + vfsPath + `",
  262. "/bar": "/foo",
  263. "/quux": "/quux"
  264. },
  265. "VolumesRW": {
  266. "/foo": true,
  267. "/bar": true,
  268. "/quux": false
  269. }
  270. }
  271. `)
  272. volStore, err := store.New(volumeRoot)
  273. if err != nil {
  274. t.Fatal(err)
  275. }
  276. drv, err := local.New(volumeRoot, idtools.IDPair{UID: 0, GID: 0})
  277. if err != nil {
  278. t.Fatal(err)
  279. }
  280. volumedrivers.Register(drv, volume.DefaultDriverName)
  281. daemon := &Daemon{
  282. root: rootDir,
  283. repository: containerRoot,
  284. volumes: volStore,
  285. }
  286. err = ioutil.WriteFile(filepath.Join(containerRoot, cid, "config.v2.json"), config, 600)
  287. if err != nil {
  288. t.Fatal(err)
  289. }
  290. c, err := daemon.load(cid)
  291. if err != nil {
  292. t.Fatal(err)
  293. }
  294. if err := daemon.verifyVolumesInfo(c); err != nil {
  295. t.Fatal(err)
  296. }
  297. expected := map[string]volume.MountPoint{
  298. "/foo": {Destination: "/foo", RW: true, Name: vid},
  299. "/bar": {Source: "/foo", Destination: "/bar", RW: true},
  300. "/quux": {Source: "/quux", Destination: "/quux", RW: false},
  301. }
  302. for id, mp := range c.MountPoints {
  303. x, exists := expected[id]
  304. if !exists {
  305. t.Fatal("volume not migrated")
  306. }
  307. if mp.Source != x.Source || mp.Destination != x.Destination || mp.RW != x.RW || mp.Name != x.Name {
  308. t.Fatalf("got unexpected mountpoint, expected: %+v, got: %+v", x, mp)
  309. }
  310. }
  311. }