github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/daemon/cluster/convert/service_test.go (about)

     1  package convert
     2  
     3  import (
     4  	"testing"
     5  
     6  	swarmtypes "github.com/docker/docker/api/types/swarm"
     7  	swarmapi "github.com/docker/swarmkit/api"
     8  	google_protobuf3 "github.com/gogo/protobuf/types"
     9  )
    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  
    34  	svc, err := ServiceFromGRPC(gs)
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  
    39  	if svc.Spec.TaskTemplate.Runtime != swarmtypes.RuntimeContainer {
    40  		t.Fatalf("expected type %s; received %T", swarmtypes.RuntimeContainer, svc.Spec.TaskTemplate.Runtime)
    41  	}
    42  }
    43  
    44  func TestServiceConvertFromGRPCGenericRuntimePlugin(t *testing.T) {
    45  	kind := string(swarmtypes.RuntimePlugin)
    46  	url := swarmtypes.RuntimeURLPlugin
    47  	gs := swarmapi.Service{
    48  		Meta: swarmapi.Meta{
    49  			Version: swarmapi.Version{
    50  				Index: 1,
    51  			},
    52  			CreatedAt: nil,
    53  			UpdatedAt: nil,
    54  		},
    55  		SpecVersion: &swarmapi.Version{
    56  			Index: 1,
    57  		},
    58  		Spec: swarmapi.ServiceSpec{
    59  			Task: swarmapi.TaskSpec{
    60  				Runtime: &swarmapi.TaskSpec_Generic{
    61  					Generic: &swarmapi.GenericRuntimeSpec{
    62  						Kind: kind,
    63  						Payload: &google_protobuf3.Any{
    64  							TypeUrl: string(url),
    65  						},
    66  					},
    67  				},
    68  			},
    69  		},
    70  	}
    71  
    72  	svc, err := ServiceFromGRPC(gs)
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  
    77  	if svc.Spec.TaskTemplate.Runtime != swarmtypes.RuntimePlugin {
    78  		t.Fatalf("expected type %s; received %T", swarmtypes.RuntimePlugin, svc.Spec.TaskTemplate.Runtime)
    79  	}
    80  }
    81  
    82  func TestServiceConvertToGRPCGenericRuntimePlugin(t *testing.T) {
    83  	s := swarmtypes.ServiceSpec{
    84  		TaskTemplate: swarmtypes.TaskSpec{
    85  			Runtime: swarmtypes.RuntimePlugin,
    86  		},
    87  		Mode: swarmtypes.ServiceMode{
    88  			Global: &swarmtypes.GlobalService{},
    89  		},
    90  	}
    91  
    92  	svc, err := ServiceSpecToGRPC(s)
    93  	if err != nil {
    94  		t.Fatal(err)
    95  	}
    96  
    97  	v, ok := svc.Task.Runtime.(*swarmapi.TaskSpec_Generic)
    98  	if !ok {
    99  		t.Fatal("expected type swarmapi.TaskSpec_Generic")
   100  	}
   101  
   102  	if v.Generic.Payload.TypeUrl != string(swarmtypes.RuntimeURLPlugin) {
   103  		t.Fatalf("expected url %s; received %s", swarmtypes.RuntimeURLPlugin, v.Generic.Payload.TypeUrl)
   104  	}
   105  }
   106  
   107  func TestServiceConvertToGRPCContainerRuntime(t *testing.T) {
   108  	image := "alpine:latest"
   109  	s := swarmtypes.ServiceSpec{
   110  		TaskTemplate: swarmtypes.TaskSpec{
   111  			ContainerSpec: swarmtypes.ContainerSpec{
   112  				Image: image,
   113  			},
   114  		},
   115  		Mode: swarmtypes.ServiceMode{
   116  			Global: &swarmtypes.GlobalService{},
   117  		},
   118  	}
   119  
   120  	svc, err := ServiceSpecToGRPC(s)
   121  	if err != nil {
   122  		t.Fatal(err)
   123  	}
   124  
   125  	v, ok := svc.Task.Runtime.(*swarmapi.TaskSpec_Container)
   126  	if !ok {
   127  		t.Fatal("expected type swarmapi.TaskSpec_Container")
   128  	}
   129  
   130  	if v.Container.Image != image {
   131  		t.Fatalf("expected image %s; received %s", image, v.Container.Image)
   132  	}
   133  }
   134  
   135  func TestServiceConvertToGRPCGenericRuntimeCustom(t *testing.T) {
   136  	s := swarmtypes.ServiceSpec{
   137  		TaskTemplate: swarmtypes.TaskSpec{
   138  			Runtime: "customruntime",
   139  		},
   140  		Mode: swarmtypes.ServiceMode{
   141  			Global: &swarmtypes.GlobalService{},
   142  		},
   143  	}
   144  
   145  	if _, err := ServiceSpecToGRPC(s); err != ErrUnsupportedRuntime {
   146  		t.Fatal(err)
   147  	}
   148  }