From 736838838e1250981e146a1e99408f8f0b738453 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Tue, 9 Apr 2019 23:17:54 +1000 Subject: [PATCH 01/20] add configuration to service struct --- service/inject_definition.go | 18 +++++++++--------- service/service.go | 27 +++++++++++++-------------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/service/inject_definition.go b/service/inject_definition.go index ed2bec177..184d98341 100644 --- a/service/inject_definition.go +++ b/service/inject_definition.go @@ -17,18 +17,18 @@ func (s *Service) injectDefinition(def *importer.ServiceDefinition) { s.Tasks = s.defTasksToService(def.Tasks) s.Dependencies = s.defDependenciesToService(def.Dependencies) - configuration := &Dependency{ - Key: importer.ConfigurationDependencyKey, + s.Configuration = &Dependency{ + Key: MainServiceKey, } if def.Configuration != nil { - configuration.Command = def.Configuration.Command - configuration.Args = def.Configuration.Args - configuration.Ports = def.Configuration.Ports - configuration.Volumes = def.Configuration.Volumes - configuration.VolumesFrom = def.Configuration.VolumesFrom - configuration.Env = def.Configuration.Env + s.Configuration.Command = def.Configuration.Command + s.Configuration.Args = def.Configuration.Args + s.Configuration.Ports = def.Configuration.Ports + s.Configuration.Volumes = def.Configuration.Volumes + s.Configuration.VolumesFrom = def.Configuration.VolumesFrom + s.Configuration.Env = def.Configuration.Env } - s.Dependencies = append(s.Dependencies, configuration) + s.Dependencies = s.Dependencies } func (s *Service) defTasksToService(tasks map[string]*importer.Task) []*Task { diff --git a/service/service.go b/service/service.go index 4df42f9ac..f1a99a62c 100644 --- a/service/service.go +++ b/service/service.go @@ -21,6 +21,9 @@ import ( // * this is required for not breaking Service IDs unless there is a behavioral // change. +// MainServiceKey is key for main service. +const MainServiceKey = importer.ConfigurationDependencyKey + // Service represents a MESG service. type Service struct { // Hash is calculated from the combination of service's source and mesg.yml. @@ -46,6 +49,9 @@ type Service struct { // Dependencies are the Docker containers that service can depend on. Dependencies []*Dependency `hash:"name:6"` + // Configuration of the service + Configuration *Dependency `hash:"name:8"` + // Repository holds the service's repository url if it's living on // a Git host. Repository string `hash:"name:7"` @@ -102,8 +108,8 @@ func New(contextDir string, c container.Container, statuses chan DeployStatus, e } // replace default env with new one. - defenv := xos.EnvSliceToMap(s.configuration().Env) - s.configuration().Env = xos.EnvMapToSlice(xos.EnvMergeMaps(defenv, env)) + defenv := xos.EnvSliceToMap(s.Configuration.Env) + s.Configuration.Env = xos.EnvMapToSlice(xos.EnvMergeMaps(defenv, env)) if err := s.deploy(contextDir, c, statuses); err != nil { return nil, err @@ -123,7 +129,7 @@ func (s *Service) deploy(contextDir string, c container.Container, statuses chan s.sendStatus(statuses, "Image built with success", DDonePositive) - s.configuration().Image = imageHash + s.Configuration.Image = imageHash // TODO: the following test should be moved in New function if s.Sid == "" { // make sure that sid doesn't have the same length with id. @@ -151,6 +157,9 @@ func (s *Service) closeStatus(statuses chan DeployStatus) { // getDependency returns dependency dependencyKey or a not found error. func (s *Service) getDependency(dependencyKey string) (*Dependency, error) { + if dependencyKey == MainServiceKey { + return s.Configuration, nil + } for _, dep := range s.Dependencies { if dep.Key == dependencyKey { return dep, nil @@ -165,7 +174,7 @@ func (s *Service) validateConfigurationEnv(env map[string]string) error { for key := range env { exists := false // check if "key=" exists in configuration - for _, env := range s.configuration().Env { + for _, env := range s.Configuration.Env { if strings.HasPrefix(env, key+"=") { exists = true } @@ -181,16 +190,6 @@ func (s *Service) validateConfigurationEnv(env map[string]string) error { return nil } -// helper to return the configuration of the service from the dependencies array -func (s *Service) configuration() *Dependency { - for _, dep := range s.Dependencies { - if dep.Key == importer.ConfigurationDependencyKey { - return dep - } - } - return nil -} - // ErrNotDefinedEnv error returned when optionally given env variables // are not defined in the mesg.yml file. type ErrNotDefinedEnv struct { From 23df8480618f80db7f11f34b809a0f75c32bfe81 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Tue, 9 Apr 2019 23:18:44 +1000 Subject: [PATCH 02/20] update docker related functions with configuration --- service/start.go | 16 ++++++++++++---- service/status.go | 5 ++++- service/stop.go | 23 ++++++++++++++--------- service/volume.go | 17 +++++++++++------ 4 files changed, 41 insertions(+), 20 deletions(-) diff --git a/service/start.go b/service/start.go index dffe07ccf..26ae96a8f 100644 --- a/service/start.go +++ b/service/start.go @@ -19,7 +19,7 @@ func (s *Service) Start(c container.Container) (serviceIDs []string, err error) } // If there is one but not all services running stop to restart all if status == PARTIAL { - if err := s.StopDependencies(c); err != nil { + if err := s.Stop(c); err != nil { return nil, err } } @@ -29,14 +29,22 @@ func (s *Service) Start(c container.Container) (serviceIDs []string, err error) } // BUG: https://github.com/mesg-foundation/core/issues/382 // After solving this by docker, switch back to deploy in parallel - serviceIDs = make([]string, len(s.Dependencies)) - for i, dep := range s.Dependencies { + serviceIDs = make([]string, 0) + for _, dep := range s.Dependencies { serviceID, err := dep.Start(c, s, networkID) if err != nil { s.Stop(c) return nil, err } - serviceIDs[i] = serviceID + serviceIDs = append(serviceIDs, serviceID) + } + if s.Configuration != nil { + serviceID, err := s.Configuration.Start(c, s, networkID) + if err != nil { + s.Stop(c) + return nil, err + } + serviceIDs = append(serviceIDs, serviceID) } return serviceIDs, err } diff --git a/service/status.go b/service/status.go index e38f583dc..256427697 100644 --- a/service/status.go +++ b/service/status.go @@ -42,7 +42,10 @@ var containerStatusTypeMappings = map[container.StatusType]StatusType{ // Status returns StatusType of all dependency. func (s *Service) Status(c container.Container) (StatusType, error) { statuses := make(map[container.StatusType]bool) - for _, dep := range s.Dependencies { + for _, dep := range append(s.Dependencies, s.Configuration) { + if dep == nil { + continue + } status, err := dep.Status(c, s) if err != nil { return UNKNOWN, err diff --git a/service/stop.go b/service/stop.go index 0ec346013..ce87d45ec 100644 --- a/service/stop.go +++ b/service/stop.go @@ -24,17 +24,22 @@ func (s *Service) StopDependencies(c container.Container) error { var mutex sync.Mutex var wg sync.WaitGroup var err error + stop := func(d *Dependency) { + defer wg.Done() + errStop := d.Stop(c, s) + mutex.Lock() + defer mutex.Unlock() + if errStop != nil && err == nil { + err = errStop + } + } + if s.Configuration != nil { + wg.Add(1) + go stop(s.Configuration) + } for _, dep := range s.Dependencies { wg.Add(1) - go func(d *Dependency) { - defer wg.Done() - errStop := d.Stop(c, s) - mutex.Lock() - defer mutex.Unlock() - if errStop != nil && err == nil { - err = errStop - } - }(dep) + go stop(dep) } wg.Wait() return err diff --git a/service/volume.go b/service/volume.go index 7f2e59f5f..4087251b2 100644 --- a/service/volume.go +++ b/service/volume.go @@ -13,14 +13,19 @@ func (s *Service) DeleteVolumes(c container.Container) error { wg sync.WaitGroup errs xerrors.SyncErrors ) + delete := func(d *Dependency) { + defer wg.Done() + if err := d.DeleteVolumes(c, s); err != nil { + errs.Append(err) + } + } for _, d := range s.Dependencies { wg.Add(1) - go func(d *Dependency) { - defer wg.Done() - if err := d.DeleteVolumes(c, s); err != nil { - errs.Append(err) - } - }(d) + go delete(d) + } + if s.Configuration != nil { + wg.Add(1) + go delete(s.Configuration) } wg.Wait() return errs.ErrorOrNil() From 16dfcdd6b24344e67363b56aaddbb3d37a4c61c8 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Tue, 9 Apr 2019 23:19:44 +1000 Subject: [PATCH 03/20] update proto definition with configuration --- interface/grpc/core/proto.go | 30 +++-- protobuf/definition/service.pb.go | 188 +++++++++++++++++++++--------- protobuf/definition/service.proto | 9 ++ 3 files changed, 166 insertions(+), 61 deletions(-) diff --git a/interface/grpc/core/proto.go b/interface/grpc/core/proto.go index 6a6beedc6..6bf3c7c37 100644 --- a/interface/grpc/core/proto.go +++ b/interface/grpc/core/proto.go @@ -16,14 +16,15 @@ func toProtoServices(ss []*service.Service) []*definition.Service { func toProtoService(s *service.Service) *definition.Service { return &definition.Service{ - Hash: s.Hash, - Sid: s.Sid, - Name: s.Name, - Description: s.Description, - Repository: s.Repository, - Tasks: toProtoTasks(s.Tasks), - Events: toProtoEvents(s.Events), - Dependencies: toProtoDependencies(s.Dependencies), + Hash: s.Hash, + Sid: s.Sid, + Name: s.Name, + Description: s.Description, + Repository: s.Repository, + Tasks: toProtoTasks(s.Tasks), + Events: toProtoEvents(s.Events), + Configuration: toProtoConfiguration(s.Configuration), + Dependencies: toProtoDependencies(s.Dependencies), } } @@ -95,6 +96,19 @@ func toProtoParameters(params []*service.Parameter) []*definition.Parameter { return ps } +func toProtoConfiguration(configuration *service.Dependency) *definition.Configuration { + if configuration == nil { + return nil + } + return &definition.Configuration{ + Args: configuration.Args, + Command: configuration.Command, + Ports: configuration.Ports, + Volumes: configuration.Volumes, + Volumesfrom: configuration.VolumesFrom, + } +} + func toProtoDependency(dep *service.Dependency) *definition.Dependency { if dep == nil { return nil diff --git a/protobuf/definition/service.pb.go b/protobuf/definition/service.pb.go index bea81ed33..b63e49e6c 100644 --- a/protobuf/definition/service.pb.go +++ b/protobuf/definition/service.pb.go @@ -20,24 +20,25 @@ const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package // Service represents the service's definition. type Service struct { - Hash string `protobuf:"bytes,10,opt,name=hash,proto3" json:"hash,omitempty"` - Sid string `protobuf:"bytes,12,opt,name=sid,proto3" json:"sid,omitempty"` - Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` - Description string `protobuf:"bytes,2,opt,name=description,proto3" json:"description,omitempty"` - Tasks []*Task `protobuf:"bytes,5,rep,name=tasks,proto3" json:"tasks,omitempty"` - Events []*Event `protobuf:"bytes,6,rep,name=events,proto3" json:"events,omitempty"` - Dependencies []*Dependency `protobuf:"bytes,7,rep,name=dependencies,proto3" json:"dependencies,omitempty"` - Repository string `protobuf:"bytes,9,opt,name=repository,proto3" json:"repository,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + Hash string `protobuf:"bytes,10,opt,name=hash,proto3" json:"hash,omitempty"` + Sid string `protobuf:"bytes,12,opt,name=sid,proto3" json:"sid,omitempty"` + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + Description string `protobuf:"bytes,2,opt,name=description,proto3" json:"description,omitempty"` + Configuration *Configuration `protobuf:"bytes,8,opt,name=configuration,proto3" json:"configuration,omitempty"` + Tasks []*Task `protobuf:"bytes,5,rep,name=tasks,proto3" json:"tasks,omitempty"` + Events []*Event `protobuf:"bytes,6,rep,name=events,proto3" json:"events,omitempty"` + Dependencies []*Dependency `protobuf:"bytes,7,rep,name=dependencies,proto3" json:"dependencies,omitempty"` + Repository string `protobuf:"bytes,9,opt,name=repository,proto3" json:"repository,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` } func (m *Service) Reset() { *m = Service{} } func (m *Service) String() string { return proto.CompactTextString(m) } func (*Service) ProtoMessage() {} func (*Service) Descriptor() ([]byte, []int) { - return fileDescriptor_service_1b145be26108d61d, []int{0} + return fileDescriptor_service_c543e920957ebb96, []int{0} } func (m *Service) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_Service.Unmarshal(m, b) @@ -85,6 +86,13 @@ func (m *Service) GetDescription() string { return "" } +func (m *Service) GetConfiguration() *Configuration { + if m != nil { + return m.Configuration + } + return nil +} + func (m *Service) GetTasks() []*Task { if m != nil { return m.Tasks @@ -128,7 +136,7 @@ func (m *Event) Reset() { *m = Event{} } func (m *Event) String() string { return proto.CompactTextString(m) } func (*Event) ProtoMessage() {} func (*Event) Descriptor() ([]byte, []int) { - return fileDescriptor_service_1b145be26108d61d, []int{1} + return fileDescriptor_service_c543e920957ebb96, []int{1} } func (m *Event) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_Event.Unmarshal(m, b) @@ -192,7 +200,7 @@ func (m *Task) Reset() { *m = Task{} } func (m *Task) String() string { return proto.CompactTextString(m) } func (*Task) ProtoMessage() {} func (*Task) Descriptor() ([]byte, []int) { - return fileDescriptor_service_1b145be26108d61d, []int{2} + return fileDescriptor_service_c543e920957ebb96, []int{2} } func (m *Task) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_Task.Unmarshal(m, b) @@ -262,7 +270,7 @@ func (m *Output) Reset() { *m = Output{} } func (m *Output) String() string { return proto.CompactTextString(m) } func (*Output) ProtoMessage() {} func (*Output) Descriptor() ([]byte, []int) { - return fileDescriptor_service_1b145be26108d61d, []int{3} + return fileDescriptor_service_c543e920957ebb96, []int{3} } func (m *Output) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_Output.Unmarshal(m, b) @@ -328,7 +336,7 @@ func (m *Parameter) Reset() { *m = Parameter{} } func (m *Parameter) String() string { return proto.CompactTextString(m) } func (*Parameter) ProtoMessage() {} func (*Parameter) Descriptor() ([]byte, []int) { - return fileDescriptor_service_1b145be26108d61d, []int{4} + return fileDescriptor_service_c543e920957ebb96, []int{4} } func (m *Parameter) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_Parameter.Unmarshal(m, b) @@ -397,6 +405,76 @@ func (m *Parameter) GetObject() []*Parameter { return nil } +type Configuration struct { + Volumes []string `protobuf:"bytes,1,rep,name=volumes,proto3" json:"volumes,omitempty"` + Volumesfrom []string `protobuf:"bytes,2,rep,name=volumesfrom,proto3" json:"volumesfrom,omitempty"` + Ports []string `protobuf:"bytes,3,rep,name=ports,proto3" json:"ports,omitempty"` + Args []string `protobuf:"bytes,4,rep,name=args,proto3" json:"args,omitempty"` + Command string `protobuf:"bytes,5,opt,name=command,proto3" json:"command,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *Configuration) Reset() { *m = Configuration{} } +func (m *Configuration) String() string { return proto.CompactTextString(m) } +func (*Configuration) ProtoMessage() {} +func (*Configuration) Descriptor() ([]byte, []int) { + return fileDescriptor_service_c543e920957ebb96, []int{5} +} +func (m *Configuration) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_Configuration.Unmarshal(m, b) +} +func (m *Configuration) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_Configuration.Marshal(b, m, deterministic) +} +func (dst *Configuration) XXX_Merge(src proto.Message) { + xxx_messageInfo_Configuration.Merge(dst, src) +} +func (m *Configuration) XXX_Size() int { + return xxx_messageInfo_Configuration.Size(m) +} +func (m *Configuration) XXX_DiscardUnknown() { + xxx_messageInfo_Configuration.DiscardUnknown(m) +} + +var xxx_messageInfo_Configuration proto.InternalMessageInfo + +func (m *Configuration) GetVolumes() []string { + if m != nil { + return m.Volumes + } + return nil +} + +func (m *Configuration) GetVolumesfrom() []string { + if m != nil { + return m.Volumesfrom + } + return nil +} + +func (m *Configuration) GetPorts() []string { + if m != nil { + return m.Ports + } + return nil +} + +func (m *Configuration) GetArgs() []string { + if m != nil { + return m.Args + } + return nil +} + +func (m *Configuration) GetCommand() string { + if m != nil { + return m.Command + } + return "" +} + // A dependency is a configuration of an other container that runs separately from the service. type Dependency struct { Key string `protobuf:"bytes,8,opt,name=key,proto3" json:"key,omitempty"` @@ -415,7 +493,7 @@ func (m *Dependency) Reset() { *m = Dependency{} } func (m *Dependency) String() string { return proto.CompactTextString(m) } func (*Dependency) ProtoMessage() {} func (*Dependency) Descriptor() ([]byte, []int) { - return fileDescriptor_service_1b145be26108d61d, []int{5} + return fileDescriptor_service_c543e920957ebb96, []int{6} } func (m *Dependency) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_Dependency.Unmarshal(m, b) @@ -490,44 +568,48 @@ func init() { proto.RegisterType((*Task)(nil), "definition.Task") proto.RegisterType((*Output)(nil), "definition.Output") proto.RegisterType((*Parameter)(nil), "definition.Parameter") + proto.RegisterType((*Configuration)(nil), "definition.Configuration") proto.RegisterType((*Dependency)(nil), "definition.Dependency") } func init() { - proto.RegisterFile("protobuf/definition/service.proto", fileDescriptor_service_1b145be26108d61d) -} - -var fileDescriptor_service_1b145be26108d61d = []byte{ - // 483 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xc4, 0x54, 0x4d, 0x8f, 0xd3, 0x30, - 0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3, 0x64, - 0xaf, 0x6f, 0xda, 0x0a, 0x2d, 0x1f, 0xcf, 0xa2, 0x79, 0x26, 0xfa, 0xd6, 0xbb, 0x3d, 0x94, 0x6b, - 0xa3, 0x2b, 0xca, 0x34, 0x13, 0x43, 0xc8, 0xdf, 0xd8, 0x68, 0xe3, 0x2c, 0x8f, 0x89, 0x0b, 0x8d, - 0xcf, 0x40, 0x9a, 0x4d, 0x78, 0x31, 0x99, 0xa0, 0xda, 0x4f, 0x29, 0x74, 0x55, 0xc9, 0xba, 0xe4, - 0x09, 0x4d, 0xef, 0xdb, 0x57, 0x2f, 0x3e, 0x5e, 0x6f, 0x94, 0xdb, 0xb6, 0xab, 0x45, 0xa1, 0xab, - 0x65, 0x85, 0x76, 0x73, 0xb5, 0xd6, 0x6d, 0x5d, 0x4a, 0xfa, 0x7d, 0x0a, 0x6d, 0x70, 0x79, 0xcb, - 0xb7, 0xb4, 0x4a, 0x09, 0xbc, 0xfe, 0x19, 0x00, 0x00, 0xff, 0xff, 0x11, 0x2a, 0xb7, 0x3e, 0xb4, - 0x04, 0x00, 0x00, + proto.RegisterFile("protobuf/definition/service.proto", fileDescriptor_service_c543e920957ebb96) +} + +var fileDescriptor_service_c543e920957ebb96 = []byte{ + // 536 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xc4, 0x54, 0x4d, 0x8b, 0x13, 0x41, + 0x10, 0x65, 0x32, 0x1f, 0x49, 0x6a, 0x77, 0x61, 0x6d, 0x56, 0x69, 0x3d, 0x48, 0x9c, 0x83, 0x64, + 0xc1, 0x4d, 0x60, 0x17, 0x2f, 0x5e, 0x04, 0x3f, 0xce, 0x4a, 0xeb, 0xc9, 0x5b, 0x67, 0xa6, 0x92, + 0x6d, 0xb3, 0x33, 0x3d, 0x74, 0xf7, 0x2c, 0x44, 0x7f, 0x84, 0x3f, 0xc4, 0xab, 0xbf, 0xc3, 0x9b, + 0xff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d, 0xb3, + 0x14, 0xe5, 0x50, 0x34, 0x8d, 0x3f, 0x4f, 0x9a, 0x85, 0xe5, 0x11, 0x81, 0x54, 0xfb, 0x2d, 0x99, + 0x2e, 0x0a, 0x59, 0xe6, 0x3c, 0xa6, 0xab, 0xdb, 0x36, 0xfd, 0x16, 0x00, 0x6c, 0x9f, 0xd1, 0x9e, + 0x8c, 0xcf, 0x20, 0x56, 0x85, 0x5c, 0xb4, 0x21, 0x37, 0x4d, 0xd7, 0x76, 0xef, 0xa0, 0xed, 0xf0, + 0x80, 0xed, 0x68, 0x9f, 0xed, 0xe4, 0x77, 0x6c, 0xbf, 0x78, 0xfa, 0xe1, 0x6a, 0xa1, 0xdc, 0x75, + 0x3d, 0x9b, 0x64, 0xba, 0x98, 0x16, 0x68, 0x17, 0x17, 0x73, 0x5d, 0x97, 0x39, 0x85, 0x3a, 0xcd, + 0xb4, 0xc1, 0xe9, 0x9e, 0x2f, 0xed, 0x2c, 0x21, 0xf0, 0xea, 0x67, 0x00, 0x00, 0x00, 0xff, 0xff, + 0x5e, 0xa2, 0x91, 0x46, 0x87, 0x05, 0x00, 0x00, } diff --git a/protobuf/definition/service.proto b/protobuf/definition/service.proto index c996a0285..115c0288d 100644 --- a/protobuf/definition/service.proto +++ b/protobuf/definition/service.proto @@ -9,6 +9,7 @@ message Service { string sid = 12; // Service's sid. string name = 1; // Service's name. string description = 2; // Service's description. + Configuration configuration = 8; // Configurations related to the service repeated Task tasks = 5; // The list of tasks this service can execute. repeated Event events = 6; // The list of events this service can emit. repeated Dependency dependencies = 7; // The container dependencies this service requires. @@ -51,6 +52,14 @@ message Parameter { repeated Parameter object = 10; // Optional object structure definition when type is set to `Object` } +message Configuration { + repeated string volumes = 1; // List of volumes. + repeated string volumesfrom = 2; // List of volumes mounted from other dependencies. + repeated string ports = 3; // List of ports the container exposes. + repeated string args = 4; // Args to pass to the container. + string command = 5; // Command to run the container. +} + // A dependency is a configuration of an other container that runs separately from the service. message Dependency { string key = 8; // Dependency's key. From 25bfd09e08ebdb265436362abd3402cf60e41181 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Tue, 9 Apr 2019 23:31:21 +1000 Subject: [PATCH 04/20] tests + fixes --- service/inject_definition.go | 1 - service/service_test.go | 20 ++++++++++---------- service/start_test.go | 1 + 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/service/inject_definition.go b/service/inject_definition.go index 184d98341..869fc177f 100644 --- a/service/inject_definition.go +++ b/service/inject_definition.go @@ -28,7 +28,6 @@ func (s *Service) injectDefinition(def *importer.ServiceDefinition) { s.Configuration.VolumesFrom = def.Configuration.VolumesFrom s.Configuration.Env = def.Configuration.Env } - s.Dependencies = s.Dependencies } func (s *Service) defTasksToService(tasks map[string]*importer.Task) []*Task { diff --git a/service/service_test.go b/service/service_test.go index 739c00802..0a99104ca 100644 --- a/service/service_test.go +++ b/service/service_test.go @@ -28,9 +28,9 @@ func TestNew(t *testing.T) { s, err := New(path, mc, statuses, nil) require.NoError(t, err) - require.Equal(t, "service", s.Dependencies[0].Key) - require.Equal(t, hash, s.Dependencies[0].Image) - require.Len(t, s.Dependencies[0].Env, 0) + require.Equal(t, "service", s.Configuration.Key) + require.Equal(t, hash, s.Configuration.Image) + require.Len(t, s.Configuration.Env, 0) require.Equal(t, DeployStatus{ Message: "Building Docker image...", @@ -57,9 +57,9 @@ func TestNewWithDefaultEnv(t *testing.T) { s, err := New(path, mc, nil, nil) require.NoError(t, err) - require.Equal(t, "service", s.Dependencies[0].Key) - require.Equal(t, hash, s.Dependencies[0].Image) - require.Equal(t, env, s.Dependencies[0].Env) + require.Equal(t, "service", s.Configuration.Key) + require.Equal(t, hash, s.Configuration.Image) + require.Equal(t, env, s.Configuration.Env) mc.AssertExpectations(t) } @@ -76,9 +76,9 @@ func TestNewWithOverwrittenEnv(t *testing.T) { s, err := New(path, mc, nil, xos.EnvSliceToMap(env)) require.NoError(t, err) - require.Equal(t, "service", s.Dependencies[0].Key) - require.Equal(t, hash, s.Dependencies[0].Image) - require.Equal(t, env, s.Dependencies[0].Env) + require.Equal(t, "service", s.Configuration.Key) + require.Equal(t, hash, s.Configuration.Image) + require.Equal(t, env, s.Configuration.Env) mc.AssertExpectations(t) } @@ -109,7 +109,7 @@ func TestInjectDefinitionWithConfig(t *testing.T) { Command: command, }, }) - require.Equal(t, command, s.configuration().Command) + require.Equal(t, command, s.Configuration.Command) } func TestInjectDefinitionWithDependency(t *testing.T) { diff --git a/service/start_test.go b/service/start_test.go index a89554c0e..cc6bc49c3 100644 --- a/service/start_test.go +++ b/service/start_test.go @@ -225,6 +225,7 @@ func TestPartiallyRunningService(t *testing.T) { mc.On("StopService", d2.namespace(s.namespace())).Once().Return(nil) mc.On("CreateNetwork", s.namespace()).Once().Return(networkID, nil) mc.On("SharedNetworkID").Twice().Return(sharedNetworkID, nil) + mc.On("DeleteNetwork", s.namespace()).Return(nil) for i, d := range ds { mockStartService(s, d, mc, networkID, sharedNetworkID, containerServiceIDs[i], nil) From 8c7926515a78657ace5bb359fb661ad90f705dd4 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Wed, 10 Apr 2019 10:38:18 +1000 Subject: [PATCH 05/20] rename volumefrom from the proto definition --- interface/grpc/core/proto.go | 4 +- protobuf/definition/service.pb.go | 96 +++++++++++++++---------------- protobuf/definition/service.proto | 4 +- 3 files changed, 52 insertions(+), 52 deletions(-) diff --git a/interface/grpc/core/proto.go b/interface/grpc/core/proto.go index 6bf3c7c37..c9d92e1e2 100644 --- a/interface/grpc/core/proto.go +++ b/interface/grpc/core/proto.go @@ -105,7 +105,7 @@ func toProtoConfiguration(configuration *service.Dependency) *definition.Configu Command: configuration.Command, Ports: configuration.Ports, Volumes: configuration.Volumes, - Volumesfrom: configuration.VolumesFrom, + VolumesFrom: configuration.VolumesFrom, } } @@ -117,7 +117,7 @@ func toProtoDependency(dep *service.Dependency) *definition.Dependency { Key: dep.Key, Image: dep.Image, Volumes: dep.Volumes, - Volumesfrom: dep.VolumesFrom, + VolumesFrom: dep.VolumesFrom, Ports: dep.Ports, Command: dep.Command, Args: dep.Args, diff --git a/protobuf/definition/service.pb.go b/protobuf/definition/service.pb.go index b63e49e6c..1a37369a1 100644 --- a/protobuf/definition/service.pb.go +++ b/protobuf/definition/service.pb.go @@ -38,7 +38,7 @@ func (m *Service) Reset() { *m = Service{} } func (m *Service) String() string { return proto.CompactTextString(m) } func (*Service) ProtoMessage() {} func (*Service) Descriptor() ([]byte, []int) { - return fileDescriptor_service_c543e920957ebb96, []int{0} + return fileDescriptor_service_0372ce040157adf6, []int{0} } func (m *Service) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_Service.Unmarshal(m, b) @@ -136,7 +136,7 @@ func (m *Event) Reset() { *m = Event{} } func (m *Event) String() string { return proto.CompactTextString(m) } func (*Event) ProtoMessage() {} func (*Event) Descriptor() ([]byte, []int) { - return fileDescriptor_service_c543e920957ebb96, []int{1} + return fileDescriptor_service_0372ce040157adf6, []int{1} } func (m *Event) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_Event.Unmarshal(m, b) @@ -200,7 +200,7 @@ func (m *Task) Reset() { *m = Task{} } func (m *Task) String() string { return proto.CompactTextString(m) } func (*Task) ProtoMessage() {} func (*Task) Descriptor() ([]byte, []int) { - return fileDescriptor_service_c543e920957ebb96, []int{2} + return fileDescriptor_service_0372ce040157adf6, []int{2} } func (m *Task) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_Task.Unmarshal(m, b) @@ -270,7 +270,7 @@ func (m *Output) Reset() { *m = Output{} } func (m *Output) String() string { return proto.CompactTextString(m) } func (*Output) ProtoMessage() {} func (*Output) Descriptor() ([]byte, []int) { - return fileDescriptor_service_c543e920957ebb96, []int{3} + return fileDescriptor_service_0372ce040157adf6, []int{3} } func (m *Output) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_Output.Unmarshal(m, b) @@ -336,7 +336,7 @@ func (m *Parameter) Reset() { *m = Parameter{} } func (m *Parameter) String() string { return proto.CompactTextString(m) } func (*Parameter) ProtoMessage() {} func (*Parameter) Descriptor() ([]byte, []int) { - return fileDescriptor_service_c543e920957ebb96, []int{4} + return fileDescriptor_service_0372ce040157adf6, []int{4} } func (m *Parameter) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_Parameter.Unmarshal(m, b) @@ -407,7 +407,7 @@ func (m *Parameter) GetObject() []*Parameter { type Configuration struct { Volumes []string `protobuf:"bytes,1,rep,name=volumes,proto3" json:"volumes,omitempty"` - Volumesfrom []string `protobuf:"bytes,2,rep,name=volumesfrom,proto3" json:"volumesfrom,omitempty"` + VolumesFrom []string `protobuf:"bytes,2,rep,name=volumesFrom,proto3" json:"volumesFrom,omitempty"` Ports []string `protobuf:"bytes,3,rep,name=ports,proto3" json:"ports,omitempty"` Args []string `protobuf:"bytes,4,rep,name=args,proto3" json:"args,omitempty"` Command string `protobuf:"bytes,5,opt,name=command,proto3" json:"command,omitempty"` @@ -420,7 +420,7 @@ func (m *Configuration) Reset() { *m = Configuration{} } func (m *Configuration) String() string { return proto.CompactTextString(m) } func (*Configuration) ProtoMessage() {} func (*Configuration) Descriptor() ([]byte, []int) { - return fileDescriptor_service_c543e920957ebb96, []int{5} + return fileDescriptor_service_0372ce040157adf6, []int{5} } func (m *Configuration) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_Configuration.Unmarshal(m, b) @@ -447,9 +447,9 @@ func (m *Configuration) GetVolumes() []string { return nil } -func (m *Configuration) GetVolumesfrom() []string { +func (m *Configuration) GetVolumesFrom() []string { if m != nil { - return m.Volumesfrom + return m.VolumesFrom } return nil } @@ -480,7 +480,7 @@ type Dependency struct { Key string `protobuf:"bytes,8,opt,name=key,proto3" json:"key,omitempty"` Image string `protobuf:"bytes,1,opt,name=image,proto3" json:"image,omitempty"` Volumes []string `protobuf:"bytes,2,rep,name=volumes,proto3" json:"volumes,omitempty"` - Volumesfrom []string `protobuf:"bytes,3,rep,name=volumesfrom,proto3" json:"volumesfrom,omitempty"` + VolumesFrom []string `protobuf:"bytes,3,rep,name=volumesFrom,proto3" json:"volumesFrom,omitempty"` Ports []string `protobuf:"bytes,4,rep,name=ports,proto3" json:"ports,omitempty"` Args []string `protobuf:"bytes,6,rep,name=args,proto3" json:"args,omitempty"` Command string `protobuf:"bytes,5,opt,name=command,proto3" json:"command,omitempty"` @@ -493,7 +493,7 @@ func (m *Dependency) Reset() { *m = Dependency{} } func (m *Dependency) String() string { return proto.CompactTextString(m) } func (*Dependency) ProtoMessage() {} func (*Dependency) Descriptor() ([]byte, []int) { - return fileDescriptor_service_c543e920957ebb96, []int{6} + return fileDescriptor_service_0372ce040157adf6, []int{6} } func (m *Dependency) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_Dependency.Unmarshal(m, b) @@ -534,9 +534,9 @@ func (m *Dependency) GetVolumes() []string { return nil } -func (m *Dependency) GetVolumesfrom() []string { +func (m *Dependency) GetVolumesFrom() []string { if m != nil { - return m.Volumesfrom + return m.VolumesFrom } return nil } @@ -573,43 +573,43 @@ func init() { } func init() { - proto.RegisterFile("protobuf/definition/service.proto", fileDescriptor_service_c543e920957ebb96) + proto.RegisterFile("protobuf/definition/service.proto", fileDescriptor_service_0372ce040157adf6) } -var fileDescriptor_service_c543e920957ebb96 = []byte{ +var fileDescriptor_service_0372ce040157adf6 = []byte{ // 536 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xc4, 0x54, 0x4d, 0x8b, 0x13, 0x41, - 0x10, 0x65, 0x32, 0x1f, 0x49, 0x6a, 0x77, 0x61, 0x6d, 0x56, 0x69, 0x3d, 0x48, 0x9c, 0x83, 0x64, - 0xc1, 0x4d, 0x60, 0x17, 0x2f, 0x5e, 0x04, 0x3f, 0xce, 0x4a, 0xeb, 0xc9, 0x5b, 0x67, 0xa6, 0x92, - 0x6d, 0xb3, 0x33, 0x3d, 0x74, 0xf7, 0x2c, 0x44, 0x7f, 0x84, 0x3f, 0xc4, 0xab, 0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, 0x06, 0x71, 0xa5, 0x8d, 0xb3, - 0x14, 0xe5, 0x50, 0x34, 0x8d, 0x3f, 0x4f, 0x9a, 0x85, 0xe5, 0x11, 0x81, 0x54, 0xfb, 0x2d, 0x99, - 0x2e, 0x0a, 0x59, 0xe6, 0x3c, 0xa6, 0xab, 0xdb, 0x36, 0xfd, 0x16, 0x00, 0x6c, 0x9f, 0xd1, 0x9e, - 0x8c, 0xcf, 0x20, 0x56, 0x85, 0x5c, 0xb4, 0x21, 0x37, 0x4d, 0xd7, 0x76, 0xef, 0xa0, 0xed, 0xf0, - 0x80, 0xed, 0x68, 0x9f, 0xed, 0xe4, 0x77, 0x6c, 0xbf, 0x78, 0xfa, 0xe1, 0x6a, 0xa1, 0xdc, 0x75, - 0x3d, 0x9b, 0x64, 0xba, 0x98, 0x16, 0x68, 0x17, 0x17, 0x73, 0x5d, 0x97, 0x39, 0x85, 0x3a, 0xcd, - 0xb4, 0xc1, 0xe9, 0x9e, 0x2f, 0xed, 0x2c, 0x21, 0xf0, 0xea, 0x67, 0x00, 0x00, 0x00, 0xff, 0xff, - 0x5e, 0xa2, 0x91, 0x46, 0x87, 0x05, 0x00, 0x00, + 0x10, 0x65, 0x32, 0x1f, 0x49, 0x2a, 0xbb, 0xb0, 0x36, 0xab, 0xb4, 0x1e, 0x24, 0xce, 0x41, 0xb2, + 0xe0, 0x26, 0xb0, 0x8b, 0x17, 0x2f, 0x82, 0x5f, 0x57, 0x65, 0xf4, 0xe4, 0xad, 0x33, 0x53, 0x49, + 0xda, 0xec, 0x4c, 0x0f, 0xdd, 0x3d, 0x81, 0xe8, 0x8f, 0xf0, 0x87, 0x78, 0xf5, 0x77, 0x78, 0xf3, + 0xff, 0x48, 0xd7, 0x64, 0x92, 0x09, 0xc4, 0x20, 0x2a, 0xec, 0xad, 0xea, 0xbd, 0x57, 0xa9, 0x57, + 0x8f, 0xf4, 0xc0, 0xa3, 0x52, 0x2b, 0xab, 0xa6, 0xd5, 0x6c, 0x92, 0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dd, 0xb4, 0xf1, 0x77, 0x0f, 0x60, 0xf7, 0x8c, 0x0e, + 0x64, 0x7c, 0x0e, 0xa1, 0xcc, 0xc5, 0xbc, 0x09, 0xb9, 0x6e, 0xda, 0xb6, 0x3b, 0x47, 0x6d, 0xfb, + 0x47, 0x6c, 0x07, 0x87, 0x6c, 0x47, 0x7f, 0x62, 0xfb, 0xc5, 0xd3, 0x8f, 0xd7, 0x73, 0x69, 0x17, + 0xd5, 0x74, 0x9c, 0xaa, 0x7c, 0x92, 0xa3, 0x99, 0x5f, 0xce, 0x54, 0x55, 0x64, 0x14, 0xea, 0x24, + 0x55, 0x1a, 0x27, 0x07, 0xbe, 0xb4, 0xd3, 0x88, 0xc0, 0xeb, 0x5f, 0x01, 0x00, 0x00, 0xff, 0xff, + 0xad, 0x9d, 0x3a, 0x5f, 0x87, 0x05, 0x00, 0x00, } diff --git a/protobuf/definition/service.proto b/protobuf/definition/service.proto index 115c0288d..29c6ad622 100644 --- a/protobuf/definition/service.proto +++ b/protobuf/definition/service.proto @@ -54,7 +54,7 @@ message Parameter { message Configuration { repeated string volumes = 1; // List of volumes. - repeated string volumesfrom = 2; // List of volumes mounted from other dependencies. + repeated string volumesFrom = 2; // List of volumes mounted from other dependencies. repeated string ports = 3; // List of ports the container exposes. repeated string args = 4; // Args to pass to the container. string command = 5; // Command to run the container. @@ -65,7 +65,7 @@ message Dependency { string key = 8; // Dependency's key. string image = 1; // Image's name of the container. repeated string volumes = 2; // List of volumes. - repeated string volumesfrom = 3; // List of volumes mounted from other dependencies. + repeated string volumesFrom = 3; // List of volumes mounted from other dependencies. repeated string ports = 4; // List of ports the container exposes. repeated string args = 6; // Args to pass to the container. string command = 5; // Command to run the container. From e83a8975b0266dbb2798b286ea33aa8c67fcb71a Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Wed, 10 Apr 2019 10:47:02 +1000 Subject: [PATCH 06/20] remove change of behavior about partial stop --- service/start.go | 2 +- service/start_test.go | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/service/start.go b/service/start.go index 26ae96a8f..3bc55c040 100644 --- a/service/start.go +++ b/service/start.go @@ -19,7 +19,7 @@ func (s *Service) Start(c container.Container) (serviceIDs []string, err error) } // If there is one but not all services running stop to restart all if status == PARTIAL { - if err := s.Stop(c); err != nil { + if err := s.StopDependencies(c); err != nil { return nil, err } } diff --git a/service/start_test.go b/service/start_test.go index cc6bc49c3..a89554c0e 100644 --- a/service/start_test.go +++ b/service/start_test.go @@ -225,7 +225,6 @@ func TestPartiallyRunningService(t *testing.T) { mc.On("StopService", d2.namespace(s.namespace())).Once().Return(nil) mc.On("CreateNetwork", s.namespace()).Once().Return(networkID, nil) mc.On("SharedNetworkID").Twice().Return(sharedNetworkID, nil) - mc.On("DeleteNetwork", s.namespace()).Return(nil) for i, d := range ds { mockStartService(s, d, mc, networkID, sharedNetworkID, containerServiceIDs[i], nil) From 3f660abbb51e28a6aa353a1265bbc621e218e658 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Wed, 10 Apr 2019 10:51:35 +1000 Subject: [PATCH 07/20] use stop function to stop all dependencies --- service/start.go | 2 +- service/start_test.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/service/start.go b/service/start.go index dffe07ccf..42dd18710 100644 --- a/service/start.go +++ b/service/start.go @@ -19,7 +19,7 @@ func (s *Service) Start(c container.Container) (serviceIDs []string, err error) } // If there is one but not all services running stop to restart all if status == PARTIAL { - if err := s.StopDependencies(c); err != nil { + if err := s.Stop(c); err != nil { return nil, err } } diff --git a/service/start_test.go b/service/start_test.go index a89554c0e..cc6bc49c3 100644 --- a/service/start_test.go +++ b/service/start_test.go @@ -225,6 +225,7 @@ func TestPartiallyRunningService(t *testing.T) { mc.On("StopService", d2.namespace(s.namespace())).Once().Return(nil) mc.On("CreateNetwork", s.namespace()).Once().Return(networkID, nil) mc.On("SharedNetworkID").Twice().Return(sharedNetworkID, nil) + mc.On("DeleteNetwork", s.namespace()).Return(nil) for i, d := range ds { mockStartService(s, d, mc, networkID, sharedNetworkID, containerServiceIDs[i], nil) From a5e562f89e7fc12d2dd1ccc021846461e173143a Mon Sep 17 00:00:00 2001 From: krhubert Date: Wed, 10 Apr 2019 08:13:01 +0200 Subject: [PATCH 08/20] Validate env variable --- service/importer/definition.go | 2 +- service/importer/service_file.go | 9 +++++++++ x/xvalidator/validator.go | 10 ++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/service/importer/definition.go b/service/importer/definition.go index e5a2c93d6..a8cbd087c 100644 --- a/service/importer/definition.go +++ b/service/importer/definition.go @@ -64,7 +64,7 @@ type Dependency struct { Args []string `yaml:"args" json:"args,omitempty" validate:"dive,printascii"` // Env is a slice of environment variables in key=value format. - Env []string `yaml:"env" json:"env,omitempty" validate:"unique,dive,printascii"` + Env []string `yaml:"env" json:"env,omitempty" validate:"unique,dive,env"` } // Task describes a service task. diff --git a/service/importer/service_file.go b/service/importer/service_file.go index 110902b38..dc72eb91f 100644 --- a/service/importer/service_file.go +++ b/service/importer/service_file.go @@ -105,6 +105,15 @@ func newValidator() (*validator.Validate, ut.Translator) { uni := ut.New(en, en) trans, _ := uni.GetTranslator("en") validate := validator.New() + + validate.RegisterValidation("env", xvalidator.IsEnv) + validate.RegisterTranslation("env", trans, func(ut ut.Translator) error { + return ut.Add("portmap", "{0} must be a valid env variable name", false) + }, func(ut ut.Translator, fe validator.FieldError) string { + t, _ := ut.T("env", fe.Field(), namespacePrefix) + return t + }) + validate.RegisterValidation("portmap", xvalidator.IsPortMapping) validate.RegisterTranslation("portmap", trans, func(ut ut.Translator) error { return ut.Add("portmap", "{0} must be a valid port mapping. Eg: 80 or 80:80", false) diff --git a/x/xvalidator/validator.go b/x/xvalidator/validator.go index dca27f724..997017b2e 100644 --- a/x/xvalidator/validator.go +++ b/x/xvalidator/validator.go @@ -1,6 +1,7 @@ package xvalidator import ( + "regexp" "strconv" "strings" @@ -13,8 +14,11 @@ const ( maxPort = 65535 portSeparator = ":" + envSeparator = "=" ) +var envNameRegexp = regexp.MustCompile("^[a-zA-Z][a-zA-Z0-9_]*$") + // IsDomainName validates if given field is valid domain name. func IsDomainName(fl validator.FieldLevel) bool { return xnet.IsDomainName(fl.Field().String()) @@ -35,3 +39,9 @@ func IsPortMapping(fl validator.FieldLevel) bool { } return true } + +// IsEnv validates if given field is valid env variable declaration. +func IsEnv(fl validator.FieldLevel) bool { + e := strings.Split(fl.Field().String(), envSeparator) + return len(e) == 2 && envNameRegexp.MatchString(e[0]) +} From 9f6e6dde531886d6aa64132990bf5175d15db6d9 Mon Sep 17 00:00:00 2001 From: Anthony Date: Thu, 11 Apr 2019 11:13:15 +0200 Subject: [PATCH 09/20] Fix translation for env validation Co-Authored-By: krhubert --- service/importer/service_file.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/importer/service_file.go b/service/importer/service_file.go index dc72eb91f..bde76cd78 100644 --- a/service/importer/service_file.go +++ b/service/importer/service_file.go @@ -108,7 +108,7 @@ func newValidator() (*validator.Validate, ut.Translator) { validate.RegisterValidation("env", xvalidator.IsEnv) validate.RegisterTranslation("env", trans, func(ut ut.Translator) error { - return ut.Add("portmap", "{0} must be a valid env variable name", false) + return ut.Add("env", "{0} must be a valid env variable name", false) }, func(ut ut.Translator, fe validator.FieldError) string { t, _ := ut.T("env", fe.Field(), namespacePrefix) return t From 899efb0a74f0d281b8aebee9348607ab98af4845 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=CC=87lker=20Go=CC=88ktug=CC=86=20O=CC=88ztu=CC=88rk?= Date: Thu, 11 Apr 2019 12:24:51 +0300 Subject: [PATCH 10/20] service: simplify tests by removing unneded newFromServiceAndContainerMocks func --- service/dependency_test.go | 6 ++++-- service/service_test.go | 5 ----- service/start_test.go | 30 ++++++++++++++++++------------ service/status_test.go | 26 ++++++++++++++++---------- service/stop_test.go | 11 +++++++---- service/volume_test.go | 6 ++++-- 6 files changed, 49 insertions(+), 35 deletions(-) diff --git a/service/dependency_test.go b/service/dependency_test.go index 664e0d7a0..76ddefa61 100644 --- a/service/dependency_test.go +++ b/service/dependency_test.go @@ -7,6 +7,7 @@ import ( "github.com/docker/docker/pkg/stdcopy" "github.com/mesg-foundation/core/container" + "github.com/mesg-foundation/core/container/mocks" "github.com/stretchr/testify/require" ) @@ -34,12 +35,13 @@ func testDependencyLogs(t *testing.T, do func(s *Service, c container.Container, go wstd.Write(stdData) go werr.Write(errData) - s, mc := newFromServiceAndContainerMocks(t, &Service{ + s := &Service{ Hash: "1", Dependencies: []*Dependency{ {Key: dependencyKey}, }, - }) + } + mc := &mocks.Container{} d, _ := s.getDependency(dependencyKey) mc.On("ServiceLogs", d.namespace(s.namespace())).Once().Return(rp, nil) diff --git a/service/service_test.go b/service/service_test.go index 739c00802..5c0baab38 100644 --- a/service/service_test.go +++ b/service/service_test.go @@ -10,11 +10,6 @@ import ( "github.com/stretchr/testify/require" ) -func newFromServiceAndContainerMocks(t *testing.T, s *Service) (*Service, *mocks.Container) { - _ = t - return s, &mocks.Container{} -} - func TestNew(t *testing.T) { var ( path = "../service-test/task" diff --git a/service/start_test.go b/service/start_test.go index cc6bc49c3..c87049273 100644 --- a/service/start_test.go +++ b/service/start_test.go @@ -86,7 +86,7 @@ func TestStartService(t *testing.T) { Sid = "SidStartService" networkID = "3" sharedNetworkID = "4" - s, mc = newFromServiceAndContainerMocks(t, &Service{ + s = &Service{ Hash: "1", Name: serviceName, Sid: Sid, @@ -96,7 +96,8 @@ func TestStartService(t *testing.T) { Image: "http-server", }, }, - }) + } + mc = &mocks.Container{} ) d, _ := s.getDependency(dependencyKey) @@ -124,7 +125,7 @@ func TestStartWith2Dependencies(t *testing.T) { networkID = "7" sharedNetworkID = "8" serviceName = "TestStartWith2Dependencies" - s, mc = newFromServiceAndContainerMocks(t, &Service{ + s = &Service{ Hash: "1", Name: serviceName, Dependencies: []*Dependency{ @@ -137,7 +138,8 @@ func TestStartWith2Dependencies(t *testing.T) { Image: dependencyImage2, }, }, - }) + } + mc = &mocks.Container{} ) var ( @@ -169,7 +171,7 @@ func TestStartWith2Dependencies(t *testing.T) { func TestStartServiceRunning(t *testing.T) { var ( dependencyKey = "1" - s, mc = newFromServiceAndContainerMocks(t, &Service{ + s = &Service{ Hash: "1", Dependencies: []*Dependency{ { @@ -177,7 +179,8 @@ func TestStartServiceRunning(t *testing.T) { Image: "2", }, }, - }) + } + mc = &mocks.Container{} ) d, _ := s.getDependency(dependencyKey) @@ -197,7 +200,7 @@ func TestPartiallyRunningService(t *testing.T) { networkID = "3" sharedNetworkID = "4" containerServiceIDs = []string{"5", "6"} - s, mc = newFromServiceAndContainerMocks(t, &Service{ + s = &Service{ Hash: "1", Name: "TestPartiallyRunningService", Dependencies: []*Dependency{ @@ -210,7 +213,8 @@ func TestPartiallyRunningService(t *testing.T) { Image: "http-server", }, }, - }) + } + mc = &mocks.Container{} ) var ( @@ -248,7 +252,7 @@ func TestStartDependency(t *testing.T) { networkID = "3" sharedNetworkID = "4" containerServiceID = "5" - s, mc = newFromServiceAndContainerMocks(t, &Service{ + s = &Service{ Hash: "1", Name: "TestStartDependency", Dependencies: []*Dependency{ @@ -257,7 +261,8 @@ func TestStartDependency(t *testing.T) { Image: "http-server", }, }, - }) + } + mc = &mocks.Container{} ) d, _ := s.getDependency(dependencyKey) @@ -278,7 +283,7 @@ func TestServiceStartError(t *testing.T) { networkID = "3" sharedNetworkID = "4" startErr = errors.New("ops") - s, mc = newFromServiceAndContainerMocks(t, &Service{ + s = &Service{ Hash: "1", Name: "TestNetworkCreated", Dependencies: []*Dependency{ @@ -287,7 +292,8 @@ func TestServiceStartError(t *testing.T) { Image: "http-server", }, }, - }) + } + mc = &mocks.Container{} ) d, _ := s.getDependency(dependencyKey) diff --git a/service/status_test.go b/service/status_test.go index 34bf28022..6599b19a0 100644 --- a/service/status_test.go +++ b/service/status_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/mesg-foundation/core/container" + "github.com/mesg-foundation/core/container/mocks" "github.com/stretchr/testify/require" ) @@ -12,7 +13,7 @@ func TestUnknownServiceStatus(t *testing.T) { var ( dependencyKey = "1" statusErr = errors.New("ops") - s, mc = newFromServiceAndContainerMocks(t, &Service{ + s = &Service{ Hash: "1", Name: "TestUnknownServiceStatus", Dependencies: []*Dependency{ @@ -21,7 +22,8 @@ func TestUnknownServiceStatus(t *testing.T) { Image: "http-server", }, }, - }) + } + mc = &mocks.Container{} ) d, _ := s.getDependency(dependencyKey) @@ -38,7 +40,7 @@ func TestUnknownServiceStatus(t *testing.T) { func TestStoppedServiceStatus(t *testing.T) { var ( dependencyKey = "1" - s, mc = newFromServiceAndContainerMocks(t, &Service{ + s = &Service{ Hash: "1", Name: "TestStoppedServiceStatus", Dependencies: []*Dependency{ @@ -47,7 +49,8 @@ func TestStoppedServiceStatus(t *testing.T) { Image: "http-server", }, }, - }) + } + mc = &mocks.Container{} ) d, _ := s.getDependency(dependencyKey) @@ -64,7 +67,7 @@ func TestStoppedServiceStatus(t *testing.T) { func TestRunningServiceStatus(t *testing.T) { var ( dependencyKey = "1" - s, mc = newFromServiceAndContainerMocks(t, &Service{ + s = &Service{ Hash: "1", Name: "TestRunningServiceStatus", Dependencies: []*Dependency{ @@ -73,7 +76,8 @@ func TestRunningServiceStatus(t *testing.T) { Image: "http-server", }, }, - }) + } + mc = &mocks.Container{} ) d, _ := s.getDependency(dependencyKey) @@ -91,7 +95,7 @@ func TestPartialServiceStatus(t *testing.T) { var ( dependencyKey = "1" dependencyKey2 = "2" - s, mc = newFromServiceAndContainerMocks(t, &Service{ + s = &Service{ Hash: "1", Name: "TestPartialServiceStatus", Dependencies: []*Dependency{ @@ -104,7 +108,8 @@ func TestPartialServiceStatus(t *testing.T) { Image: "http-server", }, }, - }) + } + mc = &mocks.Container{} ) var ( @@ -125,7 +130,7 @@ func TestPartialServiceStatus(t *testing.T) { func TestDependencyStatus(t *testing.T) { var ( dependencyKey = "1" - s, mc = newFromServiceAndContainerMocks(t, &Service{ + s = &Service{ Hash: "1", Name: "TestDependencyStatus", Dependencies: []*Dependency{ @@ -134,7 +139,8 @@ func TestDependencyStatus(t *testing.T) { Image: "http-server", }, }, - }) + } + mc = &mocks.Container{} ) d, _ := s.getDependency(dependencyKey) diff --git a/service/stop_test.go b/service/stop_test.go index 628406934..c808ec961 100644 --- a/service/stop_test.go +++ b/service/stop_test.go @@ -4,13 +4,14 @@ import ( "testing" "github.com/mesg-foundation/core/container" + "github.com/mesg-foundation/core/container/mocks" "github.com/stretchr/testify/require" ) func TestStopRunningService(t *testing.T) { var ( dependencyKey = "1" - s, mc = newFromServiceAndContainerMocks(t, &Service{ + s = &Service{ Hash: "1", Name: "TestStopRunningService", Dependencies: []*Dependency{ @@ -19,7 +20,8 @@ func TestStopRunningService(t *testing.T) { Image: "http-server", }, }, - }) + } + mc = &mocks.Container{} ) d, _ := s.getDependency(dependencyKey) @@ -37,7 +39,7 @@ func TestStopRunningService(t *testing.T) { func TestStopDependency(t *testing.T) { var ( dependencyKey = "1" - s, mc = newFromServiceAndContainerMocks(t, &Service{ + s = &Service{ Hash: "1", Name: "TestStopService", Dependencies: []*Dependency{ @@ -46,7 +48,8 @@ func TestStopDependency(t *testing.T) { Image: "http-server", }, }, - }) + } + mc = &mocks.Container{} ) d, _ := s.getDependency(dependencyKey) diff --git a/service/volume_test.go b/service/volume_test.go index 0be160e92..7fdb1d24f 100644 --- a/service/volume_test.go +++ b/service/volume_test.go @@ -3,6 +3,7 @@ package service import ( "testing" + "github.com/mesg-foundation/core/container/mocks" "github.com/stretchr/testify/require" ) @@ -12,7 +13,7 @@ func TestDeleteVolumes(t *testing.T) { dependencyKey2 = "2" volumeA = "a" volumeB = "b" - s, mc = newFromServiceAndContainerMocks(t, &Service{ + s = &Service{ Name: "TestCreateVolumes", Dependencies: []*Dependency{ { @@ -26,7 +27,8 @@ func TestDeleteVolumes(t *testing.T) { VolumesFrom: []string{dependencyKey1}, }, }, - }) + } + mc = &mocks.Container{} ) var ( From 6160ca93a7aa775db8453839b461fe972b9c6dec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=CC=87lker=20Go=CC=88ktug=CC=86=20O=CC=88ztu=CC=88rk?= Date: Thu, 11 Apr 2019 12:40:09 +0300 Subject: [PATCH 11/20] service: cosmetic updates on tests --- service/dependency_test.go | 16 +- service/event_test.go | 2 + service/logs_integration_test.go | 62 +++---- service/service_test.go | 17 +- service/start_integration_test.go | 253 ++++++++++++++++------------- service/status_integration_test.go | 100 +++++++----- service/stop_integration_test.go | 92 ++++++----- service/volume_integration_test.go | 1 + 8 files changed, 303 insertions(+), 240 deletions(-) diff --git a/service/dependency_test.go b/service/dependency_test.go index 76ddefa61..08918f9f4 100644 --- a/service/dependency_test.go +++ b/service/dependency_test.go @@ -35,13 +35,15 @@ func testDependencyLogs(t *testing.T, do func(s *Service, c container.Container, go wstd.Write(stdData) go werr.Write(errData) - s := &Service{ - Hash: "1", - Dependencies: []*Dependency{ - {Key: dependencyKey}, - }, - } - mc := &mocks.Container{} + var ( + s = &Service{ + Hash: "1", + Dependencies: []*Dependency{ + {Key: dependencyKey}, + }, + } + mc = &mocks.Container{} + ) d, _ := s.getDependency(dependencyKey) mc.On("ServiceLogs", d.namespace(s.namespace())).Once().Return(rp, nil) diff --git a/service/event_test.go b/service/event_test.go index af6ecbecb..48935739d 100644 --- a/service/event_test.go +++ b/service/event_test.go @@ -15,6 +15,7 @@ func TestGetEvent(t *testing.T) { }, } ) + e, err := s.GetEvent(eventKey) require.NoError(t, err) require.Equal(t, eventKey, e.Key) @@ -31,6 +32,7 @@ func TestGetEventNonExistent(t *testing.T) { }, } ) + e, err := s.GetEvent(eventKey) require.Zero(t, e) require.Equal(t, &EventNotFoundError{ diff --git a/service/logs_integration_test.go b/service/logs_integration_test.go index 15d19d5fa..562b82875 100644 --- a/service/logs_integration_test.go +++ b/service/logs_integration_test.go @@ -9,21 +9,24 @@ import ( ) func TestIntegrationLogs(t *testing.T) { - service := &Service{ - Hash: "1", - Name: "TestLogs", - Dependencies: []*Dependency{ - { - Key: "test", - Image: "http-server", + var ( + service = &Service{ + Hash: "1", + Name: "TestLogs", + Dependencies: []*Dependency{ + { + Key: "test", + Image: "http-server", + }, + { + Key: "test2", + Image: "http-server", + }, }, - { - Key: "test2", - Image: "http-server", - }, - }, - } - c := newIntegrationContainer(t) + } + c = newIntegrationContainer(t) + ) + service.Start(c) defer service.Stop(c) readers, err := service.Logs(c) @@ -32,21 +35,24 @@ func TestIntegrationLogs(t *testing.T) { } func TestIntegrationLogsOnlyOneDependency(t *testing.T) { - service := &Service{ - Hash: "1", - Name: "TestLogsOnlyOneDependency", - Dependencies: []*Dependency{ - { - Key: "test", - Image: "http-server", + var ( + service = &Service{ + Hash: "1", + Name: "TestLogsOnlyOneDependency", + Dependencies: []*Dependency{ + { + Key: "test", + Image: "http-server", + }, + { + Key: "test2", + Image: "http-server", + }, }, - { - Key: "test2", - Image: "http-server", - }, - }, - } - c := newIntegrationContainer(t) + } + c = newIntegrationContainer(t) + ) + service.Start(c) defer service.Stop(c) readers, err := service.Logs(c, "test2") diff --git a/service/service_test.go b/service/service_test.go index 5c0baab38..aefb24da7 100644 --- a/service/service_test.go +++ b/service/service_test.go @@ -14,9 +14,9 @@ func TestNew(t *testing.T) { var ( path = "../service-test/task" hash = "1" + mc = &mocks.Container{} ) - mc := &mocks.Container{} mc.On("Build", mock.Anything).Once().Return(hash, nil) statuses := make(chan DeployStatus, 4) @@ -45,9 +45,9 @@ func TestNewWithDefaultEnv(t *testing.T) { path = "../service-test/env" hash = "1" env = []string{"A=1", "B=2"} + mc = &mocks.Container{} ) - mc := &mocks.Container{} mc.On("Build", mock.Anything).Once().Return(hash, nil) s, err := New(path, mc, nil, nil) @@ -64,9 +64,9 @@ func TestNewWithOverwrittenEnv(t *testing.T) { path = "../service-test/env" hash = "1" env = []string{"A=3", "B=4"} + mc = &mocks.Container{} ) - mc := &mocks.Container{} mc.On("Build", mock.Anything).Once().Return(hash, nil) s, err := New(path, mc, nil, xos.EnvSliceToMap(env)) @@ -81,10 +81,9 @@ func TestNewWithOverwrittenEnv(t *testing.T) { func TestNewWitNotDefinedEnv(t *testing.T) { var ( path = "../service-test/task" + mc = &mocks.Container{} ) - mc := &mocks.Container{} - _, err := New(path, mc, nil, xos.EnvSliceToMap([]string{"A=1", "B=2"})) require.Equal(t, ErrNotDefinedEnv{[]string{"A", "B"}}, err) @@ -97,8 +96,11 @@ func TestErrNotDefinedEnv(t *testing.T) { } func TestInjectDefinitionWithConfig(t *testing.T) { - command := "xxx" - s := &Service{} + var ( + command = "xxx" + s = &Service{} + ) + s.injectDefinition(&importer.ServiceDefinition{ Configuration: &importer.Dependency{ Command: command, @@ -112,6 +114,7 @@ func TestInjectDefinitionWithDependency(t *testing.T) { s = &Service{} image = "xxx" ) + s.injectDefinition(&importer.ServiceDefinition{ Dependencies: map[string]*importer.Dependency{ "test": { diff --git a/service/start_integration_test.go b/service/start_integration_test.go index 41259e2a6..ffa5baafc 100644 --- a/service/start_integration_test.go +++ b/service/start_integration_test.go @@ -10,17 +10,19 @@ import ( ) func TestIntegrationStartServiceIntegration(t *testing.T) { - service := &Service{ - Hash: "1", - Name: "TestStartService", - Dependencies: []*Dependency{ - { - Key: "test", - Image: "http-server", + var ( + service = &Service{ + Hash: "1", + Name: "TestStartService", + Dependencies: []*Dependency{ + { + Key: "test", + Image: "http-server", + }, }, - }, - } - c := newIntegrationContainer(t) + } + c = newIntegrationContainer(t) + ) dockerServices, err := service.Start(c) defer service.Stop(c) require.NoError(t, err) @@ -30,21 +32,23 @@ func TestIntegrationStartServiceIntegration(t *testing.T) { } func TestIntegrationStartWith2DependenciesIntegration(t *testing.T) { - service := &Service{ - Hash: "1", - Name: "TestStartWith2Dependencies", - Dependencies: []*Dependency{ - { - Key: "testa", - Image: "http-server:latest", + var ( + service = &Service{ + Hash: "1", + Name: "TestStartWith2Dependencies", + Dependencies: []*Dependency{ + { + Key: "testa", + Image: "http-server:latest", + }, + { + Key: "testb", + Image: "sleep:latest", + }, }, - { - Key: "testb", - Image: "sleep:latest", - }, - }, - } - c := newIntegrationContainer(t) + } + c = newIntegrationContainer(t) + ) servicesID, err := service.Start(c) defer service.Stop(c) require.NoError(t, err) @@ -59,17 +63,20 @@ func TestIntegrationStartWith2DependenciesIntegration(t *testing.T) { } func TestIntegrationStartAgainService(t *testing.T) { - service := &Service{ - Hash: "1", - Name: "TestStartAgainService", - Dependencies: []*Dependency{ - { - Key: "test", - Image: "http-server", + var ( + service = &Service{ + Hash: "1", + Name: "TestStartAgainService", + Dependencies: []*Dependency{ + { + Key: "test", + Image: "http-server", + }, }, - }, - } - c := newIntegrationContainer(t) + } + c = newIntegrationContainer(t) + ) + service.Start(c) defer service.Stop(c) dockerServices, err := service.Start(c) @@ -81,20 +88,23 @@ func TestIntegrationStartAgainService(t *testing.T) { // // TODO: Disable this test in order to have the CI working // func TestIntegrationPartiallyRunningService(t *testing.T) { -// service := &Service{ -// Name: "TestPartiallyRunningService", -// Dependencies: []*Dependency{ -// { -// Key: "testa", -// Image: "http-server", -// }, -// { -// Key: "testb", -// Image: "http-server", +// var ( +// service = &Service{ +// Name: "TestPartiallyRunningService", +// Dependencies: []*Dependency{ +// { +// Key: "testa", +// Image: "http-server", +// }, +// { +// Key: "testb", +// Image: "http-server", +// }, // }, -// }, -// } -// c := newIntegrationContainer(t) +// } +// c = newIntegrationContainer(t) +// ) + // service.Start(c) // defer service.Stop(c) // service.Dependencies[0].Stop(c) @@ -108,17 +118,20 @@ func TestIntegrationStartAgainService(t *testing.T) { // } func TestIntegrationStartDependency(t *testing.T) { - service := &Service{ - Hash: "1", - Name: "TestStartDependency", - Dependencies: []*Dependency{ - { - Key: "test", - Image: "http-server", + var ( + service = &Service{ + Hash: "1", + Name: "TestStartDependency", + Dependencies: []*Dependency{ + { + Key: "test", + Image: "http-server", + }, }, - }, - } - c := newIntegrationContainer(t) + } + c = newIntegrationContainer(t) + ) + networkID, err := c.CreateNetwork(service.namespace()) require.NoError(t, err) defer c.DeleteNetwork(service.namespace()) @@ -132,17 +145,20 @@ func TestIntegrationStartDependency(t *testing.T) { } func TestIntegrationNetworkCreated(t *testing.T) { - service := &Service{ - Hash: "1", - Name: "TestNetworkCreated", - Dependencies: []*Dependency{ - { - Key: "test", - Image: "http-server", + var ( + service = &Service{ + Hash: "1", + Name: "TestNetworkCreated", + Dependencies: []*Dependency{ + { + Key: "test", + Image: "http-server", + }, }, - }, - } - c := newIntegrationContainer(t) + } + c = newIntegrationContainer(t) + ) + service.Start(c) defer service.Stop(c) network, err := c.FindNetwork(service.namespace()) @@ -152,17 +168,20 @@ func TestIntegrationNetworkCreated(t *testing.T) { // Test for https://github.com/mesg-foundation/core/issues/88 func TestIntegrationStartStopStart(t *testing.T) { - service := &Service{ - Hash: "1", - Name: "TestStartStopStart", - Dependencies: []*Dependency{ - { - Key: "test", - Image: "http-server", + var ( + service = &Service{ + Hash: "1", + Name: "TestStartStopStart", + Dependencies: []*Dependency{ + { + Key: "test", + Image: "http-server", + }, }, - }, - } - c := newIntegrationContainer(t) + } + c = newIntegrationContainer(t) + ) + service.Start(c) service.Stop(c) dockerServices, err := service.Start(c) @@ -174,29 +193,32 @@ func TestIntegrationStartStopStart(t *testing.T) { } func TestIntegrationServiceDependenciesListensFromSamePort(t *testing.T) { - service := &Service{ - Hash: "1", - Name: "TestServiceDependenciesListensFromSamePort", - Dependencies: []*Dependency{ - { - Key: "test", - Image: "http-server", - Ports: []string{"80"}, + var ( + service = &Service{ + Hash: "1", + Name: "TestServiceDependenciesListensFromSamePort", + Dependencies: []*Dependency{ + { + Key: "test", + Image: "http-server", + Ports: []string{"80"}, + }, }, - }, - } - service1 := &Service{ - Hash: "2", - Name: "TestServiceDependenciesListensFromSamePort1", - Dependencies: []*Dependency{ - { - Key: "test", - Image: "http-server", - Ports: []string{"80"}, + } + service1 = &Service{ + Hash: "2", + Name: "TestServiceDependenciesListensFromSamePort1", + Dependencies: []*Dependency{ + { + Key: "test", + Image: "http-server", + Ports: []string{"80"}, + }, }, - }, - } - c := newIntegrationContainer(t) + } + c = newIntegrationContainer(t) + ) + _, err := service.Start(c) require.NoError(t, err) defer service.Stop(c) @@ -207,23 +229,26 @@ func TestIntegrationServiceDependenciesListensFromSamePort(t *testing.T) { } func TestStartWithSamePorts(t *testing.T) { - service := &Service{ - Hash: "1", - Name: "TestStartWithSamePorts", - Dependencies: []*Dependency{ - { - Key: "1", - Image: "nginx", - Ports: []string{"80"}, + var ( + service = &Service{ + Hash: "1", + Name: "TestStartWithSamePorts", + Dependencies: []*Dependency{ + { + Key: "1", + Image: "nginx", + Ports: []string{"80"}, + }, + { + Key: "2", + Image: "nginx", + Ports: []string{"80"}, + }, }, - { - Key: "2", - Image: "nginx", - Ports: []string{"80"}, - }, - }, - } - c := newIntegrationContainer(t) + } + c = newIntegrationContainer(t) + ) + _, err := service.Start(c) require.Error(t, err) } diff --git a/service/status_integration_test.go b/service/status_integration_test.go index ef2fcecd3..5a3ef08f0 100644 --- a/service/status_integration_test.go +++ b/service/status_integration_test.go @@ -10,17 +10,20 @@ import ( ) func TestIntegrationStatusService(t *testing.T) { - service := &Service{ - Hash: "1", - Name: "TestStatusService", - Dependencies: []*Dependency{ - { - Key: "test", - Image: "http-server", + var ( + service = &Service{ + Hash: "1", + Name: "TestStatusService", + Dependencies: []*Dependency{ + { + Key: "test", + Image: "http-server", + }, }, - }, - } - c := newIntegrationContainer(t) + } + c = newIntegrationContainer(t) + ) + status, err := service.Status(c) require.NoError(t, err) require.Equal(t, STOPPED, status) @@ -34,17 +37,20 @@ func TestIntegrationStatusService(t *testing.T) { } func TestIntegrationStatusDependency(t *testing.T) { - service := &Service{ - Hash: "1", - Name: "TestStatusDependency", - Dependencies: []*Dependency{ - { - Key: "test", - Image: "http-server", + var ( + service = &Service{ + Hash: "1", + Name: "TestStatusDependency", + Dependencies: []*Dependency{ + { + Key: "test", + Image: "http-server", + }, }, - }, - } - c := newIntegrationContainer(t) + } + c = newIntegrationContainer(t) + ) + dep := service.Dependencies[0] status, err := dep.Status(c, service) require.NoError(t, err) @@ -59,17 +65,20 @@ func TestIntegrationStatusDependency(t *testing.T) { } func TestIntegrationListRunning(t *testing.T) { - service := &Service{ - Hash: "1", - Name: "TestList", - Dependencies: []*Dependency{ - { - Key: "test", - Image: "http-server", + var ( + service = &Service{ + Hash: "1", + Name: "TestList", + Dependencies: []*Dependency{ + { + Key: "test", + Image: "http-server", + }, }, - }, - } - c := newIntegrationContainer(t) + } + c = newIntegrationContainer(t) + ) + service.Start(c) defer service.Stop(c) list, err := ListRunning() @@ -79,21 +88,24 @@ func TestIntegrationListRunning(t *testing.T) { } func TestIntegrationListRunningMultipleDependencies(t *testing.T) { - service := &Service{ - Hash: "1", - Name: "TestListMultipleDependencies", - Dependencies: []*Dependency{ - { - Key: "test", - Image: "http-server", + var ( + service = &Service{ + Hash: "1", + Name: "TestListMultipleDependencies", + Dependencies: []*Dependency{ + { + Key: "test", + Image: "http-server", + }, + { + Key: "test2", + Image: "http-server", + }, }, - { - Key: "test2", - Image: "http-server", - }, - }, - } - c := newIntegrationContainer(t) + } + c = newIntegrationContainer(t) + ) + service.Start(c) defer service.Stop(c) list, err := ListRunning() diff --git a/service/stop_integration_test.go b/service/stop_integration_test.go index 2c65837a3..78d668d6a 100644 --- a/service/stop_integration_test.go +++ b/service/stop_integration_test.go @@ -10,17 +10,20 @@ import ( ) func TestIntegrationStopRunningService(t *testing.T) { - service := &Service{ - Hash: "1", - Name: "TestStopRunningService", - Dependencies: []*Dependency{ - { - Key: "test", - Image: "http-server", + var ( + service = &Service{ + Hash: "1", + Name: "TestStopRunningService", + Dependencies: []*Dependency{ + { + Key: "test", + Image: "http-server", + }, }, - }, - } - c := newIntegrationContainer(t) + } + c = newIntegrationContainer(t) + ) + service.Start(c) err := service.Stop(c) require.NoError(t, err) @@ -29,17 +32,20 @@ func TestIntegrationStopRunningService(t *testing.T) { } func TestIntegrationStopNonRunningService(t *testing.T) { - service := &Service{ - Hash: "1", - Name: "TestStopNonRunningService", - Dependencies: []*Dependency{ - { - Key: "test", - Image: "http-server", + var ( + service = &Service{ + Hash: "1", + Name: "TestStopNonRunningService", + Dependencies: []*Dependency{ + { + Key: "test", + Image: "http-server", + }, }, - }, - } - c := newIntegrationContainer(t) + } + c = newIntegrationContainer(t) + ) + err := service.Stop(c) require.NoError(t, err) status, _ := service.Status(c) @@ -47,17 +53,20 @@ func TestIntegrationStopNonRunningService(t *testing.T) { } func TestIntegrationStopDependency(t *testing.T) { - service := &Service{ - Hash: "1", - Name: "TestStopDependency", - Dependencies: []*Dependency{ - { - Key: "test", - Image: "http-server", + var ( + service = &Service{ + Hash: "1", + Name: "TestStopDependency", + Dependencies: []*Dependency{ + { + Key: "test", + Image: "http-server", + }, }, - }, - } - c := newIntegrationContainer(t) + } + c = newIntegrationContainer(t) + ) + networkID, err := c.CreateNetwork(service.namespace()) require.NoError(t, err) defer c.DeleteNetwork(service.namespace()) @@ -70,17 +79,20 @@ func TestIntegrationStopDependency(t *testing.T) { } func TestIntegrationNetworkDeleted(t *testing.T) { - service := &Service{ - Hash: "1", - Name: "TestNetworkDeleted", - Dependencies: []*Dependency{ - { - Key: "test", - Image: "http-server", + var ( + service = &Service{ + Hash: "1", + Name: "TestNetworkDeleted", + Dependencies: []*Dependency{ + { + Key: "test", + Image: "http-server", + }, }, - }, - } - c := newIntegrationContainer(t) + } + c = newIntegrationContainer(t) + ) + service.Start(c) service.Stop(c) n, err := c.FindNetwork(service.namespace()) diff --git a/service/volume_integration_test.go b/service/volume_integration_test.go index 69d98f272..dada5fdf7 100644 --- a/service/volume_integration_test.go +++ b/service/volume_integration_test.go @@ -35,6 +35,7 @@ func TestIntegrationDeleteVolumes(t *testing.T) { } c = newIntegrationContainer(t) ) + _, err := s.Start(c) require.NoError(t, err) err = s.Stop(c) From d0f45ef12c6c4e545b229ecf07f53022fb93cae8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=CC=87lker=20Go=CC=88ktug=CC=86=20O=CC=88ztu=CC=88rk?= Date: Thu, 11 Apr 2019 12:47:31 +0300 Subject: [PATCH 12/20] api: make New to receive Container as a required param * drop support for receving Options as params since there is no need anymore. --- api/api.go | 27 +++++---------------------- api/api_test.go | 3 +-- core/main.go | 5 +---- interface/grpc/core/test_test.go | 3 +-- interface/grpc/service/test_test.go | 4 +--- 5 files changed, 9 insertions(+), 33 deletions(-) diff --git a/api/api.go b/api/api.go index 548eb8db8..4b679966a 100644 --- a/api/api.go +++ b/api/api.go @@ -19,29 +19,12 @@ type API struct { container container.Container } -// Option is a configuration func for MESG. -type Option func(*API) - // New creates a new API with given options. -func New(db database.ServiceDB, execDB database.ExecutionDB, options ...Option) (*API, error) { - a := &API{db: db, execDB: execDB} - for _, option := range options { - option(a) - } - if a.container == nil { - var err error - a.container, err = container.New() - if err != nil { - return nil, err - } - } - return a, nil -} - -// ContainerOption configures underlying container access API. -func ContainerOption(container container.Container) Option { - return func(a *API) { - a.container = container +func New(c container.Container, db database.ServiceDB, execDB database.ExecutionDB) *API { + return &API{ + container: c, + db: db, + execDB: execDB, } } diff --git a/api/api_test.go b/api/api_test.go index cc32413fa..051eea9e7 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -40,8 +40,7 @@ func newTesting(t *testing.T) (*API, *apiTesting) { execDB, err := database.NewExecutionDB(execdbname) require.NoError(t, err) - a, err := New(db, execDB, ContainerOption(containerMock)) - require.NoError(t, err) + a := New(containerMock, db, execDB) return a, &apiTesting{ T: t, diff --git a/core/main.go b/core/main.go index e240f806c..30c4c10f2 100644 --- a/core/main.go +++ b/core/main.go @@ -50,10 +50,7 @@ func initDependencies() (*dependencies, error) { } // init api. - api, err := api.New(serviceDB, executionDB, api.ContainerOption(c)) - if err != nil { - return nil, err - } + api := api.New(c, serviceDB, executionDB) return &dependencies{ config: config, diff --git a/interface/grpc/core/test_test.go b/interface/grpc/core/test_test.go index 0098b66e5..e29e12a7f 100644 --- a/interface/grpc/core/test_test.go +++ b/interface/grpc/core/test_test.go @@ -31,8 +31,7 @@ func newServerWithContainer(t *testing.T, c container.Container) (*Server, func( execDB, err := database.NewExecutionDB(execdbname) require.NoError(t, err) - a, err := api.New(db, execDB, api.ContainerOption(c)) - require.NoError(t, err) + a := api.New(c, db, execDB) server := NewServer(a) diff --git a/interface/grpc/service/test_test.go b/interface/grpc/service/test_test.go index 787f3b94b..bbb6a0e9f 100644 --- a/interface/grpc/service/test_test.go +++ b/interface/grpc/service/test_test.go @@ -30,9 +30,7 @@ func newServer(t *testing.T) (*Server, func()) { execDB, err := database.NewExecutionDB(execdbname) require.NoError(t, err) - a, err := api.New(db, execDB) - require.NoError(t, err) - + a := api.New(nil, db, execDB) server := NewServer(a) closer := func() { From 45a769280b8c71d225265cd131c611fe975f2341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=CC=87lker=20Go=CC=88ktug=CC=86=20O=CC=88ztu=CC=88rk?= Date: Thu, 11 Apr 2019 15:36:03 +0300 Subject: [PATCH 13/20] interface/grpc/service: fix tests --- interface/grpc/service/test_test.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/interface/grpc/service/test_test.go b/interface/grpc/service/test_test.go index bbb6a0e9f..af0bca3d5 100644 --- a/interface/grpc/service/test_test.go +++ b/interface/grpc/service/test_test.go @@ -9,6 +9,7 @@ import ( "github.com/docker/docker/pkg/archive" "github.com/mesg-foundation/core/api" + "github.com/mesg-foundation/core/container" "github.com/mesg-foundation/core/database" "github.com/stretchr/testify/require" ) @@ -30,7 +31,10 @@ func newServer(t *testing.T) (*Server, func()) { execDB, err := database.NewExecutionDB(execdbname) require.NoError(t, err) - a := api.New(nil, db, execDB) + c, err := container.New() + require.NoError(t, err) + + a := api.New(c, db, execDB) server := NewServer(a) closer := func() { From a6b68f481a4900205934c2fad63c8560ee002e88 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Fri, 12 Apr 2019 10:35:17 +1000 Subject: [PATCH 14/20] update start function --- service/start.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/service/start.go b/service/start.go index 3bc55c040..1986323c7 100644 --- a/service/start.go +++ b/service/start.go @@ -27,24 +27,29 @@ func (s *Service) Start(c container.Container) (serviceIDs []string, err error) if err != nil { return nil, err } + + start := func(dep *Dependency) error { + serviceID, err := dep.Start(c, s, networkID) + if err != nil { + s.Stop(c) + return err + } + serviceIDs = append(serviceIDs, serviceID) + return nil + } + // BUG: https://github.com/mesg-foundation/core/issues/382 // After solving this by docker, switch back to deploy in parallel serviceIDs = make([]string, 0) for _, dep := range s.Dependencies { - serviceID, err := dep.Start(c, s, networkID) - if err != nil { - s.Stop(c) + if err := start(dep); err != nil { return nil, err } - serviceIDs = append(serviceIDs, serviceID) } if s.Configuration != nil { - serviceID, err := s.Configuration.Start(c, s, networkID) - if err != nil { - s.Stop(c) + if err := start(s.Configuration); err != nil { return nil, err } - serviceIDs = append(serviceIDs, serviceID) } return serviceIDs, err } From bac69d2b50e13687c7861d2b0f89b4739fa30b0e Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Fri, 12 Apr 2019 10:38:13 +1000 Subject: [PATCH 15/20] fix issues with previous services --- service/service.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/service/service.go b/service/service.go index f1a99a62c..95f5d3f32 100644 --- a/service/service.go +++ b/service/service.go @@ -157,14 +157,14 @@ func (s *Service) closeStatus(statuses chan DeployStatus) { // getDependency returns dependency dependencyKey or a not found error. func (s *Service) getDependency(dependencyKey string) (*Dependency, error) { - if dependencyKey == MainServiceKey { - return s.Configuration, nil - } for _, dep := range s.Dependencies { if dep.Key == dependencyKey { return dep, nil } } + if dependencyKey == MainServiceKey { + return s.Configuration, nil + } return nil, fmt.Errorf("dependency %s do not exist", dependencyKey) } From 9374808bb2c6aef92f1efd7dc62900aba0392936 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Fri, 12 Apr 2019 11:08:24 +1000 Subject: [PATCH 16/20] remove hack for dependencies needed for the volume from --- service/service.go | 3 --- service/start.go | 6 +++++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/service/service.go b/service/service.go index 95f5d3f32..b4dbca39b 100644 --- a/service/service.go +++ b/service/service.go @@ -162,9 +162,6 @@ func (s *Service) getDependency(dependencyKey string) (*Dependency, error) { return dep, nil } } - if dependencyKey == MainServiceKey { - return s.Configuration, nil - } return nil, fmt.Errorf("dependency %s do not exist", dependencyKey) } diff --git a/service/start.go b/service/start.go index 1986323c7..af0a2fa83 100644 --- a/service/start.go +++ b/service/start.go @@ -130,7 +130,11 @@ func (d *Dependency) extractVolumesFrom(s *Service) ([]container.Mount, error) { for _, depName := range d.VolumesFrom { dep, err := s.getDependency(depName) if err != nil { - return nil, err + if depName == MainServiceKey { + dep = s.Configuration + } else { + return nil, err + } } for _, volume := range dep.Volumes { volumesFrom = append(volumesFrom, container.Mount{ From 31f7dd194d25fcddcac7c8b1b5cfa32c07775d4e Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Sat, 13 Apr 2019 11:38:53 +1000 Subject: [PATCH 17/20] improve error message --- interface/grpc/core/core.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/interface/grpc/core/core.go b/interface/grpc/core/core.go index 9036209c1..4711c6fce 100644 --- a/interface/grpc/core/core.go +++ b/interface/grpc/core/core.go @@ -3,6 +3,7 @@ package core import ( "context" "encoding/json" + "fmt" "sync" "github.com/mesg-foundation/core/api" @@ -188,7 +189,7 @@ func (s *Server) ListenResult(request *coreapi.ListenResultRequest, stream corea func (s *Server) ExecuteTask(ctx context.Context, request *coreapi.ExecuteTaskRequest) (*coreapi.ExecuteTaskReply, error) { var inputs map[string]interface{} if err := json.Unmarshal([]byte(request.InputData), &inputs); err != nil { - return nil, err + return nil, fmt.Errorf("cannot parse execution's inputs (JSON format): %s", err) } executionID, err := s.api.ExecuteTask(request.ServiceID, request.TaskKey, inputs, request.ExecutionTags) From 07d67ef6483770dbe8fb8f459e869bf36d5c1899 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Sat, 13 Apr 2019 11:53:56 +1000 Subject: [PATCH 18/20] fix tests --- interface/grpc/core/core_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interface/grpc/core/core_test.go b/interface/grpc/core/core_test.go index 12c732f0a..5daebdc66 100644 --- a/interface/grpc/core/core_test.go +++ b/interface/grpc/core/core_test.go @@ -160,7 +160,7 @@ func TestExecuteWithInvalidJSON(t *testing.T) { InputData: "", }) require.Error(t, err) - require.Equal(t, err.Error(), "unexpected end of JSON input") + require.Equal(t, err.Error(), "cannot parse execution's inputs (JSON format): unexpected end of JSON input") } func TestExecuteWithInvalidTask(t *testing.T) { From 19eba37638cd8a1aba346ed1f1f33f88ae434a08 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Sun, 14 Apr 2019 22:58:59 +1000 Subject: [PATCH 19/20] fix bug with hash with extra data --- utils/dirhash/dirhash.go | 3 ++- utils/dirhash/dirhash_test.go | 9 +++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/utils/dirhash/dirhash.go b/utils/dirhash/dirhash.go index 39304dbf0..7113235da 100644 --- a/utils/dirhash/dirhash.go +++ b/utils/dirhash/dirhash.go @@ -92,5 +92,6 @@ func (ds *DirHash) Sum(extra []byte) ([]byte, error) { return nil, fmt.Errorf("dirhash: %s", err) } - return fhash.Sum(extra), nil + fhash.Write(extra) + return fhash.Sum(nil), nil } diff --git a/utils/dirhash/dirhash_test.go b/utils/dirhash/dirhash_test.go index 19cadf4a6..4099deecd 100644 --- a/utils/dirhash/dirhash_test.go +++ b/utils/dirhash/dirhash_test.go @@ -30,7 +30,7 @@ func TestDirHash(t *testing.T) { "regular file with extra data", "testdata/01", []byte{'0'}, - "3011d376d7bfad120831035cda21097f8d4a3a3b8cac03c5dc52863a574b1d5614", + "4a8fba57a6d0b0609cd9b0219fca9afecf88ec827e888c772eeda23989a5b0aa", }, { "directory with file", @@ -42,7 +42,7 @@ func TestDirHash(t *testing.T) { "directory with file with extra data", "testdata/02", []byte{'0'}, - "300a8fc06675a7ee082161b9c85d658b58952c3d22668fc6e84146efc4aef56b3c", + "12cf85424d527741d530632687ae38c63ca5bd0adb7803fe09c7c1a87f21b270", }, { "symlink", @@ -54,7 +54,7 @@ func TestDirHash(t *testing.T) { "symlink with extra data", "testdata/03", []byte{'0'}, - "30599b534f45d7d986aede06bc12e23ebbd2fe917e3b66f9f4d5a49e646ef42005", + "c45664de7a405ef1010ea74066c0572efcf3fa8137ed10d96d0c5aaf8dcd9a63", }, { "regular file + directory + symlink", @@ -66,13 +66,14 @@ func TestDirHash(t *testing.T) { "regular file + directory + symlink with extra data", "testdata/04", []byte{'0'}, - "30b4388f782e77ebfb584bac2ba13396bbb21138c954ad1836163b03bca182099d", + "203fed7ec98926b4926cc6977b0ba6d5e9a3a9325ad5bfa0edcbce671243325c", }, } for _, tt := range tests { hash, err := New(tt.path).Sum(tt.extra) assert.NoError(t, err, tt.name) + assert.Equal(t, 32, len(hash)) assert.Equal(t, tt.hash, hex.EncodeToString(hash), tt.name) } } From 4eb8aea4406dcd70cd10b8da7051bd6da3f6a858 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Mon, 15 Apr 2019 14:16:53 +1000 Subject: [PATCH 20/20] fix logs with configuration --- commands/provider/service_provider.go | 2 ++ commands/service_logs.go | 2 ++ service/logs.go | 19 +++++++++++++++---- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/commands/provider/service_provider.go b/commands/provider/service_provider.go index 300c948f1..3defd10dd 100644 --- a/commands/provider/service_provider.go +++ b/commands/provider/service_provider.go @@ -12,6 +12,7 @@ import ( "github.com/mesg-foundation/core/commands/provider/assets" "github.com/mesg-foundation/core/protobuf/acknowledgement" "github.com/mesg-foundation/core/protobuf/coreapi" + "github.com/mesg-foundation/core/service" "github.com/mesg-foundation/core/service/importer" "github.com/mesg-foundation/core/utils/chunker" "github.com/mesg-foundation/core/utils/pretty" @@ -117,6 +118,7 @@ func (p *ServiceProvider) ServiceLogs(id string, dependencies ...string) (logs [ if err != nil { return nil, nil, nil, err } + dependencies = append(dependencies, service.MainServiceKey) for _, dep := range resp.Service.Definition.Dependencies { dependencies = append(dependencies, dep.Key) } diff --git a/commands/service_logs.go b/commands/service_logs.go index 4b517df10..1de719c94 100644 --- a/commands/service_logs.go +++ b/commands/service_logs.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/mesg-foundation/core/commands/provider" + "github.com/mesg-foundation/core/service" "github.com/mesg-foundation/core/utils/pretty" "github.com/mesg-foundation/core/x/xsignal" "github.com/mesg-foundation/core/x/xstrings" @@ -69,6 +70,7 @@ func showLogs(e ServiceExecutor, serviceID string, dependencies ...string) (clos // if there was no dependencies copy all returned // by service logs. if len(dependencies) == 0 { + dependencies = append(dependencies, service.MainServiceKey) for _, log := range logs { dependencies = append(dependencies, log.Dependency) } diff --git a/service/logs.go b/service/logs.go index e941812e5..1380018a3 100644 --- a/service/logs.go +++ b/service/logs.go @@ -21,18 +21,29 @@ func (s *Service) Logs(c container.Container, dependencies ...string) ([]*Log, e logs []*Log isNoFilter = len(dependencies) == 0 ) - for _, dep := range s.Dependencies { - if isNoFilter || xstrings.SliceContains(dependencies, dep.Key) { + addLog := func(dep *Dependency, name string) error { + if isNoFilter || xstrings.SliceContains(dependencies, name) { rstd, rerr, err := dep.Logs(c, s.namespace()) if err != nil { - return nil, err + return err } logs = append(logs, &Log{ - Dependency: dep.Key, + Dependency: name, Standard: rstd, Error: rerr, }) } + return nil + } + if s.Configuration != nil { + if err := addLog(s.Configuration, MainServiceKey); err != nil { + return nil, err + } + } + for _, dep := range s.Dependencies { + if err := addLog(dep, dep.Key); err != nil { + return nil, err + } } return logs, nil }