Esempio n. 1
0
def test_lease_wallet_on_lease_result_empty():
    lease_wallet = LeaseWallet()
    assert None == lease_wallet.get_lease_state(resource='A')
    lease_use_result = _create_lease_use_result(
        LeaseProto.LeaseUseResult.STATUS_OK, _create_lease('A', 'B', [1, 0]))
    lease_wallet.on_lease_use_result(lease_use_result, resource='A')
    assert None == lease_wallet.get_lease_state(resource='A')
Esempio n. 2
0
def test_lease_wallet_on_lease_result_ok():
    lease_wallet = LeaseWallet()
    lease = _create_lease('A', 'epoch', [1])
    lease_wallet.add(lease)

    # Assert that initial state of adding a lease looks fine.
    lease_state = lease_wallet.get_lease_state(resource='A')
    assert None != lease_state
    assert Lease.CompareResult.SAME == lease.compare(lease_state.lease_original)
    assert Lease.CompareResult.SUPER_LEASE == lease.compare(lease_state.lease_current)
    assert LeaseState.STATUS_SELF_OWNER == lease_state.lease_status

    # When an "OK" lease_use_result comes, LeaseState should not change.
    lease_use_result = _create_lease_use_result(LeaseProto.LeaseUseResult.STATUS_OK,
                                                lease_state.lease_current, None)
    lease_wallet.on_lease_use_result(lease_use_result, resource='A')
    new_lease_state = lease_wallet.get_lease_state(resource='A')
    assert None != new_lease_state
    assert Lease.CompareResult.SAME == lease.compare(new_lease_state.lease_original)
    assert Lease.CompareResult.SUPER_LEASE == lease.compare(new_lease_state.lease_current)
    assert LeaseState.STATUS_SELF_OWNER == new_lease_state.lease_status

    # When an "OK" lease_use_result arrives for a lease other than current one, LeaseState also should not change
    lease_wallet.advance(resource='A')
    lease_wallet.on_lease_use_result(lease_use_result, resource='A')
    newer_lease_state = lease_wallet.get_lease_state(resource='A')
    assert None != newer_lease_state
    assert Lease.CompareResult.SAME == lease.compare(newer_lease_state.lease_original)
    assert Lease.CompareResult.SUPER_LEASE == lease.compare(newer_lease_state.lease_current)
    assert Lease.CompareResult.OLDER == new_lease_state.lease_current.compare(
        newer_lease_state.lease_current)
    assert LeaseState.STATUS_SELF_OWNER == newer_lease_state.lease_status
Esempio n. 3
0
def test_lease_wallet_on_lease_result_empty():
    lease_wallet = LeaseWallet()
    with pytest.raises(NoSuchLease) as excinfo:
        lease_wallet.get_lease_state(resource='A')
    assert 'No lease for resource "A"' == str(excinfo.value)
    lease_use_result = _create_lease_use_result(
        LeaseProto.LeaseUseResult.STATUS_OK, _create_lease('A', 'B', [1, 0]))
    lease_wallet.on_lease_use_result(lease_use_result, resource='A')
    with pytest.raises(NoSuchLease) as excinfo:
        lease_wallet.get_lease_state(resource='A')
    assert 'No lease for resource "A"' == str(excinfo.value)
Esempio n. 4
0
def test_lease_wallet_on_lease_result_older():
    lease_wallet = LeaseWallet()
    lease = _create_lease('A', 'epoch', [1])
    lease_wallet.add(lease)

    # Assert that initial state of adding a lease looks fine.
    lease_state = lease_wallet.get_lease_state(resource='A')
    assert None != lease_state
    assert Lease.CompareResult.SAME == lease.compare(
        lease_state.lease_original)
    assert Lease.CompareResult.SUPER_LEASE == lease.compare(
        lease_state.lease_current)
    assert LeaseState.Status.SELF_OWNER == lease_state.lease_status

    stale_lease = lease_state.lease_current

    # Advance the Lease so we can compare newer and older leases
    recent_lease = lease_wallet.advance(resource='A')

    # When an "OLDER" result comes in for an attempt which is not the current lease,
    # do not change the current lease state
    lease_use_result = _create_lease_use_result(
        LeaseProto.LeaseUseResult.STATUS_OLDER, stale_lease, None)
    lease_wallet.on_lease_use_result(lease_use_result, resource='A')
    new_lease_state = lease_wallet.get_lease_state(resource='A')
    assert None != new_lease_state
    assert Lease.CompareResult.SAME == lease.compare(
        new_lease_state.lease_original)
    assert Lease.CompareResult.SUPER_LEASE == lease.compare(
        new_lease_state.lease_current)
    assert LeaseState.Status.SELF_OWNER == new_lease_state.lease_status

    # When an "OLDER" result comes in for an attempt which is the current lease,
    # the lease state should change to other owner.
    lease_use_result = _create_lease_use_result(
        LeaseProto.LeaseUseResult.STATUS_OLDER, recent_lease, None)
    lease_wallet.on_lease_use_result(lease_use_result, resource='A')
    newer_lease_state = lease_wallet.get_lease_state(resource='A')
    assert None != newer_lease_state
    assert None == newer_lease_state.lease_current
    assert None == newer_lease_state.lease_original
    assert LeaseState.Status.OTHER_OWNER == newer_lease_state.lease_status

    with pytest.raises(LeaseNotOwnedByWallet) as excinfo:
        lease_wallet.get_lease(resource='A')
    assert 'Lease on "A" has state ({})'.format(
        LeaseState.Status.OTHER_OWNER) == str(excinfo.value)
Esempio n. 5
0
def test_lease_wallet_on_lease_result_wrong_epoch():
    lease_wallet = LeaseWallet()
    lease = _create_lease('A', 'epoch', [1])
    lease_wallet.add(lease)

    # Assert that initial state of adding a lease looks fine.
    lease_state = lease_wallet.get_lease_state(resource='A')
    assert None != lease_state
    assert Lease.CompareResult.SAME == lease.compare(
        lease_state.lease_original)
    assert Lease.CompareResult.SUPER_LEASE == lease.compare(
        lease_state.lease_current)
    assert LeaseState.STATUS_SELF_OWNER == lease_state.lease_status

    stale_lease = lease_state.lease_current

    # Advance the Lease so we can compare newer and older leases
    recent_lease = lease_wallet.advance(resource='A')

    # When a "REVOKED" result comes in for an attempt which is not the current lease,
    # do not change the current lease state
    lease_use_result = _create_lease_use_result(
        LeaseProto.LeaseUseResult.STATUS_WRONG_EPOCH, stale_lease, None)
    lease_wallet.on_lease_use_result(lease_use_result, resource='A')
    new_lease_state = lease_wallet.get_lease_state(resource='A')
    assert None != new_lease_state
    assert Lease.CompareResult.SAME == lease.compare(
        new_lease_state.lease_original)
    assert Lease.CompareResult.SUPER_LEASE == lease.compare(
        new_lease_state.lease_current)
    assert LeaseState.STATUS_SELF_OWNER == new_lease_state.lease_status

    # When an "REVOKED" result comes in for an attempt which is the current lease,
    # the lease state should change to revoked status
    lease_use_result = _create_lease_use_result(
        LeaseProto.LeaseUseResult.STATUS_WRONG_EPOCH, recent_lease, None)
    lease_wallet.on_lease_use_result(lease_use_result, resource='A')
    newer_lease_state = lease_wallet.get_lease_state(resource='A')
    assert None != newer_lease_state
    assert None == newer_lease_state.lease_current
    assert None == newer_lease_state.lease_original
    assert LeaseState.STATUS_UNOWNED == newer_lease_state.lease_status