Skip to content

Commit

Permalink
Migration to the new default config path
Browse files Browse the repository at this point in the history
  • Loading branch information
codebien committed Jan 30, 2025
1 parent 89e6f91 commit d93279c
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 9 deletions.
41 changes: 36 additions & 5 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@ func getConfig(flags *pflag.FlagSet) (Config, error) {
}, nil
}

func legacyConfigFilePath(gs *state.GlobalState) string {
return filepath.Join(gs.UserOSConfigDir, "loadimpact", "k6", "config.json")
}

// Reads the configuration file from the supplied filesystem and returns it or
// an error. The only situation in which an error won't be returned is if the
// user didn't explicitly specify a config file path and the default config file
Expand Down Expand Up @@ -155,6 +151,10 @@ func readDiskConfig(gs *state.GlobalState) (Config, error) {
return conf, nil
}

func legacyConfigFilePath(gs *state.GlobalState) string {
return filepath.Join(gs.UserOSConfigDir, "loadimpact", "k6", "config.json")
}

func readLegacyDiskConfig(gs *state.GlobalState) (Config, error) {
// Try to see if the legacy config exists in the supplied filesystem
legacyPath := filepath.Join(gs.UserOSConfigDir, "loadimpact", "k6", "config.json")
Expand Down Expand Up @@ -218,7 +218,7 @@ func getConsolidatedConfig(gs *state.GlobalState, cliConf Config, runnerOpts lib
} else if err != nil {
return conf, errext.WithExitCodeIfNone(err, exitcodes.InvalidConfig)
} else {
gs.Logger.Warn("The configuration file has been found on the old path. Please, run again `k6 cloud login` or `k6 login` commands to migrate it to the new path. If you migrated it manually, then remove the old config file.")
gs.Logger.Warn("The configuration file has been found on the old path. Please, run again `k6 cloud login` or `k6 login` commands to migrate to the new path. If you already migrated it manually, then remove the file from the old path.\n\n")
}

envConf, err := readEnvConfig(gs.Env)
Expand Down Expand Up @@ -333,3 +333,34 @@ func validateScenarioConfig(conf lib.ExecutorConfig, isExecutable func(string) b
}
return nil
}

// migrateLegacyConfigFileIfAny moves the configuration file from
// the old default `~/.config/loadimpact/...` folder
// to the new `~/.config/k6/...` default folder.
func migrateLegacyConfigFileIfAny(gs *state.GlobalState) error {
fn := func() error {
legacyFpath := legacyConfigFilePath(gs)
if _, err := gs.FS.Stat(legacyFpath); errors.Is(err, fs.ErrNotExist) {
return nil
} else if err != nil {
return err
}

if err := gs.FS.MkdirAll(filepath.Dir(gs.Flags.ConfigFilePath), 0o755); err != nil {
return err
}

err := gs.FS.Rename(legacyFpath, gs.Flags.ConfigFilePath)
if err != nil {
return err
}

gs.Logger.Infof("Note, the configuration file has been migrated "+
"from old default path (%q) to the new version (%q).\n\n", legacyFpath, gs.Flags.ConfigFilePath)
return nil
}
if err := fn(); err != nil {
return fmt.Errorf("move from the old to the new configuration's filepath failed: %w", err)
}
return nil
}
55 changes: 51 additions & 4 deletions internal/cmd/config_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package cmd

import (
"io"
"io/fs"
"testing"
"time"

"github.com/mstoykov/envconfig"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/guregu/null.v3"
Expand Down Expand Up @@ -211,7 +213,7 @@ func TestReadDiskConfigWithDefaultFlags(t *testing.T) {
memfs := fsext.NewMemMapFs()

conf := []byte(`{"iterations":1028,"cloud":{"field1":"testvalue"}}`)
defaultConfigPath := ".config/loadimpact/k6/config.json"
defaultConfigPath := ".config/k6/config.json"
fsext.WriteFile(memfs, defaultConfigPath, conf, 0o644)

Check failure on line 217 in internal/cmd/config_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `fsext.WriteFile` is not checked (errcheck)

defaultFlags := state.GetDefaultFlags(".config")
Expand Down Expand Up @@ -292,7 +294,7 @@ func TestReadDiskConfigNotJSONContentError(t *testing.T) {
memfs := fsext.NewMemMapFs()

conf := []byte(`bad json format`)
defaultConfigPath := ".config/loadimpact/k6/config.json"
defaultConfigPath := ".config/k6/config.json"
fsext.WriteFile(memfs, defaultConfigPath, conf, 0o644)

Check failure on line 298 in internal/cmd/config_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `fsext.WriteFile` is not checked (errcheck)

gs := &state.GlobalState{
Expand Down Expand Up @@ -333,7 +335,7 @@ func TestWriteDiskConfigWithDefaultFlags(t *testing.T) {
err := writeDiskConfig(gs, c)
require.NoError(t, err)

finfo, err := memfs.Stat(".config/loadimpact/k6/config.json")
finfo, err := memfs.Stat(".config/k6/config.json")
require.NoError(t, err)
assert.NotEmpty(t, finfo.Size())
}
Expand All @@ -342,7 +344,7 @@ func TestWriteDiskConfigOverwrite(t *testing.T) {
memfs := fsext.NewMemMapFs()

conf := []byte(`{"iterations":1028,"cloud":{"field1":"testvalue"}}`)
defaultConfigPath := ".config/loadimpact/k6/config.json"
defaultConfigPath := ".config/k6/config.json"
fsext.WriteFile(memfs, defaultConfigPath, conf, 0o644)

Check failure on line 348 in internal/cmd/config_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `fsext.WriteFile` is not checked (errcheck)

defaultFlags := state.GetDefaultFlags(".config")
Expand Down Expand Up @@ -391,3 +393,48 @@ func TestWriteDiskConfigNoJSONContentError(t *testing.T) {
err := writeDiskConfig(gs, c)
assert.ErrorContains(t, err, "json: error")
}

func TestMigrateLegacyConfigFileIfAny(t *testing.T) {
memfs := fsext.NewMemMapFs()

conf := []byte(`{"iterations":1028,"cloud":{"field1":"testvalue"}}`)
legacyConfigPath := ".config/loadimpact/k6/config.json"
fsext.WriteFile(memfs, legacyConfigPath, conf, 0o644)

Check failure on line 402 in internal/cmd/config_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `fsext.WriteFile` is not checked (errcheck)

logger := logrus.New()
logger.SetOutput(io.Discard)

defaultFlags := state.GetDefaultFlags(".config")
gs := &state.GlobalState{
FS: memfs,
Flags: defaultFlags,
DefaultFlags: defaultFlags,
UserOSConfigDir: ".config",
Logger: logger,
}

err := migrateLegacyConfigFileIfAny(gs)
require.NoError(t, err)

f, err := fsext.ReadFile(memfs, ".config/k6/config.json")
require.NoError(t, err)
assert.Equal(t, f, conf)

_, err = memfs.Stat(legacyConfigPath)
assert.ErrorIs(t, err, fs.ErrNotExist)
}

func TestMigrateLegacyConfigFileIfAnyWhenFileDoesNotExist(t *testing.T) {
memfs := fsext.NewMemMapFs()

defaultFlags := state.GetDefaultFlags(".config")
gs := &state.GlobalState{
FS: memfs,
Flags: defaultFlags,
DefaultFlags: defaultFlags,
UserOSConfigDir: ".config",
}

err := migrateLegacyConfigFileIfAny(gs)
require.NoError(t, err)
}

0 comments on commit d93279c

Please sign in to comment.