File: test_services.py

package info (click to toggle)
rally 5.0.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,368 kB
  • sloc: python: 42,541; javascript: 487; sh: 198; makefile: 192; xml: 43
file content (266 lines) | stat: -rw-r--r-- 9,279 bytes parent folder | download | duplicates (4)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#
#    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 unittest import mock

from rally import exceptions
from rally.task import service
from tests.unit import test


PATH = "rally.task.service"


class ServiceTestCase(test.TestCase):
    def setUp(self):
        super(ServiceTestCase, self).setUp()
        self.clients = mock.MagicMock()
        self.clients.cc.choose_version.return_value = 1

    @mock.patch("%s.atomic" % PATH)
    def test_atomic(self, mock_atomic):
        @service.service("some_service", "some_type", version="1")
        class SomeV1Service(service.Service):
            pass

        SomeV1Service(mock.MagicMock(), atomic_inst=mock.MagicMock())
        self.assertFalse(mock_atomic.ActionTimerMixin.called)

        # ensure that previous call will no affect anything
        mock_atomic.ActionTimerMixin.reset_mock()

        SomeV1Service(mock.MagicMock())
        self.assertTrue(mock_atomic.ActionTimerMixin.called)

    def test_generate_random_name(self):
        @service.service("some_service", "some_type", version="1")
        class SomeV1Service(service.Service):
            pass

        self.assertRaises(exceptions.RallyException,
                          SomeV1Service(self.clients).generate_random_name)

        name_generator = mock.MagicMock()
        impl = SomeV1Service(self.clients, name_generator=name_generator)
        self.assertEqual(name_generator.return_value,
                         impl.generate_random_name())
        name_generator.assert_called_once_with()

    def test_version(self):
        class SomeService(service.UnifiedService):
            pass

        @service.service("some_service", "some_type", version="1")
        class SomeV1Service(service.Service):
            pass

        @service.compat_layer(SomeV1Service)
        class UnifiedSomeV1Service(SomeService):
            pass

        clients = mock.MagicMock()
        clients.some_service.choose_version.return_value = "1"

        self.assertEqual("1", SomeService(clients).version)
        self.assertEqual("1", SomeV1Service(clients).version)
        self.assertEqual("1", UnifiedSomeV1Service(clients).version)

    def test_is_applicable(self):
        class SomeService(service.UnifiedService):
            pass

        @service.service("some_service", "some_type", version="1")
        class SomeV1Service(service.Service):
            pass

        @service.compat_layer(SomeV1Service)
        class UnifiedSomeV1Service(SomeService):
            pass

        clients = mock.MagicMock()
        clients.some_service.choose_version.return_value = "1"

        self.assertFalse(SomeService.is_applicable(clients))
        self.assertTrue(UnifiedSomeV1Service.is_applicable(clients))

        clients.some_service.choose_version.return_value = "2"
        self.assertFalse(SomeService.is_applicable(clients))
        self.assertFalse(UnifiedSomeV1Service.is_applicable(clients))


class ServiceMetaTestCase(test.TestCase):
    def test_servicemeta_fail_on_missed_public_function(self):
        def init_classes():
            class SomeService(service.UnifiedService):
                @service.should_be_overridden
                def foo(self):
                    pass

            @service.service("some_service", "some_type", version="1")
            class SomeV1Service(service.Service):
                pass

            @service.compat_layer(SomeV1Service)
            class UnifiedSomeV1Service(SomeService):
                pass

        e = self.assertRaises(exceptions.RallyException, init_classes)
        self.assertIn("Missed method(s): foo", str(e))


