def do_validate(self, cfg): """Validate the attributes of a configuration object.""" 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 pulp_selinux_enabled"): self.assertEqual(cfg.pulp_selinux_enabled, True) with self.subTest("check hosts"): self.assertEqual( sorted(cfg.hosts), sorted([ config.PulpHost( 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.PulpHost( hostname="second.example.com", roles={ "api": { "port": 2345, "scheme": "https", "verify": False, }, "pulp celerybeat": {}, "pulp resource manager": {}, "pulp workers": {}, "shell": { "transport": "ssh" }, "squid": {}, }, ), ]), )
def test_implicit_pulp_host(self): """Assert it is possible to implicitly target a pulp cli PulpHost.""" cfg = _get_pulp_smash_config(hosts=[ config.PulpHost(hostname=utils.uuid4(), roles={"shell": {}}), config.PulpHost(hostname=utils.uuid4(), roles={"shell": {}}), ]) 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.hosts[0].hostname)
def do_validate(self, cfg): """Validate the attributes of a configuration object.""" 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 pulp_selinux_enabled'): self.assertEqual(cfg.pulp_selinux_enabled, True) with self.subTest('check hosts'): self.assertEqual( sorted(cfg.hosts), sorted([ config.PulpHost(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.PulpHost(hostname='second.example.com', roles={ 'api': { 'port': 2345, 'scheme': 'https', 'verify': False, }, 'pulp celerybeat': {}, 'pulp resource manager': {}, 'pulp workers': {}, 'shell': { 'transport': 'ssh' }, 'squid': {} }), ]))
def test_run_as_sudo(self): """Test run commands as sudo.""" cfg = _get_pulp_smash_config(hosts=[ config.PulpHost( hostname=socket.getfqdn(), roles={'shell': {}}, ) ]) client = cli.Client(cfg) with mock.patch.object(client, 'machine') as machine: machine.__getitem__.return_value = machine machine.run.return_value = (0, 'ok', None) result = client.run(('ls', '-la'), sudo=True) # Internal call is: `machine[args[0]].run(args[1:], **kwargs)` # So assert `machine['sudo']` is called machine.__getitem__.assert_called_once_with('sudo') # then `.run(('ls', '-la'), **kwargs)` machine.run.assert_called_once_with(('ls', '-la'), retcode=None) self.assertEqual(result.returncode, 0) self.assertEqual(result.stdout, 'ok') self.assertIsNone(result.stderr)
def test_implicit_local_transport(self): """Assert it is possible to implicitly ask for a "local" transport.""" cfg = _get_pulp_smash_config(hosts=[ config.PulpHost( hostname=socket.getfqdn(), roles={'shell': {}}, ) ]) self.assertIsInstance(cli.Client(cfg).machine, LocalMachine)
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)
def setUpClass(cls): """Set common cfg for all tests.""" cls.cfg = _get_pulp_smash_config(hosts=[ config.PulpHost( hostname=socket.getfqdn(), roles={ "shell": {}, "api": { "scheme": "https" } }, ) ])
def setUpClass(cls): """Set common cfg for all tests.""" cls.cfg = _get_pulp_smash_config(hosts=[ config.PulpHost( hostname=socket.getfqdn(), roles={ 'shell': {}, 'api': { 'scheme': 'https' } }, ) ])
def test_explicit_local_transport(self): """Assert it is possible to explicitly ask for a "local" transport.""" cfg = _get_pulp_smash_config(hosts=[ config.PulpHost( hostname=utils.uuid4(), roles={ 'pulp cli': {}, 'shell': { 'transport': 'local' } }, ) ]) self.assertIsInstance(cli.Client(cfg).machine, LocalMachine)
def test_default_response_handler(self): """Assert the default response handler checks return codes.""" cfg = _get_pulp_smash_config(hosts=[ config.PulpHost( hostname=utils.uuid4(), roles={ 'pulp cli': {}, 'shell': { 'transport': 'local' } }, ) ]) self.assertIs(cli.Client(cfg).response_handler, cli.code_handler)
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)
def test_explicit_response_handler(self): """Assert it is possible to explicitly set a response handler.""" cfg = _get_pulp_smash_config(hosts=[ config.PulpHost( hostname=utils.uuid4(), roles={ 'pulp cli': {}, 'shell': { 'transport': 'local' } }, ) ]) handler = mock.Mock() self.assertIs(cli.Client(cfg, handler).response_handler, handler)
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' }}, ) ])
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)
def _gen_attrs(): """Generate attributes for populating a ``PulpSmashConfig``. Example usage: ``PulpSmashConfig(**_gen_attrs())``. :returns: A dict. It populates all attributes in a ``PulpSmashConfig``. """ return { "pulp_auth": [utils.uuid4() for _ in range(2)], "pulp_version": ".".join(str(random.randint(1, 150)) for _ in range(4)), "timeout": random.randint(1, 1800), "custom": {}, "pulp_selinux_enabled": True, "hosts": [ config.PulpHost( hostname="pulp.example.com", roles={ "amqp broker": { "service": "qpidd" }, "api": { "port": random.randint(1, 65535), "scheme": "https", "verify": True, }, "mongod": {}, "pulp cli": {}, "pulp celerybeat": {}, "pulp resource manager": {}, "pulp workers": {}, "shell": { "transport": "local" }, "squid": {}, }, ) ], }
def test_run(self): """Test run commands.""" cfg = _get_pulp_smash_config(hosts=[ config.PulpHost(hostname=socket.getfqdn(), roles={"shell": {}}) ]) client = cli.Client(cfg) with mock.patch.object(client, "_machine") as machine: machine.__getitem__.return_value = machine machine.run.return_value = (0, "ok", None) result = client.run(("ls", "-la")) # Internal call is: `machine[args[0]].run(args[1:], **kwargs)` # So assert `machine['ls']` is called machine.__getitem__.assert_called_once_with("ls") # then `.run(('-la',), **kwargs)` machine.run.assert_called_once_with(("-la", ), retcode=None) self.assertEqual(result.returncode, 0) self.assertEqual(result.stdout, "ok") self.assertIsNone(result.stderr)
def _gen_attrs(): """Generate attributes for populating a ``PulpSmashConfig``. Example usage: ``PulpSmashConfig(**_gen_attrs())``. :returns: A dict. It populates all attributes in a ``PulpSmashConfig``. """ return { 'pulp_auth': [utils.uuid4() for _ in range(2)], 'pulp_version': '.'.join(str(random.randint(1, 150)) for _ in range(4)), 'pulp_selinux_enabled': True, 'hosts': [ config.PulpHost(hostname='pulp.example.com', roles={ 'amqp broker': { 'service': 'qpidd' }, 'api': { 'port': random.randint(1, 65535), 'scheme': 'https', 'verify': True }, 'mongod': {}, 'pulp cli': {}, 'pulp celerybeat': {}, 'pulp resource manager': {}, 'pulp workers': {}, 'shell': { 'transport': 'local' }, 'squid': {} }) ], }