From 3412d3d5238c602cb3ef7732f33920bf5e85bef4 Mon Sep 17 00:00:00 2001 From: psergee Date: Tue, 20 Dec 2022 20:54:04 +0300 Subject: [PATCH 1/2] tt: minor fixes in config defaults Fix max values and some default paths if the config file does not contain them. --- cli/configure/configure.go | 167 ++++++++++++++++++++++---------- cli/configure/configure_test.go | 91 +++++++++++++++-- cli/util/util.go | 10 -- cli/util/util_test.go | 8 -- 4 files changed, 199 insertions(+), 77 deletions(-) diff --git a/cli/configure/configure.go b/cli/configure/configure.go index d7284b26c..dcc506d49 100644 --- a/cli/configure/configure.go +++ b/cli/configure/configure.go @@ -37,20 +37,23 @@ const ( ) const ( - varPath = "var" - logPath = "log" - runPath = "run" - dataPath = "lib" -) - -var ( - varDataPath = filepath.Join(varPath, dataPath) - varLogPath = filepath.Join(varPath, logPath) - varRunPath = filepath.Join(varPath, runPath) + varPath = "var" + logPath = "log" + runPath = "run" + dataPath = "lib" binPath = "bin" includePath = "include" modulesPath = "modules" distfilesPath = "distfiles" + logMaxSize = 100 + logMaxAge = 8 + logMaxBackups = 10 +) + +var ( + varDataPath = filepath.Join(varPath, dataPath) + varLogPath = filepath.Join(varPath, logPath) + varRunPath = filepath.Join(varPath, runPath) ) var ( @@ -65,9 +68,9 @@ func getDefaultAppOpts() *config.AppOpts { InstancesEnabled: ".", RunDir: varRunPath, LogDir: varLogPath, - LogMaxSize: 100, - LogMaxAge: 8, - LogMaxBackups: 10, + LogMaxSize: logMaxSize, + LogMaxAge: logMaxAge, + LogMaxBackups: logMaxBackups, Restartable: false, DataDir: varDataPath, BinDir: binPath, @@ -75,7 +78,7 @@ func getDefaultAppOpts() *config.AppOpts { } } -// GetDefaultCliOpts returns `CliOpts`filled with default values. +// GetDefaultCliOpts returns `CliOpts` filled with default values. func GetDefaultCliOpts() *config.CliOpts { modules := config.ModulesOpts{ Directory: modulesPath, @@ -117,11 +120,100 @@ func getDefaultDaemonOpts() *config.DaemonOpts { // adjustPathWithConfigLocation adjust provided filePath with configDir. // Absolute filePath is returned as is. Relative filePath is calculated relative to configDir. // If filePath is empty, defaultDirName is appended to configDir. -func adjustPathWithConfigLocation(filePath string, configDir string, defaultDirName string) string { +func adjustPathWithConfigLocation(filePath string, configDir string, + defaultDirName string) (string, error) { if filePath == "" { - return filepath.Join(configDir, defaultDirName) + return filepath.Abs(filepath.Join(configDir, defaultDirName)) + } + if filepath.IsAbs(filePath) { + return filePath, nil + } + return filepath.Abs(filepath.Join(configDir, filePath)) +} + +// resolveConfigPaths resolves all paths in config relative to specified location, and +// sets uninitialized values to defaults. +func updateCliOpts(cliOpts *config.CliOpts, configDir string) error { + var err error + + if cliOpts.App == nil { + cliOpts.App = getDefaultAppOpts() + } + if cliOpts.Repo == nil { + cliOpts.Repo = &config.RepoOpts{ + Rocks: "", + Install: distfilesPath, + } + } + if cliOpts.Modules == nil { + cliOpts.Modules = &config.ModulesOpts{ + Directory: modulesPath, + } + } + if cliOpts.EE == nil { + cliOpts.EE = &config.EEOpts{ + CredPath: "", + } + } + + if cliOpts.App.InstancesEnabled == "" { + cliOpts.App.InstancesEnabled = "." + } else if cliOpts.App.InstancesEnabled != "." { + if cliOpts.App.InstancesEnabled, err = + adjustPathWithConfigLocation(cliOpts.App.InstancesEnabled, configDir, ""); err != nil { + return err + } + } + if cliOpts.App.RunDir, err = adjustPathWithConfigLocation(cliOpts.App.RunDir, + configDir, varRunPath); err != nil { + return err + } + if cliOpts.App.LogDir, err = adjustPathWithConfigLocation(cliOpts.App.LogDir, + configDir, varLogPath); err != nil { + return err + } + if cliOpts.App.DataDir, err = adjustPathWithConfigLocation(cliOpts.App.DataDir, + configDir, varDataPath); err != nil { + return err + } + if cliOpts.App.BinDir, err = adjustPathWithConfigLocation(cliOpts.App.BinDir, + configDir, binPath); err != nil { + return err + } + if cliOpts.App.IncludeDir, err = adjustPathWithConfigLocation(cliOpts.App.IncludeDir, + configDir, includePath); err != nil { + return err + } + if cliOpts.Repo.Install, err = adjustPathWithConfigLocation(cliOpts.Repo.Install, + configDir, distfilesPath); err != nil { + return err + } + + if cliOpts.Modules != nil { + if cliOpts.Modules.Directory, err = adjustPathWithConfigLocation(cliOpts.Modules.Directory, + configDir, modulesPath); err != nil { + return err + } + } + + for i := range cliOpts.Templates { + if cliOpts.Templates[i].Path, err = adjustPathWithConfigLocation( + cliOpts.Templates[i].Path, configDir, "."); err != nil { + return err + } + } + + if cliOpts.App.LogMaxAge == 0 { + cliOpts.App.LogMaxAge = logMaxAge + } + if cliOpts.App.LogMaxSize == 0 { + cliOpts.App.LogMaxSize = logMaxSize } - return util.GetAbsPath(configDir, filePath) + if cliOpts.App.LogMaxBackups == 0 { + cliOpts.App.LogMaxBackups = logMaxBackups + } + + return nil } // GetCliOpts returns Tarantool CLI options from the config file @@ -158,47 +250,16 @@ func GetCliOpts(configurePath string) (*config.CliOpts, string, error) { if configPath == "" { configDir, err = os.Getwd() if err != nil { - return cfg.CliConfig, "", err + return cfg.CliConfig, configPath, err } } else { - configDir = filepath.Dir(configPath) - } - - if cfg.CliConfig.App == nil { - cfg.CliConfig.App = getDefaultAppOpts() - } - if cfg.CliConfig.Repo == nil { - cfg.CliConfig.Repo = &config.RepoOpts{ - Rocks: "", - Install: distfilesPath, + if configDir, err = filepath.Abs(filepath.Dir(configPath)); err != nil { + return cfg.CliConfig, configPath, err } } - if cfg.CliConfig.App.InstancesEnabled != "." { - cfg.CliConfig.App.InstancesEnabled = - adjustPathWithConfigLocation(cfg.CliConfig.App.InstancesEnabled, configDir, "") - } - cfg.CliConfig.App.RunDir = adjustPathWithConfigLocation(cfg.CliConfig.App.RunDir, - configDir, varRunPath) - cfg.CliConfig.App.LogDir = adjustPathWithConfigLocation(cfg.CliConfig.App.LogDir, - configDir, varLogPath) - cfg.CliConfig.App.DataDir = adjustPathWithConfigLocation(cfg.CliConfig.App.DataDir, - configDir, varDataPath) - cfg.CliConfig.App.BinDir = adjustPathWithConfigLocation(cfg.CliConfig.App.BinDir, - configDir, binPath) - cfg.CliConfig.App.IncludeDir = adjustPathWithConfigLocation(cfg.CliConfig.App.IncludeDir, - configDir, includePath) - cfg.CliConfig.Repo.Install = adjustPathWithConfigLocation(cfg.CliConfig.Repo.Install, - configDir, distfilesPath) - - if cfg.CliConfig.Modules != nil { - cfg.CliConfig.Modules.Directory = util.GetAbsPath(configDir, - cfg.CliConfig.Modules.Directory) - } - - for i := range cfg.CliConfig.Templates { - cfg.CliConfig.Templates[i].Path = adjustPathWithConfigLocation( - cfg.CliConfig.Templates[i].Path, configDir, ".") + if err = updateCliOpts(cfg.CliConfig, configDir); err != nil { + return cfg.CliConfig, "", err } return cfg.CliConfig, configPath, nil diff --git a/cli/configure/configure_test.go b/cli/configure/configure_test.go index 21cac56c8..b0cf7a7db 100644 --- a/cli/configure/configure_test.go +++ b/cli/configure/configure_test.go @@ -97,12 +97,66 @@ func TestConfigureCli(t *testing.T) { } func TestAdjustPathWithConfigLocation(t *testing.T) { - require.Equal(t, adjustPathWithConfigLocation("", "/config/dir", "bin"), - "/config/dir/bin") - require.Equal(t, adjustPathWithConfigLocation("/bin_dir", "/config/dir", "bin"), - "/bin_dir") - require.Equal(t, adjustPathWithConfigLocation("./bin_dir", "/config/dir", "bin"), - "/config/dir/bin_dir") + type args struct { + filePath string + baseDir string + defaultPath string + } + + cwd, err := os.Getwd() + require.NoError(t, err) + + tests := []struct { + name string + args args + wantPath string + wantErr bool + }{ + { + name: "Test empty file path", + args: args{"", "/abs/path", "bin"}, + wantPath: "/abs/path/bin", + wantErr: false, + }, + { + name: "Test absolute file path", + args: args{"/abs/my_bin", "/base/dir", "bin"}, + wantPath: "/abs/my_bin", + wantErr: false, + }, + { + name: "Test relative file path", + args: args{"./rel/my_bin", "/base/dir", "bin"}, + wantPath: "/base/dir/rel/my_bin", + wantErr: false, + }, + { + name: "Test relative file path without dot", + args: args{"rel/my_bin", "/base/dir", "bin"}, + wantPath: "/base/dir/rel/my_bin", + wantErr: false, + }, + { + name: "Test relative base dir", + args: args{"my_bin", "rel/path", "bin"}, + wantPath: filepath.Join(cwd, "rel/path/my_bin"), + wantErr: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + str, err := adjustPathWithConfigLocation(tt.args.filePath, tt.args.baseDir, + tt.args.defaultPath) + if tt.wantErr { + require.Error(t, err) + return + } else { + require.NoError(t, err) + } + require.EqualValues(t, tt.wantPath, str) + }) + } } func TestExcludeArgs(t *testing.T) { @@ -219,3 +273,28 @@ func TestGetConfigPath(t *testing.T) { assert.Equal(t, filepath.Join(tempDir, "a", "tarantool.yml"), configName) assert.NoError(t, err) } + +func TestUpdateCliOpts(t *testing.T) { + cliOpts := config.CliOpts{ + App: &config.AppOpts{ + RunDir: "/var/run", + LogDir: "var/log", + DataDir: "./var/lib", + IncludeDir: "../include_dir", + LogMaxAge: 42, + }, + } + configDir := "/etc/tarantool" + + err := updateCliOpts(&cliOpts, configDir) + require.NoError(t, err) + assert.Equal(t, "/var/run", cliOpts.App.RunDir) + assert.Equal(t, filepath.Join(configDir, "var", "log"), cliOpts.App.LogDir) + assert.Equal(t, filepath.Join(configDir, "var", "lib"), cliOpts.App.DataDir) + assert.Equal(t, filepath.Join(configDir, "..", "include_dir"), cliOpts.App.IncludeDir) + assert.Equal(t, filepath.Join(configDir, modulesPath), cliOpts.Modules.Directory) + assert.Equal(t, ".", cliOpts.App.InstancesEnabled) + assert.Equal(t, 42, cliOpts.App.LogMaxAge) + assert.Equal(t, logMaxBackups, cliOpts.App.LogMaxBackups) + assert.Equal(t, logMaxSize, cliOpts.App.LogMaxSize) +} diff --git a/cli/util/util.go b/cli/util/util.go index fec51a9fc..7f89c9781 100644 --- a/cli/util/util.go +++ b/cli/util/util.go @@ -757,16 +757,6 @@ func CheckRequiredBinaries(binaries ...string) error { return nil } -// GetAbsPath returns absolute path of filePath. -// If filePath is absolute, it is returned as is, -// if filePath is relative, baseDir + filePath is returned. -func GetAbsPath(baseDir, filePath string) string { - if filepath.IsAbs(filePath) { - return filePath - } - return filepath.Join(baseDir, filePath) -} - // CreateDirectory create a directory with existence and error checks. func CreateDirectory(dirName string, fileMode os.FileMode) error { stat, err := os.Stat(dirName) diff --git a/cli/util/util_test.go b/cli/util/util_test.go index 14f969958..a21c49b2b 100644 --- a/cli/util/util_test.go +++ b/cli/util/util_test.go @@ -197,14 +197,6 @@ func TestSetupTarantoolPrefix(t *testing.T) { } } -func TestGetAbsPath(t *testing.T) { - require.Equal(t, GetAbsPath("/base/dir", "/abs/path"), "/abs/path") - require.Equal(t, GetAbsPath("/base/dir", "./abs/path"), - "/base/dir/abs/path") - require.Equal(t, GetAbsPath("/base/dir", "abs/path"), - "/base/dir/abs/path") -} - func TestCreateDirectory(t *testing.T) { tempDir := t.TempDir() require.NoError(t, os.Mkdir(filepath.Join(tempDir, "dir1"), 0750)) From 764199bad693cf24e2620d3a3edd08cca6afb24a Mon Sep 17 00:00:00 2001 From: psergee Date: Tue, 20 Dec 2022 20:58:19 +0300 Subject: [PATCH 2/2] cfg: implement cfg dump module cfg dump prints tt environment configuration with all paths resolved. With --raw flag set, it prints config file contents as is. Closes #135 --- CHANGELOG.md | 1 + README.rst | 1 + cli/cfg/dump.go | 59 ++++++++++++++++ cli/cfg/dump_test.go | 109 ++++++++++++++++++++++++++++ cli/cfg/testdata/tt_cfg.yaml | 6 ++ cli/cmd/cfg.go | 26 +++++++ cli/cmd/cfg_dump.go | 42 +++++++++++ cli/cmd/root.go | 1 + test/integration/cfg/test_dump.py | 114 ++++++++++++++++++++++++++++++ test/integration/cfg/tt_cfg.yaml | 9 +++ 10 files changed, 368 insertions(+) create mode 100644 cli/cfg/dump.go create mode 100644 cli/cfg/dump_test.go create mode 100644 cli/cfg/testdata/tt_cfg.yaml create mode 100644 cli/cmd/cfg.go create mode 100644 cli/cmd/cfg_dump.go create mode 100644 test/integration/cfg/test_dump.py create mode 100644 test/integration/cfg/tt_cfg.yaml diff --git a/CHANGELOG.md b/CHANGELOG.md index 102743d04..0130dbbe6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Added - Support of rocks repository specified in tt config. +- ``cfg dump`` module. It prints tt environment configuration. ## [0.3.0] - 2022-12-05 diff --git a/README.rst b/README.rst index 4c13a4d1c..e7e76a01d 100644 --- a/README.rst +++ b/README.rst @@ -420,3 +420,4 @@ Common description. For a detailed description, use ``tt help command`` . * ``uninstall`` - uninstall tarantool/tt. * ``init`` - create tt environment configuration file. * ``daemon (experimental)`` - manage tt daemon. +* ``cfg dump`` - print tt environment configuration. diff --git a/cli/cfg/dump.go b/cli/cfg/dump.go new file mode 100644 index 000000000..aa14422f3 --- /dev/null +++ b/cli/cfg/dump.go @@ -0,0 +1,59 @@ +package cfg + +import ( + "fmt" + "io" + "io/ioutil" + "os" + + "github.com/tarantool/tt/cli/cmdcontext" + "github.com/tarantool/tt/cli/config" + "gopkg.in/yaml.v2" +) + +// DumpCtx contains information for tt config dump. +type DumpCtx struct { + // rawDump is a dump mode flag. If set, raw contents of tt configuration file is printed. + RawDump bool +} + +// dumpRaw prints raw content of tt config file. +func dumpRaw(writer io.Writer, cmdCtx *cmdcontext.CmdCtx) error { + if cmdCtx.Cli.ConfigPath != "" { + _, err := os.Stat(cmdCtx.Cli.ConfigPath) + if err != nil { + return err + } + fileContent, err := ioutil.ReadFile(cmdCtx.Cli.ConfigPath) + if err != nil { + return err + } + writer.Write([]byte(cmdCtx.Cli.ConfigPath + ":\n")) + writer.Write(fileContent) + } else { + return fmt.Errorf("tt configuration file is not found") + } + + return nil +} + +// dumpConfiguration prints tt env configuration with all resolved paths. +func dumpConfiguration(writer io.Writer, cmdCtx *cmdcontext.CmdCtx, + cliOpts *config.CliOpts) error { + if cmdCtx.Cli.ConfigPath != "" { + if _, err := os.Stat(cmdCtx.Cli.ConfigPath); err == nil { + writer.Write([]byte(cmdCtx.Cli.ConfigPath + ":\n")) + } + } + err := yaml.NewEncoder(writer).Encode(config.Config{CliConfig: cliOpts}) + return err +} + +// RunDump prints tt configuration. +func RunDump(writer io.Writer, cmdCtx *cmdcontext.CmdCtx, dumpCtx *DumpCtx, + cliOpts *config.CliOpts) error { + if dumpCtx.RawDump { + return dumpRaw(writer, cmdCtx) + } + return dumpConfiguration(writer, cmdCtx, cliOpts) +} diff --git a/cli/cfg/dump_test.go b/cli/cfg/dump_test.go new file mode 100644 index 000000000..8a75117fa --- /dev/null +++ b/cli/cfg/dump_test.go @@ -0,0 +1,109 @@ +package cfg + +import ( + "bytes" + "fmt" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + "github.com/tarantool/tt/cli/cmdcontext" + "github.com/tarantool/tt/cli/config" + "github.com/tarantool/tt/cli/configure" +) + +func TestRunDump(t *testing.T) { + type args struct { + cmdCtx *cmdcontext.CmdCtx + dumpCtx *DumpCtx + cliOpts *config.CliOpts + } + + cliOpts, configPath, err := configure.GetCliOpts("./testdata/tt_cfg.yaml") + require.NoError(t, err) + require.Equal(t, "testdata/tt_cfg.yaml", configPath) + + cwd, err := os.Getwd() + require.NoError(t, err) + configDir := filepath.Join(cwd, "testdata") + + tests := []struct { + name string + args args + wantWriter string + wantErr bool + }{ + { + name: "Raw dump of configuration file", + args: args{ + &cmdcontext.CmdCtx{ + Cli: cmdcontext.CliCtx{ + ConfigPath: "./testdata/tt_cfg.yaml", + }, + }, + &DumpCtx{RawDump: true}, + configure.GetDefaultCliOpts(), + }, + wantWriter: `./testdata/tt_cfg.yaml: +tt: + app: + inc_dir: ./test_inc + log_maxsize: 1024 + modules: + directory: /root/modules +`, + wantErr: false, + }, + { + name: "Default config dump", + args: args{ + &cmdcontext.CmdCtx{ + Cli: cmdcontext.CliCtx{ + ConfigPath: "./testdata/tt_cfg.yaml", + }, + }, + &DumpCtx{RawDump: false}, + cliOpts, + }, + wantWriter: fmt.Sprintf(`./testdata/tt_cfg.yaml: +tt: + modules: + directory: /root/modules + app: + run_dir: %[1]s/var/run + log_dir: %[1]s/var/log + log_maxsize: 1024 + log_maxage: 8 + log_maxbackups: 10 + restart_on_failure: false + data_dir: %[1]s/var/lib + bin_dir: %[1]s/bin + inc_dir: %[1]s/test_inc + instances_enabled: . + ee: + credential_path: "" + templates: [] + repo: + rocks: "" + distfiles: %[1]s/distfiles +`, configDir), + wantErr: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + writer := &bytes.Buffer{} + err := RunDump(writer, tt.args.cmdCtx, tt.args.dumpCtx, tt.args.cliOpts) + if tt.wantErr { + require.Error(t, err) + return + } else { + require.NoError(t, err) + } + gotWriter := writer.String() + require.EqualValues(t, tt.wantWriter, gotWriter) + }) + } +} diff --git a/cli/cfg/testdata/tt_cfg.yaml b/cli/cfg/testdata/tt_cfg.yaml new file mode 100644 index 000000000..74b6c555a --- /dev/null +++ b/cli/cfg/testdata/tt_cfg.yaml @@ -0,0 +1,6 @@ +tt: + app: + inc_dir: ./test_inc + log_maxsize: 1024 + modules: + directory: /root/modules diff --git a/cli/cmd/cfg.go b/cli/cmd/cfg.go new file mode 100644 index 000000000..29514b96f --- /dev/null +++ b/cli/cmd/cfg.go @@ -0,0 +1,26 @@ +package cmd + +import ( + "github.com/spf13/cobra" +) + +// NewCfgCmd creates a new cfg command. +func NewCfgCmd() *cobra.Command { + var cfgCmd = &cobra.Command{ + Use: "cfg [command flags]", + DisableFlagParsing: true, + DisableFlagsInUseLine: true, + Short: "Environment configuration management utility", + Run: func(cmd *cobra.Command, args []string) { + cmd.Help() + }, + Example: `# Print tt environment configuration: + + $ tt cfg dump`, + } + cfgCmd.AddCommand( + NewDumpCmd(), + ) + + return cfgCmd +} diff --git a/cli/cmd/cfg_dump.go b/cli/cmd/cfg_dump.go new file mode 100644 index 000000000..933857736 --- /dev/null +++ b/cli/cmd/cfg_dump.go @@ -0,0 +1,42 @@ +package cmd + +import ( + "os" + + "github.com/spf13/cobra" + "github.com/tarantool/tt/cli/cfg" + "github.com/tarantool/tt/cli/cmdcontext" + "github.com/tarantool/tt/cli/modules" +) + +var ( + rawDump bool +) + +// NewDumpCmd creates a new dump command. +func NewDumpCmd() *cobra.Command { + var dumpCmd = &cobra.Command{ + Use: "dump", + Short: "Print environment configuration", + Run: func(cmd *cobra.Command, args []string) { + cmdCtx.CommandName = cmd.Name() + err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, + internalDumpModule, args) + handleCmdErr(cmd, err) + }, + } + + dumpCmd.Flags().BoolVarP(&rawDump, "raw", "r", false, + "Display the raw contents of tt environment config.") + + return dumpCmd +} + +// internalDumpModule is a default dump module. +func internalDumpModule(cmdCtx *cmdcontext.CmdCtx, args []string) error { + dumpCtx := cfg.DumpCtx{ + RawDump: rawDump, + } + + return cfg.RunDump(os.Stdout, cmdCtx, &dumpCtx, cliOpts) +} diff --git a/cli/cmd/root.go b/cli/cmd/root.go index b3c8fdc20..bd088d2c8 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -120,6 +120,7 @@ func NewCmdRoot() *cobra.Command { NewPackCmd(), NewInitCmd(), NewDaemonCmd(), + NewCfgCmd(), ) if err := injectCmds(rootCmd); err != nil { panic(err.Error()) diff --git a/test/integration/cfg/test_dump.py b/test/integration/cfg/test_dump.py new file mode 100644 index 000000000..146bce687 --- /dev/null +++ b/test/integration/cfg/test_dump.py @@ -0,0 +1,114 @@ +import os +import shutil +import subprocess + + +def test_cfg_dump_default(tt_cmd, tmpdir): + shutil.copy(os.path.join(os.path.dirname(__file__), "tt_cfg.yaml"), + os.path.join(tmpdir, "tarantool.yaml")) + + buid_cmd = [tt_cmd, "cfg", "dump"] + tt_process = subprocess.Popen( + buid_cmd, + cwd=tmpdir, + stderr=subprocess.STDOUT, + stdout=subprocess.PIPE, + stdin=subprocess.PIPE, + text=True + ) + tt_process.stdin.close() + tt_process.wait() + assert tt_process.returncode == 0 + + output = tt_process.stdout.read() + assert "bin_dir: /usr/bin" in output + assert "run_dir: /var/run" in output + assert f"data_dir: {os.path.join(tmpdir, 'var', 'lib')}" in output + assert f"log_dir: {os.path.join(tmpdir, 'var', 'log')}" in output + assert f"inc_dir: {os.path.join(tmpdir, 'include')}" in output + assert f"directory: {os.path.join(tmpdir, 'new_modules')}" in output + assert f"distfiles: {os.path.join(tmpdir, 'distfiles')}" in output + assert "log_maxsize: 100" in output + assert "log_maxbackups: 12" in output + assert "instances_enabled: ." in output + assert "templates: []" in output + assert 'credential_path: ""' in output + + +def test_cfg_dump_raw(tt_cmd, tmpdir): + shutil.copy(os.path.join(os.path.dirname(__file__), "tt_cfg.yaml"), + os.path.join(tmpdir, "tarantool.yaml")) + + buid_cmd = [tt_cmd, "cfg", "dump", "--raw"] + tt_process = subprocess.Popen( + buid_cmd, + cwd=tmpdir, + stderr=subprocess.STDOUT, + stdout=subprocess.PIPE, + stdin=subprocess.PIPE, + text=True + ) + tt_process.stdin.close() + tt_process.wait() + assert tt_process.returncode == 0 + + output = tt_process.stdout.read() + assert output == f"""{os.path.join(tmpdir, "tarantool.yaml")}: +tt: + modules: + directory: new_modules + app: + run_dir: /var/run + log_dir: ./var/log + log_maxbackups: 12 + data_dir: var/lib + bin_dir: /usr/bin +""" + + +def test_cfg_dump_no_config(tt_cmd, tmpdir): + buid_cmd = [tt_cmd, "cfg", "dump", "--raw"] + tt_process = subprocess.Popen( + buid_cmd, + cwd=tmpdir, + stderr=subprocess.STDOUT, + stdout=subprocess.PIPE, + stdin=subprocess.PIPE, + text=True + ) + tt_process.stdin.close() + tt_process.wait() + assert tt_process.returncode == 1 + + output = tt_process.stdout.read() + assert "tt configuration file is not found" in output + + +def test_cfg_dump_default_no_config(tt_cmd, tmpdir): + buid_cmd = [tt_cmd, "cfg", "dump"] + tt_process = subprocess.Popen( + buid_cmd, + cwd=tmpdir, + stderr=subprocess.STDOUT, + stdout=subprocess.PIPE, + stdin=subprocess.PIPE, + text=True + ) + tt_process.stdin.close() + tt_process.wait() + assert tt_process.returncode == 0 + + output = tt_process.stdout.read() + print(output) + assert f"bin_dir: {os.path.join(tmpdir, 'bin')}" in output + assert f"run_dir: {os.path.join(tmpdir, 'var', 'run')}" in output + assert f"data_dir: {os.path.join(tmpdir, 'var', 'lib')}" in output + assert f"log_dir: {os.path.join(tmpdir, 'var', 'log')}" in output + assert f"inc_dir: {os.path.join(tmpdir, 'include')}" in output + assert f"directory: {os.path.join(tmpdir, 'modules')}" in output + assert f"distfiles: {os.path.join(tmpdir, 'distfiles')}" in output + assert "log_maxsize: 100" in output + assert "log_maxbackups: 10" in output + assert "instances_enabled: ." in output + assert f"templates:\n - path: {os.path.join(tmpdir, 'templates')}" in output + assert 'credential_path: ""' in output diff --git a/test/integration/cfg/tt_cfg.yaml b/test/integration/cfg/tt_cfg.yaml new file mode 100644 index 000000000..15bcfd70e --- /dev/null +++ b/test/integration/cfg/tt_cfg.yaml @@ -0,0 +1,9 @@ +tt: + modules: + directory: new_modules + app: + run_dir: /var/run + log_dir: ./var/log + log_maxbackups: 12 + data_dir: var/lib + bin_dir: /usr/bin