[Git][NTPsec/ntpsec][master] Fixed / completed test for NTPStats.__init__()

Ian Bruene gitlab at mg.gitlab.com
Sat Sep 9 00:42:17 UTC 2017


Ian Bruene pushed to branch master at NTPsec / ntpsec


Commits:
9edcde2d by Ian Bruene at 2017-09-09T00:41:44+00:00
Fixed / completed test for NTPStats.__init__()

- - - - -


2 changed files:

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


Changes:

=====================================
tests/pylib/jigs.py
=====================================
--- a/tests/pylib/jigs.py
+++ b/tests/pylib/jigs.py
@@ -8,10 +8,10 @@ import select
 import os.path
 
 class FileJig:
-    def __init__(self):
+    def __init__(self, returns=[""]):
         self.data = []
         self.flushed = False
-        self.readline_return = [""]
+        self.readline_return = returns
 
     def __enter__(self):
         return self
@@ -34,6 +34,11 @@ class FileJig:
             return self.readline_return.pop(0)
         return ""
 
+    def readlines(self):
+        ret = self.readline_return
+        self.readline_return = []
+        return ret
+
 
 class SocketJig:
     def __init__(self):
@@ -112,6 +117,8 @@ class SocketModuleJig:
         self.socket_fail_connect = False
         self.socketsReturned = []
         self.inet_ntop_calls = []
+        self.getfqdn_calls = []
+        self.getfqdn_returns = []
 
     def getaddrinfo(self, host, port, family=None, socktype=None,
                     proto=None, flags=None):
@@ -149,6 +156,10 @@ class SocketModuleJig:
         self.inet_ntop_calls.append((addr, family))
         return "canon.com"
 
+    def getfqdn(self, name=""):
+        self.getfqdn_calls.append(name)
+        return self.getfqdn_returns.pop(0)
+
 
 class GetpassModuleJig:
     def __init__(self):
@@ -271,9 +282,7 @@ class GzipModuleJig:
 
     def open(self, filename, filemode):
         self.open_calls.append((filename, filemode))
-        fd = FileJig()
-        self.files_returned.append(fd)
-        return fd
+        return self.files_returned.pop(0)
 
 
 class GlobModuleJig:
@@ -283,4 +292,5 @@ class GlobModuleJig:
 
     def glob(self, pathname):
         self.glob_calls.append(pathname)
-        return self.glob_returns.pop(0)
+        ret =  self.glob_returns.pop(0)
+        return ret


=====================================
tests/pylib/test_statfiles.py
=====================================
--- a/tests/pylib/test_statfiles.py
+++ b/tests/pylib/test_statfiles.py
@@ -64,14 +64,22 @@ class TestNTPStats(unittest.TestCase):
 
         def open_jig(filename, filemode):
             open_calls.append((filename, filemode))
-            return open_returns.pop(0)
+            ret = open_returns.pop(0)
+            if ret is None:
+                raise IOError
+            else:
+                return ret
 
+        fakegzipmod = jigs.GzipModuleJig()
         fakesockmod = jigs.SocketModuleJig()
         logjig = jigs.FileJig()
         faketimemod = jigs.TimeModuleJig()
         fakeosmod = jigs.OSModuleJig()
         fakeglobmod = jigs.GlobModuleJig()
         try:
+            # Splice in jigs
+            gziptemp = ntp.statfiles.gzip
+            ntp.statfiles.gzip = fakegzipmod
             socktemp = ntp.statfiles.socket
             ntp.statfiles.socket = fakesockmod
             errtemp = sys.stderr
@@ -82,42 +90,129 @@ class TestNTPStats(unittest.TestCase):
             ntp.statfiles.os = fakeosmod
             globtemp = ntp.statfiles.glob
             ntp.statfiles.glob = fakeglobmod
-            global open
             opentemp = open
-            open = open_jig
+            ntp.statfiles.open = open_jig
             # Test simplest
             TDP = self.target.DefaultPeriod
-            faketimemod.time_returns = [TDP * 3]
-            fakesockmod.fqdn_returns = ["jabber"]
+            faketimemod.time_returns = [TDP * 2]
             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
+            fakeosmod.path.getmtime_returns = [TDP+1, TDP+2, TDP+3, TDP+4,
+                                               TDP+5, TDP+6, TDP+7, TDP+8,
+                                               TDP+9, TDP+10, TDP+11, TDP-1]
+            open_returns = [jigs.FileJig(["40594 10\n", "40594 11"]),
+                            jigs.FileJig(["40594 30\n", "40594 31"]),
+                            jigs.FileJig(["40594 40\n", "40594 41"]),
+                            jigs.FileJig(["40594 50\n", "40594 51"]),
+                            jigs.FileJig(["40594 60\n", "40594 61"]),
+                            jigs.FileJig(["40594 70\n", "40594 71"]),
+                            jigs.FileJig(["40594 80\n", "40594 81"]),
+                            jigs.FileJig(["604801.25 40594 90\n", "#blah",
+                                          "604802.25 40594 91"]),
+                            jigs.FileJig(["604803.25 40594 100\n", "#blah",
+                                          "604804.25 40594 101"]),
+                            jigs.FileJig(["604805.25 40594 110\n", "#blah",
+                                          "604806.25 40594 111"]),
+                            jigs.FileJig(["604807.25 40594 120\n", "#blah",
+                                          "604808.25 40594 121"])]
+            fakegzipmod.files_returned = [jigs.FileJig(["40594 20\n",
+                                                        "40594 21"])]
+            fakeglobmod.glob_returns = [("/foo/bar/clockstats.0",
+                                         "/foo/bar/clockstats.1gz"),
+                                        ("/foo/bar/peerstats.0",
+                                         "/foo/bar/peerstats.1"),
+                                        ("/foo/bar/loopstats.0",
+                                         "/foo/bar/loopstats.1"),
+                                        ("/foo/bar/rawstats.0",
+                                         "/foo/bar/rawstats.1"),
+                                        ("/foo/bar/temps0",
+                                         "/foo/bar/temps1"),
+                                        ("/foo/bar/gpsd0",
+                                         "/foo/bar/gpsd1")]  # time kicked
             cls = self.target("/foo/bar")
