package budget import ( "bytes" "os" "path/filepath" "strings" "testing" "github.com/BurntSushi/toml" ) func TestConfigInvalid(t *testing.T) { for _, tc := range []struct { name string field string change func(*config) }{ {"cpu", "profiles.compute", func(c *config) { p := c.Profiles["compute"] p.CPU = "invalid%" c.Profiles["compute"] = p }}, {"cpu-suffix", "profiles.compute", func(c *config) { p := c.Profiles["compute"] p.CPU = "800" c.Profiles["compute"] = p }}, {"high", "profiles.compute", func(c *config) { p := c.Profiles["compute"] p.High = "invalid" c.Profiles["compute"] = p }}, {"memory", "profiles.compute", func(c *config) { p := c.Profiles["compute"] p.Memory = "invalid" c.Profiles["compute"] = p }}, {"swap", "profiles.compute", func(c *config) { p := c.Profiles["compute"] p.Swap = "invalid" c.Profiles["compute"] = p }}, {"io", "profiles.compute", func(c *config) { c.Devices[0].Limits["compute"] = ioLimit{"invalid", "500M", "20000", "20000"} }}, {"command", "commands.java", func(c *config) { c.Commands["java"] = "missing" }}, {"python", "python", func(c *config) { c.Python = "missing" }}, {"shared", "shared.editor", func(c *config) { c.Shared["editor"] = "missing" }}, {"apps", "apps.missing", func(c *config) { c.Apps["missing"] = []string{"editor"} }}, {"units", "units.missing", func(c *config) { c.Units["missing"] = []string{"worker.service"} }}, {"prefixes", "prefixes.missing", func(c *config) { c.Prefixes["missing"] = []string{"app-editor-.scope.d"} }}, {"device", "devices[0].limits", func(c *config) { c.Devices[0].Limits["missing"] = ioLimit{"100M", "100M", "4000", "4000"} }}, {"prefix", "prefixes.default", func(c *config) { c.Prefixes["default"] = []string{"snap-firefox"} }}, } { t.Run(tc.name, func(t *testing.T) { c := testConfig() tc.change(&c) var data bytes.Buffer if err := toml.NewEncoder(&data).Encode(c); err != nil { t.Fatal(err) } file := filepath.Join(t.TempDir(), "profiles.toml") if err := os.WriteFile(file, data.Bytes(), 0600); err != nil { t.Fatal(err) } if _, err := loadConfig(file); err == nil || !strings.Contains(err.Error(), tc.field) { t.Fatalf("expected an error naming %s, got %v", tc.field, err) } }) } }