2015-10-30 19:48:29 +00:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from datetime import datetime, timedelta
|
2016-09-28 03:20:31 +00:00
|
|
|
|
|
|
|
import jwt
|
2015-12-08 20:00:50 +00:00
|
|
|
import json
|
2015-10-30 19:48:29 +00:00
|
|
|
|
|
|
|
from Crypto.PublicKey import RSA
|
2016-09-28 03:20:31 +00:00
|
|
|
from cryptography.hazmat.backends import default_backend
|
|
|
|
from cryptography.hazmat.primitives.serialization import load_der_public_key
|
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
from util.license import (decode_license, LicenseDecodeError, ExpirationType,
|
2016-10-24 19:50:24 +00:00
|
|
|
MONTHLY_GRACE_PERIOD, YEARLY_GRACE_PERIOD, TRIAL_GRACE_PERIOD,
|
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT, QUAY_ENTITLEMENT)
|
2015-10-30 19:48:29 +00:00
|
|
|
|
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
def get_date(delta):
|
|
|
|
return str(datetime.now() + delta)
|
|
|
|
|
2015-10-30 19:48:29 +00:00
|
|
|
class TestLicense(unittest.TestCase):
|
|
|
|
def keys(self):
|
|
|
|
with open('test/data/test.pem') as f:
|
|
|
|
private_key = f.read()
|
|
|
|
|
2016-09-28 03:20:31 +00:00
|
|
|
public_key = load_der_public_key(RSA.importKey(private_key).publickey().exportKey('DER'),
|
|
|
|
backend=default_backend())
|
|
|
|
return (public_key, private_key)
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
def create_license(self, license_data, keys=None):
|
2015-12-08 20:00:50 +00:00
|
|
|
jwt_data = {
|
|
|
|
'license': json.dumps(license_data),
|
|
|
|
}
|
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
(public_key, private_key) = keys or self.keys()
|
2015-10-30 19:48:29 +00:00
|
|
|
|
|
|
|
# Encode the license with the JWT key.
|
2015-12-08 20:00:50 +00:00
|
|
|
encoded = jwt.encode(jwt_data, private_key, algorithm='RS256')
|
2015-10-30 19:48:29 +00:00
|
|
|
|
|
|
|
# Decode it into a license object.
|
2016-09-28 03:20:31 +00:00
|
|
|
return decode_license(encoded, public_key_instance=public_key)
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
def test_license_decodeerror_invalid(self):
|
|
|
|
with self.assertRaises(LicenseDecodeError):
|
|
|
|
decode_license('some random stuff')
|
|
|
|
|
|
|
|
def test_license_decodeerror_badkey(self):
|
|
|
|
(_, private_key) = self.keys()
|
|
|
|
jwt_data = {
|
|
|
|
'license': json.dumps({}),
|
2015-10-30 19:48:29 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
encoded_stuff = jwt.encode(jwt_data, private_key, algorithm='RS256')
|
|
|
|
with self.assertRaises(LicenseDecodeError):
|
|
|
|
# Note that since we don't give a key here, the prod one will be used, and it should fail.
|
|
|
|
decode_license(encoded_stuff)
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
def assertValid(self, license, config=None):
|
|
|
|
results = license.validate(config or {})
|
|
|
|
is_met = all([r.is_met() for r in results])
|
|
|
|
self.assertTrue(is_met, [r for r in results if not r.is_met()])
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
def assertNotValid(self, license, config=None, requirement=None, expired=None):
|
|
|
|
results = license.validate(config or {})
|
|
|
|
is_met = all([r.is_met() for r in results])
|
|
|
|
self.assertFalse(is_met)
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
invalid_results = [r for r in results if not r.is_met()]
|
|
|
|
if requirement is not None:
|
|
|
|
self.assertEquals(invalid_results[0].requirement.name, requirement)
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
if expired is not None:
|
|
|
|
self.assertEquals(invalid_results[0].entitlement.expiration.expiration_type, expired)
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
def test_missing_subscriptions(self):
|
|
|
|
license = self.create_license({
|
|
|
|
"expirationDate": get_date(timedelta(days=10)),
|
|
|
|
})
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2016-10-24 19:50:24 +00:00
|
|
|
self.assertNotValid(license, requirement=QUAY_ENTITLEMENT)
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
def test_empty_subscriptions(self):
|
|
|
|
license = self.create_license({
|
|
|
|
"expirationDate": get_date(timedelta(days=10)),
|
|
|
|
"subscriptions": {},
|
|
|
|
})
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2016-10-24 19:50:24 +00:00
|
|
|
self.assertNotValid(license, requirement=QUAY_ENTITLEMENT)
|
2016-10-19 03:44:08 +00:00
|
|
|
|
|
|
|
def test_missing_quay_entitlement(self):
|
|
|
|
license = self.create_license({
|
|
|
|
"expirationDate": get_date(timedelta(days=10)),
|
|
|
|
"subscriptions": {
|
|
|
|
"somesub": {
|
|
|
|
"serviceEnd": get_date(timedelta(days=10)),
|
|
|
|
"entitlements": {
|
2016-10-24 19:50:24 +00:00
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 0,
|
2016-10-19 03:44:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2016-10-24 19:50:24 +00:00
|
|
|
self.assertNotValid(license, requirement=QUAY_ENTITLEMENT)
|
2016-10-19 03:44:08 +00:00
|
|
|
|
|
|
|
def test_valid_quay_entitlement(self):
|
|
|
|
license = self.create_license({
|
|
|
|
"expirationDate": get_date(timedelta(days=10)),
|
|
|
|
"subscriptions": {
|
|
|
|
"somesub": {
|
|
|
|
"serviceEnd": get_date(timedelta(days=10)),
|
|
|
|
"entitlements": {
|
2016-10-24 19:50:24 +00:00
|
|
|
QUAY_ENTITLEMENT: 1,
|
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 1,
|
2016-10-19 03:44:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
self.assertValid(license)
|
|
|
|
|
|
|
|
def test_missing_expiration(self):
|
|
|
|
license = self.create_license({
|
|
|
|
"subscriptions": {
|
|
|
|
"somesub": {
|
|
|
|
"serviceEnd": get_date(timedelta(days=10)),
|
|
|
|
"entitlements": {
|
2016-10-24 19:50:24 +00:00
|
|
|
QUAY_ENTITLEMENT: 1,
|
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 1,
|
2016-10-19 03:44:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
self.assertNotValid(license, expired=ExpirationType.license_wide)
|
|
|
|
|
|
|
|
def test_expired_license(self):
|
|
|
|
license = self.create_license({
|
|
|
|
"expirationDate": get_date(timedelta(days=-10)),
|
|
|
|
"subscriptions": {
|
|
|
|
"somesub": {
|
|
|
|
"serviceEnd": get_date(timedelta(days=10)),
|
|
|
|
"entitlements": {
|
2016-10-24 19:50:24 +00:00
|
|
|
QUAY_ENTITLEMENT: 1,
|
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 1,
|
2016-10-19 03:44:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
self.assertNotValid(license, expired=ExpirationType.license_wide)
|
|
|
|
|
|
|
|
def test_expired_sub_implicit_monthly_withingrace(self):
|
|
|
|
license = self.create_license({
|
|
|
|
"expirationDate": get_date(timedelta(days=10)),
|
|
|
|
"subscriptions": {
|
|
|
|
"somesub": {
|
|
|
|
"serviceEnd": get_date(MONTHLY_GRACE_PERIOD * -1 + timedelta(days=1)),
|
|
|
|
"entitlements": {
|
2016-10-24 19:50:24 +00:00
|
|
|
QUAY_ENTITLEMENT: 1,
|
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 1,
|
2016-10-19 03:44:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
self.assertValid(license)
|
|
|
|
|
|
|
|
def test_expired_sub_monthly_withingrace(self):
|
|
|
|
license = self.create_license({
|
|
|
|
"expirationDate": get_date(timedelta(days=10)),
|
|
|
|
"subscriptions": {
|
|
|
|
"somesub": {
|
|
|
|
"serviceEnd": get_date(MONTHLY_GRACE_PERIOD * -1 + timedelta(days=1)),
|
|
|
|
"durationPeriod": "monthly",
|
|
|
|
"entitlements": {
|
2016-10-24 19:50:24 +00:00
|
|
|
QUAY_ENTITLEMENT: 1,
|
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 1,
|
2016-10-19 03:44:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
self.assertValid(license)
|
|
|
|
|
|
|
|
def test_expired_sub_monthly_outsidegrace(self):
|
|
|
|
license = self.create_license({
|
|
|
|
"expirationDate": get_date(timedelta(days=10)),
|
|
|
|
"subscriptions": {
|
|
|
|
"somesub": {
|
|
|
|
"serviceEnd": get_date(MONTHLY_GRACE_PERIOD * -1 + timedelta(days=-1)),
|
|
|
|
"durationPeriod": "monthly",
|
|
|
|
"entitlements": {
|
2016-10-24 19:50:24 +00:00
|
|
|
QUAY_ENTITLEMENT: 1,
|
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 1,
|
2016-10-19 03:44:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
self.assertNotValid(license, expired=ExpirationType.monthly)
|
|
|
|
|
|
|
|
def test_expired_sub_yearly_withingrace(self):
|
|
|
|
license = self.create_license({
|
|
|
|
"expirationDate": get_date(timedelta(days=10)),
|
|
|
|
"subscriptions": {
|
|
|
|
"somesub": {
|
|
|
|
"serviceEnd": get_date(YEARLY_GRACE_PERIOD * -1 + timedelta(days=1)),
|
|
|
|
"durationPeriod": "yearly",
|
|
|
|
"entitlements": {
|
2016-10-24 19:50:24 +00:00
|
|
|
QUAY_ENTITLEMENT: 1,
|
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 1,
|
2016-10-19 03:44:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
self.assertValid(license)
|
|
|
|
|
|
|
|
def test_expired_sub_yearly_outsidegrace(self):
|
|
|
|
license = self.create_license({
|
|
|
|
"expirationDate": get_date(timedelta(days=10)),
|
|
|
|
"subscriptions": {
|
|
|
|
"somesub": {
|
|
|
|
"serviceEnd": get_date(YEARLY_GRACE_PERIOD * -1 + timedelta(days=-1)),
|
|
|
|
"durationPeriod": "yearly",
|
|
|
|
"entitlements": {
|
2016-10-24 19:50:24 +00:00
|
|
|
QUAY_ENTITLEMENT: 1,
|
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 1,
|
2016-10-19 03:44:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
self.assertNotValid(license, expired=ExpirationType.yearly)
|
|
|
|
|
|
|
|
def test_expired_sub_intrial_withingrace(self):
|
|
|
|
license = self.create_license({
|
|
|
|
"expirationDate": get_date(timedelta(days=10)),
|
|
|
|
"subscriptions": {
|
|
|
|
"somesub": {
|
|
|
|
"serviceEnd": get_date(TRIAL_GRACE_PERIOD * -1 + timedelta(days=1)),
|
|
|
|
"inTrial": True,
|
|
|
|
"entitlements": {
|
2016-10-24 19:50:24 +00:00
|
|
|
QUAY_ENTITLEMENT: 1,
|
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 1,
|
2016-10-19 03:44:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
self.assertValid(license)
|
|
|
|
|
|
|
|
def test_expired_sub_intrial_outsidegrace(self):
|
|
|
|
license = self.create_license({
|
|
|
|
"expirationDate": get_date(timedelta(days=10)),
|
|
|
|
"subscriptions": {
|
|
|
|
"somesub": {
|
|
|
|
"serviceEnd": get_date(TRIAL_GRACE_PERIOD * -1 + timedelta(days=-1)),
|
|
|
|
"inTrial": True,
|
|
|
|
"entitlements": {
|
2016-10-24 19:50:24 +00:00
|
|
|
QUAY_ENTITLEMENT: 1,
|
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 1,
|
2016-10-19 03:44:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
self.assertNotValid(license, expired=ExpirationType.in_trial)
|
|
|
|
|
|
|
|
def test_expired_sub_trialonly_withingrace(self):
|
|
|
|
license = self.create_license({
|
|
|
|
"expirationDate": get_date(timedelta(days=10)),
|
|
|
|
"subscriptions": {
|
|
|
|
"somesub": {
|
|
|
|
"trialEnd": get_date(TRIAL_GRACE_PERIOD * -1 + timedelta(days=1)),
|
|
|
|
"trialOnly": True,
|
|
|
|
"entitlements": {
|
2016-10-24 19:50:24 +00:00
|
|
|
QUAY_ENTITLEMENT: 1,
|
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 1,
|
2016-10-19 03:44:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
self.assertValid(license)
|
|
|
|
|
|
|
|
def test_expired_sub_trialonly_outsidegrace(self):
|
|
|
|
license = self.create_license({
|
|
|
|
"expirationDate": get_date(timedelta(days=10)),
|
|
|
|
"subscriptions": {
|
|
|
|
"somesub": {
|
|
|
|
"trialEnd": get_date(TRIAL_GRACE_PERIOD * -1 + timedelta(days=-1)),
|
|
|
|
"trialOnly": True,
|
|
|
|
"entitlements": {
|
2016-10-24 19:50:24 +00:00
|
|
|
QUAY_ENTITLEMENT: 1,
|
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 1,
|
2016-10-19 03:44:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-10-30 19:48:29 +00:00
|
|
|
})
|
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
self.assertNotValid(license, expired=ExpirationType.trial_only)
|
|
|
|
|
|
|
|
def test_valid_quay_entitlement_regions(self):
|
|
|
|
license = self.create_license({
|
|
|
|
"expirationDate": get_date(timedelta(days=10)),
|
|
|
|
"subscriptions": {
|
|
|
|
"somesub": {
|
|
|
|
"serviceEnd": get_date(timedelta(days=10)),
|
|
|
|
"entitlements": {
|
2016-10-24 19:50:24 +00:00
|
|
|
QUAY_ENTITLEMENT: 1,
|
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 1,
|
2016-10-19 03:44:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
config = {
|
|
|
|
'DISTRIBUTED_STORAGE_CONFIG': [
|
|
|
|
{'name': 'first'},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
self.assertValid(license, config=config)
|
|
|
|
|
|
|
|
def test_invalid_quay_entitlement_regions(self):
|
|
|
|
license = self.create_license({
|
|
|
|
"expirationDate": get_date(timedelta(days=10)),
|
|
|
|
"subscriptions": {
|
|
|
|
"somesub": {
|
|
|
|
"serviceEnd": get_date(timedelta(days=10)),
|
|
|
|
"entitlements": {
|
2016-10-24 19:50:24 +00:00
|
|
|
QUAY_ENTITLEMENT: 1,
|
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 1,
|
2016-10-19 03:44:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
config = {
|
|
|
|
'DISTRIBUTED_STORAGE_CONFIG': [
|
|
|
|
{'name': 'first'},
|
|
|
|
{'name': 'second'},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
2016-10-24 19:50:24 +00:00
|
|
|
self.assertNotValid(license, config=config, requirement=QUAY_DEPLOYMENTS_ENTITLEMENT)
|
2016-10-19 03:44:08 +00:00
|
|
|
|
|
|
|
def test_valid_regions_across_multiple_sub(self):
|
|
|
|
license = self.create_license({
|
|
|
|
"expirationDate": get_date(timedelta(days=10)),
|
|
|
|
"subscriptions": {
|
|
|
|
"somesub": {
|
|
|
|
"serviceEnd": get_date(timedelta(days=10)),
|
|
|
|
"entitlements": {
|
2016-10-24 19:50:24 +00:00
|
|
|
QUAY_ENTITLEMENT: 1,
|
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 1,
|
2016-10-19 03:44:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"anothersub": {
|
|
|
|
"serviceEnd": get_date(timedelta(days=20)),
|
|
|
|
"entitlements": {
|
2016-10-24 19:50:24 +00:00
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 5,
|
2016-10-19 03:44:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
config = {
|
|
|
|
'DISTRIBUTED_STORAGE_CONFIG': [
|
|
|
|
{'name': 'first'},
|
|
|
|
{'name': 'second'},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
self.assertValid(license, config=config)
|
|
|
|
|
|
|
|
def test_valid_regions_across_multiple_sub_one_expired(self):
|
|
|
|
# Setup a license with one sub having too few regions, and another having enough, but it is
|
|
|
|
# expired.
|
|
|
|
license = self.create_license({
|
|
|
|
"expirationDate": get_date(timedelta(days=10)),
|
|
|
|
"subscriptions": {
|
|
|
|
"somesub": {
|
|
|
|
"serviceEnd": get_date(timedelta(days=10)),
|
|
|
|
"entitlements": {
|
2016-10-24 19:50:24 +00:00
|
|
|
QUAY_ENTITLEMENT: 1,
|
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 1,
|
2016-10-19 03:44:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"anothersub": {
|
|
|
|
"trialEnd": get_date(TRIAL_GRACE_PERIOD * -1 + timedelta(days=-1)),
|
|
|
|
"trialOnly": True,
|
|
|
|
"entitlements": {
|
2016-10-24 19:50:24 +00:00
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 5,
|
2016-10-19 03:44:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-10-30 19:48:29 +00:00
|
|
|
})
|
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
config = {
|
|
|
|
'DISTRIBUTED_STORAGE_CONFIG': [
|
|
|
|
{'name': 'first'},
|
|
|
|
{'name': 'second'},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
2016-10-24 19:50:24 +00:00
|
|
|
self.assertNotValid(license, config=config, requirement=QUAY_DEPLOYMENTS_ENTITLEMENT,
|
2016-10-19 03:44:08 +00:00
|
|
|
expired=ExpirationType.trial_only)
|
|
|
|
|
|
|
|
def test_valid_regions_across_multiple_sub_one_expired(self):
|
2017-02-13 20:33:47 +00:00
|
|
|
service_end = get_date(timedelta(days=20))
|
|
|
|
expiration_date = get_date(timedelta(days=10))
|
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
license = self.create_license({
|
2017-02-13 20:33:47 +00:00
|
|
|
"expirationDate": expiration_date,
|
2016-10-19 03:44:08 +00:00
|
|
|
"subscriptions": {
|
|
|
|
"somesub": {
|
|
|
|
"trialEnd": get_date(TRIAL_GRACE_PERIOD * -1 + timedelta(days=-1)),
|
|
|
|
"trialOnly": True,
|
|
|
|
"entitlements": {
|
2016-10-24 19:50:24 +00:00
|
|
|
QUAY_ENTITLEMENT: 1,
|
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 3,
|
2016-10-19 03:44:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"anothersub": {
|
2017-02-13 20:33:47 +00:00
|
|
|
"serviceEnd": service_end,
|
2016-10-19 03:44:08 +00:00
|
|
|
"entitlements": {
|
2016-10-24 19:50:24 +00:00
|
|
|
QUAY_ENTITLEMENT: 1,
|
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 5,
|
2016-10-19 03:44:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
config = {
|
|
|
|
'DISTRIBUTED_STORAGE_CONFIG': [
|
|
|
|
{'name': 'first'},
|
|
|
|
{'name': 'second'},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
self.assertValid(license, config=config)
|
|
|
|
|
2017-02-13 20:33:47 +00:00
|
|
|
entitlements = license.validate(config)
|
|
|
|
self.assertEquals(2, len(entitlements))
|
|
|
|
|
|
|
|
self.assertEntitlement(entitlements[0], QUAY_ENTITLEMENT, expiration_date)
|
|
|
|
self.assertEntitlement(entitlements[1], QUAY_DEPLOYMENTS_ENTITLEMENT, expiration_date)
|
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
def test_quay_is_under_expired_sub(self):
|
|
|
|
license = self.create_license({
|
|
|
|
"expirationDate": get_date(timedelta(days=10)),
|
|
|
|
"subscriptions": {
|
|
|
|
"somesub": {
|
|
|
|
"trialEnd": get_date(TRIAL_GRACE_PERIOD * -1 + timedelta(days=-1)),
|
|
|
|
"trialOnly": True,
|
|
|
|
"entitlements": {
|
2016-10-24 19:50:24 +00:00
|
|
|
QUAY_ENTITLEMENT: 1,
|
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 3,
|
2016-10-19 03:44:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"anothersub": {
|
|
|
|
"serviceEnd": get_date(timedelta(days=20)),
|
|
|
|
"entitlements": {
|
2016-10-24 19:50:24 +00:00
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 5,
|
2016-10-19 03:44:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
config = {
|
|
|
|
'DISTRIBUTED_STORAGE_CONFIG': [
|
|
|
|
{'name': 'first'},
|
|
|
|
{'name': 'second'},
|
|
|
|
],
|
|
|
|
}
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2016-10-19 03:44:08 +00:00
|
|
|
self.assertNotValid(license, config=config, expired=ExpirationType.trial_only,
|
2016-10-24 19:50:24 +00:00
|
|
|
requirement=QUAY_ENTITLEMENT)
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2017-02-13 20:33:47 +00:00
|
|
|
def assertEntitlement(self, entitlement, expected_name, expected_date):
|
|
|
|
self.assertEquals(expected_name, entitlement.requirement.name)
|
|
|
|
self.assertEquals(expected_date, str(entitlement.entitlement.expiration.expiration_date))
|
|
|
|
|
|
|
|
def test_license_with_multiple_subscriptions(self):
|
|
|
|
service_end = get_date(timedelta(days=20))
|
|
|
|
expiration_date = get_date(timedelta(days=10))
|
|
|
|
trial_end = get_date(timedelta(days=2))
|
|
|
|
|
|
|
|
license = self.create_license({
|
|
|
|
"expirationDate": expiration_date,
|
|
|
|
"subscriptions": {
|
|
|
|
"realsub": {
|
|
|
|
"serviceEnd": service_end,
|
|
|
|
"entitlements": {
|
|
|
|
QUAY_ENTITLEMENT: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"trialsub": {
|
|
|
|
"trialEnd": trial_end,
|
|
|
|
"trialOnly": True,
|
|
|
|
"inTrial": True,
|
|
|
|
"entitlements": {
|
|
|
|
QUAY_ENTITLEMENT: 1,
|
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 3,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
config = {
|
|
|
|
'DISTRIBUTED_STORAGE_CONFIG': [
|
|
|
|
{'name': 'first'},
|
|
|
|
{'name': 'second'},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
self.assertValid(license, config=config)
|
|
|
|
|
|
|
|
entitlements = license.validate(config)
|
|
|
|
self.assertEquals(2, len(entitlements))
|
|
|
|
|
|
|
|
self.assertEntitlement(entitlements[0], QUAY_ENTITLEMENT, expiration_date)
|
|
|
|
self.assertEntitlement(entitlements[1], QUAY_DEPLOYMENTS_ENTITLEMENT, trial_end)
|
|
|
|
|
|
|
|
def test_license_with_multiple_subscriptions_one_expired(self):
|
|
|
|
service_end = get_date(timedelta(days=20))
|
|
|
|
expiration_date = get_date(timedelta(days=10))
|
|
|
|
trial_end = get_date(timedelta(days=-2))
|
|
|
|
|
|
|
|
license = self.create_license({
|
|
|
|
"expirationDate": expiration_date,
|
|
|
|
"subscriptions": {
|
|
|
|
"realsub": {
|
|
|
|
"serviceEnd": service_end,
|
|
|
|
"entitlements": {
|
|
|
|
QUAY_ENTITLEMENT: 1,
|
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 3,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"trialsub": {
|
|
|
|
"trialEnd": trial_end,
|
|
|
|
"trialOnly": True,
|
|
|
|
"inTrial": True,
|
|
|
|
"entitlements": {
|
|
|
|
QUAY_ENTITLEMENT: 1,
|
|
|
|
QUAY_DEPLOYMENTS_ENTITLEMENT: 3,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
config = {
|
|
|
|
'DISTRIBUTED_STORAGE_CONFIG': [
|
|
|
|
{'name': 'first'},
|
|
|
|
{'name': 'second'},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
self.assertValid(license, config=config)
|
|
|
|
|
|
|
|
entitlements = license.validate(config)
|
|
|
|
self.assertEquals(2, len(entitlements))
|
|
|
|
|
|
|
|
self.assertEntitlement(entitlements[0], QUAY_ENTITLEMENT, expiration_date)
|
|
|
|
self.assertEntitlement(entitlements[1], QUAY_DEPLOYMENTS_ENTITLEMENT, expiration_date)
|
|
|
|
|
2015-10-30 19:48:29 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
|