Skip to content

Commit 729452c

Browse files
tai271828dannf
authored andcommitted
test(pytest): bootstrap function test
1 parent 94289b5 commit 729452c

9 files changed

+314
-3
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
__pycache__
2+
dist

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ Today `ubuntu-server-netboot` needs to run on Ubuntu or another Debian derivativ
4747

4848
This script is tested with Ubuntu 18.04 ("bionic beaver") and above.
4949

50-
## Contribution
50+
## Contribution and Development
5151

5252
Please report bugs to [this github issue tracker](https://github.com/dannf/ubuntu-server-netboot/issues). The github templates including "Issue" and "Pull requests" are originally forked from [this "cookiecutter" templates for python](https://github.com/Lee-W/cookiecutter-python-template).
5353

54+
Place `pytest` to cover the basic test sets.

conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import sys
2+
3+
4+
# we would like to use the distro-info provided by system
5+
sys.path.append("/usr/lib/python3/dist-packages")

poetry.lock

Lines changed: 198 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ authors = ["dann frazier <[email protected]>", "Taihsiang Ho (tai271828) <taihsia
88
python = "^3.6.5"
99

1010
[tool.poetry.dev-dependencies]
11+
pytest = "^6.2.4"
1112

1213
[tool.poetry.scripts]
1314
ubuntu-server-netboot = "usn.usn:ubuntu_server_netboot"

usn/tests/__init__.py

Whitespace-only changes.

usn/tests/data/grub_expected.cfg

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
set menu_color_normal=white/black
2+
set menu_color_highlight=black/light-gray
3+
if background_color 44,0,30,0; then
4+
clear
5+
fi
6+
7+
insmod gzio
8+
9+
set timeout=30
10+
menuentry "Install Ubuntu Server" {
11+
set gfxpayload=keep
12+
linux /casper/vmlinuz quiet splash root=/dev/ram0 ramdisk_size=1500000 ip=dhcp url=http://cdimage.ubuntu.com/ubuntu/releases/20.04.2/release/ubuntu-20.04.2-live-server-arm64.iso autoinstall "ds=nocloud-net;s=http://12.34.56.78/" ---
13+
initrd /casper/initrd
14+
}
15+
menuentry 'Boot from next volume' {
16+
exit 1
17+
}
18+
menuentry 'UEFI Firmware Settings' {
19+
fwsetup
20+
}
21+
submenu 'Boot and Install with the HWE kernel' {
22+
menuentry "Install Ubuntu Server" {
23+
set gfxpayload=keep
24+
linux /casper/hwe-vmlinuz quiet splash root=/dev/ram0 ramdisk_size=1500000 ip=dhcp url=http://cdimage.ubuntu.com/ubuntu/releases/20.04.2/release/ubuntu-20.04.2-live-server-arm64.iso autoinstall "ds=nocloud-net;s=http://12.34.56.78/" ---
25+
initrd /casper/hwe-initrd
26+
}
27+
}
28+
29+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
set menu_color_normal=white/black
2+
set menu_color_highlight=black/light-gray
3+
if background_color 44,0,30,0; then
4+
clear
5+
fi
6+
7+
insmod gzio
8+
9+
set timeout=30
10+
menuentry "Install Ubuntu Server" {
11+
set gfxpayload=keep
12+
linux /casper/vmlinuz quiet splash ---
13+
initrd /casper/initrd
14+
}
15+
menuentry 'Boot from next volume' {
16+
exit 1
17+
}
18+
menuentry 'UEFI Firmware Settings' {
19+
fwsetup
20+
}
21+
submenu 'Boot and Install with the HWE kernel' {
22+
menuentry "Install Ubuntu Server" {
23+
set gfxpayload=keep
24+
linux /casper/hwe-vmlinuz quiet splash ---
25+
initrd /casper/hwe-initrd
26+
}
27+
}

usn/tests/test_grub_cfg.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import pytest
2+
3+
from pkg_resources import resource_filename
4+
from usn import ubuntu_server_netboot
5+
6+
7+
class TestGrubCfg:
8+
@pytest.fixture(scope="class")
9+
def grub_cfg_orig_filename(self):
10+
resource = "tests/data/grub_ubuntu-20.04.2-live-server-arm64.cfg"
11+
grub_cfg_orig_filename = resource_filename("usn", resource)
12+
13+
return grub_cfg_orig_filename
14+
15+
@pytest.fixture(scope="class")
16+
def grub_cfg_expected_filename(self):
17+
resource = "tests/data/grub_expected.cfg"
18+
grub_cfg_expected_filename = resource_filename("usn", resource)
19+
20+
return grub_cfg_expected_filename
21+
22+
def test_grub_cfg_ubuntu_20_4_2_live_server_arm64(
23+
self, grub_cfg_orig_filename, grub_cfg_expected_filename
24+
):
25+
"""
26+
Check the generated grub.cfg content
27+
28+
Compare the grub.cfg generated with the original grub.cfg extracted from the iso ubuntu 20.04.2 live server
29+
arm64.
30+
"""
31+
with open(grub_cfg_orig_filename, "r", encoding="utf-8") as grub_cfg_orig_f:
32+
bootloader_cfg = ubuntu_server_netboot.GrubConfig(grub_cfg_orig_f.read())
33+
34+
url = "http://cdimage.ubuntu.com/ubuntu/releases/20.04.2/release/ubuntu-20.04.2-live-server-arm64.iso"
35+
autoinstall_url = "http://12.34.56.78/"
36+
extra_args = ""
37+
ubuntu_server_netboot.setup_kernel_params(
38+
bootloader_cfg, url, autoinstall_url, extra_args
39+
)
40+
cfg_modified = bootloader_cfg.cfg
41+
42+
with open(
43+
grub_cfg_expected_filename, "r", encoding="utf-8"
44+
) as grub_cfg_expected_f:
45+
bootloader_cfg_expected = ubuntu_server_netboot.GrubConfig(
46+
grub_cfg_expected_f.read()
47+
)
48+
cfg_expected = bootloader_cfg_expected.cfg
49+
50+
assert cfg_modified == cfg_expected

0 commit comments

Comments
 (0)