add service convert tests
Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>
This commit is contained in:
parent
3a9be92927
commit
dc762610ab
|
@ -1,6 +1,7 @@
|
||||||
package convert
|
package convert
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -10,6 +11,11 @@ import (
|
||||||
gogotypes "github.com/gogo/protobuf/types"
|
gogotypes "github.com/gogo/protobuf/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrUnsupportedRuntime returns an error if the runtime is not supported by the daemon
|
||||||
|
ErrUnsupportedRuntime = errors.New("unsupported runtime")
|
||||||
|
)
|
||||||
|
|
||||||
// ServiceFromGRPC converts a grpc Service to a Service.
|
// ServiceFromGRPC converts a grpc Service to a Service.
|
||||||
func ServiceFromGRPC(s swarmapi.Service) (types.Service, error) {
|
func ServiceFromGRPC(s swarmapi.Service) (types.Service, error) {
|
||||||
curSpec, err := serviceSpecFromGRPC(&s.Spec)
|
curSpec, err := serviceSpecFromGRPC(&s.Spec)
|
||||||
|
@ -170,7 +176,7 @@ func ServiceSpecToGRPC(s types.ServiceSpec) (swarmapi.ServiceSpec, error) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return swarmapi.ServiceSpec{}, fmt.Errorf("error creating service; unsupported runtime %q", s.TaskTemplate.Runtime)
|
return swarmapi.ServiceSpec{}, ErrUnsupportedRuntime
|
||||||
}
|
}
|
||||||
|
|
||||||
restartPolicy, err := restartPolicyToGRPC(s.TaskTemplate.RestartPolicy)
|
restartPolicy, err := restartPolicyToGRPC(s.TaskTemplate.RestartPolicy)
|
||||||
|
@ -489,8 +495,14 @@ func taskSpecFromGRPC(taskSpec swarmapi.TaskSpec) types.TaskSpec {
|
||||||
taskNetworks = append(taskNetworks, types.NetworkAttachmentConfig{Target: n.Target, Aliases: n.Aliases})
|
taskNetworks = append(taskNetworks, types.NetworkAttachmentConfig{Target: n.Target, Aliases: n.Aliases})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c := taskSpec.GetContainer()
|
||||||
|
cSpec := types.ContainerSpec{}
|
||||||
|
if c != nil {
|
||||||
|
cSpec = containerSpecFromGRPC(c)
|
||||||
|
}
|
||||||
|
|
||||||
return types.TaskSpec{
|
return types.TaskSpec{
|
||||||
ContainerSpec: containerSpecFromGRPC(taskSpec.GetContainer()),
|
ContainerSpec: cSpec,
|
||||||
Resources: resourcesFromGRPC(taskSpec.Resources),
|
Resources: resourcesFromGRPC(taskSpec.Resources),
|
||||||
RestartPolicy: restartPolicyFromGRPC(taskSpec.Restart),
|
RestartPolicy: restartPolicyFromGRPC(taskSpec.Restart),
|
||||||
Placement: placementFromGRPC(taskSpec.Placement),
|
Placement: placementFromGRPC(taskSpec.Placement),
|
||||||
|
|
|
@ -0,0 +1,148 @@
|
||||||
|
package convert
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
swarmtypes "github.com/docker/docker/api/types/swarm"
|
||||||
|
swarmapi "github.com/docker/swarmkit/api"
|
||||||
|
google_protobuf3 "github.com/gogo/protobuf/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestServiceConvertFromGRPCRuntimeContainer(t *testing.T) {
|
||||||
|
gs := swarmapi.Service{
|
||||||
|
Meta: swarmapi.Meta{
|
||||||
|
Version: swarmapi.Version{
|
||||||
|
Index: 1,
|
||||||
|
},
|
||||||
|
CreatedAt: nil,
|
||||||
|
UpdatedAt: nil,
|
||||||
|
},
|
||||||
|
SpecVersion: &swarmapi.Version{
|
||||||
|
Index: 1,
|
||||||
|
},
|
||||||
|
Spec: swarmapi.ServiceSpec{
|
||||||
|
Task: swarmapi.TaskSpec{
|
||||||
|
Runtime: &swarmapi.TaskSpec_Container{
|
||||||
|
Container: &swarmapi.ContainerSpec{
|
||||||
|
Image: "alpine:latest",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
svc, err := ServiceFromGRPC(gs)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if svc.Spec.TaskTemplate.Runtime != swarmtypes.RuntimeContainer {
|
||||||
|
t.Fatalf("expected type %s; received %T", swarmtypes.RuntimeContainer, svc.Spec.TaskTemplate.Runtime)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServiceConvertFromGRPCGenericRuntimePlugin(t *testing.T) {
|
||||||
|
kind := string(swarmtypes.RuntimePlugin)
|
||||||
|
url := swarmtypes.RuntimeURLPlugin
|
||||||
|
gs := swarmapi.Service{
|
||||||
|
Meta: swarmapi.Meta{
|
||||||
|
Version: swarmapi.Version{
|
||||||
|
Index: 1,
|
||||||
|
},
|
||||||
|
CreatedAt: nil,
|
||||||
|
UpdatedAt: nil,
|
||||||
|
},
|
||||||
|
SpecVersion: &swarmapi.Version{
|
||||||
|
Index: 1,
|
||||||
|
},
|
||||||
|
Spec: swarmapi.ServiceSpec{
|
||||||
|
Task: swarmapi.TaskSpec{
|
||||||
|
Runtime: &swarmapi.TaskSpec_Generic{
|
||||||
|
Generic: &swarmapi.GenericRuntimeSpec{
|
||||||
|
Kind: kind,
|
||||||
|
Payload: &google_protobuf3.Any{
|
||||||
|
TypeUrl: string(url),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
svc, err := ServiceFromGRPC(gs)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if svc.Spec.TaskTemplate.Runtime != swarmtypes.RuntimePlugin {
|
||||||
|
t.Fatalf("expected type %s; received %T", swarmtypes.RuntimePlugin, svc.Spec.TaskTemplate.Runtime)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServiceConvertToGRPCGenericRuntimePlugin(t *testing.T) {
|
||||||
|
s := swarmtypes.ServiceSpec{
|
||||||
|
TaskTemplate: swarmtypes.TaskSpec{
|
||||||
|
Runtime: swarmtypes.RuntimePlugin,
|
||||||
|
},
|
||||||
|
Mode: swarmtypes.ServiceMode{
|
||||||
|
Global: &swarmtypes.GlobalService{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
svc, err := ServiceSpecToGRPC(s)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
v, ok := svc.Task.Runtime.(*swarmapi.TaskSpec_Generic)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("expected type swarmapi.TaskSpec_Generic")
|
||||||
|
}
|
||||||
|
|
||||||
|
if v.Generic.Payload.TypeUrl != string(swarmtypes.RuntimeURLPlugin) {
|
||||||
|
t.Fatalf("expected url %s; received %s", swarmtypes.RuntimeURLPlugin, v.Generic.Payload.TypeUrl)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServiceConvertToGRPCContainerRuntime(t *testing.T) {
|
||||||
|
image := "alpine:latest"
|
||||||
|
s := swarmtypes.ServiceSpec{
|
||||||
|
TaskTemplate: swarmtypes.TaskSpec{
|
||||||
|
ContainerSpec: swarmtypes.ContainerSpec{
|
||||||
|
Image: image,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Mode: swarmtypes.ServiceMode{
|
||||||
|
Global: &swarmtypes.GlobalService{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
svc, err := ServiceSpecToGRPC(s)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
v, ok := svc.Task.Runtime.(*swarmapi.TaskSpec_Container)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("expected type swarmapi.TaskSpec_Container")
|
||||||
|
}
|
||||||
|
|
||||||
|
if v.Container.Image != image {
|
||||||
|
t.Fatalf("expected image %s; received %s", image, v.Container.Image)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServiceConvertToGRPCGenericRuntimeCustom(t *testing.T) {
|
||||||
|
s := swarmtypes.ServiceSpec{
|
||||||
|
TaskTemplate: swarmtypes.TaskSpec{
|
||||||
|
Runtime: "customruntime",
|
||||||
|
},
|
||||||
|
Mode: swarmtypes.ServiceMode{
|
||||||
|
Global: &swarmtypes.GlobalService{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := ServiceSpecToGRPC(s); err != ErrUnsupportedRuntime {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue