def test_WriteServers(self): """Test WriteServers().""" servers = [ prodmon.Server(hostname='harvestasha-xp', status='primary', roles=frozenset(('scheduler', 'host_scheduler', 'suite_scheduler', 'afe')), created='2014-12-11 22:48:43', modified='2014-12-11 22:48:43', note=''), prodmon.Server(hostname='harvestasha-vista', status='primary', roles=frozenset(('devserver', )), created='2015-01-05 13:32:49', modified='2015-01-05 13:32:49', note=''), ] presence_metric = mock.NonCallableMock(['set']) roles_metric = mock.NonCallableMock(['set']) sink = prodmon.TsMonSink(presence_metric=presence_metric, roles_metric=roles_metric) sink.WriteServers(servers) self.assertEqual(presence_metric.set.call_args_list, [ mock.call(True, {'target_hostname': 'harvestasha-xp'}), mock.call(True, {'target_hostname': 'harvestasha-vista'}), ]) self.assertEqual(roles_metric.set.call_args_list, [ mock.call('afe,host_scheduler,scheduler,suite_scheduler', {'target_hostname': 'harvestasha-xp'}), mock.call('devserver', {'target_hostname': 'harvestasha-vista'}), ])
def testCalculateDeviceMd5Sums_singlePath(self): test_path = '/storage/emulated/legacy/test/file.dat' device = mock.NonCallableMock() device.adb = mock.NonCallableMock() device.adb.Push = mock.Mock() device_md5sum_output = [ '0123456789abcdeffedcba9876543210 ' '/storage/emulated/legacy/test/file.dat', ] device.RunShellCommand = mock.Mock(return_value=device_md5sum_output) mock_temp_file = mock.mock_open() mock_temp_file.return_value.name = '/tmp/test/script/file.sh' mock_device_temp_file = mock.mock_open() mock_device_temp_file.return_value.name = ( '/data/local/tmp/test/script/file.sh') with mock.patch('tempfile.NamedTemporaryFile', new=mock_temp_file), (mock.patch( 'pylib.utils.device_temp_file.DeviceTempFile', new=mock_device_temp_file)): out = md5sum.CalculateDeviceMd5Sums(test_path, device) self.assertEquals(1, len(out)) self.assertTrue('/storage/emulated/legacy/test/file.dat' in out) self.assertEquals('0123456789abcdeffedcba9876543210', out['/storage/emulated/legacy/test/file.dat']) device.adb.Push.assert_called_once_with( '/tmp/test/script/file.sh', '/data/local/tmp/test/script/file.sh') device.RunShellCommand.assert_called_once_with( ['sh', '/data/local/tmp/test/script/file.sh'])
def testRecvFloodedSocket(self): """Test receiving an event when watermark was passed.""" streamsock = mock.NonCallableMock() streamsock.recv.side_effect = self.events[2] streamsock.getsockopt.side_effect = [True, True, False] reqsock = mock.NonCallableMock() toreceive = (self.events[1][0], self.events[1][2], b'END') reqsock.recv.side_effect = toreceive # Need two 'False' here due to assertion logic in query code reqsock.getsockopt.side_effect = [True, True, False, False] results = [] for result in clients.yield_events_after(streamsock, reqsock, self.events[0][0]): results.append(result) # Implementation specific tests that have been used mostly for # debugging of the code. Can be removed without being too worried. assert not streamsock.send.called reqsock.send.assert_has_calls([ mock.call(b"QUERY", zmq.SNDMORE), mock.call(self.events[0][0], zmq.SNDMORE), mock.call(self.events[1][0]) ]) self.assertEqual(streamsock.recv.call_count, 3, streamsock.recv.call_args_list) self.assertEqual(reqsock.recv.call_count, 3) # The actual test that makes sure result is what it's supposed to be. self.assertEqual(results, [(self.events[1][0], self.events[1][2]), (self.events[2][0], self.events[2][2])])
def testCalculateDeviceMd5Sums_requiresBinary(self): test_path = '/storage/emulated/legacy/test/file.dat' device = mock.NonCallableMock() device.adb = mock.NonCallableMock() device.adb.Push = mock.Mock() device_md5sum_output = [ 'WARNING: linker: /data/local/tmp/md5sum/md5sum_bin: ' 'unused DT entry: type 0x1d arg 0x15db', 'THIS_IS_NOT_A_VALID_CHECKSUM_ZZZ some random text', '0123456789abcdef', ] error = device_errors.AdbShellCommandFailedError('cmd', 'out', 2) device.RunShellCommand = mock.Mock( side_effect=(error, '', device_md5sum_output)) with mock.patch( 'os.path.isdir', return_value=True), (mock.patch( 'os.path.getsize', return_value=1337)): out = md5sum.CalculateDeviceMd5Sums(test_path, device) self.assertEquals(1, len(out)) self.assertTrue('/storage/emulated/legacy/test/file.dat' in out) self.assertEquals('0123456789abcdef', out['/storage/emulated/legacy/test/file.dat']) self.assertEquals(3, len(device.RunShellCommand.call_args_list)) device.adb.Push.assert_called_once_with('test/out/directory/md5sum_dist', '/data/local/tmp/md5sum')
async def test_all_masters__with_attempt_timeout(mocker, event_loop): cl = Cluster(["addr1"], attempt_timeout=0.001) mocked_pooler = mocker.patch.object(cl, "_pooler", new=get_pooler_mock()) pool = mocked_pooler._pool execute1_fut = event_loop.create_future() execute1_fut.set_result(None) execute2_fut = event_loop.create_future() execute_futs_iter = iter([execute1_fut, execute2_fut]) async def execute_side_effect(*args, **kwargs): return await next(execute_futs_iter) pool.execute.side_effect = execute_side_effect mocked_manager = mocker.patch.object(cl, "_manager", new=get_manager_mock()) state = mocked_manager.get_state.return_value state._data.masters = [ mock.NonCallableMock(addr=Address("1.2.3.4", 6666)), mock.NonCallableMock(addr=Address("1.2.3.4", 9999)), ] mocked_execute_retry_slowdown = mocker.patch.object( cl, "_execute_retry_slowdown", new=create_async_mock()) with pytest.raises(asyncio.TimeoutError): await cl.all_masters() assert mocked_manager.get_state.call_count == 1 assert mocked_pooler.ensure_pool.call_count == 2 assert mocked_manager.require_reload_state.call_count == 1 mocked_execute_retry_slowdown.assert_not_called()
def get_pooler_mock(): mocked = mock.NonCallableMock() mocked.ensure_pool = create_async_mock() mocked.close_only = create_async_mock() conn = mock.NonCallableMock() conn.execute = create_async_mock() pool = mocked.ensure_pool.return_value pool.execute = create_async_mock() pool.auth = create_async_mock() pool.acquire = create_async_mock(return_value=conn) pool.maxsize = 10 pool.minsize = 1 pool.size = 1 pool.freesize = 0 conn_acquirer = mock.MagicMock() conn_acquirer.__aenter__ = create_async_mock(return_value=conn) conn_acquirer.__aexit__ = create_async_mock(return_value=None) pool.get.return_value = conn_acquirer mocked._pool = pool mocked._conn = conn return mocked
def get_mock(cls, buffer_content=None, current_line_index=None, **config): """ Get a vim mock with the configuration according to the arguments. buffer_content: A list of lines in the current buffer current_line_index: An index into the buffer_content **config: Vim configuration. See ConfigMapping for possible keys and what they mean. """ factory_instance = cls( current_line_index=current_line_index, buffer_content=buffer_content, **config) vim_mock = mock.NonCallableMock( spec_set=['eval', 'command', 'current', 'buffers']) vim_mock.eval = mock.Mock(spec_set=[], side_effect=factory_instance.eval_mocker) vim_mock.command = mock.Mock(spec_set=[]) vim_mock.current = mock.NonCallableMock( spec_set=['buffer', 'line', 'window']) if buffer_content is None: type(vim_mock.current).buffer = mock.PropertyMock( side_effect=LCTestUtilsError( "ERROR: no buffer specified")) else: vim_mock.current.buffer = buffer_content return vim_mock
def test_simple(tmpdir, capsys): with tmpdir.join('ceph.conf').open('w') as f: f.write("""\ [global] fsid = 6ede5564-3cf1-44b5-aa96-1c77b0c3e1d0 mon host = host1 """) ns = argparse.Namespace() conn_osd = mock.NonCallableMock(name='PushyClient-osd') mock_compiled_osd = collections.defaultdict(mock.Mock) #conn_osd.compile.side_effect = mock_compiled_osd.__getitem__ conn_osd.compile.return_value = mock.Mock(return_value='fakekeyring') conn_mon = mock.NonCallableMock(name='PushyClient-mon') mock_compiled_mon = collections.defaultdict(mock.Mock) conn_mon.compile.side_effect = mock_compiled_mon.__getitem__ ns.pushy = mock.Mock(name='pushy namespace') def _conn(url): if url == 'ssh+sudo:host1': return conn_mon elif url == 'ssh+sudo:storehost1:sdc': return conn_osd else: raise AssertionError('Unexpected connection url: %r', url) ns.pushy.side_effect = _conn BOOTSTRAP_KEY = 'fakekeyring' mock_compiled_mon[osd.get_bootstrap_osd_key].side_effect = BOOTSTRAP_KEY def _create_osd(cluster, find_key): key = find_key() assert key == BOOTSTRAP_KEY mock_compiled_osd[osd.create_osd].side_effect = _create_osd with directory(str(tmpdir)): main( args=['-v', 'gatherkeys', 'storehost1:sdc'], namespace=ns, ) main( args=['-v', 'osd', 'prepare', 'storehost1:sdc'], namespace=ns, ) out, err = capsys.readouterr() err = err.lower() assert 'have ceph.mon.keyring' in err assert 'have ceph.client.admin.keyring' in err assert 'have ceph.bootstrap-osd.keyring' in err assert 'got ceph.bootstrap-mds.keyring key from storehost1:sdc' in err assert 'got ceph.bootstrap-osd.keyring key from storehost1:sdc' in err
def test_extract_fk(self): """Test that we can get attributes of a fk field using dot notation.""" self.row1.parent = mock.NonCallableMock() self.row1.parent.name = 'parent 1' self.row2.parent = mock.NonCallableMock() self.row2.parent.name = 'parent 2' header = ['name', 'address', 'counter', 'parent.name'] data = utils.extract_qset_data(self.qset, header) self.assertEqual(['me', 'somewhere', '5', 'parent 1'], data[1]) self.assertEqual(['you', 'elsewhere', '17', 'parent 2'], data[2])
def test_callable_queryset(self): request = mock.NonCallableMock(spec=[]) qs = mock.NonCallableMock(spec=[]) qs_callable = mock.Mock(return_value=qs) f = ModelMultipleChoiceFilter(queryset=qs_callable) f.parent = mock.Mock(request=request) field = f.field qs_callable.assert_called_with(request) self.assertEqual(field.queryset, qs)
def test_get_api_version(self, nova): context = mock.NonCallableMock(session=mock.sentinel.session) v2 = mock.NonCallableMock() v2.configure_mock(id='v2', version='', links=[{ 'href': 'http://host:port/path/v2/' }]) v2_1 = mock.NonCallableMock() v2_1.configure_mock(id='v2.1', version='2.11', links=[{ 'href': 'http://host:port/path/v2.1/' }]) # test normal flow nova.return_value.versions.get_current.return_value = v2_1 with fixtures.LoggerFixture( format='[%(levelname)s] %(message)s') as logs: res = clients._get_nova_api_version(context) self.assertEqual(clients.REQUIRED_NOVA_API_MICROVERSION, res) nova.assert_called_with('2.1', service_type='compute', session=mock.sentinel.session) nova.return_value.versions.get_current.assert_called_with() self.assertTrue(logs.output.startswith('[INFO]')) # test Nova doesn't supprt required microversion v2_1.version = '2.2' with fixtures.LoggerFixture( format='[%(levelname)s] %(message)s') as logs: res = clients._get_nova_api_version(context) self.assertEqual('2.2', res) self.assertTrue(logs.output.startswith('[WARNING]')) # test service type is not v2.1 nova.return_value.versions.get_current.return_value = v2 self.configure(nova_service_type='compute_legacy') with fixtures.LoggerFixture( format='[%(levelname)s] %(message)s') as logs: res = clients._get_nova_api_version(context) self.assertEqual('2', res) self.assertTrue(logs.output.startswith('[WARNING]')) self.configure(nova_service_type='compute') # test service url is not found in version list nova.return_value.versions.get_current.return_value = None with fixtures.LoggerFixture( format='[%(levelname)s] %(message)s') as logs: res = clients._get_nova_api_version(context) self.assertEqual(clients.REQUIRED_NOVA_API_MICROVERSION, res) self.assertTrue(logs.output.startswith('[WARNING]'))
def test_get_queryset_override(self): request = mock.NonCallableMock(spec=[]) qs = mock.NonCallableMock(spec=[]) class F(ModelChoiceFilter): get_queryset = mock.create_autospec(ModelChoiceFilter.get_queryset, return_value=qs) f = F() f.parent = mock.Mock(request=request) field = f.field f.get_queryset.assert_called_with(f, request) self.assertEqual(field.queryset, qs)
def test_extract_multilevel_callable(self): """Test that we can go through multiple levels of callable attributes.""" child1 = mock.Mock() child1.name = lambda: "Child 1" # row1.parent.child().name() child2 = mock.Mock() child2.name = lambda: "Child 2" # row2.parent.child().name() self.row1.parent = mock.NonCallableMock() self.row1.parent.child = lambda: child1 self.row2.parent = mock.NonCallableMock() self.row2.parent.child = lambda: child2 header = ['parent.child.name'] data = utils.extract_qset_data(self.qset, header) self.assertEqual(['Child 1'], data[1]) self.assertEqual(['Child 2'], data[2])
def testRecvFirstEvent(self): """Test fetching the absolutely first event.""" streamsock = mock.NonCallableMock() streamsock.recv.side_effect = self.events[0] streamsock.getsockopt.side_effect = [True, True, False, False] reqsock = mock.NonCallableMock() results = [] for result in clients.yield_events_after(streamsock, reqsock): results.append(result) self.assertEqual(results, [(self.events[0][0], self.events[0][2])]) assert streamsock.recv.called assert not reqsock.recv.called
def SetUp(self): self.StartObjectPatch(os.path, 'isdir', return_value=True) self.service = service.Service.New(self.mock_serverless_client, self.Project()) self.service.domain = 'https://foo-bar.baz' self.service.status.latestReadyRevisionName = 'rev.1' self.service.status_traffic.SetPercent('rev.1', 100) self.service.spec_traffic.SetPercent('rev.1', 100) self.operations.GetService.return_value = self.service self.app = mock.NonCallableMock() self.StartObjectPatch(config_changes, 'ImageChange', return_value=self.app) self.env_changes = mock.NonCallableMock() self.env_mock = self.StartObjectPatch( config_changes, 'EnvVarLiteralChanges', return_value=self.env_changes)
def testRecvNonFloodedNextEvent(self): """Test receiving the next event through streaming socket only.""" streamsock = mock.NonCallableMock() streamsock.recv.side_effect = self.events[2] streamsock.getsockopt.side_effect = [True, True, False] reqsock = mock.NonCallableMock() results = [] for result in clients.yield_events_after(streamsock, reqsock, self.events[1][0]): results.append(result) self.assertEqual(results, [(self.events[2][0], self.events[2][2])]) assert streamsock.recv.called assert not reqsock.recv.called
def _createSnapshot(self, start_time): """ Helper to create a mock EC2.Snapshot instance that has a start_date attribute. """ snapshot = mock.NonCallableMock() snapshot.start_time = self._createDatetime(start_time) return snapshot
def test_get_brand_multiple_ents_with_branding_different_branded_name( self): # inj a prod dir with some installed products stub_product = DefaultStubProduct() # same product id, different name stub_product_2 = StubProduct( id=123, brand_type='OS', name='A Different Stub Product', brand_name='A Different branded Stub Product') mock_product_dir = mock.NonCallableMock() # note stub_product.id=123 will match the Product from both ents mock_product_dir.get_installed_products.return_value = [ stub_product.id ] inj.provide(inj.PROD_DIR, mock_product_dir) mock_ent_cert = mock.Mock() mock_ent_cert.products = [stub_product] mock_ent_cert_2 = mock.Mock() mock_ent_cert_2.products = [stub_product_2] ent_certs = [mock_ent_cert, mock_ent_cert_2] brand_picker = rhelentbranding.RHELBrandPicker(ent_certs) brand = brand_picker.get_brand() self.assertTrue(brand is None)
def test(self): stub_product = DefaultStubProduct() mock_prod_dir = mock.NonCallableMock(name='MockProductDir') mock_prod_dir.get_installed_products.return_value = [stub_product.id] inj.provide(inj.PROD_DIR, mock_prod_dir) mock_ent_cert = mock.Mock(name='MockEntCert') mock_ent_cert.products = [stub_product] brand_installer = self.brand_installer_class([mock_ent_cert]) brand_installer.install() self.assertTrue(self.mock_install.called) call_args = self.mock_install.call_args brand_arg = call_args[0][0] self.assertTrue(isinstance(brand_arg, entbranding.ProductBrand)) self.assertTrue(isinstance(brand_arg, rhelentbranding.RHELProductBrand)) self.assertEqual("Awesome OS super", brand_arg.name) # verify the install on all the installers got called count = 0 for bi in brand_installer.brand_installers: if isinstance(bi, mock.Mock): self.assertTrue(bi.install.called) count = count + 1 self.assertEqual(2, count)
def test_glance(self, glance): context = mock.NonCallableMock(session=mock.sentinel.session) res = clients.glance(context) self.assertEqual(glance.return_value, res) glance.assert_called_with('1', service_type='image', session=mock.sentinel.session)
def test_get_brand_branded_unknown_brand_type(self): stub_installed_product = StubProduct(id=123, name="Stub Product Name") # note, no 'brand_type' attribute other_stub_installed_product = StubProduct( id=321, name='A Non Branded Product') mock_product_dir = mock.NonCallableMock() mock_product_dir.get_installed_products.return_value = [ stub_installed_product.id, other_stub_installed_product.id ] inj.provide(inj.PROD_DIR, mock_product_dir) stub_product = StubProduct(id=123, brand_type="middleware", name="Stub Product Name", brand_name="Awesome Middleware") mock_ent_cert = mock.Mock() mock_ent_cert.products = [stub_product] ent_certs = [mock_ent_cert] # NOTE: this looks like a branded product, except the brand type is one # the RHELBrandPicker doesn't know brand_picker = rhelentbranding.RHELBrandPicker(ent_certs) brand = brand_picker.get_brand() self.assertTrue(brand is None)
def SetUp(self): self._SetDomainRef('www.example.com') self.launch_stage_changes = mock.NonCallableMock() self.StartObjectPatch( config_changes, 'SetLaunchStageAnnotationChange', return_value=self.launch_stage_changes)
def test_simple(tmpdir): ns = argparse.Namespace() ns.pushy = mock.Mock() conn = mock.NonCallableMock(name='PushyClient') ns.pushy.return_value = conn mock_compiled = collections.defaultdict(mock.Mock) conn.compile.return_value = mock.Mock(return_value=('Ubuntu', 'precise', 'cuttlefish')) fake_get_release = mock.Mock(return_value=('Ubuntu', 'precise', 'cuttlefish')) fake_distro = mock.Mock(name='FakeDistro') fake_distro.return_value = fake_distro try: with directory(str(tmpdir)): with mock.patch('ceph_deploy.hosts.lsb.get_lsb_release', fake_get_release): with mock.patch('ceph_deploy.hosts.pushy', ns.pushy): with mock.patch('ceph_deploy.hosts._get_distro', fake_distro): main( args=['-v', 'install', 'storehost1'], namespace=ns, ) except SystemExit as e: raise AssertionError('Unexpected exit: %s', e) connect_calls = ns.pushy.connect.call_args[0][0] assert connect_calls == 'ssh+sudo:storehost1' assert fake_distro.name == 'Ubuntu' assert fake_distro.release == 'precise' assert fake_distro.codename == 'cuttlefish'
def test_should_use_url_from_config_if_catalog_config_missing(self): keystone.Keystone.instance = None with mock.patch('keystoneauth1.identity.Password') as password, \ mock.patch('keystoneauth1.session.Session') as session, \ mock.patch('keystoneclient.discover.Discover') as discover: client = mock.Mock() discover.return_value = d = mock.Mock() d.create_client = mock.Mock(return_value=client) monasca_url = mock.NonCallableMock() config = base_config.get_config('Api') config.update({ 'url': monasca_url, 'service_type': None, 'endpoint_type': None, 'region_name': None }) k = keystone.Keystone(config) k.get_monasca_url() password.assert_not_called() session.assert_not_called() discover.assert_not_called() client.auth_ref.service_catalog.url_for.assert_not_called()
def _inject_mock_invalid_consumer(self, uuid=None): invalid_identity = mock.NonCallableMock(name='InvalidIdentityMock') invalid_identity.is_valid = mock.Mock(return_value=False) invalid_identity.uuid = uuid or "INVALIDCONSUMERUUID" invalid_identity.cert_dir_path = "/not/a/real/path/to/pki/consumer/" inj.provide(inj.IDENTITY, invalid_identity) return invalid_identity
def test_filter_using_action(self): qs = mock.NonCallableMock(spec=[]) action = mock.Mock(spec=['filter']) f = Filter(action=action) result = f.filter(qs, 'value') action.assert_called_once_with(qs, 'value') self.assertNotEqual(qs, result)
def test_missing_shutdown_not_excess(self): size = testutil.MockSize(1) cloud_nodes = [ testutil.cloud_node_mock(1), testutil.cloud_node_mock(2) ] self.make_daemon(cloud_nodes=cloud_nodes, arvados_nodes=[ testutil.arvados_node_mock(1), testutil.arvados_node_mock( 2, last_ping_at='1970-01-01T01:02:03.04050607Z') ], want_sizes=[size]) get_cloud_node = mock.MagicMock(name="get_cloud_node") get_cloud_node.get.return_value = cloud_nodes[1] mock_node_monitor = mock.MagicMock() mock_node_monitor.proxy.return_value = mock.NonCallableMock( cloud_node=get_cloud_node) mock_shutdown = self.node_shutdown.start( node_monitor=mock_node_monitor) self.daemon.shutdowns.get()[cloud_nodes[1].id] = mock_shutdown.proxy() self.daemon.sizes_booting_shutdown.get()[cloud_nodes[1].id] = size self.assertEqual(2, self.alive_monitor_count()) for mon_ref in self.monitor_list(): self.daemon.node_can_shutdown(mon_ref.proxy()).get(self.TIMEOUT) self.assertEqual(1, self.node_shutdown.start.call_count)
def test_cinder(self, cinder): # test normal flow context = mock.NonCallableMock(session=mock.sentinel.session) res = clients.cinder(context) self.assertEqual(cinder.return_value, res) cinder.assert_called_with('2', service_type='volumev2', session=mock.sentinel.session)
def test_close(): mock_transport = mock.NonCallableMock() client = subscriber.Client(transport=mock_transport) client.close() mock_transport.channel.close.assert_called()
def _CreateErrorProcessingMock(self, method_exceptions=None, legacy_test=False): if legacy_test: test_class = legacy_page_test.LegacyPageTest else: test_class = story_test.StoryTest root_mock = mock.NonCallableMock( story=mock.NonCallableMagicMock(story_module.Story), results=mock.NonCallableMagicMock( page_test_results.PageTestResults), test=mock.NonCallableMagicMock(test_class), state=mock.NonCallableMagicMock( story_module.SharedState, CanRunStory=mock.Mock(return_value=True))) if method_exceptions: root_mock.configure_mock( **{ path + '.side_effect': exception for path, exception in method_exceptions.iteritems() }) return root_mock