|
10 | 10 | """cernopendata-client file verifier tests."""
|
11 | 11 |
|
12 | 12 | import os
|
| 13 | +import subprocess |
13 | 14 |
|
14 | 15 | from click.testing import CliRunner
|
15 |
| -from cernopendata_client.cli import download_files |
| 16 | +from cernopendata_client.cli import download_files, verify_files |
16 | 17 | from cernopendata_client.verifier import (
|
17 | 18 | get_file_size,
|
18 | 19 | get_file_checksum,
|
@@ -61,6 +62,104 @@ def test_get_file_info_local_good_input():
|
61 | 62 | assert test_result[0]["size"] == 3644
|
62 | 63 | assert test_result[0]["checksum"] == "adler32:be83a186"
|
63 | 64 |
|
| 65 | + # now test verifier |
| 66 | + test_verifier_files = CliRunner() |
| 67 | + test_result = test_verifier_files.invoke(verify_files, ["--recid", 3005]) |
| 68 | + assert test_result.exit_code == 0 |
| 69 | + assert test_result.output.endswith("\n==> Success!\n") |
| 70 | + |
| 71 | + # remove test file |
| 72 | + if os.path.isfile(test_file): |
| 73 | + os.remove(test_file) |
| 74 | + |
| 75 | + |
| 76 | +def test_get_file_info_local_good_input_wrong_count(): |
| 77 | + """Test get_file_info_local() for good inputs simulating wring file count.""" |
| 78 | + |
| 79 | + # remove test file |
| 80 | + test_file = "3005/0d0714743f0204ed3c0144941e6ce248.configFile.py" |
| 81 | + if os.path.isfile(test_file): |
| 82 | + os.remove(test_file) |
| 83 | + |
| 84 | + # first download it |
| 85 | + test_download_files = CliRunner() |
| 86 | + test_result = test_download_files.invoke(download_files, ["--recid", 3005]) |
| 87 | + assert test_result.exit_code == 0 |
| 88 | + assert os.path.isfile(test_file) is True |
| 89 | + assert os.path.getsize(test_file) == 3644 |
| 90 | + assert test_result.output.endswith("\n==> Success!\n") |
| 91 | + |
| 92 | + # simulate getting more files |
| 93 | + cmd = "cp {} {}.extra".format(test_file, test_file) |
| 94 | + subprocess.check_output(cmd, shell=True) |
| 95 | + |
| 96 | + # now test verifier |
| 97 | + test_verifier_files = CliRunner() |
| 98 | + test_result = test_verifier_files.invoke(verify_files, ["--recid", 3005]) |
| 99 | + assert test_result.exit_code == 1 |
| 100 | + |
| 101 | + # remove test files |
| 102 | + if os.path.isfile(test_file + ".extra"): |
| 103 | + os.remove(test_file + ".extra") |
| 104 | + if os.path.isfile(test_file): |
| 105 | + os.remove(test_file) |
| 106 | + |
| 107 | + |
| 108 | +def test_get_file_info_local_good_input_wrong_checksum(): |
| 109 | + """Test get_file_info_local() for good inputs simulating wrong file checksum.""" |
| 110 | + |
| 111 | + # remove test file |
| 112 | + test_file = "3005/0d0714743f0204ed3c0144941e6ce248.configFile.py" |
| 113 | + if os.path.isfile(test_file): |
| 114 | + os.remove(test_file) |
| 115 | + |
| 116 | + # first download it |
| 117 | + test_download_files = CliRunner() |
| 118 | + test_result = test_download_files.invoke(download_files, ["--recid", 3005]) |
| 119 | + assert test_result.exit_code == 0 |
| 120 | + assert os.path.isfile(test_file) is True |
| 121 | + assert os.path.getsize(test_file) == 3644 |
| 122 | + assert test_result.output.endswith("\n==> Success!\n") |
| 123 | + |
| 124 | + # simulate checksum change |
| 125 | + cmd = "sed -i -e 's,a,b,g' {}".format(test_file) |
| 126 | + subprocess.check_output(cmd, shell=True) |
| 127 | + |
| 128 | + # now test verifier |
| 129 | + test_verifier_files = CliRunner() |
| 130 | + test_result = test_verifier_files.invoke(verify_files, ["--recid", 3005]) |
| 131 | + assert test_result.exit_code == 1 |
| 132 | + |
| 133 | + # remove test file |
| 134 | + if os.path.isfile(test_file): |
| 135 | + os.remove(test_file) |
| 136 | + |
| 137 | + |
| 138 | +def test_get_file_info_local_good_input_wrong_size(): |
| 139 | + """Test get_file_info_local() for good inputs simulating wrong file size.""" |
| 140 | + |
| 141 | + # remove test file |
| 142 | + test_file = "3005/0d0714743f0204ed3c0144941e6ce248.configFile.py" |
| 143 | + if os.path.isfile(test_file): |
| 144 | + os.remove(test_file) |
| 145 | + |
| 146 | + # first download it |
| 147 | + test_download_files = CliRunner() |
| 148 | + test_result = test_download_files.invoke(download_files, ["--recid", 3005]) |
| 149 | + assert test_result.exit_code == 0 |
| 150 | + assert os.path.isfile(test_file) is True |
| 151 | + assert os.path.getsize(test_file) == 3644 |
| 152 | + assert test_result.output.endswith("\n==> Success!\n") |
| 153 | + |
| 154 | + # simulate checksum change |
| 155 | + cmd = "sed -i -e 's,a,bbbbbb,g' {}".format(test_file) |
| 156 | + subprocess.check_output(cmd, shell=True) |
| 157 | + |
| 158 | + # now test verifier |
| 159 | + test_verifier_files = CliRunner() |
| 160 | + test_result = test_verifier_files.invoke(verify_files, ["--recid", 3005]) |
| 161 | + assert test_result.exit_code == 1 |
| 162 | + |
64 | 163 | # remove test file
|
65 | 164 | if os.path.isfile(test_file):
|
66 | 165 | os.remove(test_file)
|
0 commit comments