Skip to content

Commit

Permalink
Add some tests for our handlers and worker functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan Hermansson committed Jan 17, 2018
1 parent 26a172a commit 6280bcc
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
17 changes: 17 additions & 0 deletions tests/test_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
port: 8989
base_url: "/api/1.0"
verify_root_dir: "data/verify/"
pdc_root_dir: "data/{}/runfolders/"
dsmc_log_dir: "logs/"
job_timeout: "48h" # maximum run-time for a job
job_ttl: "72h" # maximum time to keep a job in the queue
job_result_ttl: "-1" # maximum time to keep job result; -1 never expires

# Whitelisted DSMC warnings.
#
# ANS1809W = a session with the TSM server has been disconnected: will retry again
# ANS2042W = a symblic link to a file on other fs has been uploaded: acls/extended attributes might not be backed up
# ANS2250W = a TSM core file or crash report was found
#
# See full list at e.g. https://www.ibm.com/support/knowledgecenter/en/SSGSG7_7.1.1/com.ibm.itsm.msgs.client.doc/msgs_client_list_intro.html
whitelisted_warnings: ["ANS1809W", "ANS2042W", "ANS2250W"]
48 changes: 48 additions & 0 deletions tests/test_handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import yaml

from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop
from aiohttp import web

from archive_verify.app import setup_routes

class HandlerTestCase(AioHTTPTestCase):

BASE_URL = ""

def _load_config(self):
with open("tests/test_config.yaml") as config:
return yaml.load(config)

async def get_application(self):
app = web.Application()
app["config"] = self._load_config()
self.BASE_URL = app["config"]["base_url"]
setup_routes(app)
return app

@unittest_run_loop
async def test_root(self):
request = await self.client.request("GET", "/")
assert request.status == 404
text = await request.text()
assert "not found" in text.lower()

@unittest_run_loop
async def test_basic_verify(self):
url = self.BASE_URL + "/verify"
payload = {"host": "testbox", "archive": "test_archive", "description": "test-description"}
request = await self.client.request("POST", url, json=payload)
assert request.status == 200
resp = await request.json()
assert resp["status"] == "pending"
assert resp["job_id"] != ""

@unittest_run_loop
async def test_basic_status_wrong_id(self):
url = self.BASE_URL + "/status/foobar"
request = await self.client.request("GET", url)
assert request.status == 400
resp = await request.json()
assert "no such job foobar found" in resp["msg"].lower()


101 changes: 101 additions & 0 deletions tests/test_workers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import mock
import unittest
import yaml

from archive_verify.workers import _parse_dsmc_return_code, download_from_pdc, compare_md5sum, verify_archive

class TestWorkers(unittest.TestCase):

def setUp(self):
with open("tests/test_config.yaml") as config:
self.config = yaml.load(config)

# Test with whitelisted warnings
def test_dsmc_whitelist_ok(self):
exit_code = 8
whitelist = ["ANS2250W", "ANS5000W"]
output = "TEST\nOUTPUT\nWARNING****************\nSEE ANS2250W FOR MORE INFO\nANS2250E\n"
ret = _parse_dsmc_return_code(exit_code, output, whitelist)
self.assertEqual(ret, True)

# Test with non-whitelisted warnings
def test_dsmc_whitelist_not_ok(self):
exit_code = 8
whitelist = ["ANS2250W", "ANS5000W"]
output = "FOOBAR TEST OKEJ\nWARNING ERROR ANS221E TEST\n*** ANS5050W\n"
ret = _parse_dsmc_return_code(exit_code, output, whitelist)
self.assertEqual(ret, False)

# Test with non-warning exit code
def test_dsmc_unknown_exit_code(self):
exit_code=10
whitelist = ["FOO", "BAR"]
output = "FOO\nBAR\OK\n"
ret = _parse_dsmc_return_code(exit_code, output, whitelist)
self.assertEqual(ret, False)

# Check when dsmc returns 0
@mock.patch('subprocess.Popen')
def test_download_from_pdc_ok(self, mock_popen):
mock_popen.return_value.returncode = 0
mock_popen.return_value.communicate.return_value = ("foobar", '')
ret = download_from_pdc("archive", "descr", "dest", "log-dir", "whitelist")
self.assertEqual(ret, True)

# Check when dsmc returns != 0
def test_download_from_pdc_with_ok_warning(self):
exp_ret = "33232"

with mock.patch('subprocess.Popen') as mock_popen, mock.patch('archive_verify.workers._parse_dsmc_return_code') as mock_parse_dsmc:
mock_popen.return_value.returncode = 42
mock_popen.return_value.communicate.return_value = ("foobar", '')
mock_parse_dsmc.return_value = exp_ret
ret = download_from_pdc("archive", "descr", "dest", "logdir", "whitelist")
self.assertEqual(ret, exp_ret)

# Check with passing checksums
@mock.patch('subprocess.Popen')
def test_compare_md5sum_ok(self, mock_popen):
mock_popen.return_value.returncode = 0
ret = compare_md5sum("archive-dir")
self.assertEqual(ret, True)

# Check with failing checksums
@mock.patch('subprocess.Popen')
def test_compare_md5sum_not_ok(self, mock_popen):
mock_popen.return_value.returncode = 42
ret = compare_md5sum("archive-dir")
self.assertEqual(ret, False)

def test_verify_archive_download_not_ok(self):
with mock.patch('archive_verify.workers.download_from_pdc') as mock_download, mock.patch('rq.get_current_job') as mock_job:
job_id = "42-42-42-24-24-24"
mock_download.return_value = False
mock_job.return_value.id = job_id
ret = verify_archive("my-archive", "my-host", "my-descr", self.config)
self.assertEqual(ret["state"], "error")
self.assertEqual(job_id in ret["path"], True)

def test_verify_archive_verify_not_ok(self):
with mock.patch('archive_verify.workers.download_from_pdc') as mock_download, mock.patch('rq.get_current_job') as mock_job, mock.patch('archive_verify.workers.compare_md5sum') as mock_md5sum:
job_id = "24-24-24-24"
archive = "my-archive-101"
mock_download.return_value = True
mock_job.return_value.id = job_id
mock_md5sum.return_value = False
ret = verify_archive(archive, "my-host", "my-descr", self.config)
self.assertEqual(ret["state"], "error")
self.assertEqual(archive in ret["path"], True)

def test_verify_archive_verify_ok(self):
with mock.patch('archive_verify.workers.download_from_pdc') as mock_download, mock.patch('rq.get_current_job') as mock_job, mock.patch('archive_verify.workers.compare_md5sum') as mock_md5sum:
job_id = "24-24-24-24"
archive = "my-archive-101"
mock_download.return_value = True
mock_job.return_value.id = job_id
mock_md5sum.return_value = True
ret = verify_archive(archive, "my-host", "my-descr", self.config)
self.assertEqual(ret["state"], "done")
self.assertEqual(archive in ret["path"] and job_id in ret["path"], True)


0 comments on commit 6280bcc

Please sign in to comment.