Housekeeping and polish based on test discoveries

This commit is contained in:
Cian Hughes
2023-11-03 16:52:12 +00:00
parent e279c23745
commit 2f452892b0
22 changed files with 163 additions and 185 deletions

View File

View File

@@ -2,7 +2,8 @@
"login": {
"users": [
{
"name": "root"
"name": "root",
"passwd": ""
}
]
},

View File

@@ -0,0 +1,11 @@
[create_img.create_img]
hostname = "test_hostname"
password = ""
switch_ip = "192.168.1.1"
switch_port = 42
swarm_token = "SWMTKN-1-THISISATESTSWARMTOKENFORTESTINGPURPOSESANDTHATMEANSITNEEDSTOBEQUITELONG"
[create_img.apply_ignition_settings]
hostname = "test_hostname"
password = ""
swarm_config = {SWITCH_IP_ADDRESS="192.168.1.1", SWITCH_PORT=42, SWARM_TOKEN="SWMTKN-1-THISISATESTSWARMTOKENFORTESTINGPURPOSESANDTHATMEANSITNEEDSTOBEQUITELONG"}

View File

@@ -0,0 +1,88 @@
import atexit
import filecmp
from pathlib import Path
import pickle
import shutil
from node_deployer.config import config
import tomllib
config.update_config("test")
config.BUILD_DIR = config.BUILD_DIR / "tests"
atexit.register(lambda: shutil.rmtree(config.BUILD_DIR, ignore_errors=True))
from node_deployer import autoignition, create_img # noqa: E402
with open(config.PROJECT_ROOT / "tests/data/node_deployer/test_args.toml", "rb") as f:
TEST_PARAMS = tomllib.load(f)
class TestAutoignition:
def test_json_to_img(self, tmp_path: Path):
tmp_path.mkdir(parents=True, exist_ok=True)
autoignition.json_to_img(
config.PROJECT_ROOT / "tests/data/node_deployer/fuelignition.json",
tmp_path / "ignition.img",
)
assert filecmp.cmp(
config.PROJECT_ROOT / "tests/data/node_deployer/ignition.img",
tmp_path / "ignition.img",
)
class TestCreateImg:
def test_load_template(self):
template = create_img.load_template()
with open(
config.PROJECT_ROOT / "tests/data/node_deployer/create_img/load_template.pkl", "rb"
) as f:
assert pickle.load(f) == template
def test_apply_ignition_settings(self):
with open(
config.PROJECT_ROOT / "tests/data/node_deployer/create_img/load_template.pkl",
mode="rb",
) as f:
template = pickle.load(f)
test_result = create_img.apply_ignition_settings(
template,
**TEST_PARAMS["create_img"]["apply_ignition_settings"],
)
with open(
config.PROJECT_ROOT / "tests/data/node_deployer/create_img/apply_ignition_settings.pkl",
mode="rb",
) as f:
expected = pickle.load(f)
assert expected == test_result
def test_create_img(self, tmp_path: Path):
tmp_path.mkdir(parents=True, exist_ok=True)
create_img.create_img(
**TEST_PARAMS["create_img"]["create_img"],
img_path=tmp_path / "ignition.img",
)
assert filecmp.cmp(
tmp_path / "ignition.img",
config.PROJECT_ROOT / "tests/data/node_deployer/ignition.img",
)
# class TestWriteDisk:
# def init(self):
# test_target = config.BUILD_DIR / "ignition.img"
# if not test_target.exists():
# test_target.write_bytes(
# Path(config.PROJECT_ROOT / "tests/data/node_deployer/ignition.img").read_bytes()
# )
# def test_validation_result(self):
# raise NotImplementedError
# def test_filter_validation_result(self):
# raise NotImplementedError
# def test_validate(self):
# raise NotImplementedError

View File

@@ -1,5 +0,0 @@
from node_deployer.config import config
config.update_config("test")
config.BUILD_DIR = config.BUILD_DIR / "tests"

View File

