コード例 #1
0
 def testNoteFromEntity(self):
     """Tests converting a Note datastore entity a corresponding message."""
     note_entity = datastore_entities.Note(
         user='******',
         timestamp=TIMESTAMP,
         message='Hello, World',
         offline_reason='something reasonable',
         recovery_action='press the button',
         type=common.NoteType.UNKNOWN,
         cluster_id='acluser',
         hostname='ahost',
         device_serial='adevice',
         event_time=TIMESTAMP)
     note_message = datastore_entities.ToMessage(note_entity)
     self.assertIsNone(note_message.id)
     self.assertEqual(note_entity.user, note_message.user)
     self.assertEqual(note_entity.timestamp, note_message.timestamp)
     self.assertEqual(note_entity.message, note_message.message)
     self.assertEqual(note_entity.offline_reason,
                      note_message.offline_reason)
     self.assertEqual(note_entity.recovery_action,
                      note_message.recovery_action)
     self.assertEqual(note_entity.type, note_message.type)
     self.assertEqual(note_entity.cluster_id, note_message.cluster_id)
     self.assertEqual(note_entity.hostname, note_message.hostname)
     self.assertEqual(note_entity.device_serial, note_message.device_serial)
     self.assertEqual(note_entity.event_time, note_message.event_time)
コード例 #2
0
  def NewNote(self, request):
    """Submits a note for this device.

    Args:
      request: an API request.

    Returns:
      an api_messages.Note object.
    """
    timestamp = request.timestamp
    # Datastore only accepts UTC times. Doing a conversion if necessary.
    if timestamp.utcoffset() is not None:
      timestamp = timestamp.replace(tzinfo=None) - timestamp.utcoffset()
    note = datastore_entities.Note(
        type=common.NoteType.DEVICE_NOTE,
        hostname=request.hostname,
        device_serial=request.device_serial,
        user=request.user,
        timestamp=timestamp,
        message=request.message,
        offline_reason=request.offline_reason,
        recovery_action=request.recovery_action)

    note.put()
    return datastore_entities.ToMessage(note)
コード例 #3
0
 def testDeviceNote(self):
     count = 10
     for i in range(count):
         device_note = datastore_entities.DeviceNote(
             device_serial='serial_0')
         note = datastore_entities.Note(user=str(i),
                                        timestamp=TIMESTAMP_OLD,
                                        message='Hello, World')
         device_note.note = note
         device_note.put()
     query = datastore_entities.DeviceNote.query()
     query = query.filter(
         datastore_entities.DeviceNote.device_serial == 'serial_0')
     self.assertEqual(count, query.count())
コード例 #4
0
 def testHostNote(self):
     host_note = datastore_entities.HostNote(hostname='host.name')
     note = datastore_entities.Note(user='******',
                                    timestamp=TIMESTAMP_OLD,
                                    message='Hello, World')
     host_note.note = note
     host_note.put()
     query = datastore_entities.HostNote.query()
     query = query.filter(
         datastore_entities.HostNote.hostname == 'host.name')
     for query_host_note in query.iter():
         self.assertEqual('host.name', query_host_note.hostname)
         self.assertEqual(note.message, query_host_note.note.message)
         self.assertEqual(note.timestamp, query_host_note.note.timestamp)
         self.assertEqual(note.user, query_host_note.note.user)
コード例 #5
0
 def testNoteFromEntity_withId(self):
     note_entity = datastore_entities.Note(
         key=ndb.Key(datastore_entities.Note, 123456789),
         message='Hello, World',
         offline_reason='something reasonable',
         recovery_action='press the button',
         type=common.NoteType.UNKNOWN)
     note_message = datastore_entities.ToMessage(note_entity)
     self.assertEqual('123456789', note_message.id)
     self.assertEqual(note_entity.message, note_message.message)
     self.assertEqual(note_entity.offline_reason,
                      note_message.offline_reason)
     self.assertEqual(note_entity.recovery_action,
                      note_message.recovery_action)
     self.assertEqual(note_entity.type, note_message.type)
コード例 #6
0
 def testClusterNote(self):
     cluster_note = datastore_entities.ClusterNote(cluster='free')
     key = ndb.Key(datastore_entities.Note,
                   datastore_entities.Note.allocate_ids(1)[0].id())
     note = datastore_entities.Note(key=key,
                                    user='******',
                                    timestamp=TIMESTAMP_OLD,
                                    message='Hello, World')
     cluster_note.note = note
     cluster_note_key = cluster_note.put()
     queried_cluster_note = cluster_note_key.get()
     self.assertEqual('free', queried_cluster_note.cluster)
     self.assertEqual(cluster_note_key, queried_cluster_note.key)
     self.assertEqual(note.message, queried_cluster_note.note.message)
     self.assertEqual(note.timestamp, queried_cluster_note.note.timestamp)
     self.assertEqual(note.user, queried_cluster_note.note.user)
コード例 #7
0
def CreateHostNote(hostname,
                   user='******',
                   offline_reason='offline_reason1',
                   recovery_action='recovery_action1',
                   message='message1',
                   timestamp=None):
    """Create a host note."""
    note = datastore_entities.Note(user=user,
                                   offline_reason=offline_reason,
                                   recovery_action=recovery_action,
                                   message=message,
                                   timestamp=timestamp)
    host_note = datastore_entities.HostNote(id=hostname,
                                            hostname=hostname,
                                            note=note)
    host_note.put()
    return host_note
