def test_empty_distro_name(self): # If distro.name returns an empty string, as it does on Windows and # Mac OS X, we fall back to the result of platform.system(). self.useFixture(MockPatch('distro.name', return_value='')) self.useFixture(MockPatch('platform.system', return_value='BarOS')) self.useFixture(MockPatch('socket.gethostname', return_value='bar')) consumer = SystemWideConsumer("app name") self.assertEqual(consumer.key, 'System-wide: BarOS (bar)')
def test_useful_distro_name(self): # If distro.name returns a useful string, as it does on Ubuntu, # we'll use the first string for the system type. self.useFixture(MockPatch('distro.name', return_value='Fooix')) self.useFixture(MockPatch('platform.system', return_value='FooOS')) self.useFixture(MockPatch('socket.gethostname', return_value='foo')) consumer = SystemWideConsumer("app name") self.assertEqual(consumer.key, 'System-wide: Fooix (foo)')
def test_broken_distro_name(self): # If distro.name raises an exception, we fall back to the result of # platform.system(). self.useFixture( MockPatch('distro.name', side_effect=Exception('Oh noes!'))) self.useFixture(MockPatch('platform.system', return_value='BazOS')) self.useFixture(MockPatch('socket.gethostname', return_value='baz')) consumer = SystemWideConsumer("app name") self.assertEqual(consumer.key, 'System-wide: BazOS (baz)')
def test_already_dead(self): exception = OSError() exception.errno = errno.ESRCH kill_results = iter([None, exception]) kill = self.useFixture(MockPatch('os.kill', side_effect=kill_results)).mock sleep = self.useFixture(MockPatch('time.sleep')).mock two_stage_kill(123) self.assertEqual(2, kill.call_count) kill.assert_has_calls([((123, signal.SIGTERM), {}), ((123, 0), {})]) sleep.assert_not_called()
def test_dies_immediately(self): exception = OSError() exception.errno = errno.ESRCH kill_results = iter([None, None, exception]) kill = self.useFixture(MockPatch('os.kill', side_effect=kill_results)).mock sleep = self.useFixture(MockPatch('time.sleep')).mock two_stage_kill(123) self.assertEqual(3, kill.call_count) kill.assert_has_calls([((123, signal.SIGTERM), {})] + [((123, 0), {})] * 2) sleep.assert_called_once_with(0.1)
def test_dies_slowly(self): exception = OSError() exception.errno = errno.ESRCH kill_results = iter([None] * 50 + [exception]) kill = self.useFixture(MockPatch('os.kill', side_effect=kill_results)).mock sleep = self.useFixture(MockPatch('time.sleep')).mock two_stage_kill(123) self.assertEqual(51, kill.call_count) kill.assert_has_calls([((123, signal.SIGTERM), {})] + [((123, 0), {})] * 50) self.assertEqual(49, sleep.call_count) sleep.assert_has_calls([((0.1, ), {})] * 49)
def test_search_uses_proxy(self): proxy = 'http://proxy.example:3128/' self.pushConfig('launchpad', http_proxy=proxy) fake_send = FakeMethod(result=Response()) self.useFixture( MockPatch('requests.adapters.HTTPAdapter.send', fake_send)) # Our mock doesn't return a valid response, but we don't care; we # only care about how the adapter is called. self.assertRaises(SiteSearchResponseError, self.search_service.search, 'fnord') self.assertThat( fake_send.calls[0][1]['proxies'], ContainsDict( {scheme: Equals(proxy) for scheme in ('http', 'https')}))
def test_unicode_url(self): # Python's httplib doesn't like Unicode URLs much. Ensure that # they don't cause it to crash, and we get a post-serialisation # connection error instead. self.useFixture( MockPatch("socket.getaddrinfo", side_effect=socket.gaierror( socket.EAI_NONAME, "Name or service not known"))) transport = RequestsTransport(u"http://test.invalid/") for proxy in (None, "http://squid.internal:3128/"): self.pushConfig("launchpad", http_proxy=proxy) e = self.assertRaises(requests.ConnectionError, transport.request, u"test.invalid", u"xmlrpc", u"\N{SNOWMAN}".encode('utf-8')) self.assertIn("Name or service not known", str(e))
def test_purge_zones(self): # Create 18 zones, run zone_manager, check if 7 zones are remaining self.config(quota_zones=1000) self._create_deleted_zones() self.purge_task_fixture.task() zones = self._fetch_all_zones() LOG.info("Number of zones: %d", len(zones)) self.assertEqual(7, len(zones)) fx_pool_manager = MockPatch( 'designate.zone_manager.tasks.PoolManagerAPI.get_instance', MagicMock(spec_set=[ 'update_zone', ])) class PeriodicGenerateDelayedNotifyTaskTest(TaskTest): def setUp(self): super(PeriodicGenerateDelayedNotifyTaskTest, self).setUp() self.config(interval=5, batch_size=100, group="zone_manager_task:delayed_notify") self.generate_delayed_notify_task_fixture = self.useFixture( fixtures.ZoneManagerTaskFixture( tasks.PeriodicGenerateDelayedNotifyTask))
def test_mock_patch_without_replacement(self): self.useFixture(MockPatch('%s.Foo.bar' % (__name__))) instance = Foo() self.assertIsInstance(instance.bar(), mock.MagicMock)
def test_mock_patch_with_replacement(self): self.useFixture(MockPatch('%s.Foo.bar' % (__name__), mocking_bar)) instance = Foo() self.assertEqual(instance.bar(), 'mocked!')
def test_with_unknown_error(self): exception = OSError() exception.errno = errno.ENOMEM self.useFixture(MockPatch('os.kill', side_effect=exception)) self.assertRaises(OSError, process_exists, 123)
def test_with_process_not_running(self): exception = OSError() exception.errno = errno.ESRCH self.useFixture(MockPatch('os.kill', side_effect=exception)) self.assertFalse(process_exists(123))