~ubuntu-branches/ubuntu/utopic/maas/utopic

« back to all changes in this revision

Viewing changes to src/provisioningserver/rpc/tests/test_arguments.py

  • Committer: Package Import Robot
  • Author(s): Julian Edwards, Julian Edwards, Andres Rodriguez
  • Date: 2014-08-21 18:38:27 UTC
  • mfrom: (1.2.34)
  • Revision ID: package-import@ubuntu.com-20140821183827-9xyb5u2o4l8g3zxj
Tags: 1.6.1+bzr2550-0ubuntu1
* New upstream bugfix release:
  - Auto-link node MACs to Networks (LP: #1341619)

[ Julian Edwards ]
* debian/maas-region-controller.postinst: Don't restart RabbitMQ on
  upgrades, just ensure it's running.  Should prevent a race with the
  cluster celery restarting.
* debian/rules: Pull upstream branch from the right place.

[ Andres Rodriguez ]
* debian/maas-region-controller.postinst: Ensure cluster celery is
  started if it also runs on the region.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
__metaclass__ = type
15
15
__all__ = []
16
16
 
 
17
import random
 
18
 
 
19
from maastesting.factory import factory
17
20
from maastesting.testcase import MAASTestCase
18
21
from provisioningserver.rpc import arguments
 
22
from testtools import ExpectedException
19
23
from testtools.matchers import (
20
24
    Equals,
21
25
    IsInstance,
22
26
    )
23
27
 
24
28
 
 
29
class TestBytes(MAASTestCase):
 
30
 
 
31
    def test_round_trip(self):
 
32
        argument = arguments.Bytes()
 
33
        example = factory.getRandomBytes()
 
34
        encoded = argument.toString(example)
 
35
        self.assertThat(encoded, IsInstance(bytes))
 
36
        decoded = argument.fromString(encoded)
 
37
        self.assertThat(decoded, Equals(example))
 
38
 
 
39
    def test_error_when_input_is_not_a_byte_string(self):
 
40
        with ExpectedException(TypeError, "^Not a byte string: <.*"):
 
41
            arguments.Bytes().toString(object())
 
42
 
 
43
 
 
44
class TestChoice(MAASTestCase):
 
45
 
 
46
    def test_round_trip(self):
 
47
        choices = {
 
48
            factory.make_name("name"): factory.getRandomBytes()
 
49
            for _ in xrange(10)
 
50
        }
 
51
        argument = arguments.Choice(choices)
 
52
        choice = random.choice(list(choices))
 
53
        encoded = argument.toString(choice)
 
54
        self.assertThat(encoded, IsInstance(bytes))
 
55
        decoded = argument.fromString(encoded)
 
56
        self.assertThat(decoded, Equals(choice))
 
57
 
 
58
    def test_error_when_input_is_not_in_choices(self):
 
59
        with ExpectedException(KeyError, "^<object .*"):
 
60
            arguments.Choice({}).toString(object())
 
61
 
 
62
    def test_error_when_choices_is_not_mapping(self):
 
63
        with ExpectedException(TypeError, "^Not a mapping: \[\]"):
 
64
            arguments.Choice([])
 
65
 
 
66
    def test_error_when_choices_values_are_not_byte_strings(self):
 
67
        with ExpectedException(TypeError, "^Not byte strings: 12345, u'foo'"):
 
68
            arguments.Choice({object(): 12345, object(): u'foo'})
 
69
 
 
70
 
25
71
class TestStructureAsJSON(MAASTestCase):
26
72
 
27
73
    example = {
36
82
        self.assertThat(encoded, IsInstance(bytes))
37
83
        decoded = argument.fromString(encoded)
38
84
        self.assertThat(decoded, Equals(self.example))
 
85
 
 
86
 
 
87
class TestParsedURL(MAASTestCase):
 
88
 
 
89
    def test_round_trip(self):
 
90
        argument = arguments.ParsedURL()
 
91
        example = factory.make_parsed_url()
 
92
        encoded = argument.toString(example)
 
93
        self.assertThat(encoded, IsInstance(bytes))
 
94
        decoded = argument.fromString(encoded)
 
95
        self.assertThat(decoded.geturl(), Equals(example.geturl()))
 
96
 
 
97
    def test_error_when_input_is_not_a_url_object(self):
 
98
        with ExpectedException(TypeError, "^Not a URL-like object: <.*"):
 
99
            arguments.ParsedURL().toString(object())
 
100
 
 
101
    def test_netloc_containing_non_ascii_characters_is_encoded_to_idna(self):
 
102
        argument = arguments.ParsedURL()
 
103
        example = factory.make_parsed_url()._replace(
 
104
            netloc=u'\u24b8\u211d\U0001d538\u24b5\U0001d502')
 
105
        encoded = argument.toString(example)
 
106
        self.assertThat(encoded, IsInstance(bytes))
 
107
        decoded = argument.fromString(encoded)
 
108
        # The non-ASCII netloc was encoded using IDNA.
 
109
        expected = example._replace(netloc="cra(z)y")
 
110
        self.assertThat(decoded.geturl(), Equals(expected.geturl()))