def test_peersplit(self): # Create jigs fakesockmod = jigs.SocketModuleJig() logjig = jigs.FileJig() faketimemod = jigs.TimeModuleJig() fakeosmod = jigs.OSModuleJig() try: # Splice in jigs loadtemp = self.target._NTPStats__load_stem self.target._NTPStats__load_stem = self.load_stem_jig prostemp = self.target._NTPStats__process_stem self.target._NTPStats__process_stem = self.process_stem_jig 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 TDP = self.target.DefaultPeriod faketimemod.time_returns = [TDP * 2] fakeosmod.path.isdir_returns = [True] TestNTPStats.load_stem_returns = [[]] * 6 TestNTPStats.process_stem_returns = [[]] * 6 cls = self.target("/foo/bar") cls.peerstats = [[604830000, "604830.0", "1.2.3.4"], [604831000, "604831.0", "1.2.3.4"], [604840000, "604840.0", "1.2.3.4"], [604841000, "604841.0", "5.6.7.8"]] # Test self.assertEqual( cls.peersplit(), { '1.2.3.4': [[604830000, '604830.0', '1.2.3.4'], [604831000, '604831.0', '1.2.3.4'], [604840000, '604840.0', '1.2.3.4']], '5.6.7.8': [[604841000, '604841.0', '5.6.7.8']] }) self.target.peermap["1.2.3.4"][0][0] = 42 # Test that it uses cache self.assertEqual( cls.peersplit(), { '1.2.3.4': [[42, '604830.0', '1.2.3.4'], [604831000, '604831.0', '1.2.3.4'], [604840000, '604840.0', '1.2.3.4']], '5.6.7.8': [[604841000, '604841.0', '5.6.7.8']] }) finally: ntp.statfiles.socket = socktemp ntp.statfiles.os = ostemp ntp.statfiles.time = timetemp sys.stderr = errtemp self.target._NTPStats__load_stem = loadtemp self.target._NTPStats__process_stem = prostemp
def test_tempssplit(self): # Create jigs fakesockmod = jigs.SocketModuleJig() logjig = jigs.FileJig() faketimemod = jigs.TimeModuleJig() fakeosmod = jigs.OSModuleJig() try: # Splice in jigs loadtemp = self.target._NTPStats__load_stem self.target._NTPStats__load_stem = self.load_stem_jig prostemp = self.target._NTPStats__process_stem self.target._NTPStats__process_stem = self.process_stem_jig 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 TDP = self.target.DefaultPeriod faketimemod.time_returns = [TDP * 2] fakeosmod.path.isdir_returns = [True] TestNTPStats.load_stem_returns = [[]] * 6 TestNTPStats.process_stem_returns = [[]] * 6 cls = self.target("/foo/bar") cls.temps = [["604801.25", "40594", "90"], ["604802.25", "40594", "90"], ["argh!"], ["604803.25", "40594", "100"], ["604804.25", "40594", "100"], ["604804.25", "40594", "110"], ["604804.25", "40594", "110"], ["604804.25", "40594", "120"], ["604804.25", "40594", "120"]] # Test self.assertEqual( cls.tempssplit(), { '90': [['604801.25', '40594', '90'], ['604802.25', '40594', '90']], '100': [['604803.25', '40594', '100'], ['604804.25', '40594', '100']], '110': [['604804.25', '40594', '110'], ['604804.25', '40594', '110']], '120': [['604804.25', '40594', '120'], ['604804.25', '40594', '120']] }) finally: ntp.statfiles.socket = socktemp ntp.statfiles.os = ostemp ntp.statfiles.time = timetemp sys.stderr = errtemp self.target._NTPStats__load_stem = loadtemp self.target._NTPStats__process_stem = prostemp
def test___init__(self): # Create jigs 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 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 opentemp = open ntp.statfiles.open = self.open_jig # Test simplest TDP = self.target.DefaultPeriod faketimemod.time_returns = [TDP * 2] fakeosmod.path.isdir_returns = [True] 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 ] self.open_returns = [ jigs.FileJig(["40594 10\n", "40594 11"]), jigs.FileJig(["40594 31\n", "50594 -12\n", "40594 30\n"]), 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 * 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] self.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] self.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.os = ostemp ntp.statfiles.time = timetemp sys.stderr = errtemp
def test___load_statfiles(self): # Create jigs 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 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 opentemp = open ntp.statfiles.open = self.open_jig prostemp = self.target._NTPStats__process_stem self.target._NTPStats__process_stem = self.process_stem_jig # Set up repetable data TDP = self.target.DefaultPeriod faketimemod.time_returns = [TDP * 2] fakeosmod.path.isdir_returns = [True] getmtime_data = [ TDP + 1, TDP + 2, TDP + 3, TDP + 4, TDP + 5, TDP + 6, TDP + 7, TDP + 8, TDP + 9, TDP + 10, TDP + 11, TDP - 1 ] glob_data = [("/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 # Load data into jigs # need to do it this time so that __init__ will work fakeosmod.path.getmtime_returns = getmtime_data[:] fakeglobmod.glob_returns = glob_data[:] self.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"]) ] TestNTPStats.process_stem_returns = [[]] * 6 cls = self.target("/foo/bar", "sitename") # Reload and test fakeosmod.path.getmtime_returns = getmtime_data[:] fakeglobmod.glob_returns = glob_data[:] self.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"]) ] TestNTPStats.process_stem_returns = [[]] * 6 self.assertEqual( cls._NTPStats__load_stem("/foo/bar", "clockstats"), ['40594 10\n', '40594 11', '40594 20\n', '40594 21']) self.assertEqual( cls._NTPStats__load_stem("/foo/bar", "peerstats"), ['40594 30\n', '40594 31', '40594 40\n', '40594 41']) self.assertEqual( cls._NTPStats__load_stem("/foo/bar", "loopstats"), ['40594 50\n', '40594 51', '40594 60\n', '40594 61']) self.assertEqual( cls._NTPStats__load_stem("/foo/bar", "rawstats"), ['40594 70\n', '40594 71', '40594 80\n', '40594 81']) self.assertEqual(cls._NTPStats__load_stem("/foo/bar", "temps"), [ "604801.25 40594 90\n", "#blah", "604802.25 40594 91", "604803.25 40594 100\n", "#blah", "604804.25 40594 101" ]) self.assertEqual( cls._NTPStats__load_stem("/foo/bar", "gpsd"), ['604805.25 40594 110\n', '#blah', '604806.25 40594 111']) finally: ntp.statfiles.gzip = gziptemp ntp.statfiles.socket = socktemp ntp.statfiles.os = ostemp ntp.statfiles.time = timetemp ntp.statfiles.glob = globtemp ntp.statfiles.open = opentemp sys.stderr = errtemp self.target._NTPStats__process_stem = prostemp
def test___init__(self): # Create jigs fakegzipmod = jigs.GzipModuleJig() fakesockmod = jigs.SocketModuleJig() logjig = jigs.FileJig() faketimemod = jigs.TimeModuleJig() fakeosmod = jigs.OSModuleJig() fakeglobmod = jigs.GlobModuleJig() load_args = [] load_returns = [] def loadjig(self, statsdir, stem): load_args.append((statsdir, stem)) return load_returns.pop(0) process_args = [] def processjig(self, stem, lines): process_args.append((stem, lines)) return [stem + " " + line for line in lines] try: # Splice in jigs 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 stemtemp = self.target._NTPStats__load_stem self.target._NTPStats__load_stem = loadjig processtemp = self.target._NTPStats__process_stem self.target._NTPStats__process_stem = processjig # Test simplest TDP = self.target.DefaultPeriod faketimemod.time_returns = [TDP * 2] fakeosmod.path.isdir_returns = [True] load_returns = [["clock0", "clock1"], ["peer0", "peer1"], ["loop0", "loop1"], ["raw0", "raw1"], ["temp0", "temp1"], ["gpsd0", "gpsd1"]] cls = self.target("/foo/bar") 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, ["clockstats clock0", "clockstats clock1"]) self.assertEqual(cls.peerstats, ["peerstats peer0", "peerstats peer1"]) self.assertEqual(cls.loopstats, ["loopstats loop0", "loopstats loop1"]) self.assertEqual(cls.rawstats, ["rawstats raw0", "rawstats raw1"]) self.assertEqual(cls.temps, ["temps temp0", "temps temp1"]) self.assertEqual(cls.gpsd, ["gpsd gpsd0", "gpsd gpsd1"]) # Test all arguments faketimemod.time_returns = [TDP * 2] fakesockmod.fqdn_returns = ["jabber"] fakeosmod.path.isdir_returns = [True] load_returns = [[], [], [], [], [], []] cls = self.target("/foo/bar", "Sitename", 200, 50, 150) self.assertEqual(cls.endtime, 150) self.assertEqual(cls.starttime, 50) self.assertEqual(cls.period, 200) self.assertEqual(cls.sitename, "Sitename") 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, []) # Test endtime, but no starttime faketimemod.time_returns = [TDP * 2] fakesockmod.fqdn_returns = ["jabber"] fakeosmod.path.isdir_returns = [True] load_returns = [[], [], [], [], [], []] cls = self.target("/foo/bar", "Sitename", 100, endtime=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, []) 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 endtime, but no starttime faketimemod.time_returns = [TDP * 2] fakesockmod.fqdn_returns = ["jabber"] fakeosmod.path.isdir_returns = [True] load_returns = [[], [], [], [], [], []] cls = self.target("/foo/bar", "Sitename", 100, starttime=150) self.assertEqual(cls.endtime, 250) self.assertEqual(cls.starttime, 150) self.assertEqual(cls.period, 100) self.assertEqual(cls.sitename, "Sitename") 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, []) # Test fqdn sitename logjig.data = [] faketimemod.time_returns = [TDP * 2] fakesockmod.getfqdn_returns = ["jabber"] fakeosmod.path.isdir_returns = [True] load_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.os = ostemp ntp.statfiles.time = timetemp self.target._NTPStats__load_stem = stemtemp self.target._NTPStats__process_stem = processtemp sys.stderr = errtemp