def test_save_no_changes(self): self.mox.StubOutWithMock(db, 'security_group_update') self.mox.ReplayAll() secgroup = security_group.SecurityGroup._from_db_object( self.context, security_group.SecurityGroup(), fake_secgroup) secgroup.save()
def test_in_use(self): self.mox.StubOutWithMock(db, 'security_group_in_use') db.security_group_in_use(self.context, 123).AndReturn(True) self.mox.ReplayAll() secgroup = security_group.SecurityGroup(context=self.context) secgroup.id = 123 self.assertTrue(secgroup.in_use())
def test_create_with_special_things(self): ctxt = context.get_admin_context() self.mox.StubOutWithMock(db, 'instance_create') fake_inst = fake_instance.fake_db_instance() db.instance_create( ctxt, { 'host': 'foo-host', 'security_groups': ['foo', 'bar'], 'info_cache': { 'network_info': '[]' }, }).AndReturn(fake_inst) self.mox.ReplayAll() inst = instance.Instance() inst.host = 'foo-host' secgroups = security_group.SecurityGroupList() secgroups.objects = [] for name in ('foo', 'bar'): secgroup = security_group.SecurityGroup() secgroup.name = name secgroups.objects.append(secgroup) inst.security_groups = secgroups inst.info_cache = instance_info_cache.InstanceInfoCache() inst.info_cache.network_info = [] inst.create(ctxt)
def test_in_use(self): ctxt = context.get_admin_context() self.mox.StubOutWithMock(db, 'security_group_in_use') db.security_group_in_use(ctxt, 123).AndReturn(True) self.mox.ReplayAll() secgroup = security_group.SecurityGroup() secgroup.id = 123 self.assertTrue(secgroup.in_use(ctxt)) self.assertRemotes()
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)
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'})
def test_refresh(self): updated_secgroup = dict(fake_secgroup, description='changed') self.mox.StubOutWithMock(db, 'security_group_get') db.security_group_get(self.context, 1).AndReturn(updated_secgroup) self.mox.ReplayAll() secgroup = security_group.SecurityGroup._from_db_object( security_group.SecurityGroup(), fake_secgroup) secgroup.refresh(self.context) self.assertEqual(self._fix_deleted(updated_secgroup), dict(secgroup.items())) self.assertEqual(secgroup.obj_what_changed(), set()) self.assertRemotes()
def test_get_by_security_group(self): secgroup = security_group.SecurityGroup() secgroup.id = 123 rule = dict(fake_rule) rule['grantee_group'] = dict(test_security_group.fake_secgroup, id=123) stupid_method = 'security_group_rule_get_by_security_group' with mock.patch.object(db, stupid_method) as sgrgbsg: sgrgbsg.return_value = [rule] rules = (security_group_rule.SecurityGroupRuleList. get_by_security_group(self.context, secgroup)) self.assertEqual(1, len(rules)) self.assertEqual(123, rules[0].grantee_group.id)
def test_save(self): self.mox.StubOutWithMock(db, 'security_group_update') updated_secgroup = dict(fake_secgroup, project_id='changed') db.security_group_update(self.context, 1, { 'description': 'foobar' }).AndReturn(updated_secgroup) self.mox.ReplayAll() secgroup = security_group.SecurityGroup._from_db_object( self.context, security_group.SecurityGroup(), fake_secgroup) secgroup.description = 'foobar' secgroup.save() self.assertEqual(self._fix_deleted(updated_secgroup), dict(secgroup.items())) self.assertEqual(secgroup.obj_what_changed(), set())
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.1') self.assertNotIn('uuid', primitive) # Make sure the uuid is still set before we save(). self.assertIn('uuid', secgroup) secgroup.save() self.assertFalse(mock_db_update.called)
def test_get_by_security_group(self): fake_secgroup = dict(test_security_group.fake_secgroup) fake_secgroup['instances'] = [ fake_instance.fake_db_instance(id=1, system_metadata={'foo': 'bar'}), fake_instance.fake_db_instance(id=2), ] with mock.patch.object(db, 'security_group_get') as sgg: sgg.return_value = fake_secgroup secgroup = security_group.SecurityGroup() secgroup.id = fake_secgroup['id'] instances = instance.InstanceList.get_by_security_group( self.context, secgroup) self.assertEqual(2, len(instances)) self.assertEqual([1, 2], [x.id for x in instances]) self.assertTrue(instances[0].obj_attr_is_set('system_metadata')) self.assertEqual({'foo': 'bar'}, instances[0].system_metadata)
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)
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)
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)
def _from_db_subgroup(context, db_group): if db_group is None: return None return security_group.SecurityGroup._from_db_object( context, security_group.SecurityGroup(), db_group)