Commit a4e77623 by Rafael Monnerat

slapos/test: Added more asserts

1 parent cfca57e5
......@@ -730,6 +730,14 @@ class PromiseLauncher(object):
if promise_result.hasFailed() and not failed_promise_name:
failed_promise_name = promise_name
failed_promise_output = promise_result.message
else:
# The promise was skip, so for statistic point of view we preserve
# the previous result
if promise_name in new_state_dict:
if new_state_dict[promise_name][0] == "FAILED":
error += 1
else:
success += 1
if not self.run_only_promise_list and os.path.exists(self.legacy_promise_folder) \
and os.path.isdir(self.legacy_promise_folder):
......@@ -773,8 +781,24 @@ class PromiseLauncher(object):
if promise_result.hasFailed() and not failed_promise_name:
failed_promise_name = promise_name
failed_promise_output = promise_result.message
else:
# The promise was skip, so for statistic point of view we preserve
# the previous result
if promise_name in new_state_dict:
if new_state_dict[promise_name][0] == "FAILED":
error += 1
else:
success += 1
self._updateFolderOwner(self.promise_output_dir)
# Save Global State
with open(promise_status_file, "w") as f:
json.dump(new_state_dict, f)
self._saveStatisticsData(promise_stats_file,
report_date, success, error)
if self._skipped_amount > 0:
self.logger.info("%s promises didn't need to be checked." % \
self._skipped_amount)
......@@ -783,10 +807,3 @@ class PromiseLauncher(object):
failed_promise_name,
failed_promise_output))
# Save Global State
with open(promise_status_file, "w") as f:
json.dump(new_state_dict, f)
self._saveStatisticsData(promise_stats_file,
report_date, success, error)
......@@ -219,6 +219,26 @@ class RunPromise(GenericPromise):
expected_dict = expected_history % {'name': name}
self.assertEqual(json.loads(expected_dict), result_dict)
def assertSuccessStatsResult(self, success=1, error=0, expected_stats=None):
if not expected_stats:
expected_stats = """{
"data": ["Date, Success, Error, Warning",
"__DATE__, %(success)s, %(error)s, "
]
}"""
stats_file = os.path.join(self.partition_dir, PROMISE_STATE_FOLDER_NAME, 'promise_stats.json')
self.assertTrue(os.path.exists(stats_file))
with open(stats_file) as f:
result_dict = json.loads(f.read())
result_dict.pop('date')
expected_dict = expected_stats % {'success': success, "error": error}
copy = result_dict["data"]
for nline in range(1, len(result_dict["data"])):
line = result_dict["data"][nline]
result_dict["data"][nline] = "__DATE__,%s" % ",".join(line.split(',')[1:])
self.assertEqual(json.loads(expected_dict), result_dict)
class TestSlapOSPromiseLauncher(TestSlapOSPromiseMixin):
def test_promise_match_interface(self):
......@@ -388,6 +408,7 @@ class RunPromise(GenericPromise):
self.assertSuccessResult("my_promise")
self.assertSuccessHistoryResult("my_promise")
self.assertSuccessStatsResult(1)
def test_runpromise_multiple(self):
promise_name = 'my_promise.py'
......@@ -407,6 +428,7 @@ class RunPromise(GenericPromise):
self.assertSuccessHistoryResult("my_promise")
self.assertSuccessHistoryResult("my_second_promise")
self.assertSuccessStatsResult(2)
def test_runpromise_multiple_times_same_promise(self):
promise_name = 'my_promise.py'
......@@ -420,6 +442,7 @@ class RunPromise(GenericPromise):
self.launcher.run()
time.sleep(1)
self.launcher.run()
time.sleep(1)
self.assertTrue(os.path.exists(state_folder))
self.assertTrue(os.path.exists(os.path.join(self.log_dir, 'my_promise.log')))
......@@ -440,6 +463,14 @@ class RunPromise(GenericPromise):
}]
}""")
self.assertSuccessStatsResult(1, expected_stats = """{
"data": ["Date, Success, Error, Warning",
"__DATE__, %(success)s, %(error)s, ",
"__DATE__, %(success)s, %(error)s, ",
"__DATE__, %(success)s, %(error)s, "
]
}""")
def test_runpromise_multiple_times_same_promise_with_failure(self):
promise_name = 'my_promise.py'
self.configureLauncher()
......@@ -456,6 +487,7 @@ class RunPromise(GenericPromise):
time.sleep(1)
with self.assertRaises(PromiseError):
self.launcher.run()
time.sleep(1)
self.assertTrue(os.path.exists(state_folder))
self.assertTrue(os.path.exists(os.path.join(self.log_dir, 'my_promise.log')))
......@@ -474,6 +506,13 @@ class RunPromise(GenericPromise):
"status": "ERROR"
}]
}""")
self.assertSuccessStatsResult(1, expected_stats = """{
"data": ["Date, Success, Error, Warning",
"__DATE__, %(success)s, %(error)s, ",
"__DATE__, 0, 1, ",
"__DATE__, 0, 1, "
]
}""")
def test_runpromise_multiple_times_same_promise_with_flaky_failures(self):
promise_name = 'my_promise.py'
......@@ -515,6 +554,14 @@ class RunPromise(GenericPromise):
"status": "OK"
}]
}""")
self.assertSuccessStatsResult(1, expected_stats = """{
"data": ["Date, Success, Error, Warning",
"__DATE__, %(success)s, %(error)s, ",
"__DATE__, 0, 1, ",
"__DATE__, %(success)s, %(error)s, "
]
}""")
def test_runpromise_no_logdir(self):
......@@ -529,6 +576,7 @@ class RunPromise(GenericPromise):
self.assertTrue(os.path.exists(state_file))
self.assertFalse(os.path.exists(os.path.join(self.log_dir, 'my_promise.log')))
self.assertSuccessHistoryResult("my_promise")
self.assertSuccessStatsResult(1)
def test_runpromise_savemethod(self):
promise_name = 'my_promise.py'
......@@ -553,6 +601,7 @@ class RunPromise(GenericPromise):
self.assertTrue(os.path.exists(os.path.join(self.log_dir, 'my_promise.log')))
self.assertSuccessHistoryResult("my_promise")
self.assertSuccessStatsResult(1)
def test_runpromise_savemethod_no_logdir(self):
promise_name = 'my_promise.py'
......@@ -577,6 +626,7 @@ class RunPromise(GenericPromise):
self.assertTrue(os.path.exists(state_file))
self.assertFalse(os.path.exists(os.path.join(self.log_dir, 'my_promise.log')))
self.assertSuccessHistoryResult("my_promise")
self.assertSuccessStatsResult(1)
def test_runpromise_savemethod_anomaly(self):
......@@ -601,6 +651,7 @@ class RunPromise(GenericPromise):
self.assertTrue(os.path.exists(state_file))
self.assertTrue(os.path.exists(os.path.join(self.log_dir, 'my_promise.log')))
self.assertSuccessHistoryResult("my_promise")
self.assertSuccessStatsResult(1)
def test_runpromise_savemethod_multiple(self):
......@@ -642,6 +693,7 @@ class RunPromise(GenericPromise):
"title": "%(name)s"
}]
}""")
self.assertSuccessStatsResult(success=1, error=1)
def test_runpromise_savemethod_multiple_success(self):
first_promise = 'my_first_promise.py'
......@@ -674,6 +726,7 @@ class RunPromise(GenericPromise):
self.assertSuccessHistoryResult("my_second_promise")
self.assertSuccessHistoryResult("my_third_promise")
self.assertSuccessStatsResult(3)
def test_runpromise_fail_and_success(self):
first_promise = 'my_first_promise.py'
......@@ -703,6 +756,12 @@ class RunPromise(GenericPromise):
line = f.readline()
self.assertTrue('success' in line, line)
self.assertSuccessStatsResult(expected_stats = """{
"data": ["Date, Success, Error, Warning",
"__DATE__, 1, 1, ",
"__DATE__, 2, 0, "
]
}""")
def test_runpromise_with_periodicity(self):
first_promise = 'my_first_promise.py'
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!