func (s *ConfigSuite) TestDefaultType(c *gc.C) { assertDefault := func(type_ string, value string, expected interface{}) { config := fmt.Sprintf(`options: {t: {type: %s, default: %s}}`, type_, value) result, err := charm.ReadConfig(bytes.NewBuffer([]byte(config))) c.Assert(err, gc.IsNil) c.Assert(result.Options["t"].Default, gc.Equals, expected) } assertDefault("boolean", "true", true) assertDefault("string", "golden grahams", "golden grahams") assertDefault("string", `""`, "") assertDefault("float", "2.2e11", 2.2e11) assertDefault("int", "99", int64(99)) assertTypeError := func(type_, str, value string) { config := fmt.Sprintf(`options: {t: {type: %s, default: %s}}`, type_, str) _, err := charm.ReadConfig(bytes.NewBuffer([]byte(config))) expected := fmt.Sprintf(`invalid config default: option "t" expected %s, got %s`, type_, value) c.Assert(err, gc.ErrorMatches, expected) } assertTypeError("boolean", "henry", `"henry"`) assertTypeError("string", "2.5", "2.5") assertTypeError("float", "123", "123") assertTypeError("int", "true", "true") }
func (s *ConfigSuite) SetUpSuite(c *gc.C) { // Just use a single shared config for the whole suite. There's no use case // for mutating a config, we we assume that nobody will do so here. var err error s.config, err = charm.ReadConfig(bytes.NewBuffer([]byte(` options: title: default: My Title description: A descriptive title used for the service. type: string subtitle: default: "" description: An optional subtitle used for the service. outlook: description: No default outlook. # type defaults to string in python username: default: admin001 description: The name of the initial account (given admin permissions). type: string skill-level: description: A number indicating skill. type: int agility-ratio: description: A number from 0 to 1 indicating agility. type: float reticulate-splines: description: Whether to reticulate splines on launch, or not. type: boolean `))) c.Assert(err, gc.IsNil) }
func (s *CharmTestHelperSuite) TestConfigCharm(c *gc.C) { config, err := charm.ReadConfig(bytes.NewBuffer([]byte(configYaml))) c.Assert(err, gc.IsNil) forEachStandardCharm(c, func(name string) { chd := testing.Charms.Dir(name) meta := chd.Meta() ch := s.AddConfigCharm(c, name, configYaml, 123) assertCustomCharm(c, ch, "quantal", meta, config, 123) }) }
// When an empty config is supplied an error should be returned func (s *ConfigSuite) TestEmptyConfigReturnsError(c *gc.C) { config := "" result, err := charm.ReadConfig(bytes.NewBuffer([]byte(config))) c.Assert(result, gc.IsNil) c.Assert(err, gc.ErrorMatches, "invalid config: empty configuration") }
func (s *ConfigSuite) TestConfigError(c *gc.C) { _, err := charm.ReadConfig(bytes.NewBuffer([]byte(`options: {t: {type: foo}}`))) c.Assert(err, gc.ErrorMatches, `invalid config: option "t" has unknown type "foo"`) }