def test_delete_default(self): """Assert that the method can delete the default section.""" open_ = mock.mock_open(read_data=json.dumps({'pulp': {}})) with mock.patch.object(builtins, 'open', open_): with mock.patch.object(config, '_get_config_file_path'): config.ServerConfig().delete() self.assertEqual(_get_written_json(open_), {})
def test_response_handler(self): """Assert ``__init__`` saves the ``response_handler`` argument. The argument should be saved as an instance attribute. """ response_handler = mock.Mock() client = api.Client(config.ServerConfig('base url'), response_handler) self.assertIs(client.response_handler, response_handler)
def test_save_default(self): """Assert that the method can save the default section.""" attrs = _gen_attrs() open_ = mock.mock_open(read_data=json.dumps({})) with mock.patch.object(builtins, 'open', open_): with mock.patch.object(config, '_get_config_file_path'): config.ServerConfig(**attrs).save() self.assertEqual(_get_written_json(open_), {'pulp': attrs})
def test_var_set(self): """Set the environment variable.""" os_environ = {'PULP_SMASH_CONFIG_FILE': utils.uuid4()} with mock.patch.dict(os.environ, os_environ, clear=True): cfg = config.ServerConfig() self.assertEqual( cfg._xdg_config_file, # pylint:disable=protected-access os_environ['PULP_SMASH_CONFIG_FILE'])
def setUpClass(cls): """Read a mock configuration file section. Save relevant objects.""" cls.attrs = _gen_attrs() # config section values cls.open_ = mock.mock_open( read_data=json.dumps({'default': cls.attrs})) with mock.patch.object(builtins, 'open', cls.open_): with mock.patch.object(config, '_get_config_file_path'): cls.cfg = config.ServerConfig().read()
def test_json_arg(self): """Assert methods with a ``json`` argument pass on that argument.""" json = mock.Mock() client = api.Client(config.ServerConfig('base url')) for method in {'patch', 'post', 'put'}: with self.subTest(method=method): with mock.patch.object(client, 'request') as request: getattr(client, method)('some url', json) self.assertIs(request.call_args[1]['json'], json)
def test_read_default_section(self): """Read from a configuration file with a section named 'default'.""" self.config_file['default'] = self.config_file.pop('pulp') open_ = mock.mock_open(read_data=json.dumps(self.config_file)) with mock.patch.object(builtins, 'open', open_): with mock.patch.object(config, '_get_config_file_path'): with self.assertWarns(DeprecationWarning): cfg = config.ServerConfig().read() self.assertEqual(self.config_file['default']['base_url'], cfg.base_url)
def test_delete_section(self): """Assert that the method can delete a specified section.""" attrs = {'foo': {}, 'bar': {}} section = random.choice(tuple(attrs.keys())) open_ = mock.mock_open(read_data=json.dumps(attrs)) with mock.patch.object(builtins, 'open', open_): with mock.patch.object(config, '_get_config_file_path'): config.ServerConfig().delete(section) del attrs[section] self.assertEqual(_get_written_json(open_), attrs)
def test_read_section(self): """Read a section from the configuration file. Assert that values from the configuration file are present on the resultant :class:`pulp_smash.config.ServerConfig` object. """ open_ = mock.mock_open(read_data=json.dumps(self.config_file)) with mock.patch.object(builtins, 'open', open_): with mock.patch.object(config, '_get_config_file_path'): cfg = config.ServerConfig().read() self.assertEqual(self.config_file['pulp']['base_url'], cfg.base_url)
def test_read_nonexistent_section(self): """Read a non-existent section from the configuration file. Assert a :class:`pulp_smash.exceptions.ConfigFileSectionNotFoundError` is raised. """ open_ = mock.mock_open(read_data=json.dumps(self.config_file)) with mock.patch.object(builtins, 'open', open_): with mock.patch.object(config, '_get_config_file_path'): with self.assertRaises( exceptions.ConfigFileSectionNotFoundError): config.ServerConfig().read('foo')
def test_save_section(self): """Assert that the method can save a specified section.""" # `cfg` is the existing config file. We generate a new config as # `attrs` and save it into section `section`. cfg = {'existing': {}} section = utils.uuid4() attrs = _gen_attrs() open_ = mock.mock_open(read_data=json.dumps(cfg)) with mock.patch.object(builtins, 'open', open_): with mock.patch.object(config, '_get_config_file_path'): config.ServerConfig(**attrs).save(section) cfg[section] = attrs self.assertEqual(_get_written_json(open_), cfg)
def setUpClass(cls): """Assert methods delegate to :meth:`pulp_smash.api.Client.request`. All methods on :class:`pulp_smash.api.Client`, such as :meth:`pulp_smash.api.Client.delete`, should delegate to :meth:`pulp_smash.api.Client.request`. Mock out ``request`` and call the other methods. """ methods = {'delete', 'get', 'head', 'options', 'patch', 'post', 'put'} cls.mocks = {} for method in methods: client = api.Client(config.ServerConfig('http://example.com')) with mock.patch.object(client, 'request') as request: getattr(client, method)('') cls.mocks[method] = request
def setUpClass(cls): """Read a mock configuration file's sections. Save relevant objects.""" cls.config = random.choice(( {}, { 'foo': None }, { 'foo': None, 'bar': None, 'biz': None }, )) cls.open_ = mock.mock_open(read_data=json.dumps(cls.config)) with mock.patch.object(builtins, 'open', cls.open_): with mock.patch.object(config, '_get_config_file_path'): cls.sections = config.ServerConfig().sections()
def test_var_unset(self): """Do not set the environment variable.""" with mock.patch.dict(os.environ, {}, clear=True): cfg = config.ServerConfig() # pylint:disable=protected-access self.assertEqual(cfg._xdg_config_file, 'settings.json')
def setUpClass(cls): """Generate some attributes and use them to instantiate a config.""" cls.kwargs = _gen_attrs() cls.cfg = config.ServerConfig(**cls.kwargs)
def test_run(self): """Assert the function executes ``cli.Client.run``.""" with mock.patch.object(cli, 'Client') as client: cfg = config.ServerConfig('http://example.com', auth=['u', 'p']) response = utils.pulp_admin_login(cfg) self.assertIs(response, client.return_value.run.return_value)
def test_explicit_local_transport(self): """Assert it is possible to explicitly ask for a "local" transport.""" cfg = config.ServerConfig(utils.uuid4(), cli_transport='local') self.assertIsInstance(cli.Client(cfg).machine, LocalMachine)
def test_implicit_local_transport(self): """Assert it is possible to implicitly ask for a "local" transport.""" cfg = config.ServerConfig(socket.getfqdn()) self.assertIsInstance(cli.Client(cfg).machine, LocalMachine)
def setUpClass(cls): """Generate attributes and call the method under test.""" cls.attrs = _gen_attrs() cls.result = repr(config.ServerConfig(**cls.attrs))
def setUpClass(cls): """Create a mock server config and call the method under test.""" cls.attrs = _gen_attrs() cls.cfg = config.ServerConfig(**cls.attrs) cls.kwargs = cls.cfg.get_requests_kwargs()
def test_default_response_handler(self): """Assert the default response handler checks return codes.""" cfg = config.ServerConfig(utils.uuid4(), cli_transport='local') self.assertIs(cli.Client(cfg).response_handler, cli.code_handler)
def test_explicit_response_handler(self): """Assert it is possible to explicitly set a response handler.""" cfg = config.ServerConfig(utils.uuid4(), cli_transport='local') handler = mock.Mock() self.assertIs(cli.Client(cfg, handler).response_handler, handler)