def main(argv): """Main entry point.""" if len(argv) > 1: raise app.UsageError("Unknown arguments: '{}'.".format(" ".join(argv[1:]))) channel = grpc.insecure_channel(FLAGS.ledger) stub = alice_pb2_grpc.LedgerStub(channel) phd_path = pathlib.Path(FLAGS.phd) repo = git_repo.PhdRepo(phd_path) # TODO(cec): Sanity check values. target = FLAGS.target bazel_args = FLAGS.bazel_args bin_args = FLAGS.bin_args timeout_seconds = FLAGS.timeout_seconds job_id = stub.Add( alice_pb2.RunRequest( repo_state=repo.ToRepoState(), target=target, bazel_args=bazel_args, bin_args=bin_args, timeout_seconds=timeout_seconds, ) ) print(f"Started job {job_id.id}") while True: status = stub.Get(job_id) SummarizeJobStatus(status) time.sleep(1) if status.job_status == alice_pb2.LedgerEntry.COMPLETE: sys.exit(status.returncode)
def test_BazelClient_Run_stdout(workspace: pathlib.Path, tempdir: pathlib.Path): """Check stdout of test target.""" client = bazel.BazelClient(workspace, tempdir) process = client.Run(alice_pb2.RunRequest(target="//:hello", ledger_id=1,)) process.join() assert process.stdout == "Hello, stdout!\n"
def test_BazelClient_Run_returncode( workspace: pathlib.Path, tempdir: pathlib.Path ): """Check returncode of test target.""" client = bazel.BazelClient(workspace, tempdir) process = client.Run(alice_pb2.RunRequest(target="//:hello", ledger_id=1,)) process.join() assert process.returncode == 0
def test_BazelClient_Run_stderr(workspace: pathlib.Path, tempdir: pathlib.Path): """Check stderr of test target.""" client = bazel.BazelClient(workspace, tempdir) process = client.Run(alice_pb2.RunRequest(target="//:hello", ledger_id=1,)) process.join() # Stderr starts with bazel build log. print(process.stderr) assert "Hello, stderr!\n" in process.stderr
def test_BazelClient_Run_process_id( workspace: pathlib.Path, tempdir: pathlib.Path ): """Check that process ID is set.""" client = bazel.BazelClient(workspace, tempdir) process = client.Run(alice_pb2.RunRequest(target="//:hello", ledger_id=1,)) process.join() assert process.pid assert process.pid != system.PID
def test_BazeClient_run_missing_target( workspace: pathlib.Path, tempdir: pathlib.Path ): """Check error for missing target.""" client = bazel.BazelClient(workspace, tempdir) process = client.Run( alice_pb2.RunRequest(target="//:not_a_target", ledger_id=1,) ) process.join() assert process.returncode
def test_BazelClient_Run_workdir_files( workspace: pathlib.Path, tempdir: pathlib.Path ): """Check that output files are generated.""" client = bazel.BazelClient(workspace, tempdir) process = client.Run(alice_pb2.RunRequest(target="//:hello", ledger_id=1,)) process.join() assert (process.workdir / "stdout.txt").is_file() assert (process.workdir / "stderr.txt").is_file() assert (process.workdir / "returncode.txt").is_file()
def test_BazelClient_Run_process_isnt_running( workspace: pathlib.Path, tempdir: pathlib.Path ): """Check that process isn't running after completed.""" client = bazel.BazelClient(workspace, tempdir) process = client.Run(alice_pb2.RunRequest(target="//:hello", ledger_id=1,)) process.join() try: os.kill(process.pid, 0) test.Fail( "os.kill() didn't fail, that means the process is still " "running" ) except ProcessLookupError: pass
def test_BazelClient_kill_process( workspace: pathlib.Path, tempdir: pathlib.Path ): """Test that process can be killed.""" # Create a binary which will never terminate once launched. with open(workspace / "BUILD", "a") as f: f.write( """ cc_binary( name = "nonterminating", srcs = ["nonterminating.cc"], ) """ ) with open(workspace / "nonterminating.cc", "w") as f: f.write( """ int main() { while (1) {} } """ ) client = bazel.BazelClient(workspace, tempdir) process = client.Run( alice_pb2.RunRequest(target="//:nonterminating", ledger_id=1,) ) # Sleep for luck ;-) time.sleep(3) # Send the non-terminating process a kill signal. os.kill(process.pid, signal.SIGTERM) process.join() try: os.kill(process.pid, 0) test.Fail( "os.kill() didn't fail, that means the process is still " "running" ) except ProcessLookupError: pass
def mock_run_request() -> alice_pb2.RunRequest: return alice_pb2.RunRequest( repo_state=None, target="//:foo", )