container_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/docker/swarmkit/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. assert.Equal(t, c.to, config.hostConfig().Isolation)
  32. })
  33. }
  34. }
  35. func TestContainerLabels(t *testing.T) {
  36. c := &containerConfig{
  37. task: &swarmapi.Task{
  38. ID: "real-task.id",
  39. Spec: swarmapi.TaskSpec{
  40. Runtime: &swarmapi.TaskSpec_Container{
  41. Container: &swarmapi.ContainerSpec{
  42. Labels: map[string]string{
  43. "com.docker.swarm.task": "user-specified-task",
  44. "com.docker.swarm.task.id": "user-specified-task.id",
  45. "com.docker.swarm.task.name": "user-specified-task.name",
  46. "com.docker.swarm.node.id": "user-specified-node.id",
  47. "com.docker.swarm.service.id": "user-specified-service.id",
  48. "com.docker.swarm.service.name": "user-specified-service.name",
  49. "this-is-a-user-label": "this is a user label's value",
  50. },
  51. },
  52. },
  53. },
  54. ServiceID: "real-service.id",
  55. Slot: 123,
  56. NodeID: "real-node.id",
  57. Annotations: swarmapi.Annotations{
  58. Name: "real-service.name.123.real-task.id",
  59. },
  60. ServiceAnnotations: swarmapi.Annotations{
  61. Name: "real-service.name",
  62. },
  63. },
  64. }
  65. expected := map[string]string{
  66. "com.docker.swarm.task": "",
  67. "com.docker.swarm.task.id": "real-task.id",
  68. "com.docker.swarm.task.name": "real-service.name.123.real-task.id",
  69. "com.docker.swarm.node.id": "real-node.id",
  70. "com.docker.swarm.service.id": "real-service.id",
  71. "com.docker.swarm.service.name": "real-service.name",
  72. "this-is-a-user-label": "this is a user label's value",
  73. }
  74. labels := c.labels()
  75. assert.DeepEqual(t, expected, labels)
  76. }
  77. func TestCredentialSpecConversion(t *testing.T) {
  78. cases := []struct {
  79. name string
  80. from swarmapi.Privileges_CredentialSpec
  81. to []string
  82. }{
  83. {
  84. name: "none",
  85. from: swarmapi.Privileges_CredentialSpec{},
  86. to: nil,
  87. },
  88. {
  89. name: "config",
  90. from: swarmapi.Privileges_CredentialSpec{
  91. Source: &swarmapi.Privileges_CredentialSpec_Config{Config: "0bt9dmxjvjiqermk6xrop3ekq"},
  92. },
  93. to: []string{"credentialspec=config://0bt9dmxjvjiqermk6xrop3ekq"},
  94. },
  95. {
  96. name: "file",
  97. from: swarmapi.Privileges_CredentialSpec{
  98. Source: &swarmapi.Privileges_CredentialSpec_File{File: "foo.json"},
  99. },
  100. to: []string{"credentialspec=file://foo.json"},
  101. },
  102. {
  103. name: "registry",
  104. from: swarmapi.Privileges_CredentialSpec{
  105. Source: &swarmapi.Privileges_CredentialSpec_Registry{Registry: "testing"},
  106. },
  107. to: []string{"credentialspec=registry://testing"},
  108. },
  109. }
  110. for _, c := range cases {
  111. c := c
  112. t.Run(c.name, func(t *testing.T) {
  113. task := swarmapi.Task{
  114. Spec: swarmapi.TaskSpec{
  115. Runtime: &swarmapi.TaskSpec_Container{
  116. Container: &swarmapi.ContainerSpec{
  117. Privileges: &swarmapi.Privileges{
  118. CredentialSpec: &c.from,
  119. },
  120. },
  121. },
  122. },
  123. }
  124. config := containerConfig{task: &task}
  125. assert.DeepEqual(t, c.to, config.hostConfig().SecurityOpt)
  126. })
  127. }
  128. }