Skip to content
Snippets Groups Projects
wsgi_app_test.py 3.01 KiB
Newer Older
John Chilton's avatar
John Chilton committed
import os
John Chilton's avatar
John Chilton committed
import time

John Chilton's avatar
John Chilton committed
from six.moves.urllib.parse import quote

John Chilton's avatar
John Chilton committed

    """ Tests app controller methods. These tests should be
John Chilton's avatar
John Chilton committed
    compartmentalized. Also these methods should be made to not retest
    the behavior of the associated Manager class. """
    from .test_utils import test_pulsar_app
    with test_pulsar_app(test_conf={"extra_environ": {"REMOTE_ADDR": "127.101.101.98"}}) as app:
        staging_directory = app.app.staging_directory
John Chilton's avatar
John Chilton committed
        setup_response = app.post("/jobs?job_id=12345")
John Chilton's avatar
John Chilton committed
        setup_config = json.loads(setup_response.body.decode("utf-8"))
John Chilton's avatar
John Chilton committed
        assert setup_config["working_directory"].startswith(staging_directory)
        outputs_directory = setup_config["outputs_directory"]
        assert outputs_directory.startswith(staging_directory)
        assert setup_config["path_separator"] == os.sep
John Chilton's avatar
John Chilton committed

        def test_upload(upload_type):
John Chilton's avatar
John Chilton committed
            url = "/jobs/%s/files?name=input1&type=%s" % (job_id, upload_type)
            upload_input_response = app.post(url, "Test Contents")
John Chilton's avatar
John Chilton committed
            upload_input_config = json.loads(upload_input_response.body.decode("utf-8"))
John Chilton's avatar
John Chilton committed
            staged_input_path = upload_input_config["path"]
            staged_input = open(staged_input_path, "r")
            try:
                assert staged_input.read() == "Test Contents"
            finally:
John Chilton's avatar
John Chilton committed
                staged_input.close()
John Chilton's avatar
John Chilton committed
        test_upload("input")
John Chilton's avatar
John Chilton committed

        test_output = open(os.path.join(outputs_directory, "test_output"), "w")
        try:
            test_output.write("Hello World!")
        finally:
            test_output.close()
John Chilton's avatar
John Chilton committed
        download_response = app.get("/jobs/%s/files?name=test_output&type=output" % job_id)
John Chilton's avatar
John Chilton committed
        assert download_response.body.decode("utf-8") == "Hello World!"
John Chilton's avatar
John Chilton committed

John Chilton's avatar
John Chilton committed
            app.get("/jobs/%s/files?name=test_output2&type=output" % job_id)
            assert False  # Should throw exception
        except Exception:
John Chilton's avatar
John Chilton committed
        command_line = quote("""python -c "import sys; sys.stdout.write('test_out')" """)
John Chilton's avatar
John Chilton committed
        launch_response = app.post("/jobs/%s/submit?command_line=%s" % (job_id, command_line))
John Chilton's avatar
John Chilton committed
        assert launch_response.body.decode("utf-8") == 'OK'
John Chilton's avatar
John Chilton committed

        # Hack: Call twice to ensure postprocessing occurs and has time to
        # complete. Monitor thread should get this.
John Chilton's avatar
John Chilton committed
        time.sleep(.3)
John Chilton's avatar
John Chilton committed
        check_response = app.get("/jobs/%s/status" % job_id)
John Chilton's avatar
John Chilton committed
        time.sleep(.3)
John Chilton's avatar
John Chilton committed
        check_response = app.get("/jobs/%s/status" % job_id)
John Chilton's avatar
John Chilton committed
        check_config = json.loads(check_response.body.decode("utf-8"))
John Chilton's avatar
John Chilton committed
        assert check_config['returncode'] == 0
        assert check_config['stdout'] == "test_out"
        assert check_config['stderr'] == ""
John Chilton's avatar
John Chilton committed

John Chilton's avatar
John Chilton committed
        kill_response = app.put("/jobs/%s/cancel" % job_id)
John Chilton's avatar
John Chilton committed
        assert kill_response.body.decode("utf-8") == 'OK'
John Chilton's avatar
John Chilton committed

John Chilton's avatar
John Chilton committed
        clean_response = app.delete("/jobs/%s" % job_id)
John Chilton's avatar
John Chilton committed
        assert clean_response.body.decode("utf-8") == 'OK'
John Chilton's avatar
John Chilton committed
        assert os.listdir(staging_directory) == []