Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests, implement attempted fix for ext2 root partitions #84

Merged
merged 6 commits into from
Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DEBIAN/control
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: system-installer
Version: 2.3.7
Version: 2.4.1
Maintainer: Thomas Castleman <[email protected]>
Homepage: https://github.com/drauger-os-development/system-installer
Section: admin
Expand Down
2 changes: 1 addition & 1 deletion usr/bin/system-installer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

using namespace std;

str VERSION = "2.3.7";
str VERSION = "2.4.1";
str R = "\033[0;31m";
str G = "\033[0;32m";
str Y = "\033[1;33m";
Expand Down
20 changes: 19 additions & 1 deletion usr/share/system-installer/auto_partitioner.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,13 @@ def get_min_root_size(swap=True, ram_size=False, ram_size_unit=True,
def check_disk_state():
"""Check disk state as registered with lsblk

Returns data as dictionary"""
Returns data as dictionary
"""
try:
subprocess.check_call(["partprobe"])
except subprocess.CalledProcessError:
print("`partprobe` failed. Provided info may not be up-to-date.")
time.sleep(0.1)
command = ["lsblk", "--json", "--paths", "--bytes", "--output",
"name,size,type,fstype"]
data = json.loads(subprocess.check_output(command))["blockdevices"]
Expand All @@ -232,6 +238,18 @@ def check_disk_state():
return data


def get_fs(part_name: str):
"""Get filesystem type for given partition"""
disk = check_disk_state()
for each in disk:
if each["name"] == part_name:
return each["fstype"]
if "children" in each:
for each1 in each["children"]:
if each1["name"] == part_name:
return each1["fstype"]


def __mkfs__(device, fs):
"""Set partition filesystem"""
# pre-define command
Expand Down
2 changes: 1 addition & 1 deletion usr/share/system-installer/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

def unique(starting_list):
"""Function to get a list down to only unique elements"""
# intilize a null list
# initialize a null list
unique_list = []
# traverse for all elements
for each in starting_list:
Expand Down
8 changes: 6 additions & 2 deletions usr/share/system-installer/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def install(settings, local_repo):
settings["EFI"] = partitioning["EFI"]
settings["HOME"] = partitioning["HOME"]
else:
if settings["EFI"] == "NULL":
if settings["EFI"] in ("NULL", None, "", False):
auto_partitioner.make_part_boot(settings["ROOT"])
else:
auto_partitioner.make_part_boot(settings["EFI"])
Expand Down Expand Up @@ -158,7 +158,11 @@ def install(settings, local_repo):
except FileNotFoundError:
pass
common.eprint(" ### EXTRACTING SQUASHFS ### ")
check_call(["unsquashfs", config["squashfs_Location"]])
cmd = ["unsquashfs", config["squashfs_Location"]]
# we're doing this as a tuple/list so that more can be added to this list later
if auto_partitioner.get_fs(settings["ROOT"]) in ("ext2",):
cmd.append("-no-xattrs")
check_call(cmd)
common.eprint(" ### EXTRACTION COMPLETE ### ")
file_list = os.listdir("/mnt/squashfs-root")
for each in file_list:
Expand Down
1 change: 1 addition & 0 deletions usr/share/system-installer/tests/auto_partitioner.py
1 change: 1 addition & 0 deletions usr/share/system-installer/tests/common.py
47 changes: 47 additions & 0 deletions usr/share/system-installer/tests/test_auto_partitioner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# test_auto_partitioner.py
#
# Copyright 2022 Thomas Castleman <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
"""Tests for auto_partitioner"""
import auto_partitioner as ap
import os

def test_get_min_root_size_with_swap_part():
"""Make sure our math is correct to get the min root size, assuming no swap partition"""
assert ap.get_min_root_size(swap=False) >= (ap.config["min root size"] * (1000 ** 2))
assert ap.get_min_root_size(swap=False, bytes=False) >= ap.bytes_to_gb(ap.config["min root size"] * (1000 ** 2))


def test_get_min_root_size_without_swap_part():
"""make sure our root part is big enough to accommodate a swap file"""
assert ap.get_min_root_size(ram_size=4, bytes=False) >= 29
assert ap.get_min_root_size(ram_size=6, bytes=False) >= 31.449
assert ap.get_min_root_size(ram_size=8, bytes=False) >= 33.828
assert ap.get_min_root_size(ram_size=12, bytes=False) >= 38.464
assert ap.get_min_root_size(ram_size=16, bytes=False) >= 43
assert ap.get_min_root_size(ram_size=24, bytes=False) >= 51.8989
assert ap.get_min_root_size(ram_size=64, bytes=False) >= 95


def test_is_EFI():
"""Check if the checks if a system is an EFI system are working"""
assert os.path.isdir("/boot/efi") == ap.is_EFI()
104 changes: 104 additions & 0 deletions usr/share/system-installer/tests/test_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# test_common.py
#
# Copyright 2022 Thomas Castleman <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
"""Test Common Library"""
import common
import random
import os
import string


def random_string(length=None):
"""Generate random string.

If "length" is None, use a random length, 1-100.
"""
# initializing size of string
if not isinstance(length, (int, float)):
N = random.randint(1,50)
else:
N = int(length)

# using random.choices()
# generating random strings
res = ''.join(random.choices(string.ascii_uppercase +
string.digits, k=N))

# return result
return str(res)


def test_real_num(count=0):
"""Ensure we calculate real numbers correctly"""
# set up
test_numbers = []
for each in range(10000):
if random.choice([True, False]):
# random float
test_numbers.append(random.randint(-100000000,100000000) + random.random())
else:
# random int
test_numbers.append(random.randint(-100000000,100000000))

# run the test
outputs = {}
for each in test_numbers:
outputs[each] = common.real_number(each)

# evaluate results
for each in outputs:
if each <= 0:
assert outputs[each] == 0
else:
assert isinstance(outputs[each], int)

# test again, if necessary
if count <= 200:
count += 1
test_real_num(count=count)


def test_recursive_mkdir():
"""Ensure recursive mkdir makes directories correctly and recurses
correctly
"""
for each in range(1000):
# generate random path in current directory, 1000 times over
path = []
for each1 in range(random.randint(2, 40)):
path.append(random_string())
path2 = "/".join(path)

# create random path
common.recursive_mkdir(path2)

# check if generated path exists
test = ""
for each in path:
if test == "":
test = each
else:
test = f"{ test }/{ each }"
assert os.path.isdir(test)
assert test == path2
os.removedirs(path2)