Beispiel #1
0
 def setUp(self):
     """Generate contents for a configuration file."""
     open_ = mock.mock_open(read_data=PULP_SMASH_CONFIG)
     with mock.patch.object(builtins, 'open', open_):
         cfg = config.PulpSmashConfig()
         with mock.patch.object(cfg, 'get_config_file_path'):
             self.cfg = cfg.read()
Beispiel #2
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.PulpSmashConfig(
                pulp_auth=['admin', 'admin'],
                systems=[
                    config.PulpSystem(
                        hostname='example.com',
                        roles={'api': {
                            'scheme': 'http',
                        }}
                    )
                ]
            ))
            with mock.patch.object(client, 'request') as request:
                getattr(client, method)('')
            cls.mocks[method] = request
Beispiel #3
0
 def test_read_old_config_file(self):
     """Ensure Pulp Smash can read old config file format."""
     open_ = mock.mock_open(read_data=OLD_CONFIG)
     with mock.patch.object(builtins, 'open', open_):
         cfg = config.PulpSmashConfig()
         with mock.patch.object(cfg, 'get_config_file_path'):
             with self.assertWarns(DeprecationWarning):
                 cfg = cfg.read()
     with self.subTest('check pulp_auth'):
         self.assertEqual(cfg.pulp_auth, ['username', 'password'])
     with self.subTest('check pulp_version'):
         self.assertEqual(cfg.pulp_version, config.Version('2.12'))
     with self.subTest('check systems'):
         self.assertEqual(cfg.systems, [
             config.PulpSystem(hostname='pulp.example.com',
                               roles={
                                   'amqp broker': {
                                       'service': 'qpidd'
                                   },
                                   'api': {
                                       'scheme': 'https',
                                       'verify': False,
                                   },
                                   'mongod': {},
                                   'pulp cli': {},
                                   'pulp celerybeat': {},
                                   'pulp resource manager': {},
                                   'pulp workers': {},
                                   'shell': {
                                       'transport': 'ssh'
                                   },
                                   'squid': {},
                               })
         ])
Beispiel #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.PulpSmashConfig()
     self.assertEqual(
         cfg._xdg_config_file,  # pylint:disable=protected-access
         os_environ['PULP_SMASH_CONFIG_FILE'])
Beispiel #5
0
 def test_success(self):
     """Assert the method returns a path when a config file is found."""
     with mock.patch.object(xdg.BaseDirectory, 'load_config_paths') as lcp:
         lcp.return_value = ('an_iterable', 'of_xdg', 'config_paths')
         with mock.patch.object(os.path, 'isfile') as isfile:
             isfile.return_value = True
             # pylint:disable=protected-access
             config.PulpSmashConfig().get_config_file_path()
     self.assertGreater(isfile.call_count, 0)
Beispiel #6
0
 def test_failures(self):
     """Assert the  method raises an exception when no config is found."""
     with mock.patch.object(xdg.BaseDirectory, 'load_config_paths') as lcp:
         lcp.return_value = ('an_iterable', 'of_xdg', 'config_paths')
         with mock.patch.object(os.path, 'isfile') as isfile:
             isfile.return_value = False
             with self.assertRaises(exceptions.ConfigFileNotFoundError):
                 # pylint:disable=protected-access
                 config.PulpSmashConfig().get_config_file_path()
     self.assertGreater(isfile.call_count, 0)
Beispiel #7
0
 def test_implicit_local_transport(self):
     """Assert it is possible to implicitly ask for a "local" transport."""
     cfg = config.PulpSmashConfig(systems=[
         config.PulpSystem(
             hostname=socket.getfqdn(),
             roles={
                 'pulp cli': {},
             }
         )
     ])
     self.assertIsInstance(cli.Client(cfg).machine, LocalMachine)
Beispiel #8
0
 def test_explicit_local_transport(self):
     """Assert it is possible to explicitly ask for a "local" transport."""
     cfg = config.PulpSmashConfig(systems=[
         config.PulpSystem(
             hostname=utils.uuid4(),
             roles={
                 'pulp cli': {},
                 'shell': {'transport': 'local'},
             }
         )
     ])
     self.assertIsInstance(cli.Client(cfg).machine, LocalMachine)
