Commit 72c1088b authored by Jérome Perrin's avatar Jérome Perrin

test/test_collect: fix some timezone dependent tests

This test was using time.strftime which uses the current machine timezone,
but collection is done in UTC timezone, so the test was sometimes failing
because the strftime('%Y-%m-%d') was another day than the same time in
UTC time zone.

This test https://erp5js.nexedi.net/#/test_result_module/20210811-96D6B55F/8
for example had the problem. By using faketime and TZ=Europe/Paris
environment variable, the same problems reproduced locally 100% of times.

The fix is to always use datetime.utcnow().strftime('%Y-%m-%d'), to not
depend on the timezone.
parent 86ce8b8e
Pipeline #16926 passed with stage
in 0 seconds
...@@ -42,7 +42,6 @@ import slapos.slap ...@@ -42,7 +42,6 @@ import slapos.slap
import psutil import psutil
import sqlite3 import sqlite3
import subprocess import subprocess
from time import strftime
from slapos.collect import entity, snapshot, db, reporter from slapos.collect import entity, snapshot, db, reporter
from slapos.cli.entry import SlapOSApp from slapos.cli.entry import SlapOSApp
from six.moves.configparser import ConfigParser from six.moves.configparser import ConfigParser
...@@ -247,7 +246,7 @@ class TestCollectDatabase(unittest.TestCase): ...@@ -247,7 +246,7 @@ class TestCollectDatabase(unittest.TestCase):
self.assertEqual(len(database._getGarbageCollectionDateList(0)), 0) self.assertEqual(len(database._getGarbageCollectionDateList(0)), 0)
self.assertEqual(database._getGarbageCollectionDateList(1), self.assertEqual(database._getGarbageCollectionDateList(1),
[strftime("%Y-%m-%d")]) [datetime.utcnow().strftime("%Y-%m-%d")])
def test_garbage(self): def test_garbage(self):
...@@ -378,9 +377,9 @@ class TestCollectReport(unittest.TestCase): ...@@ -378,9 +377,9 @@ class TestCollectReport(unittest.TestCase):
self.assertEqual([i for i in csv.reader(csv_file)], csv_path_dict[f_path]) self.assertEqual([i for i in csv.reader(csv_file)], csv_path_dict[f_path])
def test_system_json_report(self): def test_system_json_report(self):
database = self.getPopulatedDB(strftime("%Y-%m-%d"), amount=2) database = self.getPopulatedDB(datetime.utcnow().strftime("%Y-%m-%d"), amount=2)
reporter.SystemJSONReporterDumper(database).dump(self.instance_root) reporter.SystemJSONReporterDumper(database).dump(self.instance_root)
date_to_test = strftime("%Y-%m-%d") date_to_test = datetime.utcnow().strftime("%Y-%m-%d")
json_path_dict = { json_path_dict = {
'%s/system_memory_used.json' % self.instance_root: '%s/system_memory_used.json' % self.instance_root:
'[ { "entry": 0.09765625, "time": "%s TIME" }]' % date_to_test, '[ { "entry": 0.09765625, "time": "%s TIME" }]' % date_to_test,
...@@ -418,9 +417,9 @@ class TestCollectReport(unittest.TestCase): ...@@ -418,9 +417,9 @@ class TestCollectReport(unittest.TestCase):
def test_system_csv_report(self): def test_system_csv_report(self):
database = self.getPopulatedDB(strftime("%Y-%m-%d"), amount=2) database = self.getPopulatedDB(datetime.utcnow().strftime("%Y-%m-%d"), amount=2)
reporter.SystemCSVReporterDumper(database).dump(self.instance_root) reporter.SystemCSVReporterDumper(database).dump(self.instance_root)
date_for_test = strftime("%Y-%m-%d") date_for_test = datetime.utcnow().strftime("%Y-%m-%d")
csv_path_dict = {'%s/system_memory_used.csv' % self.instance_root: csv_path_dict = {'%s/system_memory_used.csv' % self.instance_root:
[['time','entry'], ['%s TIME' % date_for_test,'0.09765625']], [['time','entry'], ['%s TIME' % date_for_test,'0.09765625']],
'%s/system_cpu_percent.csv' % self.instance_root: '%s/system_cpu_percent.csv' % self.instance_root:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment