def test_has_command(self): arg_list_1 = ArgList.from_string("rm -rf /*") self.assertTrue(arg_list_1.has_command) arg_list_2 = ArgList.from_string("-f omnetpp.ini -c foobar") self.assertFalse(arg_list_2.has_command) arg_list_3 = ArgList.from_string("--f omnetpp.ini -c foobar") self.assertFalse(arg_list_3.has_command)
def test_from_string(self): cmd = ( "../../src/run_crownet_dbg -u Cmdenv -f omnetpp.ini -c fTestDcD -r 0 --sim-time-limit=80s " '"--fingerprint=65f7-c707/tplx 65f7-c88/tplx" --cpu-time-limit=900s --vector-recording=false ' '--scalar-recording=true --result-dir=/crownet.csv/_fooBarD_r_0 --foo "a b c"' ) arg_list = ArgList.from_string(cmd) self.assertEqual( [ ["../../src/run_crownet_dbg", None], ["-u", "Cmdenv"], ["-f", "omnetpp.ini"], ["-c", "fTestDcD"], ["-r", "0"], ["--sim-time-limit=80s", None], [ "--fingerprint=65f7-c707/tplx 65f7-c88/tplx", None, ], # space in key is allowed ["--cpu-time-limit=900s", None], ["--vector-recording=false", None], ["--scalar-recording=true", None], ["--result-dir=/crownet.csv/_fooBarD_r_0", None], ["--foo", "a b c"], # space in value is allowed ], arg_list.raw(), )
def test_remove_startswith(self): arg_list = ArgList.from_string("-f omnetpp.ini -c foobar --foo=bar") self.assertTrue(arg_list.contains_key("--foo=bar")) self.assertTrue(arg_list.contains_key("-c")) arg_list.remove_key_startswith("--foo") self.assertFalse(arg_list.contains_key("--foo=bar")) self.assertTrue(arg_list.contains_key("-c"))
def test_get_value(self): data = [["-f", "blaa"], ["-vv", None], ["-j", 80]] arg_list = ArgList.from_list(data) self.assertEqual(arg_list.get_value("-f"), "blaa") self.assertEqual(arg_list.get_value("-f", "default"), "blaa") self.assertEqual(arg_list.get_value("-XXX", "foo"), "foo") self.assertEqual(arg_list.get_value("-XXX"), None)
def add_sumo_arguments(parser: argparse.ArgumentParser, args: List[str]): parser.add_argument( "--sumo.xxx", *filter_options(args, "--sumo."), dest="sumo_args", default=ArgList.from_list([ ["--port", "9999"], ["--bind", "0.0.0.0"], ]), action=SimulationArgAction, prefix="--sumo.", help="Sumo Arguments", ) parser.add_argument( "--create-sumo-container", dest="create_sumo_container", action="store_true", default=False, required=False, help= "If set a sumo container with name sumo_<run-name> is created matching to opp_<run-name> container.", ) parser.add_argument( "--sumo-tag", dest="sumo_tag", default="latest", required=False, help="Choose Sumo container. (Default: latest)", )
def add_omnet_arguments(parser: argparse.ArgumentParser, args: List[str]): parser.add_argument( "--opp-exec", dest="opp_exec", default="", help= "Specify OMNeT++ executable Default($CROWNET_HOME/crownet/src/run_crownet). " "Use --opp. prefix to specify arguments to pass to the " "given executable.", ) parser.add_argument( "--opp.xxx", *filter_options(args, "--opp."), dest="opp_args", default=ArgList.from_list([["-f", "omnetpp.ini"], ["-u", "Cmdenv"], ["-c", "final"]]), action=SimulationArgAction, prefix="--opp.", help= "Specify OMNeT++ executable. Use --opp. prefix to specify arguments to pass to the given executable." "`--opp.foo bar` --> `--foo bar`. If single '-' is needed use `--opp.-v`. Multiple values " "are supported `-opp.bar abc efg 123` will be `--bar abc efg 123`. For possible arguments see help of " "executable. Defaults: ", ) parser.add_argument( "--omnet-tag", dest="omnet_tag", default="latest", required=False, help="Choose Omnet container. (Default: latest)", )
def create_arg_list( base_args: ArgList, result_dir, experiment_label, ): _arg = ArgList.from_list(base_args.data) _arg.add(f"--result-dir={result_dir}") _arg.add(f"--experiment-label={experiment_label}") return _arg
def test_from_flat_list(self): flat_list = "--foo bar -i 12 -i 44 -vvv --baz aa cc dd".split() arg_list = ArgList.from_flat_list(flat_list) self.compare("--foo bar -i 12 -i 44 -vvv --baz aa cc dd", arg_list) self.assertEqual( [ ["--foo", "bar"], ["-i", "12"], ["-i", "44"], ["-vvv", None], ["--baz", ["aa", "cc", "dd"]], ], arg_list.raw(), )
def test_merge3(self): self.arglist.add("a", "b") self.arglist.add("c", "D") self.arglist.add("y", "z") self.compare("a b c D y z") other = ArgList() other.add("a", "b") other.add("c", "d") other.add("e", "f") self.assertRaises(ValueError, self.arglist.merge, other, "add")
def test_merge4(self): self.arglist.add("a", "b") self.arglist.add("c", "D") self.arglist.add("y", "z") self.compare("a b c D y z") other = ArgList() other.add("a", "b") other.add("c", "d") other.add("e", "f") self.assertRaises(RuntimeError, self.arglist.merge, other, "some-wrong-function")
def test_merge2(self): self.arglist.add("a", "b") self.arglist.add("c", "D") self.arglist.add("y", "z") self.compare("a b c D y z") other = ArgList() other.add("a", "b") other.add("c", "d") other.add("e", "f") self.arglist.merge(other, how="add_if_missing") self.compare("a b c D y z e f")
def test_merge1(self): self.arglist.add("a", "b") self.arglist.add("c", "D") self.arglist.add("y", "z") self.compare("a b c D y z") other = ArgList() other.add("a", "b") other.add("c", "d") other.add("e", "f") self.arglist.merge(other, how="add_override") self.compare("y z a b c d e f")
def exec_opp_run( self, arg_list: ArgList, result_dir, experiment_label, run_args_override=None, ): """ Execute opp_run in container. """ _arg = ArgList.from_list(arg_list.data) _arg.add(f"--result-dir={result_dir}") _arg.add(f"--experiment-label={experiment_label}") _arg.add(self.run_cmd, pos=0) logger.debug(f"start omnett++ container(exec_opp_run)") logger.debug(f"cmd: {_arg.to_string()}") return self.run(_arg.to_string(), **run_args_override)
def test_getvalue_equal(self): arg_list = ArgList.from_string("-f omnetpp.ini -c foobar --foo=bar") self.assertEqual(arg_list.get_value("--foo="), "bar")
def test_from_list(self): data = [["-f", "blaa"], ["-vv", None], ["-j", 80]] arg_list = ArgList.from_list(data) self.compare("-f blaa -vv -j 80", arg_list)
def setUp(self) -> None: self.arglist = ArgList()
class ArglistTest(unittest.TestCase): def setUp(self) -> None: self.arglist = ArgList() def compare(self, should, _arg=None): _arg = self.arglist if _arg is None else _arg self.assertEqual(_arg.to_string(), should) def test_add(self): self.arglist.add("foo", "bar") self.arglist.add("--baz", [1, 2, 3]) self.compare("foo bar --baz 1 2 3") self.arglist.add_override("--baz", ["one", "tow"]) self.compare("foo bar --baz one tow") def test_add2(self): self.arglist.add("foo", "bar") self.arglist.add("--baz") self.compare("foo bar --baz") self.arglist.add("run_script.py", pos=0) self.compare("run_script.py foo bar --baz") def test_add_err(self): self.arglist.add("foo", "bar") self.arglist.add("--baz", [1, 2, 3]) self.assertRaises(ValueError, self.arglist.add, "--baz", ["one", "tow"]) def test_append(self): self.arglist.append("-l", "foo") self.arglist.append("-l", "bar") self.compare("-l foo -l bar") def test_add_if_missing(self): self.arglist.add_if_missing("-f") self.arglist.add_if_missing("-f") self.arglist.add_if_missing("-f") self.compare("-f") def test_update_value(self): self.arglist.add("--baz", [1, 2, 3]) self.compare("--baz 1 2 3") self.arglist.update_value("--baz", "one-one") self.compare("--baz one-one") self.arglist.update_value("--foo", 22) self.compare("--baz one-one --foo 22") def test_update_idx(self): self.arglist.add("a", "b") self.arglist.add("c", "D") self.arglist.add("y", "z") self.compare("a b c D y z") self.arglist.update_index(1, "foo") self.compare("a b foo y z") def test_merge1(self): self.arglist.add("a", "b") self.arglist.add("c", "D") self.arglist.add("y", "z") self.compare("a b c D y z") other = ArgList() other.add("a", "b") other.add("c", "d") other.add("e", "f") self.arglist.merge(other, how="add_override") self.compare("y z a b c d e f") def test_merge2(self): self.arglist.add("a", "b") self.arglist.add("c", "D") self.arglist.add("y", "z") self.compare("a b c D y z") other = ArgList() other.add("a", "b") other.add("c", "d") other.add("e", "f") self.arglist.merge(other, how="add_if_missing") self.compare("a b c D y z e f") def test_merge3(self): self.arglist.add("a", "b") self.arglist.add("c", "D") self.arglist.add("y", "z") self.compare("a b c D y z") other = ArgList() other.add("a", "b") other.add("c", "d") other.add("e", "f") self.assertRaises(ValueError, self.arglist.merge, other, "add") def test_merge4(self): self.arglist.add("a", "b") self.arglist.add("c", "D") self.arglist.add("y", "z") self.compare("a b c D y z") other = ArgList() other.add("a", "b") other.add("c", "d") other.add("e", "f") self.assertRaises(RuntimeError, self.arglist.merge, other, "some-wrong-function") def test_from_list(self): data = [["-f", "blaa"], ["-vv", None], ["-j", 80]] arg_list = ArgList.from_list(data) self.compare("-f blaa -vv -j 80", arg_list) def test_get_value(self): data = [["-f", "blaa"], ["-vv", None], ["-j", 80]] arg_list = ArgList.from_list(data) self.assertEqual(arg_list.get_value("-f"), "blaa") self.assertEqual(arg_list.get_value("-f", "default"), "blaa") self.assertEqual(arg_list.get_value("-XXX", "foo"), "foo") self.assertEqual(arg_list.get_value("-XXX"), None) def test_from_flat_list(self): flat_list = "--foo bar -i 12 -i 44 -vvv --baz aa cc dd".split() arg_list = ArgList.from_flat_list(flat_list) self.compare("--foo bar -i 12 -i 44 -vvv --baz aa cc dd", arg_list) self.assertEqual( [ ["--foo", "bar"], ["-i", "12"], ["-i", "44"], ["-vvv", None], ["--baz", ["aa", "cc", "dd"]], ], arg_list.raw(), ) def test_from_flat_list2(self): flat_list = "/bin/bash ./fancy-script.sh --foo bar -i 12 -i 44 -vvv --baz aa cc dd".split( ) arg_list = ArgList.from_flat_list(flat_list) self.compare( "/bin/bash ./fancy-script.sh --foo bar -i 12 -i 44 -vvv --baz aa cc dd", arg_list, ) self.assertEqual( [ ["/bin/bash", None], ["./fancy-script.sh", None], ["--foo", "bar"], ["-i", "12"], ["-i", "44"], ["-vvv", None], ["--baz", ["aa", "cc", "dd"]], ], arg_list.raw(), ) def test_remove_startswith(self): arg_list = ArgList.from_string("-f omnetpp.ini -c foobar --foo=bar") self.assertTrue(arg_list.contains_key("--foo=bar")) self.assertTrue(arg_list.contains_key("-c")) arg_list.remove_key_startswith("--foo") self.assertFalse(arg_list.contains_key("--foo=bar")) self.assertTrue(arg_list.contains_key("-c")) def test_contains_startswith(self): arg_list = ArgList.from_string("-f omnetpp.ini -c foobar --foo=bar") self.assertTrue(arg_list.contains_key_startswith("--foo")) def test_getvalue_equal(self): arg_list = ArgList.from_string("-f omnetpp.ini -c foobar --foo=bar") self.assertEqual(arg_list.get_value("--foo="), "bar") def test_has_command(self): arg_list_1 = ArgList.from_string("rm -rf /*") self.assertTrue(arg_list_1.has_command) arg_list_2 = ArgList.from_string("-f omnetpp.ini -c foobar") self.assertFalse(arg_list_2.has_command) arg_list_3 = ArgList.from_string("--f omnetpp.ini -c foobar") self.assertFalse(arg_list_3.has_command) def test_from_string(self): cmd = ( "../../src/run_crownet_dbg -u Cmdenv -f omnetpp.ini -c fTestDcD -r 0 --sim-time-limit=80s " '"--fingerprint=65f7-c707/tplx 65f7-c88/tplx" --cpu-time-limit=900s --vector-recording=false ' '--scalar-recording=true --result-dir=/crownet.csv/_fooBarD_r_0 --foo "a b c"' ) arg_list = ArgList.from_string(cmd) self.assertEqual( [ ["../../src/run_crownet_dbg", None], ["-u", "Cmdenv"], ["-f", "omnetpp.ini"], ["-c", "fTestDcD"], ["-r", "0"], ["--sim-time-limit=80s", None], [ "--fingerprint=65f7-c707/tplx 65f7-c88/tplx", None, ], # space in key is allowed ["--cpu-time-limit=900s", None], ["--vector-recording=false", None], ["--scalar-recording=true", None], ["--result-dir=/crownet.csv/_fooBarD_r_0", None], ["--foo", "a b c"], # space in value is allowed ], arg_list.raw(), )