daemon: refactoring to remove usage of os.path where possible and pathlib.Path instead

This commit is contained in:
Blake Harnden 2021-03-19 16:54:24 -07:00
parent d0a55dd471
commit 1c970bbe00
38 changed files with 520 additions and 606 deletions

View file

@ -1,7 +1,7 @@
"""
Unit tests for testing CORE EMANE networks.
"""
import os
from pathlib import Path
from tempfile import TemporaryFile
from typing import Type
from xml.etree import ElementTree
@ -28,7 +28,8 @@ _EMANE_MODELS = [
EmaneCommEffectModel,
EmaneTdmaModel,
]
_DIR = os.path.dirname(os.path.abspath(__file__))
_DIR: Path = Path(__file__).resolve().parent
_SCHEDULE: Path = _DIR / "../../examples/tdma/schedule.xml"
def ping(
@ -107,9 +108,7 @@ class TestEmane:
# configure tdma
if model == EmaneTdmaModel:
session.emane.set_model_config(
emane_network.id,
EmaneTdmaModel.name,
{"schedule": os.path.join(_DIR, "../../examples/tdma/schedule.xml")},
emane_network.id, EmaneTdmaModel.name, {"schedule": str(_SCHEDULE)}
)
# create nodes

View file

@ -1,3 +1,4 @@
from pathlib import Path
from unittest import mock
import pytest
@ -68,7 +69,8 @@ class TestConfigServices:
service.create_dirs()
# then
node.privatedir.assert_called_with(MyService.directories[0])
directory = Path(MyService.directories[0])
node.privatedir.assert_called_with(directory)
def test_create_files_custom(self):
# given
@ -81,7 +83,8 @@ class TestConfigServices:
service.create_files()
# then
node.nodefile.assert_called_with(MyService.files[0], text)
file_path = Path(MyService.files[0])
node.nodefile.assert_called_with(file_path, text)
def test_create_files_text(self):
# given
@ -92,7 +95,8 @@ class TestConfigServices:
service.create_files()
# then
node.nodefile.assert_called_with(MyService.files[0], TEMPLATE_TEXT)
file_path = Path(MyService.files[0])
node.nodefile.assert_called_with(file_path, TEMPLATE_TEXT)
def test_run_startup(self):
# given

View file

@ -2,9 +2,9 @@
Unit tests for testing basic CORE networks.
"""
import os
import threading
from typing import Type
from pathlib import Path
from typing import List, Type
import pytest
@ -16,9 +16,9 @@ from core.location.mobility import BasicRangeModel, Ns2ScriptedMobility
from core.nodes.base import CoreNode, NodeBase
from core.nodes.network import HubNode, PtpNet, SwitchNode, WlanNode
_PATH = os.path.abspath(os.path.dirname(__file__))
_MOBILITY_FILE = os.path.join(_PATH, "mobility.scen")
_WIRED = [PtpNet, HubNode, SwitchNode]
_PATH: Path = Path(__file__).resolve().parent
_MOBILITY_FILE: Path = _PATH / "mobility.scen"
_WIRED: List = [PtpNet, HubNode, SwitchNode]
def ping(from_node: CoreNode, to_node: CoreNode, ip_prefixes: IpPrefixes):
@ -195,7 +195,7 @@ class TestCore:
# configure mobility script for session
config = {
"file": _MOBILITY_FILE,
"file": str(_MOBILITY_FILE),
"refresh_ms": "50",
"loop": "1",
"autostart": "0.0",

View file

@ -1,8 +1,8 @@
"""
Tests for testing tlv message handling.
"""
import os
import time
from pathlib import Path
from typing import Optional
import mock
@ -425,7 +425,7 @@ class TestGui:
assert file_data == service_file.data
def test_file_node_file_copy(self, request, coretlv: CoreHandler):
file_name = "/var/log/test/node.log"
file_path = Path("/var/log/test/node.log")
node = coretlv.session.add_node(CoreNode)
node.makenodedir()
file_data = "echo hello"
@ -433,7 +433,7 @@ class TestGui:
MessageFlags.ADD.value,
[
(FileTlvs.NODE, node.id),
(FileTlvs.NAME, file_name),
(FileTlvs.NAME, str(file_path)),
(FileTlvs.DATA, file_data),
],
)
@ -441,10 +441,10 @@ class TestGui:
coretlv.handle_message(message)
if not request.config.getoption("mock"):
directory, basename = os.path.split(file_name)
directory = str(file_path.parent)
created_directory = directory[1:].replace("/", ".")
create_path = os.path.join(node.nodedir, created_directory, basename)
assert os.path.exists(create_path)
create_path = node.nodedir / created_directory / file_path.name
assert create_path.exists()
def test_exec_node_tty(self, coretlv: CoreHandler):
coretlv.dispatch_replies = mock.MagicMock()
@ -547,20 +547,21 @@ class TestGui:
0,
[(EventTlvs.TYPE, EventTypes.FILE_SAVE.value), (EventTlvs.NAME, file_path)],
)
coretlv.handle_message(message)
assert os.path.exists(file_path)
assert Path(file_path).exists()
def test_event_open_xml(self, coretlv: CoreHandler, tmpdir):
xml_file = tmpdir.join("coretlv.session.xml")
file_path = xml_file.strpath
file_path = Path(xml_file.strpath)
node = coretlv.session.add_node(CoreNode)
coretlv.session.save_xml(file_path)
coretlv.session.delete_node(node.id)
message = coreapi.CoreEventMessage.create(
0,
[(EventTlvs.TYPE, EventTypes.FILE_OPEN.value), (EventTlvs.NAME, file_path)],
[
(EventTlvs.TYPE, EventTypes.FILE_OPEN.value),
(EventTlvs.NAME, str(file_path)),
],
)
coretlv.handle_message(message)

View file

@ -1,5 +1,5 @@
import itertools
import os
from pathlib import Path
import pytest
from mock import MagicMock
@ -9,8 +9,8 @@ from core.errors import CoreCommandError
from core.nodes.base import CoreNode
from core.services.coreservices import CoreService, ServiceDependencies, ServiceManager
_PATH = os.path.abspath(os.path.dirname(__file__))
_SERVICES_PATH = os.path.join(_PATH, "myservices")
_PATH: Path = Path(__file__).resolve().parent
_SERVICES_PATH = _PATH / "myservices"
SERVICE_ONE = "MyService"
SERVICE_TWO = "MyService2"
@ -64,15 +64,15 @@ class TestServices:
ServiceManager.add_services(_SERVICES_PATH)
my_service = ServiceManager.get(SERVICE_ONE)
node = session.add_node(CoreNode)
file_name = my_service.configs[0]
file_path = node.hostfilename(file_name)
file_path = Path(my_service.configs[0])
file_path = node.host_path(file_path)
# when
session.services.create_service_files(node, my_service)
# then
if not request.config.getoption("mock"):
assert os.path.exists(file_path)
assert file_path.exists()
def test_service_validate(self, session: Session):
# given

View file

@ -1,3 +1,4 @@
from pathlib import Path
from tempfile import TemporaryFile
from xml.etree import ElementTree
@ -34,7 +35,7 @@ class TestXml:
# save xml
xml_file = tmpdir.join("session.xml")
file_path = xml_file.strpath
file_path = Path(xml_file.strpath)
session.save_xml(file_path)
# verify xml file was created and can be parsed
@ -85,7 +86,7 @@ class TestXml:
# save xml
xml_file = tmpdir.join("session.xml")
file_path = xml_file.strpath
file_path = Path(xml_file.strpath)
session.save_xml(file_path)
# verify xml file was created and can be parsed
@ -148,7 +149,7 @@ class TestXml:
# save xml
xml_file = tmpdir.join("session.xml")
file_path = xml_file.strpath
file_path = Path(xml_file.strpath)
session.save_xml(file_path)
# verify xml file was created and can be parsed
@ -210,7 +211,7 @@ class TestXml:
# save xml
xml_file = tmpdir.join("session.xml")
file_path = xml_file.strpath
file_path = Path(xml_file.strpath)
session.save_xml(file_path)
# verify xml file was created and can be parsed
@ -261,7 +262,7 @@ class TestXml:
# save xml
xml_file = tmpdir.join("session.xml")
file_path = xml_file.strpath
file_path = Path(xml_file.strpath)
session.save_xml(file_path)
# verify xml file was created and can be parsed
@ -321,7 +322,7 @@ class TestXml:
# save xml
xml_file = tmpdir.join("session.xml")
file_path = xml_file.strpath
file_path = Path(xml_file.strpath)
session.save_xml(file_path)
# verify xml file was created and can be parsed
@ -390,7 +391,7 @@ class TestXml:
# save xml
xml_file = tmpdir.join("session.xml")
file_path = xml_file.strpath
file_path = Path(xml_file.strpath)
session.save_xml(file_path)
# verify xml file was created and can be parsed
@ -471,7 +472,7 @@ class TestXml:
# save xml
xml_file = tmpdir.join("session.xml")
file_path = xml_file.strpath
file_path = Path(xml_file.strpath)
session.save_xml(file_path)
# verify xml file was created and can be parsed