From c77b3c47356e7f0a2fad2175b305405d459d1e17 Mon Sep 17 00:00:00 2001 From: sunjq1 <952857636@qq.com> Date: Wed, 4 Mar 2026 14:37:19 +0800 Subject: [PATCH 1/2] fix: Prevent config option from reverting modified configurations Signed-off-by: Sun Jianqiang --- api/config/v1/toml.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/api/config/v1/toml.go b/api/config/v1/toml.go index 104b4684f..d548572d6 100644 --- a/api/config/v1/toml.go +++ b/api/config/v1/toml.go @@ -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) { From 58e4927abe11cdbfeeaf76822cf0e7682ee65021 Mon Sep 17 00:00:00 2001 From: sunjq1 <952857636@qq.com> Date: Thu, 5 Mar 2026 09:43:08 +0800 Subject: [PATCH 2/2] test: Add TestLoadConfigToml Signed-off-by: Sun Jianqiang --- api/config/v1/toml_test.go | 67 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/api/config/v1/toml_test.go b/api/config/v1/toml_test.go index 1739ae989..f2ca091b9 100644 --- a/api/config/v1/toml_test.go +++ b/api/config/v1/toml_test.go @@ -18,6 +18,7 @@ package config import ( "bytes" + "os" "strings" "testing" @@ -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