Skip to content
Snippets Groups Projects
tests.py 3.29 KiB
Newer Older
  • Learn to ignore specific revisions
  • Ashwin Kumar Karnad's avatar
    Ashwin Kumar Karnad committed
    import os
    import pytest
    
    import subprocess
    
    Ashwin Kumar Karnad's avatar
    Ashwin Kumar Karnad committed
    import time
    import shutil
    
    mod = importlib.import_module("install-mpsd-software-environment")
    
    test_release_path = (
        "/tmp/test_prepare_env"  # temporary directory for testing used by multiple tests
    )
    
    Ashwin Kumar Karnad's avatar
    Ashwin Kumar Karnad committed
    
    
    Ashwin Kumar Karnad's avatar
    Ashwin Kumar Karnad committed
    def test_os_chdir():
        # create a temporary directory for testing
        temp_dir = "/tmp/test_os_chdir"
        os.mkdir(temp_dir)
    
    Ashwin Kumar Karnad's avatar
    Ashwin Kumar Karnad committed
        # initial current working directory
        initial_cwd = os.getcwd()
    
        # change to the temporary directory using os_chdir
    
        with mod.os_chdir(temp_dir):
    
    Ashwin Kumar Karnad's avatar
    Ashwin Kumar Karnad committed
            assert os.getcwd() == os.path.abspath(temp_dir)
    
        # current working directory should be back to initial directory
        assert os.getcwd() == initial_cwd
    
        # clean up the temporary directory
        os.rmdir(temp_dir)
    
    
    
    def test_prepare_env():
        # simulate running ./install-software-environment.py --release dev-23a --target-directory /tmp/test_prepare_env
    
    Ashwin Kumar Karnad's avatar
    Ashwin Kumar Karnad committed
        # prepare_env is run when cmd is not specified, we can test cmd='prepare'  and cmd=None to check both cases
    
        # if the directory exists, remove it
        if os.path.exists(test_release_path):
            shutil.rmtree(test_release_path)
        # check that the test directory does not exist
        assert not os.path.exists(test_release_path)
    
        run = mod.builder(
            release="dev-23a",
            cmd="prepare",
            target_dir=test_release_path,
            skip_build_cache=False,
        )
        run.run()
        # wait for 20 seconds for the git clone to finish
        # time.sleep(20)
        # check if the directory now is created
        assert os.path.exists(test_release_path)
    
        # check for spack-environments directory
    
    Ashwin Kumar Karnad's avatar
    Ashwin Kumar Karnad committed
        assert os.listdir(test_release_path) == ["spack-environments"]
    
        # check if the git branch is correctl checked out
        assert (
            subprocess.run(
                "cd /tmp/test_prepare_env/spack-environments && git branch",
                shell=True,
                capture_output=True,
    
            )
            .stdout.decode("utf-8")
            .split("\n")[0]
    
    Ashwin Kumar Karnad's avatar
    Ashwin Kumar Karnad committed
            == "* dev-23a"
    
        )
        # make sure running the command again raises ValueError
        with pytest.raises(ValueError):
    
    Ashwin Kumar Karnad's avatar
    Ashwin Kumar Karnad committed
            run = mod.builder(
                release="dev-23a",
                cmd=None,
                target_dir=test_release_path,
                skip_build_cache=False,
            )
            run.run()
    
        # check that ValueError is  not raised if skip_dir_check is True
        run = mod.builder(
            release="dev-23a",
            cmd=None,
            target_dir=test_release_path,
            skip_build_cache=False,
        )
        run.skip_dir_check = True
        run.run()
        # clean up the temporary directory
        shutil.rmtree(test_release_path)
    
    
    
    def test_setup_log_cmd():
        # check that logs/install-software-environment.log is updated when the module is run
        log_file = "logs/install-software-environment.log"
        if os.path.exists(log_file):
            initial_bytes = os.path.getsize(log_file)
        else:
            initial_bytes = 0
    
        # run the prepare_env functionality
        run = mod.builder(
            release="dev-23a",
            cmd=None,
            target_dir=test_release_path,
            skip_build_cache=False,
        )
        run.skip_dir_check = True
        run.run()
    
        # check that logs/install-software-environment.log is updated
        assert os.path.exists("logs/install-software-environment.log")
        assert os.path.getsize("logs/install-software-environment.log") > initial_bytes