test_sub_classes.py 3.61 KB
Newer Older
1 2 3
# -*- coding:utf-8 -*-

import data_repo_renderer
4
import textwrap
5 6 7 8 9 10 11 12 13
import pytest


def test_base_class_generates_empty_string():
    assert data_repo_renderer.Renderer(yaml_dict={}).__str__ == ""


def test_curl_single_file_rendering():
    yaml_dict = {"prefix": "pref", "file_name": "fn", "url": "http://url"}
14
    target_string = "curl -L -o \"pref/fn\" \"http://url\"\n"
15 16 17 18
    renderer = data_repo_renderer.CurlSingleFile(yaml_dict=yaml_dict)
    assert renderer.__str__ == target_string


19
def test_wget_recursive_rendering_with_excluded_dirs_and_accept_files():
Willi Rath's avatar
Willi Rath committed
20 21 22 23 24 25 26 27 28
    yaml_dict = {"prefix": "pref", "cut_dirs": 77, "url": "http://url",
                 "accept_files": "*.*", "exclude_directories": "/asdf,/zxcv/a"}
    target_string = ("wget -nv -r -c -np -nH --cut-dirs=77 "
                     "--accept \"*.*\" -X \"/asdf,/zxcv/a\" -P \"pref\" "
                     "\"http://url\"\n")
    renderer = data_repo_renderer.WgetRecursive(yaml_dict=yaml_dict)
    assert renderer.__str__ == target_string


29 30
def test_wget_recursive_rendering_without_excluded_dirs_and_accept_files():
    yaml_dict = {"prefix": "pref", "cut_dirs": 77, "url": "http://url"}
Willi Rath's avatar
Willi Rath committed
31
    target_string = ("wget -nv -r -c -np -nH --cut-dirs=77 "
32
                     "-P \"pref\" \"http://url\"\n")
33 34
    renderer = data_repo_renderer.WgetRecursive(yaml_dict=yaml_dict)
    assert renderer.__str__ == target_string
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51


def test_loading_credentials():
    yaml_dict = {"credential_files": ["~/.data_repo_creds/SOMETHING.cred",
                                      "~/.data_repo_creds/SOMEOTHER.cred"]}
    target_string = textwrap.dedent("""
        source "~/.data_repo_creds/SOMETHING.cred"
        source "~/.data_repo_creds/SOMEOTHER.cred"
        """)
    renderer = data_repo_renderer.LoadCredentials(yaml_dict=yaml_dict)
    assert renderer.__str__ == target_string


def test_wget_recursive_cred_rendering():
    yaml_dict = {"prefix": "pref", "cut_dirs": 77, "url": "http://url",
                 "accept_files": "*.*", "username_var": "ASDF_USER",
                 "password_var": "ASDF_PWD"}
Willi Rath's avatar
Willi Rath committed
52
    target_string = ("wget -nv -r -c -np -nH --cut-dirs=77 "
53 54 55 56 57
                     "--user=\"$ASDF_USER\" --password=\"$ASDF_PWD\" "
                     "--accept \"*.*\" -P \"pref\" \"http://url\"\n")
    renderer = data_repo_renderer.WgetRecursiveCred(yaml_dict=yaml_dict)
    print(renderer.yaml_dict, renderer.template)
    assert renderer.__str__ == target_string
Willi Rath's avatar
Willi Rath committed
58 59 60


def test_jblob_cred_rendering():
61 62
    yaml_dict = {"prefix": "pref", "cera_data_sets": ["example_ds_01",
                                                      "example_ds_02"],
Willi Rath's avatar
Willi Rath committed
63 64
                 "username_var": "ASDF_USER", "password_var": "ASDF_PWD"}
    target_string = textwrap.dedent("""\
65 66 67 68

        # == Get data from CERA ==
        # Download Jblob to a tmp directory and use it to download a list of
        # given data sets from CERA:
Willi Rath's avatar
Willi Rath committed
69 70 71 72 73 74 75 76 77 78 79 80 81
        jblob_tmp_dir=$(mktemp -d)
        pushd "${jblob_tmp_dir}"
        curl \\
            "http://cera-www.dkrz.de/jblob/Jblob-2.0.10.zip" \\
            -o JBlob-2.0.10.zip
        unzip JBlob-2.0.10.zip
        popd
        jblob_jar="${jblob_tmp_dir}"/Jblob-2.0.10/Jblob.jar
        commons_jar="${jblob_tmp_dir}"/Jblob-2.0.10/commons-codec-1.4.jar
        java -cp "${jblob_jar}":"${commons_jar}" \\
            de.dkrz.cera.applications.JblobClient \\
            --username "${ASDF_USER}" \\
            --password "${ASDF_PWD}" \\
82
            --dataset example_ds_01 example_ds_02 \\
Willi Rath's avatar
Willi Rath committed
83 84
            --dir pref
        rm -rf "${jblob_tmp_dir}"
85
        # == Get data from CERA ==
Willi Rath's avatar
Willi Rath committed
86 87 88 89

        """)
    renderer = data_repo_renderer.JblobCred(yaml_dict=yaml_dict)
    assert renderer.__str__ == target_string