service_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package convert
  2. import (
  3. "testing"
  4. containertypes "github.com/docker/docker/api/types/container"
  5. swarmtypes "github.com/docker/docker/api/types/swarm"
  6. "github.com/docker/docker/api/types/swarm/runtime"
  7. swarmapi "github.com/docker/swarmkit/api"
  8. google_protobuf3 "github.com/gogo/protobuf/types"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestServiceConvertFromGRPCRuntimeContainer(t *testing.T) {
  12. gs := swarmapi.Service{
  13. Meta: swarmapi.Meta{
  14. Version: swarmapi.Version{
  15. Index: 1,
  16. },
  17. CreatedAt: nil,
  18. UpdatedAt: nil,
  19. },
  20. SpecVersion: &swarmapi.Version{
  21. Index: 1,
  22. },
  23. Spec: swarmapi.ServiceSpec{
  24. Task: swarmapi.TaskSpec{
  25. Runtime: &swarmapi.TaskSpec_Container{
  26. Container: &swarmapi.ContainerSpec{
  27. Image: "alpine:latest",
  28. },
  29. },
  30. },
  31. },
  32. }
  33. svc, err := ServiceFromGRPC(gs)
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. if svc.Spec.TaskTemplate.Runtime != swarmtypes.RuntimeContainer {
  38. t.Fatalf("expected type %s; received %T", swarmtypes.RuntimeContainer, svc.Spec.TaskTemplate.Runtime)
  39. }
  40. }
  41. func TestServiceConvertFromGRPCGenericRuntimePlugin(t *testing.T) {
  42. kind := string(swarmtypes.RuntimePlugin)
  43. url := swarmtypes.RuntimeURLPlugin
  44. gs := swarmapi.Service{
  45. Meta: swarmapi.Meta{
  46. Version: swarmapi.Version{
  47. Index: 1,
  48. },
  49. CreatedAt: nil,
  50. UpdatedAt: nil,
  51. },
  52. SpecVersion: &swarmapi.Version{
  53. Index: 1,
  54. },
  55. Spec: swarmapi.ServiceSpec{
  56. Task: swarmapi.TaskSpec{
  57. Runtime: &swarmapi.TaskSpec_Generic{
  58. Generic: &swarmapi.GenericRuntimeSpec{
  59. Kind: kind,
  60. Payload: &google_protobuf3.Any{
  61. TypeUrl: string(url),
  62. },
  63. },
  64. },
  65. },
  66. },
  67. }
  68. svc, err := ServiceFromGRPC(gs)
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. if svc.Spec.TaskTemplate.Runtime != swarmtypes.RuntimePlugin {
  73. t.Fatalf("expected type %s; received %T", swarmtypes.RuntimePlugin, svc.Spec.TaskTemplate.Runtime)
  74. }
  75. }
  76. func TestServiceConvertToGRPCGenericRuntimePlugin(t *testing.T) {
  77. s := swarmtypes.ServiceSpec{
  78. TaskTemplate: swarmtypes.TaskSpec{
  79. Runtime: swarmtypes.RuntimePlugin,
  80. PluginSpec: &runtime.PluginSpec{},
  81. },
  82. Mode: swarmtypes.ServiceMode{
  83. Global: &swarmtypes.GlobalService{},
  84. },
  85. }
  86. svc, err := ServiceSpecToGRPC(s)
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. v, ok := svc.Task.Runtime.(*swarmapi.TaskSpec_Generic)
  91. if !ok {
  92. t.Fatal("expected type swarmapi.TaskSpec_Generic")
  93. }
  94. if v.Generic.Payload.TypeUrl != string(swarmtypes.RuntimeURLPlugin) {
  95. t.Fatalf("expected url %s; received %s", swarmtypes.RuntimeURLPlugin, v.Generic.Payload.TypeUrl)
  96. }
  97. }
  98. func TestServiceConvertToGRPCContainerRuntime(t *testing.T) {
  99. image := "alpine:latest"
  100. s := swarmtypes.ServiceSpec{
  101. TaskTemplate: swarmtypes.TaskSpec{
  102. ContainerSpec: &swarmtypes.ContainerSpec{
  103. Image: image,
  104. },
  105. },
  106. Mode: swarmtypes.ServiceMode{
  107. Global: &swarmtypes.GlobalService{},
  108. },
  109. }
  110. svc, err := ServiceSpecToGRPC(s)
  111. if err != nil {
  112. t.Fatal(err)
  113. }
  114. v, ok := svc.Task.Runtime.(*swarmapi.TaskSpec_Container)
  115. if !ok {
  116. t.Fatal("expected type swarmapi.TaskSpec_Container")
  117. }
  118. if v.Container.Image != image {
  119. t.Fatalf("expected image %s; received %s", image, v.Container.Image)
  120. }
  121. }
  122. func TestServiceConvertToGRPCGenericRuntimeCustom(t *testing.T) {
  123. s := swarmtypes.ServiceSpec{
  124. TaskTemplate: swarmtypes.TaskSpec{
  125. Runtime: "customruntime",
  126. },
  127. Mode: swarmtypes.ServiceMode{
  128. Global: &swarmtypes.GlobalService{},
  129. },
  130. }
  131. if _, err := ServiceSpecToGRPC(s); err != ErrUnsupportedRuntime {
  132. t.Fatal(err)
  133. }
  134. }
  135. func TestServiceConvertToGRPCIsolation(t *testing.T) {
  136. cases := []struct {
  137. name string
  138. from containertypes.Isolation
  139. to swarmapi.ContainerSpec_Isolation
  140. }{
  141. {name: "empty", from: containertypes.IsolationEmpty, to: swarmapi.ContainerIsolationDefault},
  142. {name: "default", from: containertypes.IsolationDefault, to: swarmapi.ContainerIsolationDefault},
  143. {name: "process", from: containertypes.IsolationProcess, to: swarmapi.ContainerIsolationProcess},
  144. {name: "hyperv", from: containertypes.IsolationHyperV, to: swarmapi.ContainerIsolationHyperV},
  145. {name: "proCess", from: containertypes.Isolation("proCess"), to: swarmapi.ContainerIsolationProcess},
  146. {name: "hypErv", from: containertypes.Isolation("hypErv"), to: swarmapi.ContainerIsolationHyperV},
  147. }
  148. for _, c := range cases {
  149. t.Run(c.name, func(t *testing.T) {
  150. s := swarmtypes.ServiceSpec{
  151. TaskTemplate: swarmtypes.TaskSpec{
  152. ContainerSpec: &swarmtypes.ContainerSpec{
  153. Image: "alpine:latest",
  154. Isolation: c.from,
  155. },
  156. },
  157. Mode: swarmtypes.ServiceMode{
  158. Global: &swarmtypes.GlobalService{},
  159. },
  160. }
  161. res, err := ServiceSpecToGRPC(s)
  162. require.NoError(t, err)
  163. v, ok := res.Task.Runtime.(*swarmapi.TaskSpec_Container)
  164. if !ok {
  165. t.Fatal("expected type swarmapi.TaskSpec_Container")
  166. }
  167. require.Equal(t, c.to, v.Container.Isolation)
  168. })
  169. }
  170. }
  171. func TestServiceConvertFromGRPCIsolation(t *testing.T) {
  172. cases := []struct {
  173. name string
  174. from swarmapi.ContainerSpec_Isolation
  175. to containertypes.Isolation
  176. }{
  177. {name: "default", to: containertypes.IsolationDefault, from: swarmapi.ContainerIsolationDefault},
  178. {name: "process", to: containertypes.IsolationProcess, from: swarmapi.ContainerIsolationProcess},
  179. {name: "hyperv", to: containertypes.IsolationHyperV, from: swarmapi.ContainerIsolationHyperV},
  180. }
  181. for _, c := range cases {
  182. t.Run(c.name, func(t *testing.T) {
  183. gs := swarmapi.Service{
  184. Meta: swarmapi.Meta{
  185. Version: swarmapi.Version{
  186. Index: 1,
  187. },
  188. CreatedAt: nil,
  189. UpdatedAt: nil,
  190. },
  191. SpecVersion: &swarmapi.Version{
  192. Index: 1,
  193. },
  194. Spec: swarmapi.ServiceSpec{
  195. Task: swarmapi.TaskSpec{
  196. Runtime: &swarmapi.TaskSpec_Container{
  197. Container: &swarmapi.ContainerSpec{
  198. Image: "alpine:latest",
  199. Isolation: c.from,
  200. },
  201. },
  202. },
  203. },
  204. }
  205. svc, err := ServiceFromGRPC(gs)
  206. if err != nil {
  207. t.Fatal(err)
  208. }
  209. require.Equal(t, c.to, svc.Spec.TaskTemplate.ContainerSpec.Isolation)
  210. })
  211. }
  212. }