Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
Signed-off-by: paulober <[email protected]>
  • Loading branch information
paulober committed Oct 16, 2024
1 parent 6471702 commit bf9ab5e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 37 deletions.
23 changes: 11 additions & 12 deletions tests/unittests/config/test_cc_rpi_connect.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# This file is part of cloud-init. See LICENSE file for license information.

import os
from unittest import mock
from cloudinit.config.cc_rpi_connect import handle, ENABLE_RPI_CONNECT_KEY, configure_rpi_connect
from cloudinit.config.cc_rpi_connect import ENABLE_RPI_CONNECT_KEY
from cloudinit.config.schema import validate_cloudconfig_schema, get_schema
from cloudinit.config.schema import SchemaValidationError
from tests.unittests.helpers import CiTestCase, mock, skipUnlessJsonSchema
from tests.unittests.helpers import skipUnlessJsonSchema
import pytest


Expand All @@ -16,27 +14,26 @@ def is_notPi() -> bool:
@mock.patch('cloudinit.subp.subp')
class TestCCRPiConnect(CiTestCase):
\"""Tests work in progress. Just partially implemented to show the idea.\"""
\"""Tests work in progress. Just partially implemented to
show the idea.\"""
@mock.patch('cloudinit.subp.subp')
def test_configure_rpi_connect_enabled(self, mock_subp):
if is_notPi():
return
config = {ENABLE_RPI_CONNECT_KEY: True}
handle("cc_rpi_connect", config, mock.Mock(), [])
mock_subp.assert_called_with(['/usr/bin/raspi-config', 'do_rpi_connect', '0'])
mock_subp.assert_called_with(
['/usr/bin/raspi-config', 'do_rpi_connect', '0'])
@mock.patch('cloudinit.subp.subp')
def test_configure_rpi_connect_disabled(self, mock_subp):
if is_notPi():
return
config = {ENABLE_RPI_CONNECT_KEY: False}
handle("cc_rpi_connect", config, mock.Mock(), [])
mock_subp.assert_called_with(['/usr/bin/raspi-config', 'do_rpi_connect', '1'])
def test_schema_validation(self):
config = {ENABLE_RPI_CONNECT_KEY: True}
validate_cloudconfig_schema({"cc_rpi_connect": config}, get_schema(), strict=True)
mock_subp.assert_called_with(
['/usr/bin/raspi-config', 'do_rpi_connect', '1'])
@mock.patch('os.path.exists')
def test_rpi_connect_installed(self, mock_path_exists):
Expand All @@ -62,7 +59,9 @@ class TestCCRPiConnectSchema:
"config, error_msg",
[
({ENABLE_RPI_CONNECT_KEY: True}, None),
({ENABLE_RPI_CONNECT_KEY: "true"}, "'true' is not of type 'boolean'")
({
ENABLE_RPI_CONNECT_KEY: "true"
}, "'true' is not of type 'boolean'")
],
)
def test_schema_validation(self, config, error_msg):
Expand Down
49 changes: 35 additions & 14 deletions tests/unittests/config/test_cc_rpi_interfaces.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# This file is part of cloud-init. See LICENSE file for license information.

import os
from unittest import mock
from cloudinit.config.cc_rpi_interfaces import handle, SUPPORTED_INTERFACES, RPI_INTERFACES_KEY
from cloudinit.config.cc_rpi_interfaces import RPI_INTERFACES_KEY
from cloudinit.config.schema import validate_cloudconfig_schema, get_schema
from cloudinit.config.schema import SchemaValidationError
from tests.unittests.helpers import CiTestCase, mock, skipUnlessJsonSchema
from tests.unittests.helpers import skipUnlessJsonSchema
import pytest


Expand All @@ -16,7 +14,8 @@ def is_notPi() -> bool:
@mock.patch('cloudinit.subp.subp')
class TestCCRPiInterfaces(CiTestCase):
\"""Tests work in progress. Just partially implemented to show the idea.\"""
\"""Tests work in progress. Just partially implemented to
show the idea.\"""
@mock.patch('cloudinit.subp.subp')
def test_configure_spi_interface(self, mock_subp):
Expand All @@ -29,7 +28,10 @@ def test_configure_spi_interface(self, mock_subp):
}
handle("cc_rpi_interfaces", config, mock.Mock(), [])
mock_subp.assert_called_with([
'/usr/bin/raspi-config', 'nonint', SUPPORTED_INTERFACES["spi"], '0'])
'/usr/bin/raspi-config',
'nonint',
SUPPORTED_INTERFACES["spi"],
'0'])
@mock.patch('cloudinit.subp.subp')
def test_configure_serial_interface_as_dict(self, mock_subp):
Expand Down Expand Up @@ -76,10 +78,14 @@ def test_get_enabled_interfaces(self, mock_subp, mock_path_exists):
}
handle("cc_rpi_interfaces", config, mock.Mock(), [])
# Assert all interface enabling commands were called
mock_subp.assert_any_call(['/usr/bin/raspi-config', 'nonint', 'do_spi', '0'])
mock_subp.assert_any_call(['/usr/bin/raspi-config', 'nonint', 'do_i2c', '0'])
mock_subp.assert_any_call(['/usr/bin/raspi-config', 'nonint', 'do_onewire', '0'])
mock_subp.assert_any_call(['/usr/bin/raspi-config', 'nonint', 'do_rgpio', '0'])
mock_subp.assert_any_call(['/usr/bin/raspi-config',
'nonint', 'do_spi', '0'])
mock_subp.assert_any_call(['/usr/bin/raspi-config',
'nonint', 'do_i2c', '0'])
mock_subp.assert_any_call(['/usr/bin/raspi-config',
'nonint', 'do_onewire', '0'])
mock_subp.assert_any_call(['/usr/bin/raspi-config',
'nonint', 'do_rgpio', '0'])
"""


