From 7ed063765619b6c17be444b7d023488fbfc06095 Mon Sep 17 00:00:00 2001 From: Ales Pour Date: Tue, 10 Nov 2020 18:56:21 +0100 Subject: [PATCH 1/3] fix(upgrade): check for existing 2.x configs file --- cmd/influxd/upgrade/setup.go | 6 +++++ cmd/influxd/upgrade/setup_test.go | 41 +++++++++++++++++++++++++++++++ cmd/influxd/upgrade/upgrade.go | 2 +- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/cmd/influxd/upgrade/setup.go b/cmd/influxd/upgrade/setup.go index 991f8c36d9d..a959278e160 100644 --- a/cmd/influxd/upgrade/setup.go +++ b/cmd/influxd/upgrade/setup.go @@ -139,6 +139,12 @@ func saveLocalConfig(sourceOptions *optionsV1, targetOptions *optionsV2, log *za if dPath == "" || dir == "" { return errors.New("a valid configurations path must be provided") } + _, err := os.Stat(dPath) + if !os.IsNotExist(err) { + log.Error("cannot save CLI config, file already exists", zap.String("path", dPath)) + return errors.New("CLI configs file already exists") + } + localConfigSVC := config.NewLocalConfigSVC(dPath, dir) p := config.DefaultConfig p.Token = targetOptions.token diff --git a/cmd/influxd/upgrade/setup_test.go b/cmd/influxd/upgrade/setup_test.go index 6e01a0ce9cd..41bcc297d7a 100644 --- a/cmd/influxd/upgrade/setup_test.go +++ b/cmd/influxd/upgrade/setup_test.go @@ -1,7 +1,9 @@ package upgrade import ( + "os" "path/filepath" + "strings" "testing" "github.com/google/go-cmp/cmp" @@ -81,3 +83,42 @@ func TestLocalConfig(t *testing.T) { }) } } + +func TestLocalConfigErr(t *testing.T) { + + type testCase struct { + name string + sourceOpts *optionsV1 + targetOpts *optionsV2 + errMsg string + } + + tc := testCase{ + name: "default", + sourceOpts: &optionsV1{}, + targetOpts: &optionsV2{ + orgName: "my-org", + token: "my-token", + }, + errMsg: "file already exists", + } + + dir := t.TempDir() + log := zaptest.NewLogger(t) + // adjust paths to runtime values + opts := tc.targetOpts + opts.boltPath = filepath.Join(dir, bolt.DefaultFilename) + opts.configsPath = filepath.Join(dir, "configs") + opts.enginePath = filepath.Join(dir, "engine") + // create configs file + f, err := os.Create(opts.configsPath) + require.NoError(t, err) + f.WriteString("[meta]\n[data]\n") + f.Close() + // execute op - error should occur + err = saveLocalConfig(tc.sourceOpts, opts, log) + require.Error(t, err) + if !strings.Contains(err.Error(), tc.errMsg) { + t.Fatalf("unexpected error: %v", err) + } +} diff --git a/cmd/influxd/upgrade/upgrade.go b/cmd/influxd/upgrade/upgrade.go index bec848a001b..326c7095667 100644 --- a/cmd/influxd/upgrade/upgrade.go +++ b/cmd/influxd/upgrade/upgrade.go @@ -181,7 +181,7 @@ func NewCommand() *cobra.Command { DestP: &options.target.configsPath, Flag: "influx-configs-path", Default: filepath.Join(v2dir, "configs"), - Desc: "path for CLI configurations", + Desc: "path to 2.x CLI configurations file", Short: 'c', }, { From fc5a89e400363abdc9eb22965e523f1bcb7e3019 Mon Sep 17 00:00:00 2001 From: Ales Pour Date: Tue, 10 Nov 2020 19:07:24 +0100 Subject: [PATCH 2/3] docs: add PR #19969 reference --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index caef7afdaee..3ec23aeb362 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ 1. [19925](https://github.com/influxdata/influxdb/pull/19937): Create CLI configs in `influxd upgrade` 1. [19945](https://github.com/influxdata/influxdb/pull/19945): Allow write-only V1 tokens to find DBRPs 1. [19960](https://github.com/influxdata/influxdb/pull/19960): Remove bucket and mapping auto-creation from v1 /write API +1. [19969](https://github.com/influxdata/influxdb/pull/19969): Check if CLI configs file already exists during upgrade ## v2.0.0-rc.4 [2020-11-05] From 0a97a1ca4f27ede360cb94e03ffd13047f96c94d Mon Sep 17 00:00:00 2001 From: Daniel Moran Date: Tue, 10 Nov 2020 16:16:22 -0500 Subject: [PATCH 3/3] refactor: update description of option --- cmd/influxd/upgrade/upgrade.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/influxd/upgrade/upgrade.go b/cmd/influxd/upgrade/upgrade.go index 326c7095667..df130377be3 100644 --- a/cmd/influxd/upgrade/upgrade.go +++ b/cmd/influxd/upgrade/upgrade.go @@ -181,7 +181,7 @@ func NewCommand() *cobra.Command { DestP: &options.target.configsPath, Flag: "influx-configs-path", Default: filepath.Join(v2dir, "configs"), - Desc: "path to 2.x CLI configurations file", + Desc: "path for 2.x CLI configurations file", Short: 'c', }, {