aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMilo Casagrande <milo.casagrande@linaro.org>2015-03-02 17:51:01 +0100
committerMilo Casagrande <milo.casagrande@linaro.org>2015-03-02 17:51:01 +0100
commit2507ba3e4b9c83e9fa345dfd73f7d7f60689794b (patch)
treeec617ae77bb65e3edb5556b72764eabb66fcb7be
parentd2399907205707d5841f0c7b0f58a6b54c317cf5 (diff)
downloadkernel-ci-backend-2507ba3e4b9c83e9fa345dfd73f7d7f60689794b.tar.gz
Fix case of boot time being negative.
* Add tests. Change-Id: If8ed5bb49415bae685b24e871ba4547e763db744
-rw-r--r--app/utils/bootimport.py30
-rw-r--r--app/utils/tests/test_bootimport.py92
2 files changed, 113 insertions, 9 deletions
diff --git a/app/utils/bootimport.py b/app/utils/bootimport.py
index 03be149..0437ebd 100644
--- a/app/utils/bootimport.py
+++ b/app/utils/bootimport.py
@@ -345,15 +345,27 @@ def _update_boot_doc_from_json(boot_doc, boot_json, json_pop_f):
:param json_pop_f: The function used to pop elements out of the JSON object.
:type json_pop_f: function
"""
- time_d = datetime.timedelta(
- seconds=float(json_pop_f(models.BOOT_TIME_KEY, 0.0))
- )
- boot_doc.time = datetime.datetime(
- 1970, 1, 1,
- minute=time_d.seconds / 60,
- second=time_d.seconds % 60,
- microsecond=time_d.microseconds
- )
+ seconds = float(json_pop_f(models.BOOT_TIME_KEY, 0.0))
+ if seconds < 0:
+ seconds = 0.0
+
+ try:
+ if seconds == 0:
+ boot_doc.time = datetime.datetime(
+ 1970, 1, 1, hour=0, minute=0, second=0)
+ else:
+ time_d = datetime.timedelta(seconds=seconds)
+ boot_doc.time = datetime.datetime(
+ 1970, 1, 1,
+ minute=time_d.seconds / 60,
+ second=time_d.seconds % 60,
+ microsecond=time_d.microseconds
+ )
+ except OverflowError:
+ utils.LOG.error("Boot time passed value is too large for a time value")
+ utils.LOG.info("Boot time will be set to 0")
+ boot_doc.time = datetime.datetime(
+ 1970, 1, 1, hour=0, minute=0, second=0)
boot_doc.status = json_pop_f(
models.BOOT_RESULT_KEY, models.UNKNOWN_STATUS
diff --git a/app/utils/tests/test_bootimport.py b/app/utils/tests/test_bootimport.py
index e0fe4ca..22bec01 100644
--- a/app/utils/tests/test_bootimport.py
+++ b/app/utils/tests/test_bootimport.py
@@ -11,10 +11,12 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+import datetime
import json
import logging
import mongomock
import os
+import sys
import tempfile
import types
import unittest
@@ -188,6 +190,96 @@ class TestParseBoot(unittest.TestCase):
finally:
os.remove(boot_log.name)
+ def test_parse_from_file_wrong_boot_time_too_big(self):
+ boot_log = tempfile.NamedTemporaryFile(
+ mode='w+b', bufsize=-1, suffix="json", delete=False
+ )
+ boot_obj = {
+ "job": "job",
+ "kernel": "kernel",
+ "defconfig": "defconfig",
+ "board": "board",
+ "dtb": "dtb",
+ "lab_name": "lab_name",
+ "boot_time": sys.maxint
+ }
+
+ try:
+ with open(boot_log.name, mode="w") as boot_write:
+ boot_write.write(json.dumps(boot_obj))
+
+ doc = utils.bootimport._parse_boot_from_file(
+ boot_log.name, self.db)
+
+ self.assertEqual(doc.board, "board")
+ self.assertEqual(doc.job, "job")
+ self.assertEqual(doc.kernel, "kernel")
+ self.assertEqual(doc.defconfig, "defconfig")
+ self.assertEqual(doc.dtb, "dtb")
+ self.assertEqual(doc.time, datetime.datetime(1970, 1, 1, 0, 0))
+ finally:
+ os.remove(boot_log.name)
+
+ def test_parse_from_file_wrong_boot_time_too_big_negative(self):
+ boot_log = tempfile.NamedTemporaryFile(
+ mode='w+b', bufsize=-1, suffix="json", delete=False
+ )
+ boot_obj = {
+ "job": "job",
+ "kernel": "kernel",
+ "defconfig": "defconfig",
+ "board": "board",
+ "dtb": "dtb",
+ "lab_name": "lab_name",
+ "boot_time": -sys.maxint - 1
+ }
+
+ try:
+ with open(boot_log.name, mode="w") as boot_write:
+ boot_write.write(json.dumps(boot_obj))
+
+ doc = utils.bootimport._parse_boot_from_file(
+ boot_log.name, self.db)
+
+ self.assertEqual(doc.board, "board")
+ self.assertEqual(doc.job, "job")
+ self.assertEqual(doc.kernel, "kernel")
+ self.assertEqual(doc.defconfig, "defconfig")
+ self.assertEqual(doc.dtb, "dtb")
+ self.assertEqual(doc.time, datetime.datetime(1970, 1, 1, 0, 0))
+ finally:
+ os.remove(boot_log.name)
+
+ def test_parse_from_file_wrong_boot_time_negative(self):
+ boot_log = tempfile.NamedTemporaryFile(
+ mode='w+b', bufsize=-1, suffix="json", delete=False
+ )
+ boot_obj = {
+ "job": "job",
+ "kernel": "kernel",
+ "defconfig": "defconfig",
+ "board": "board",
+ "dtb": "dtb",
+ "lab_name": "lab_name",
+ "boot_time": -1500.0
+ }
+
+ try:
+ with open(boot_log.name, mode="w") as boot_write:
+ boot_write.write(json.dumps(boot_obj))
+
+ doc = utils.bootimport._parse_boot_from_file(
+ boot_log.name, self.db)
+
+ self.assertEqual(doc.board, "board")
+ self.assertEqual(doc.job, "job")
+ self.assertEqual(doc.kernel, "kernel")
+ self.assertEqual(doc.defconfig, "defconfig")
+ self.assertEqual(doc.dtb, "dtb")
+ self.assertEqual(doc.time, datetime.datetime(1970, 1, 1, 0, 0))
+ finally:
+ os.remove(boot_log.name)
+
def test_parse_from_file_valid(self):
boot_log = tempfile.NamedTemporaryFile(
mode='w+b', bufsize=-1, suffix="json", delete=False