helpers_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package swarm // import "github.com/docker/docker/api/server/router/swarm"
  2. import (
  3. "reflect"
  4. "testing"
  5. "github.com/docker/docker/api/types/swarm"
  6. "github.com/docker/go-units"
  7. )
  8. func TestAdjustForAPIVersion(t *testing.T) {
  9. var (
  10. expectedSysctls = map[string]string{"foo": "bar"}
  11. )
  12. // testing the negative -- does this leave everything else alone? -- is
  13. // prohibitively time-consuming to write, because it would need an object
  14. // with literally every field filled in.
  15. spec := &swarm.ServiceSpec{
  16. TaskTemplate: swarm.TaskSpec{
  17. ContainerSpec: &swarm.ContainerSpec{
  18. Sysctls: expectedSysctls,
  19. Privileges: &swarm.Privileges{
  20. CredentialSpec: &swarm.CredentialSpec{
  21. Config: "someconfig",
  22. },
  23. },
  24. Configs: []*swarm.ConfigReference{
  25. {
  26. File: &swarm.ConfigReferenceFileTarget{
  27. Name: "foo",
  28. UID: "bar",
  29. GID: "baz",
  30. },
  31. ConfigID: "configFile",
  32. ConfigName: "configFile",
  33. },
  34. {
  35. Runtime: &swarm.ConfigReferenceRuntimeTarget{},
  36. ConfigID: "configRuntime",
  37. ConfigName: "configRuntime",
  38. },
  39. },
  40. Ulimits: []*units.Ulimit{
  41. {
  42. Name: "nofile",
  43. Soft: 100,
  44. Hard: 200,
  45. },
  46. },
  47. },
  48. Placement: &swarm.Placement{
  49. MaxReplicas: 222,
  50. },
  51. Resources: &swarm.ResourceRequirements{
  52. Limits: &swarm.Limit{
  53. Pids: 300,
  54. },
  55. },
  56. },
  57. }
  58. // first, does calling this with a later version correctly NOT strip
  59. // fields? do the later version first, so we can reuse this spec in the
  60. // next test.
  61. adjustForAPIVersion("1.41", spec)
  62. if !reflect.DeepEqual(spec.TaskTemplate.ContainerSpec.Sysctls, expectedSysctls) {
  63. t.Error("Sysctls was stripped from spec")
  64. }
  65. if spec.TaskTemplate.Resources.Limits.Pids == 0 {
  66. t.Error("PidsLimit was stripped from spec")
  67. }
  68. if spec.TaskTemplate.Resources.Limits.Pids != 300 {
  69. t.Error("PidsLimit did not preserve the value from spec")
  70. }
  71. if spec.TaskTemplate.ContainerSpec.Privileges.CredentialSpec.Config != "someconfig" {
  72. t.Error("CredentialSpec.Config field was stripped from spec")
  73. }
  74. if spec.TaskTemplate.ContainerSpec.Configs[1].Runtime == nil {
  75. t.Error("ConfigReferenceRuntimeTarget was stripped from spec")
  76. }
  77. if spec.TaskTemplate.Placement.MaxReplicas != 222 {
  78. t.Error("MaxReplicas was stripped from spec")
  79. }
  80. if len(spec.TaskTemplate.ContainerSpec.Ulimits) == 0 {
  81. t.Error("Ulimits were stripped from spec")
  82. }
  83. // next, does calling this with an earlier version correctly strip fields?
  84. adjustForAPIVersion("1.29", spec)
  85. if spec.TaskTemplate.ContainerSpec.Sysctls != nil {
  86. t.Error("Sysctls was not stripped from spec")
  87. }
  88. if spec.TaskTemplate.Resources.Limits.Pids != 0 {
  89. t.Error("PidsLimit was not stripped from spec")
  90. }
  91. if spec.TaskTemplate.ContainerSpec.Privileges.CredentialSpec.Config != "" {
  92. t.Error("CredentialSpec.Config field was not stripped from spec")
  93. }
  94. if spec.TaskTemplate.ContainerSpec.Configs[1].Runtime != nil {
  95. t.Error("ConfigReferenceRuntimeTarget was not stripped from spec")
  96. }
  97. if spec.TaskTemplate.Placement.MaxReplicas != 0 {
  98. t.Error("MaxReplicas was not stripped from spec")
  99. }
  100. if len(spec.TaskTemplate.ContainerSpec.Ulimits) != 0 {
  101. t.Error("Ulimits were not stripped from spec")
  102. }
  103. }