[Git][NTPsec/ntpsec][master] Added missing tests for several functions

Ian Bruene gitlab at mg.gitlab.com
Fri Sep 8 15:49:26 UTC 2017


Ian Bruene pushed to branch master at NTPsec / ntpsec


Commits:
15dd5045 by Ian Bruene at 2017-09-08T15:48:23+00:00
Added missing tests for several functions

- - - - -


3 changed files:

- tests/pylib/jigs.py
- tests/pylib/test_statfiles.py
- tests/pylib/test_util.py


Changes:

=====================================
tests/pylib/jigs.py
=====================================
--- a/tests/pylib/jigs.py
+++ b/tests/pylib/jigs.py
@@ -5,6 +5,7 @@ from __future__ import print_function, division
 
 import socket
 import select
+import os.path
 
 class FileJig:
     def __init__(self):
@@ -177,6 +178,8 @@ class SelectModuleJig:
         self.select_calls = []
         self.select_fail = 0
         self.do_return = []
+        self.fqdn_calls = []
+        self.fqdn_returns = []
 
     def select(self, ins, outs, excepts, timeout=0):
         self.select_calls.append((ins, outs, excepts, timeout))
@@ -191,11 +194,41 @@ class SelectModuleJig:
         else:
             return ([], [], [])
 
+    def getfqdn(self, name=""):
+        self.fqdn_calls.append(name)
+        return self.fqdn_returns.pop(0)
+
+
+class path_mod:
+    def __init__(self):
+        self.isdir_calls = []
+        self.isdir_returns = []
+        self.getmtime_calls = []
+        self.getmtime_returns = []
+        self.join_calls = []
+
+    def isdir(self, dirname):
+        self.isdir_calls.append(dirname)
+        return self.isdir_returns.pop(0)
+
+    def join(self, *args):
+        self.join_calls.append(args)
+        return os.path.join(*args)
+
+    def basename(self, pathname):
+        self.join_calls.append(pathname)
+        return os.path.basename(pathname)
+
+    def getmtime(self, filename):
+        self.getmtime_calls.append(filename)
+        return self.getmtime_returns.pop(0)
+
 
 class OSModuleJig:
     def __init__(self):
         self.isatty_calls = []
         self.isatty_returns = []
+        self.path = path_mod()  # Need os.path
 
     def isatty(self, fd):
         self.isatty_calls.append(fd)
@@ -229,3 +262,25 @@ class TimeModuleJig:
     def time(self):
         self.time_calls += 1
         return self.time_returns.pop(0)
+
+
+class GzipModuleJig:
+    def __init__(self):
+        self.open_calls = []
+        self.files_returned = []
+
+    def open(self, filename, filemode):
+        self.open_calls.append((filename, filemode))
+        fd = FileJig()
+        self.files_returned.append(fd)
+        return fd
+
+
+class GlobModuleJig:
+    def __init__(self):
+        self.glob_calls = []
+        self.glob_returns = []
+
+    def glob(self, pathname):
+        self.glob_calls.append(pathname)
+        return self.glob_returns.pop(0)


=====================================
tests/pylib/test_statfiles.py
=====================================
--- a/tests/pylib/test_statfiles.py
+++ b/tests/pylib/test_statfiles.py
@@ -3,19 +3,22 @@
 
 import unittest
 import ntp.statfiles
+import jigs
+import sys
 
 
-class TestPylibStatfilesMethods(unittest.TestCase):
+class TestPylibStatfiles(unittest.TestCase):
 
     def test_iso_to_posix(self):
-        self.assertEqual(ntp.statfiles.iso_to_posix(1480999786),
-                         1480999786)
-        self.assertEqual(ntp.statfiles.iso_to_posix("2016-12-06T04:49:46"),
-                         1480999786)
+        f = ntp.statfiles.iso_to_posix
+
+        self.assertEqual(f(1480999786), 1480999786)
+        self.assertEqual(f("2016-12-06T04:49:46"), 1480999786)
 
     def test_posix_to_iso(self):
-        self.assertEqual(ntp.statfiles.posix_to_iso(1480999786),
-                         "2016-12-06T04:49:46")
+        f = ntp.statfiles.posix_to_iso
+
+        self.assertEqual(f(1480999786), "2016-12-06T04:49:46")
 
     def test_iso_to_posix_inverts_posix_to_iso(self):
         self.assertEqual(ntp.statfiles.iso_to_posix(
@@ -26,5 +29,95 @@ class TestPylibStatfilesMethods(unittest.TestCase):
             ntp.statfiles.iso_to_posix("2016-12-06T04:49:46")),
             "2016-12-06T04:49:46")
 
