From 2f6a5572ed94f85f5f44369a9bd7dc0432b0c369 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Wed, 19 Sep 2018 18:24:13 +0700 Subject: [PATCH 01/14] add core name in config --- config/config.go | 3 +++ config/config_test.go | 1 + 2 files changed, 4 insertions(+) diff --git a/config/config.go b/config/config.go index 170296357..07b4df7aa 100644 --- a/config/config.go +++ b/config/config.go @@ -38,6 +38,7 @@ type Config struct { Core struct { Image string + Name string Path string } @@ -62,6 +63,8 @@ func New() (*Config, error) { c.Log.Format = "text" c.Log.Level = "info" c.Core.Image = "mesg/core:" + strings.Split(version.Version, " ")[0] + c.Core.Name = "mesg-core" + // TODO: Add core.name in core.path c.Core.Path = filepath.Join(home, ".mesg") c.Docker.Core.Path = "/mesg" c.Docker.Socket = "/var/run/docker.sock" diff --git a/config/config_test.go b/config/config_test.go index 47dc6b0cf..5b9cc9519 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -19,6 +19,7 @@ func TestDefaultValue(t *testing.T) { require.Equal(t, "text", c.Log.Format) require.Equal(t, "info", c.Log.Level) require.Equal(t, filepath.Join(home, ".mesg"), c.Core.Path) + require.Equal(t, "mesg-core", c.Core.Name) require.Equal(t, "/mesg", c.Docker.Core.Path) require.Equal(t, "/var/run/docker.sock", c.Docker.Socket) require.True(t, strings.HasPrefix(c.Core.Image, "mesg/core:")) From cd5ffa116e96390917865c14a7daae1bed1ed04d Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Wed, 19 Sep 2018 18:24:32 +0700 Subject: [PATCH 02/14] add network name based on the config --- container/container_test.go | 4 +++- container/namespace.go | 17 ++++++++++++----- container/namespace_test.go | 7 +++++-- container/shared_network.go | 6 ++---- container/shared_network_test.go | 8 ++++---- 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/container/container_test.go b/container/container_test.go index f315dd6a8..1fa8c403b 100644 --- a/container/container_test.go +++ b/container/container_test.go @@ -8,6 +8,7 @@ import ( "github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/swarm" + "github.com/mesg-foundation/core/config" "github.com/mesg-foundation/core/container/dockertest" "github.com/stretchr/testify/require" ) @@ -15,6 +16,7 @@ import ( func TestNew(t *testing.T) { dt := dockertest.New() c, err := New(ClientOption(dt.Client())) + cfg, _ := config.Global() require.Nil(t, err) require.NotNil(t, c) @@ -33,7 +35,7 @@ func TestNew(t *testing.T) { require.Equal(t, "0.0.0.0:2377", (<-dt.LastSwarmInit()).Request.ListenAddr) ln := <-dt.LastNetworkCreate() - require.Equal(t, "mesg-shared", ln.Name) + require.Equal(t, cfg.Core.Name, ln.Name) require.Equal(t, types.NetworkCreate{ CheckDuplicate: true, Driver: "overlay", diff --git a/container/namespace.go b/container/namespace.go index dd74c7607..fe125acda 100644 --- a/container/namespace.go +++ b/container/namespace.go @@ -1,17 +1,24 @@ package container import ( + "log" "strings" -) -const ( - namespacePrefix string = "mesg" - namespaceSeparator string = "-" + "github.com/mesg-foundation/core/config" ) +const namespaceSeparator string = "-" + // Namespace creates a namespace from a list of string. +// TODO: Put this function as in the Container type. +// TODO: load config in the container struct +// TODO: change input as "..string" func Namespace(ss []string) string { - ssWithPrefix := append([]string{namespacePrefix}, ss...) + c, err := config.Global() + if err != nil { + log.Fatalln("TODO: Manage error") + } + ssWithPrefix := append([]string{c.Core.Name}, ss...) namespace := strings.Join(ssWithPrefix, namespaceSeparator) namespace = strings.Replace(namespace, " ", namespaceSeparator, -1) return namespace diff --git a/container/namespace_test.go b/container/namespace_test.go index a853e1861..fb0064b72 100644 --- a/container/namespace_test.go +++ b/container/namespace_test.go @@ -4,15 +4,18 @@ import ( "strings" "testing" + "github.com/mesg-foundation/core/config" "github.com/stretchr/testify/require" ) func TestNamespace(t *testing.T) { + c, _ := config.Global() namespace := Namespace([]string{"test"}) - require.Equal(t, namespace, strings.Join([]string{namespacePrefix, "test"}, namespaceSeparator)) + require.Equal(t, namespace, strings.Join([]string{c.Core.Name, "test"}, namespaceSeparator)) } func TestNamespaceReplaceSpace(t *testing.T) { + c, _ := config.Global() namespace := Namespace([]string{"test foo"}) - require.Equal(t, namespace, strings.Join([]string{namespacePrefix, "test-foo"}, namespaceSeparator)) + require.Equal(t, namespace, strings.Join([]string{c.Core.Name, "test-foo"}, namespaceSeparator)) } diff --git a/container/shared_network.go b/container/shared_network.go index fdd04ad97..befafbbce 100644 --- a/container/shared_network.go +++ b/container/shared_network.go @@ -7,8 +7,6 @@ import ( docker "github.com/docker/docker/client" ) -var sharedNetworkNamespace = []string{"shared"} - // SharedNetworkID returns the ID of the shared network. func (c *Container) SharedNetworkID() (networkID string, err error) { network, err := c.sharedNetwork() @@ -31,7 +29,7 @@ func (c *Container) createSharedNetworkIfNeeded() error { defer cancel() // Create the new network needed to run containers. - namespace := Namespace(sharedNetworkNamespace) + namespace := Namespace([]string{}) _, err = c.client.NetworkCreate(ctx, namespace, types.NetworkCreate{ CheckDuplicate: true, Driver: "overlay", @@ -46,5 +44,5 @@ func (c *Container) createSharedNetworkIfNeeded() error { func (c *Container) sharedNetwork() (network types.NetworkResource, err error) { ctx, cancel := context.WithTimeout(context.Background(), c.callTimeout) defer cancel() - return c.client.NetworkInspect(ctx, Namespace(sharedNetworkNamespace), types.NetworkInspectOptions{}) + return c.client.NetworkInspect(ctx, Namespace([]string{}), types.NetworkInspectOptions{}) } diff --git a/container/shared_network_test.go b/container/shared_network_test.go index 7b60e4705..6e881505e 100644 --- a/container/shared_network_test.go +++ b/container/shared_network_test.go @@ -25,7 +25,7 @@ func TestSharedNetwork(t *testing.T) { require.Equal(t, id, network.ID) li := <-dt.LastNetworkInspect() - require.Equal(t, Namespace(sharedNetworkNamespace), li.Network) + require.Equal(t, Namespace([]string{}), li.Network) require.Equal(t, types.NetworkInspectOptions{}, li.Options) } @@ -42,12 +42,12 @@ func TestCreateSharedNetworkIfNeeded(t *testing.T) { require.Nil(t, c.createSharedNetworkIfNeeded()) lc := <-dt.LastNetworkCreate() - require.Equal(t, Namespace(sharedNetworkNamespace), lc.Name) + require.Equal(t, Namespace([]string{}), lc.Name) require.Equal(t, types.NetworkCreate{ CheckDuplicate: true, Driver: "overlay", Labels: map[string]string{ - "com.docker.stack.namespace": Namespace(sharedNetworkNamespace), + "com.docker.stack.namespace": Namespace([]string{}), }, }, lc.Options) } @@ -89,6 +89,6 @@ func TestSharedNetworkID(t *testing.T) { require.Equal(t, network, id) li := <-dt.LastNetworkInspect() - require.Equal(t, Namespace(sharedNetworkNamespace), li.Network) + require.Equal(t, Namespace([]string{}), li.Network) require.Equal(t, types.NetworkInspectOptions{}, li.Options) } From a7715209165ad8c09de42dacf877db1bc843a237 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Wed, 19 Sep 2018 19:15:05 +0700 Subject: [PATCH 03/14] add core path helper method --- config/config.go | 24 +++++++++++++++--------- config/config_test.go | 3 ++- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/config/config.go b/config/config.go index 07b4df7aa..074800790 100644 --- a/config/config.go +++ b/config/config.go @@ -37,9 +37,9 @@ type Config struct { } Core struct { - Image string - Name string - Path string + Image string + Name string + RootPath string } Docker struct { @@ -64,13 +64,17 @@ func New() (*Config, error) { c.Log.Level = "info" c.Core.Image = "mesg/core:" + strings.Split(version.Version, " ")[0] c.Core.Name = "mesg-core" - // TODO: Add core.name in core.path - c.Core.Path = filepath.Join(home, ".mesg") + c.Core.RootPath = filepath.Join(home, ".mesg") c.Docker.Core.Path = "/mesg" c.Docker.Socket = "/var/run/docker.sock" return &c, nil } +// CorePath returns the path based on the Core.Name +func (c *Config) CorePath() string { + return filepath.Join(c.Core.RootPath, c.Core.Name) +} + // Global returns a singleton of a Config after loaded ENV and validate the values. func Global() (*Config, error) { var err error @@ -103,7 +107,7 @@ func (c *Config) Load() error { // Prepare setups local directories or any other required thing based on config func (c *Config) Prepare() error { - return os.MkdirAll(c.Core.Path, os.FileMode(0755)) + return os.MkdirAll(c.CorePath(), os.FileMode(0755)) } // Validate checks values and return an error if any validation failed. @@ -120,8 +124,10 @@ func (c *Config) Validate() error { // DaemonEnv returns the needed environmental variable for the Daemon. func (c *Config) DaemonEnv() map[string]string { return map[string]string{ - "MESG_LOG_FORMAT": c.Log.Format, - "MESG_LOG_LEVEL": c.Log.Level, - "MESG_CORE_PATH": c.Docker.Core.Path, + "MESG_SERVER_ADDRESS": c.Server.Address, + "MESG_LOG_FORMAT": c.Log.Format, + "MESG_LOG_LEVEL": c.Log.Level, + "MESG_CORE_NAME": c.Core.Name, + "MESG_CORE_ROOTPATH": c.Docker.Core.Path, } } diff --git a/config/config_test.go b/config/config_test.go index 5b9cc9519..d1bb8d2ae 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -18,11 +18,12 @@ func TestDefaultValue(t *testing.T) { require.Equal(t, "localhost:50052", c.Client.Address) require.Equal(t, "text", c.Log.Format) require.Equal(t, "info", c.Log.Level) - require.Equal(t, filepath.Join(home, ".mesg"), c.Core.Path) + require.Equal(t, filepath.Join(home, ".mesg"), c.Core.RootPath) require.Equal(t, "mesg-core", c.Core.Name) require.Equal(t, "/mesg", c.Docker.Core.Path) require.Equal(t, "/var/run/docker.sock", c.Docker.Socket) require.True(t, strings.HasPrefix(c.Core.Image, "mesg/core:")) + require.Equal(t, filepath.Join(home, ".mesg", c.Core.Name), c.CorePath()) } func TestGlobal(t *testing.T) { From de25479dc2df7c4a1279451e0ca5ee6cef7e1cdc Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Wed, 19 Sep 2018 19:16:00 +0700 Subject: [PATCH 04/14] make daemon startable multiple time with it's own name --- daemon/const.go | 10 ---------- daemon/logs.go | 2 +- daemon/start.go | 8 +++++--- daemon/start_test.go | 11 ++++++++--- daemon/status.go | 2 +- daemon/stop.go | 2 +- 6 files changed, 16 insertions(+), 19 deletions(-) delete mode 100644 daemon/const.go diff --git a/daemon/const.go b/daemon/const.go deleted file mode 100644 index f0c88651c..000000000 --- a/daemon/const.go +++ /dev/null @@ -1,10 +0,0 @@ -package daemon - -const ( - name = "core" -) - -// Namespace returns the namespace of the MESG Core. -func Namespace() []string { - return []string{name} -} diff --git a/daemon/logs.go b/daemon/logs.go index 77e362781..117c4be16 100644 --- a/daemon/logs.go +++ b/daemon/logs.go @@ -6,5 +6,5 @@ import ( // Logs returns the core's docker service logs. func Logs() (io.ReadCloser, error) { - return defaultContainer.ServiceLogs(Namespace()) + return defaultContainer.ServiceLogs([]string{}) } diff --git a/daemon/start.go b/daemon/start.go index ceebf6fc4..cc82728eb 100644 --- a/daemon/start.go +++ b/daemon/start.go @@ -1,6 +1,8 @@ package daemon import ( + "path/filepath" + "github.com/mesg-foundation/core/config" "github.com/mesg-foundation/core/container" "github.com/mesg-foundation/core/x/xnet" @@ -33,7 +35,7 @@ func serviceSpec() (spec container.ServiceOptions, err error) { return container.ServiceOptions{}, err } return container.ServiceOptions{ - Namespace: Namespace(), + Namespace: []string{}, Image: c.Core.Image, Env: container.MapToEnv(c.DaemonEnv()), Mounts: []container.Mount{ @@ -43,8 +45,8 @@ func serviceSpec() (spec container.ServiceOptions, err error) { Bind: true, }, { - Source: c.Core.Path, - Target: c.Docker.Core.Path, + Source: c.CorePath(), + Target: filepath.Join(c.Docker.Core.Path, c.Core.Name), Bind: true, }, }, diff --git a/daemon/start_test.go b/daemon/start_test.go index 64a412f0d..4c581ea8a 100644 --- a/daemon/start_test.go +++ b/daemon/start_test.go @@ -11,6 +11,10 @@ import ( // startForTest starts a dummy MESG Core service func startForTest() { + c, err := config.Global() + if err != nil { + panic(err) + } status, err := Status() if err != nil { panic(err) @@ -23,7 +27,7 @@ func startForTest() { panic(err) } _, err = defaultContainer.StartService(container.ServiceOptions{ - Namespace: Namespace(), + Namespace: []string{c.Core.Name}, Image: "http-server", NetworksID: []string{sharedNetworkID}, }) @@ -37,10 +41,11 @@ func TestStartConfig(t *testing.T) { c, _ := config.Global() spec, err := serviceSpec() require.NoError(t, err) + require.Equal(t, []string{c.Core.Name}, spec.Namespace) // Make sure that the config directory is passed in parameter to write on the same folder require.Contains(t, spec.Env, "MESG_LOG_LEVEL=info") require.Contains(t, spec.Env, "MESG_LOG_FORMAT=text") - require.Contains(t, spec.Env, "MESG_CORE_PATH="+c.Docker.Core.Path) + require.Contains(t, spec.Env, "MESG_CORE_ROOTPATH="+c.Docker.Core.Path) // Ensure that the port is shared _, port, _ := xnet.SplitHostPort(c.Server.Address) require.Equal(t, spec.Ports[0].Published, uint32(port)) @@ -50,7 +55,7 @@ func TestStartConfig(t *testing.T) { require.Equal(t, spec.Mounts[0].Target, c.Docker.Socket) require.True(t, spec.Mounts[0].Bind) // Ensure that the host users folder is sync with the core - require.Equal(t, spec.Mounts[1].Source, c.Core.Path) + require.Equal(t, spec.Mounts[1].Source, c.Core.RootPath) require.Equal(t, spec.Mounts[1].Target, c.Docker.Core.Path) require.True(t, spec.Mounts[1].Bind) } diff --git a/daemon/status.go b/daemon/status.go index 0e7bbf9a1..de982c516 100644 --- a/daemon/status.go +++ b/daemon/status.go @@ -6,5 +6,5 @@ import ( // Status returns the Status of the docker service of the daemon. func Status() (container.StatusType, error) { - return defaultContainer.Status(Namespace()) + return defaultContainer.Status([]string{}) } diff --git a/daemon/stop.go b/daemon/stop.go index 246b32aac..3e395bcf8 100644 --- a/daemon/stop.go +++ b/daemon/stop.go @@ -10,5 +10,5 @@ func Stop() error { if err != nil || status == container.STOPPED { return err } - return defaultContainer.StopService(Namespace()) + return defaultContainer.StopService([]string{}) } From ea45586aac35b6d182cd721a3cc37a8764b56b3e Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Wed, 19 Sep 2018 19:16:10 +0700 Subject: [PATCH 05/14] update database path --- database/services/db.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/services/db.go b/database/services/db.go index b26bc2bf4..d15d73c84 100644 --- a/database/services/db.go +++ b/database/services/db.go @@ -20,7 +20,7 @@ func open() (db *leveldb.DB, err error) { if err != nil { return nil, err } - storagePath := filepath.Join(c.Core.Path, "database", "services") + storagePath := filepath.Join(c.CorePath(), "database", "services") _instance, err = leveldb.OpenFile(storagePath, nil) if err != nil { return nil, err From 6698103e7a3896eab82d44a1ee76a38287a605f1 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Wed, 19 Sep 2018 19:16:22 +0700 Subject: [PATCH 06/14] use network based on the config --- service/start.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/start.go b/service/start.go index 41b72e2af..000384f1f 100644 --- a/service/start.go +++ b/service/start.go @@ -68,7 +68,7 @@ func (d *Dependency) Start(networkID string) (containerServiceID string, err err return "", err } _, port, err := xnet.SplitHostPort(c.Server.Address) - endpoint := "mesg-core:" + strconv.Itoa(port) // TODO: should get this from daemon namespace and config + endpoint := c.Core.Name + ":" + strconv.Itoa(port) return defaultContainer.StartService(container.ServiceOptions{ Namespace: d.namespace(), Labels: map[string]string{ From 46a0b5c5be9cce9449586e5247da52cb7045be9f Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Thu, 20 Sep 2018 11:44:40 +0700 Subject: [PATCH 07/14] Update container.ListServices function to accept multiple labels --- container/service.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/container/service.go b/container/service.go index 1f149e76a..45b73b1e1 100644 --- a/container/service.go +++ b/container/service.go @@ -12,14 +12,18 @@ import ( ) // ListServices returns existing docker services matching a specific label name. -func (c *Container) ListServices(label string) ([]swarm.Service, error) { +func (c *Container) ListServices(labels ...string) ([]swarm.Service, error) { + args := make([]filters.KeyValuePair, 0) + for _, label := range labels { + args = append(args, filters.KeyValuePair{ + Key: "label", + Value: label, + }) + } ctx, cancel := context.WithTimeout(context.Background(), c.callTimeout) defer cancel() return c.client.ServiceList(ctx, types.ServiceListOptions{ - Filters: filters.NewArgs(filters.KeyValuePair{ - Key: "label", - Value: label, - }), + Filters: filters.NewArgs(args...), }) } From 462108b51ef7c4111ac980185bfd350503adff77 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Thu, 20 Sep 2018 11:45:58 +0700 Subject: [PATCH 08/14] Update service.ListRunning function to only return services for a specific core base on its name --- service/start.go | 1 + service/status.go | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/service/start.go b/service/start.go index 000384f1f..df79f1eb6 100644 --- a/service/start.go +++ b/service/start.go @@ -74,6 +74,7 @@ func (d *Dependency) Start(networkID string) (containerServiceID string, err err Labels: map[string]string{ "mesg.service": d.service.Name, "mesg.hash": d.service.ID, + "mesg.core": c.Core.Name, }, Image: d.Image, Args: strings.Fields(d.Command), diff --git a/service/status.go b/service/status.go index d1bb9e68d..a46a42662 100644 --- a/service/status.go +++ b/service/status.go @@ -1,6 +1,7 @@ package service import ( + "github.com/mesg-foundation/core/config" "github.com/mesg-foundation/core/container" ) @@ -70,10 +71,15 @@ func (d *Dependency) Status() (container.StatusType, error) { // ListRunning returns all the running services.2 // TODO: should move to another file func ListRunning() ([]string, error) { - services, err := defaultContainer.ListServices("mesg.hash") + c, err := config.Global() if err != nil { return nil, err } + services, err := defaultContainer.ListServices("mesg.hash", "mesg.core="+c.Core.Name) + if err != nil { + return nil, err + } + // Make service list unique. One mesg service can have multiple docker service. mapRes := make(map[string]uint) for _, service := range services { serviceName := service.Spec.Annotations.Labels["mesg.hash"] From 72fdb6437f2390e980ed55826b2e12dacb06d547 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Thu, 20 Sep 2018 11:54:37 +0700 Subject: [PATCH 09/14] Fix test --- config/config_test.go | 5 ++++- daemon/start_test.go | 13 +++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/config/config_test.go b/config/config_test.go index d1bb8d2ae..2ef90f758 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -79,6 +79,9 @@ func TestValidate(t *testing.T) { func TestDaemonEnv(t *testing.T) { c, _ := New() env := c.DaemonEnv() - require.Equal(t, c.Log.Format, env["MESG_LOG_FORMAT"]) + require.Equal(t, c.Server.Address, env["MESG_SERVER_ADDRESS"]) require.Equal(t, c.Log.Level, env["MESG_LOG_LEVEL"]) + require.Equal(t, c.Log.Format, env["MESG_LOG_FORMAT"]) + require.Equal(t, c.Core.Name, env["MESG_CORE_NAME"]) + require.Equal(t, c.Docker.Core.Path, env["MESG_CORE_ROOTPATH"]) } diff --git a/daemon/start_test.go b/daemon/start_test.go index 4c581ea8a..be7ca8f0c 100644 --- a/daemon/start_test.go +++ b/daemon/start_test.go @@ -1,6 +1,7 @@ package daemon import ( + "path/filepath" "testing" "github.com/mesg-foundation/core/config" @@ -11,10 +12,6 @@ import ( // startForTest starts a dummy MESG Core service func startForTest() { - c, err := config.Global() - if err != nil { - panic(err) - } status, err := Status() if err != nil { panic(err) @@ -27,7 +24,7 @@ func startForTest() { panic(err) } _, err = defaultContainer.StartService(container.ServiceOptions{ - Namespace: []string{c.Core.Name}, + Namespace: []string{}, Image: "http-server", NetworksID: []string{sharedNetworkID}, }) @@ -41,7 +38,7 @@ func TestStartConfig(t *testing.T) { c, _ := config.Global() spec, err := serviceSpec() require.NoError(t, err) - require.Equal(t, []string{c.Core.Name}, spec.Namespace) + require.Equal(t, []string{}, spec.Namespace) // Make sure that the config directory is passed in parameter to write on the same folder require.Contains(t, spec.Env, "MESG_LOG_LEVEL=info") require.Contains(t, spec.Env, "MESG_LOG_FORMAT=text") @@ -55,7 +52,7 @@ func TestStartConfig(t *testing.T) { require.Equal(t, spec.Mounts[0].Target, c.Docker.Socket) require.True(t, spec.Mounts[0].Bind) // Ensure that the host users folder is sync with the core - require.Equal(t, spec.Mounts[1].Source, c.Core.RootPath) - require.Equal(t, spec.Mounts[1].Target, c.Docker.Core.Path) + require.Equal(t, spec.Mounts[1].Source, c.CorePath()) + require.Equal(t, spec.Mounts[1].Target, filepath.Join(c.Docker.Core.Path, c.Core.Name)) require.True(t, spec.Mounts[1].Bind) } From f584cbdbd74b3ae2d28363fa2764da8cff73829e Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Thu, 20 Sep 2018 12:02:58 +0700 Subject: [PATCH 10/14] update container namespace --- container/container.go | 10 +++++++++- container/container_test.go | 6 +++--- container/namespace.go | 14 ++------------ container/namespace_test.go | 14 ++++++++------ container/network.go | 4 ++-- container/network_test.go | 10 +++++----- container/service.go | 8 ++++---- container/service_options.go | 4 ++-- container/service_options_test.go | 14 +++++++++----- container/service_test.go | 23 +++++++++++------------ container/shared_network.go | 4 ++-- container/shared_network_test.go | 8 ++++---- container/task.go | 2 +- container/task_test.go | 2 +- 14 files changed, 63 insertions(+), 60 deletions(-) diff --git a/container/container.go b/container/container.go index 16f101a2c..720f0229a 100644 --- a/container/container.go +++ b/container/container.go @@ -8,6 +8,7 @@ import ( "github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/swarm" docker "github.com/docker/docker/client" + "github.com/mesg-foundation/core/config" ) // Container provides high level interactions with Docker API for MESG. @@ -17,6 +18,8 @@ type Container struct { // callTimeout is the timeout value for Docker API calls. callTimeout time.Duration + + config *config.Config } // Option is a configuration func for Container. @@ -31,6 +34,11 @@ func New(options ...Option) (*Container, error) { option(c) } var err error + cfg, err := config.Global() + if err != nil { + return nil, err + } + c.config = cfg if c.client == nil { c.client, err = docker.NewEnvClient() if err != nil { @@ -89,7 +97,7 @@ func (c *Container) FindContainer(namespace []string) (types.ContainerJSON, erro containers, err := c.client.ContainerList(ctx, types.ContainerListOptions{ Filters: filters.NewArgs(filters.KeyValuePair{ Key: "label", - Value: "com.docker.stack.namespace=" + Namespace(namespace), + Value: "com.docker.stack.namespace=" + c.Namespace(namespace), }), Limit: 1, }) diff --git a/container/container_test.go b/container/container_test.go index 1fa8c403b..e75ba9132 100644 --- a/container/container_test.go +++ b/container/container_test.go @@ -74,7 +74,7 @@ func TestFindContainerNonExistent(t *testing.T) { require.Equal(t, types.ContainerListOptions{ Filters: filters.NewArgs(filters.KeyValuePair{ Key: "label", - Value: "com.docker.stack.namespace=" + Namespace(namespace), + Value: "com.docker.stack.namespace=" + c.Namespace(namespace), }), Limit: 1, }, (<-dt.LastContainerList()).Options) @@ -105,7 +105,7 @@ func TestFindContainer(t *testing.T) { require.Equal(t, types.ContainerListOptions{ Filters: filters.NewArgs(filters.KeyValuePair{ Key: "label", - Value: "com.docker.stack.namespace=" + Namespace(namespace), + Value: "com.docker.stack.namespace=" + c.Namespace(namespace), }), Limit: 1, }, (<-dt.LastContainerList()).Options) @@ -127,7 +127,7 @@ func TestNonExistentContainerStatus(t *testing.T) { require.Equal(t, STOPPED, status) resp := <-dt.LastServiceInspectWithRaw() - require.Equal(t, Namespace(namespace), resp.ServiceID) + require.Equal(t, c.Namespace(namespace), resp.ServiceID) require.Equal(t, types.ServiceInspectOptions{false}, resp.Options) } diff --git a/container/namespace.go b/container/namespace.go index fe125acda..c73df34ba 100644 --- a/container/namespace.go +++ b/container/namespace.go @@ -1,24 +1,14 @@ package container import ( - "log" "strings" - - "github.com/mesg-foundation/core/config" ) const namespaceSeparator string = "-" // Namespace creates a namespace from a list of string. -// TODO: Put this function as in the Container type. -// TODO: load config in the container struct -// TODO: change input as "..string" -func Namespace(ss []string) string { - c, err := config.Global() - if err != nil { - log.Fatalln("TODO: Manage error") - } - ssWithPrefix := append([]string{c.Core.Name}, ss...) +func (c *Container) Namespace(ss []string) string { + ssWithPrefix := append([]string{c.config.Core.Name}, ss...) namespace := strings.Join(ssWithPrefix, namespaceSeparator) namespace = strings.Replace(namespace, " ", namespaceSeparator, -1) return namespace diff --git a/container/namespace_test.go b/container/namespace_test.go index fb0064b72..70dd46421 100644 --- a/container/namespace_test.go +++ b/container/namespace_test.go @@ -9,13 +9,15 @@ import ( ) func TestNamespace(t *testing.T) { - c, _ := config.Global() - namespace := Namespace([]string{"test"}) - require.Equal(t, namespace, strings.Join([]string{c.Core.Name, "test"}, namespaceSeparator)) + cfg, _ := config.Global() + c, _ := New() + namespace := c.Namespace([]string{"test"}) + require.Equal(t, namespace, strings.Join([]string{cfg.Core.Name, "test"}, namespaceSeparator)) } func TestNamespaceReplaceSpace(t *testing.T) { - c, _ := config.Global() - namespace := Namespace([]string{"test foo"}) - require.Equal(t, namespace, strings.Join([]string{c.Core.Name, "test-foo"}, namespaceSeparator)) + cfg, _ := config.Global() + c, _ := New() + namespace := c.Namespace([]string{"test foo"}) + require.Equal(t, namespace, strings.Join([]string{cfg.Core.Name, "test-foo"}, namespaceSeparator)) } diff --git a/container/network.go b/container/network.go index d8785e5c8..7f456a9b0 100644 --- a/container/network.go +++ b/container/network.go @@ -17,7 +17,7 @@ func (c *Container) CreateNetwork(namespace []string) (id string, err error) { if network.ID != "" { return network.ID, nil } - namespaceFlat := Namespace(namespace) + namespaceFlat := c.Namespace(namespace) ctx, cancel := context.WithTimeout(context.Background(), c.callTimeout) defer cancel() response, err := c.client.NetworkCreate(ctx, namespaceFlat, types.NetworkCreate{ @@ -69,5 +69,5 @@ func (c *Container) DeleteNetwork(namespace []string) error { func (c *Container) FindNetwork(namespace []string) (types.NetworkResource, error) { ctx, cancel := context.WithTimeout(context.Background(), c.callTimeout) defer cancel() - return c.client.NetworkInspect(ctx, Namespace(namespace), types.NetworkInspectOptions{}) + return c.client.NetworkInspect(ctx, c.Namespace(namespace), types.NetworkInspectOptions{}) } diff --git a/container/network_test.go b/container/network_test.go index cd7ef9203..5d66da7d7 100644 --- a/container/network_test.go +++ b/container/network_test.go @@ -29,12 +29,12 @@ func TestCreateNetwork(t *testing.T) { require.Equal(t, id, networkID) li := <-dt.LastNetworkCreate() - require.Equal(t, Namespace(namespace), li.Name) + require.Equal(t, c.Namespace(namespace), li.Name) require.Equal(t, types.NetworkCreate{ CheckDuplicate: true, Driver: "overlay", Labels: map[string]string{ - "com.docker.stack.namespace": Namespace(namespace), + "com.docker.stack.namespace": c.Namespace(namespace), }, }, li.Options) } @@ -57,7 +57,7 @@ func TestCreateAlreadyExistingNetwork(t *testing.T) { require.Equal(t, id, networkID) li := <-dt.LastNetworkInspect() - require.Equal(t, Namespace(namespace), li.Network) + require.Equal(t, c.Namespace(namespace), li.Network) require.Equal(t, types.NetworkInspectOptions{}, li.Options) select { @@ -88,7 +88,7 @@ func TestDeleteNetwork(t *testing.T) { require.Nil(t, c.DeleteNetwork(namespace)) li := <-dt.LastNetworkInspect() - require.Equal(t, Namespace(namespace), li.Network) + require.Equal(t, c.Namespace(namespace), li.Network) require.Equal(t, types.NetworkInspectOptions{}, li.Options) require.Equal(t, id, (<-dt.LastNetworkRemove()).Network) @@ -150,7 +150,7 @@ func TestFindNetwork(t *testing.T) { require.Equal(t, id, network.ID) li := <-dt.LastNetworkInspect() - require.Equal(t, Namespace(namespace), li.Network) + require.Equal(t, c.Namespace(namespace), li.Network) require.Equal(t, types.NetworkInspectOptions{}, li.Options) } diff --git a/container/service.go b/container/service.go index 1f149e76a..1b5bea03c 100644 --- a/container/service.go +++ b/container/service.go @@ -27,7 +27,7 @@ func (c *Container) ListServices(label string) ([]swarm.Service, error) { func (c *Container) FindService(namespace []string) (swarm.Service, error) { ctx, cancel := context.WithTimeout(context.Background(), c.callTimeout) defer cancel() - service, _, err := c.client.ServiceInspectWithRaw(ctx, Namespace(namespace), + service, _, err := c.client.ServiceInspectWithRaw(ctx, c.Namespace(namespace), types.ServiceInspectOptions{}, ) return service, err @@ -35,7 +35,7 @@ func (c *Container) FindService(namespace []string) (swarm.Service, error) { // StartService starts a docker service. func (c *Container) StartService(options ServiceOptions) (serviceID string, err error) { - service := options.toSwarmServiceSpec() + service := options.toSwarmServiceSpec(c) ctx, cancel := context.WithTimeout(context.Background(), c.callTimeout) defer cancel() response, err := c.client.ServiceCreate(ctx, service, types.ServiceCreateOptions{}) @@ -53,7 +53,7 @@ func (c *Container) StopService(namespace []string) (err error) { if err != nil { return err } - if err := c.client.ServiceRemove(ctx, Namespace(namespace)); err != nil && !docker.IsErrNotFound(err) { + if err := c.client.ServiceRemove(ctx, c.Namespace(namespace)); err != nil && !docker.IsErrNotFound(err) { return err } timeout := 1 * time.Second @@ -68,7 +68,7 @@ func (c *Container) StopService(namespace []string) (err error) { // ServiceLogs returns the logs of a service. func (c *Container) ServiceLogs(namespace []string) (io.ReadCloser, error) { - return c.client.ServiceLogs(context.Background(), Namespace(namespace), + return c.client.ServiceLogs(context.Background(), c.Namespace(namespace), types.ContainerLogsOptions{ ShowStdout: true, ShowStderr: true, diff --git a/container/service_options.go b/container/service_options.go index f4c63acb3..e0a14beaf 100644 --- a/container/service_options.go +++ b/container/service_options.go @@ -33,8 +33,8 @@ type Mount struct { Bind bool } -func (options *ServiceOptions) toSwarmServiceSpec() swarm.ServiceSpec { - namespace := Namespace(options.Namespace) +func (options *ServiceOptions) toSwarmServiceSpec(c *Container) swarm.ServiceSpec { + namespace := c.Namespace(options.Namespace) return swarm.ServiceSpec{ Annotations: swarm.Annotations{ Name: namespace, diff --git a/container/service_options_test.go b/container/service_options_test.go index c0f61991e..bc9bdfb26 100644 --- a/container/service_options_test.go +++ b/container/service_options_test.go @@ -11,8 +11,9 @@ func TestServiceOptionNamespace(t *testing.T) { options := &ServiceOptions{ Namespace: namespace, } - expectedNamespace := Namespace(namespace) - service := options.toSwarmServiceSpec() + c, _ := New() + expectedNamespace := c.Namespace(namespace) + service := options.toSwarmServiceSpec(c) require.Equal(t, expectedNamespace, service.Annotations.Name) require.Equal(t, expectedNamespace, service.Annotations.Labels["com.docker.stack.namespace"]) require.Equal(t, expectedNamespace, service.TaskTemplate.ContainerSpec.Labels["com.docker.stack.namespace"]) @@ -23,7 +24,8 @@ func TestServiceOptionImage(t *testing.T) { options := &ServiceOptions{ Image: image, } - service := options.toSwarmServiceSpec() + c, _ := New() + service := options.toSwarmServiceSpec(c) require.Equal(t, image, service.Annotations.Labels["com.docker.stack.image"]) require.Equal(t, image, service.TaskTemplate.ContainerSpec.Image) } @@ -52,7 +54,8 @@ func TestServiceOptionLabels(t *testing.T) { "label2": "bar", }, } - service := options.toSwarmServiceSpec() + c, _ := New() + service := options.toSwarmServiceSpec(c) require.Equal(t, "foo", service.Annotations.Labels["label1"]) require.Equal(t, "bar", service.Annotations.Labels["label2"]) } @@ -97,7 +100,8 @@ func TestServiceOptionEnv(t *testing.T) { options := &ServiceOptions{ Env: []string{"env1", "env2"}, } - service := options.toSwarmServiceSpec() + c, _ := New() + service := options.toSwarmServiceSpec(c) env := service.TaskTemplate.ContainerSpec.Env require.Equal(t, 2, len(env)) require.Equal(t, "env1", env[0]) diff --git a/container/service_test.go b/container/service_test.go index 28193e4a4..a36b0f11c 100644 --- a/container/service_test.go +++ b/container/service_test.go @@ -30,7 +30,7 @@ func TestStartService(t *testing.T) { require.Equal(t, containerID, id) ls := <-dt.LastServiceCreate() - require.Equal(t, options.toSwarmServiceSpec(), ls.Service) + require.Equal(t, options.toSwarmServiceSpec(c), ls.Service) require.Equal(t, types.ServiceCreateOptions{}, ls.Options) } @@ -56,7 +56,7 @@ func TestStopService(t *testing.T) { }() require.Nil(t, c.StopService(namespace)) - require.Equal(t, Namespace(namespace), (<-dt.LastServiceRemove()).ServiceID) + require.Equal(t, c.Namespace(namespace), (<-dt.LastServiceRemove()).ServiceID) ls := <-dt.LastContainerStop() require.Equal(t, containerID, ls.Container) @@ -98,7 +98,7 @@ func TestFindService(t *testing.T) { require.Equal(t, swarmService.ID, service.ID) li := <-dt.LastServiceInspectWithRaw() - require.Equal(t, Namespace(namespace), li.ServiceID) + require.Equal(t, c.Namespace(namespace), li.ServiceID) require.Equal(t, types.ServiceInspectOptions{}, li.Options) } @@ -114,7 +114,7 @@ func TestFindServiceNotExisting(t *testing.T) { require.Equal(t, dockertest.NotFoundErr{}, err) li := <-dt.LastServiceInspectWithRaw() - require.Equal(t, Namespace(namespace), li.ServiceID) + require.Equal(t, c.Namespace(namespace), li.ServiceID) require.Equal(t, types.ServiceInspectOptions{}, li.Options) } @@ -122,21 +122,20 @@ func TestListServices(t *testing.T) { namespace := []string{"namespace"} namespace1 := []string{"namespace"} label := "1" - swarmServices := []swarm.Service{ - {Spec: swarm.ServiceSpec{Annotations: swarm.Annotations{Name: Namespace(namespace)}}}, - {Spec: swarm.ServiceSpec{Annotations: swarm.Annotations{Name: Namespace(namespace1)}}}, - } - dt := dockertest.New() c, _ := New(ClientOption(dt.Client())) + swarmServices := []swarm.Service{ + {Spec: swarm.ServiceSpec{Annotations: swarm.Annotations{Name: c.Namespace(namespace)}}}, + {Spec: swarm.ServiceSpec{Annotations: swarm.Annotations{Name: c.Namespace(namespace1)}}}, + } dt.ProvideServiceList(swarmServices, nil) services, err := c.ListServices(label) require.Nil(t, err) require.Equal(t, 2, len(services)) - require.Equal(t, Namespace(namespace), services[0].Spec.Name) - require.Equal(t, Namespace(namespace1), services[1].Spec.Name) + require.Equal(t, c.Namespace(namespace), services[0].Spec.Name) + require.Equal(t, c.Namespace(namespace1), services[1].Spec.Name) require.Equal(t, types.ServiceListOptions{ Filters: filters.NewArgs(filters.KeyValuePair{ @@ -164,7 +163,7 @@ func TestServiceLogs(t *testing.T) { require.Equal(t, data, bytes) ll := <-dt.LastServiceLogs() - require.Equal(t, Namespace(namespace), ll.ServiceID) + require.Equal(t, c.Namespace(namespace), ll.ServiceID) require.Equal(t, types.ContainerLogsOptions{ ShowStdout: true, ShowStderr: true, diff --git a/container/shared_network.go b/container/shared_network.go index befafbbce..7ae232bb4 100644 --- a/container/shared_network.go +++ b/container/shared_network.go @@ -29,7 +29,7 @@ func (c *Container) createSharedNetworkIfNeeded() error { defer cancel() // Create the new network needed to run containers. - namespace := Namespace([]string{}) + namespace := c.Namespace([]string{}) _, err = c.client.NetworkCreate(ctx, namespace, types.NetworkCreate{ CheckDuplicate: true, Driver: "overlay", @@ -44,5 +44,5 @@ func (c *Container) createSharedNetworkIfNeeded() error { func (c *Container) sharedNetwork() (network types.NetworkResource, err error) { ctx, cancel := context.WithTimeout(context.Background(), c.callTimeout) defer cancel() - return c.client.NetworkInspect(ctx, Namespace([]string{}), types.NetworkInspectOptions{}) + return c.client.NetworkInspect(ctx, c.Namespace([]string{}), types.NetworkInspectOptions{}) } diff --git a/container/shared_network_test.go b/container/shared_network_test.go index 6e881505e..278384907 100644 --- a/container/shared_network_test.go +++ b/container/shared_network_test.go @@ -25,7 +25,7 @@ func TestSharedNetwork(t *testing.T) { require.Equal(t, id, network.ID) li := <-dt.LastNetworkInspect() - require.Equal(t, Namespace([]string{}), li.Network) + require.Equal(t, c.Namespace([]string{}), li.Network) require.Equal(t, types.NetworkInspectOptions{}, li.Options) } @@ -42,12 +42,12 @@ func TestCreateSharedNetworkIfNeeded(t *testing.T) { require.Nil(t, c.createSharedNetworkIfNeeded()) lc := <-dt.LastNetworkCreate() - require.Equal(t, Namespace([]string{}), lc.Name) + require.Equal(t, c.Namespace([]string{}), lc.Name) require.Equal(t, types.NetworkCreate{ CheckDuplicate: true, Driver: "overlay", Labels: map[string]string{ - "com.docker.stack.namespace": Namespace([]string{}), + "com.docker.stack.namespace": c.Namespace([]string{}), }, }, lc.Options) } @@ -89,6 +89,6 @@ func TestSharedNetworkID(t *testing.T) { require.Equal(t, network, id) li := <-dt.LastNetworkInspect() - require.Equal(t, Namespace([]string{}), li.Network) + require.Equal(t, c.Namespace([]string{}), li.Network) require.Equal(t, types.NetworkInspectOptions{}, li.Options) } diff --git a/container/task.go b/container/task.go index ff2ab32ed..526a77ede 100644 --- a/container/task.go +++ b/container/task.go @@ -15,7 +15,7 @@ func (c *Container) ListTasks(namespace []string) ([]swarm.Task, error) { return c.client.TaskList(ctx, types.TaskListOptions{ Filters: filters.NewArgs(filters.KeyValuePair{ Key: "label", - Value: "com.docker.stack.namespace=" + Namespace(namespace), + Value: "com.docker.stack.namespace=" + c.Namespace(namespace), }), }) } diff --git a/container/task_test.go b/container/task_test.go index 1ac41f494..9c81e14ee 100644 --- a/container/task_test.go +++ b/container/task_test.go @@ -31,7 +31,7 @@ func TestListTasks(t *testing.T) { require.Equal(t, types.TaskListOptions{ Filters: filters.NewArgs(filters.KeyValuePair{ Key: "label", - Value: "com.docker.stack.namespace=" + Namespace(namespace), + Value: "com.docker.stack.namespace=" + c.Namespace(namespace), }), }, (<-dt.LastTaskList()).Options) } From 2732e0d03f85f0e2cef365c80f0077168297f19c Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Fri, 21 Sep 2018 10:23:59 +0700 Subject: [PATCH 11/14] Change default name of Config.Core.Name from "mesg-core" to "core" --- config/config.go | 2 +- config/config_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/config.go b/config/config.go index 074800790..139fff54b 100644 --- a/config/config.go +++ b/config/config.go @@ -63,7 +63,7 @@ func New() (*Config, error) { c.Log.Format = "text" c.Log.Level = "info" c.Core.Image = "mesg/core:" + strings.Split(version.Version, " ")[0] - c.Core.Name = "mesg-core" + c.Core.Name = "core" c.Core.RootPath = filepath.Join(home, ".mesg") c.Docker.Core.Path = "/mesg" c.Docker.Socket = "/var/run/docker.sock" diff --git a/config/config_test.go b/config/config_test.go index 2ef90f758..fc4e7b989 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -19,7 +19,7 @@ func TestDefaultValue(t *testing.T) { require.Equal(t, "text", c.Log.Format) require.Equal(t, "info", c.Log.Level) require.Equal(t, filepath.Join(home, ".mesg"), c.Core.RootPath) - require.Equal(t, "mesg-core", c.Core.Name) + require.Equal(t, "core", c.Core.Name) require.Equal(t, "/mesg", c.Docker.Core.Path) require.Equal(t, "/var/run/docker.sock", c.Docker.Socket) require.True(t, strings.HasPrefix(c.Core.Image, "mesg/core:")) From e13071c5e0e4d9694a9671fd5b5631494537ccf6 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Mon, 24 Sep 2018 18:16:52 +0700 Subject: [PATCH 12/14] remove RootPath in favor of Core.Path config all the time --- config/config.go | 17 ++++++----------- config/config_test.go | 5 ++--- daemon/start.go | 6 ++---- daemon/start_test.go | 7 +++---- database/services/db.go | 2 +- 5 files changed, 14 insertions(+), 23 deletions(-) diff --git a/config/config.go b/config/config.go index 139fff54b..f8358cc4f 100644 --- a/config/config.go +++ b/config/config.go @@ -37,9 +37,9 @@ type Config struct { } Core struct { - Image string - Name string - RootPath string + Image string + Name string + Path string } Docker struct { @@ -64,17 +64,12 @@ func New() (*Config, error) { c.Log.Level = "info" c.Core.Image = "mesg/core:" + strings.Split(version.Version, " ")[0] c.Core.Name = "core" - c.Core.RootPath = filepath.Join(home, ".mesg") + c.Core.Path = filepath.Join(home, ".mesg") c.Docker.Core.Path = "/mesg" c.Docker.Socket = "/var/run/docker.sock" return &c, nil } -// CorePath returns the path based on the Core.Name -func (c *Config) CorePath() string { - return filepath.Join(c.Core.RootPath, c.Core.Name) -} - // Global returns a singleton of a Config after loaded ENV and validate the values. func Global() (*Config, error) { var err error @@ -107,7 +102,7 @@ func (c *Config) Load() error { // Prepare setups local directories or any other required thing based on config func (c *Config) Prepare() error { - return os.MkdirAll(c.CorePath(), os.FileMode(0755)) + return os.MkdirAll(c.Core.Path, os.FileMode(0755)) } // Validate checks values and return an error if any validation failed. @@ -128,6 +123,6 @@ func (c *Config) DaemonEnv() map[string]string { "MESG_LOG_FORMAT": c.Log.Format, "MESG_LOG_LEVEL": c.Log.Level, "MESG_CORE_NAME": c.Core.Name, - "MESG_CORE_ROOTPATH": c.Docker.Core.Path, + "MESG_CORE_PATH": c.Docker.Core.Path, } } diff --git a/config/config_test.go b/config/config_test.go index fc4e7b989..e000e4195 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -18,12 +18,11 @@ func TestDefaultValue(t *testing.T) { require.Equal(t, "localhost:50052", c.Client.Address) require.Equal(t, "text", c.Log.Format) require.Equal(t, "info", c.Log.Level) - require.Equal(t, filepath.Join(home, ".mesg"), c.Core.RootPath) + require.Equal(t, filepath.Join(home, ".mesg"), c.Core.Path) require.Equal(t, "core", c.Core.Name) require.Equal(t, "/mesg", c.Docker.Core.Path) require.Equal(t, "/var/run/docker.sock", c.Docker.Socket) require.True(t, strings.HasPrefix(c.Core.Image, "mesg/core:")) - require.Equal(t, filepath.Join(home, ".mesg", c.Core.Name), c.CorePath()) } func TestGlobal(t *testing.T) { @@ -83,5 +82,5 @@ func TestDaemonEnv(t *testing.T) { require.Equal(t, c.Log.Level, env["MESG_LOG_LEVEL"]) require.Equal(t, c.Log.Format, env["MESG_LOG_FORMAT"]) require.Equal(t, c.Core.Name, env["MESG_CORE_NAME"]) - require.Equal(t, c.Docker.Core.Path, env["MESG_CORE_ROOTPATH"]) + require.Equal(t, c.Docker.Core.Path, env["MESG_CORE_PATH"]) } diff --git a/daemon/start.go b/daemon/start.go index cc82728eb..858febbf1 100644 --- a/daemon/start.go +++ b/daemon/start.go @@ -1,8 +1,6 @@ package daemon import ( - "path/filepath" - "github.com/mesg-foundation/core/config" "github.com/mesg-foundation/core/container" "github.com/mesg-foundation/core/x/xnet" @@ -45,8 +43,8 @@ func serviceSpec() (spec container.ServiceOptions, err error) { Bind: true, }, { - Source: c.CorePath(), - Target: filepath.Join(c.Docker.Core.Path, c.Core.Name), + Source: c.Core.Path, + Target: c.Docker.Core.Path, Bind: true, }, }, diff --git a/daemon/start_test.go b/daemon/start_test.go index be7ca8f0c..5b2d73ee9 100644 --- a/daemon/start_test.go +++ b/daemon/start_test.go @@ -1,7 +1,6 @@ package daemon import ( - "path/filepath" "testing" "github.com/mesg-foundation/core/config" @@ -42,7 +41,7 @@ func TestStartConfig(t *testing.T) { // Make sure that the config directory is passed in parameter to write on the same folder require.Contains(t, spec.Env, "MESG_LOG_LEVEL=info") require.Contains(t, spec.Env, "MESG_LOG_FORMAT=text") - require.Contains(t, spec.Env, "MESG_CORE_ROOTPATH="+c.Docker.Core.Path) + require.Contains(t, spec.Env, "MESG_CORE_PATH="+c.Docker.Core.Path) // Ensure that the port is shared _, port, _ := xnet.SplitHostPort(c.Server.Address) require.Equal(t, spec.Ports[0].Published, uint32(port)) @@ -52,7 +51,7 @@ func TestStartConfig(t *testing.T) { require.Equal(t, spec.Mounts[0].Target, c.Docker.Socket) require.True(t, spec.Mounts[0].Bind) // Ensure that the host users folder is sync with the core - require.Equal(t, spec.Mounts[1].Source, c.CorePath()) - require.Equal(t, spec.Mounts[1].Target, filepath.Join(c.Docker.Core.Path, c.Core.Name)) + require.Equal(t, spec.Mounts[1].Source, c.Core.Path) + require.Equal(t, spec.Mounts[1].Target, c.Docker.Core.Path) require.True(t, spec.Mounts[1].Bind) } diff --git a/database/services/db.go b/database/services/db.go index d15d73c84..b26bc2bf4 100644 --- a/database/services/db.go +++ b/database/services/db.go @@ -20,7 +20,7 @@ func open() (db *leveldb.DB, err error) { if err != nil { return nil, err } - storagePath := filepath.Join(c.CorePath(), "database", "services") + storagePath := filepath.Join(c.Core.Path, "database", "services") _instance, err = leveldb.OpenFile(storagePath, nil) if err != nil { return nil, err From 580b0f57106560494bf3e9106d320db169027af9 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Mon, 24 Sep 2018 18:44:24 +0700 Subject: [PATCH 13/14] fix tests --- service/start_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/service/start_test.go b/service/start_test.go index 3d1e94bf4..4cf327048 100644 --- a/service/start_test.go +++ b/service/start_test.go @@ -94,6 +94,8 @@ func TestStartService(t *testing.T) { }) ) + c, _ := container.New() + dt.ProvideContainerList(nil, dockertest.NotFoundErr{}) dt.ProvideServiceInspectWithRaw(swarm.Service{}, nil, dockertest.NotFoundErr{}) dt.ProvideNetworkInspect(types.NetworkResource{ID: "3"}, nil) @@ -109,7 +111,7 @@ func TestStartService(t *testing.T) { lc := <-dt.LastServiceCreate() require.Equal(t, types.ServiceCreateOptions{}, lc.Options) - require.Equal(t, container.Namespace([]string{s.ID, dependencyKey}), lc.Service.Name) + require.Equal(t, c.Namespace([]string{s.ID, dependencyKey}), lc.Service.Name) } func TestStartWith2Dependencies(t *testing.T) { From f3b67c0ec45d01fa456604cc2fc39121fedd0865 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Mon, 24 Sep 2018 18:47:12 +0700 Subject: [PATCH 14/14] dont create useless container in test --- service/start_test.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/service/start_test.go b/service/start_test.go index 4cf327048..6b786f7c3 100644 --- a/service/start_test.go +++ b/service/start_test.go @@ -5,7 +5,6 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/swarm" - "github.com/mesg-foundation/core/container" "github.com/mesg-foundation/core/container/dockertest" "github.com/mesg-foundation/core/x/xstrings" "github.com/stretchr/testify/require" @@ -94,8 +93,6 @@ func TestStartService(t *testing.T) { }) ) - c, _ := container.New() - dt.ProvideContainerList(nil, dockertest.NotFoundErr{}) dt.ProvideServiceInspectWithRaw(swarm.Service{}, nil, dockertest.NotFoundErr{}) dt.ProvideNetworkInspect(types.NetworkResource{ID: "3"}, nil) @@ -111,7 +108,7 @@ func TestStartService(t *testing.T) { lc := <-dt.LastServiceCreate() require.Equal(t, types.ServiceCreateOptions{}, lc.Options) - require.Equal(t, c.Namespace([]string{s.ID, dependencyKey}), lc.Service.Name) + require.Equal(t, s.docker.Namespace([]string{s.ID, dependencyKey}), lc.Service.Name) } func TestStartWith2Dependencies(t *testing.T) {