class WhenPicklingDingus: def setup(self): self.dingus = Dingus("something") # interact before pickling self.dingus('arg', kwarg=None) self.dingus.child.function_with_return_value.return_value = 'RETURN' self.dingus.child('arg', kwarg=None) self.dump_str = pickle.dumps(self.dingus, pickle.HIGHEST_PROTOCOL) del self.dingus self.unpickled_dingus = pickle.loads(self.dump_str) def should_remember_name(self): assert self.unpickled_dingus.__name__ == 'something' def should_remember_called_functions(self): assert self.unpickled_dingus.calls('()').one().args == ('arg',) def should_remember_child_calls(self): assert self.unpickled_dingus.calls("child").one().args == ('arg',) def should_remember_child_functions_return_value(self): assert self.unpickled_dingus.child.function_with_return_value() == 'RETURN' def should_have_replaced_init(self): assert self.unpickled_dingus.__init__ == self.unpickled_dingus._fake_init assert self.unpickled_dingus.child.__init__ == self.unpickled_dingus.child._fake_init
def test_composer_tool_install_latest(self): ctx = utils.FormattedDict({ 'DOWNLOAD_URL': 'http://server/bins', 'PHP_VM': 'will_default_to_php_strategy', 'BUILD_DIR': '/build/dir', 'CACHE_DIR': '/cache/dir', 'COMPOSER_VERSION': 'latest', 'BP_DIR': '' }) builder = Dingus(_ctx=ctx) installer = Dingus() cfInstaller = Dingus() builder.install = Dingus(_installer=cfInstaller, return_value=installer) ct = self.extension_module.ComposerExtension(ctx) ct._builder = builder ct.install() eq_(2, len(builder.install.calls())) # make sure PHP is installed assert installer.package.calls().once() eq_('PHP', installer.package.calls()[0].args[0]) call = installer.package.calls()[0] assert call.return_value.calls().once() assert installer.calls().once() # make sure composer is installed assert installer._installer.calls().once() assert installer._installer.calls()[0].args[0] == \ 'https://getcomposer.org/composer.phar', \ "was %s" % installer._installer.calls()[0].args[0]
class WhenModifyingAnExistingResource(object): def setup(self): self.web_client = Dingus() self.web_client.request.return_value = ['application/json', {'foo': [1, 2]}] root = Resource.bookmark('/foo', self.web_client) root['foo'].append(3) root.put() def should_do_a_get_request_to_retrieve_original_version(self): assert self.web_client.calls('request', 'GET', '/foo', False, None).one() def should_do_a_put_request_to_modify_the_resource(self): assert self.web_client.calls('request', 'PUT', '/foo', DontCare, DontCare).one() def should_send_modified_representation(self): assert self.web_client.calls('request', DontCare, DontCare, DontCare, {'foo': [1, 2, 3]}).one() def should_not_make_any_other_requests(self): assert len(self.web_client.calls('request')) == 2
def test_github_download_rate_is_exceeded(self): # noqa ctx = utils.FormattedDict({ 'BUILD_DIR': tempfile.gettempdir(), 'PHP_VM': 'php', 'TMPDIR': tempfile.gettempdir(), 'LIBDIR': 'lib', 'CACHE_DIR': 'cache', 'WEBDIR': '' }) instance_stub = Dingus() instance_stub._set_return_value("""{"rate": {"limit": 60, "remaining": 0}}""") stream_output_stub = Dingus( 'test_github_oauth_token_uses_curl : stream_output') with patches({ 'StringIO.StringIO.getvalue': instance_stub, 'composer.extension.stream_output': stream_output_stub, }): ct = self.extension_module.ComposerExtension(ctx) result = ct._github_rate_exceeded(False) assert result is True, \ '_github_oauth_token_is_valid returned %s, expected True' % result
def test_composer_tool_run_custom_composer_opts(self): ctx = utils.FormattedDict({ 'DOWNLOAD_URL': 'http://server/bins', 'CACHE_HASH_ALGORITHM': 'sha1', 'BUILD_DIR': '/build/dir', 'CACHE_DIR': '/cache/dir', 'TMPDIR': tempfile.gettempdir(), 'COMPOSER_INSTALL_OPTIONS': ['--optimize-autoloader'] }) builder = Dingus(_ctx=ctx) old_check_output = self.ct.check_output co = Dingus() self.ct.check_output = co try: ct = self.ct.ComposerTool(builder) ct.run() eq_(2, len(builder.move.calls())) assert co.calls().once() instCmd = co.calls()[0].args[0] assert instCmd.find('install') > 0 assert instCmd.find('--no-progress') > 0 assert instCmd.find('--no-interaction') == -1 assert instCmd.find('--no-dev') == -1 assert instCmd.find('--optimize-autoloader') > 0 finally: self.ct.check_output = old_check_output
def test_github_oauth_token_is_valid_interprets_github_api_401_as_false(self): # noqa ctx = utils.FormattedDict({ 'BUILD_DIR': tempfile.gettempdir(), 'PHP_VM': 'php', 'TMPDIR': tempfile.gettempdir(), 'LIBDIR': 'lib', 'CACHE_DIR': 'cache', 'WEBDIR': '' }) instance_stub = Dingus() instance_stub._set_return_value("""{}""") stream_output_stub = Dingus( 'test_github_oauth_token_uses_curl : stream_output') with patches({ 'StringIO.StringIO.getvalue': instance_stub, 'composer.extension.stream_output': stream_output_stub, }): ct = self.extension_module.ComposerExtension(ctx) result = ct._github_oauth_token_is_valid('MADE_UP_TOKEN_VALUE') assert result is False, \ '_github_oauth_token_is_valid returned %s, expected False' % result
def test_composer_tool_run_sanity_checks(self): ctx = utils.FormattedDict({ 'DOWNLOAD_URL': 'http://server/bins', 'CACHE_HASH_ALGORITHM': 'sha1', 'BUILD_DIR': '/build/dir', 'CACHE_DIR': '/cache/dir', 'TMPDIR': tempfile.gettempdir() }) builder = Dingus(_ctx=ctx) old_check_output = self.ct.check_output co = Dingus() self.ct.check_output = co try: ct = self.ct.ComposerTool(builder) ct._log = Dingus() ct.run() assert len(ct._log.warning.calls()) > 0 assert ct._log.warning.calls()[0].args[0].find('PROTIP:') == 0 exists = Dingus(return_value=True) with patch('os.path.exists', exists): ct._log = Dingus() ct.run() assert len(exists.calls()) == 1 assert len(ct._log.warning.calls()) == 0 finally: self.ct.check_output = old_check_output
def test_run_does_not_set_github_oauth_if_missing(self): ctx = utils.FormattedDict({ 'DOWNLOAD_URL': 'http://server/bins', 'CACHE_HASH_ALGORITHM': 'sha1', 'BUILD_DIR': '/usr/awesome', 'PHP_VM': 'php', 'TMPDIR': tempfile.gettempdir(), 'LIBDIR': 'lib', 'CACHE_DIR': 'cache', }) stream_output_stub = Dingus() old_stream_output = self.extension_module.stream_output self.extension_module.stream_output = stream_output_stub old_rewrite = self.extension_module.utils.rewrite_cfgs rewrite = Dingus() self.extension_module.utils.rewrite_cfgs = rewrite try: ct = self.extension_module.ComposerExtension(ctx) builder_stub = Dingus(_ctx=ctx) ct._builder = builder_stub ct.run() executed_command = stream_output_stub.calls()[0].args[1] finally: self.extension_module.stream_output = old_stream_output self.extension_module.utils.rewrite_cfgs = old_rewrite assert stream_output_stub.calls().once(), 'stream_output() was called more than once'
def test_composer_tool_install(self): ctx = utils.FormattedDict({ 'PHP_VM': 'will_default_to_php_strategy', 'BUILD_DIR': '/build/dir', 'CACHE_DIR': '/cache/dir', 'WEBDIR': '' }) builder = Dingus(_ctx=ctx) installer = Dingus() cfInstaller = Dingus() builder.install = Dingus(_installer=cfInstaller, return_value=installer) ct = self.extension_module.ComposerExtension(ctx) ct._builder = builder ct.install() eq_(2, len(builder.install.calls())) # make sure PHP is installed assert installer.package.calls().once() eq_('PHP', installer.package.calls()[0].args[0]) call = installer.package.calls()[0] assert call.return_value.calls().once() assert installer.calls().once() # make sure composer is installed assert installer._installer.calls().once() assert installer._installer.calls()[0].args[0] == \ '/composer/1.0.0-alpha10/composer.phar', \ "was %s" % installer._installer.calls()[0].args[0]
def test_composer_run_streams_output(self): ctx = utils.FormattedDict({ 'PHP_VM': 'hhvm', # PHP strategy does other stuff 'DOWNLOAD_URL': 'http://server/bins', 'CACHE_HASH_ALGORITHM': 'sha1', 'BUILD_DIR': '/build/dir', 'CACHE_DIR': '/cache/dir', 'TMPDIR': tempfile.gettempdir(), 'WEBDIR': 'htdocs', 'LIBDIR': 'lib' }) builder = Dingus(_ctx=ctx) # patch stream_output method old_stream_output = self.extension_module.stream_output co = Dingus() self.extension_module.stream_output = co try: ct = self.extension_module.ComposerExtension(ctx) ct._builder = builder ct.run() assert co.calls().once() instCmd = co.calls()[0].args[1] assert instCmd.find('/build/dir/php/bin/composer.phar') > 0 assert instCmd.find('install') > 0 assert instCmd.find('--no-progress') > 0 assert instCmd.find('--no-interaction') > 0 assert instCmd.find('--no-dev') > 0 finally: self.extension_module.stream_output = old_stream_output
def test_provision(self): panel = Dingus("Panel") result = mule_setup(panel, 1) self.assertEquals(result, {"status": "fail", "reason": "worker is already in use"}) # Ensure we're now in the default queue queue = Dingus("Queue") queue.name = conf.DEFAULT_QUEUE panel.consumer.task_consumer.queues = [queue] result = mule_setup(panel, 1) self.assertTrue("build_id" in result) self.assertEquals(result["build_id"], 1) self.assertTrue("status" in result) self.assertEquals(result["status"], "ok") calls = dingus_calls_to_dict(panel.consumer.task_consumer.calls) self.assertTrue("cancel_by_queue" in calls) self.assertTrue(len(calls["cancel_by_queue"]), 1) call = calls["cancel_by_queue"][0] self.assertTrue(len(call[0]), 1) self.assertTrue(call[0][0], conf.DEFAULT_QUEUE) self.assertTrue("consume" in calls) self.assertTrue(len(calls["consume"]), 1) self.assertTrue("add_consumer_from_dict" in calls) self.assertTrue(len(calls["add_consumer_from_dict"]), 1) call = calls["add_consumer_from_dict"][0] self.assertTrue("queue" in call[1]) self.assertEquals(call[1]["queue"], "%s-1" % conf.BUILD_QUEUE_PREFIX)
def test_build_composer_environment_inherits_from_ctx(self): ctx = utils.FormattedDict( { "BUILD_DIR": "/usr/awesome", "WEBDIR": "", "PHPRC": "/usr/awesome/phpini", "PHP_VM": "php", "TMPDIR": "tmp", "LIBDIR": "lib", "CACHE_DIR": "cache", "OUR_SPECIAL_KEY": "SPECIAL_VALUE", } ) environ_stub = Dingus() environ_stub._set_return_value(["OUR_SPECIAL_KEY"]) write_config_stub = Dingus() with patches( {"os.environ.keys": environ_stub, "composer.extension.PHPComposerStrategy.write_config": write_config_stub} ): self.extension_module.ComposerExtension(ctx) cr = self.extension_module.ComposerCommandRunner(ctx, None) built_environment = cr._build_composer_environment() assert "OUR_SPECIAL_KEY" in built_environment, "OUR_SPECIAL_KEY was not found in the built_environment variable" assert built_environment["OUR_SPECIAL_KEY"] == "SPECIAL_VALUE", ( '"OUR_SPECIAL_KEY" key in built_environment was %s; expected "SPECIAL_VALUE"' % built_environment["OUR_SPECIAL_KEY"] )
def test_composer_tool_install_latest(self): ctx = utils.FormattedDict( { "PHP_VM": "will_default_to_php_strategy", "BUILD_DIR": "/build/dir", "CACHE_DIR": "/cache/dir", "COMPOSER_VERSION": "latest", "BP_DIR": "", "WEBDIR": "", } ) builder = Dingus(_ctx=ctx) installer = Dingus() cfInstaller = Dingus() builder.install = Dingus(_installer=cfInstaller, return_value=installer) ct = self.extension_module.ComposerExtension(ctx) ct._builder = builder ct.install() eq_(2, len(builder.install.calls())) # make sure PHP is installed assert installer.package.calls().once() eq_("PHP", installer.package.calls()[0].args[0]) call = installer.package.calls()[0] assert call.return_value.calls().once() assert installer.calls().once() # make sure composer is installed assert installer._installer.calls().once() assert installer._installer.calls()[0].args[0] == "https://getcomposer.org/composer.phar", ( "was %s" % installer._installer.calls()[0].args[0] )
def test_build_composer_environment_inherits_from_ctx(self): ctx = utils.FormattedDict({ 'BUILD_DIR': '/usr/awesome', 'PHPRC': '/usr/awesome/phpini', 'PHP_VM': 'php', 'TMPDIR': 'tmp', 'LIBDIR': 'lib', 'CACHE_DIR': 'cache', 'OUR_SPECIAL_KEY': 'SPECIAL_VALUE' }) environ_stub = Dingus() environ_stub._set_return_value(['OUR_SPECIAL_KEY']) write_config_stub = Dingus() with patches({ 'os.environ.keys': environ_stub, 'composer.extension.PHPComposerStrategy.write_config': write_config_stub }): self.extension_module.ComposerExtension(ctx) cr = self.extension_module.ComposerCommandRunner(ctx, None) built_environment = cr._build_composer_environment() assert 'OUR_SPECIAL_KEY' in built_environment, \ 'OUR_SPECIAL_KEY was not found in the built_environment variable' assert built_environment['OUR_SPECIAL_KEY'] == 'SPECIAL_VALUE', \ '"OUR_SPECIAL_KEY" key in built_environment was %s; expected "SPECIAL_VALUE"' % built_environment['OUR_SPECIAL_KEY']
def test_composer_tool_install(self): ctx = utils.FormattedDict({ 'DOWNLOAD_URL': 'http://server/bins', 'CACHE_HASH_ALGORITHM': 'sha1', 'PHP_VM': 'will_default_to_php_strategy', 'BUILD_DIR': '/build/dir', 'CACHE_DIR': '/cache/dir' }) builder = Dingus(_ctx=ctx) installer = Dingus() cfInstaller = Dingus() builder.install = Dingus(_installer=cfInstaller, return_value=installer) ct = self.extension_module.ComposerExtension(ctx) ct._builder = builder ct.install() eq_(2, len(builder.install.calls())) # make sure PHP cli is installed assert installer.modules.calls().once() eq_('PHP', installer.modules.calls()[0].args[0]) call = installer.modules.calls()[0] assert call.return_value.calls().once() eq_('cli', call.return_value.calls()[0].args[0]) assert installer.calls().once() # make sure composer is installed assert installer._installer.calls().once() assert installer._installer.calls()[0].args[0] == \ 'http://server/bins/composer/1.0.0-alpha9/composer.phar', \ "was %s" % installer._installer.calls()[0].args[0]
def test_process_extensions(self): process = Dingus() ctx = {'EXTENSIONS': ['test/data/plugins/test1', 'test/data/plugins/test2']} utils.process_extensions(ctx, 'service_environment', process) eq_(2, len(process.calls())) eq_('1234', process.calls()[0].args[0]['TEST_ENV']) eq_('4321', process.calls()[1].args[0]['TEST_ENV'])
def test_proc_count(): dingus = Dingus() foo(dingus.func, True) assert len(dingus.calls('func')) == 2 dingus = Dingus() foo(dingus.func, False) assert len(dingus.calls('func')) == 1
def test_parser(): on_raw = Dingus() parser = Parser() parser.add_option('raw', on_raw) parser(['trash-list', '--raw']) assert on_raw.calls('()')
def should_not_put_into_queue(self): headers = {'persistent': 'true', 'bytes_message': 'true'} body = 'Testing' this_frame = self.frame.build_frame({'command': 'SEND', 'headers': headers, 'body': body}) this_frame = Dingus() self.queue.put(this_frame) ret_frame = self.queue.get(this_frame) assert this_frame.calls('parse_frame', nb=False)
def test_stop(self): config.set_config(StringIO(test_config)) ch = Dingus(is_attached=False) self.tsprotocol.transport.session.conn.transport.factory.consolecollection = \ Dingus(find_by_name__returns=ch) result = self.tsprotocol.process("stop /dev/ttyUSB0") self.failUnless(result is None) name, args, kwargs, result = self.tsprotocol.consolecollection.calls('find_by_name')[0] self.assertEqual('/dev/ttyUSB0', args[0]) #TODO make sure closed called on the self.assertEqual(1, len(ch.calls('close'))) name, args, kwargs, result = ch.calls('close')[0]
def test_composer_tool_install_latest(self): ctx = utils.FormattedDict({ 'PHP_VM': 'will_default_to_php_strategy', 'BUILD_DIR': '/build/dir', 'CACHE_DIR': '/cache/dir', 'COMPOSER_VERSION': 'latest', 'BP_DIR': '', 'WEBDIR': '' }) builder = Dingus(_ctx=ctx) installer = Dingus() cfInstaller = Dingus() builder.install = Dingus(_installer=cfInstaller, return_value=installer) ct = self.extension_module.ComposerExtension(ctx) ct._builder = builder ct.install() eq_(2, len(builder.install.calls())) # make sure PHP is installed assert installer.package.calls().once() eq_('PHP', installer.package.calls()[0].args[0]) call = installer.package.calls()[0] assert call.return_value.calls().once() assert installer.calls().once() # make sure composer is installed assert installer._installer.calls().once() assert installer._installer.calls()[0].args[0] == \ 'https://getcomposer.org/composer.phar', \ "was %s" % installer._installer.calls()[0].args[0]
def test_composer_tool_detect(self): ctx = utils.FormattedDict({ 'DOWNLOAD_URL': 'http://server/bins', 'BUILD_DIR': '/build/dir', 'CACHE_DIR': '/cache/dir' }) builder = Dingus(_ctx=ctx) listdir = Dingus(return_value=('composer.json',)) exists = Dingus(return_value=True) with patch('os.listdir', listdir): with patch('os.path.exists', exists): ct = self.ct.ComposerTool(builder) assert ct.detect() assert listdir.calls().once()
def test_composer_tool_run_sanity_checks(self): ctx = utils.FormattedDict({ 'DOWNLOAD_URL': 'http://server/bins', 'CACHE_HASH_ALGORITHM': 'sha1', 'BUILD_DIR': '/build/dir', 'CACHE_DIR': '/cache/dir', 'TMPDIR': tempfile.gettempdir() }) builder = Dingus(_ctx=ctx) # patch stream_output method old_stream_output = self.ct.stream_output co = Dingus() self.ct.stream_output = co # patch utils.rewrite_cfg method old_rewrite = self.ct.utils.rewrite_cfgs rewrite = Dingus() self.ct.utils.rewrite_cfgs = rewrite try: ct = self.ct.ComposerTool(builder) ct._log = Dingus() ct.run() assert len(ct._log.warning.calls()) > 0 assert ct._log.warning.calls()[0].args[0].find('PROTIP:') == 0 exists = Dingus(return_value=True) with patch('os.path.exists', exists): ct._log = Dingus() ct.run() assert len(exists.calls()) == 1 assert len(ct._log.warning.calls()) == 0 finally: self.ct.stream_output = old_stream_output self.ct.utils.rewrite_cfgs = old_rewrite
def test_compile(self): composer = Dingus() composer.return_value.detect.return_value = True builder = Dingus() old_composer_tool = self.ct.ComposerTool self.ct.ComposerTool = composer try: self.ct.compile(builder) assert composer.calls().once() assert composer.return_value.detect.calls().once() assert composer.return_value.install.calls().once() assert composer.return_value.run.calls().once() finally: self.ct.ComposerTool = old_composer_tool
def test_compile_detect_fails(self): composer = Dingus() composer.return_value.detect.return_value = False builder = Dingus() old_composer_tool = self.ct.ComposerTool self.ct.ComposerTool = composer try: self.ct.compile(builder) assert composer.calls().once() assert composer.return_value.detect.calls().once() eq_(0, len(composer.return_value.install.calls())) eq_(0, len(composer.return_value.run.calls())) finally: self.ct.ComposerTool = old_composer_tool
def test_github_oauth_token_is_valid_uses_curl(self): ctx = utils.FormattedDict({ 'BP_DIR': '', 'BUILD_DIR': '/usr/awesome', 'PHP_VM': 'php', 'TMPDIR': tempfile.gettempdir(), 'LIBDIR': 'lib', 'CACHE_DIR': 'cache', 'WEBDIR': '' }) instance_stub = Dingus() instance_stub._set_return_value("""{"resources": {}}""") stream_output_stub = Dingus( 'test_github_oauth_token_uses_curl : stream_output') with patches({ 'StringIO.StringIO.getvalue': instance_stub, 'composer.extension.stream_output': stream_output_stub, }): ct = self.extension_module.ComposerExtension(ctx) ct._github_oauth_token_is_valid('MADE_UP_TOKEN_VALUE') executed_command = stream_output_stub.calls()[0].args[1] assert stream_output_stub.calls().once(), \ 'stream_output() was called more than once' assert executed_command.find('curl') == 0, \ 'Curl was not called, executed_command was %s' % executed_command assert executed_command.find( '-H "Authorization: token MADE_UP_TOKEN_VALUE"') > 0, \ 'No token was passed to curl. Command was: %s' % executed_command assert executed_command.find('https://api.github.com/rate_limit') > 0,\ 'No URL was passed to curl. Command was: %s' % executed_command
def should_not_get_from_queue(self): headers = {'destination': '/queue/nose_test', 'persistent': 'true', 'bytes_message': 'true'} body = 'Testing' this_frame = self.frame.build_frame({'command': 'SEND', 'headers': headers, 'body': body}) this_frame = Dingus() extracted_frame = self.queue.get(this_frame) print this_frame.calls assert this_frame.calls('parse_frame', nb=False)
class WhenTypeIsSchemaOperator(object): def setup(self): self.value = Dingus() self.expected_type = Dingus() self.returned = is_field_of_expected_type( self.value, self.expected_type) def should_evaluate_value(self): assert self.expected_type.calls('evaluate', self.value) def should_return_evaluation(self): assert self.expected_type.calls('evaluate').once() assert self.returned == self.expected_type.evaluate()
def should_be_able_to_return_something(self): open = Dingus() open().__enter__().read.return_value = "some data" with open('foo') as h: data_that_was_read = h.read() assert data_that_was_read == "some data"
def test_teardown(self): panel = Dingus('Panel') result = mule_teardown(panel, 1) self.assertTrue('build_id' in result) self.assertEquals(result['build_id'], 1) self.assertTrue('status' in result) self.assertEquals(result['status'], 'ok') calls = dingus_calls_to_dict(panel.consumer.task_consumer.calls) self.assertTrue('cancel_by_queue' in calls) self.assertTrue(len(calls['cancel_by_queue']), 1) call = calls['cancel_by_queue'][0] self.assertTrue(len(call[0]), 1) self.assertTrue(call[0][0], '%s-1' % conf.BUILD_QUEUE_PREFIX) self.assertTrue('consume' in calls) self.assertTrue(len(calls['consume']), 1) self.assertTrue('add_consumer_from_dict' in calls) self.assertTrue(len(calls['add_consumer_from_dict']), 1) call = calls['add_consumer_from_dict'][0] self.assertTrue('queue' in call[1]) self.assertEquals(call[1]['queue'], conf.DEFAULT_QUEUE)
def setup(self): super(WhenInitializingPriorityQueue, self).setup() self.connection = Dingus('connection') self.priority = Dingus('priority') self.task = Dingus('task') self.priority_queue = PriorityQueue(self.connection, 'name')
def should_be_able_to_consume_multiple_exceptions(self): dingus = Dingus( consumed_context_manager_exceptions=(NameError, NotImplementedError)) self._raiser(NameError, dingus)() self._raiser(NotImplementedError, dingus)() assert_raises(KeyError, self._raiser(KeyError, dingus))
class WhenSettingItems: def setup(self): self.dingus = Dingus() self.item = Dingus() self.dingus['item'] = self.item def should_remember_item(self): assert self.dingus['item'] is self.item def should_remember_item_even_if_its_value_is_None(self): self.dingus['item'] = None assert self.dingus['item'] is None def should_log_access(self): assert self.dingus.calls('__setitem__', 'item', self.item).one() def should_not_return_items_as_attributes(self): assert self.dingus.item is not self.item def should_return_distinct_dinguses_for_different_items(self): assert self.dingus['item'] is not self.dingus['item2'] def should_accept_tuples_as_item_name(self): dingus = Dingus() dingus[('x', 'y')] = 'foo' assert dingus[('x', 'y')] == 'foo'
def setup(self): self.web_client = Dingus() self.web_client.request.return_value = ['application/json', {'foo': [1, 2]}] root = Resource.bookmark('/foo', self.web_client) root['foo'].append(3) root.put()
def should_record_call(self): for operator in self.operators: left, right = Dingus.many(2) operator(left, right) operator_name_without_mangling = operator.__name__.replace('_', '') magic_method_name = '__%s__' % operator_name_without_mangling yield assert_call_was_logged, left, magic_method_name, right
def test_build_composer_environment_forbids_overwriting_key_vars(self): ctx = utils.FormattedDict({ 'BP_DIR': '', 'BUILD_DIR': '/usr/awesome', 'WEBDIR': '', 'PHP_VM': 'php', 'TMPDIR': 'tmp', 'LIBDIR': 'lib', 'CACHE_DIR': 'cache', 'PHPRC': '/usr/awesome/phpini', }) write_config_stub = Dingus() with patches({ 'composer.extension.PHPComposerStrategy.write_config': write_config_stub }): self.extension_module.ComposerExtension(ctx) cr = self.extension_module.ComposerCommandRunner(ctx, None) built_environment = cr._build_composer_environment() eq_(built_environment['LD_LIBRARY_PATH'], '/usr/awesome/php/lib') eq_(built_environment['PHPRC'], 'tmp')
def test_build_composer_environment_converts_vars_to_str(self): ctx = utils.FormattedDict({ 'BP_DIR': '', 'BUILD_DIR': '/usr/awesome', 'WEBDIR': '', 'PHP_VM': 'php', 'TMPDIR': 'tmp', 'LIBDIR': 'lib', 'CACHE_DIR': 'cache', 'PHPRC': '/usr/awesome/phpini', 'MY_DICTIONARY': { 'KEY': 'VALUE' }, }) write_config_stub = Dingus() with patches({ 'composer.extension.PHPComposerStrategy.write_config': write_config_stub }): self.extension_module.ComposerExtension(ctx) cr = self.extension_module.ComposerCommandRunner(ctx, None) built_environment = cr._build_composer_environment() for key, val in built_environment.iteritems(): assert type(val) == str, \ "Expected [%s]:[%s] to be type `str`, but found type [%s]" % ( key, val, type(val))
def test_build_composer_environment_sets_composer_env_vars(self): ctx = utils.FormattedDict({ 'BP_DIR': '', 'BUILD_DIR': '/tmp/build', 'WEBDIR': '', 'CACHE_DIR': '/tmp/cache', 'LIBDIR': 'lib', 'TMPDIR': '/tmp', 'PHP_VM': 'php' }) write_config_stub = Dingus() with patches({ 'composer.extension.PHPComposerStrategy.write_config': write_config_stub }): self.extension_module.ComposerExtension(ctx) cr = self.extension_module.ComposerCommandRunner(ctx, None) built_environment = cr._build_composer_environment() assert 'COMPOSER_VENDOR_DIR' in built_environment, \ 'Expect to find COMPOSER_VENDOR_DIR in built_environment' assert 'COMPOSER_BIN_DIR' in built_environment, \ 'Expect to find COMPOSER_BIN_DIR in built_environment' assert 'COMPOSER_CACHE_DIR' in built_environment, \ 'Expect to find COMPOSER_CACHE_DIR in built_environment'
def test_compile(self): ctx = utils.FormattedDict({ 'BUILD_DIR': self.build_dir, 'PHP_VERSION': '5.4.33', 'VCAP_SERVICES': { "rediscloud": [{"credentials": {}, "label": "rediscloud"}], "codizy": [{"credentials": {}, "label": "codizy"}] } }) codizy = codizy_extn.CodizyExtension(ctx) codizy._setup_codizy = Dingus() inst = Dingus() codizy.compile(inst) eq_(1, len(inst.package.calls())) eq_('CODIZY', inst.package.calls()[0].args[0]) eq_(1, len(codizy._setup_codizy.calls()))
def test_build_composer_environment_existing_path(self): ctx = utils.FormattedDict({ 'BP_DIR': '', 'BUILD_DIR': '/usr/awesome', 'WEBDIR': '', 'PHP_VM': 'php', 'TMPDIR': 'tmp', 'LIBDIR': 'lib', 'CACHE_DIR': 'cache', 'PATH': '/bin:/usr/bin' }) write_config_stub = Dingus() with patches({ 'composer.extension.PHPComposerStrategy.write_config': write_config_stub }): self.extension_module.ComposerExtension(ctx) cr = self.extension_module.ComposerCommandRunner(ctx, None) built_environment = cr._build_composer_environment() assert 'PATH' in built_environment, "should have PATH set" assert built_environment['PATH'].endswith(":/usr/awesome/php/bin"), \ "PATH should contain path to PHP, found [%s]" \ % built_environment['PATH']
def test_configure_adds_redis_config_to_php_ini(self): ctx = json.load(open('tests/data/sessions/vcap_services_redis.json')) sessions = self.extension_module.SessionStoreConfig(ctx) sessions.load_config = Dingus() php_ini = Dingus() sessions._php_ini = php_ini sessions._php_ini_path = '/tmp/staged/app/php/etc/php.ini' sessions.compile(None) eq_(1, len(sessions.load_config.calls())) eq_(3, len(php_ini.update_lines.calls())) eq_(1, len(php_ini.save.calls())) eq_(4, len(php_ini.calls())) eq_('session.save_handler = redis', php_ini.update_lines.calls()[1].args[1]) eq_('session.save_path = "tcp://redis-host:45629?auth=redis-pass"', php_ini.update_lines.calls()[2].args[1])
def test_configure_adds_redis_extension(self): ctx = json.load(open('tests/data/sessions/vcap_services_redis.json')) ctx['PHP_EXTENSIONS'] = [] sessions = self.extension_module.SessionStoreConfig(ctx) sessions._php_ini = Dingus() sessions.configure() eq_(True, 'redis' in ctx['PHP_EXTENSIONS'])
def test_build_composer_environment_has_missing_key(self): os.environ['SOME_KEY'] = 'does not matter' ctx = utils.FormattedDict({ 'BP_DIR': '', 'BUILD_DIR': '/usr/awesome', 'WEBDIR': '', 'PHP_VM': 'php', 'TMPDIR': 'tmp', 'LIBDIR': 'lib', 'CACHE_DIR': 'cache', 'SOME_KEY': utils.wrap('{exact_match}') }) write_config_stub = Dingus() with patches({ 'composer.extension.PHPComposerStrategy.write_config': write_config_stub }): self.extension_module.ComposerExtension(ctx) cr = self.extension_module.ComposerCommandRunner(ctx, None) try: built_environment = cr._build_composer_environment() assert "{exact_match}" == built_environment['SOME_KEY'], \ "value should match" except KeyError, e: assert 'exact_match' != e.message, \ "Should not try to evaluate value [%s]" % e raise
def should_fail_to_get_headers(self): my_frame = Frame() my_frame._getline = Dingus() my_frame._getline.return_value = \ 'RECEIPTreceipt-id:ID:nose-receipt123' nose_tools.assert_raises(UnknownBrokerResponseError, my_frame.parse_frame)
def setup(self): web_client = Dingus() web_client.request.return_value = [ u'application/json', { u'foo': [1, 2, u'/bar'] } ] self.root = Resource.bookmark('/', web_client)
def setUp(self): request = Dingus('request') toolbar = DebugToolbar(request) DebugToolbarMiddleware.debug_toolbars[thread.get_ident()] = toolbar self.request = request self.toolbar = toolbar
class WhenCallingAttributeChild: def setup(self): self.parent = Dingus() self.child = self.parent.child self.child('arg', kwarg=None) def should_record_call_on_child(self): assert self.child.calls.one() def should_record_call_on_parent(self): assert self.parent.calls('child').one() def should_record_args(self): assert self.parent.calls('child').one().args == ('arg', ) def should_record_kwargs(self): assert self.parent.calls('child').one().kwargs == {'kwarg': None}
def test_run_does_not_set_github_oauth_if_missing(self): ctx = utils.FormattedDict({ 'DOWNLOAD_URL': 'http://server/bins', 'CACHE_HASH_ALGORITHM': 'sha1', 'BUILD_DIR': '/usr/awesome', 'PHP_VM': 'php', 'TMPDIR': tempfile.gettempdir(), 'LIBDIR': 'lib', 'CACHE_DIR': 'cache', 'BP_DIR': '' }) instance_stub = Dingus() instance_stub._set_return_value( """{"rate": {"limit": 60, "remaining": 60}}""") stream_output_stub = Dingus() rewrite_stub = Dingus() builder = Dingus(_ctx=ctx) setup_composer_github_token_stub = Dingus() with patches({ 'StringIO.StringIO.getvalue': instance_stub, 'composer.extension.stream_output': stream_output_stub, 'composer.extension.utils.rewrite_cfgs': rewrite_stub, 'composer.extension.ComposerExtension.setup_composer_github_token': setup_composer_github_token_stub }): ct = self.extension_module.ComposerExtension(ctx) ct._builder = builder ct.composer_runner = \ self.extension_module.ComposerCommandRunner(ctx, builder) ct.run() setup_composer_github_token_calls = setup_composer_github_token_stub.calls( ) assert 0 == len(setup_composer_github_token_calls), \ 'setup_composer_github_token() was called %s times, expected 0' % len(setup_composer_github_token_calls)
def test_composer_tool_run_custom_composer_opts(self): ctx = utils.FormattedDict({ 'PHP_VM': 'php', 'DOWNLOAD_URL': 'http://server/bins', 'CACHE_HASH_ALGORITHM': 'sha1', 'BUILD_DIR': '/build/dir', 'CACHE_DIR': '/cache/dir', 'TMPDIR': tempfile.gettempdir(), 'WEBDIR': 'htdocs', 'LIBDIR': 'lib', 'COMPOSER_INSTALL_OPTIONS': ['--optimize-autoloader'] }) builder = Dingus(_ctx=ctx) # patch stream_output method old_stream_output = self.extension_module.stream_output co = Dingus() self.extension_module.stream_output = co # patch utils.rewrite_cfg method old_rewrite = self.extension_module.utils.rewrite_cfgs rewrite = Dingus() self.extension_module.utils.rewrite_cfgs = rewrite try: ct = self.extension_module.ComposerExtension(ctx) ct._builder = builder ct.run() eq_(2, len(builder.move.calls())) eq_(1, len(builder.copy.calls())) assert rewrite.calls().once() rewrite_args = rewrite.calls()[0].args assert rewrite_args[0].endswith('php.ini') assert 'HOME' in rewrite_args[1] assert 'TMPDIR' in rewrite_args[1] assert co.calls().once() instCmd = co.calls()[0].args[1] assert instCmd.find('install') > 0 assert instCmd.find('--no-progress') > 0 assert instCmd.find('--no-interaction') == -1 assert instCmd.find('--no-dev') == -1 assert instCmd.find('--optimize-autoloader') > 0 finally: self.extension_module.stream_output = old_stream_output self.extension_module.utils.rewrite_cfgs = old_rewrite
def should_connect(self): self.frame._getline = Dingus() self.frame._getline.return_value = \ 'CONNECTED\nsession:ID:nose-session123\n\n\x00\n' self.frame.connect(self.sockobj.connect('localhost', 99999)) sendall = self.frame.sock.calls('sendall', DontCare).one().args[0] assert self.frame.session is not None assert 'CONNECT' in sendall
def should_get_message_and_return_frame(self): my_frame = Frame() my_frame._getline = Dingus() command = 'MESSAGE' body = 'Test 1' headers = { 'session': 'ID:nose-session123', 'content-length': '%d' % len(body) } my_frame.parse_frame = Dingus() this_frame = my_frame.build_frame({ 'command': command, 'headers': headers, 'body': body }) my_frame.parse_frame.return_value = this_frame ret_frame = my_frame.get_message(nb=True) assert isinstance(ret_frame, Frame)
class WhenCreatingNewDingus: def setup(self): self.dingus = Dingus() def should_not_have_any_recorded_calls(self): assert not self.dingus.calls() def should_have_a_name(self): assert self.dingus.__name__ == 'dingus_%i' % id(self.dingus)
class WhenCallingAttributesOfReturnedValues: def setup(self): self.grandparent = Dingus() self.parent = self.grandparent() self.child = self.parent.child self.child('arg', kwarg=None) def should_record_call_on_grandparent(self): assert self.grandparent.calls('()').one() def should_record_child_call_on_child(self): assert self.child.calls('()').one() def should_record_child_call_on_parent(self): assert self.parent.calls('child').one() def should_not_record_child_call_on_grandparent(self): assert not self.grandparent.calls('().child')
def should_not_get_from_queue(self): headers = { 'destination': '/queue/nose_test', 'persistent': 'true', 'bytes_message': 'true' } body = 'Testing' this_frame = self.frame.build_frame({ 'command': 'SEND', 'headers': headers, 'body': body }) this_frame = Dingus() extracted_frame = self.queue.get(this_frame) print this_frame.calls assert this_frame.calls('parse_frame', nb=False)
class TestSocketReader: def setup(self): self.socket = Dingus() self.data_that_was_read = read_socket(self.socket) def should_read_from_socket(self): assert self.socket.calls('recv', 1024).once() def should_return_what_is_read(self): assert self.data_that_was_read == self.socket.recv() def should_close_socket_after_reading(self): # Sequence tests like this often aren't needed, as your higher-level # system tests will catch such problems. But I include one here to # illustrate more complex use of the "calls" list. assert self.socket.calls('close') call_names = [call.name for call in self.socket.calls] assert call_names.index('close') > call_names.index('recv')