def test_initialization(tmpdir, popen): t1 = Target.empty("Target1") t2 = Target.empty("Target2") t3 = Target.empty("Target3") t4 = Target.empty("Target4") gwf_dir = tmpdir.ensure_dir(".gwf") tracked_file = gwf_dir.join("slurm-backend-tracked.json") tracked_file.write( '{"Target1": "1", "Target2": "2", "Target3": "3", "Target4": "4"}' ) popen.return_value.returncode = 0 popen.return_value.communicate.return_value = ("3;SE\n2;BF\n1;R\n", "") with tmpdir.as_cwd(): backend = SlurmBackend() assert backend.status(t1) == Status.RUNNING assert backend.status(t2) == Status.UNKNOWN assert backend.status(t3) == Status.SUBMITTED assert backend.status(t4) == Status.UNKNOWN popen.return_value.returncode = 1 popen.return_value.communicate.return_value = ("", "This is stderr") with pytest.raises(BackendError): with SlurmBackend(): pass
def test_submit_1(popen): t1 = Target.empty("Target1") t2 = Target.empty("Target2") t3 = Target.empty("Target3") t4 = Target.empty("Target4") popen.return_value.returncode = 0 popen.return_value.communicate.return_value = ("", "") backend = SlurmBackend() popen.return_value.returncode = 0 popen.return_value.communicate.side_effect = [ ("1\n", ""), ("2\n", ""), ("3\n", ""), ("", "This is stderr\n"), ] backend.submit(t1, []) backend.submit(t2, []) backend.submit(t3, [t1, t2]) assert ( call( ["/bin/sbatch", "--parsable"], stderr=-1, stdin=-1, stdout=-1, universal_newlines=True, ) in popen.call_args_list ) assert ( call( ["/bin/sbatch", "--parsable"], stderr=-1, stdin=-1, stdout=-1, universal_newlines=True, ) in popen.call_args_list ) assert ( call( ["/bin/sbatch", "--parsable", "--dependency=afterok:1:2"], stderr=-1, stdin=-1, stdout=-1, universal_newlines=True, ) in popen.call_args_list ) with pytest.raises(DependencyError): backend.submit(t3, [t4]) popen.return_value.returncode = 1 with pytest.raises(BackendError): backend.submit(t4, [])
def test_cancel(popen): t = Target.empty("Target") popen.return_value.returncode = 0 popen.return_value.communicate.return_value = ("", "") backend = SlurmBackend() with pytest.raises(TargetError): backend.cancel(t) popen.return_value.communicate.return_value = ("1\n", "") backend.submit(t, []) assert backend.status(t) == Status.SUBMITTED backend.cancel(t) assert backend.status(t) == Status.UNKNOWN assert ( call( ["/bin/scancel", "--verbose", "1"], stderr=-1, stdin=-1, stdout=-1, universal_newlines=True, ) in popen.call_args_list ) popen.return_value.returncode = 1 popen.return_value.communicate.return_value = ("", "This is stderr") with pytest.raises(BackendError): backend.cancel(t)
def test_executable_unavailable_after_initialization(popen, monkeypatch): exes = {"squeue": "/bin/squeue"} monkeypatch.setattr("gwf.backends.slurm.find_executable", lambda e: exes.get(e)) t = Target.empty("TestTarget") popen.return_value.returncode = 0 popen.return_value.communicate.return_value = ("", "") backend = SlurmBackend() with pytest.raises(BackendError): backend.cancel(t) with pytest.raises(BackendError): backend.submit(t, dependencies=[])
def test_file_log_manager_remove(log_manager, tmpdir): target = Target.empty("TestTarget") stdout_file = tmpdir.join(".gwf", "logs", "TestTarget.stdout").ensure() stderr_file = tmpdir.join(".gwf", "logs", "TestTarget.stderr").ensure() log_manager.remove_stdout(target) assert not stdout_file.exists() log_manager.remove_stderr(target) assert not stderr_file.exists() with pytest.raises(LogError): log_manager.remove_stdout(target) with pytest.raises(LogError): log_manager.remove_stderr(target)
def test_file_log_manager_open(log_manager, tmpdir): target = Target.empty("TestTarget") with pytest.raises(LogError): log_manager.open_stdout(target) with pytest.raises(LogError): log_manager.open_stderr(target) stdout_file = tmpdir.join(".gwf", "logs", "TestTarget.stdout") stdout_file.write("This is standard output...") stderr_file = tmpdir.join(".gwf", "logs", "TestTarget.stderr") stderr_file.write("This is standard error...") assert log_manager.open_stdout(target).read() == "This is standard output..." assert log_manager.open_stderr(target).read() == "This is standard error..."
def test_file_log_manager_open(log_manager, tmpdir): target = Target.empty("TestTarget") with pytest.raises(LogError): log_manager.open_stdout(target) with pytest.raises(LogError): log_manager.open_stderr(target) stdout_file = tmpdir.join(".gwf", "logs", "TestTarget.stdout") stdout_file.write("This is standard output...") stderr_file = tmpdir.join(".gwf", "logs", "TestTarget.stderr") stderr_file.write("This is standard error...") assert log_manager.open_stdout( target).read() == "This is standard output..." assert log_manager.open_stderr( target).read() == "This is standard error..."
def test_file_log_manager_path(log_manager): target = Target.empty("TestTarget") assert log_manager.stderr_path(target) == ".gwf/logs/TestTarget.stderr" assert log_manager.stdout_path(target) == ".gwf/logs/TestTarget.stdout"