+
+class TestNTPStats(unittest.TestCase):
+    target = ntp.statfiles.NTPStats
+
+    def test_unixize(self):
+        f = self.target.unixize
+
+        # Test empty
+        self.assertEqual(f([], 0, 0xFFFFFFFF), [])
+        # Test everything in range
+        self.assertEqual(f(["5 3600", "6 86399", "40587 .125"],
+                           -3600000000, 10000),
+                         [[-3506281200000, "-3506281200.0"],
+                          [-3506112001000, "-3506112001.0"],
+                          [125, "0.125"]])
+        # Test with unparseables, all in range
+        self.assertEqual(f(["heeeey duuuude!", "40588 0"], 0, 86401),
+                         [[86400000, "86400.0"]])
+        # Test something not in range
+        self.assertEqual(f(["40587 0", "40587 1.0625",
+                            "40587 86399", "40588 1"], 1, 86400),
+                         [[1062, "1.0625"], [86399000, "86399.0"]])
+
+    def test_timestamp(self):
+        f = self.target.timestamp
+
+        # Test
+        self.assertEqual(f("12345.6789 blah blah"), 12345.6789)
+
+    def test___init__(self):
+        open_calls = []
+        open_returns = []
+
+        def open_jig(filename, filemode):
+            open_calls.append((filename, filemode))
+            return open_returns.pop(0)
+
+        fakesockmod = jigs.SocketModuleJig()
+        logjig = jigs.FileJig()
+        faketimemod = jigs.TimeModuleJig()
+        fakeosmod = jigs.OSModuleJig()
+        fakeglobmod = jigs.GlobModuleJig()
+        try:
+            socktemp = ntp.statfiles.socket
+            ntp.statfiles.socket = fakesockmod
+            errtemp = sys.stderr
+            sys.stderr = logjig
+            timetemp = ntp.statfiles.time
+            ntp.statfiles.time = faketimemod
+            ostemp = ntp.statfiles.os
+            ntp.statfiles.os = fakeosmod
+            globtemp = ntp.statfiles.glob
+            ntp.statfiles.glob = fakeglobmod
+            global open
+            opentemp = open
+            open = open_jig
+            # Test simplest
+            TDP = self.target.DefaultPeriod
+            faketimemod.time_returns = [TDP * 3]
+            fakesockmod.fqdn_returns = ["jabber"]
+            fakeosmod.path.isdir_returns = [True]
+            TM = TDP * 2  # equal to starttime
+            fakeosmod.path.getmtime_returns = [TM+1, TM+2, TM+3, TM+4,
+                                               TM+5, TM+6, TM+7, TM+8,
+                                               TM+9, TM+10, TM+11, TM-1]
+            fakeglobmod.glob_returns = [("/foo/bar/clockstats0",
+                                         "/foo/bar/clockstats1"),
+                                        ("/foo/bar/peerstats0",
+                                         "/foo/bar/peerstats1"),
+                                        ("/foo/bar/loopstats0",
+                                         "/foo/bar/loopstats1"),
+                                        ("/foo/bar/rawstats0",
+                                         "/foo/bar/rawstats1"),
+                                        ("/foo/bar/temps.0",
+                                         "/foo/bar/temps.1"),
+                                        ("/foo/bar/gpsd.0",
+                                         "/foo/bar/gpsd.1")]  # time kicked
+            cls = self.target("/foo/bar")
+            self.assertEqual(cls.endtime, TDP * 3)
+            self.assertEqual(cls.starttime, TDP * 2)
+            self.assertEqual(cls.sitename, "bar")
+            self.assertEqual(logjig.data, [])
+        finally:
+            ntp.statfiles.socket = socktemp
+            ntp.statfiles.os = ostemp
+            ntp.statfiles.time = timetemp
+            ntp.statfiles.glob = fakeglobmod
+            #open = opentemp
+            sys.stderr = errtemp
+
 if __name__ == '__main__':
     unittest.main()


=====================================
tests/pylib/test_util.py
=====================================
--- a/tests/pylib/test_util.py
+++ b/tests/pylib/test_util.py
@@ -41,6 +41,39 @@ class TestPylibUtilMethods(unittest.TestCase):
         f(jig, "blah", 4, 3)
         self.assertEqual((jig.written, jig.flushed), ("blah", True))
 
+    def test_safeargcast(self):
+        f = ntp.util.safeargcast
+
+        errjig = jigs.FileJig()
+        try:
+            errtemp = sys.stderr
+            sys.stderr = errjig
+            # Test successful int
+            self.assertEqual(f("42", int, "blah %s", "\nDo the needful\n"), 42)
+            self.assertEqual(errjig.data, [])
+            # Test successful float
+            self.assertEqual(f("5.23", float, "blah %s", "\nDo the needful\n"),
+                             5.23)
+            self.assertEqual(errjig.data, [])
+            # Test failure
+            try:
+                f("23.5", int, "blah %s", "\nDo the needful\n")
+                errored = False
+            except SystemExit:
+                errored = True
+            self.assertEqual(errored, True)
+            self.assertEqual(errjig.data, ["blah 23.5", "\nDo the needful\n"])
+        finally:
+            sys.stderr = errtemp
+
+    def test_stdversion(self):
+        f = ntp.util.stdversion
+
+        ver = str(ntp.version.VERSION)
+        tick = str(ntp.version.VCS_TICK)
+        date = str(ntp.version.VCS_DATE)
+        self.assertEqual(f(), "ntpsec-" + ver + "+" + tick + " " + date)
+
     def test_rfc3339(self):
         f = ntp.util.rfc3339
 



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/15dd5045bf347a11b3ab4e125a0f78709dbb9d7d

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/15dd5045bf347a11b3ab4e125a0f78709dbb9d7d
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170908/b8591049/attachment.html>


More information about the vc mailing list