summaryrefslogtreecommitdiff
path: root/ironic/tests/unit/objects/test_fields.py
blob: 35cc050e60379fa6e42baf22efbda50b8cea2472 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Copyright 2015 Red Hat, Inc.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from ironic.common import exception
from ironic.objects import fields
from ironic.tests import base as test_base


class TestMacAddressField(test_base.TestCase):

    def setUp(self):
        super(TestMacAddressField, self).setUp()
        self.field = fields.MACAddressField()

    def test_coerce(self):
        values = {'aa:bb:cc:dd:ee:ff': 'aa:bb:cc:dd:ee:ff',
                  'AA:BB:CC:DD:EE:FF': 'aa:bb:cc:dd:ee:ff',
                  'AA:bb:cc:11:22:33': 'aa:bb:cc:11:22:33'}
        for k in values:
            self.assertEqual(values[k], self.field.coerce('obj', 'attr', k))

    def test_coerce_bad_values(self):
        for v in ('invalid-mac', 'aa-bb-cc-dd-ee-ff'):
            self.assertRaises(exception.InvalidMAC,
                              self.field.coerce, 'obj', 'attr', v)


class TestFlexibleDictField(test_base.TestCase):

    def setUp(self):
        super(TestFlexibleDictField, self).setUp()
        self.field = fields.FlexibleDictField()

    def test_coerce(self):
        d = {'foo_1': 'bar', 'foo_2': 2, 'foo_3': [], 'foo_4': {}}
        self.assertEqual(d, self.field.coerce('obj', 'attr', d))
        self.assertEqual({'foo': 'bar'},
                         self.field.coerce('obj', 'attr', '{"foo": "bar"}'))

    def test_coerce_bad_values(self):
        self.assertRaises(TypeError, self.field.coerce, 'obj', 'attr', 123)
        self.assertRaises(TypeError, self.field.coerce, 'obj', 'attr', True)

    def test_coerce_nullable_translation(self):
        # non-nullable
        self.assertRaises(ValueError, self.field.coerce, 'obj', 'attr', None)

        # nullable
        self.field = fields.FlexibleDictField(nullable=True)
        self.assertEqual({}, self.field.coerce('obj', 'attr', None))


class TestStringFieldThatAcceptsCallable(test_base.TestCase):

    def setUp(self):
        super(TestStringFieldThatAcceptsCallable, self).setUp()

        def test_default_function():
            return "default value"

        self.field = fields.StringFieldThatAcceptsCallable(
            default=test_default_function)

    def test_coerce_string(self):
        self.assertEqual("value", self.field.coerce('obj', 'attr', "value"))

    def test_coerce_function(self):
        def test_function():
            return "value"
        self.assertEqual("value",
                         self.field.coerce('obj', 'attr', test_function))

    def test_coerce_invalid_type(self):
        self.assertRaises(ValueError, self.field.coerce,
                          'obj', 'attr', ('invalid', 'tuple'))

    def test_coerce_function_invalid_type(self):
        def test_function():
            return ('invalid', 'tuple',)
        self.assertRaises(ValueError,
                          self.field.coerce, 'obj', 'attr', test_function)

    def test_coerce_default_as_function(self):
        self.assertEqual("default value",
                         self.field.coerce('obj', 'attr', None))

    def test__repr__includes_default_function_name_and_source_hash(self):
        expected = ('StringAcceptsCallable(default=<function '
                    'test_default_function>,nullable=False)')
        self.assertEqual(expected, repr(self.field))


class TestNotificationLevelField(test_base.TestCase):

    def setUp(self):
        super(TestNotificationLevelField, self).setUp()
        self.field = fields.NotificationLevelField()

    def test_coerce_good_value(self):
        self.assertEqual(fields.NotificationLevel.WARNING,
                         self.field.coerce('obj', 'attr', 'warning'))

    def test_coerce_bad_value(self):
        self.assertRaises(ValueError, self.field.coerce, 'obj', 'attr',
                          'not_a_priority')


class TestNotificationStatusField(test_base.TestCase):

    def setUp(self):
        super(TestNotificationStatusField, self).setUp()
        self.field = fields.NotificationStatusField()

    def test_coerce_good_value(self):
        self.assertEqual(fields.NotificationStatus.START,
                         self.field.coerce('obj', 'attr', 'start'))

    def test_coerce_bad_value(self):
        self.assertRaises(ValueError, self.field.coerce, 'obj', 'attr',
                          'not_a_priority')