@@ -1,76 +0,0 @@
{
"ignition": {
"version": "3.2.0"
},
"passwd": {
"users": [
{
"name": "root",
"passwordHash": "$2a$08$YdxsvKFRc1q2S1lmVixm5Oel3Y2oCNAsx9Sh2Dx4pL/udApPzUQu6"
}
]
},
"storage": {
"files": [
{
"path": "/root/join_swarm.json",
"mode": 420,
"overwrite": true,
"contents": {
"source": "data:text/plain;charset=utf-8;base64,eyJTV0lUQ0hfSVBfQUREUkVTUyI6ICIxOTIuMTY4LjEuMSIsICJTV0lUQ0hfUE9SVCI6IDQyLCAiU1dBUk1fVE9LRU4iOiAiU1dNVEtOLTEtVEhJU0lTQVRFU1RTV0FSTVRPS0VORk9SVEVTVElOR1BVUlBPU0VTQU5EVEhBVE1FQU5TSVRORUVEU1RPQkVRVUlURUxPTkcifQ=="
}
},
{
"path": "/root/join_swarm.sh",
"mode": 420,
"overwrite": true,
"contents": {
"source": "data:text/plain;charset=utf-8;base64,IyEvYmluL2Jhc2gKCmlmIFtbICRFVUlEIC1uZSAwIF1dOyB0aGVuCiAgIGVjaG8gIlRoaXMgc2NyaXB0IG11c3QgYmUgcnVuIGFzIHJvb3QiIAogICBleGl0IDEKZmkKCiMgTG9hZCB0aGUgY29uZmlnIGZpbGUgaW50byB2YXJpYWJsZXMKZXZhbCAiJChqcSAtciAndG9fZW50cmllc1tdIHwgImV4cG9ydCBcKC5rZXkpPVwoLnZhbHVlIHwgQHNoKSInIC9yb290L2pvaW5fc3dhcm0uanNvbikiCgppZiBbWyAkKGRvY2tlciBpbmZvIHwgZ3JlcCBTd2FybSB8IGF3ayAne3ByaW50ICQyfScpID09ICJpbmFjdGl2ZSIgXV07IHRoZW4KICAgIGRvY2tlciBzd2FybSBqb2luIC0tdG9rZW4gJFNXQVJNX1RPS0VOIFskU1dJVENIX0lQX0FERFJFU1NdOiRTV0lUQ0hfUE9SVAplbHNlCiAgICBlY2hvICJUaGlzIG5vZGUgaXMgYWxyZWFkeSBwYXJ0IG9mIGEgc3dhcm0iCiAgICBkb2NrZXIgaW5mbyAtZiBqc29uIHwganEgLlN3YXJtCmZpCg=="
}
},
{
"path": "/etc/hostname",
"mode": 420,
"overwrite": true,
"contents": {
"source": "data:,test_hostname"
}
},
{
"path": "/etc/NetworkManager/system-connections/eth0.nmconnection",
"mode": 384,
"overwrite": true,
"contents": {
"source": "data:text/plain;charset=utf-8;base64,Cltjb25uZWN0aW9uXQppZD1ldGgwCnR5cGU9ZXRoZXJuZXQKaW50ZXJmYWNlLW5hbWU9ZXRoMAoKW2lwdjRdCmRucy1zZWFyY2g9Cm1ldGhvZD1hdXRvCgpbaXB2Nl0KZG5zLXNlYXJjaD0KYWRkci1nZW4tbW9kZT1ldWk2NAptZXRob2Q9YXV0bwo=",
"human_read": "\n[connection]\nid=eth0\ntype=ethernet\ninterface-name=eth0\n\n[ipv4]\ndns-search=\nmethod=auto\n\n[ipv6]\ndns-search=\naddr-gen-mode=eui64\nmethod=auto\n"
}
},
{
"path": "/etc/NetworkManager/conf.d/noauto.conf",
"mode": 420,
"overwrite": true,
"contents": {
"source": "data:text/plain;charset=utf-8;base64,W21haW5dCiMgRG8gbm90IGRvIGF1dG9tYXRpYyAoREhDUC9TTEFBQykgY29uZmlndXJhdGlvbiBvbiBldGhlcm5ldCBkZXZpY2VzCiMgd2l0aCBubyBvdGhlciBtYXRjaGluZyBjb25uZWN0aW9ucy4Kbm8tYXV0by1kZWZhdWx0PSoK",
"human_read": "[main]\n# Do not do automatic (DHCP/SLAAC) configuration on ethernet devices\n# with no other matching connections.\nno-auto-default=*\n"
}
}
]
},
"systemd": {
"units": [
{
"name": "cockpit.socket.service",
"enabled": true
},
{
"name": "docker.service",
"enabled": true
},
{
"name": "join_swarm.service",
"enabled": false,
"contents": "[Unit]\nDescription=Ensure that node joins a swarm on startup\n\n[Service]\nExecStart=/root/join_swarm.sh\n\n[Install]\nWantedBy=multi-user.target"
}
]
}
}

View File

@@ -1,16 +0,0 @@
import filecmp
from node_deployer import autoignition
from node_deployer.config import config
class TestAutoignition:
def test_json_to_img(self, tmpdir):
autoignition.json_to_img(
config.PROJECT_ROOT / "tests/test_node_deployer/data/fuelignition.json",
tmpdir / "ignition.img",
)
assert filecmp.cmp(
config.PROJECT_ROOT / "tests/test_node_deployer/data/ignition.img",
tmpdir / "ignition.img",
)

View File

@@ -1,50 +0,0 @@
import filecmp
import pickle
from node_deployer import create_img
from node_deployer.config import config
class TestCreateImg:
def test_load_template(self):
template = create_img.load_template()
with open(
config.PROJECT_ROOT / "tests/test_node_deployer/data/create_img/load_template.pkl", "rb"
) as f:
assert pickle.load(f) == template
def test_apply_ignition_settings(self):
with open(
config.PROJECT_ROOT / "tests/test_node_deployer/data/create_img/load_template.pkl",
mode="rb",
) as f:
template = pickle.load(f)
test_result = create_img.apply_ignition_settings(
template,
"test_hostname",
"",
{
"SWITCH_IP_ADDRESS": "192.168.1.1",
"SWITCH_PORT": 42,
"SWARM_TOKEN": "SWMTKN-1-THISISATESTSWARMTOKENFORTESTINGPURPOSESANDTHATMEANSITNEEDSTOBEQUITELONG", # noqa: E501
},
)
with open(
config.PROJECT_ROOT
/ "tests/test_node_deployer/data/create_img/apply_ignition_settings.pkl",
mode="rb",
) as f:
assert pickle.load(f) == test_result
def test_create_img(self, tmpdir):
create_img.create_img(
hostname="test_hostname",
password="",
switch_ip="192.168.1.1",
switch_port=42,
img_path=tmpdir / "ignition.img",
)
assert filecmp.cmp(
tmpdir / "ignition.img",
config.PROJECT_ROOT / "tests/test_node_deployer/data/ignition.img",
)