def test_galaxy_api_init(): gc = GalaxyContext() api = rest_api.GalaxyAPI(gc) assert isinstance(api, rest_api.GalaxyAPI) assert api.galaxy_context == gc
def galaxy_api(request): gc = GalaxyContext(server=request.param['server']) # log.debug('gc: %s', gc) # mock the result of _get_server_api_versions here, so that we dont get the extra # call when calling the tests patcher = mock.patch('ansible_galaxy.rest_api.open_url', new=FauxUrlResponder( [ FauxUrlOpenResponse(data={'current_version': 'v1'}), FauxUrlOpenResponse(data={'results': [{'foo1': 11, 'foo2': 12}]}, url='blippyblopfakeurl'), ] )) patcher.start() api = rest_api.GalaxyAPI(gc) # do a call so that we run g_connect and _get_server_api_versions by side effect now api.lookup_repo_by_name('test-namespace', 'test-repo') # now unpatch patcher.stop() yield api
def galaxy_api_mocked(mocker, galaxy_context_example_invalid, requests_mock): requests_mock.get('http://bogus.invalid:9443/api/', json={'current_version': 'v2'}) api = rest_api.GalaxyAPI(galaxy_context_example_invalid) return api
def test_galaxy_api_get_server_api_version_not_supported_version(mocker): mocker.patch( 'ansible_galaxy.rest_api.requests.Session.request', new=FauxUrlResponder([ FauxUrlOpenResponse(data={ 'current_version': 'v11.22.13beta4.preRC-16.0.0.0.42.42.37.1final' }), # doesn't matter response, just need a second call FauxUrlOpenResponse(data={'results': 'stuff'}, url='blippyblopfakeurl'), ])) gc = GalaxyContext(server=default_server_dict) api = rest_api.GalaxyAPI(gc) # expected to raise a client error about the server version not being supported in client # TODO: should be the server deciding if the client version is sufficient try: api.get_collection_detail('test-namespace', 'test-repo') except exceptions.GalaxyClientError as e: log.exception(e) assert 'Unsupported Galaxy server API' in '%s' % e return assert False, 'Expected a GalaxyClientError about supported server apis, but didnt happen'
def run(self): raw_config_file_path = get_config_path_from_env( ) or defaults.CONFIG_FILE self.config_file_path = os.path.abspath( os.path.expanduser(raw_config_file_path)) super(GalaxyCLI, self).run() self.config = config.load(self.config_file_path) log.debug('configuration: %s', json.dumps(self.config.as_dict(), indent=None)) # cli --server value or the url field of the first server in config # TODO: pass list of server config objects to GalaxyContext and/or create a GalaxyContext later # server_url = self.options.server_url or self.config['servers'][0]['url'] # ignore_certs = self.options.ignore_certs or self.config['servers'][0]['ignore_certs'] galaxy_context = self._get_galaxy_context(self.options, self.config) log.debug('galaxy context: %s', galaxy_context) self.api = rest_api.GalaxyAPI(galaxy_context) log.debug('execute action: %s', self.action) log.debug('execute action with options: %s', self.options) log.debug('execute action with args: %s', self.args) return self.execute()
def test_galaxy_api_get_server_api_version(galaxy_context_example_invalid, requests_mock): requests_mock.get('http://bogus.invalid:9443/api/', json={'current_version': 'v1'}) api = rest_api.GalaxyAPI(galaxy_context_example_invalid) res = api._get_server_api_version() log.debug('res: %s %r', res, res) assert isinstance(res, text_type) assert res == 'v1'
def test_galaxy_api_get_server_api_version_not_supported_version(galaxy_context_example_invalid, requests_mock): requests_mock.get('http://bogus.invalid:9443/api/', json={'current_version': 'v11.22.13beta4.preRC-16.0.0.0.42.42.37.1final'}) api = rest_api.GalaxyAPI(galaxy_context_example_invalid) # expected to raise a client error about the server version not being supported in client # TODO: should be the server deciding if the client version is sufficient with pytest.raises(exceptions.GalaxyClientError, match='.*Unsupported Galaxy server API.*') as exc_info: api.get_collection_detail('test-namespace', 'test-repo') log.debug('exc_info: %s', exc_info)
def test_galaxy_api_get_server_api_version_no_current_version(galaxy_context_example_invalid, requests_mock): requests_mock.get('http://bogus.invalid:9443/api/', json={'some_key': 'some_value', 'but_not_current_value': 2}, status_code=200) api = rest_api.GalaxyAPI(galaxy_context_example_invalid) with pytest.raises(exceptions.GalaxyClientError, match="The Galaxy API version could not be determined. The required 'current_version' field is missing.*") as exc_info: api._get_server_api_version() log.debug('exc_info: %s', exc_info)
def test_galaxy_api_get_server_api_version(mocker): mocker.patch('ansible_galaxy.rest_api.requests.Session.request', new=FauxUrlResponder([ FauxUrlOpenResponse(data={'current_version': 'v1'}), ])) gc = GalaxyContext(server=default_server_dict) api = rest_api.GalaxyAPI(gc) res = api._get_server_api_version() log.debug('res: %s %r', res, res) assert isinstance(res, text_type) assert res == 'v1'
def test_galaxy_api_get_server_api_version_HTTPError_not_json(mocker): mocker.patch('ansible_galaxy.rest_api.requests.Session.request', new=FauxUrlResponder([ FauxUrlOpenResponse(status=500, body='{stuff-that-is-not-valid-json'), ])) gc = GalaxyContext(server=default_server_dict) api = rest_api.GalaxyAPI(gc) try: api._get_server_api_version() except exceptions.GalaxyClientError as e: log.exception(e) # fragile, but currently return same exception so look for the right msg in args assert 'Could not process data from the API server' in '%s' % e return assert False, 'Expected a GalaxyClientError here but that did not happen'
def galaxy_api(galaxy_context_example_invalid, requests_mock): # mock the result of _get_server_api_versions here, so that we dont get the extra # call when calling the tests requests_mock.get('http://bogus.invalid:9443/api/', json={'current_version': 'v1'}) requests_mock.get('http://bogus.invalid:9443/api/whatever/', json={'results': [{'foo1': 11, 'foo2': 12} ] }) api = rest_api.GalaxyAPI(galaxy_context_example_invalid) # do a call so that we run g_connect and _get_server_api_versions by side effect now # api.get_object(href='http://bogus.invalid:9443/api/whatever/') log.debug('api: %s', api) yield api
def test_galaxy_api_get_server_api_version_HTTPError_500(mocker): error_body = u'{"detail": "Stuff broke, 500 error but server response has valid json include the detail key"}' mocker.patch('ansible_galaxy.rest_api.open_url', side_effect=HTTPError(url='http://whatever', code=500, msg='Stuff broke.', hdrs={}, fp=io.StringIO(initial_value=error_body))) gc = GalaxyContext(server=default_server_dict) api = rest_api.GalaxyAPI(gc) try: api._get_server_api_version() except exceptions.GalaxyClientError as e: log.exception(e) # fragile, but currently return same exception so look for the right msg in args assert 'Failed to get data from the API server' in '%s' % e return assert False, 'Expected a GalaxyClientError here but that did not happen'
def test_galaxy_api_get_server_api_version_no_current_version(mocker): mocker.patch('ansible_galaxy.rest_api.open_url', new=FauxUrlResponder( [ FauxUrlOpenResponse(status=500, data={'some_key': 'some_value', 'but_not_current_value': 2}), ] )) gc = GalaxyContext(server=default_server_dict) api = rest_api.GalaxyAPI(gc) try: api._get_server_api_version() except exceptions.GalaxyClientError as e: log.exception(e) # fragile, but currently return same exception so look for the right msg in args assert "missing required 'current_version'" in '%s' % e return assert False, 'Expected a GalaxyClientError here but that did not happen'
def execute_info(self): """ Display detailed information about an installed collection, as well as info available from the Galaxy API. """ if len(self.args) == 0: # the user needs to specify a collection raise cli_exceptions.CliOptionsError("- you must specify a collection name") log.debug('args=%s', self.args) galaxy_context = self._get_galaxy_context(self.options, self.config) repository_spec_strings = self.args api = rest_api.GalaxyAPI(galaxy_context) # FIXME: rc? return info.info_repository_specs(galaxy_context, api, repository_spec_strings, display_callback=self.display, offline=self.options.offline)
def test_galaxy_api_get_server_api_version_HTTPError_500(galaxy_context_example_invalid, requests_mock): url = 'http://bogus.invalid:9443/api/' response_data = { 'code': 'error', 'message': 'A server error occurred.', 'errors': [ {'code': 'error', 'message': 'Error message 1.'}, {'code': 'error', 'message': 'Stuff broke, 500 error but server response has valid json include the detail key'}, ] } requests_mock.get(url, json=response_data, reason='Internal Server Ooopsie', status_code=500) api = rest_api.GalaxyAPI(galaxy_context_example_invalid) with pytest.raises(exceptions.GalaxyRestAPIError, match='.*500 Server Error: Internal Server Ooopsie for url.*bogus.invalid:9443.*') as exc_info: api._get_server_api_version() log.debug('exc_info: %s', exc_info) exc = exc_info.value log.debug('exc.response: %s', exc.response) log.debug('exc.request: %s', exc.request) assert exc.code == 'error' assert exc.message == 'A server error occurred.' assert isinstance(exc.errors, list) assert exc.request.url == url assert exc.response.status_code == 500 assert exc.response.reason == 'Internal Server Ooopsie' assert exc.response.json()['code'] == 'error' assert exc.response.json()['errors'][0]['message'] == 'Error message 1.'
def test_galaxy_api_get_server_api_version_HTTPError_500(mocker): data = { "detail": "Stuff broke, 500 error but server response has valid json include the detail key" } mocker.patch('ansible_galaxy.rest_api.requests.Session.request', new=FauxUrlResponder([ FauxUrlOpenResponse(status=500, data=data), ], )) gc = GalaxyContext(server=default_server_dict) api = rest_api.GalaxyAPI(gc) try: api._get_server_api_version() except exceptions.GalaxyClientError as e: log.exception(e) # fragile, but currently return same exception so look for the right msg in args assert 'Failed to get data from the API server' in '%s' % e return assert False, 'Expected a GalaxyClientError here but that did not happen'
def test_galaxy_api_get_server_api_version_HTTPError_not_json(galaxy_context_example_invalid, requests_mock): url = 'http://bogus.invalid:9443/api/' requests_mock.get(url, text='{stuff-that-is-not-valid-json', reason='Internal Server Ooopsie', status_code=500) api = rest_api.GalaxyAPI(galaxy_context_example_invalid) with pytest.raises(exceptions.GalaxyRestServerError, match='.*500 Server Error: Internal Server Ooopsie for url.*bogus.invalid:9443.*') as exc_info: api._get_server_api_version() log.debug('exc_info: %s', exc_info) exc = exc_info.value log.debug('exc.response: %s', exc.response) log.debug('exc.request: %s', exc.request) assert exc.request.url == url assert exc.response.status_code == 500 assert exc.response.reason == 'Internal Server Ooopsie'