File: flavor.py

package info (click to toggle)
python-zaqarclient 4.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 832 kB
  • sloc: python: 4,417; makefile: 18; sh: 2
file content (186 lines) | stat: -rw-r--r-- 6,665 bytes parent folder | download
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
# Copyright (c) 2014 Red Hat, Inc.
#
# 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.

import json
from unittest import mock

from zaqarclient.queues.v2 import iterator
from zaqarclient.tests.queues import base
from zaqarclient.transport import response


class QueuesV2FlavorUnitTest(base.QueuesTestBase):

    def test_flavor_create(self):
        pool_list = ['pool1', 'pool2']
        flavor_data = {'pool_list': pool_list}

        with mock.patch.object(self.transport, 'send',
                               autospec=True) as send_method:

            resp = response.Response(None, None)
            send_method.return_value = resp

            # NOTE(flaper87): This will call
            # ensure exists in the client instance
            # since auto_create's default is True
            flavor = self.client.flavor('tasty', **flavor_data)
            self.assertEqual('tasty', flavor.name)

    def test_flavor_get(self):
        flavor_data = {'name': 'test'}

        with mock.patch.object(self.transport, 'send',
                               autospec=True) as send_method:

            resp = response.Response(None, json.dumps(flavor_data))
            send_method.return_value = resp

            # NOTE(flaper87): This will call
            # ensure exists in the client instance
            # since auto_create's default is True
            flavor = self.client.flavor('test')
            flavor1 = flavor.get()
            self.assertEqual('test', flavor1['name'])

    def test_flavor_update(self):
        pool_list1 = ['pool1', 'pool2']
        pool_list2 = ['pool3', 'pool4']
        flavor_data = {'pool_list': pool_list1}
        updated_data = {'pool_list': pool_list2}

        with mock.patch.object(self.transport, 'send',
                               autospec=True) as send_method:
            resp = response.Response(None, json.dumps(updated_data))
            send_method.return_value = resp

            flavor = self.client.flavor('tasty', **flavor_data)
            flavor.update({'pool_list': pool_list2})
            self.assertEqual(pool_list2, flavor.pool_list)

    def test_flavor_list(self):
        returned = {
            'links': [{
                'rel': 'next',
                'href': '/v2/flavors?marker=6244-244224-783'
            }],
            'flavors': [{
                'name': 'tasty'
            }]
        }

        with mock.patch.object(self.transport, 'send',
                               autospec=True) as send_method:

            resp = response.Response(None, json.dumps(returned))
            send_method.return_value = resp

            flavor_var = self.client.flavors(limit=1)
            self.assertIsInstance(flavor_var, iterator._Iterator)
            self.assertEqual(1, len(list(flavor_var)))

    def test_flavor_delete(self):
        pool_list = ['pool1', 'pool2']
        flavor_data = {'pool_list': pool_list}

        with mock.patch.object(self.transport, 'send',
                               autospec=True) as send_method:

            resp = response.Response(None, None)
            resp_data = response.Response(None, json.dumps(flavor_data))
            send_method.side_effect = iter([resp_data, resp])
            # NOTE(flaper87): This will call
            # ensure exists in the client instance
            # since auto_create's default is True
            flavor = self.client.flavor('tasty', **flavor_data)
            flavor.delete()

            # NOTE(flaper87): Nothing to assert here,
            # just checking our way down to the transport
            # doesn't crash.


class QueuesV2FlavorFunctionalTest(base.QueuesTestBase):

    def test_flavor_create(self):
        pool_data = {'uri': 'mongodb://127.0.0.1:27017',
                     'weight': 10,
                     'flavor': 'tasty'}
        pool = self.client.pool('stomach', **pool_data)
        self.addCleanup(pool.delete)
        pool_list = ['stomach']
        flavor_data = {'pool_list': pool_list}
        flavor = self.client.flavor('tasty', **flavor_data)
        self.addCleanup(flavor.delete)

        self.assertEqual('tasty', flavor.name)
        self.assertEqual(pool_list, flavor.pool_list)

    def test_flavor_get(self):
        pool_data = {'weight': 10,
                     'flavor': 'tasty',
                     'uri': 'mongodb://127.0.0.1:27017'}
        pool = self.client.pool('stomach', **pool_data)
        self.addCleanup(pool.delete)

        pool_list = ['stomach']
        flavor_data = {'pool_list': pool_list}
        flavor = self.client.flavor('tasty', **flavor_data)
        resp_data = flavor.get()
        self.addCleanup(flavor.delete)

        self.assertEqual('tasty', resp_data['name'])

    def test_flavor_update(self):
        pool_data = {'weight': 10,
                     'uri': 'mongodb://127.0.0.1:27017',
                     'flavor': 'tasty'}

        pool = self.client.pool('stomach', **pool_data)
        self.addCleanup(pool.delete)

        pool_list = ['stomach']
        flavor_data = {'pool_list': pool_list}
        flavor = self.client.flavor('tasty', **flavor_data)
        self.addCleanup(flavor.delete)

    def test_flavor_list(self):
        pool_data = {'uri': 'mongodb://127.0.0.1:27017',
                     'weight': 10,
                     'flavor': 'test_flavor'}

        pool = self.client.pool('stomach', **pool_data)
        self.addCleanup(pool.delete)

        pool_list = ['stomach']
        flavor_data = {'pool_list': pool_list}
        flavor = self.client.flavor("test_flavor", **flavor_data)
        self.addCleanup(flavor.delete)

        flavors = self.client.flavors()
        self.assertIsInstance(flavors, iterator._Iterator)
        self.assertEqual(1, len(list(flavors)))

    def test_flavor_delete(self):
        pool_data = {'uri': 'mongodb://127.0.0.1:27017',
                     'weight': 10,
                     'flavor': 'tasty'}
        pool = self.client.pool('stomach', **pool_data)
        self.addCleanup(pool.delete)

        pool_list = ['stomach']
        flavor_data = {'pool_list': pool_list}
        flavor = self.client.flavor('tasty', **flavor_data)
        flavor.delete()