service_test.go 3.2 KB

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