def setUp(self): super(HostModuleTest, self).setUp() self.common_serial = str(uuid.uuid4()) models.Computer( active=True, hostname='host1', serial=self.common_serial, uuid='UUID1', key_name='UUID1', owner='zaspire', client_version='2.3.1', os_version='10.11', site='NYC', track='stable', config_track='stable', connections_on_corp=1, connections_off_corp=0, uptime=90000.0, root_disk_free=0, user_disk_free=10).put() models.Computer( active=True, hostname='new-host1', serial=self.common_serial, uuid='NEWUUID1==', key_name='NEWUUID1==', owner='user', os_version='10.11', site='NYC', track='stable', config_track='stable', connections_on_corp=1, connections_off_corp=0, uptime=90000.0, root_disk_free=0, user_disk_free=10, client_version='2.7.1').put() models.Computer( active=True, hostname='host10', serial=str(uuid.uuid4()), uuid='UUID2', key_name='UUID2', owner='user', client_version='2.3.2', os_version='10.11', site='MTV', track='stable', config_track='stable', connections_on_corp=1, connections_off_corp=1, uptime=90000.0, root_disk_free=0, user_disk_free=10).put()
def testGenerateComputersSummaryCache(self): today = datetime.datetime.now() models.Computer(active=True, hostname='xyz-macbook', serial='SERIAL', uuid='UUID', owner='zerocool', client_version='2.3.3', os_version='10.10', site='MTV', track='unstable', config_track='unstable', connection_dates=[today], connections_on_corp=0, connections_off_corp=100, uptime=90000.0, root_disk_free=0, user_disk_free=10, preflight_datetime=today).put() reports_cache._GenerateComputersSummaryCache() taskqueue_stub = self.testbed.get_stub(testbed.TASKQUEUE_SERVICE_NAME) tasks = taskqueue_stub.get_filtered_tasks() self.assertEqual(1, len(tasks)) deferred.run(tasks[0].payload) self.assertEqual(1, len(taskqueue_stub.get_filtered_tasks())) self.assertEqual( 100, models.ReportsCache.GetStatsSummary()[0]['conns_off_corp'])
def testGet(self, render_mock, _): past = datetime.datetime.now() - datetime.timedelta(days=1) broken_uuid = 'uuid1' models.Computer( key_name=broken_uuid, preflight_datetime=past, uuid=broken_uuid).put() models.ComputerClientBroken( uuid=broken_uuid, details='42343243234', connections_off_corp=50, broken_datetimes=[datetime.datetime.now()]).put() toomany_preflight_uuid = 'toomany_preflight_since_postflight' models.Computer( uuid=toomany_preflight_uuid, connections_on_corp=40, preflight_count_since_postflight=50, postflight_datetime=past, preflight_datetime=datetime.datetime.now(), ).put() models.Computer( uuid='not_broken', connections_on_corp=40, preflight_count_since_postflight=3, postflight_datetime=past, preflight_datetime=datetime.datetime.now(), ).put() no_connections_uuid = 'client without connections' models.Computer( uuid=no_connections_uuid, preflight_count_since_postflight=6).put() self.testapp.get('/admin/brokenclients') args = test.GetArgFromCallHistory(render_mock, arg_index=1) self.assertEqual( [no_connections_uuid], _ComputersListToUuidList(args['zero_conn_computers'])) self.assertEqual( [broken_uuid], _ComputersListToUuidList(args['py_computers'])) self.assertEqual( [toomany_preflight_uuid], _ComputersListToUuidList(args['pf_computers']))
def setUp(self): super(HostModuleTest, self).setUp() self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.setup_env( overwrite=True, USER_EMAIL='*****@*****.**', USER_ID='123', USER_IS_ADMIN='0', DEFAULT_VERSION_HOSTNAME='example.appspot.com') self.testbed.init_all_stubs() self.common_serial = str(uuid.uuid4()) models.Computer( active=True, hostname='host1', serial=self.common_serial, uuid='UUID1', key_name='UUID1', owner='zaspire', client_version='2.3.1', os_version='10.11', site='NYC', track='stable', config_track='stable', connections_on_corp=1, connections_off_corp=0, uptime=90000.0, root_disk_free=0, user_disk_free=10).put() models.Computer( active=True, hostname='new-host1', serial=self.common_serial, uuid='NEWUUID1==', key_name='NEWUUID1==', owner='zerocool', os_version='10.11', site='NYC', track='stable', config_track='stable', connections_on_corp=1, connections_off_corp=0, uptime=90000.0, root_disk_free=0, user_disk_free=10, client_version='2.7.1').put() models.Computer( active=True, hostname='host10', serial=str(uuid.uuid4()), uuid='UUID2', key_name='UUID2', owner='zerocool', client_version='2.3.2', os_version='10.11', site='MTV', track='stable', config_track='stable', connections_on_corp=1, connections_off_corp=1, uptime=90000.0, root_disk_free=0, user_disk_free=10).put()
def __UpdateComputerEntity( event, _client_id, _user_settings, _pkgs_to_install, _apple_updates_to_install, _ip_address, _report_feedback, c=None): """Update the computer entity, or create a new one if it doesn't exists.""" now = datetime.datetime.utcnow() is_new_client = False if c is None: c = models.Computer.get_by_key_name(_client_id['uuid']) if c is None: # First time this client has connected. c = models.Computer(key_name=_client_id['uuid']) is_new_client = True c.uuid = _client_id['uuid'] c.hostname = _client_id['hostname'] c.serial= _client_id['serial'] c.owner = _client_id['owner'] c.track = _client_id['track'] c.site = _client_id['site'] c.office = _client_id['office'] c.config_track = _client_id['config_track'] c.client_version = _client_id['client_version'] c.os_version = _client_id['os_version'] c.uptime = _client_id['uptime'] c.root_disk_free = _client_id['root_disk_free'] c.user_disk_free = _client_id['user_disk_free'] c.global_uuid = _client_id['global_uuid'] c.runtype = _client_id['runtype'] c.ip_address = _ip_address last_notified_datetime = _client_id['last_notified_datetime'] if last_notified_datetime: # might be None try: last_notified_datetime = datetime.datetime.strptime( last_notified_datetime, '%Y-%m-%d %H:%M:%S') # timestamp is UTC. c.last_notified_datetime = last_notified_datetime except ValueError: # non-standard datetime sent. logging.warning( 'Non-standard last_notified_datetime: %s', last_notified_datetime) # Update event specific (preflight vs postflight) report values. if event == 'preflight': c.preflight_datetime = now if _client_id['on_corp'] == True: c.last_on_corp_preflight_datetime = now # Increment the number of preflight connections since the last successful # postflight, but only if the current connection is not going to exit due # to report feedback (WWAN, GoGo InFlight, etc.) if _report_feedback != ReportFeedback.EXIT: if c.preflight_count_since_postflight is not None: c.preflight_count_since_postflight += 1 else: c.preflight_count_since_postflight = 1 elif event == 'postflight': c.preflight_count_since_postflight = 0 c.postflight_datetime = now # Update pkgs_to_install. if _pkgs_to_install: c.pkgs_to_install = _pkgs_to_install c.all_pkgs_installed = False else: c.pkgs_to_install = [] c.all_pkgs_installed = True # Update all_apple_updates_installed and add Apple updates to # pkgs_to_install. It's important that this code block comes after # all_pkgs_installed is updated above, to ensure that all_pkgs_installed # is only considers Munki updates, ignoring Apple updates added below. # NOTE: if there are any pending Munki updates then we simply assume # there are also pending Apple Updates, even though we cannot be sure # due to the fact that Munki only checks for Apple Updates if all regular # updates are installed if not pkgs_to_install and not _apple_updates_to_install: c.all_apple_updates_installed = True else: c.all_apple_updates_installed = False # For now, let's store Munki and Apple Update pending installs together, # using APPLESUS_PKGS_TO_INSTALL_FORMAT to format the text as desired. for update in _apple_updates_to_install: c.pkgs_to_install.append(APPLESUS_PKGS_TO_INSTALL_FORMAT % update) # Keep the last CONNECTION_DATETIMES_LIMIT connection datetimes. if len(c.connection_datetimes) == CONNECTION_DATETIMES_LIMIT: c.connection_datetimes.pop(0) c.connection_datetimes.append(now) # Increase on_corp/off_corp count appropriately. if _client_id['on_corp'] == True: c.connections_on_corp = (c.connections_on_corp or 0) + 1 elif _client_id['on_corp'] == False: c.connections_off_corp = (c.connections_off_corp or 0) + 1 # Keep the last CONNECTION_DATES_LIMIT connection dates # (with time = 00:00:00) # Use newly created datetime.time object to set time to 00:00:00 now_date = datetime.datetime.combine(now, datetime.time()) if now_date not in c.connection_dates: if len(c.connection_dates) == CONNECTION_DATES_LIMIT: c.connection_dates.pop(0) c.connection_dates.append(now_date) else: logging.warning('Unknown event value: %s', event) c.put() if is_new_client: # Queue welcome email to be sent. #logging.debug('Deferring _SaveFirstConnection....') deferred.defer( _SaveFirstConnection, client_id=_client_id, computer=c, _countdown=300, _queue='first')
def setUp(self): super(SummaryModuleTest, self).setUp() self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.setup_env(overwrite=True, USER_EMAIL='*****@*****.**', USER_ID='123', USER_IS_ADMIN='0', DEFAULT_VERSION_HOSTNAME='example.appspot.com') self.testbed.init_all_stubs() today = datetime.datetime.today() five_days_ago = today - datetime.timedelta(days=5) long_ago = today - datetime.timedelta( days=models.COMPUTER_ACTIVE_DAYS + 1) # computer in NYC models.Computer(active=True, hostname='host1', serial=str(uuid.uuid4()), uuid=str(uuid.uuid4()), owner='zaspire', client_version='2.3.1', os_version='10.11', site='NYC', track='stable', config_track='stable', connection_dates=[today], connections_on_corp=1, connections_off_corp=0, uptime=90000.0, root_disk_free=0, user_disk_free=10, preflight_datetime=today).put() # last active five days ago models.Computer(active=True, hostname='host10', serial=str(uuid.uuid4()), uuid=str(uuid.uuid4()), owner='zerocool', client_version='2.3.2', os_version='10.11', site='MTV', track='stable', config_track='stable', connection_dates=[five_days_ago], connections_on_corp=1, connections_off_corp=1, uptime=90000.0, root_disk_free=0, user_disk_free=10, preflight_datetime=five_days_ago).put() # inactive computer in MTV models.Computer(active=False, hostname='host2', serial=str(uuid.uuid4()), uuid=str(uuid.uuid4()), owner='zerocool', client_version='2.3.1', os_version='10.11', site='MTV', track='stable', config_track='stable', connection_dates=[long_ago], connections_on_corp=1, connections_off_corp=1, uptime=90000.0, root_disk_free=0, user_disk_free=10, preflight_datetime=long_ago).put() # track unstable models.Computer(active=True, hostname='xyz-macbook', serial=str(uuid.uuid4()), uuid=str(uuid.uuid4()), owner='zerocool', client_version='2.3.3', os_version='10.10', site='MTV', track='unstable', config_track='unstable', connection_dates=[today], connections_on_corp=0, connections_off_corp=100, uptime=90000.0, root_disk_free=0, user_disk_free=10, preflight_datetime=today).put() reports_cache = models.ReportsCache() trending = { 'success': { 'packages': [('emacs', 10, 61.1)], 'total': 100 }, 'failure': { 'packages': [('vim', 10, 61.1)], 'total': 50 }, } reports_cache.SetTrendingInstalls(1, trending) trending['failure']['packages'].append(('office', 132, 99.9)) reports_cache.SetTrendingInstalls(24, trending)