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