コード例 #1
0
 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_), {})
コード例 #2
0
    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)
コード例 #3
0
 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})
コード例 #4
0
 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'])
コード例 #5
0
 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()
コード例 #6
0
 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)
コード例 #7
0
 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)
コード例 #8
0
 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)
コード例 #9
0
    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)
コード例 #10
0
    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')
コード例 #11
0
 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)
コード例 #12
0
    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
コード例 #13
0
 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()
コード例 #14
0
 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')
コード例 #15
0
 def setUpClass(cls):
     """Generate some attributes and use them to instantiate a config."""
     cls.kwargs = _gen_attrs()
     cls.cfg = config.ServerConfig(**cls.kwargs)
コード例 #16
0
 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)
コード例 #17
0
 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)
コード例 #18
0
 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)
コード例 #19
0
 def setUpClass(cls):
     """Generate attributes and call the method under test."""
     cls.attrs = _gen_attrs()
     cls.result = repr(config.ServerConfig(**cls.attrs))
コード例 #20
0
 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()
コード例 #21
0
 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)
コード例 #22
0
 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)