File: test_utils.py

package info (click to toggle)
rally-openstack 3.0.0-7
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 8,928 kB
  • sloc: python: 53,131; sh: 262; makefile: 38
file content (196 lines) | stat: -rw-r--r-- 7,423 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
# Copyright 2015: Cisco Systems, 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 unittest import mock

from rally_openstack.task import context
from rally_openstack.task.contexts.swift import utils
from tests.unit import test


class SwiftContext(utils.SwiftObjectMixin, context.OpenStackContext):
    def __init__(self, context):
        self.context = context

    def setup(self):
        pass

    def cleanup(self):
        pass


class SwiftObjectMixinTestCase(test.TestCase):

    @mock.patch("rally_openstack.common.osclients.Clients")
    def test__create_containers(self, mock_clients):
        tenants = 2
        containers_per_tenant = 2
        context = test.get_test_context()
        c = [mock.MagicMock(), mock.MagicMock()]
        context.update({
            "tenants": {
                "1001": {"name": "t1_name"},
                "1002": {"name": "t2_name"}
            },
            "users": [
                {"id": "u1", "tenant_id": "1001", "credential": c[0]},
                {"id": "u2", "tenant_id": "1002", "credential": c[1]}
            ]
        })

        mixin = SwiftContext(context)
        containers = mixin._create_containers(containers_per_tenant, 15)

        self.assertEqual(tenants * containers_per_tenant, len(containers))
        for index, container in enumerate(sorted(containers)):
            offset = int(index / containers_per_tenant) + 1
            self.assertEqual(str(1000 + offset), container[0])

        for index, tenant_id in enumerate(sorted(context["tenants"]), start=1):
            containers = context["tenants"][tenant_id]["containers"]
            self.assertEqual(containers_per_tenant, len(containers))
            for container in containers:
                self.assertEqual("u%d" % index, container["user"]["id"])
                self.assertEqual(c[index - 1],
                                 container["user"]["credential"])
                self.assertEqual(0, len(container["objects"]))

    @mock.patch("rally_openstack.common.osclients.Clients")
    def test__create_objects(self, mock_clients):
        tenants = 2
        containers_per_tenant = 1
        objects_per_container = 5
        context = test.get_test_context()
        context.update({
            "tenants": {
                "1001": {
                    "name": "t1_name",
                    "containers": [
                        {"user": {
                            "id": "u1", "tenant_id": "1001",
                            "credential": mock.MagicMock()},
                         "container": "c1",
                         "objects": []}
                    ]
                },
                "1002": {
                    "name": "t2_name",
                    "containers": [
                        {"user": {
                            "id": "u2", "tenant_id": "1002",
                            "credential": mock.MagicMock()},
                         "container": "c2",
                         "objects": []}
                    ]
                }
            }
        })

        mixin = SwiftContext(context)
        objects_list = mixin._create_objects(objects_per_container, 1024, 25)

        self.assertEqual(
            tenants * containers_per_tenant * objects_per_container,
            len(objects_list))
        chunk = containers_per_tenant * objects_per_container
        for index, obj in enumerate(sorted(objects_list)):
            offset = int(index / chunk) + 1
            self.assertEqual(str(1000 + offset), obj[0])
            self.assertEqual("c%d" % offset, obj[1])

        for tenant_id in context["tenants"]:
            for container in context["tenants"][tenant_id]["containers"]:
                self.assertEqual(objects_per_container,
                                 len(container["objects"]))

    @mock.patch("rally_openstack.common.osclients.Clients")
    def test__delete_containers(self, mock_clients):
        context = test.get_test_context()
        context.update({
            "tenants": {
                "1001": {
                    "name": "t1_name",
                    "containers": [
                        {"user": {
                            "id": "u1", "tenant_id": "1001",
                            "credential": mock.MagicMock()},
                         "container": "c1",
                         "objects": []}
                    ]
                },
                "1002": {
                    "name": "t2_name",
                    "containers": [
                        {"user": {
                            "id": "u2", "tenant_id": "1002",
                            "credential": mock.MagicMock()},
                         "container": "c2",
                         "objects": []}
                    ]
                }
            }
        })

        SwiftContext(context)._delete_containers(1)

        mock_swift = mock_clients.return_value.swift.return_value
        expected_containers = ["c1", "c2"]
        mock_swift.delete_container.assert_has_calls(
            [mock.call(con) for con in expected_containers], any_order=True)

        for tenant_id in context["tenants"]:
            self.assertEqual(0,
                             len(context["tenants"][tenant_id]["containers"]))

    @mock.patch("rally_openstack.common.osclients.Clients")
    def test__delete_objects(self, mock_clients):
        context = test.get_test_context()
        context.update({
            "tenants": {
                "1001": {
                    "name": "t1_name",
                    "containers": [
                        {"user": {
                            "id": "u1", "tenant_id": "1001",
                            "credential": mock.MagicMock()},
                         "container": "c1",
                         "objects": ["o1", "o2", "o3"]}
                    ]
                },
                "1002": {
                    "name": "t2_name",
                    "containers": [
                        {"user": {
                            "id": "u2", "tenant_id": "1002",
                            "credential": mock.MagicMock()},
                         "container": "c2",
                         "objects": ["o4", "o5", "o6"]}
                    ]
                }
            }
        })

        SwiftContext(context)._delete_objects(1)

        mock_swift = mock_clients.return_value.swift.return_value
        expected_objects = [("c1", "o1"), ("c1", "o2"), ("c1", "o3"),
                            ("c2", "o4"), ("c2", "o5"), ("c2", "o6")]
        mock_swift.delete_object.assert_has_calls(
            [mock.call(con, obj) for con, obj in expected_objects],
            any_order=True)

        for tenant_id in context["tenants"]:
            for container in context["tenants"][tenant_id]["containers"]:
                self.assertEqual(0, len(container["objects"]))