service_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package convert
  2. import (
  3. "testing"
  4. swarmtypes "github.com/docker/docker/api/types/swarm"
  5. "github.com/docker/docker/api/types/swarm/runtime"
  6. swarmapi "github.com/docker/swarmkit/api"
  7. google_protobuf3 "github.com/gogo/protobuf/types"
  8. )
  9. func TestServiceConvertFromGRPCRuntimeContainer(t *testing.T) {
  10. gs := swarmapi.Service{
  11. Meta: swarmapi.Meta{
  12. Version: swarmapi.Version{
  13. Index: 1,
  14. },
  15. CreatedAt: nil,
  16. UpdatedAt: nil,
  17. },
  18. SpecVersion: &swarmapi.Version{
  19. Index: 1,
  20. },
  21. Spec: swarmapi.ServiceSpec{
  22. Task: swarmapi.TaskSpec{
  23. Runtime: &swarmapi.TaskSpec_Container{
  24. Container: &swarmapi.ContainerSpec{
  25. Image: "alpine:latest",
  26. },
  27. },
  28. },
  29. },
  30. }
  31. svc, err := ServiceFromGRPC(gs)
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. if svc.Spec.TaskTemplate.Runtime != swarmtypes.RuntimeContainer {
  36. t.Fatalf("expected type %s; received %T", swarmtypes.RuntimeContainer, svc.Spec.TaskTemplate.Runtime)
  37. }
  38. }
  39. func TestServiceConvertFromGRPCGenericRuntimePlugin(t *testing.T) {
  40. kind := string(swarmtypes.RuntimePlugin)
  41. url := swarmtypes.RuntimeURLPlugin
  42. gs := swarmapi.Service{
  43. Meta: swarmapi.Meta{
  44. Version: swarmapi.Version{
  45. Index: 1,
  46. },
  47. CreatedAt: nil,
  48. UpdatedAt: nil,
  49. },
  50. SpecVersion: &swarmapi.Version{
  51. Index: 1,
  52. },
  53. Spec: swarmapi.ServiceSpec{
  54. Task: swarmapi.TaskSpec{
  55. Runtime: &swarmapi.TaskSpec_Generic{
  56. Generic: &swarmapi.GenericRuntimeSpec{
  57. Kind: kind,
  58. Payload: &google_protobuf3.Any{
  59. TypeUrl: string(url),
  60. },
  61. },
  62. },
  63. },
  64. },
  65. }
  66. svc, err := ServiceFromGRPC(gs)
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. if svc.Spec.TaskTemplate.Runtime != swarmtypes.RuntimePlugin {
  71. t.Fatalf("expected type %s; received %T", swarmtypes.RuntimePlugin, svc.Spec.TaskTemplate.Runtime)
  72. }
  73. }
  74. func TestServiceConvertToGRPCGenericRuntimePlugin(t *testing.T) {
  75. s := swarmtypes.ServiceSpec{
  76. TaskTemplate: swarmtypes.TaskSpec{
  77. Runtime: swarmtypes.RuntimePlugin,
  78. PluginSpec: &runtime.PluginSpec{},
  79. },
  80. Mode: swarmtypes.ServiceMode{
  81. Global: &swarmtypes.GlobalService{},
  82. },
  83. }
  84. svc, err := ServiceSpecToGRPC(s)
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. v, ok := svc.Task.Runtime.(*swarmapi.TaskSpec_Generic)
  89. if !ok {
  90. t.Fatal("expected type swarmapi.TaskSpec_Generic")
  91. }
  92. if v.Generic.Payload.TypeUrl != string(swarmtypes.RuntimeURLPlugin) {
  93. t.Fatalf("expected url %s; received %s", swarmtypes.RuntimeURLPlugin, v.Generic.Payload.TypeUrl)
  94. }
  95. }
  96. func TestServiceConvertToGRPCContainerRuntime(t *testing.T) {
  97. image := "alpine:latest"
  98. s := swarmtypes.ServiceSpec{
  99. TaskTemplate: swarmtypes.TaskSpec{
  100. ContainerSpec: &swarmtypes.ContainerSpec{
  101. Image: image,
  102. },
  103. },
  104. Mode: swarmtypes.ServiceMode{
  105. Global: &swarmtypes.GlobalService{},
  106. },
  107. }
  108. svc, err := ServiceSpecToGRPC(s)
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. v, ok := svc.Task.Runtime.(*swarmapi.TaskSpec_Container)
  113. if !ok {
  114. t.Fatal("expected type swarmapi.TaskSpec_Container")
  115. }
  116. if v.Container.Image != image {
  117. t.Fatalf("expected image %s; received %s", image, v.Container.Image)
  118. }
  119. }
  120. func TestServiceConvertToGRPCGenericRuntimeCustom(t *testing.T) {
  121. s := swarmtypes.ServiceSpec{
  122. TaskTemplate: swarmtypes.TaskSpec{
  123. Runtime: "customruntime",
  124. },
  125. Mode: swarmtypes.ServiceMode{
  126. Global: &swarmtypes.GlobalService{},
  127. },
  128. }
  129. if _, err := ServiceSpecToGRPC(s); err != ErrUnsupportedRuntime {
  130. t.Fatal(err)
  131. }
  132. }