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_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_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'
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_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 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_uses_curl(self): ctx = utils.FormattedDict( { "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 )
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_oauth_token_is_valid_uses_curl(self): ctx = utils.FormattedDict({ 'BUILD_DIR': '/usr/awesome', 'PHP_VM': 'php', 'TMPDIR': tempfile.gettempdir(), 'LIBDIR': 'lib', 'CACHE_DIR': 'cache', }) 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 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_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_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'], '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) with patches({ 'StringIO.StringIO.getvalue': instance_stub, 'composer.extension.stream_output': stream_output_stub, 'composer.extension.utils.rewrite_cfgs': rewrite_stub }): ct = self.extension_module.ComposerExtension(ctx) ct._builder = builder ct.composer_runner = \ self.extension_module.ComposerCommandRunner(ctx, builder) ct.run() eq_(2, len(builder.move.calls())) eq_(1, len(builder.copy.calls())) assert rewrite_stub.calls().once() rewrite_args = rewrite_stub.calls()[0].args assert rewrite_args[0].endswith('php.ini') assert 'HOME' in rewrite_args[1] assert 'TMPDIR' in rewrite_args[1] instCmd = stream_output_stub.calls()[-1].args[1] assert instCmd.find('--optimize-autoloader') > 0
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_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
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 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_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_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]
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 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_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_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]
def test_composer_tool_install(self): ctx = utils.FormattedDict({ 'DOWNLOAD_URL': 'http://server/bins', '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-alpha10/composer.phar', \ "was %s" % installer._installer.calls()[0].args[0]
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]
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}
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_parser(): on_raw = Dingus() parser = Parser() parser.add_option('raw', on_raw) parser(['trash-list', '--raw']) assert on_raw.calls('()')
def test_create(self): # patch so we know the notifier config.set_config(StringIO(test_config)) inotify = Dingus() with patch('consoleserver.monitoredconsolecollection.INotify', Dingus(return_value=inotify)): collection = MonitoredConsoleCollection() name, args, kwargs, result = inotify.calls('watch')[0] self.assertEqual(2, len(args)) self.assertEqual('/dev', args[0].path) name, args, kwargs, result = inotify.calls('startReading')[0] self.assertEqual(0, len(args)) collection.created('ignored', FilePath('/dev/ttyUSB0'), 'mask') # fake the notify self.assertEqual(1, len(collection)) collection.created('ignored', FilePath('/dev/tty0'), 'mask') # fake the notify self.assertEqual(1, len(collection))
def test_run_sets_github_oauth_token_if_present(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', 'COMPOSER_GITHUB_OAUTH_TOKEN': 'MADE_UP_TOKEN_VALUE' }) 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 old_environment = os.environ os.environ = {'COMPOSER_GITHUB_OAUTH_TOKEN': 'MADE_UP_TOKEN_VALUE'} try: ct = self.extension_module.ComposerExtension(ctx) builder_stub = Dingus(_ctx=ctx) ct._builder = builder_stub github_oauth_token_is_valid_stub = Dingus( 'test_run_sets_github_oauth_token_if_present:github_oauth_token_is_valid_stub' ) github_oauth_token_is_valid_stub._set_return_value(True) ct._github_oauth_token_is_valid = github_oauth_token_is_valid_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 os.environ = old_environment assert executed_command.find('config') > 0, 'did not see "config"' assert executed_command.find('-g') > 0, 'did not see "-g"' assert executed_command.find( 'github-oauth.github.com' ) > 0, 'did not see "github-oauth.github.com"' assert executed_command.find( '"MADE_UP_TOKEN_VALUE"') > 0, 'did not see "MADE_UP_TOKEN_VALUE"'
def test_no_github_api_call_with_cached_buildpack(self): ctx = utils.FormattedDict({ 'BUILD_DIR': tempfile.gettempdir(), 'PHP_VM': 'php', 'TMPDIR': tempfile.gettempdir(), 'LIBDIR': 'lib', 'CACHE_DIR': 'cache', 'BP_DIR': '', 'WEBDIR': '' }) builder = Dingus(_ctx=ctx) path_exists_stub = Dingus() path_exists_stub._set_return_value(True) setup_composer_github_token_stub = Dingus() check_github_rate_exceeded_stub = Dingus() rewrite_stub = Dingus() stream_output_stub = Dingus( 'test_github_oauth_token_uses_curl : stream_output') with patches({ 'os.path.exists': path_exists_stub, 'composer.extension.ComposerExtension.setup_composer_github_token': setup_composer_github_token_stub, 'composer.extension.ComposerExtension.check_github_rate_exceeded': check_github_rate_exceeded_stub, 'composer.extension.utils.rewrite_cfgs': rewrite_stub, 'composer.extension.stream_output': stream_output_stub }): ct = self.extension_module.ComposerExtension(ctx) ct._builder = builder ct.composer_runner = \ self.extension_module.ComposerCommandRunner(ctx, builder) ct.run() assert 0 == len(setup_composer_github_token_stub.calls()), \ 'setup_composer_github_token was called, expected no calls' assert 0 == len(check_github_rate_exceeded_stub.calls()), \ 'check_github_rate_exceeded was called, expected no calls'
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')
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)
def test_no_github_api_call_with_cached_buildpack(self): ctx = utils.FormattedDict( { "BUILD_DIR": tempfile.gettempdir(), "PHP_VM": "php", "TMPDIR": tempfile.gettempdir(), "LIBDIR": "lib", "CACHE_DIR": "cache", "BP_DIR": "", "WEBDIR": "", } ) builder = Dingus(_ctx=ctx) path_exists_stub = Dingus() path_exists_stub._set_return_value(True) setup_composer_github_token_stub = Dingus() check_github_rate_exceeded_stub = Dingus() rewrite_stub = Dingus() stream_output_stub = Dingus("test_github_oauth_token_uses_curl : stream_output") with patches( { "os.path.exists": path_exists_stub, "composer.extension.ComposerExtension.setup_composer_github_token": setup_composer_github_token_stub, "composer.extension.ComposerExtension.check_github_rate_exceeded": check_github_rate_exceeded_stub, "composer.extension.utils.rewrite_cfgs": rewrite_stub, "composer.extension.stream_output": stream_output_stub, } ): ct = self.extension_module.ComposerExtension(ctx) ct._builder = builder ct.composer_runner = self.extension_module.ComposerCommandRunner(ctx, builder) ct.run() assert 0 == len( setup_composer_github_token_stub.calls() ), "setup_composer_github_token was called, expected no calls" assert 0 == len( check_github_rate_exceeded_stub.calls() ), "check_github_rate_exceeded was called, expected no calls"
def test_composer_run_streams_debug_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', 'BP_DEBUG': 'True', 'BP_DIR': '' }) instance_stub = Dingus( return_value="""{"rate": {"limit": 60, "remaining": 60}}""") stream_output_stub = Dingus() builder = Dingus(_ctx=ctx) with patches({ 'StringIO.StringIO.getvalue': instance_stub, 'composer.extension.stream_output': stream_output_stub }): ct = self.extension_module.ComposerExtension(ctx) ct._builder = builder ct.composer_runner = \ self.extension_module.ComposerCommandRunner(ctx, builder) ct.run() stream_output_calls = stream_output_stub.calls() assert 3 == len(stream_output_calls), \ "The number of stream_output calls returned %s, expected 3" % len(stream_output_stub.calls()) # first is called `composer -V` verCmd = stream_output_calls[0].args[1] assert verCmd.find('composer.phar -V') # then composer install instCmd = stream_output_calls[-1].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
class WhenFollowingTemplatedLinks(object): def setup(self): self.web_client = Dingus() self.web_client.request.return_value = ['application/fake', ''] link = Link('/foo/%s/bar', self.web_client) link.get('variable') def should_send_requests_to_server(self): assert self.web_client.calls( 'request', 'GET', '/foo/variable/bar', False, None).one()
class WhenFollowingTemplatedLinks(object): def setup(self): self.web_client = Dingus() self.web_client.request.return_value = ['application/fake', ''] link = Link('/foo/%s/bar', self.web_client) link.get('variable') def should_send_requests_to_server(self): assert self.web_client.calls('request', 'GET', '/foo/variable/bar', False, None).one()
class WhenCallingDingusAsFunction: def setup(self): self.dingus = Dingus() self.dingus('arg', kwarg=None) def should_record_call(self): assert self.dingus.calls() def should_have_exactly_one_call(self): assert self.dingus.calls().one() def should_have_once_method_as_alias_for_one_method(self): assert self.dingus.calls().once() def should_record_args(self): assert self.dingus.calls.one().args == ('arg',) def should_record_kwargs(self): assert self.dingus.calls.one().kwargs == {'kwarg': None}
class WhenCallingDingusAsFunction: def setup(self): self.dingus = Dingus() self.dingus('arg', kwarg=None) def should_record_call(self): assert self.dingus.calls() def should_have_exactly_one_call(self): assert self.dingus.calls().one() def should_have_once_method_as_alias_for_one_method(self): assert self.dingus.calls().once() def should_record_args(self): assert self.dingus.calls.one().args == ('arg', ) def should_record_kwargs(self): assert self.dingus.calls.one().kwargs == {'kwarg': None}
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')
def test_composer_tool_run_custom_composer_opts(self): ctx = utils.FormattedDict( { "PHP_VM": "php", "BUILD_DIR": "/build/dir", "CACHE_DIR": "/cache/dir", "TMPDIR": tempfile.gettempdir(), "WEBDIR": "htdocs", "LIBDIR": "lib", "COMPOSER_INSTALL_OPTIONS": ["--optimize-autoloader"], "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) with patches( { "StringIO.StringIO.getvalue": instance_stub, "composer.extension.stream_output": stream_output_stub, "composer.extension.utils.rewrite_cfgs": rewrite_stub, } ): ct = self.extension_module.ComposerExtension(ctx) ct._builder = builder ct.composer_runner = self.extension_module.ComposerCommandRunner(ctx, builder) ct.run() eq_(1, len(builder.copy.calls())) assert rewrite_stub.calls().once() rewrite_args = rewrite_stub.calls()[0].args assert rewrite_args[0].endswith("php.ini") assert "HOME" in rewrite_args[1] assert "TMPDIR" in rewrite_args[1] instCmd = stream_output_stub.calls()[-1].args[1] assert instCmd.find("--optimize-autoloader") > 0
def test_composer_run_streams_debug_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', 'BP_DEBUG': 'True', 'BP_DIR': '' }) instance_stub = Dingus(return_value="""{"rate": {"limit": 60, "remaining": 60}}""") stream_output_stub = Dingus() builder = Dingus(_ctx=ctx) with patches({ 'StringIO.StringIO.getvalue': instance_stub, 'composer.extension.stream_output': stream_output_stub }): ct = self.extension_module.ComposerExtension(ctx) ct._builder = builder ct.composer_runner = \ self.extension_module.ComposerCommandRunner(ctx, builder) ct.run() stream_output_calls = stream_output_stub.calls() assert 3 == len(stream_output_calls), \ "The number of stream_output calls returned %s, expected 3" % len(stream_output_stub.calls()) # first is called `composer -V` verCmd = stream_output_calls[0].args[1] assert verCmd.find('composer.phar -V') # then composer install instCmd = stream_output_calls[-1].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
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'], '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) with patches({ 'StringIO.StringIO.getvalue': instance_stub, 'composer.extension.stream_output': stream_output_stub, 'composer.extension.utils.rewrite_cfgs': rewrite_stub }): ct = self.extension_module.ComposerExtension(ctx) ct._builder = builder ct.composer_runner = \ self.extension_module.ComposerCommandRunner(ctx, builder) ct.run() eq_(2, len(builder.move.calls())) eq_(1, len(builder.copy.calls())) assert rewrite_stub.calls().once() rewrite_args = rewrite_stub.calls()[0].args assert rewrite_args[0].endswith('php.ini') assert 'HOME' in rewrite_args[1] assert 'TMPDIR' in rewrite_args[1] instCmd = stream_output_stub.calls()[-1].args[1] assert instCmd.find('--optimize-autoloader') > 0
class WhenCallingListItemOfDingus: def setup(self): self.parent = Dingus() self.child = self.parent[0] self.child() def should_record_call_on_parent(self): assert self.parent.calls('[0]').one() def should_record_call_on_child(self): assert self.child.calls('()').one()
class WhenCallingAttributeGrandchild: def setup(self): self.grandparent = Dingus() self.parent = self.grandparent.parent self.child = self.parent.child self.child('arg', kwarg=None) def should_not_record_call_on_grandparent(self): assert not self.grandparent.calls('parent.child') def should_record_call_on_parent(self): assert self.parent.calls('child').one()
def test_run_sets_github_oauth_token_if_present(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', 'COMPOSER_GITHUB_OAUTH_TOKEN': 'MADE_UP_TOKEN_VALUE', 'BP_DIR': '' }) instance_stub = Dingus() instance_stub._set_return_value( """{"rate": {"limit": 60, "remaining": 60}}""") stream_output_stub = Dingus() rewrite_stub = Dingus() environ_stub = Dingus() environ_stub._set_return_value('MADE_UP_TOKEN_VALUE') with patches({ 'StringIO.StringIO.getvalue': instance_stub, 'composer.extension.stream_output': stream_output_stub, 'composer.extension.utils.rewrite_cfgs': rewrite_stub, 'os.environ.get': environ_stub }): ct = self.extension_module.ComposerExtension(ctx) builder_stub = Dingus(_ctx=ctx) ct._builder = builder_stub ct.composer_runner = \ self.extension_module.ComposerCommandRunner(ctx, builder_stub) github_oauth_token_is_valid_stub = Dingus( 'test_run_sets_github_oauth_token_if_present:' 'github_oauth_token_is_valid_stub') github_oauth_token_is_valid_stub._set_return_value(True) ct._github_oauth_token_is_valid = github_oauth_token_is_valid_stub ct.run() executed_command = stream_output_stub.calls()[0].args[1] assert executed_command.find('config') > 0, 'did not see "config"' assert executed_command.find('-g') > 0, 'did not see "-g"' assert executed_command.find('github-oauth.github.com') > 0, \ 'did not see "github-oauth.github.com"' assert executed_command.find('"MADE_UP_TOKEN_VALUE"') > 0, \ 'did not see "MADE_UP_TOKEN_VALUE"'
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_composer_tool_run(self): ctx = utils.FormattedDict({ '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.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.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') > 0 assert instCmd.find('--no-dev') > 0 finally: self.ct.stream_output = old_stream_output self.ct.utils.rewrite_cfgs = old_rewrite
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_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
class WhenMakingRequests: def setup(self): self.web_client = Dingus() self.web_client.request.return_value = ['application/fake', ''] self.link = Link('/', self.web_client) def should_forward_get_requests_to_web_client(self): self.link.get() assert self.web_client.calls('request', 'GET', '/', False, None).one() def should_forward_put_requests_to_web_client(self): self.link.put('putdata') assert self.web_client.calls('request', 'PUT', '/', False, 'putdata').one() def should_forward_post_requests_to_web_client(self): self.link.post('postdata') assert self.web_client.calls('request', 'POST', '/', False, 'postdata').one() def should_forward_delete_requests_to_web_client(self): self.link.delete() assert self.web_client.calls('request', 'DELETE', '/', False, None).one()
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_composer_tool_run_sanity_checks(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(), 'LIBDIR': 'lib', '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) exists_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 = \ self.extension_module.ComposerExtension(ctx) composer_extension._log = Dingus() composer_extension._builder = builder composer_extension.composer_runner = \ self.extension_module.ComposerCommandRunner(ctx, builder) composer_extension.run() composer_extension_calls = composer_extension._log.warning.calls() assert len(composer_extension_calls) > 0 assert composer_extension_calls[0].args[0].find('PROTIP:') == 0 exists = Dingus(return_value=True) with patch('os.path.exists', exists_stub): composer_extension._log = Dingus() composer_extension.run() assert len(exists_stub.calls()) == 2 assert len(composer_extension._log.warning.calls()) == 0
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)