Skip to content

Commit

Permalink
Drop use_sudo attribute on IntegrationInstance
Browse files Browse the repository at this point in the history
Since pycloudlib LXD instance will stop running
all commands as root by default, we can make
all of the execute commands to run as sudo by
default. This is already the behavior for every
other cloud except LXD
  • Loading branch information
lucasmoura committed Nov 26, 2020
1 parent 6ee0107 commit 3b7f30b
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 80 deletions.
29 changes: 12 additions & 17 deletions tests/integration_tests/instances.py
Expand Up @@ -26,8 +26,6 @@ def _get_tmp_path():


class IntegrationInstance:
use_sudo = True

def __init__(self, cloud: 'IntegrationCloud', instance: BaseInstance,
settings=integration_settings):
self.cloud = cloud
Expand All @@ -37,11 +35,9 @@ def __init__(self, cloud: 'IntegrationCloud', instance: BaseInstance,
def destroy(self):
self.instance.delete()

def execute(self, command, *, use_sudo=None) -> Result:
def execute(self, command, *, use_sudo=True) -> Result:
if self.instance.username == 'root' and use_sudo is False:
raise Exception('Root user cannot run unprivileged')
if use_sudo is None:
use_sudo = self.use_sudo
return self.instance.execute(command, use_sudo=use_sudo)

def pull_file(self, remote_path, local_path):
Expand Down Expand Up @@ -97,21 +93,21 @@ def _install_new_cloud_init(self, remote_script):
def install_proposed_image(self):
log.info('Installing proposed image')
remote_script = (
'{sudo} echo deb "http://archive.ubuntu.com/ubuntu '
'echo deb "http://archive.ubuntu.com/ubuntu '
'$(lsb_release -sc)-proposed main" | '
'{sudo} tee /etc/apt/sources.list.d/proposed.list\n'
'{sudo} apt-get update -q\n'
'{sudo} apt-get install -qy cloud-init'
).format(sudo='sudo' if self.use_sudo else '')
'tee /etc/apt/sources.list.d/proposed.list\n'
'apt-get update -q\n'
'apt-get install -qy cloud-init'
)
self._install_new_cloud_init(remote_script)

def install_ppa(self, repo):
log.info('Installing PPA')
remote_script = (
'{sudo} add-apt-repository {repo} -y && '
'{sudo} apt-get update -q && '
'{sudo} apt-get install -qy cloud-init'
).format(sudo='sudo' if self.use_sudo else '', repo=repo)
'add-apt-repository {repo} -y && '
'apt-get update -q && '
'apt-get install -qy cloud-init'
).format(repo=repo)
self._install_new_cloud_init(remote_script)

def install_deb(self):
Expand All @@ -122,8 +118,7 @@ def install_deb(self):
self.push_file(
local_path=integration_settings.CLOUD_INIT_SOURCE,
remote_path=remote_path)
remote_script = '{sudo} dpkg -i {path}'.format(
sudo='sudo' if self.use_sudo else '', path=remote_path)
remote_script = 'dpkg -i {path}'.format(path=remote_path)
self._install_new_cloud_init(remote_script)

def __enter__(self):
Expand Down Expand Up @@ -151,4 +146,4 @@ class IntegrationOciInstance(IntegrationInstance):


class IntegrationLxdInstance(IntegrationInstance):
use_sudo = False
pass
138 changes: 75 additions & 63 deletions tests/integration_tests/modules/test_ssh_keys_provided.py
Expand Up @@ -83,66 +83,78 @@
@pytest.mark.user_data(USER_DATA)
class TestSshKeysProvided:

def test_ssh_dsa_keys_provided(self, class_client):
"""Test dsa public key was imported."""
out = class_client.read_from_file("/etc/ssh/ssh_host_dsa_key.pub")
assert (
"AAAAB3NzaC1kc3MAAACBAPkWy1zbchVIN7qTgM0/yyY8q4R"
"ZS8cNM4ZpeuE5UB/Nnr6OSU/nmbO8LuM") in out

"""Test dsa private key was imported."""
out = class_client.read_from_file("/etc/ssh/ssh_host_dsa_key")
assert (
"MIIBuwIBAAKBgQD5Fstc23IVSDe6k4DNP8smPKuEWUvHDTOGaXr"
"hOVAfzZ6+jklP") in out

def test_ssh_rsa_keys_provided(self, class_client):
"""Test rsa public key was imported."""
out = class_client.read_from_file("/etc/ssh/ssh_host_rsa_key.pub")
assert (
"AAAAB3NzaC1yc2EAAAADAQABAAABAQC0/Ho+o3eJISydO2JvIgT"
"LnZOtrxPl+fSvJfKDjoOLY0HB2eOjy2s2/2N6d9X9SGZ4") in out

"""Test rsa private key was imported."""
out = class_client.read_from_file("/etc/ssh/ssh_host_rsa_key")
assert (
"4DOkqNiUGl80Zp1RgZNohHUXlJMtAbrIlAVEk+mTmg7vjfyp2un"
"RQvLZpMRdywBm") in out

