diff --git a/internal/config/config.go b/internal/config/config.go index 3cb7541d..f9f9d425 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -31,9 +31,9 @@ import ( const ( FilePathOverrideEnvVar = "NVCTK_CONFIG_FILE_PATH" + RelativeFilePath = "nvidia-container-runtime/config.toml" - configOverride = "XDG_CONFIG_HOME" - configFilePath = "nvidia-container-runtime/config.toml" + configRootOverride = "XDG_CONFIG_HOME" nvidiaCTKExecutable = "nvidia-ctk" nvidiaCTKDefaultFilePath = "/usr/bin/nvidia-ctk" @@ -76,11 +76,12 @@ func GetConfigFilePath() string { if configFilePathOverride := os.Getenv(FilePathOverrideEnvVar); configFilePathOverride != "" { return configFilePathOverride } - if XDGConfigDir := os.Getenv(configOverride); len(XDGConfigDir) != 0 { - return filepath.Join(XDGConfigDir, configFilePath) + configRoot := "/etc" + if XDGConfigDir := os.Getenv(configRootOverride); len(XDGConfigDir) != 0 { + configRoot = XDGConfigDir } - return filepath.Join("/etc", configFilePath) + return filepath.Join(configRoot, RelativeFilePath) } // GetConfig sets up the config struct. Values are read from a toml file diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 0873ebd2..34bf4c27 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -27,9 +27,26 @@ import ( func TestGetConfigWithCustomConfig(t *testing.T) { testDir := t.TempDir() - t.Setenv(configOverride, testDir) + t.Setenv(configRootOverride, testDir) - filename := filepath.Join(testDir, configFilePath) + filename := filepath.Join(testDir, RelativeFilePath) + + // By default debug is disabled + contents := []byte("[nvidia-container-runtime]\ndebug = \"/nvidia-container-toolkit.log\"") + + require.NoError(t, os.MkdirAll(filepath.Dir(filename), 0766)) + require.NoError(t, os.WriteFile(filename, contents, 0600)) + + cfg, err := GetConfig() + require.NoError(t, err) + require.Equal(t, "/nvidia-container-toolkit.log", cfg.NVIDIAContainerRuntimeConfig.DebugFilePath) +} + +func TestGetConfigWithConfigFilePathOverride(t *testing.T) { + testDir := t.TempDir() + filename := filepath.Join(testDir, RelativeFilePath) + + t.Setenv(FilePathOverrideEnvVar, filename) // By default debug is disabled contents := []byte("[nvidia-container-runtime]\ndebug = \"/nvidia-container-toolkit.log\"")