Beispiel #9
0
def _get_pulp_smash_config(hosts):
    """Return a config object with made-up attributes.

    :param hosts: Passed through to :data:`pulp_smash.config.PulpSmashConfig`.
    :rtype: pulp_smash.config.PulpSmashConfig
    """
    return config.PulpSmashConfig(
        pulp_auth=['admin', 'admin'],
        pulp_version='1!0',
        pulp_selinux_enabled=True,
        hosts=hosts,
    )
Beispiel #10
0
 def test_default_response_handler(self):
     """Assert the default response handler checks return codes."""
     cfg = config.PulpSmashConfig(systems=[
         config.PulpSystem(
             hostname=utils.uuid4(),
             roles={
                 'pulp cli': {},
                 'shell': {'transport': 'local'},
             }
         )
     ])
     self.assertIs(cli.Client(cfg).response_handler, cli.code_handler)
Beispiel #11
0
def _get_pulp_smash_config(**kwargs):
    """Return a config object with made-up attributes.

    :rtype: pulp_smash.config.PulpSmashConfig
    """
    kwargs.setdefault("pulp_auth", ["admin", "admin"])
    kwargs.setdefault("pulp_version", "1!0")
    kwargs.setdefault("pulp_selinux_enabled", True)
    kwargs.setdefault("timeout", 1800)
    hosts = [config.PulpHost(hostname="example.com", roles={"api": {"scheme": "http"}})]
    kwargs.setdefault("hosts", hosts)
    return config.PulpSmashConfig(**kwargs)
Beispiel #12
0
 def test_explicit_response_handler(self):
     """Assert it is possible to explicitly set a response handler."""
     cfg = config.PulpSmashConfig(systems=[
         config.PulpSystem(
             hostname=utils.uuid4(),
             roles={
                 'pulp cli': {},
                 'shell': {'transport': 'local'},
             }
         )
     ])
     handler = mock.Mock()
     self.assertIs(cli.Client(cfg, handler).response_handler, handler)
Beispiel #13
0
 def test_read_config_file(self):
     """Ensure Pulp Smash can read the config file."""
     open_ = mock.mock_open(read_data=PULP_SMASH_CONFIG)
     with mock.patch.object(builtins, 'open', open_):
         cfg = config.PulpSmashConfig()
         with mock.patch.object(cfg, 'get_config_file_path'):
             cfg = cfg.read()
     with self.subTest('check pulp_auth'):
         self.assertEqual(cfg.pulp_auth, ['username', 'password'])
     with self.subTest('check pulp_version'):
         self.assertEqual(cfg.pulp_version, config.Version('2.12.1'))
     with self.subTest('check systems'):
         self.assertEqual(
             sorted(cfg.systems),
             sorted([
                 config.PulpSystem(hostname='first.example.com',
                                   roles={
                                       'amqp broker': {
                                           'service': 'qpidd'
                                       },
                                       'api': {
                                           'port': 1234,
                                           'scheme': 'https',
                                           'verify': True,
                                       },
                                       'mongod': {},
                                       'pulp cli': {},
                                       'pulp celerybeat': {},
                                       'pulp resource manager': {},
                                       'pulp workers': {},
                                       'shell': {
                                           'transport': 'local'
                                       },
                                       'squid': {},
                                   }),
                 config.PulpSystem(hostname='second.example.com',
                                   roles={
                                       'api': {
                                           'port': 2345,
                                           'scheme': 'https',
                                           'verify': False,
                                       },
                                       'pulp celerybeat': {},
                                       'pulp resource manager': {},
                                       'pulp workers': {},
                                       'shell': {
                                           'transport': 'ssh'
                                       },
                                       'squid': {}
                                   }),
             ]))
Beispiel #14
0
 def test_run(self):
     """Assert the function executes ``cli.Client.run``."""
     with mock.patch.object(cli, 'Client') as client:
         cfg = config.PulpSmashConfig(
             pulp_auth=['u', 'p'],
             systems=[
                 config.PulpSystem(
                     hostname='example.com',
                     roles={'pulp cli': {}}
                 )
             ]
         )
         response = utils.pulp_admin_login(cfg)
         self.assertIs(response, client.return_value.run.return_value)
