container_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package container // import "github.com/docker/docker/daemon/cluster/executor/container"
  2. import (
  3. "testing"
  4. "github.com/docker/docker/api/types/container"
  5. swarmapi "github.com/moby/swarmkit/v2/api"
  6. "gotest.tools/v3/assert"
  7. )
  8. func TestIsolationConversion(t *testing.T) {
  9. cases := []struct {
  10. name string
  11. from swarmapi.ContainerSpec_Isolation
  12. to container.Isolation
  13. }{
  14. {name: "default", from: swarmapi.ContainerIsolationDefault, to: container.IsolationDefault},
  15. {name: "process", from: swarmapi.ContainerIsolationProcess, to: container.IsolationProcess},
  16. {name: "hyperv", from: swarmapi.ContainerIsolationHyperV, to: container.IsolationHyperV},
  17. }
  18. for _, c := range cases {
  19. t.Run(c.name, func(t *testing.T) {
  20. task := swarmapi.Task{
  21. Spec: swarmapi.TaskSpec{
  22. Runtime: &swarmapi.TaskSpec_Container{
  23. Container: &swarmapi.ContainerSpec{
  24. Image: "alpine:latest",
  25. Isolation: c.from,
  26. },
  27. },
  28. },
  29. }
  30. config := containerConfig{task: &task}
  31. // NOTE(dperny): you shouldn't ever pass nil outside of testing,
  32. // because if there are CSI volumes, the code will panic. However,
  33. // in testing. this is acceptable.
  34. assert.Equal(t, c.to, config.hostConfig(nil).Isolation)
  35. })
  36. }
  37. }
  38. func TestContainerLabels(t *testing.T) {
  39. c := &containerConfig{
  40. task: &swarmapi.Task{
  41. ID: "real-task.id",
  42. Spec: swarmapi.TaskSpec{
  43. Runtime: &swarmapi.TaskSpec_Container{
  44. Container: &swarmapi.ContainerSpec{
  45. Labels: map[string]string{
  46. "com.docker.swarm.task": "user-specified-task",
  47. "com.docker.swarm.task.id": "user-specified-task.id",
  48. "com.docker.swarm.task.name": "user-specified-task.name",
  49. "com.docker.swarm.node.id": "user-specified-node.id",
  50. "com.docker.swarm.service.id": "user-specified-service.id",
  51. "com.docker.swarm.service.name": "user-specified-service.name",
  52. "this-is-a-user-label": "this is a user label's value",
  53. },
  54. },
  55. },
  56. },
  57. ServiceID: "real-service.id",
  58. Slot: 123,
  59. NodeID: "real-node.id",
  60. Annotations: swarmapi.Annotations{
  61. Name: "real-service.name.123.real-task.id",
  62. },
  63. ServiceAnnotations: swarmapi.Annotations{
  64. Name: "real-service.name",
  65. },
  66. },
  67. }
  68. expected := map[string]string{
  69. "com.docker.swarm.task": "",
  70. "com.docker.swarm.task.id": "real-task.id",
  71. "com.docker.swarm.task.name": "real-service.name.123.real-task.id",
  72. "com.docker.swarm.node.id": "real-node.id",
  73. "com.docker.swarm.service.id": "real-service.id",
  74. "com.docker.swarm.service.name": "real-service.name",
  75. "this-is-a-user-label": "this is a user label's value",
  76. }
  77. labels := c.labels()
  78. assert.DeepEqual(t, expected, labels)
  79. }
  80. func TestCredentialSpecConversion(t *testing.T) {
  81. cases := []struct {
  82. name string
  83. from swarmapi.Privileges_CredentialSpec
  84. to []string
  85. }{
  86. {
  87. name: "none",
  88. from: swarmapi.Privileges_CredentialSpec{},
  89. to: nil,
  90. },
  91. {
  92. name: "config",
  93. from: swarmapi.Privileges_CredentialSpec{
  94. Source: &swarmapi.Privileges_CredentialSpec_Config{Config: "0bt9dmxjvjiqermk6xrop3ekq"},
  95. },
  96. to: []string{"credentialspec=config://0bt9dmxjvjiqermk6xrop3ekq"},
  97. },
  98. {
  99. name: "file",
  100. from: swarmapi.Privileges_CredentialSpec{
  101. Source: &swarmapi.Privileges_CredentialSpec_File{File: "foo.json"},
  102. },
  103. to: []string{"credentialspec=file://foo.json"},
  104. },
  105. {
  106. name: "registry",
  107. from: swarmapi.Privileges_CredentialSpec{
  108. Source: &swarmapi.Privileges_CredentialSpec_Registry{Registry: "testing"},
  109. },
  110. to: []string{"credentialspec=registry://testing"},
  111. },
  112. }
  113. for _, c := range cases {
  114. c := c
  115. t.Run(c.name, func(t *testing.T) {
  116. task := swarmapi.Task{
  117. Spec: swarmapi.TaskSpec{
  118. Runtime: &swarmapi.TaskSpec_Container{
  119. Container: &swarmapi.ContainerSpec{
  120. Privileges: &swarmapi.Privileges{
  121. CredentialSpec: &c.from,
  122. },
  123. },
  124. },
  125. },
  126. }
  127. config := containerConfig{task: &task}
  128. // NOTE(dperny): you shouldn't ever pass nil outside of testing,
  129. // because if there are CSI volumes, the code will panic. However,
  130. // in testing. this is acceptable.
  131. assert.DeepEqual(t, c.to, config.hostConfig(nil).SecurityOpt)
  132. })
  133. }
  134. }