Skip to content
This repository has been archived by the owner on Oct 27, 2023. It is now read-only.

Commit

Permalink
Add XDG override for config directory
Browse files Browse the repository at this point in the history
Signed-off-by: Renaud Gaubert <rgaubert@nvidia.com>
  • Loading branch information
Renaud Gaubert committed Aug 7, 2019
1 parent dc6b7df commit f6b61c4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
16 changes: 14 additions & 2 deletions runtime/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"os"
"os/exec"
"path"
"strings"
"syscall"

Expand All @@ -15,10 +16,16 @@ import (
)

const (
configFilePath = "/etc/nvidia-container-runtime/config.toml"
configOverride = "XDG_CONFIG_HOME"
configFilePath = "nvidia-container-runtime/config.toml"

hookDefaultFilePath = "/usr/bin/nvidia-container-runtime-hook"
)

var (
configDir = "/etc/"
)

var fileLogger *log.Logger = nil

type args struct {
Expand All @@ -33,6 +40,12 @@ type config struct {
func getConfig() (*config, error) {
cfg := &config{}

if XDGConfigDir := os.Getenv(configOverride); len(XDGConfigDir) != 0 {
configDir = XDGConfigDir
}

configFilePath := path.Join(configDir, configFilePath)

tomlContent, err := ioutil.ReadFile(configFilePath)
if err != nil {
return nil, err
Expand Down Expand Up @@ -128,7 +141,6 @@ func addNVIDIAHook(spec *specs.Spec) error {
}

func main() {

cfg, err := getConfig()
exitOnError(err, "fail to get config")

Expand Down
22 changes: 22 additions & 0 deletions runtime/src/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
"testing"
Expand Down Expand Up @@ -192,3 +193,24 @@ func nvidiaHookCount(hooks *specs.Hooks) int {
}
return count
}

func TestGetConfigWithCustomConfig(t *testing.T) {
wd, err := os.Getwd()
require.NoError(t, err)

// By default debug is disabled
contents := []byte("[nvidia-container-runtime]\ndebug = \"/nvidia-container-toolkit.log\"")
testDir := path.Join(wd, "test")
filename := path.Join(testDir, configFilePath)

os.Setenv(configOverride, testDir)

require.NoError(t, os.MkdirAll(filepath.Dir(filename), 0766))
require.NoError(t, ioutil.WriteFile(filename, contents, 0766))

defer func() { require.NoError(t, os.RemoveAll(testDir)) } ()

cfg, err := getConfig()
require.NoError(t, err)
require.Equal(t, cfg.debugFilePath, "/nvidia-container-toolkit.log")
}

0 comments on commit f6b61c4

Please sign in to comment.