def test_build_composer_environment_inherits_from_ctx(self): ctx = utils.FormattedDict({ 'BP_DIR': '', '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_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_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_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_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_github_oauth_token_is_valid_interprets_github_api_401_as_false( self): # noqa ctx = utils.FormattedDict({ 'BP_DIR': '', '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_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_github_download_rate_is_exceeded(self): # noqa ctx = utils.FormattedDict({ 'BP_DIR': '', '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 should_not_get_message(self): my_frame = Frame() my_frame._getline = Dingus() my_frame._getline.return_value = None my_frame.iqueue.get = Dingus() my_frame.iqueue.get.return_value = None ret_frame = my_frame.get_message(nb=True) assert ret_frame is None
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 setUp(self): AWeberAPITest.setUp(self) self.response = "oauth_token=cheeseburger&oauth_token_secret=hotdog" self.aweber.adapter = Dingus() self.aweber.adapter.user = AWeberUser() self.aweber.adapter.request = Dingus(return_value=self.response) self.aweber.user.request_token = '1234' self.aweber.user.token_secret = 'abcd' self.aweber.user.verifier = '234a35a1'
def setUp(self): request = Dingus('request') response = Dingus('response') toolbar = DebugToolbar(request) DebugToolbarMiddleware.debug_toolbars[thread.get_ident()] = toolbar self.request = request self.response = response self.toolbar = toolbar self.toolbar.stats = {}
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_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_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 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 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_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_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 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_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 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_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_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_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 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))
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): self.web_client = Dingus() self.web_client.request.return_value = [ u'application/json', { u'foo': u'/foo' } ] self.root = Resource.bookmark('/', self.web_client)
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)