Expand All @@ -89,14 +95,29 @@ class TestCCRPiInterfacesSchema:
"config, error_msg",
[
({RPI_INTERFACES_KEY: {"spi": True, "i2c": False}}, None),
({RPI_INTERFACES_KEY: {"spi": "true"}}, "'true' is not of type 'boolean'"),
({RPI_INTERFACES_KEY: {"serial": {"console": True, "hardware": False}}}, None),
({RPI_INTERFACES_KEY: {"serial": {"console": 123}}}, "123 is not of type 'boolean'")
({
RPI_INTERFACES_KEY: {"spi": "true"}
}, "'true' is not of type 'boolean'"),
({
RPI_INTERFACES_KEY: {
"serial": {
"console": True,
"hardware": False
}
}
}, None),
({
RPI_INTERFACES_KEY: {
"serial": {
"console": 123
}
}
}, "123 is not of type 'boolean'")
],
)
def test_schema_validation(self, config, error_msg):
if error_msg is None:
validate_cloudconfig_schema(config, get_schema(), strict=True)
else:
with pytest.raises(SchemaValidationError, match=error_msg):
validate_cloudconfig_schema(config, get_schema(), strict=True)
validate_cloudconfig_schema(config, get_schema(), strict=True)
21 changes: 12 additions & 9 deletions tests/unittests/config/test_cc_rpi_userdata.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
# This file is part of cloud-init. See LICENSE file for license information.

import os
import pytest
from unittest import mock
from cloudinit.config.cc_rpi_userdata import DISABLE_PIWIZ_KEY, RPI_USERCONF_KEY, run_service
from cloudinit.config.schema import validate_cloudconfig_schema, get_schema
from cloudinit.config.cc_rpi_userdata import DISABLE_PIWIZ_KEY, RPI_USERCONF_KEY
from cloudinit.config.schema import (
SchemaValidationError,
get_schema,
validate_cloudconfig_schema,
)
from tests.unittests.helpers import CiTestCase, mock, skipUnlessJsonSchema
from tests.unittests.helpers import skipUnlessJsonSchema

"""
def is_notPi() -> bool:
Expand All @@ -19,7 +16,8 @@ def is_notPi() -> bool:
@mock.patch('cloudinit.subp.subp')
class TestCCRPiUserdata(CiTestCase):
\"""Tests work in progress. Just partially implemented to show the idea.\"""
\"""Tests work in progress. Just partially implemented
to show the idea.\"""
@mock.patch('subprocess.run')
def test_userconf_service_runs(self, mock_run):
Expand Down Expand Up @@ -67,9 +65,15 @@ class TestCCRPiUserdataSchema:
"config, error_msg",
[
({DISABLE_PIWIZ_KEY: True}, None),
({RPI_USERCONF_KEY: {"password": "hashedpassword", "user": "newuser"}}, None),
({
RPI_USERCONF_KEY: {
"password": "hashedpassword", "user": "newuser"
}
}, None),
({DISABLE_PIWIZ_KEY: "true"}, "'true' is not of type 'boolean'"),
({RPI_USERCONF_KEY: {"password": 12345}}, "12345 is not of type 'string'")
({
RPI_USERCONF_KEY: {"password": 12345}
}, "12345 is not of type 'string'")
],
)
def test_schema_validation(self, config, error_msg):
Expand All @@ -78,4 +82,3 @@ def test_schema_validation(self, config, error_msg):
else:
with pytest.raises(SchemaValidationError, match=error_msg):
validate_cloudconfig_schema(config, get_schema(), strict=True)

3 changes: 2 additions & 1 deletion tests/unittests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ def test_wb_schema_subcommand_parser(self, m_read_cfg, capsys):
"centos, cloudlinux, cos, debian, eurolinux, fedora, "
"freebsd, mariner, miraclelinux, openbsd, openeuler, "
"OpenCloudOS, openmandriva, opensuse, opensuse-microos, "
"opensuse-tumbleweed, opensuse-leap, photon, raspberry-pi-os, rhel, rocky, "
"opensuse-tumbleweed, opensuse-leap, photon, "
"raspberry-pi-os, rhel, rocky, "
"sle_hpc, sle-micro, sles, TencentOS, ubuntu, virtuozzo",
" **resize_rootfs:** ",
"(``true``/``false``/``noblock``)",
Expand Down
5 changes: 4 additions & 1 deletion tests/unittests/test_render_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ def test_variant_sets_default_user_in_cloud_cfg(self, variant, tmpdir):
("netbsd", ["netbsd"]),
("openbsd", ["openbsd"]),
("ubuntu", ["netplan", "eni", "sysconfig"]),
("raspberry-pi-os", ["netplan", "network-manager", "networkd", "eni"])
(
"raspberry-pi-os",
["netplan", "network-manager", "networkd", "eni"]
)
),
)
def test_variant_sets_network_renderer_priority_in_cloud_cfg(
Expand Down

0 comments on commit bf9ab5e

Please sign in to comment.