From f099c8aaa7aeae805f7534382bfef789894abffb Mon Sep 17 00:00:00 2001 From: mbeierl Date: Fri, 11 Dec 2015 15:31:17 -0500 Subject: Workload reporting Use a local db to track start/end times of runs so we can go back to the carbon db and summarize values at reporting time based off the raw data. Change-Id: Ie62afd339fd1c15d82bc56c93c7cba5bd4f90fe2 JIRA: STORPERF-29 Signed-off-by: mbeierl --- storperf/tests/db_tests/job_db_test.py | 174 +++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 storperf/tests/db_tests/job_db_test.py (limited to 'storperf/tests/db_tests/job_db_test.py') diff --git a/storperf/tests/db_tests/job_db_test.py b/storperf/tests/db_tests/job_db_test.py new file mode 100644 index 0000000..d9b10a2 --- /dev/null +++ b/storperf/tests/db_tests/job_db_test.py @@ -0,0 +1,174 @@ +############################################################################## +# Copyright (c) 2015 EMC and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +import unittest + +import mock + +from db.job_db import JobDB + + +class JobDBTest(unittest.TestCase): + + def setUp(self): + JobDB.db_name = ":memory:" + self.job = JobDB() + + @mock.patch("uuid.uuid4") + def test_create_job(self, mock_uuid): + expected = "ABCDE-12345" + mock_uuid.side_effect = (expected,) + + self.job.create_job_id() + + actual = self.job.job_id + + self.assertEqual( + expected, actual, "Did not expect: " + str(actual)) + + @mock.patch("uuid.uuid4") + def test_duplicate_job_generated(self, mock_uuid): + duplicate = "EDCBA-12345" + expected = "EDCBA-54321" + + mock_uuid.side_effect = (duplicate, duplicate, expected,) + + self.job.create_job_id() + self.job.create_job_id() + + actual = self.job.job_id + + self.assertEqual( + expected, actual, "Did not expect: " + str(actual)) + + @mock.patch("uuid.uuid4") + @mock.patch("calendar.timegm") + def test_start_job(self, mock_calendar, mock_uuid): + job_id = "ABCDE-12345" + start_time = "12345" + mock_calendar.side_effect = (start_time,) + mock_uuid.side_effect = (job_id,) + workload_name = "Workload" + + cursor = self.job.db.cursor() + + row = cursor.execute( + """select * from jobs + where job_id = ? + and workload = ?""", + (job_id, workload_name,)) + + self.assertEqual(None, + row.fetchone(), + "Should not have been a row in the db") + + self.job.start_workload(workload_name) + + cursor.execute( + """select job_id, workload, start from jobs + where job_id = ? + and workload = ?""", + (job_id, workload_name,)) + + row = cursor.fetchone() + + self.assertNotEqual(None, row, "Should be a row in the db") + self.assertEqual(job_id, row[0], "Did not expect " + str(row[0])) + self.assertEqual( + workload_name, row[1], "Did not expect " + str(row[1])) + self.assertEqual(start_time, row[2], "Did not expect " + str(row[2])) + + @mock.patch("uuid.uuid4") + @mock.patch("calendar.timegm") + def test_end_job(self, mock_calendar, mock_uuid): + job_id = "ABCDE-12345" + start_time = "12345" + end_time = "54321" + mock_calendar.side_effect = (start_time, end_time,) + mock_uuid.side_effect = (job_id,) + workload_name = "Workload" + + self.job.start_workload(workload_name) + self.job.end_workload(workload_name) + + cursor = self.job.db.cursor() + cursor.execute( + """select job_id, workload, start, end from jobs + where job_id = ? + and workload = ?""", + (job_id, workload_name,)) + + row = cursor.fetchone() + + self.assertNotEqual(None, row, "Should be a row in the db") + self.assertEqual(job_id, row[0], "Did not expect " + str(row[0])) + self.assertEqual( + workload_name, row[1], "Did not expect " + str(row[1])) + self.assertEqual(start_time, row[2], "Did not expect " + str(row[2])) + self.assertEqual(end_time, row[3], "Did not expect " + str(row[3])) + + @mock.patch("uuid.uuid4") + @mock.patch("calendar.timegm") + def test_duplicate_start_job(self, mock_calendar, mock_uuid): + job_id = "ABCDE-12345" + start_time_1 = "12345" + start_time_2 = "12346" + + mock_calendar.side_effect = (start_time_1, start_time_2) + mock_uuid.side_effect = (job_id,) + workload_name = "Workload" + + cursor = self.job.db.cursor() + + self.job.start_workload(workload_name) + self.job.start_workload(workload_name) + + cursor.execute( + """select job_id, workload, start from jobs + where job_id = ? + and workload = ?""", + (job_id, workload_name,)) + + row = cursor.fetchone() + + self.assertNotEqual(None, row, "Should be a row in the db") + self.assertEqual(job_id, row[0], "Did not expect " + str(row[0])) + self.assertEqual( + workload_name, row[1], "Did not expect " + str(row[1])) + self.assertEqual(start_time_2, row[2], "Did not expect " + str(row[2])) + + @mock.patch("uuid.uuid4") + @mock.patch("calendar.timegm") + def test_end_job_without_start(self, mock_calendar, mock_uuid): + job_id = "ABCDE-12345" + start_time = "12345" + end_time = "54321" + mock_calendar.side_effect = (start_time, end_time,) + mock_uuid.side_effect = (job_id,) + workload_name = "Workload" + + self.job.end_workload(workload_name) + + cursor = self.job.db.cursor() + cursor.execute( + """select job_id, workload, start, end from jobs + where job_id = ? + and workload = ?""", + (job_id, workload_name,)) + + row = cursor.fetchone() + + self.assertNotEqual(None, row, "Should be a row in the db") + self.assertEqual(job_id, row[0], "Did not expect " + str(row[0])) + self.assertEqual( + workload_name, row[1], "Did not expect " + str(row[1])) + # The start time is set to the same time as end if it was never set + # before + self.assertEqual(start_time, row[2], "Did not expect " + str(row[2])) + self.assertEqual(start_time, row[3], "Did not expect " + str(row[3])) -- cgit 1.2.3-korg