Skip to content

Commit

Permalink
Add initial get_command_from_result test
Browse files Browse the repository at this point in the history
  • Loading branch information
pagmatt committed Feb 9, 2024
1 parent 8de1e5b commit f381ee5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
4 changes: 2 additions & 2 deletions sem/__init__.py
Expand Up @@ -4,11 +4,11 @@
from .lptrunner import LptRunner
from .gridrunner import BUILD_GRID_PARAMS, SIMULATION_GRID_PARAMS
from .database import DatabaseManager
from .utils import list_param_combinations, automatic_parser, stdout_automatic_parser, only_load_some_files, CallbackBase
from .utils import list_param_combinations, automatic_parser, stdout_automatic_parser, only_load_some_files, get_command_from_result, CallbackBase
from .cli import cli

__all__ = ('CampaignManager', 'SimulationRunner', 'ParallelRunner', 'LptRunner',
'DatabaseManager', 'list_param_combinations', 'automatic_parser',
'only_load_some_files', 'CallbackBase')
'only_load_some_files', 'get_command_from_result', 'CallbackBase')

name = 'sem'
4 changes: 2 additions & 2 deletions sem/utils.py
Expand Up @@ -116,11 +116,11 @@ def get_command_from_result(script, path, result, debug=False):
command = "./ns3 run " if os.path.exists(os.path.join(path, "ns3")) else "python3 ./waf --run "

if not debug:
command += script + " " + " ".join(
command += "\"" + script + " " + " ".join(
['--%s=%s' % (param, value) for param, value in
result['params'].items()]) + "\""
else:
command += script + " --command-template=\"" +\
command += "\"" + script + " --command-template=\"" +\
"gdb --args %s " + " ".join(['--%s=%s' % (param, value) for
param, value in
result['params'].items()]) + "\""
Expand Down
45 changes: 44 additions & 1 deletion tests/test_utils.py
@@ -1,9 +1,24 @@
from sem import list_param_combinations, automatic_parser, stdout_automatic_parser, CallbackBase, CampaignManager
from sem import list_param_combinations, automatic_parser, stdout_automatic_parser, get_command_from_result, CallbackBase, CampaignManager
import json
import numpy as np
import pytest
from operator import getitem


@pytest.fixture(scope='function', params=[['compiled', False]])
def ns_3_compiled_folder_and_command(ns_3_compiled, ns_3_compiled_examples, request):
if request.param[0] == 'compiled':
if request.param[1] is False:
return [ns_3_compiled, False, './ns3 run \"hash-example --dict=/usr/share/dict/american-english --time=False --RngRun=0\"']
# elif request.param[1] is True:
# return [ns_3_compiled, True, './ns3 run \"hash-example --dict=/usr/share/dict/american-english --time=False --RngRun=0\"']
elif request.param[0] == 'compiled_examples':
if request.param[1] is False:
return [ns_3_compiled_examples, False, 'python3 ./waf --run \"hash-example --dict=/usr/share/dict/american-english --time=False --RngRun=0\"']
# elif request.param[1] is True:
# return [ns_3_compiled_examples, True, 'python3 ./waf --run \"hash-example --dict=/usr/share/dict/american-english --time=False --RngRun=0\"']


def test_list_param_combinations():
# Two possible combinations
d1 = {'a': [1, 2]}
Expand Down Expand Up @@ -139,3 +154,31 @@ def test_callback(ns_3_compiled, config, parameter_combination):
campaign.run_missing_simulations(
param_list=[parameter_combination], callbacks=[cb])
assert expected_output == cb.output
@pytest.mark.parametrize('ns_3_compiled_folder_and_command',
[
['compiled', False],
['compiled_examples', False]
],
indirect=True)


def test_get_cmnd_from_result(ns_3_compiled_folder_and_command, config, parameter_combination):

# Create an ns-3 campaign to run simulations and obtain a result
ns_3_folder = ns_3_compiled_folder_and_command[0]
hardcoded_command = ns_3_compiled_folder_and_command[2]

cmpgn = CampaignManager.new(ns_3_folder,
config['script'],
config['campaign_dir'],
overwrite=True,
skip_configuration=True)

cmpgn.run_simulations([parameter_combination], show_progress=False)

# Retrieve the results, and compare the output of get_command_from_result
# with the expected command
result = cmpgn.db.get_complete_results()[0]
cmnd = get_command_from_result(
config['script'], ns_3_folder, result)
assert (hardcoded_command == cmnd)

0 comments on commit f381ee5

Please sign in to comment.