Skip to content

Commit

Permalink
Add more integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmoura committed Oct 16, 2020
1 parent 8766784 commit 7214b39
Show file tree
Hide file tree
Showing 10 changed files with 500 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tests/integration_tests/modules/test_runcmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Integration test for the runcmd module.
This test specifies a command to be executed by the ``runcmd`` module
and then checks if that command was executed during boot.
(This is ported from
``tests/cloud_tests/testcases/modules/runcmd.yaml``.)"""

import pytest


USER_DATA = """\
#cloud-config
runcmd:
- echo cloud-init run cmd test > /var/tmp/run_cmd
"""


class TestRuncmd:

@pytest.mark.user_data(USER_DATA)
def test_runcmd(self, client):
runcmd_output = client.read_from_file("/var/tmp/run_cmd")
assert runcmd_output.strip() == "cloud-init run cmd test"
27 changes: 27 additions & 0 deletions tests/integration_tests/modules/test_seed_random_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Integration test for the random seed module.
This test specifies a command to be executed by the ``seed_random`` module, by
providing a different data to be used as seed data. We will then check
if that seed data was actually used.
(This is ported from
``tests/cloud_tests/testcases/modules/seed_random_data.yaml``.)"""

import pytest


USER_DATA = """\
#cloud-config
random_seed:
data: 'MYUb34023nD:LFDK10913jk;dfnk:Df'
encoding: raw
file: /root/seed
"""


class TestSeedRandomData:

@pytest.mark.user_data(USER_DATA)
def test_seed_random_data(self, client):
seed_output = client.read_from_file("/root/seed")
assert seed_output.strip() == "MYUb34023nD:LFDK10913jk;dfnk:Df"
46 changes: 46 additions & 0 deletions tests/integration_tests/modules/test_set_hostname.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Integration test for the set_hostname module.
This module specify two tests: One updates only the hostname and the other
one updates the hostname and fqdn of the system. For both of these tests
we will check is the changes requested by the user data are being respected
after the system is boot.
(This is ported from
``tests/cloud_tests/testcases/modules/set_hostname.yaml`` and
``tests/cloud_tests/testcases/modules/set_hostname_fqdn.yaml``.)"""

import pytest


USER_DATA_HOSTNAME = """\
#cloud-config
hostname: cloudinit2
"""

USER_DATA_FQDN = """\
#cloud-config
manage_etc_hosts: true
hostname: cloudinit1
fqdn: cloudinit2.i9n.cloud-init.io
"""


class TestHostname:

@pytest.mark.user_data(USER_DATA_HOSTNAME)
def test_hostname(self, client):
hostname_output = client.execute("hostname")
assert "cloudinit2" in hostname_output.strip()

@pytest.mark.user_data(USER_DATA_FQDN)
def test_hostname_and_fqdn(self, client):
hostname_output = client.execute("hostname")
assert "cloudinit1" in hostname_output.strip()

fqdn_output = client.execute("hostname --fqdn")
assert "cloudinit2.i9n.cloud-init.io" in fqdn_output.strip()

host_output = client.execute("grep ^127 /etc/hosts")
assert '127.0.1.1 {} {}'.format(
fqdn_output, hostname_output) in host_output
assert '127.0.0.1 localhost' in host_output
28 changes: 28 additions & 0 deletions tests/integration_tests/modules/test_snap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Integration test for the snap module.
This test specifies a command to be executed by the ``snap`` module
and then checks that if that command was executed during boot.
(This is ported from
``tests/cloud_tests/testcases/modules/runcmd.yaml``.)"""

import pytest


USER_DATA = """\
#cloud-config
package_update: true
snap:
squashfuse_in_container: true
commands:
- snap install hello-world
"""


class TestSnap:

@pytest.mark.user_data(USER_DATA)
def test_snap(self, client):
snap_output = client.execute("snap list")
assert "core " in snap_output
assert "hello-world " in snap_output
47 changes: 47 additions & 0 deletions tests/integration_tests/modules/test_ssh_auth_key_fingerprints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""Integration test for the ssh_authkey_fingerprints module.
This modules specifies two test regarding the ``ssh_authkey_fingerprints``
module. The first one verifies that we can disable the module behavior while
the second one verifies if the module is working as expected if enabled.
(This is ported from
``tests/cloud_tests/testcases/modules/ssh_auth_key_fingerprints_disable.yaml``,
``tests/cloud_tests/testcases/modules/ssh_auth_key_fingerprints_enable.yaml``.
)"""
import re

import pytest


USER_DATA_SSH_AUTHKEY_DISABLE = """\
#cloud-config
no_ssh_fingerprints: true
"""

USER_DATA_SSH_AUTHKEY_ENABLE="""\
#cloud-config
ssh_genkeytypes:
- ecdsa
- ed25519
ssh_authorized_keys:
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDXW9Gg5H7ehjdSc6qDzwNtgCy94XYHhEYlXZMO2+FJrH3wfHGiMfCwOHxcOMt2QiXItULthdeQWS9QjBSSjVRXf6731igFrqPFyS9qBlOQ5D29C4HBXFnQggGVpBNJ82IRJv7szbbe/vpgLBP4kttUza9Dr4e1YM1ln4PRnjfXea6T0m+m1ixNb5432pTXlqYOnNOxSIm1gHgMLxPuDrJvQERDKrSiKSjIdyC9Jd8t2e1tkNLY0stmckVRbhShmcJvlyofHWbc2Ca1mmtP7MlS1VQnfLkvU1IrFwkmaQmaggX6WR6coRJ6XFXdWcq/AI2K6GjSnl1dnnCxE8VCEXBlXgFzad+PMSG4yiL5j8Oo1ZVpkTdgBnw4okGqTYCXyZg6X00As9IBNQfZMFlQXlIo4FiWgj3CO5QHQOyOX6FuEumaU13GnERrSSdp9tCs1Qm3/DG2RSCQBWTfcgMcStIvKqvJ3IjFn0vGLvI3Ampnq9q1SHwmmzAPSdzcMA76HyMUA5VWaBvWHlUxzIM6unxZASnwvuCzpywSEB5J2OF+p6H+cStJwQ32XwmOG8pLp1srlVWpqZI58Du/lzrkPqONphoZx0LDV86w7RUz1ksDzAdcm0tvmNRFMN1a0frDs506oA3aWK0oDk4Nmvk8sXGTYYw3iQSkOvDUUlIsqdaO+w==
""" # noqa


class TestSshAuthkeyFingerprints:

@pytest.mark.user_data(USER_DATA_SSH_AUTHKEY_DISABLE)
def test_ssh_authkey_fingerprints_disable(self, client):
cloudinit_output = client.read_from_file("/var/log/cloud-init.log")
assert (
"Skipping module named ssh-authkey-fingerprints, "
"logging of SSH fingerprints disabled") in cloudinit_output

@pytest.mark.user_data(USER_DATA_SSH_AUTHKEY_ENABLE)
def test_ssh_authkey_fingerprints_enable(self, client):
syslog_output = client.read_from_file("/var/log/syslog")

assert re.search(r'256 SHA256:.*(ECDSA)', syslog_output) is not None
assert re.search(r'256 SHA256:.*(ED25519)', syslog_output) is not None
assert re.search(r'1024 SHA256:.*(DSA)', syslog_output) is None
assert re.search(r'2048 SHA256:.*(RSA)', syslog_output) is None
66 changes: 66 additions & 0 deletions tests/integration_tests/modules/test_ssh_generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""Integration test for the ssh module.
This module has two tests to verify if we can create ssh keys
through the ``ssh`` module. The first test asserts that some keys
were not created while the second one verifies if the expected
keys were created.
(This is ported from
``tests/cloud_tests/testcases/modules/ssh_keys_generate.yaml``.)"""

import pytest


USER_DATA = """\
#cloud-config
ssh_genkeytypes:
- ecdsa
- ed25519
authkey_hash: sha512
"""


@pytest.mark.user_data(USER_DATA)
class TestSshKeysGenerate:

def test_ssh_keys_not_generated(self, class_client):
"""Test dsa public key not generated."""
out = class_client.execute(
"test -e /etc/ssh/ssh_host_dsa_key.pub"
)
assert out.failed

"""Test dsa private key not generated."""
out = class_client.execute(
"test -e /etc/ssh/ssh_host_dsa_key"
)
assert out.failed

"""Test rsa public key not generated."""
out = class_client.execute(
"test -e /etc/ssh/ssh_host_rsa_key.pub"
)
assert out.failed

"""Test rsa private key not generated."""
out = class_client.execute(
"test -e /etc/ssh/ssh_host_rsa_key"
)
assert out.failed

def test_ssh_keys_generated(self, class_client):
"""Test ecdsa public key generated."""
out = class_client.read_from_file("/etc/ssh/ssh_host_ecdsa_key.pub")
assert "" != out.strip()

"""Test ecdsa private key generated."""
out = class_client.read_from_file("/etc/ssh/ssh_host_ecdsa_key")
assert "" != out.strip()

"""Test ed25519 public key generated."""
out = class_client.read_from_file("/etc/ssh/ssh_host_ed25519_key.pub")
assert "" != out.strip()

"""Test ed25519 private key generated."""
out = class_client.read_from_file("/etc/ssh/ssh_host_ed25519_key")
assert "" != out.strip()
28 changes: 28 additions & 0 deletions tests/integration_tests/modules/test_ssh_import_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Integration test for the ssh_import_id module.
This test specifies ssh keys to be imported by the ``ssh_import_id`` module
and then checks that if the ssh keys were successfully imported.
(This is ported from
``tests/cloud_tests/testcases/modules/ssh_import_id.yaml``.)"""

import pytest


USER_DATA = """\
#cloud-config
ssh_import_id:
- gh:powersj
- lp:smoser
"""


class TestSshImportId:

@pytest.mark.user_data(USER_DATA)
def test_ssh_import_id(self, client):
ssh_output = client.read_from_file(
"/home/ubuntu/.ssh/authorized_keys")

assert '# ssh-import-id gh:powersj' in ssh_output
assert '# ssh-import-id lp:smoser' in ssh_output
Loading

0 comments on commit 7214b39

Please sign in to comment.