-            self.assertEqual(cls.endtime, TDP * 3)
-            self.assertEqual(cls.starttime, TDP * 2)
+            self.assertEqual(cls.endtime, TDP * 2)
+            self.assertEqual(cls.starttime, TDP)
+            self.assertEqual(cls.period, TDP)
             self.assertEqual(cls.sitename, "bar")
             self.assertEqual(logjig.data, [])
+            self.assertEqual(cls.clockstats,
+                             [[604810000, '604810.0'],
+                              [604811000, '604811.0'],
+                              [604820000, '604820.0'],
+                              [604821000, '604821.0']])
+            self.assertEqual(cls.peerstats,
+                             [[604830000, '604830.0'],
+                              [604831000, '604831.0'],
+                              [604840000, '604840.0'],
+                              [604841000, '604841.0']])
+            self.assertEqual(cls.loopstats,
+                             [[604850000, '604850.0'],
+                              [604851000, '604851.0'],
+                              [604860000, '604860.0'],
+                              [604861000, '604861.0']])
+            self.assertEqual(cls.rawstats,
+                             [[604870000, '604870.0'],
+                              [604871000, '604871.0'],
+                              [604880000, '604880.0'],
+                              [604881000, '604881.0']])
+            self.assertEqual(cls.temps,
+                             [[604801250, '604801.25', '40594', '90'],
+                              [604802250, '604802.25', '40594', '91'],
+                              [604803250, '604803.25', '40594', '100'],
+                              [604804250, '604804.25', '40594', '101']])
+            self.assertEqual(cls.gpsd,
+                             [[604805250, '604805.25', '40594', '110'],
+                              [604806250, '604806.25', '40594', '111']])
+            # Test all arguments
+            faketimemod.time_returns = [TDP * 2]
+            fakesockmod.fqdn_returns = ["jabber"]
+            fakeosmod.path.isdir_returns = [True]
+            open_returns = [None]
+            fakeglobmod.glob_returns = [(["/foo/bar/clockstats.0"]),
+                                        ([]), ([]), ([]), ([]), ([])]
+            fakeosmod.path.getmtime_returns = [101, 102, 103, 104, 105, 106]
+            cls = self.target("/foo/bar", "Sitename", 100, 50, 150)
+            self.assertEqual(cls.endtime, 150)
+            self.assertEqual(cls.starttime, 50)
+            self.assertEqual(cls.period, 100)
+            self.assertEqual(cls.sitename, "Sitename")
+            self.assertEqual(logjig.data,
+                             ["ntpviz: WARNING: could not read "
+                              "/foo/bar/clockstats.0\n"])
+            self.assertEqual(cls.clockstats, [])
+            self.assertEqual(cls.peerstats, [])
+            self.assertEqual(cls.loopstats, [])
+            self.assertEqual(cls.rawstats, [])
+            self.assertEqual(cls.temps, [])
+            self.assertEqual(cls.gpsd, [])
+            # Test fqdn sitename
+            logjig.data = []
+            faketimemod.time_returns = [TDP * 2]
+            fakesockmod.getfqdn_returns = ["jabber"]
+            fakeosmod.path.isdir_returns = [True]
+            open_returns = [None]
+            fakeglobmod.glob_returns = [([]), ([]), ([]), ([]), ([]), ([])]
+            fakeosmod.path.getmtime_returns = []
+            cls = self.target("/foo/bar", "ntpstats", 100, 50, 150)
+            self.assertEqual(cls.endtime, 150)
+            self.assertEqual(cls.starttime, 50)
+            self.assertEqual(cls.period, 100)
+            self.assertEqual(cls.sitename, "jabber")
+            self.assertEqual(logjig.data, [])
+            self.assertEqual(cls.clockstats, [])
+            self.assertEqual(cls.peerstats, [])
+            self.assertEqual(cls.loopstats, [])
+            self.assertEqual(cls.rawstats, [])
+            self.assertEqual(cls.temps, [])
+            self.assertEqual(cls.gpsd, [])
         finally:
+            ntp.statfiles.gzip = gziptemp
             ntp.statfiles.socket = socktemp
             ntp.statfiles.os = ostemp
             ntp.statfiles.time = timetemp
             ntp.statfiles.glob = fakeglobmod
-            #open = opentemp
+            ntp.statfiles.open = opentemp
             sys.stderr = errtemp
 
+
 if __name__ == '__main__':
     unittest.main()



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/9edcde2d1a667d246f4183dfdd5650c9fcdef5bd

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/9edcde2d1a667d246f4183dfdd5650c9fcdef5bd
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/20170909/eea8b586/attachment.html>


More information about the vc mailing list