From 9c68ec58f78d2a492454741d053db9225fedbaeb Mon Sep 17 00:00:00 2001 From: Ace-Tang Date: Tue, 17 Jul 2018 10:38:55 +0800 Subject: [PATCH] fix: make daemon good after update damon offline 1. make Namespace field not marshal to config file to ensure daemon restart successful after update daemon offline. 2. add update daemon offline test. 3. fix update daemon error when config file is not exist. Signed-off-by: Ace-Tang --- cli/update_daemon.go | 5 +++-- daemon/config/config.go | 5 +++-- test/z_cli_daemon_test.go | 21 +++++++++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/cli/update_daemon.go b/cli/update_daemon.go index 615fc622f..0ae58fdcd 100644 --- a/cli/update_daemon.go +++ b/cli/update_daemon.go @@ -117,8 +117,9 @@ func (udc *DaemonUpdateCommand) updateDaemonConfigFile() error { } daemonConfig := &config.Config{} - if err = json.NewDecoder(bytes.NewReader(contents)).Decode(daemonConfig); err != nil { - return errors.Wrap(err, "failed to decode json: %s") + // do not return error if config file is empty + if err := json.NewDecoder(bytes.NewReader(contents)).Decode(daemonConfig); err != nil && err != io.EOF { + return errors.Wrapf(err, "failed to decode json: %s", udc.configFile) } flagSet := udc.cmd.Flags() diff --git a/daemon/config/config.go b/daemon/config/config.go index fc0346e53..9a572b769 100644 --- a/daemon/config/config.go +++ b/daemon/config/config.go @@ -114,8 +114,9 @@ type Config struct { // add a resolution later if it needed. Runtimes map[string]types.Runtime `json:"add-runtime,omitempty"` - // Namespace is passed to containerd - Namespace string + // Namespace is passed to containerd, Namespace is not a daemon flag, + // do not marshal this field to config file. + Namespace string `json:"-"` } // Validate validates the user input config. diff --git a/test/z_cli_daemon_test.go b/test/z_cli_daemon_test.go index 0d9923c6e..7be141063 100644 --- a/test/z_cli_daemon_test.go +++ b/test/z_cli_daemon_test.go @@ -470,6 +470,7 @@ func (suite *PouchDaemonSuite) TestDaemonWithMultiRuntimes(c *check.C) { dcfg2.KillDaemon() } +// TestUpdateDaemonWithLabels tests update daemon online with labels updated func (suite *PouchDaemonSuite) TestUpdateDaemonWithLabels(c *check.C) { cfg := daemon.NewConfig() err := cfg.StartDaemon() @@ -485,3 +486,23 @@ func (suite *PouchDaemonSuite) TestUpdateDaemonWithLabels(c *check.C) { updated := strings.Contains(ret.Stdout(), "aaa=bbb") c.Assert(updated, check.Equals, true) } + +// TestUpdateDaemonWithLabels tests update daemon offline +func (suite *PouchDaemonSuite) TestUpdateDaemonOffline(c *check.C) { + path := "/tmp/pouchconfig.json" + fd, err := os.Create(path) + c.Assert(err, check.IsNil) + fd.Close() + defer os.Remove(path) + + cfg := daemon.NewConfig() + err = cfg.StartDaemon() + c.Assert(err, check.IsNil) + + defer cfg.KillDaemon() + + RunWithSpecifiedDaemon(&cfg, "updatedaemon", "--config-file", path, "--offline=true").Assert(c, icmd.Success) + + ret := RunWithSpecifiedDaemon(&cfg, "info") + ret.Assert(c, icmd.Success) +}