import copy import os import sys import yaml from juju.lib.schema import (SchemaError, KeyDict, Dict, String, Constant, OneOf, Int, Float) from juju.charm.errors import (ServiceConfigError, ServiceConfigValueError) OPTION_SCHEMA = KeyDict( { "type": OneOf( Constant("string"), Constant("str"), # Obsolete Constant("int"), Constant("float")), "default": OneOf(String(), Int(), Float()), "description": String(), }, optional=["default", "description"], ) # Schema used to validate ConfigOptions specifications CONFIG_SCHEMA = KeyDict({ "options": Dict(String(), OPTION_SCHEMA), })
def test_one_of(self): schema = OneOf(Constant(None), Unicode()) self.assertEquals(schema.coerce(None, PATH), None) self.assertEquals(schema.coerce(u"foo", PATH), u"foo")
def test_one_of_bad(self): schema = OneOf(Constant(None), Unicode()) error = self.assertRaises(SchemaError, schema.coerce, "<obj>", PATH) # When no values are supported, raise the first error. self.assertEquals(str(error), "<path>: expected None, got '<obj>'")
def test_constant_bad(self): error = self.assertRaises(SchemaError, Constant("foo").coerce, "bar", PATH) self.assertEquals(str(error), "<path>: expected 'foo', got 'bar'")
def test_constant_arbitrary(self): obj = object() self.assertEquals(Constant(obj).coerce(obj, PATH), obj)
def test_constant(self): self.assertEquals(Constant("hello").coerce("hello", PATH), "hello")
import os import yaml from juju.charm.errors import MetaDataError from juju.errors import FileNotFound from juju.lib.schema import (SchemaError, Bool, Constant, Dict, Int, KeyDict, OneOf, UnicodeOrString) log = logging.getLogger("juju.charm") UTF8_SCHEMA = UnicodeOrString("utf-8") INTERFACE_SCHEMA = KeyDict({ "interface": UTF8_SCHEMA, "limit": OneOf(Constant(None), Int()), "optional": Bool() }) class InterfaceExpander(object): """Schema coercer that expands the interface shorthand notation. We need this class because our charm shorthand is difficult to work with (unfortunately). So we coerce shorthand and then store the desired format in ZK. Supports the following variants:: provides: server: riak
type: ec2 control-bucket: %(control-bucket)s admin-secret: %(admin-secret)s default-series: oneiric """ SCHEMA = KeyDict({ "default": String(), "environments": Dict(String(), SelectDict("type", { "ec2": KeyDict({"control-bucket": String(), "admin-secret": String(), "access-key": String(), "secret-key": String(), "region": OneOf( Constant("us-east-1"), Constant("us-west-1"), Constant("us-west-2"), Constant("eu-west-1"), Constant("sa-east-1"), Constant("ap-northeast-1"), Constant("ap-southeast-1")), "default-instance-type": String(), "default-ami": String(), "ec2-uri": String(), "s3-uri": String(), "placement": OneOf( Constant("unassigned"), Constant("local")), "default-series": String()}, optional=["access-key", "secret-key",