Python testtools.ExpectedException() Examples

The following are 30 code examples of testtools.ExpectedException(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module testtools , or try the search function .
Example #1
Source File: test_service.py    From designate with Apache License 2.0 6 votes vote down vote up
def test_is_valid_zone_name_with_tlds(self):
        # Stop Service
        self.central_service.stop()
        list = objects.TldList()
        list.append(objects.Tld(name='com'))
        list.append(objects.Tld(name='biz'))
        list.append(objects.Tld(name='z'))

        with mock.patch.object(self.central_service.storage, 'find_tlds',
                return_value=list):
            self.central_service.start()

        context = self.get_context()
        with mock.patch.object(self.central_service.storage, 'find_tld',
                return_value=objects.Tld(name='biz')):
            with testtools.ExpectedException(exceptions.InvalidZoneName):
                self.central_service._is_valid_zone_name(context, 'biz.') 
Example #2
Source File: test_basic.py    From designate with Apache License 2.0 6 votes vote down vote up
def test_is_valid_recordset_placement_failing_2(self):
        zone = RoObject(name='example.org.', id=CentralZoneTestCase.zone__id)
        self.service.storage.find_recordsets.return_value = [
            RoObject(),
            RoObject()
        ]
        with testtools.ExpectedException(
                exceptions.InvalidRecordSetLocation) as e:
            self.service._is_valid_recordset_placement(
                self.context,
                zone,
                'example.org.',
                'A',
            )
            self.assertEqual(
                'CNAME recordsets may not share a name with any other records',
                six.text_type(e)) 
Example #3
Source File: test_service.py    From designate with Apache License 2.0 6 votes vote down vote up
def test_create_blacklisted_zone_fail(self):
        self.create_blacklist()

        # Set the policy to reject the authz
        self.policy({'use_blacklisted_zone': '!'})

        values = dict(
            name='blacklisted.com.',
            email='info@blacklisted.com'
        )

        exc = self.assertRaises(rpc_dispatcher.ExpectedException,
                                self.central_service.create_zone,
                                self.admin_context,
                                objects.Zone.from_dict(values))

        self.assertEqual(exceptions.InvalidZoneName, exc.exc_info[0]) 
Example #4
Source File: test_basic.py    From designate with Apache License 2.0 6 votes vote down vote up
def test_update_recordset_action_fail_on_managed(self):
        self.service.storage.get_zone.return_value = RoObject(
            type='foo',
            name='example.org.',
            tenant_id='2',
            action='bogus',
        )
        recordset = mock.Mock()
        recordset.obj_get_changes.return_value = ['foo']
        recordset.managed = True
        self.context = mock.Mock()
        self.context.edit_managed_records = False

        exc = self.assertRaises(rpc_dispatcher.ExpectedException,
                                self.service.update_recordset,
                                self.context,
                                recordset)

        self.assertEqual(exceptions.BadRequest, exc.exc_info[0]) 
Example #5
Source File: test_basic.py    From designate with Apache License 2.0 6 votes vote down vote up
def test_delete_recordset_not_found(self):
        self.service.storage.get_zone.return_value = RoObject(
            action='bogus',
            id=CentralZoneTestCase.zone__id_2,
            name='example.org.',
            tenant_id='2',
            type='foo',
        )
        self.service.storage.get_recordset.return_value = RoObject(
            zone_id=CentralZoneTestCase.zone__id,
            id=CentralZoneTestCase.recordset__id,
            managed=False,
        )
        self.context = mock.Mock()
        self.context.edit_managed_records = False

        exc = self.assertRaises(rpc_dispatcher.ExpectedException,
                                self.service.delete_recordset,
                                self.context,
                                CentralZoneTestCase.zone__id_2,
                                CentralZoneTestCase.recordset__id)

        self.assertEqual(exceptions.RecordSetNotFound, exc.exc_info[0]) 
Example #6
Source File: test_basic.py    From designate with Apache License 2.0 6 votes vote down vote up
def test_delete_recordset_action_delete(self):
        self.service.storage.get_zone.return_value = RoObject(
            action='DELETE',
            id=CentralZoneTestCase.zone__id_2,
            name='example.org.',
            tenant_id='2',
            type='foo',
        )
        self.service.storage.get_recordset.return_value = RoObject(
            zone_id=CentralZoneTestCase.zone__id_2,
            id=CentralZoneTestCase.recordset__id,
            managed=False,
        )
        self.context = mock.Mock()
        self.context.edit_managed_records = False

        exc = self.assertRaises(rpc_dispatcher.ExpectedException,
                                self.service.delete_recordset,
                                self.context,
                                CentralZoneTestCase.zone__id_2,
                                CentralZoneTestCase.recordset__id)

        self.assertEqual(exceptions.BadRequest, exc.exc_info[0]) 
Example #7
Source File: test_basic.py    From designate with Apache License 2.0 6 votes vote down vote up
def test_create_record_fail_on_delete(self):
        self.service.storage.get_zone.return_value = RoObject(
            action='DELETE',
            id=CentralZoneTestCase.zone__id_2,
            name='example.org.',
            tenant_id='2',
            type='foo',
        )
        exc = self.assertRaises(rpc_dispatcher.ExpectedException,
                                self.service.create_record,
                                self.context,
                                CentralZoneTestCase.zone__id,
                                CentralZoneTestCase.recordset__id,
                                RoObject())

        self.assertEqual(exceptions.BadRequest, exc.exc_info[0]) 
Example #8
Source File: test_service.py    From designate with Apache License 2.0 6 votes vote down vote up
def test_create_invalid_recordset_location_cname_at_apex(self):
        zone = self.create_zone()

        values = dict(
            name=zone['name'],
            type='CNAME'
        )

        # Attempt to create a CNAME record at the apex
        exc = self.assertRaises(rpc_dispatcher.ExpectedException,
                                self.central_service.create_recordset,
                                self.admin_context,
                                zone['id'],
                                recordset=objects.RecordSet.from_dict(values))

        self.assertEqual(exceptions.InvalidRecordSetLocation, exc.exc_info[0]) 
Example #9
Source File: test_service.py    From designate with Apache License 2.0 6 votes vote down vote up
def test_create_invalid_recordset_location_cname_sharing(self):
        zone = self.create_zone()
        expected = self.create_recordset(zone)

        values = dict(
            name=expected['name'],
            type='CNAME'
        )

        # Attempt to create a CNAME record alongside another record
        exc = self.assertRaises(rpc_dispatcher.ExpectedException,
                                self.central_service.create_recordset,
                                self.admin_context,
                                zone['id'],
                                recordset=objects.RecordSet.from_dict(values))

        self.assertEqual(exceptions.InvalidRecordSetLocation, exc.exc_info[0]) 
Example #10
Source File: test_basic.py    From designate with Apache License 2.0 6 votes vote down vote up
def test_is_valid_ttl(self):
        self.CONF.set_override('min_ttl', 10, 'service:central')
        self.service._is_valid_ttl(self.context, 20)

        # policy.check() not to raise: the user is allowed to create low TTLs
        designate.central.service.policy.check = mock.Mock(return_value=None)
        self.service._is_valid_ttl(self.context, None)
        self.service._is_valid_ttl(self.context, 1)

        # policy.check() to raise
        designate.central.service.policy.check = mock.Mock(
            side_effect=exceptions.Forbidden
        )

        with testtools.ExpectedException(exceptions.InvalidTTL):
            self.service._is_valid_ttl(self.context, 3) 
Example #11
Source File: test_service.py    From designate with Apache License 2.0 6 votes vote down vote up
def test_create_invalid_recordset_location_wrong_zone(self):
        zone = self.create_zone()
        other_zone = self.create_zone(fixture=1)

        values = dict(
            name=other_zone['name'],
            type='A'
        )

        # Attempt to create a record in the incorrect zone
        exc = self.assertRaises(rpc_dispatcher.ExpectedException,
                                self.central_service.create_recordset,
                                self.admin_context,
                                zone['id'],
                                recordset=objects.RecordSet.from_dict(values))

        self.assertEqual(exceptions.InvalidRecordSetLocation, exc.exc_info[0]) 
Example #12
Source File: test_service.py    From designate with Apache License 2.0 6 votes vote down vote up
def test_delete_recordset(self):
        zone = self.create_zone()
        original_serial = zone.serial

        # Create a recordset
        recordset = self.create_recordset(zone)

        # Delete the recordset
        self.central_service.delete_recordset(
            self.admin_context, zone['id'], recordset['id'])

        # Fetch the recordset again, ensuring an exception is raised
        exc = self.assertRaises(rpc_dispatcher.ExpectedException,
                                self.central_service.get_recordset,
                                self.admin_context, zone['id'],
                                recordset['id'])

        self.assertEqual(exceptions.RecordSetNotFound, exc.exc_info[0])

        # Fetch the zone again to verify serial number increased
        updated_zone = self.central_service.get_zone(self.admin_context,
                                                     zone.id)
        new_serial = updated_zone.serial
        self.assertThat(new_serial, GreaterThan(original_serial)) 
Example #13
Source File: test_basic.py    From designate with Apache License 2.0 6 votes vote down vote up
def test_get_record_not_found(self):
        self.service.storage.get_zone.return_value = RoObject(
            id=CentralZoneTestCase.zone__id_2,
        )
        self.service.storage.get_recordset.return_value = RoObject(
            zone_id=CentralZoneTestCase.recordset__id
        )

        exc = self.assertRaises(rpc_dispatcher.ExpectedException,
                                self.service.get_record,
                                self.context,
                                CentralZoneTestCase.zone__id_2,
                                CentralZoneTestCase.recordset__id,
                                CentralZoneTestCase.record__id)

        self.assertEqual(exceptions.RecordNotFound, exc.exc_info[0]) 
Example #14
Source File: test_basic.py    From designate with Apache License 2.0 6 votes vote down vote up
def test_get_record_not_found_2(self):
        self.service.storage.get_zone.return_value = RoObject(
            id=CentralZoneTestCase.zone__id_2,
            name='example.org.',
            tenant_id=2,
        )
        self.service.storage.get_recordset.return_value = RoObject(
            zone_id=CentralZoneTestCase.zone__id_2,
            id=999,  # not matching record.recordset_id
            name='foo'
        )
        self.service.storage.get_record.return_value = RoObject(
            id=CentralZoneTestCase.record__id,
            zone_id=CentralZoneTestCase.zone__id_2,
            recordset_id=CentralZoneTestCase.recordset__id
        )

        exc = self.assertRaises(rpc_dispatcher.ExpectedException,
                                self.service.get_record,
                                self.context,
                                CentralZoneTestCase.zone__id_2,
                                CentralZoneTestCase.recordset__id,
                                CentralZoneTestCase.record__id)

        self.assertEqual(exceptions.RecordNotFound, exc.exc_info[0]) 
Example #15
Source File: test_service.py    From designate with Apache License 2.0 6 votes vote down vote up
def test_get_record_incorrect_recordset_id(self):
        zone = self.create_zone()
        recordset = self.create_recordset(zone)
        other_recordset = self.create_recordset(zone, fixture=1)

        # Create a record
        expected = self.create_record(zone, recordset)

        exc = self.assertRaises(rpc_dispatcher.ExpectedException,
                                self.central_service.get_record,
                                self.admin_context, zone['id'],
                                other_recordset['id'],
                                expected['id'])

        # Ensure we get a 404 if we use the incorrect recordset_id
        self.assertEqual(exceptions.RecordNotFound, exc.exc_info[0]) 
Example #16
Source File: test_service.py    From designate with Apache License 2.0 6 votes vote down vote up
def test_update_record_immutable_zone_id(self):
        zone = self.create_zone()
        recordset = self.create_recordset(zone)
        other_zone = self.create_zone(fixture=1)

        # Create a record
        record = self.create_record(zone, recordset)

        # Update the record
        record.zone_id = other_zone.id

        # Ensure we get a BadRequest if we change the zone_id
        exc = self.assertRaises(rpc_dispatcher.ExpectedException,
                                self.central_service.update_record,
                                self.admin_context, record)

        self.assertEqual(exceptions.BadRequest, exc.exc_info[0]) 
Example #17
Source File: test_service.py    From designate with Apache License 2.0 6 votes vote down vote up
def test_update_record_immutable_recordset_id(self):
        zone = self.create_zone()
        recordset = self.create_recordset(zone)
        other_recordset = self.create_recordset(zone, fixture=1)

        # Create a record
        record = self.create_record(zone, recordset)

        # Update the record
        record.recordset_id = other_recordset.id

        # Ensure we get a BadRequest if we change the recordset_id
        exc = self.assertRaises(rpc_dispatcher.ExpectedException,
                                self.central_service.update_record,
                                self.admin_context, record)

        self.assertEqual(exceptions.BadRequest, exc.exc_info[0]) 
Example #18
Source File: test_service.py    From designate with Apache License 2.0 6 votes vote down vote up
def test_delete_record_incorrect_recordset_id(self):
        zone = self.create_zone()
        recordset = self.create_recordset(zone)
        other_recordset = self.create_recordset(zone, fixture=1)

        # Create a record
        record = self.create_record(zone, recordset)

        # Ensure we get a 404 if we use the incorrect recordset_id
        exc = self.assertRaises(rpc_dispatcher.ExpectedException,
                                self.central_service.delete_record,
                                self.admin_context, zone['id'],
                                other_recordset['id'],
                                record['id'])

        self.assertEqual(exceptions.RecordNotFound, exc.exc_info[0]) 
Example #19
Source File: test_ssh.py    From tempest-lib with Apache License 2.0 6 votes vote down vote up
def test_get_ssh_connection_timeout(self):
        c_mock, aa_mock, client_mock = self._set_ssh_connection_mocks()

        c_mock.return_value = client_mock
        client_mock.connect.side_effect = [
            socket.error,
            socket.error,
            socket.error,
        ]

        client = ssh.Client('localhost', 'root', timeout=2)
        start_time = int(time.time())
        with testtools.ExpectedException(exceptions.SSHTimeout):
            client._get_ssh_connection()
        end_time = int(time.time())
        self.assertLess((end_time - start_time), 5)
        self.assertGreaterEqual((end_time - start_time), 2) 
Example #20
Source File: test_service.py    From designate with Apache License 2.0 6 votes vote down vote up
def test_is_valid_zone_name(self):
        self.config(max_zone_name_len=10,
                    group='service:central')

        context = self.get_context()

        self.central_service._is_valid_zone_name(context, 'valid.org.')

        with testtools.ExpectedException(exceptions.InvalidZoneName):
            self.central_service._is_valid_zone_name(context, 'example.org.')

        with testtools.ExpectedException(exceptions.InvalidZoneName):
            self.central_service._is_valid_zone_name(context, 'example.tld.')

        with testtools.ExpectedException(exceptions.InvalidZoneName):
            self.central_service._is_valid_zone_name(context, 'tld.') 
Example #21
Source File: test_service.py    From designate with Apache License 2.0 5 votes vote down vote up
def test_create_record_and_update_over_zone_quota(self):
        # SOA and NS Records exist
        self.config(quota_zone_records=1)

        # Creating the zone automatically creates SOA & NS records
        zone = self.create_zone()
        recordset = self.create_recordset(zone)

        self.create_record(zone, recordset)

        exc = self.assertRaises(rpc_dispatcher.ExpectedException,
                                self.create_record,
                                zone, recordset)

        self.assertEqual(exceptions.OverQuota, exc.exc_info[0]) 
Example #22
Source File: test_service.py    From designate with Apache License 2.0 5 votes vote down vote up
def test_delete_recordset_incorrect_zone_id(self):
        zone = self.create_zone()
        other_zone = self.create_zone(fixture=1)

        # Create a recordset
        recordset = self.create_recordset(zone)

        # Ensure we get a 404 if we use the incorrect zone_id
        exc = self.assertRaises(rpc_dispatcher.ExpectedException,
                                self.central_service.delete_recordset,
                                self.admin_context, other_zone['id'],
                                recordset['id'])

        self.assertEqual(exceptions.RecordSetNotFound, exc.exc_info[0]) 
Example #23
Source File: test_service.py    From designate with Apache License 2.0 5 votes vote down vote up
def _test_create_zone_fail(self, values, exception):

        with testtools.ExpectedException(exception):
            # Create an invalid zone
            self.central_service.create_zone(
                self.admin_context, objects.Zone.from_dict(values)) 
Example #24
Source File: test_service.py    From designate with Apache License 2.0 5 votes vote down vote up
def test_count_zones_policy_check(self):
        # Set the policy to reject the authz
        self.policy({'count_zones': '!'})

        exc = self.assertRaises(rpc_dispatcher.ExpectedException,
                                self.central_service.count_zones,
                                self.get_context())

        self.assertEqual(exceptions.Forbidden, exc.exc_info[0]) 
Example #25
Source File: test_service.py    From designate with Apache License 2.0 5 votes vote down vote up
def test_create_superzone_failure(self):
        context = self.get_admin_context()

        # Explicitly set a tenant_id
        context.project_id = '1'

        # Set up zone and subzone values
        zone_values = self.get_zone_fixture(fixture=1)
        zone_name = zone_values['name']

        subzone_values = copy.deepcopy(zone_values)
        subzone_values['name'] = 'www.%s' % zone_name
        subzone_values['context'] = context

        # Create sub zone
        self.create_zone(**subzone_values)

        context = self.get_admin_context()

        # Explicitly use a different tenant_id
        context.project_id = '2'

        # Attempt to create the zone
        exc = self.assertRaises(rpc_dispatcher.ExpectedException,
                                self.central_service.create_zone,
                                context, objects.Zone.from_dict(zone_values))

        self.assertEqual(exceptions.IllegalParentZone, exc.exc_info[0]) 
Example #26
Source File: test_service.py    From designate with Apache License 2.0 5 votes vote down vote up
def test_create_zone_over_quota(self):
        self.config(quota_zones=1)

        self.create_zone()

        exc = self.assertRaises(rpc_dispatcher.ExpectedException,
                                self.create_zone)

        self.assertEqual(exceptions.OverQuota, exc.exc_info[0]) 
Example #27
Source File: test_service.py    From designate with Apache License 2.0 5 votes vote down vote up
def test_is_valid_ttl(self):
        self.policy({'use_low_ttl': '!'})
        self.config(min_ttl=100,
                    group='service:central')
        context = self.get_context()

        values = self.get_zone_fixture(fixture=1)
        values['ttl'] = 0

        with testtools.ExpectedException(exceptions.InvalidTTL):
            self.central_service._is_valid_ttl(
                context, values['ttl'])

    # TLD Tests 
Example #28
Source File: test_service.py    From designate with Apache License 2.0 5 votes vote down vote up
def test_delete_tld(self):
        # Create a tld
        tld = self.create_tld(fixture=0)
        # Delete the tld
        self.central_service.delete_tld(self.admin_context, tld['id'])

        # Fetch the tld again, ensuring an exception is raised
        exc = self.assertRaises(rpc_dispatcher.ExpectedException,
                                self.central_service.get_tld,
                                self.admin_context, tld['id'])

        self.assertEqual(exceptions.TldNotFound, exc.exc_info[0])

    # TsigKey Tests 
Example #29
Source File: test_service.py    From designate with Apache License 2.0 5 votes vote down vote up
def test_delete_tsigkey(self):
        # Create a tsigkey
        tsigkey = self.create_tsigkey()

        # Delete the tsigkey
        self.central_service.delete_tsigkey(self.admin_context, tsigkey['id'])

        # Fetch the tsigkey again, ensuring an exception is raised
        exc = self.assertRaises(rpc_dispatcher.ExpectedException,
                                self.central_service.get_tsigkey,
                                self.admin_context, tsigkey['id'])

        self.assertEqual(exceptions.TsigKeyNotFound, exc.exc_info[0])

    # Tenant Tests 
Example #30
Source File: test_service.py    From designate with Apache License 2.0 5 votes vote down vote up
def test_count_tenants_policy_check(self):
        # Set the policy to reject the authz
        self.policy({'count_tenants': '!'})

        exc = self.assertRaises(rpc_dispatcher.ExpectedException,
                                self.central_service.count_tenants,
                                self.get_context())

        self.assertEqual(exceptions.Forbidden, exc.exc_info[0])

    # Zone Tests