Beispiel #15
0
 def test_run(self):
     """Assert the function executes ``cli.Client.run``."""
     with mock.patch.object(cli, "Client") as client:
         cfg = config.PulpSmashConfig(
             pulp_auth=["admin", "admin"],
             pulp_version="1!0",
             pulp_selinux_enabled=True,
             timeout=1800,
             hosts=[
                 config.PulpHost(hostname="example.com",
                                 roles={"pulp cli": {}})
             ],
         )
         response = pulp_admin_login(cfg)
         self.assertIs(response, client.return_value.run.return_value)
Beispiel #16
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.PulpSmashConfig(
            pulp_auth=['admin', 'admin'],
            systems=[
                config.PulpSystem(
                    hostname='base url',
                    roles={'api': {'scheme': 'http'}},
                )
            ]
        ), response_handler)
        self.assertIs(client.response_handler, response_handler)
Beispiel #17
0
 def test_run(self):
     """Assert the function executes ``cli.Client.run``."""
     with mock.patch.object(cli, 'Client') as client:
         cfg = config.PulpSmashConfig(
             pulp_auth=['admin', 'admin'],
             pulp_version='1!0',
             pulp_selinux_enabled=True,
             hosts=[
                 config.PulpHost(
                     hostname='example.com',
                     roles={'pulp cli': {}},
                 )
             ]
         )
         response = pulp_admin_login(cfg)
         self.assertIs(response, client.return_value.run.return_value)
Beispiel #18
0
def _get_pulp_smash_config():
    """Return a config object with made-up attributes.

    :rtype: pulp_smash.config.PulpSmashConfig
    """
    return config.PulpSmashConfig(pulp_auth=['admin', 'admin'],
                                  pulp_version='1!0',
                                  pulp_selinux_enabled=True,
                                  hosts=[
                                      config.PulpHost(
                                          hostname='example.com',
                                          roles={'api': {
                                              'scheme': 'http'
                                          }},
                                      )
                                  ])
Beispiel #19
0
 def test_json_arg(self):
     """Assert methods with a ``json`` argument pass on that argument."""
     json = mock.Mock()
     client = api.Client(config.PulpSmashConfig(
         pulp_auth=['admin', 'admin'],
         systems=[
             config.PulpSystem(
                 hostname='base url',
                 roles={'api': {'scheme': 'http'}},
             )
         ]
     ))
     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.assertEqual(
                 request.call_args[0], (method.upper(), 'some url'))
             self.assertIs(request.call_args[1]['json'], json)
Beispiel #20
0
 def test_implicit_pulp_system(self):
     """Assert it is possible to implicitly target a pulp cli PulpSystem."""
     cfg = config.PulpSmashConfig(systems=[
         config.PulpSystem(
             hostname=utils.uuid4(),
             roles={
                 'pulp cli': {},
             }
         ),
         config.PulpSystem(
             hostname=utils.uuid4(),
             roles={
                 'pulp cli': {},
             }
         )
     ])
     with mock.patch('pulp_smash.cli.plumbum') as plumbum:
         machine = mock.Mock()
         plumbum.machines.SshMachine.return_value = machine
         self.assertEqual(cli.Client(cfg).machine, machine)
         plumbum.machines.SshMachine.assert_called_once_with(
             cfg.systems[0].hostname)
Beispiel #21
0
 def setUpClass(cls):
     """Generate attributes and call the method under test."""
     cls.attrs = _gen_attrs()
     cls.cfg = config.PulpSmashConfig(**cls.attrs)
     cls.result = repr(cls.cfg)
Beispiel #22
0
 def setUpClass(cls):
     """Create a mock server config and call the method under test."""
     cls.attrs = _gen_attrs()
     cls.cfg = config.PulpSmashConfig(**cls.attrs)
     cls.kwargs = cls.cfg.get_requests_kwargs()
Beispiel #23
0
 def setUpClass(cls):
     """Generate some attributes and use them to instantiate a config."""
     cls.kwargs = _gen_attrs()
     cls.cfg = config.PulpSmashConfig(**cls.kwargs)
Beispiel #24
0
 def test_var_unset(self):
     """Do not set the environment variable."""
     with mock.patch.dict(os.environ, {}, clear=True):
         cfg = config.PulpSmashConfig()
     # pylint:disable=protected-access
     self.assertEqual(cfg._xdg_config_file, 'settings.json')