Esempio n. 1
0
 def test_dict_path_without_dots(self):
     """
     The first path entry shouldn't have a dot as prefix.
     """
     schema = Dict(Int(), Dict(Int(), Int()))
     error = self.assertRaises(SchemaError, schema.coerce, {1: {
         2: "hi"
     }}, [])
     self.assertEquals(str(error), "1.2: expected int, got 'hi'")
Esempio n. 2
0
 def test_select_dict_errors(self):
     schema = SelectDict("name", {
         "foo": KeyDict({"foo_": Int()}),
         "bar": KeyDict({"bar_": Int()})
     })
     error = self.assertRaises(SchemaError, schema.coerce, {"name": "foo"},
                               PATH)
     self.assertEquals(str(error), "<path>.foo_: required value not found")
     error = self.assertRaises(SchemaError, schema.coerce, {"name": "bar"},
                               PATH)
     self.assertEquals(str(error), "<path>.bar_: required value not found")
Esempio n. 3
0
 def test_key_dict_multiple_items(self):
     schema = KeyDict({"one": Int(), "two": List(Float())})
     input = {"one": 32, "two": [1.5, 2.3]}
     self.assertEquals(schema.coerce(input, PATH), {
         "one": 32,
         "two": [1.5, 2.3]
     })
Esempio n. 4
0
 def test_key_dict_must_have_all_keys(self):
     """
     dicts which are applied to a KeyDict must have all the keys
     specified in the KeyDict.
     """
     schema = KeyDict({"foo": Int()})
     error = self.assertRaises(SchemaError, schema.coerce, {}, PATH)
     self.assertEquals(str(error), "<path>.foo: required value not found")
Esempio n. 5
0
 def test_dict_bad_value_unstringifiable(self):
     """
     If the path can't be stringified, it's repr()ed.
     """
     a = u"\N{HIRAGANA LETTER A}"
     schema = Dict(Unicode(), Int())
     error = self.assertRaises(SchemaError, schema.coerce, {a: "hi"}, PATH)
     self.assertEquals(str(error),
                       "<path>.u'\\u3042': expected int, got 'hi'")
Esempio n. 6
0
 def test_key_dict_arbitrary_keys(self):
     """
     KeyDict doesn't actually need to have strings as keys, just any
     object which hashes the same.
     """
     key = object()
     self.assertEquals(
         KeyDict({
             key: Int()
         }).coerce({key: 32}, PATH), {key: 32})
Esempio n. 7
0
 def test_key_dict_unknown_key(self):
     """
     Unknown key/value pairs processed by a KeyDict are left untouched.
     This is an attempt at not eating values by mistake due to something
     like different application versions operating on the same data.
     """
     schema = KeyDict({"foo": Int()})
     self.assertEquals(schema.coerce({
         "foo": 1,
         "bar": "hi"
     }, PATH), {
         "foo": 1,
         "bar": "hi"
     })
Esempio n. 8
0
 def test_select_dict(self):
     schema = SelectDict("name", {
         "foo": KeyDict({"value": Int()}),
         "bar": KeyDict({"value": String()})
     })
     self.assertEquals(schema.coerce({
         "name": "foo",
         "value": 1
     }, PATH), {
         "name": "foo",
         "value": 1
     })
     self.assertEquals(schema.coerce({
         "name": "bar",
         "value": "one"
     }, PATH), {
         "name": "bar",
         "value": "one"
     })
Esempio n. 9
0
 def test_key_dict_optional_keys(self):
     """KeyDict allows certain keys to be optional.
     """
     schema = KeyDict({"foo": Int(), "bar": Int()}, optional=["bar"])
     self.assertEquals(schema.coerce({"foo": 32}, PATH), {"foo": 32})
Esempio n. 10
0
 def test_list_bad(self):
     error = self.assertRaises(SchemaError, List(Int()).coerce, 32, PATH)
     self.assertEquals(str(error), "<path>: expected list, got 32")
Esempio n. 11
0
 def test_list_bad_inner_schema_at_1(self):
     error = self.assertRaises(SchemaError,
                               List(Int()).coerce, [1, "hello"], PATH)
     self.assertEquals(str(error), "<path>[1]: expected int, got 'hello'")
Esempio n. 12
0
 def test_list(self):
     schema = List(Int())
     self.assertEquals(schema.coerce([1], PATH), [1])
Esempio n. 13
0
 def test_tuple_inner_schema_bad_at_1(self):
     error = self.assertRaises(SchemaError,
                               Tuple(Int(), Int()).coerce, (1, "hi"), PATH)
     self.assertEquals(str(error), "<path>[1]: expected int, got 'hi'")
Esempio n. 14
0
 def test_int_accepts_long(self):
     self.assertEquals(Int().coerce(3L, PATH), 3)
Esempio n. 15
0
 def test_tuple(self):
     self.assertEquals(Tuple(Int()).coerce((1, ), PATH), (1, ))
Esempio n. 16
0
 def test_dict_bad_value(self):
     error = self.assertRaises(SchemaError,
                               Dict(Int(), Int()).coerce, {32: "hi"}, PATH)
     self.assertEquals(str(error), "<path>.32: expected int, got 'hi'")
Esempio n. 17
0
 def test_dict_bad_key(self):
     error = self.assertRaises(SchemaError,
                               Dict(Int(), Int()).coerce, {"hi": 32}, PATH)
     self.assertEquals(str(error), "<path>: expected int, got 'hi'")
Esempio n. 18
0
 def test_dict(self):
     self.assertEquals(
         Dict(Int(), String()).coerce({32: "hello."}, PATH), {32: "hello."})
Esempio n. 19
0
 def test_tuple_must_have_no_more_items(self):
     error = self.assertRaises(SchemaError,
                               Tuple(Int()).coerce, (1, 2), PATH)
     self.assertEquals(
         str(error), "<path>: expected tuple with 1 elements, got (1, 2)")
Esempio n. 20
0
 def test_key_dict_pass_optional_key(self):
     """Regression test. It should be possible to pass an optional key.
     """
     schema = KeyDict({"foo": Int()}, optional=["foo"])
     self.assertEquals(schema.coerce({"foo": 32}, PATH), {"foo": 32})
Esempio n. 21
0
 def test_int_bad_float(self):
     error = self.assertRaises(SchemaError, Int().coerce, 3.0, PATH)
     self.assertEquals(str(error), "<path>: expected int, got 3.0")
Esempio n. 22
0
 def test_tuple_coerces(self):
     self.assertEquals(
         Tuple(Int(), DummySchema()).coerce((23, object()), PATH),
         (23, "hello!"))
Esempio n. 23
0
 def test_int(self):
     self.assertEquals(Int().coerce(3, PATH), 3)
Esempio n. 24
0
 def test_key_dict(self):
     self.assertEquals(
         KeyDict({
             "foo": Int()
         }).coerce({"foo": 1}, PATH), {"foo": 1})
Esempio n. 25
0
 def test_int_bad_str(self):
     error = self.assertRaises(SchemaError, Int().coerce, "3", PATH)
     self.assertEquals(str(error), "<path>: expected int, got '3'")
Esempio n. 26
0
 def test_key_dict_bad_value(self):
     schema = KeyDict({"foo": Int()})
     error = self.assertRaises(SchemaError, schema.coerce, {"foo": "hi"},
                               PATH)
     self.assertEquals(str(error), "<path>.foo: expected int, got 'hi'")
Esempio n. 27
0
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),
})

WARNED_STR_IS_OBSOLETE = False


class ConfigOptions(object):
Esempio n. 28
0
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