def test011(): """ test011: this test checks the basic behavior of the ProcessCopyFileTask. The copied file should have the supplied Vars replacing the variable references in the file. """ test_file = "test011.txt" test_file_path = find_file(test_file) class SimpleInfra(InfraModel): testbox = StaticServer("testbox", find_ip()) infra = SimpleInfra("simple") class SimpleNamespace(NamespaceModel): with_variables( Var("DEST", "/tmp/!{FILE}"), Var("FILE", test_file), Var("var1", "summat"), Var("var2", "or"), Var("var3", "the"), Var("var4", "other"), ) target = Role("target", host_ref=SimpleInfra.testbox) ns = SimpleNamespace() class SimpleConfig(ConfigModel): reset = CommandTask("reset", "/bin/rm -rf !{DEST}", removes="!{DEST}", task_role=SimpleNamespace.target) process = ProcessCopyFileTask( "pcf", "!{DEST}", src=test_file_path, task_role=SimpleNamespace.target, repeat_count=1 ) with_dependencies(reset | process) cfg = SimpleConfig() ea = AnsibleExecutionAgent(config_model_instance=cfg, namespace_model_instance=ns, no_delay=True) try: ea.perform_config() file_content = file("/tmp/test011.txt", "r").read() assert "summat or the other" == file_content except ExecutionException, e: import traceback for task, etype, value, tb in ea.get_aborted_tasks(): print ">>>Task {} failed with the following:".format(task.name) traceback.print_exception(etype, value, tb, file=sys.stdout) print assert False, e.message
def test013(): """ test013: Similar to test011, but with multi-line files. The replacements should be made across all lines of the file """ test_file = "test013.txt" test_file_path = find_file(test_file) class SimpleInfra(InfraModel): testbox = StaticServer("testbox", find_ip()) infra = SimpleInfra("simple") class SimpleNamespace(NamespaceModel): with_variables( Var("DEST", "/tmp/!{FILE}"), Var("FILE", test_file), Var("var1", "summat"), Var("var2", "or"), Var("var3", "the"), Var("var4", "other"), ) target = Role("target", host_ref=SimpleInfra.testbox) ns = SimpleNamespace() class SimpleConfig(ConfigModel): reset = CommandTask("reset", "/bin/rm -rf !{DEST}", removes="!{DEST}", task_role=SimpleNamespace.target) process = ProcessCopyFileTask( "pcf", "!{DEST}", src=test_file_path, task_role=SimpleNamespace.target, repeat_count=1 ) with_dependencies(reset | process) cfg = SimpleConfig() ea = AnsibleExecutionAgent(config_model_instance=cfg, namespace_model_instance=ns, no_delay=True) try: ea.perform_config() file_content = [l.strip() for l in file("/tmp/test013.txt", "r").readlines()] assert "summat or" == file_content[0] and "the other" == file_content[1] except ExecutionException, e: import traceback for task, etype, value, tb in ea.get_aborted_tasks(): print ">>>Task {} failed with the following:".format(task.name) traceback.print_exception(etype, value, tb, file=sys.stdout) print assert False, e.message
def test021(): "test021: Have a multi-task get the proper user from the config class" from datetime import datetime target_dir = "/home/lxle1/tmp/test021" num_files = 5 class SimpleNamespace(NamespaceModel): with_variables( Var("TARGET_DIR", target_dir), Var("COMPNUM", ctxt.name), Var("FILE_NAME", "!{TARGET_DIR}/!{COMPNUM}-target.txt"), ) targets = MultiRole(Role("pseudo-role", host_ref=find_ip())) ns = SimpleNamespace() for i in range(num_files): _ = ns.targets[i] now_str = datetime.now().ctime() class SimpleConfig(ConfigModel): clear = CommandTask("clear-previous", "/bin/rm -rf !{TARGET_DIR}", task_role=SimpleNamespace.targets[0]) make = CommandTask("make-output-dir", "/bin/mkdir -p !{TARGET_DIR}", task_role=SimpleNamespace.targets[0]) copies = MultiTask( "copy", CopyFileTask("cpf", "!{FILE_NAME}", content="Created at: {}\n".format(now_str)), SimpleNamespace.q.targets.all(), ) with_dependencies(clear | make | copies) # we're testing if setting the user stuff at this level works right cfg = SimpleConfig(remote_user="******", private_key_file=find_file("lxle1-dev-key")) ea = AnsibleExecutionAgent(config_model_instance=cfg, namespace_model_instance=ns, no_delay=True) try: ea.perform_config() assert num_files == len(os.listdir(target_dir)) except ExecutionException, e: import traceback for task, etype, value, tb in ea.get_aborted_tasks(): print ">>>Task {} failed with the following:".format(task.name) traceback.print_exception(etype, value, tb, file=sys.stdout) print assert False, e.message
def test012(): """ test012: this checks ProcessCopyFileTask if not all Vars are supplied. The variable 'var3' won't be supplied. """ test_file = "test012.txt" test_file_path = find_file(test_file) class SimpleInfra(InfraModel): testbox = StaticServer("testbox", find_ip()) infra = SimpleInfra("simple") class SimpleNamespace(NamespaceModel): with_variables( Var("DEST", "/tmp/!{FILE}"), Var("FILE", test_file), Var("var1", "summat"), Var("var2", "or"), # Var("var3", "the"), #This one is to be missing Var("var4", "other"), ) target = Role("target", host_ref=SimpleInfra.testbox) ns = SimpleNamespace() class SimpleConfig(ConfigModel): reset = CommandTask("reset", "/bin/rm -rf !{DEST}", removes="!{DEST}", task_role=SimpleNamespace.target) process = ProcessCopyFileTask( "pcf", "!{DEST}", src=test_file_path, task_role=SimpleNamespace.target, repeat_count=1 ) with_dependencies(reset | process) cfg = SimpleConfig() ea = AnsibleExecutionAgent(config_model_instance=cfg, namespace_model_instance=ns, no_delay=True) try: ea.perform_config() assert False, "this should have raised an exception about not finding var3" except ExecutionException, _: found_it = False for _, _, value, _ in ea.get_aborted_tasks(): if "var3" in value.message: found_it = True break assert found_it, "an exception was raised, but not about missing var3"
def test014(): """ test014: multiple copies of the same task going against the same host to see if any parallel processing issues arise. """ test_file = "test014-BigTextFile.txt" test_file_path = find_file(test_file) class SimpleInfra(InfraModel): testbox = StaticServer("testbox", find_ip()) infra = SimpleInfra("simple") class SimpleNamespace(NamespaceModel): with_variables(Var("PREFIX", ctxt.name), Var("DEST", "/tmp/!{PREFIX}-!{FILE}"), Var("FILE", test_file)) target = MultiRole(Role("target", host_ref=SimpleInfra.testbox)) ns = SimpleNamespace() class SingleCopy(ConfigModel): reset = CommandTask("014_reset", "/bin/rm -rf !{DEST}", removes="!{DEST}") copy = CopyFileTask("014_cpf", "!{DEST}", src=test_file_path) with_dependencies(reset | copy) class MultiCopy(ConfigModel): task_suite = MultiTask("all-copies", ConfigClassTask("one-copy", SingleCopy), SimpleNamespace.q.target.all()) cfg = MultiCopy() for i in range(5): _ = ns.target[i] ea = AnsibleExecutionAgent(config_model_instance=cfg, namespace_model_instance=ns, num_threads=5, no_delay=True) try: ea.perform_config() except ExecutionException, e: import traceback for task, etype, value, tb in ea.get_aborted_tasks(): print ">>>Task {} failed with the following:".format(task.name) traceback.print_exception(etype, value, tb, file=sys.stdout) print assert False, e.message
def test020(): "test020: write some data to a user's tmp dir based on config-level user" from datetime import datetime target = "/home/lxle1/tmp/020test.txt" class SimpleNamespace(NamespaceModel): with_variables(Var("TARGET_FILE", target)) copy_target = Role("copy-target", host_ref=find_ip()) ns = SimpleNamespace() now_str = datetime.now().ctime() class SimpleConfig(ConfigModel): copy = CopyFileTask( "cpf", "!{TARGET_FILE}", task_role=SimpleNamespace.copy_target, content="This content created at: {}\n".format(now_str), ) # we're testing if setting the user stuff at this level works right cfg = SimpleConfig(remote_user="******", private_key_file=find_file("lxle1-dev-key")) ea = AnsibleExecutionAgent(config_model_instance=cfg, namespace_model_instance=ns, no_delay=True) try: ea.perform_config() assert now_str in "".join(file(target, "r").readlines()) except ExecutionException, e: import traceback for task, etype, value, tb in ea.get_aborted_tasks(): print ">>>Task {} failed with the following:".format(task.name) traceback.print_exception(etype, value, tb, file=sys.stdout) print assert False, e.message
def setup(): # make sure the private key is read-only for the owner pkeyfile = find_file("lxle1-dev-key") os.chmod(pkeyfile, stat.S_IRUSR | stat.S_IWUSR)