Skip to content

Commit

Permalink
Change dir to the correct test directory when running tests
Browse files Browse the repository at this point in the history
Consistency cleanup
  • Loading branch information
tmikuska committed Sep 1, 2022
1 parent 837f5f4 commit b1d7a40
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
36 changes: 19 additions & 17 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

CURRENT_VERSION = ClientLibrary.VERSION.version_str

FAKE_HOST = "https://0.0.0.0"
FAKE_HOST_API = "https://0.0.0.0/api/v0/"


def client_library_patched_system_info(version):
Expand Down Expand Up @@ -70,21 +70,23 @@ def mocked_session():

def resp_body_from_file(req, context):
"""
A callback that retuns the contents of a file based on the request.
:param req: The title to search for
:type req: An HTTP request (or proxy) object
:returns: A string, the contents of a file that corresponds to a response body for the request.
:rtype: string
A callback that returns the contents of a file based on the request.
:param req: The title to search for
:type req: An HTTP request (or proxy) object
:param context: unused but required context parameter
:type context: An HTTP response object
:returns: A string, the contents of a file that corresponds to a response body for the request.
:rtype: string
"""
endpoint_parts = req.path.split("/")[3:]
filename = "not initialized"
if len(endpoint_parts) == 1:
filename = f"{endpoint_parts[0]}.json"
filename = endpoint_parts[0] + ".json"
elif endpoint_parts[0] == "labs":
lab_id = endpoint_parts[1]
filename = "_".join(endpoint_parts[2:]) + "-" + lab_id + ".json"
file_path = Path("tests/test_data", filename)
file_path = Path("test_data", filename)
return file_path.read_text()


Expand All @@ -96,26 +98,26 @@ def requests_mock_with_labs(requests_mock):
some runtime data, like node states and simulation_statistics.
"""
requests_mock.get(
FAKE_HOST + "/api/v0/system_information",
FAKE_HOST_API + "system_information",
json={"version": CURRENT_VERSION, "ready": True},
)
requests_mock.post(FAKE_HOST + "/api/v0/authenticate", json="BOGUS_TOKEN")
requests_mock.get(FAKE_HOST + "/api/v0/authok", text=None)
requests_mock.post(FAKE_HOST_API + "authenticate", json="BOGUS_TOKEN")
requests_mock.get(FAKE_HOST_API + "authok", text=None)
resp_from_files = (
"labs",
"populate_lab_tiles",
"labs/444a78d1-575c-4746-8469-696e580f17b6/topology?exclude_configurations=True",
"labs/444a78d1-575c-4746-8469-696e580f17b6/topology",
"labs/444a78d1-575c-4746-8469-696e580f17b6/simulation_stats",
"labs/444a78d1-575c-4746-8469-696e580f17b6/layer3_addresses",
"labs/df76a038-076f-4744-85c0-b2e1daf1bc06/topology?exclude_configurations=True",
"labs/df76a038-076f-4744-85c0-b2e1daf1bc06/topology",
"labs/df76a038-076f-4744-85c0-b2e1daf1bc06/simulation_stats",
"labs/df76a038-076f-4744-85c0-b2e1daf1bc06/layer3_addresses",
"labs/3031b614-0e76-4450-9fe0-6b3be0bc0bd2/topology?exclude_configurations=True",
"labs/3031b614-0e76-4450-9fe0-6b3be0bc0bd2/topology",
"labs/3031b614-0e76-4450-9fe0-6b3be0bc0bd2/simulation_stats",
"labs/3031b614-0e76-4450-9fe0-6b3be0bc0bd2/layer3_addresses",
"labs/863799a0-3d09-4af4-be26-cad997b6ab27/topology?exclude_configurations=True",
"labs/863799a0-3d09-4af4-be26-cad997b6ab27/topology",
"labs/863799a0-3d09-4af4-be26-cad997b6ab27/simulation_stats",
"labs/863799a0-3d09-4af4-be26-cad997b6ab27/layer3_addresses",
)
for api in resp_from_files:
requests_mock.get(f"{FAKE_HOST}/api/v0/{api}", text=resp_body_from_file)
requests_mock.get(FAKE_HOST_API + api, text=resp_body_from_file)
18 changes: 9 additions & 9 deletions tests/test_client_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ def test_import_lab_from_path_virl_title(
Lab.sync = Mock()
new_title = "new_title"
(tmp_path / "topology.virl").write_text("<?xml version='1.0' encoding='UTF-8'?>")
with patch.object(Lab, "sync", autospec=True) as sync_mock:
with patch.object(Lab, "sync", autospec=True):
lab = cl.import_lab_from_path(
path=(tmp_path / "topology.virl").as_posix(), title=new_title
)
assert lab.title is not None
assert lab.lab_base_url.startswith("https://0.0.0.0/fake_url/api/v0/labs/")

cl.session.post.assert_called_once_with(
f"https://0.0.0.0/fake_url/api/v0/import/virl-1x?title={new_title}",
"https://0.0.0.0/fake_url/api/v0/import/virl-1x?title=" + new_title,
data="<?xml version='1.0' encoding='UTF-8'?>",
)

Expand Down Expand Up @@ -701,24 +701,24 @@ def test_import_lab_offline(


def test_convergence_parametrization(client_library_server_current, mocked_session):
MAX_ITER = 2
WAIT_TIME = 1
max_iter = 2
max_time = 1
cl = ClientLibrary(
url="https://0.0.0.0/fake_url/",
username="test",
password="pa$$",
convergence_wait_max_iter=MAX_ITER,
convergence_wait_time=WAIT_TIME,
convergence_wait_max_iter=max_iter,
convergence_wait_time=max_time,
)
# check that passing of value from client to lab is working
lab = cl.create_lab()
assert lab.wait_max_iterations == MAX_ITER
assert lab.wait_time == WAIT_TIME
assert lab.wait_max_iterations == max_iter
assert lab.wait_time == max_time
with patch.object(Lab, "has_converged", return_value=False):
with pytest.raises(RuntimeError) as err:
lab.wait_until_lab_converged()
assert (
"has not converged, maximum tries %s exceeded" % MAX_ITER
"has not converged, maximum tries %s exceeded" % max_iter
) in err.value.args[0]

# try to override values on function
Expand Down
7 changes: 4 additions & 3 deletions tests/test_client_library_labs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
# limitations under the License.
#

from pathlib import Path
from unittest.mock import MagicMock, Mock

import pytest
Expand Down Expand Up @@ -270,14 +269,16 @@ def test_connect_two_nodes(requests_mock):
assert link.id == "l0"


def test_join_existing_lab(requests_mock_with_labs):
def test_join_existing_lab(change_test_dir, requests_mock_with_labs):
client = ClientLibrary(url=FAKE_HOST, username="test", password="pa$$")
lab = client.join_existing_lab("444a78d1-575c-4746-8469-696e580f17b6")
assert lab.title == "IOSv Feature Tests"
assert lab.statistics == {"nodes": 7, "links": 8, "interfaces": 24}


def test_all_labs(client_library_server_current, requests_mock_with_labs):
def test_all_labs(
client_library_server_current, change_test_dir, requests_mock_with_labs
):
client = ClientLibrary(url=FAKE_HOST, username="test", password="pa$$")
all_labs = client.all_labs()
assert len(all_labs) == 4
Expand Down

0 comments on commit b1d7a40

Please sign in to comment.