def test_lease_untriaged(self):
        machine_type = lease_management.MachineType(
            id='untriaged',
            leases=[
                lease_management.MachineLease(client_request_id='fake-id-1'),
            ],
            mp_dimensions=machine_provider.Dimensions(
                os_family=machine_provider.OSFamily.LINUX, ),
            request_id_base='untriaged',
            target_size=2,
        )
        machine_type.put()

        responses = rpc_to_json(
            machine_provider.BatchedLeaseResponse(responses=[
                machine_provider.LeaseResponse(
                    client_request_id='fake-id-1',
                    request_hash='fake-hash-1',
                    state=machine_provider.LeaseRequestState.UNTRIAGED,
                ),
            ]))

        lease_management.update_leases(machine_type.key, responses)
        updated_machine_type = machine_type.key.get()
        self.assertEqual(len(updated_machine_type.leases), 1)
        self.assertEqual(updated_machine_type.leases[0].client_request_id,
                         'fake-id-1')
        self.failIf(updated_machine_type.leases[0].hostname)
        self.failIf(updated_machine_type.leases[0].lease_expiration_ts)
        self.assertEqual(updated_machine_type.leases[0].request_hash,
                         'fake-hash-1')
    def test_lease_expired(self):
        machine_type = lease_management.MachineType(
            id='untriaged',
            leases=[
                lease_management.MachineLease(client_request_id='fake-id-1'),
            ],
            mp_dimensions=machine_provider.Dimensions(
                os_family=machine_provider.OSFamily.LINUX, ),
            request_id_base='untriaged',
            target_size=2,
        )
        machine_type.put()

        responses = rpc_to_json(
            machine_provider.BatchedLeaseResponse(responses=[
                machine_provider.LeaseResponse(
                    client_request_id='fake-id-1',
                    request_hash='fake-hash-1',
                    state=machine_provider.LeaseRequestState.FULFILLED,
                ),
            ]))

        lease_management.update_leases(machine_type.key, responses)
        updated_machine_type = machine_type.key.get()
        self.failIf(updated_machine_type.leases)
  def test_lease_expired(self):
    machine_type = lease_management.MachineType(
      id='untriaged',
      leases=[
          lease_management.MachineLease(client_request_id='fake-id-1'),
      ],
      mp_dimensions=machine_provider.Dimensions(
          os_family=machine_provider.OSFamily.LINUX,
      ),
      request_id_base='untriaged',
      target_size=2,
    )
    machine_type.put()

    responses = rpc_to_json(machine_provider.BatchedLeaseResponse(responses=[
        machine_provider.LeaseResponse(
            client_request_id='fake-id-1',
            request_hash='fake-hash-1',
            state=machine_provider.LeaseRequestState.FULFILLED,
        ),
    ]))

    lease_management.update_leases(machine_type.key, responses)
    updated_machine_type = machine_type.key.get()
    self.failIf(updated_machine_type.leases)
  def test_lease_untriaged(self):
    machine_type = lease_management.MachineType(
      id='untriaged',
      leases=[
          lease_management.MachineLease(client_request_id='fake-id-1'),
      ],
      mp_dimensions=machine_provider.Dimensions(
          os_family=machine_provider.OSFamily.LINUX,
      ),
      request_id_base='untriaged',
      target_size=2,
    )
    machine_type.put()

    responses = rpc_to_json(machine_provider.BatchedLeaseResponse(responses=[
        machine_provider.LeaseResponse(
            client_request_id='fake-id-1',
            request_hash='fake-hash-1',
            state=machine_provider.LeaseRequestState.UNTRIAGED,
        ),
    ]))

    lease_management.update_leases(machine_type.key, responses)
    updated_machine_type = machine_type.key.get()
    self.assertEqual(len(updated_machine_type.leases), 1)
    self.assertEqual(
        updated_machine_type.leases[0].client_request_id, 'fake-id-1')
    self.failIf(updated_machine_type.leases[0].hostname)
    self.failIf(updated_machine_type.leases[0].lease_expiration_ts)
    self.assertEqual(updated_machine_type.leases[0].request_hash, 'fake-hash-1')
    def test_leases_fulfilled(self):
        machine_type = lease_management.MachineType(
            id='fulfilled',
            leases=[
                lease_management.MachineLease(client_request_id='fake-id-1'),
                lease_management.MachineLease(client_request_id='fake-id-2'),
            ],
            mp_dimensions=machine_provider.Dimensions(
                os_family=machine_provider.OSFamily.LINUX, ),
            request_id_base='fulfilled',
            target_size=2,
        )
        machine_type.put()

        responses = rpc_to_json(
            machine_provider.BatchedLeaseResponse(responses=[
                machine_provider.LeaseResponse(
                    client_request_id='fake-id-1',
                    hostname='fake-host-1',
                    lease_expiration_ts=1,
                    request_hash='fake-hash-1',
                    state=machine_provider.LeaseRequestState.FULFILLED,
                ),
                machine_provider.LeaseResponse(
                    client_request_id='fake-id-2',
                    hostname='fake-host-2',
                    lease_expiration_ts=2,
                    request_hash='fake-hash-2',
                    state=machine_provider.LeaseRequestState.FULFILLED,
                ),
            ]))

        lease_management.update_leases(machine_type.key, responses)
        updated_machine_type = machine_type.key.get()
        self.assertEqual(len(updated_machine_type.leases), 2)
        self.failUnless(updated_machine_type.leases[0].hostname)
        self.failUnless(updated_machine_type.leases[1].hostname)
        self.failUnless(updated_machine_type.leases[0].lease_expiration_ts)
        self.failUnless(updated_machine_type.leases[1].lease_expiration_ts)
        request_hashes = sorted(lease.request_hash
                                for lease in updated_machine_type.leases)
        self.assertEqual(request_hashes[0], 'fake-hash-1')
        self.assertEqual(request_hashes[1], 'fake-hash-2')
  def test_leases_fulfilled(self):
    machine_type = lease_management.MachineType(
      id='fulfilled',
      leases=[
          lease_management.MachineLease(client_request_id='fake-id-1'),
          lease_management.MachineLease(client_request_id='fake-id-2'),
      ],
      mp_dimensions=machine_provider.Dimensions(
          os_family=machine_provider.OSFamily.LINUX,
      ),
      request_id_base='fulfilled',
      target_size=2,
    )
    machine_type.put()

    responses = rpc_to_json(machine_provider.BatchedLeaseResponse(responses=[
        machine_provider.LeaseResponse(
            client_request_id='fake-id-1',
            hostname='fake-host-1',
            lease_expiration_ts=1,
            request_hash='fake-hash-1',
            state=machine_provider.LeaseRequestState.FULFILLED,
        ),
        machine_provider.LeaseResponse(
            client_request_id='fake-id-2',
            hostname='fake-host-2',
            lease_expiration_ts=2,
            request_hash='fake-hash-2',
            state=machine_provider.LeaseRequestState.FULFILLED,
        ),
    ]))

    lease_management.update_leases(machine_type.key, responses)
    updated_machine_type = machine_type.key.get()
    self.assertEqual(len(updated_machine_type.leases), 2)
    self.failUnless(updated_machine_type.leases[0].hostname)
    self.failUnless(updated_machine_type.leases[1].hostname)
    self.failUnless(updated_machine_type.leases[0].lease_expiration_ts)
    self.failUnless(updated_machine_type.leases[1].lease_expiration_ts)
    request_hashes = sorted(
        lease.request_hash for lease in updated_machine_type.leases)
    self.assertEqual(request_hashes[0], 'fake-hash-1')
    self.assertEqual(request_hashes[1], 'fake-hash-2')
    def test_lease_errors(self):
        machine_type = lease_management.MachineType(
            id='errors',
            leases=[
                lease_management.MachineLease(client_request_id='fake-id-1'),
                lease_management.MachineLease(client_request_id='fake-id-2'),
                lease_management.MachineLease(client_request_id='fake-id-3'),
            ],
            mp_dimensions=machine_provider.Dimensions(
                os_family=machine_provider.OSFamily.LINUX, ),
            request_id_base='errors',
            target_size=2,
        )
        machine_type.put()

        responses = rpc_to_json(
            machine_provider.BatchedLeaseResponse(responses=[
                machine_provider.LeaseResponse(
                    client_request_id='fake-id-1',
                    error=machine_provider.LeaseRequestError.DEADLINE_EXCEEDED,
                ),
                machine_provider.LeaseResponse(
                    client_request_id='fake-id-2',
                    error=machine_provider.LeaseRequestError.REQUEST_ID_REUSE,
                ),
                machine_provider.LeaseResponse(
                    client_request_id='fake-id-3',
                    error=machine_provider.LeaseRequestError.TRANSIENT_ERROR,
                ),
            ]))

        lease_management.update_leases(machine_type.key, responses)
        updated_machine_type = machine_type.key.get()
        self.assertEqual(len(updated_machine_type.leases), 2)
        request_ids = sorted(request.client_request_id
                             for request in updated_machine_type.leases)
        self.assertEqual(request_ids[0], 'fake-id-1')
        self.assertEqual(request_ids[1], 'fake-id-3')
  def test_lease_errors(self):
    machine_type = lease_management.MachineType(
      id='errors',
      leases=[
          lease_management.MachineLease(client_request_id='fake-id-1'),
          lease_management.MachineLease(client_request_id='fake-id-2'),
          lease_management.MachineLease(client_request_id='fake-id-3'),
      ],
      mp_dimensions=machine_provider.Dimensions(
          os_family=machine_provider.OSFamily.LINUX,
      ),
      request_id_base='errors',
      target_size=2,
    )
    machine_type.put()

    responses = rpc_to_json(machine_provider.BatchedLeaseResponse(responses=[
        machine_provider.LeaseResponse(
            client_request_id='fake-id-1',
            error=machine_provider.LeaseRequestError.DEADLINE_EXCEEDED,
        ),
        machine_provider.LeaseResponse(
            client_request_id='fake-id-2',
            error=machine_provider.LeaseRequestError.REQUEST_ID_REUSE,
        ),
        machine_provider.LeaseResponse(
            client_request_id='fake-id-3',
            error=machine_provider.LeaseRequestError.TRANSIENT_ERROR,
        ),
    ]))

    lease_management.update_leases(machine_type.key, responses)
    updated_machine_type = machine_type.key.get()
    self.assertEqual(len(updated_machine_type.leases), 2)
    request_ids = sorted(
        request.client_request_id for request in updated_machine_type.leases)
    self.assertEqual(request_ids[0], 'fake-id-1')
    self.assertEqual(request_ids[1], 'fake-id-3')