Skip to content

Commit

Permalink
fix(upgrade): check for existing 2.x CLI configs file during upgrade (#…
Browse files Browse the repository at this point in the history
…19969)



Co-authored-by: Daniel Moran <danxmoran@gmail.com>
  • Loading branch information
alespour and danxmoran authored Nov 10, 2020
1 parent 63974e8 commit c4eb629
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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. [19972](https://github.com/influxdata/influxdb/pull/19972): Remove unused 'influx-command-path' option from upgrade command
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]

Expand Down
6 changes: 6 additions & 0 deletions cmd/influxd/upgrade/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions cmd/influxd/upgrade/setup_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package upgrade

import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion cmd/influxd/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,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 for 2.x CLI configurations file",
Short: 'c',
},
{
Expand Down

0 comments on commit c4eb629

Please sign in to comment.