-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add enterprise license check for virt plugin
- Loading branch information
Showing
4 changed files
with
107 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from middlewared.service import private, Service | ||
|
||
|
||
class VirtLicenseGlobalService(Service): | ||
|
||
class Config: | ||
namespace = 'virt.global' | ||
|
||
@private | ||
async def license_active(self, instance_type=None): | ||
""" | ||
If this is iX enterprise hardware and has NOT been licensed to run virt plugin | ||
then this will return False, otherwise this will return true. | ||
""" | ||
system_chassis = (await self.middleware.call('truenas.get_chassis_hardware')).removeprefix('TRUENAS-') | ||
if system_chassis.startswith(('UNKNOWN', 'MINI')): | ||
return True | ||
|
||
license_ = await self.middleware.call('system.license') | ||
can_run_virt = license_ is not None | ||
if can_run_virt is False: | ||
return False | ||
elif instance_type is None: | ||
can_run_virt = any(k in license_['features'] for k in ('JAILS', 'VM')) | ||
else: | ||
feature = 'JAILS' if instance_type == 'CONTAINER' else 'VM' | ||
can_run_virt = feature in license_['features'] | ||
|
||
return can_run_virt |
68 changes: 68 additions & 0 deletions
68
src/middlewared/middlewared/pytest/unit/plugins/virt/test_virt_license.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import pytest | ||
|
||
from middlewared.plugins.virt.license import VirtLicenseGlobalService | ||
from middlewared.pytest.unit.middleware import Middleware | ||
|
||
|
||
@pytest.mark.parametrize('instance_type,chassis_hardware,license_active,expected_result', [ | ||
( | ||
None, | ||
'TRUENAS-UNKNOWN', | ||
None, | ||
True | ||
), | ||
( | ||
None, | ||
'TRUENAS-MINI-3.0-XL+', | ||
None, | ||
True | ||
), | ||
( | ||
None, | ||
'TRUENAS-M60-HA', | ||
{'features': ['JAILS', 'VM']}, | ||
True | ||
), | ||
( | ||
'CONTAINER', | ||
'TRUENAS-M60-HA', | ||
{'features': ['JAILS', 'VM']}, | ||
True | ||
), | ||
( | ||
'VM', | ||
'TRUENAS-M60-HA', | ||
{'features': ['JAILS', 'VM']}, | ||
True | ||
), | ||
( | ||
'CONTAINER', | ||
'TRUENAS-M60-HA', | ||
{'features': ['VM']}, | ||
False | ||
), | ||
( | ||
'VM', | ||
'TRUENAS-M60-HA', | ||
{'features': ['JAILS']}, | ||
False | ||
), | ||
( | ||
None, | ||
'TRUENAS-M60-HA', | ||
None, | ||
False | ||
), | ||
( | ||
'VM', | ||
'TRUENAS-M60-HA', | ||
None, | ||
False | ||
), | ||
]) | ||
@pytest.mark.asyncio | ||
async def test_virt_license_validation(instance_type, chassis_hardware, license_active, expected_result): | ||
m = Middleware() | ||
m['truenas.get_chassis_hardware'] = lambda *arg: chassis_hardware | ||
m['system.license'] = lambda *arg: license_active | ||
assert await VirtLicenseGlobalService(m).license_active(instance_type) == expected_result |