def testStartupFlow(self): client_id = self.SetupClient(0) self._RunSendStartupInfo(client_id) # Check the client's boot time and info. fd = aff4.FACTORY.Open(client_id, token=self.token) client_info = fd.Get(fd.Schema.CLIENT_INFO) boot_time = fd.Get(fd.Schema.LAST_BOOT_TIME) self.assertEqual(client_info.client_name, config.CONFIG["Client.name"]) self.assertEqual(client_info.client_description, config.CONFIG["Client.description"]) # Check that the boot time is accurate. self.assertAlmostEqual(psutil.boot_time(), boot_time.AsSecondsSinceEpoch()) # Run it again - this should not update any record. self._RunSendStartupInfo(client_id) fd = aff4.FACTORY.Open(client_id, token=self.token) self.assertEqual(boot_time.age, fd.Get(fd.Schema.LAST_BOOT_TIME).age) self.assertEqual(client_info.age, fd.Get(fd.Schema.CLIENT_INFO).age) # Simulate a reboot. current_boot_time = psutil.boot_time() with utils.Stubber(psutil, "boot_time", lambda: current_boot_time + 600): # Run it again - this should now update the boot time. self._RunSendStartupInfo(client_id) # Ensure only this attribute is updated. fd = aff4.FACTORY.Open(client_id, token=self.token) self.assertNotEqual(int(boot_time.age), int(fd.Get(fd.Schema.LAST_BOOT_TIME).age)) self.assertEqual(int(client_info.age), int(fd.Get(fd.Schema.CLIENT_INFO).age)) # Now set a new client build time. build_time = compatibility.FormatTime("%a %b %d %H:%M:%S %Y") with test_lib.ConfigOverrider({"Client.build_time": build_time}): # Run it again - this should now update the client info. self._RunSendStartupInfo(client_id) # Ensure the client info attribute is updated. fd = aff4.FACTORY.Open(client_id, token=self.token) self.assertNotEqual(int(client_info.age), int(fd.Get(fd.Schema.CLIENT_INFO).age))
def testTimeZoneConversions(self): time_string = "2011-11-01 10:23:00" # Human readable strings are assumed to always be in UTC # timezone. Initialize from the human readable string. date1 = rdfvalue.RDFDatetime.FromHumanReadable(time_string) self.assertEqual(int(date1), 1320142980000000) self.assertEqual( compatibility.FormatTime("%Y-%m-%d %H:%M:%S", time.gmtime(int(date1) // 1e6)), time_string) # We always stringify the date in UTC timezone. self.assertEqual(str(date1), time_string)
def testTimeZoneConversions(self): time_string = "2011-11-01 10:23:00" # Human readable strings are assumed to always be in UTC # timezone. Initialize from the human readable string. date1 = self.rdfvalue_class.FromHumanReadable(time_string) self.assertEqual(date1.AsMicrosecondsSinceEpoch(), 1320142980000000) self.assertEqual( compatibility.FormatTime("%Y-%m-%d %H:%M:%S", time.gmtime(date1.AsSecondsSinceEpoch())), time_string) # We always stringify the date in UTC timezone. self.assertEqual(str(date1), time_string)
def testStartupHandler(self): with test_lib.ConfigOverrider({ "Database.useForReads": True, "Database.useForReads.message_handlers": True, }): client_id = self.SetupClient(0).Basename() self._RunSendStartupInfo(client_id) si = data_store.REL_DB.ReadClientStartupInfo(client_id) self.assertIsNotNone(si) self.assertEqual(si.client_info.client_name, config.CONFIG["Client.name"]) self.assertEqual(si.client_info.client_description, config.CONFIG["Client.description"]) # Run it again - this should not update any record. self._RunSendStartupInfo(client_id) new_si = data_store.REL_DB.ReadClientStartupInfo(client_id) self.assertEqual(new_si, si) # Simulate a reboot. current_boot_time = psutil.boot_time() with utils.Stubber(psutil, "boot_time", lambda: current_boot_time + 600): # Run it again - this should now update the boot time. self._RunSendStartupInfo(client_id) new_si = data_store.REL_DB.ReadClientStartupInfo(client_id) self.assertIsNotNone(new_si) self.assertNotEqual(new_si.boot_time, si.boot_time) # Now set a new client build time. build_time = compatibility.FormatTime("%a %b %d %H:%M:%S %Y") with test_lib.ConfigOverrider( {"Client.build_time": build_time}): # Run it again - this should now update the client info. self._RunSendStartupInfo(client_id) new_si = data_store.REL_DB.ReadClientStartupInfo(client_id) self.assertIsNotNone(new_si) self.assertNotEqual(new_si.client_info, si.client_info)
def testDefault(self): self.assertRegex(compatibility.FormatTime("%Y-%m-%d %H:%M"), "^\\d{4}\\-\\d{2}\\-\\d{2} \\d{2}:\\d{2}$")
def testTime(self): stime = datetime.datetime.combine( date=datetime.date.today(), time=datetime.time(hour=13, minute=47, second=58)).timetuple() self.assertEqual(compatibility.FormatTime("%H:%M:%S", stime), "13:47:58")
def testDate(self): stime = datetime.date(year=2012, month=3, day=4).timetuple() self.assertEqual(compatibility.FormatTime("%Y-%m-%d", stime), "2012-03-04")
def FormatAsTimestamp(timestamp): if not timestamp: return "-" return compatibility.FormatTime("%Y-%m-%d %H:%M:%S", time.gmtime(timestamp))
def Format(self, fmt): """Return the value as a string formatted as per strftime semantics.""" precondition.AssertType(fmt, Text) stime = time.gmtime(self._value / self.converter) return compatibility.FormatTime(fmt, stime)
def FormatISOTime(t): """Format a time in epoch notation to ISO UTC.""" return compatibility.FormatTime("%Y-%m-%d %H:%M:%S", time.gmtime(t / 1e6))
help="Enabled logging engines, to be copied to Logging.engines in client " "configuration.") config_lib.DEFINE_string( name="ClientBuilder.client_installer_logfile", default="%(Logging.path)/%(Client.name)_installer.txt", help="Logfile for logging the client installation process, to be copied to" " Installer.logfile in client built.") config_lib.DEFINE_string(name="ClientBuilder.maintainer", default="GRR <*****@*****.**>", help="The client package's maintainer.") config_lib.DEFINE_string( name="ClientBuilder.debian_build_time", default=compatibility.FormatTime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()), help="The build time put into the debian package. Needs to be formatted" " like the output of 'date -R'.") config_lib.DEFINE_string( name="ClientBuilder.rpm_build_time", default=compatibility.FormatTime("%a %b %d %Y", time.gmtime()), help="The build time put into the rpm package. Needs to be formatted" " according to the rpm specs.") config_lib.DEFINE_string(name="ClientBuilder.debian_version", default="%(Template.version_numeric)-1", help="The version of the debian package.") config_lib.DEFINE_string( name="ClientBuilder.debian_package_base",
def testStartupFlow(self): with test_lib.ConfigOverrider( {"Database.useForReads.message_handlers": False}): client_id = self.SetupClient(0) rel_client_id = client_id.Basename() data_store.REL_DB.WriteClientMetadata(rel_client_id, fleetspeak_enabled=False) self._RunSendStartupInfo(client_id) # AFF4 client. # Check the client's boot time and info. fd = aff4.FACTORY.Open(client_id, token=self.token) client_info = fd.Get(fd.Schema.CLIENT_INFO) boot_time = fd.Get(fd.Schema.LAST_BOOT_TIME) self.assertEqual(client_info.client_name, config.CONFIG["Client.name"]) self.assertEqual(client_info.client_description, config.CONFIG["Client.description"]) # Check that the boot time is accurate. self.assertAlmostEqual(psutil.boot_time(), boot_time.AsSecondsSinceEpoch()) # objects.ClientSnapshot. si = data_store.REL_DB.ReadClientStartupInfo(rel_client_id) self.assertIsNotNone(si) self.assertEqual(si.client_info.client_name, config.CONFIG["Client.name"]) self.assertEqual(si.client_info.client_description, config.CONFIG["Client.description"]) # Run it again - this should not update any record. self._RunSendStartupInfo(client_id) # AFF4 client. fd = aff4.FACTORY.Open(client_id, token=self.token) self.assertEqual(boot_time.age, fd.Get(fd.Schema.LAST_BOOT_TIME).age) self.assertEqual(client_info.age, fd.Get(fd.Schema.CLIENT_INFO).age) # objects.ClientSnapshot. new_si = data_store.REL_DB.ReadClientStartupInfo(rel_client_id) self.assertEqual(new_si, si) # Simulate a reboot. current_boot_time = psutil.boot_time() with utils.Stubber(psutil, "boot_time", lambda: current_boot_time + 600): # Run it again - this should now update the boot time. self._RunSendStartupInfo(client_id) # AFF4 client. # Ensure only this attribute is updated. fd = aff4.FACTORY.Open(client_id, token=self.token) self.assertNotEqual(int(boot_time.age), int(fd.Get(fd.Schema.LAST_BOOT_TIME).age)) self.assertEqual(int(client_info.age), int(fd.Get(fd.Schema.CLIENT_INFO).age)) # objects.ClientSnapshot. new_si = data_store.REL_DB.ReadClientStartupInfo(rel_client_id) self.assertIsNotNone(new_si) self.assertNotEqual(new_si.boot_time, si.boot_time) # Now set a new client build time. build_time = compatibility.FormatTime("%a %b %d %H:%M:%S %Y") with test_lib.ConfigOverrider( {"Client.build_time": build_time}): # Run it again - this should now update the client info. self._RunSendStartupInfo(client_id) # AFF4 client. # Ensure the client info attribute is updated. fd = aff4.FACTORY.Open(client_id, token=self.token) self.assertNotEqual(int(client_info.age), int(fd.Get(fd.Schema.CLIENT_INFO).age)) # objects.ClientSnapshot. new_si = data_store.REL_DB.ReadClientStartupInfo( rel_client_id) self.assertIsNotNone(new_si) self.assertNotEqual(new_si.client_info, si.client_info)