def test_ssh_rsa_certificate_provided(self, class_client):
"""Test rsa certificate was imported."""
out = class_client.read_from_file("/etc/ssh/ssh_host_rsa_key-cert.pub")
assert (
"AAAAHHNzaC1yc2EtY2VydC12MDFAb3BlbnNzaC5jb20AAAAgMpg"
"BP4Phn3L8I7Vqh7lmHKcOfIokEvSEbHDw83Y3JloAAAAD") in out

def test_ssh_certificate_updated_sshd_config(self, class_client):
"""Test ssh certificate was added to /etc/ssh/sshd_config."""
out = class_client.read_from_file("/etc/ssh/sshd_config").strip()
assert "HostCertificate /etc/ssh/ssh_host_rsa_key-cert.pub" in out

def test_ssh_ecdsa_keys_provided(self, class_client):
"""Test ecdsa public key was imported."""
out = class_client.read_from_file("/etc/ssh/ssh_host_ecdsa_key.pub")
assert (
"AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAAB"
"BBFsS5Tvky/IC/dXhE/afxxU") in out

"""Test ecdsa private key generated."""
out = class_client.read_from_file("/etc/ssh/ssh_host_ecdsa_key")
assert (
"AwEHoUQDQgAEWxLlO+TL8gL91eET9p/HFQbqR1A691AkJgZk3jY"
"5mpZqxgX4vcgb") in out

def test_ssh_ed25519_keys_provided(self, class_client):
"""Test ed25519 public key was imported."""
out = class_client.read_from_file("/etc/ssh/ssh_host_ed25519_key.pub")
assert (
"AAAAC3NzaC1lZDI1NTE5AAAAINudAZSu4vjZpVWzId5pXmZg1M6"
"G15dqjQ2XkNVOEnb5") in out

"""Test ed25519 private key was imported."""
out = class_client.read_from_file("/etc/ssh/ssh_host_ed25519_key")
assert (
"XAAAAAtzc2gtZWQyNTUxOQAAACDbnQGUruL42aVVsyHeaV5mYNT"
"OhteXao0Nl5DVThJ2+Q") in out
@pytest.mark.parametrize(
"config_path,expected_out",
(
(
"/etc/ssh/ssh_host_dsa_key.pub",
(
"AAAAB3NzaC1kc3MAAACBAPkWy1zbchVIN7qTgM0/yyY8q4R"
"ZS8cNM4ZpeuE5UB/Nnr6OSU/nmbO8LuM"
),
),
(
"/etc/ssh/ssh_host_dsa_key",
(
"MIIBuwIBAAKBgQD5Fstc23IVSDe6k4DNP8smPKuEWUvHDTOGaXr"
"hOVAfzZ6+jklP"
),
),
(
"/etc/ssh/ssh_host_rsa_key.pub",
(
"AAAAB3NzaC1yc2EAAAADAQABAAABAQC0/Ho+o3eJISydO2JvIgT"
"LnZOtrxPl+fSvJfKDjoOLY0HB2eOjy2s2/2N6d9X9SGZ4"
),
),
(
"/etc/ssh/ssh_host_rsa_key",
(
"4DOkqNiUGl80Zp1RgZNohHUXlJMtAbrIlAVEk+mTmg7vjfyp2un"
"RQvLZpMRdywBm"
),
),
(
"/etc/ssh/ssh_host_rsa_key-cert.pub",
(
"AAAAHHNzaC1yc2EtY2VydC12MDFAb3BlbnNzaC5jb20AAAAgMpg"
"BP4Phn3L8I7Vqh7lmHKcOfIokEvSEbHDw83Y3JloAAAAD"
),
),
(
"/etc/ssh/sshd_config",
"HostCertificate /etc/ssh/ssh_host_rsa_key-cert.pub",
),
(
"/etc/ssh/ssh_host_ecdsa_key.pub",
(
"AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAAB"
"BBFsS5Tvky/IC/dXhE/afxxU"
),
),
(
"/etc/ssh/ssh_host_ecdsa_key",
(
"AwEHoUQDQgAEWxLlO+TL8gL91eET9p/HFQbqR1A691AkJgZk3jY"
"5mpZqxgX4vcgb"
),
),
(
"/etc/ssh/ssh_host_ed25519_key.pub",
(
"AAAAC3NzaC1lZDI1NTE5AAAAINudAZSu4vjZpVWzId5pXmZg1M6"
"G15dqjQ2XkNVOEnb5"
),
),
(
"/etc/ssh/ssh_host_ed25519_key",
(
"XAAAAAtzc2gtZWQyNTUxOQAAACDbnQGUruL42aVVsyHeaV5mYNT"
"OhteXao0Nl5DVThJ2+Q"
),
),
)
)
def test_ssh_provided_keys(self, config_path, expected_out, class_client):
out = class_client.read_from_file(config_path).strip()
assert expected_out in out

0 comments on commit 3b7f30b

Please sign in to comment.