Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
25 changes: 24 additions & 1 deletion api/config/v1/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,31 @@ func (o options) loadConfigToml() (*Toml, error) {
}
defer tomlFile.Close()

return loadConfigTomlFrom(tomlFile)
t, err := loadConfigTomlFrom(tomlFile)
if err != nil {
return nil, fmt.Errorf("failed to load specified config file: %w", err)
}

for _, key := range t.tree.Keys() {
t.markKeysSet(key, "")
}
return t, nil
}

func (t *Toml) markKeysSet(key string, prefix string) {
fullKey := key
if prefix != "" {
fullKey = prefix + "." + key
}

t.valuesSet[fullKey] = true

subTree := t.tree.Get(fullKey)
if nextTree, ok := subTree.(*toml.Tree); ok {
for _, nextKey := range nextTree.Keys() {
t.markKeysSet(nextKey, fullKey)
}
}
}

func defaultToml() (*Toml, error) {
Expand Down
67 changes: 67 additions & 0 deletions api/config/v1/toml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package config

import (
"bytes"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -326,6 +327,72 @@ func TestConfigFromToml(t *testing.T) {
}
}

func TestLoadConfigToml(t *testing.T) {
cfgToml, err := defaultToml()
require.NoError(t, err)

cfgToml.Set("nvidia-container-cli.ldconfig", "OVERRIDDEN")
cfgToml.Set("nvidia-container-cli.debug", "/var/log/nvidia-container-toolkit.log")
cfgToml.Set("nvidia-container-runtime.debug", "/var/log/nvidia-container-runtime.log")

tmpFile, err := os.CreateTemp("", "config.toml")
require.NoError(t, err)
defer os.Remove(tmpFile.Name())

_, err = cfgToml.Save(tmpFile)
require.NoError(t, err)
tmpFile.Close()

testCases := []struct {
description string
options options
expected string
}{
{
description: "empty filename returns default config",
options: options{
configFile: "",
},
expected: func() string {
cfgToml, _ := defaultToml()
buffer := new(bytes.Buffer)
cfgToml.Save(buffer)
return buffer.String()
}(),
},
{
description: "save uncommented configuration items to file",
options: options{
configFile: tmpFile.Name(),
},
expected: func() string {
cfgToml, _ := defaultToml()
cfgToml.Set("nvidia-container-cli.ldconfig", "OVERRIDDEN")
cfgToml.Set("nvidia-container-cli.debug", "/var/log/nvidia-container-toolkit.log")
cfgToml.Set("nvidia-container-runtime.debug", "/var/log/nvidia-container-runtime.log")
buffer := new(bytes.Buffer)
cfgToml.Save(buffer)
return buffer.String()
}(),
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
tomlCfg, err := tc.options.loadConfigToml()
require.NoError(t, err)

buffer := new(bytes.Buffer)
_, err = tomlCfg.Save(buffer)
require.NoError(t, err)

require.EqualValues(t,
strings.TrimSpace(tc.expected),
strings.TrimSpace(buffer.String()),
)
})
}
}

func createEmpty() *Toml {
t, _ := TreeFromMap(nil)
return t
Expand Down