コード例 #8
0
def CreateDeviceNote(device_serial,
                     user='******',
                     offline_reason='offline_reason1',
                     recovery_action='recovery_action1',
                     message='message1',
                     timestamp=None):
    """Create a device note."""
    note = datastore_entities.Note(user=user,
                                   offline_reason=offline_reason,
                                   recovery_action=recovery_action,
                                   message=message,
                                   timestamp=timestamp)
    device_note = datastore_entities.DeviceNote(id=device_serial,
                                                device_serial=device_serial,
                                                note=note)
    device_note.put()
    return device_note
コード例 #9
0
 def testHostNoteFromEntity(self):
     entity = datastore_entities.HostNote(
         key=ndb.Key(datastore_entities.HostNote, 123456789),
         hostname='hostname_1',
         note=datastore_entities.Note(user='******',
                                      timestamp=TIMESTAMP,
                                      offline_reason='offline_reason_1',
                                      recovery_action='recovery_action_1',
                                      message='message_1'))
     msg = datastore_entities.ToMessage(entity)
     self.assertEqual('123456789', msg.id)
     self.assertEqual(entity.hostname, msg.hostname)
     self.assertEqual(entity.note.user, msg.user)
     self.assertEqual(entity.note.timestamp, msg.update_timestamp)
     self.assertEqual(entity.note.offline_reason, msg.offline_reason)
     self.assertEqual(entity.note.recovery_action, msg.recovery_action)
     self.assertEqual(entity.note.message, msg.message)
コード例 #10
0
  def NewNote(self, request):
    """Submits a note for this host.

    Args:
      request: an API request.
    Returns:
      a VoidMessage
    """
    cluster = request.cluster_id
    timestamp = request.timestamp
    # Datastore only accepts UTC times. Doing a conversion if necessary.
    if timestamp.utcoffset() is not None:
      timestamp = timestamp.replace(tzinfo=None) - timestamp.utcoffset()
    note = datastore_entities.Note(user=request.user, timestamp=timestamp,
                                   message=request.message)
    cluster_note = datastore_entities.ClusterNote(cluster=cluster)
    cluster_note.note = note
    cluster_note.put()
    return datastore_entities.ToMessage(note)
コード例 #11
0
def CreateNote(hostname='host1',
               user='******',
               offline_reason='offline_reason1',
               recovery_action='recovery_action1',
               message='message1',
               timestamp=None,
               cluster_id=None,
               device_serial=None,
               note_type=common.NoteType.UNKNOWN):
    """Create a host note."""
    note = datastore_entities.Note(user=user,
                                   offline_reason=offline_reason,
                                   recovery_action=recovery_action,
                                   message=message,
                                   timestamp=timestamp,
                                   cluster_id=cluster_id,
                                   hostname=hostname,
                                   device_serial=device_serial,
                                   type=note_type)
    note.put()
    return note
コード例 #12
0
  def setUp(self):
    api_test.ApiTest.setUp(self)
    self.host_update_state_summary_0 = (
        datastore_entities.HostUpdateStateSummary(
            total=2,
            succeeded=2,
            target_version='v1'))
    host_count_by_harness_version = {
        'version1': 72,
        'version2': 20,
    }
    self.cluster_0 = datastore_test_util.CreateCluster(
        cluster='free',
        total_devices=10,
        offline_devices=1,
        available_devices=2,
        allocated_devices=7,
        device_count_timestamp=self.TIMESTAMP,
        host_update_state_summary=self.host_update_state_summary_0,
        host_update_state_summaries_by_version=[
            self.host_update_state_summary_0],
        host_count_by_harness_version=host_count_by_harness_version)
    self.cluster_1 = datastore_test_util.CreateCluster(
        cluster='paid',
        total_devices=2,
        offline_devices=0,
        available_devices=1,
        allocated_devices=1,
        device_count_timestamp=self.TIMESTAMP)
    datastore_test_util.CreateHost(cluster='free', hostname='host_0')
    datastore_test_util.CreateDevice(
        cluster='free',
        hostname='host_0',
        device_serial='device_0',
        device_type=api_messages.DeviceTypeMessage.PHYSICAL,
        battery_level='100',
        run_target='shamu')
    datastore_test_util.CreateHost(cluster='free', hostname='host_1')
    datastore_test_util.CreateDevice(
        cluster='free',
        hostname='host_1',
        device_serial='device_1',
        device_type=api_messages.DeviceTypeMessage.PHYSICAL,
        timestamp=self.TIMESTAMP,
        run_target='flounder')

    datastore_test_util.CreateHost(cluster='paid', hostname='host_2')
    datastore_test_util.CreateDevice(
        cluster='paid',
        hostname='host_2',
        device_serial='device_2',
        device_type=api_messages.DeviceTypeMessage.PHYSICAL,
        run_target='shamu')
    # A hidden device
    datastore_test_util.CreateDevice(
        cluster='paid',
        hostname='host_2',
        device_serial='device_3',
        device_type=api_messages.DeviceTypeMessage.PHYSICAL,
        run_target='shamu',
        hidden=True)
    # A hidden host
    datastore_test_util.CreateHost(
        cluster='paid', hostname='host_3', hidden=True)

    self.note = datastore_entities.Note(user='******',
                                        timestamp=self.TIMESTAMP,
                                        message='Hello, World')
    cluster_note = datastore_entities.ClusterNote(cluster='free')
    cluster_note.note = self.note
    cluster_note.put()