Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(upgrade): check for existing 2.x CLI configs file #19969

Merged
merged 4 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

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 @@ -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',
},
{
Expand Down