Esempio n. 1
0
func (s *ConfigSuite) TestDefaultType(c *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, IsNil)
		c.Assert(result.Options["t"].Default, Equals, expected)
	}

	assertDefault("boolean", "true", true)
	assertDefault("string", "golden grahams", "golden grahams")
	assertDefault("float", "2.2e11", 2.2e11)
	assertDefault("int", "99", int64(99))

	assertTypeError := func(type_ string, value string) {
		config := fmt.Sprintf(`options: {t: {type: %s, default: %s}}`, type_, value)
		_, err := charm.ReadConfig(bytes.NewBuffer([]byte(config)))
		expected := fmt.Sprintf(`Bad default for "t": %s is not of type %s`, value, type_)
		c.Assert(err, ErrorMatches, expected)
	}

	assertTypeError("boolean", "henry")
	assertTypeError("string", "2.5")
	assertTypeError("float", "blob")
	assertTypeError("int", "33.2")
}
Esempio n. 2
0
func (s *ConfigSuite) TestDefaultType(c *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, IsNil)
		c.Assert(result.Options["t"].Default, 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, ErrorMatches, expected)
	}

	assertTypeError("boolean", "henry", `"henry"`)
	assertTypeError("string", "2.5", "2.5")
	assertTypeError("float", "123", "123")
	assertTypeError("int", "true", "true")
}
Esempio n. 3
0
func (s *ConfigSuite) SetUpSuite(c *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, IsNil)
}
Esempio n. 4
0
func (s *ConfigSuite) TestReadConfig(c *C) {
	config, err := charm.ReadConfig(repoConfig("dummy"))
	c.Assert(err, IsNil)
	c.Assert(config.Options["title"], DeepEquals,
		charm.Option{
			Default:     "My Title",
			Description: "A descriptive title used for the service.",
			Type:        "string",
		},
	)
}
Esempio n. 5
0
func (s *CharmTestHelperSuite) TestConfigCharm(c *C) {
	config, err := charm.ReadConfig(bytes.NewBuffer([]byte(configYaml)))
	c.Assert(err, IsNil)

	forEachStandardCharm(c, func(name string) {
		chd := testing.Charms.Dir(name)
		meta := chd.Meta()

		ch := s.AddConfigCharm(c, name, configYaml, 123)
		assertCustomCharm(c, ch, "series", meta, config, 123)
	})
}
Esempio n. 6
0
func (s *ConfigSuite) TestValidate(c *C) {
	config, err := charm.ReadConfig(bytes.NewBuffer([]byte(sampleConfig)))
	c.Assert(err, IsNil)

	input := map[string]string{
		"title":   "Helpful Title",
		"outlook": "Peachy",
	}

	// This should include an overridden value, a default and a new value.
	expected := map[string]interface{}{
		"title":    "Helpful Title",
		"outlook":  "Peachy",
		"username": "******",
	}

	output, err := config.Validate(input)
	c.Assert(err, IsNil)
	c.Assert(output, DeepEquals, expected)

	// Check whether float conversion is working.
	input["agility-ratio"] = "0.5"
	input["skill-level"] = "7"
	expected["agility-ratio"] = 0.5
	expected["skill-level"] = int64(7)
	output, err = config.Validate(input)
	c.Assert(err, IsNil)
	c.Assert(output, DeepEquals, expected)

	// Check whether float errors are caught.
	input["agility-ratio"] = "foo"
	output, err = config.Validate(input)
	c.Assert(err, ErrorMatches, `Value for "agility-ratio" is not a float: "foo"`)
	input["agility-ratio"] = "0.5"

	// Check whether int errors are caught.
	input["skill-level"] = "foo"
	output, err = config.Validate(input)
	c.Assert(err, ErrorMatches, `Value for "skill-level" is not an int: "foo"`)
	input["skill-level"] = "7"

	// Check whether boolean errors are caught.
	input["reticulate-splines"] = "maybe"
	output, err = config.Validate(input)
	c.Assert(err, ErrorMatches, `Value for "reticulate-splines" is not a boolean: "maybe"`)
	input["reticulate-splines"] = "false"

	// Now try to set a value outside the expected.
	input["bad"] = "value"
	output, err = config.Validate(input)
	c.Assert(output, IsNil)
	c.Assert(err, ErrorMatches, `Unknown configuration option: "bad"`)
}
Esempio n. 7
0
func (s *ConfigSuite) TestParseSample(c *C) {
	config, err := charm.ReadConfig(bytes.NewBuffer([]byte(sampleConfig)))
	c.Assert(err, IsNil)

	opt := config.Options
	c.Assert(opt["title"], DeepEquals,
		charm.Option{
			Default:     "My Title",
			Description: "A descriptive title used for the service.",
			Type:        "string",
		},
	)
	c.Assert(opt["outlook"], DeepEquals,
		charm.Option{
			Description: "No default outlook.",
			Type:        "string",
		},
	)
	c.Assert(opt["username"], DeepEquals,
		charm.Option{
			Default:     "admin001",
			Description: "The name of the initial account (given admin permissions).",
			Type:        "string",
		},
	)
	c.Assert(opt["skill-level"], DeepEquals,
		charm.Option{
			Description: "A number indicating skill.",
			Type:        "int",
		},
	)
	c.Assert(opt["reticulate-splines"], DeepEquals,
		charm.Option{
			Description: "Whether to reticulate splines on launch, or not.",
			Type:        "boolean",
		},
	)
}
Esempio n. 8
0
func (s *ConfigSuite) TestConfigError(c *C) {
	_, err := charm.ReadConfig(bytes.NewBuffer([]byte(`options: {t: {type: foo}}`)))
	c.Assert(err, ErrorMatches, `config: options.t.type: unexpected value.*`)
}
Esempio n. 9
0
// When an empty config is supplied an error should be returned
func (s *ConfigSuite) TestEmptyConfigReturnsError(c *C) {
	config := ""
	result, err := charm.ReadConfig(bytes.NewBuffer([]byte(config)))
	c.Assert(result, IsNil)
	c.Assert(err, ErrorMatches, "invalid config: empty configuration")
}
Esempio n. 10
0
func (s *ConfigSuite) TestConfigError(c *C) {
	_, err := charm.ReadConfig(bytes.NewBuffer([]byte(`options: {t: {type: foo}}`)))
	c.Assert(err, ErrorMatches, `invalid config: option "t" has unknown type "foo"`)
}