Verified Commit db1b9aab authored by Karel Koci's avatar Karel Koci 🤘
Browse files

selftests: add simple tests for cli shell

This is simple test on OpenWrt as well as on Alpine of nsfarm's Shell
abstraction.
parent cae250c2
#!/bin/sh
# images:openwrt/19.07/amd64
##################################################################################
# OpenWrt container we can use to test nsfarm as well as cooperation with Turris.
##################################################################################
set -e
# Make wait scripts executable
for script in wait4boot wait4network; do
chmod +x "/bin/$script"
done
wait4boot
# Configure the Internet access
uci set network.wan.ifname=internet
uci commit network
/etc/init.d/network reload
# Now extend system
wait4network
opkg update
opkg install bash coreutils-stty
#!/bin/sh
# This file is part of NSFarm
set -e
# First we have to wait for ubus to start
while ! pgrep -x /sbin/ubusd >/dev/null; do
sleep 1
done
# We have to wait for log to actually appear
ubus wait_for log
# Wait for procd to report completed init
while ! logread | grep -qF 'procd: - init complete -'; do
sleep 1
done
#!/bin/sh
# This file is part of NSFarm
# Call this script to block execution untill there is default route present.
while ! ip route | grep -q default; do
sleep 1
done
"""Shell class tests.
"""
import pytest
from nsfarm.cli import Shell
from nsfarm.lxd import Container
# pylint: disable=no-self-use
class Common:
"""Common tests implementation for Shell.
"""
@pytest.fixture(scope="function", autouse=True)
def shell(self, container):
return Shell(container.pexpect())
def test_true(self, shell):
"""Simple command that has no effect just to test full process match.
"""
shell.run("true")
def test_false(self, shell):
"""Simple command that has no effect but fails with known exit code.
"""
assert shell.run("false", None) == 1
class TestOpenWrt(Common):
"""These are tests in OpenWrt (ash) shell.
"""
@pytest.fixture(scope="class", autouse=True)
def container(self, lxd_connection):
with Container(lxd_connection, "openwrt") as container:
yield container
class TestAlpine(Common):
"""These are tests in Alpine Linux (ash) shell.
"""
@pytest.fixture(scope="class", autouse=True)
def container(self, lxd_connection):
with Container(lxd_connection, "base-alpine") as container:
yield container
import pytest
from nsfarm import lxd
@pytest.fixture(name="lxd_connection", scope="package")
def fixture_lxd_connection():
return lxd.LXDConnection()
import pytest
from nsfarm import lxd
@pytest.fixture(name="connection", scope="package")
def fixture_connection():
return lxd.LXDConnection()
......@@ -4,10 +4,10 @@ from nsfarm.lxd import Container, Image
from .test_image import BASE_IMG
def test_new_container(connection):
def test_new_container(lxd_connection):
"""Try to create container for BASE_IMG.
"""
container = Container(connection, BASE_IMG)
container = Container(lxd_connection, BASE_IMG)
assert isinstance(container.image, Image)
assert container.image.name == BASE_IMG
assert not container.image.wants_internet # Base image should not have Internet enabled as a baseline
......@@ -15,49 +15,49 @@ def test_new_container(connection):
assert container.devices == dict() # Base image has no devices assigned
def test_new_container_image(connection):
def test_new_container_image(lxd_connection):
"""Try to create container for BASE_IMG using Image instance.
"""
image = Image(connection, BASE_IMG)
container = Container(connection, image)
image = Image(lxd_connection, BASE_IMG)
container = Container(lxd_connection, image)
assert isinstance(container.image, Image)
assert container.image.name == BASE_IMG
assert container.image is image # This is intentionally 'is' as it should be the same instance
def test_start_stop(connection):
def test_start_stop(lxd_connection):
"""Prepare and cleanup container and check that it really creates and removes container.
"""
container = Container(connection, BASE_IMG)
container = Container(lxd_connection, BASE_IMG)
assert container.name is None
container.prepare()
assert container.name is not None
assert connection.local.containers.exists(container.name)
assert lxd_connection.local.containers.exists(container.name)
container.cleanup()
# It takes some time before it disappears but it should go away
for _ in range(10):
if not connection.local.containers.exists(container.name):
if not lxd_connection.local.containers.exists(container.name):
return
time.sleep(1)
assert not connection.local.containers.exists(container.name)
assert not lxd_connection.local.containers.exists(container.name)
def test_context(connection):
def test_context(lxd_connection):
"""Check if we can correctly work with context.
"""
with Container(connection, BASE_IMG) as container:
with Container(lxd_connection, BASE_IMG) as container:
container.prepare()
assert connection.local.containers.exists(container.name)
assert lxd_connection.local.containers.exists(container.name)
# It takes some time before it disappears but it should go away
for _ in range(10):
if not connection.local.containers.exists(container.name):
if not lxd_connection.local.containers.exists(container.name):
return
time.sleep(1)
assert not connection.local.containers.exists(container.name)
assert not lxd_connection.local.containers.exists(container.name)
# TODO add tests for enabled and disabled internet and for devices
......@@ -5,25 +5,25 @@ BASE_IMG = "base-alpine"
NOEX_IMG = "no-such-image"
def test_new_image(connection):
def test_new_image(lxd_connection):
"""Test public attributes set by Image.__init__
"""
img = Image(connection, BASE_IMG)
img = Image(lxd_connection, BASE_IMG)
assert img.name == BASE_IMG
assert img.lxd_image is None
def test_image_prepare(connection):
def test_image_prepare(lxd_connection):
"""Test image preparation and lxd_image attribute it should set.
"""
img = Image(connection, BASE_IMG)
img = Image(lxd_connection, BASE_IMG)
img.prepare()
assert img.lxd_image is not None
assert img.is_prepared()
def test_nonexisting_image(connection):
def test_nonexisting_image(lxd_connection):
"""Try to initialize Image for undefined image name.
"""
with pytest.raises(exceptions.LXDImageUndefinedError):
Image(connection, NOEX_IMG)
Image(lxd_connection, NOEX_IMG)
......@@ -7,10 +7,10 @@ from .test_image import BASE_IMG
@pytest.fixture(scope="module")
def container(connection):
def container(lxd_connection):
"""Base container to be used for testing.
"""
with Container(connection, BASE_IMG, internet=True) as cont:
with Container(lxd_connection, BASE_IMG, internet=True) as cont:
shell = Shell(cont.pexpect())
shell.run("wait4network")
yield shell
......
......@@ -6,21 +6,21 @@ from nsfarm import lxd
lxd.LXDConnection.ROOT_PROFILE,
lxd.LXDConnection.INTERNET_PROFILE,
])
def test_profiles_exists(connection, profile):
def test_profiles_exists(lxd_connection, profile):
"""Check that all profiles we need are configured in LXD.
"""
assert connection.local.profiles.exists(profile)
assert lxd_connection.local.profiles.exists(profile)
def test_profile_root(connection):
def test_profile_root(lxd_connection):
"""Minimal sanity check of root profile.
"""
profile = connection.local.profiles.get(lxd.LXDConnection.ROOT_PROFILE)
profile = lxd_connection.local.profiles.get(lxd.LXDConnection.ROOT_PROFILE)
assert any(dev for dev in profile.devices.values() if dev["type"] == "disk")
def test_profile_internet(connection):
def test_profile_internet(lxd_connection):
"""Minimal sanity check of internet profile.
"""
profile = connection.local.profiles.get(lxd.LXDConnection.INTERNET_PROFILE)
profile = lxd_connection.local.profiles.get(lxd.LXDConnection.INTERNET_PROFILE)
assert any(dev for dev in profile.devices.values() if dev["type"] == "nic" and dev["name"] == "internet")
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment