def test_typical_run(self): m_parse_args = self.patch('pelicanizer.clargs.parse_args') m_init = self.patch('pelicanizer.log.init') m_WebServer = self.patch('pelicanizer.web.server.WebServer') m_reactor = self.make_mock() result = main(sentinel.args, m_reactor) self.assertIs(result, None) self.assert_calls_equal( m_parse_args, [call(main.__doc__, sentinel.args)]) self.assert_calls_equal( m_init, [call()]) self.assert_calls_equal( m_WebServer, [call(m_reactor), call().listen(m_parse_args().port)]) self.assert_calls_equal( m_reactor, [call.run()])
def test_typical_run(self): m_parse_args = self.patch('dockomorph.clargs.parse_args') m_parse_config = self.patch('dockomorph.config.parse_config') m_init = self.patch('dockomorph.log.init') m_GitsDir = self.patch('dockomorph.paths.GitsDir') m_cols = self.patch('dockomorph.secrets.create_or_load_secret') m_WebServer = self.patch('dockomorph.web.server.WebServer') m_reactor = self.make_mock() m_parse_config.return_value = { 'github': { 'secret': sentinel.GH_SECRET, }, } result = main(sentinel.args, m_reactor) self.assertIs(result, None) self.assert_calls_equal(m_parse_args, [call(main.__doc__, sentinel.args)]) self.assert_calls_equal(m_init, [call()]) self.assert_calls_equal(m_GitsDir, [call.makedirs(ignoreExistingDirectory=True)]) self.assert_calls_equal(m_cols, [call('github')]) self.assert_calls_equal(m_WebServer, [ call(m_reactor, m_cols.return_value, ANY), call().listen(m_parse_args().port) ]) self.assert_calls_equal(m_reactor, [call.run()])
def test_main(self): """ test main function """ def se_config_get(name): x = { 'api_port': 8088, 'verbose': 0, 'mongo_host': 'mhost', 'mongo_port': 1234 } return x.get(name, None) mock_args = Mock(verbose=0, show_config=False) with patch('%s.logger' % pbm, autospec=True) as mocklogger: with patch.multiple( pbm, autospec=True, APIServer=DEFAULT, Site=DEFAULT, reactor=DEFAULT, PythonLoggingObserver=DEFAULT, parse_args=DEFAULT, show_config=DEFAULT, Config=DEFAULT, set_log_info=DEFAULT, set_log_debug=DEFAULT, connect_mongodb=DEFAULT, ) as mocks: mocks['Config'].return_value.get.side_effect = se_config_get mocks['parse_args'].return_value = mock_args mocks['connect_mongodb'].return_value = self.mock_mongo with patch.object(sys, 'argv', ['bad', 'foo', 'bar']): main() assert mocks['show_config'].mock_calls == [] assert mocks['parse_args'].mock_calls == [call(['foo', 'bar'])] assert mocks['connect_mongodb'].mock_calls == [call('mhost', 1234)] assert mocks['APIServer'].mock_calls == [ call(self.mock_mongo), call().app.resource() ] site_app_res = mocks[ 'APIServer'].return_value.app.resource.return_value assert mocks['Site'].mock_calls == [call(site_app_res)] assert mocks['reactor'].mock_calls == [ call.listenTCP(8088, mocks['Site'].return_value), call.run() ] assert mocks['PythonLoggingObserver'].mock_calls == [ call(), call().start() ] assert mocklogger.mock_calls == [ call.debug('instantiating apiserver'), call.debug("reactor.listenTCP"), call.debug("reactor.run() - listening on port %d", 8088), call.debug("reactor.run() returned") ] assert mocks['set_log_info'].mock_calls == [] assert mocks['set_log_debug'].mock_calls == []
def _test_flaky_plugin_handles_success(self, current_passes=0, current_runs=0, is_test_method=True, max_runs=2, min_passes=1): self._expect_test_flaky(is_test_method, max_runs, min_passes) self._set_flaky_attribute(is_test_method, FlakyNames.CURRENT_PASSES, current_passes) self._set_flaky_attribute(is_test_method, FlakyNames.CURRENT_RUNS, current_runs) too_few_passes = current_passes + 1 < min_passes retries_remaining = current_runs + 1 < max_runs expected_plugin_handles_success = too_few_passes and retries_remaining self._flaky_plugin.prepareTestCase(self._mock_test_case) actual_plugin_handles_success = self._flaky_plugin.addSuccess( self._mock_test_case) self.assertEqual( expected_plugin_handles_success, actual_plugin_handles_success, 'Expected plugin{} to handle the test run, but it did{}.'.format( ' to' if expected_plugin_handles_success else '', '' if actual_plugin_handles_success else ' not')) self._assert_flaky_attributes_contains({ FlakyNames.CURRENT_PASSES: current_passes + 1, FlakyNames.CURRENT_RUNS: current_runs + 1, }) expected_test_case_calls = [call.address(), call.address()] expected_stream_calls = [ call.writelines([ self._mock_test_method_name, " passed {} out of the required {} times. ".format( current_passes + 1, min_passes, ), ]) ] if expected_plugin_handles_success: expected_test_case_calls.append(call.run(self._mock_test_result)) expected_stream_calls.append( call.write( 'Running test again until it passes {} times.\n'.format( min_passes, ), ), ) else: expected_stream_calls.append(call.write('Success!\n')) self.assertEqual( self._mock_test_case.mock_calls, expected_test_case_calls, 'Unexpected TestCase calls = {} vs {}'.format( self._mock_test_case.mock_calls, expected_test_case_calls, ), ) self.assertEqual(self._mock_stream.mock_calls, expected_stream_calls)
def test__run_handling(self, LOGGER_mock): mock = Mock( __class__=BaseOneShotCollector, input_data=dict(a=SAMPLE_ARG_A, bb=SAMPLE_ARG_B), _output_components=None, get_output_components=Mock(return_value=SAMPLE_OUTPUT_COMPONENTS)) # the call BaseOneShotCollector.run_handling(mock) # assertions self.assertEqual(mock.mock_calls, [ call.get_output_components(a=SAMPLE_ARG_A, bb=SAMPLE_ARG_B), call.run() ]) self.assertIs(mock._output_components, SAMPLE_OUTPUT_COMPONENTS)
def install_test(self, remote_cls, installation_cls, transaction_cls): """Test flatpak installation is working.""" flatpak = FlatpakPayload("remote/path") self._setup_flatpak_objects(remote_cls, installation_cls, transaction_cls) flatpak.initialize_with_system_path() mock_ref_list = [ RefMock(name="org.space.coolapp", kind=RefKind.APP, arch="x86_64", branch="stable"), RefMock(name="com.prop.notcoolapp", kind=RefKind.APP, arch="i386", branch="f36"), RefMock(name="org.space.coolruntime", kind=RefKind.RUNTIME, arch="x86_64", branch="stable"), RefMock(name="com.prop.notcoolruntime", kind=RefKind.RUNTIME, arch="i386", branch="f36") ] self._installation.list_remote_refs_sync.return_value = mock_ref_list flatpak.install_all() expected_calls = [ call.connect("new_operation", flatpak._operation_started_callback), call.connect("operation_done", flatpak._operation_stopped_callback), call.connect("operation_error", flatpak._operation_error_callback), call.add_install(FlatpakPayload.LOCAL_REMOTE_NAME, mock_ref_list[0].format_ref(), None), call.add_install(FlatpakPayload.LOCAL_REMOTE_NAME, mock_ref_list[1].format_ref(), None), call.add_install(FlatpakPayload.LOCAL_REMOTE_NAME, mock_ref_list[2].format_ref(), None), call.add_install(FlatpakPayload.LOCAL_REMOTE_NAME, mock_ref_list[3].format_ref(), None), call.run() ] self.assertEqual(self._transaction.mock_calls, expected_calls)
def test_typical_run(self): m_parse_args = self.patch('dockomorph.clargs.parse_args') m_parse_config = self.patch('dockomorph.config.parse_config') m_init = self.patch('dockomorph.log.init') m_GitsDir = self.patch('dockomorph.paths.GitsDir') m_cols = self.patch('dockomorph.secrets.create_or_load_secret') m_WebServer = self.patch('dockomorph.web.server.WebServer') m_reactor = self.make_mock() m_parse_config.return_value = { 'github': { 'secret': sentinel.GH_SECRET, }, } result = main(sentinel.args, m_reactor) self.assertIs(result, None) self.assert_calls_equal( m_parse_args, [call(main.__doc__, sentinel.args)]) self.assert_calls_equal( m_init, [call()]) self.assert_calls_equal( m_GitsDir, [call.makedirs(ignoreExistingDirectory=True)]) self.assert_calls_equal( m_cols, [call('github')]) self.assert_calls_equal( m_WebServer, [call(m_reactor, m_cols.return_value, ANY), call().listen(m_parse_args().port)]) self.assert_calls_equal( m_reactor, [call.run()])
def test_main_no_args(self): websecret = 'abc' webport = 8080 irchost = 'irc.oftc.net' ircport = 6697 nick = 'leastbot' password = '******' nickserv = 'nickserv' channel = '#leastbot-test' m_config = self.m_config_load.return_value m_config.secret.irc.password = password m_config.secret.web.githubsecret = websecret m_config.public.irc.host = irchost m_config.public.irc.port = ircport m_config.public.irc.nick = nick m_config.public.irc.nickserv = nickserv m_config.public.irc.channel = channel m_config.public.web.port = webport main(args=[], reactor=self.m_reactor) self.assert_calls_equal( self.m_config_load, [call(ArgIsTypeWithAttrs(FilePath, path=os.path.expanduser('~/.leastbot')))]) self.assert_calls_equal( self.m_Client, [call(self.m_reactor, irchost, ircport, nick, password, nickserv, channel), call().connect()]) self.assert_calls_equal( self.m_WebServer, [call(self.m_reactor, webport, websecret, self.m_Client.return_value.handle_github_notification), call().listen()]) self.assert_calls_equal( self.m_reactor, [call.run()])
def _test_flaky_plugin_handles_success( self, current_passes=0, current_runs=0, is_test_method=True, max_runs=2, min_passes=1 ): self._expect_test_flaky(is_test_method, max_runs, min_passes) self._set_flaky_attribute( is_test_method, FlakyNames.CURRENT_PASSES, current_passes ) self._set_flaky_attribute( is_test_method, FlakyNames.CURRENT_RUNS, current_runs ) too_few_passes = current_passes + 1 < min_passes retries_remaining = current_runs + 1 < max_runs expected_plugin_handles_success = too_few_passes and retries_remaining self._flaky_plugin.prepareTestCase(self._mock_test_case) actual_plugin_handles_success = self._flaky_plugin.addSuccess( self._mock_test_case ) self.assertEqual( expected_plugin_handles_success, actual_plugin_handles_success, 'Expected plugin{} to handle the test run, but it did{}.'.format( ' to' if expected_plugin_handles_success else '', '' if actual_plugin_handles_success else ' not' ) ) self._assert_flaky_attributes_contains( { FlakyNames.CURRENT_PASSES: current_passes + 1, FlakyNames.CURRENT_RUNS: current_runs + 1, } ) expected_test_case_calls = [call.address(), call.address()] expected_stream_calls = [call.writelines([ self._mock_test_method_name, " passed {} out of the required {} times. ".format( current_passes + 1, min_passes, ), ])] if expected_plugin_handles_success: expected_test_case_calls.append(call.run(self._mock_test_result)) expected_stream_calls.append( call.write( 'Running test again until it passes {} times.\n'.format( min_passes, ), ), ) else: expected_stream_calls.append(call.write('Success!\n')) self.assertEqual( self._mock_test_case.mock_calls, expected_test_case_calls, 'Unexpected TestCase calls = {} vs {}'.format( self._mock_test_case.mock_calls, expected_test_case_calls, ), ) self.assertEqual(self._mock_stream.mock_calls, expected_stream_calls)
def _test_flaky_plugin_handles_failure_or_error( self, current_errors=None, current_passes=0, current_runs=0, is_failure=False, is_test_method=True, max_runs=2, min_passes=1, ): self._expect_test_flaky(is_test_method, max_runs, min_passes) if current_errors is None: current_errors = [self._mock_error] else: current_errors.append(self._mock_error) self._set_flaky_attribute( is_test_method, FlakyNames.CURRENT_ERRORS, current_errors ) self._set_flaky_attribute( is_test_method, FlakyNames.CURRENT_PASSES, current_passes ) self._set_flaky_attribute( is_test_method, FlakyNames.CURRENT_RUNS, current_runs ) too_few_passes = current_passes < min_passes retries_remaining = current_runs + 1 < max_runs expected_plugin_handles_failure = too_few_passes and retries_remaining self._flaky_plugin.prepareTestCase(self._mock_test_case) if is_failure: actual_plugin_handles_failure = self._flaky_plugin.handleFailure( self._mock_test_case, self._mock_error ) else: actual_plugin_handles_failure = self._flaky_plugin.handleError( self._mock_test_case, self._mock_error ) self.assertEqual( expected_plugin_handles_failure, actual_plugin_handles_failure, 'Expected plugin{} to handle the test run, but it did{}.'.format( ' to' if expected_plugin_handles_failure else '', '' if actual_plugin_handles_failure else ' not' ) ) self._assert_flaky_attributes_contains( { FlakyNames.CURRENT_RUNS: current_runs + 1, FlakyNames.CURRENT_ERRORS: current_errors } ) expected_test_case_calls = [call.address(), call.address()] if expected_plugin_handles_failure: expected_test_case_calls.append(call.run(self._mock_test_result)) expected_stream_calls = [call.writelines([ self._mock_test_method_name, ' failed ({} runs remaining out of {}).'.format( max_runs - current_runs - 1, max_runs ), '\n\t', unicode(self._mock_error[0]), '\n\t', unicode(self._mock_error[1].message), '\n\t', unicode(self._mock_error[2]), '\n' ])] else: expected_stream_calls = [call.writelines([ self._mock_test_method_name, ' failed; it passed {} out of the required {} times.'.format( current_passes, min_passes ), '\n\t', unicode(self._mock_error[0]), '\n\t', unicode(self._mock_error[1].message), '\n\t', unicode(self._mock_error[2]), '\n' ])] self.assertEqual( self._mock_test_case.mock_calls, expected_test_case_calls, 'Unexpected TestCase calls: {} vs {}'.format( self._mock_test_case.mock_calls, expected_test_case_calls ) ) self.assertEqual(self._mock_stream.mock_calls, expected_stream_calls)
def _test_flaky_plugin_handles_failure_or_error( self, current_errors=None, current_passes=0, current_runs=0, is_failure=False, is_test_method=True, max_runs=2, min_passes=1, ): self._expect_test_flaky(is_test_method, max_runs, min_passes) if current_errors is None: current_errors = [self._mock_error] else: current_errors.append(self._mock_error) self._set_flaky_attribute(is_test_method, FlakyNames.CURRENT_ERRORS, current_errors) self._set_flaky_attribute(is_test_method, FlakyNames.CURRENT_PASSES, current_passes) self._set_flaky_attribute(is_test_method, FlakyNames.CURRENT_RUNS, current_runs) too_few_passes = current_passes < min_passes retries_remaining = current_runs + 1 < max_runs expected_plugin_handles_failure = too_few_passes and retries_remaining self._flaky_plugin.prepareTestCase(self._mock_test_case) if is_failure: actual_plugin_handles_failure = self._flaky_plugin.handleFailure( self._mock_test_case, self._mock_error) else: actual_plugin_handles_failure = self._flaky_plugin.handleError( self._mock_test_case, self._mock_error) self.assertEqual( expected_plugin_handles_failure, actual_plugin_handles_failure, 'Expected plugin{} to handle the test run, but it did{}.'.format( ' to' if expected_plugin_handles_failure else '', '' if actual_plugin_handles_failure else ' not')) self._assert_flaky_attributes_contains({ FlakyNames.CURRENT_RUNS: current_runs + 1, FlakyNames.CURRENT_ERRORS: current_errors }) expected_test_case_calls = [call.address(), call.address()] if expected_plugin_handles_failure: expected_test_case_calls.append(call.run(self._mock_test_result)) expected_stream_calls = [ call.writelines([ self._mock_test_method_name, ' failed ({} runs remaining out of {}).'.format( max_runs - current_runs - 1, max_runs), '\n\t', unicode(self._mock_error[0]), '\n\t', unicode(self._mock_error[1].message), '\n\t', unicode(self._mock_error[2]), '\n' ]) ] else: expected_stream_calls = [ call.writelines([ self._mock_test_method_name, ' failed; it passed {} out of the required {} times.'. format(current_passes, min_passes), '\n\t', unicode(self._mock_error[0]), '\n\t', unicode(self._mock_error[1].message), '\n\t', unicode(self._mock_error[2]), '\n' ]) ] self.assertEqual( self._mock_test_case.mock_calls, expected_test_case_calls, 'Unexpected TestCase calls: {} vs {}'.format( self._mock_test_case.mock_calls, expected_test_case_calls)) self.assertEqual(self._mock_stream.mock_calls, expected_stream_calls)
def test_main(self): """ test main function """ def se_config_get(name): x = { 'api_port': 8088, 'verbose': 0, 'mongo_host': 'mhost', 'mongo_port': 1234 } return x.get(name, None) mock_args = Mock(verbose=0, show_config=False) with patch('%s.logger' % pbm, autospec=True) as mocklogger: with patch.multiple( pbm, autospec=True, APIServer=DEFAULT, Site=DEFAULT, reactor=DEFAULT, PythonLoggingObserver=DEFAULT, parse_args=DEFAULT, show_config=DEFAULT, Config=DEFAULT, set_log_info=DEFAULT, set_log_debug=DEFAULT, connect_mongodb=DEFAULT, ) as mocks: mocks['Config'].return_value.get.side_effect = se_config_get mocks['parse_args'].return_value = mock_args mocks['connect_mongodb'].return_value = self.mock_mongo with patch.object(sys, 'argv', ['bad', 'foo', 'bar']): main() assert mocks['show_config'].mock_calls == [] assert mocks['parse_args'].mock_calls == [call(['foo', 'bar'])] assert mocks['connect_mongodb'].mock_calls == [call('mhost', 1234)] assert mocks['APIServer'].mock_calls == [ call(self.mock_mongo), call().app.resource() ] site_app_res = mocks[ 'APIServer'].return_value.app.resource.return_value assert mocks['Site'].mock_calls == [ call(site_app_res) ] assert mocks['reactor'].mock_calls == [ call.listenTCP(8088, mocks['Site'].return_value), call.run() ] assert mocks['PythonLoggingObserver'].mock_calls == [ call(), call().start() ] assert mocklogger.mock_calls == [ call.debug('instantiating apiserver'), call.debug("reactor.listenTCP"), call.debug("reactor.run() - listening on port %d", 8088), call.debug("reactor.run() returned") ] assert mocks['set_log_info'].mock_calls == [] assert mocks['set_log_debug'].mock_calls == []
def test_run_reactor(self): self.cls.reactor = Mock(spec_set=reactor) self.cls.run_reactor() assert self.cls.reactor.mock_calls == [call.run()]