def testDeviceReport(self):
   # Test DeviceReport class
   devices = device_info_reporter.GetDevicesToReport()
   report = device_info_reporter.DeviceReport(
       devices=devices, aggregation='product', states=['Allocated'])
   counts = report.GetReportCounts()
   self.assertEqual(1, counts['shamu'].total)
   self.assertEqual(1, counts['shamu'].count)
   self.assertEqual(1, counts['flounder'].total)
   self.assertEqual(0, counts['flounder'].count)
 def testGetDevicesToReport(self):
   # Test GetDevicesToReport()
   datastore_test_util.CreateDevice(
       'free', 'atl-1001.mtv', 'null-300', product='shamu',
       run_target='shamu', state='Allocated',
       device_type=api_messages.DeviceTypeMessage.NULL)
   devices = device_info_reporter.GetDevicesToReport()
   self.assertEqual(2, len(devices))
   for d in devices:
     self.assertEqual(d.physical_cluster, d.cluster)
 def testStoreDeviceSnapshot(self, mock_job_result):
   # Test StoreDeviceSnapshot()
   devices = device_info_reporter.GetDevicesToReport()
   device_snapshot = device_info_reporter.DeviceSnapshot(
       date=DATE, update_time=TIMESTAMP, devices=devices)
   device_info_reporter.StoreDeviceSnapshot(device_snapshot)
   json_data = json.dumps(device_info_reporter._DevicesToDicts(devices))
   # Read back.
   with self.mock_file_storage.OpenFile(device_snapshot.filename) as f:
     gz = gzip.GzipFile(mode='r', fileobj=f)
     self.assertEqual(json_data, six.ensure_str(gz.read()))
   mock_job_result.assert_called_once_with(device_snapshot)
 def testGetDeviceSnapshotForDate_withCluster(self):
   # Test GetDeviceSnapshotForDate() with date for a cluster
   date_0 = datetime.date(2016, 7, 25)
   devices = device_info_reporter.GetDevicesToReport()
   device_snapshot_0 = device_info_reporter.DeviceSnapshot(
       date=date_0, update_time=TIMESTAMP, devices=devices)
   device_info_reporter.StoreDeviceSnapshot(device_snapshot_0)
   snapshot_0 = device_info_reporter.GetDeviceSnapshotForDate(
       date=date_0, cluster_prefix='fre')
   self.assertEqual(2, len(snapshot_0.devices))
   self.assertEqual(date_0, snapshot_0.date)
   self.assertEqual(TIMESTAMP, snapshot_0.update_time)
   snapshot_1 = device_info_reporter.GetDeviceSnapshotForDate(
       date=date_0, cluster_prefix='fro')
   self.assertEqual(0, len(snapshot_1.devices))
   self.assertEqual(date_0, snapshot_1.date)
   self.assertEqual(TIMESTAMP, snapshot_1.update_time)
 def testGetDeviceSnapshotForDate(self):
   # Test GetDeviceSnapshotForDate()
   date_0 = datetime.date(2016, 7, 25)
   date_1 = datetime.date(2016, 7, 26)
   devices = device_info_reporter.GetDevicesToReport()
   device_snapshot_0 = device_info_reporter.DeviceSnapshot(
       date=date_0, update_time=TIMESTAMP, devices=devices)
   device_info_reporter.StoreDeviceSnapshot(device_snapshot_0)
   device_snapshot_1 = device_info_reporter.DeviceSnapshot(
       date=date_1, update_time=TIMESTAMP, devices=[])
   device_info_reporter.StoreDeviceSnapshot(device_snapshot_1)
   snapshot_20160725 = device_info_reporter.GetDeviceSnapshotForDate(date_0)
   self.assertEqual(2, len(snapshot_20160725.devices))
   self.assertEqual(date_0, snapshot_20160725.date)
   self.assertEqual(TIMESTAMP, snapshot_20160725.update_time)
   snapshot_20160726 = device_info_reporter.GetDeviceSnapshotForDate(date_1)
   self.assertEqual(0, len(snapshot_20160726.devices))
   self.assertEqual(date_1, snapshot_20160726.date)
   self.assertEqual(TIMESTAMP, snapshot_20160726.update_time)
 def testFilterDevices(self):
   # Test FilterDevices()
   datastore_test_util.CreateDevice(
       'foobar',
       'atl-1003.mtv',
       'a300',
       product='walleye',
       run_target='walleye',
       timestamp=TIMESTAMP)
   devices = device_info_reporter.GetDevicesToReport()
   free_devices = device_info_reporter.FilterDevices(devices, 'free')
   self.assertEqual(2, len(free_devices))
   self.assertEqual('free', free_devices[0].physical_cluster)
   self.assertEqual('shamu', free_devices[0].product)
   self.assertEqual('free', free_devices[1].physical_cluster)
   self.assertEqual('flounder', free_devices[1].product)
   foo_devices = device_info_reporter.FilterDevices(devices, 'foo')
   self.assertEqual(1, len(foo_devices))
   self.assertEqual('foobar', foo_devices[0].physical_cluster)
   self.assertEqual('walleye', foo_devices[0].product)
 def testGetDevicesToReport_withCluster(self):
   # Test GetDevicesToReport() with cluster
   devices = device_info_reporter.GetDevicesToReport(cluster_prefix='fre')
   self.assertEqual(2, len(devices))
   devices = device_info_reporter.GetDevicesToReport(cluster_prefix='fro')
   self.assertEqual(0, len(devices))