From 8e11a6251b1be2085a85cb1ff5119bc7e56cfde6 Mon Sep 17 00:00:00 2001 From: Daniel Gospodinow Date: Sat, 10 Feb 2024 15:43:33 +0000 Subject: [PATCH] Add script that uses docker to execute tests (#7) --- .version | 2 +- tests/test.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 tests/test.py diff --git a/.version b/.version index 99d85ec..5c4511c 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -0.0.6 \ No newline at end of file +0.0.7 \ No newline at end of file diff --git a/tests/test.py b/tests/test.py new file mode 100644 index 0000000..b2e2813 --- /dev/null +++ b/tests/test.py @@ -0,0 +1,58 @@ +import os +import docker + + +def test_healthy_package(client, image): + print("Testing healthy package...") + + container = client.containers.run( + image, + volumes={os.getcwd(): {"bind": "/test", "mode": "rw"}}, + working_dir="/test/healthypkg/", + entrypoint="nilaway ./...", + detach=True, + ) + + exit_code = container.wait() + logs = container.logs(stdout=True, stderr=True).decode("utf-8") + print(exit_code, "\n", logs) + + container.remove() + + assert exit_code["StatusCode"] == 0 + assert "Potential nil panic detected" not in logs + + +def test_unhealthy_package(client, image): + print("Testing unhealthy package...") + + container = client.containers.run( + image, + volumes={os.getcwd(): {"bind": "/test", "mode": "rw"}}, + working_dir="/test/unhealthypkg/", + entrypoint="nilaway ./...", + detach=True, + ) + + exit_code = container.wait() + logs = container.logs(stdout=True, stderr=True).decode("utf-8") + print(exit_code, "\n", logs) + + container.remove() + + assert exit_code["StatusCode"] != 0 + assert "Potential nil panic detected" in logs + + +def main(): + client = docker.from_env() + image, _ = client.images.build(path="../", tag="nilaway-action-test-image") + + test_healthy_package(client, image) + test_unhealthy_package(client, image) + + print("All tests passed!") + + +if __name__ == "__main__": + main()