Skip to content

Commit

Permalink
Added more install script tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lamemakes committed Jul 10, 2023
1 parent 5ff8eed commit 08ac549
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 35 deletions.
19 changes: 7 additions & 12 deletions backend/pilot_drive/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import sys
import argparse
from enum import StrEnum
from typing import List, Dict, Optional
from typing import List, Dict, Literal, Optional
import requests

from pilot_drive.constants import (
Expand Down Expand Up @@ -279,8 +279,7 @@ def prompt_input(

user_in = ""
while re.search(regex_validator, user_in) is None:
user_in = input(
f"{Colors.BLUE}{Colors.BOLD}{prompt_str}: {Colors.ENDC}")
user_in = input(f"{Colors.BLUE}{Colors.BOLD}{prompt_str}: {Colors.ENDC}")

if re.search(regex_validator, user_in) is None:
print(
Expand All @@ -289,7 +288,7 @@ def prompt_input(

return user_in

def detect_distro_manager(self) -> DistroManagers:
def detect_distro_manager(self) -> Literal["yum", "apt"]:
"""
Detect the distribution manager used by the host
Expand Down Expand Up @@ -559,14 +558,12 @@ def install_ancs(self, settings: Dict) -> Dict:

# Enable & start the ANCS service
try:
self.exec_cmd(
f"{Cmd.SYSTEMCTL} enable ancs4linux-observer.service")
self.exec_cmd(f"{Cmd.SYSTEMCTL} enable ancs4linux-observer.service")
except FailedToExecuteCommandException:
pass

try:
self.exec_cmd(
f"{Cmd.SYSTEMCTL} enable ancs4linux-advertising.service")
self.exec_cmd(f"{Cmd.SYSTEMCTL} enable ancs4linux-advertising.service")
except FailedToExecuteCommandException:
pass

Expand All @@ -578,8 +575,7 @@ def install_ancs(self, settings: Dict) -> Dict:
pass

self.exec_cmd(f"{Cmd.SYSTEMCTL} restart ancs4linux-observer.service")
self.exec_cmd(
f"{Cmd.SYSTEMCTL} restart ancs4linux-advertising.service")
self.exec_cmd(f"{Cmd.SYSTEMCTL} restart ancs4linux-advertising.service")

print(f"{Colors.GREEN}Completed ANCS4Linux install!{Colors.ENDC}")

Expand Down Expand Up @@ -678,8 +674,7 @@ def for_production(self): # pylint: disable=too-many-branches
new_lxde_contents.append(line)
write_contents = True
else:
new_lxde_contents = lxde_contents.split(
"\n") + [f"{autostart_string}"]
new_lxde_contents = lxde_contents.split("\n") + [f"{autostart_string}"]
write_contents = True

if write_contents and len(new_lxde_contents) > 0:
Expand Down
21 changes: 7 additions & 14 deletions backend/pilot_drive/services/media/bluetooth_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ def position(self) -> int:
return self.bluetooth.bluez_media_player.Position
except DBusError as exc:
default = 0
self.__get_track_property_failed(
prop="position", exc=exc, default=default)
self.__get_track_property_failed(prop="position", exc=exc, default=default)
return default

@property
Expand All @@ -86,10 +85,8 @@ def track(self) -> Union[TrackProps, Dict[str, NoneType]]:
print(track)
except DBusError as exc:
print("GETTING TRACK FAILED")
default = {"Title": None, "Artist": None,
"Album": None, "Duration": None}
self.__get_track_property_failed(
prop="track", exc=exc, default=default)
default = {"Title": None, "Artist": None, "Album": None, "Duration": None}
self.__get_track_property_failed(prop="track", exc=exc, default=default)
return default

is_empty_track = not bool(
Expand Down Expand Up @@ -132,8 +129,7 @@ def playing(self) -> bool:
return self.bluetooth.bluez_media_player.Status == TrackStatus.PLAYING
except DBusError as exc:
default = False
self.__get_track_property_failed(
prop="playing", exc=exc, default=default)
self.__get_track_property_failed(prop="playing", exc=exc, default=default)
return default

@property
Expand Down Expand Up @@ -178,8 +174,7 @@ def __interfaces_added(
path=path, props_changed_callback=self.__properties_changed
)
self.__devices.append(bt_device)
bt_device.bluez_device.PropertiesChanged.connect(
bt_device.prop_changed)
bt_device.bluez_device.PropertiesChanged.connect(bt_device.prop_changed)

def __interfaces_removed(
self, path: ObjPath, interfaces: List[Str]
Expand Down Expand Up @@ -223,10 +218,8 @@ def __initialize_observers(self) -> None:
for path, services in self.bluetooth.bluez_root.GetManagedObjects().items():
self.__interfaces_added(path, services)

self.bluetooth.bluez_root.InterfacesAdded.connect(
self.__interfaces_added)
self.bluetooth.bluez_root.InterfacesRemoved.connect(
self.__interfaces_removed)
self.bluetooth.bluez_root.InterfacesAdded.connect(self.__interfaces_added)
self.bluetooth.bluez_root.InterfacesRemoved.connect(self.__interfaces_removed)
self.adapter.PropertiesChanged.connect(self.__properties_changed)

def main(self) -> None:
Expand Down
131 changes: 122 additions & 9 deletions backend/test/test_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,32 @@

from unittest.mock import MagicMock, patch

from pilot_drive.installer import Installer, FailedToExecuteCommandException
import pilot_drive.installer
from pilot_drive.installer import Installer

# Fixtures to create the installer with and without using default values


@pytest.fixture
def installer_not_default():
return Installer(use_default=False)


@pytest.fixture
def installer_default():
return Installer(use_default=True)

# Test that the use_default variable is properly set for both objects


def test_not_default(installer_not_default: Installer):
assert installer_not_default.use_default is False


def test_default(installer_default: Installer):
assert installer_default.use_default is True


@patch("pilot_drive.installer.subprocess.run")
def test_exec_cmd(mock_run, installer_default: Installer, installer_not_default: Installer):
test_str = 'yeet!'
Expand All @@ -39,9 +47,9 @@ def test_exec_cmd(mock_run, installer_default: Installer, installer_not_default:
result_nd = installer_not_default.exec_cmd('testing')
assert result_nd == test_str


@patch("pilot_drive.installer.subprocess.run")
def test_exec_cmd_exception(mock_run, installer_default: Installer, installer_not_default: Installer):
test_str = 'yeet!'
mock_stdout = MagicMock()
mock_stdout.configure_mock(
**{
Expand All @@ -52,20 +60,22 @@ def test_exec_cmd_exception(mock_run, installer_default: Installer, installer_no

mock_run.return_value = mock_stdout

with pytest.raises(FailedToExecuteCommandException):
with pytest.raises(pilot_drive.installer.FailedToExecuteCommandException):
installer_default.exec_cmd('testing')
with pytest.raises(FailedToExecuteCommandException):

with pytest.raises(pilot_drive.installer.FailedToExecuteCommandException):
installer_not_default.exec_cmd('testing')


def test_prompt_yes_no_yes(monkeypatch, installer_default: Installer, installer_not_default: Installer):
yes_val = "Y"
monkeypatch.setattr('builtins.input', lambda _: yes_val)

user_in = installer_default.prompt_yes_no(prompt="yeet?", default_in="y")
assert user_in is True

user_in = installer_not_default.prompt_yes_no(prompt="yeet?", default_in="y")
user_in = installer_not_default.prompt_yes_no(
prompt="yeet?", default_in="y")
assert user_in is True

user_in = installer_default.prompt_yes_no(prompt="yeet?", default_in="n")
Expand All @@ -74,14 +84,16 @@ def test_prompt_yes_no_yes(monkeypatch, installer_default: Installer, installer_
user_in = installer_default.prompt_yes_no(prompt="yeet?", default_in="n")
assert user_in is False


def test_prompt_yes_no_no(monkeypatch, installer_default: Installer, installer_not_default: Installer):
no_val = "N"
monkeypatch.setattr('builtins.input', lambda _: no_val)

user_in = installer_default.prompt_yes_no(prompt="yeet?", default_in="y")
assert user_in is False

user_in = installer_not_default.prompt_yes_no(prompt="yeet?", default_in="y")
user_in = installer_not_default.prompt_yes_no(
prompt="yeet?", default_in="y")
assert user_in is False

user_in = installer_default.prompt_yes_no(prompt="yeet?", default_in="n")
Expand All @@ -90,14 +102,16 @@ def test_prompt_yes_no_no(monkeypatch, installer_default: Installer, installer_n
user_in = installer_default.prompt_yes_no(prompt="yeet?", default_in="n")
assert user_in is True


def test_prompt_yes_no_default(monkeypatch, installer_default: Installer, installer_not_default: Installer):
default_val = ""
monkeypatch.setattr('builtins.input', lambda _: default_val)

user_in = installer_default.prompt_yes_no(prompt="yeet?", default_in="y")
assert user_in is True

user_in = installer_not_default.prompt_yes_no(prompt="yeet?", default_in="y")
user_in = installer_not_default.prompt_yes_no(
prompt="yeet?", default_in="y")
assert user_in is True

user_in = installer_default.prompt_yes_no(prompt="yeet?", default_in="n")
Expand All @@ -106,6 +120,7 @@ def test_prompt_yes_no_default(monkeypatch, installer_default: Installer, instal
user_in = installer_default.prompt_yes_no(prompt="yeet?", default_in="n")
assert user_in is True


def test_prompt_yes_no_exception(monkeypatch, installer_default: Installer, installer_not_default: Installer):
no_val = "N"
monkeypatch.setattr('builtins.input', lambda _: no_val)
Expand All @@ -114,4 +129,102 @@ def test_prompt_yes_no_exception(monkeypatch, installer_default: Installer, inst
installer_default.prompt_yes_no(prompt="yeet?", default_in="skeeb")

with pytest.raises(ValueError):
installer_not_default.prompt_yes_no(prompt="yeet?", default_in="skeeb")
installer_not_default.prompt_yes_no(prompt="yeet?", default_in="skeeb")


def test_prompt_list_select(monkeypatch, installer_default: Installer, installer_not_default: Installer):
list_selection = "2"
opts_list = ["Test Option #1", "Test Option #2", "Test Option #3"]
monkeypatch.setattr('builtins.input', lambda _: list_selection)

user_in_default = installer_default.prompt_list(
prompt="yeet?", options=opts_list, default_in=0)

assert user_in_default == int(list_selection)

user_in = installer_not_default.prompt_list(
prompt="yeet?", options=opts_list, default_in=0)

assert user_in == int(list_selection)


def test_prompt_list_default(monkeypatch, installer_default: Installer, installer_not_default: Installer):
default_selection = ""
default_opt = 1
opts_list = ["Test Option #1", "Test Option #2", "Test Option #3"]
monkeypatch.setattr('builtins.input', lambda _: default_selection)

user_in_default = installer_default.prompt_list(
prompt="yeet?", options=opts_list, default_in=default_opt)

assert user_in_default == int(default_opt)

user_in = installer_not_default.prompt_list(
prompt="yeet?", options=opts_list, default_in=default_opt)

assert user_in == int(default_opt)


def test_distro_detect_yum(monkeypatch, installer_default: Installer):

def yum_cmd_test(_, param: str):
if param == "yum":
return "yum works!"
else:
raise pilot_drive.installer.FailedToExecuteCommandException

monkeypatch.setattr(
"pilot_drive.installer.Installer.exec_cmd", yum_cmd_test)

distro_detect = installer_default.detect_distro_manager()

assert distro_detect == "yum"


def test_distro_detect_apt(monkeypatch, installer_default: Installer):

def apt_cmd_test(_, param: str):
if param == "apt":
return "apt works!"
else:
raise pilot_drive.installer.FailedToExecuteCommandException

monkeypatch.setattr(
"pilot_drive.installer.Installer.exec_cmd", apt_cmd_test)

distro_detect = installer_default.detect_distro_manager()

assert distro_detect == "apt"


def test_distro_detect_fail(monkeypatch, installer_default: Installer):

def fail_cmd_test(_, param: str):
raise pilot_drive.installer.FailedToExecuteCommandException

monkeypatch.setattr(
"pilot_drive.installer.Installer.exec_cmd", fail_cmd_test)

with pytest.raises(pilot_drive.installer.FailedToDetectDistroManagerException):
installer_default.detect_distro_manager()


def test_detect_sys_arch_valid(monkeypatch, installer_default: Installer):

for arch in pilot_drive.installer.CommonArchs:

class ArchClass:
machine = arch

monkeypatch.setattr("platform.uname", lambda: ArchClass())
assert installer_default.detect_current_arch() == arch


def test_detect_sys_arch_fail(monkeypatch, installer_default: Installer):
class ArchClass:
machine = "yeet"

monkeypatch.setattr("platform.uname", lambda: ArchClass())

with pytest.raises(pilot_drive.installer.FailedToDetectSysArchException):
installer_default.detect_current_arch()

0 comments on commit 08ac549

Please sign in to comment.