helpers_test.go 3.2 KB

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