class DiscoverTestCase(test.TestCase):
    def test_discover_impl_based_on_version(self):
        class SomeService(service.UnifiedService):
            pass

        @service.service("some_service", "some_type", version="1")
        class SomeV1Service(service.Service):
            pass

        @service.compat_layer(SomeV1Service)
        class UnifiedSomeV1Service(SomeService):
            pass

        @service.service("some_service", "some_type", version="2")
        class SomeV2Service(service.Service):
            pass

        @service.compat_layer(SomeV2Service)
        class UnifiedSomeV2Service(SomeService):
            pass

        clients = mock.MagicMock()
        clients.some_service.choose_version.return_value = "1"
        self.assertIsInstance(SomeService(clients)._impl, UnifiedSomeV1Service)

        clients.some_service.choose_version.return_value = "2"
        self.assertIsInstance(SomeService(clients)._impl, UnifiedSomeV2Service)

        self.assertFalse(clients.services.called)

    def test_discover_impl_based_on_service(self):
        class SomeService(service.UnifiedService):
            pass

        @service.service("some_service", "some_type", version="1")
        class SomeV1Service(service.Service):
            pass

        @service.compat_layer(SomeV1Service)
        class UnifiedSomeV1Service(SomeService):
            pass

        @service.service("another_impl_of_some_service", "another_type",
                         version="2")
        class AnotherSomeV2Service(service.Service):
            pass

        @service.compat_layer(AnotherSomeV2Service)
        class UnifiedAnotherSomeV2Service(SomeService):
            pass

        clients = mock.MagicMock()
        clients.some_service.choose_version.return_value = "1"
        clients.another_impl_of_some_service.choose_version.return_value = "2"

        clients.services.return_value = {"some_type": "some_service"}
        self.assertIsInstance(SomeService(clients)._impl, UnifiedSomeV1Service)

        clients.services.return_value = {
            "another_type": "another_impl_of_some_service"}
        self.assertIsInstance(SomeService(clients)._impl,
                              UnifiedAnotherSomeV2Service)

    def test_discover_impl_fail_with_wrong_version(self):

        class SomeService(service.UnifiedService):
            pass

        @service.service("some_service", "some_type", version="1")
        class SomeV1Service(service.Service):
            pass

        @service.compat_layer(SomeV1Service)
        class UnifiedSomeV1Service(SomeService):
            pass

        clients = mock.MagicMock()
        clients.some_service.choose_version.return_value = "2"

        e = self.assertRaises(exceptions.RallyException, SomeService, clients)
        self.assertEqual("There is no proper implementation for "
                         "SomeService.", str(e))

    def test_discover_impl_fail_with_unavailable_service(self):

        class SomeService(service.UnifiedService):
            pass

        @service.service("some_service", "some_type", version="1")
        class SomeV1Service(SomeService):
            pass

        @service.compat_layer(SomeV1Service)
        class UnifiedSomeV1Service(SomeService):
            pass

        @service.service("another_service", "another_type", version="2")
        class AnotherSomeV2Service(SomeService):
            pass

        @service.compat_layer(AnotherSomeV2Service)
        class UnifiedAnotherSomeV2Service(SomeService):
            pass

        clients = mock.MagicMock()
        clients.some_service.choose_version.return_value = "1"
        clients.another_service.choose_version.return_value = "2"
        clients.services.return_value = {}

        e = self.assertRaises(exceptions.RallyException, SomeService, clients)
        self.assertEqual("There is no proper implementation for SomeService.",
                         str(e))


class UtilsTestCase(test.TestCase):
    def test_method_wrapper(self):
        class Some(object):
            @service.method_wrapper
            def foo(slf, *args, **kwargs):
                if len(args) > 1:
                    self.fail("`method_wrapper` should fail when number of "
                              "positional arguments are bigger than 1.")

        Some().foo()
        Some().foo(some=2, another=3)
        Some().foo(1, some=2, another=3)
        self.assertRaises(TypeError, Some().foo, 1, 2)

    def test_make_resource_cls(self):
        FooCls = service.make_resource_cls("Foo", properties=["a", "b"])

        # check repr
        self.assertEqual("<Foo id=1>", "%s" % FooCls(a=1))

        # check getitem
        self.assertEqual(1, FooCls(a=1)["a"])

        # check used name for a type in error messages
        e = self.assertRaises(AttributeError, getattr, FooCls(a=1), "c")
        self.assertEqual("'Foo' object has no attribute 'c'",
                         "%s" % e)

        # check asserts
        self.assertEqual(FooCls(a=1), FooCls(a=1))
        self.assertEqual(FooCls(a=1), FooCls(a=1, b=3))
        self.assertNotEqual(FooCls(a=1), FooCls(a=2))