summaryrefslogtreecommitdiff
path: root/nova/tests/unit/objects/test_security_group.py
blob: 527e5d84d6547c0271706bb226374305413e4357 (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
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
#    Copyright 2013 IBM Corp.
#
#    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 oslo_utils.fixture import uuidsentinel as uuids
from oslo_versionedobjects import fixture as ovo_fixture

from nova.db.main import api as db
from nova.objects import instance
from nova.objects import security_group
from nova.tests.unit.objects import test_objects


fake_secgroup = {
    'created_at': None,
    'updated_at': None,
    'deleted_at': None,
    'deleted': None,
    'id': 1,
    'name': 'fake-name',
    'description': 'fake-desc',
    'user_id': 'fake-user',
    'project_id': 'fake-project',
    }


class _TestSecurityGroupObject(object):
    def _fix_deleted(self, db_secgroup):
        # NOTE(danms): Account for the difference in 'deleted'
        return dict(db_secgroup.items(), deleted=False)

    @mock.patch.object(db, 'security_group_get',
                       return_value=fake_secgroup)
    def test_get(self, mock_get):
        secgroup = security_group.SecurityGroup.get(self.context, 1)
        ovo_fixture.compare_obj(self, secgroup,
                                self._fix_deleted(fake_secgroup))
        self.assertEqual(secgroup.obj_what_changed(), set())
        mock_get.assert_called_once_with(self.context, 1)

    @mock.patch.object(db, 'security_group_get_by_name',
                       return_value=fake_secgroup)
    def test_get_by_name(self, mock_get):
        secgroup = security_group.SecurityGroup.get_by_name(self.context,
                                                            'fake-project',
                                                            'fake-name')
        ovo_fixture.compare_obj(self, secgroup,
                                self._fix_deleted(fake_secgroup))
        self.assertEqual(secgroup.obj_what_changed(), set())
        mock_get.assert_called_once_with(self.context,
                                         'fake-project',
                                         'fake-name')

    @mock.patch.object(db, 'security_group_in_use',
                       return_value=True)
    def test_in_use(self, mock_inuse):
        secgroup = security_group.SecurityGroup(context=self.context)
        secgroup.id = 123
        self.assertTrue(secgroup.in_use())
        mock_inuse.assert_called_once_with(self.context, 123)

    @mock.patch.object(db, 'security_group_update')
    def test_save(self, mock_update):
        updated_secgroup = dict(fake_secgroup, project_id='changed')
        mock_update.return_value = updated_secgroup
        secgroup = security_group.SecurityGroup._from_db_object(
            self.context, security_group.SecurityGroup(),
            fake_secgroup)
        secgroup.description = 'foobar'
        secgroup.save()
        ovo_fixture.compare_obj(self, secgroup,
                                self._fix_deleted(updated_secgroup))
        self.assertEqual(secgroup.obj_what_changed(), set())
        mock_update.assert_called_once_with(self.context, 1,
                                            {'description': 'foobar'})

    @mock.patch.object(db, 'security_group_update')
    def test_save_no_changes(self, mock_update):
        secgroup = security_group.SecurityGroup._from_db_object(
            self.context, security_group.SecurityGroup(),
            fake_secgroup)
        secgroup.save()
        self.assertFalse(mock_update.called)

    @mock.patch.object(db, 'security_group_get')
    def test_refresh(self, mock_get):
        updated_secgroup = dict(fake_secgroup, description='changed')
        mock_get.return_value = updated_secgroup
        secgroup = security_group.SecurityGroup._from_db_object(
            self.context, security_group.SecurityGroup(self.context),
            fake_secgroup)
        secgroup.refresh()
        ovo_fixture.compare_obj(self, secgroup,
                                self._fix_deleted(updated_secgroup))
        self.assertEqual(secgroup.obj_what_changed(), set())
        mock_get.assert_called_once_with(self.context, 1)

    @mock.patch.object(db, 'security_group_update')
    def test_with_uuid(self, mock_db_update):
        """Tests that we can set a uuid but not save it and it's removed when
        backporting to an older version of the object.
        """
        # Test set/get.
        secgroup = security_group.SecurityGroup(
            self.context, uuid=uuids.neutron_id)
        self.assertEqual(uuids.neutron_id, secgroup.uuid)
        # Test backport.
        primitive = secgroup.obj_to_primitive(target_version='1.2')
        self.assertIn('uuid', primitive['nova_object.data'])
        primitive = secgroup.obj_to_primitive(target_version='1.1')
        self.assertNotIn('uuid', primitive['nova_object.data'])
        # Make sure the uuid is still set before we save().
        self.assertIn('uuid', secgroup)
        secgroup.save()
        self.assertFalse(mock_db_update.called)

    def test_identifier(self):
        secgroup = security_group.SecurityGroup(name='foo')
        self.assertEqual('foo', secgroup.identifier)
        secgroup.uuid = uuids.secgroup
        self.assertEqual(uuids.secgroup, secgroup.identifier)


class TestSecurityGroupObject(test_objects._LocalTest,
                              _TestSecurityGroupObject):
    pass


class TestSecurityGroupObjectRemote(test_objects._RemoteTest,
                                    _TestSecurityGroupObject):
    pass


fake_secgroups = [
    dict(fake_secgroup, id=1, name='secgroup1'),
    dict(fake_secgroup, id=2, name='secgroup2'),
    ]


class _TestSecurityGroupListObject(object):
    @mock.patch.object(db, 'security_group_get_all',
                       return_value=fake_secgroups)
    def test_get_all(self, mock_get):
        secgroup_list = security_group.SecurityGroupList.get_all(self.context)
        for i in range(len(fake_secgroups)):
            self.assertIsInstance(secgroup_list[i],
                                  security_group.SecurityGroup)
            self.assertEqual(fake_secgroups[i]['id'],
                             secgroup_list[i].id)
            self.assertEqual(secgroup_list[i]._context, self.context)
            mock_get.assert_called_once_with(self.context)

    @mock.patch.object(db, 'security_group_get_by_project',
                       return_value=fake_secgroups)
    def test_get_by_project(self, mock_get):
        secgroup_list = security_group.SecurityGroupList.get_by_project(
            self.context, 'fake-project')
        for i in range(len(fake_secgroups)):
            self.assertIsInstance(secgroup_list[i],
                                  security_group.SecurityGroup)
            self.assertEqual(fake_secgroups[i]['id'],
                             secgroup_list[i].id)
        mock_get.assert_called_once_with(self.context, 'fake-project')

    @mock.patch.object(db, 'security_group_get_by_instance',
                       return_value=fake_secgroups)
    def test_get_by_instance(self, mock_get):
        inst = instance.Instance()
        inst.uuid = uuids.instance
        secgroup_list = security_group.SecurityGroupList.get_by_instance(
            self.context, inst)
        for i in range(len(fake_secgroups)):
            self.assertIsInstance(secgroup_list[i],
                                  security_group.SecurityGroup)
            self.assertEqual(fake_secgroups[i]['id'],
                             secgroup_list[i].id)
        mock_get.assert_called_once_with(self.context, inst.uuid)


class TestSecurityGroupListObject(test_objects._LocalTest,
                                  _TestSecurityGroupListObject):
    pass


class TestSecurityGroupListObjectRemote(test_objects._RemoteTest,
                                        _TestSecurityGroupListObject):
    pass