2021-03-02 09:44:24 +03:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
|
|
|
# Licensed under the MIT license.
|
|
|
|
|
2020-10-26 02:52:06 +03:00
|
|
|
from dataclasses import dataclass, field
|
2020-11-20 12:26:18 +03:00
|
|
|
from typing import Any, List, Optional, Type, Union
|
2020-09-16 07:15:41 +03:00
|
|
|
from unittest.case import TestCase
|
|
|
|
|
2021-03-26 23:48:41 +03:00
|
|
|
from dataclasses_json import dataclass_json
|
2020-10-26 02:52:06 +03:00
|
|
|
|
2021-06-11 09:56:19 +03:00
|
|
|
import lisa
|
2020-09-16 07:15:41 +03:00
|
|
|
from lisa import schema
|
2020-11-09 08:14:42 +03:00
|
|
|
from lisa.environment import (
|
|
|
|
Environment,
|
|
|
|
Environments,
|
|
|
|
EnvironmentStatus,
|
2021-12-15 22:33:36 +03:00
|
|
|
LisaException,
|
|
|
|
constants,
|
2020-11-09 08:14:42 +03:00
|
|
|
load_environments,
|
|
|
|
)
|
2020-09-23 13:32:53 +03:00
|
|
|
from lisa.feature import Feature
|
2020-09-18 06:31:02 +03:00
|
|
|
from lisa.platform_ import Platform, WaitMoreResourceError, load_platform
|
2021-12-15 22:33:36 +03:00
|
|
|
from lisa.util import plugin_manager
|
2020-09-16 07:15:41 +03:00
|
|
|
from lisa.util.logger import Logger
|
2021-08-06 10:07:11 +03:00
|
|
|
from selftests.test_environment import generate_runbook as generate_env_runbook
|
2020-09-16 07:15:41 +03:00
|
|
|
|
2020-10-26 02:52:06 +03:00
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class MockPlatformTestData:
|
|
|
|
prepared_envs: List[str] = field(default_factory=list)
|
|
|
|
deployed_envs: List[str] = field(default_factory=list)
|
|
|
|
deleted_envs: List[str] = field(default_factory=list)
|
|
|
|
|
|
|
|
|
2021-02-01 07:23:01 +03:00
|
|
|
@dataclass_json()
|
2020-10-26 02:52:06 +03:00
|
|
|
@dataclass
|
|
|
|
class MockPlatformSchema:
|
|
|
|
# for other UT to set value
|
|
|
|
return_prepared: bool = True
|
|
|
|
deploy_success: bool = True
|
2020-11-09 08:14:42 +03:00
|
|
|
deployed_status: EnvironmentStatus = EnvironmentStatus.Deployed
|
2020-10-26 02:52:06 +03:00
|
|
|
wait_more_resource_error: bool = False
|
2020-09-18 06:31:02 +03:00
|
|
|
|
2020-09-16 07:15:41 +03:00
|
|
|
|
|
|
|
class MockPlatform(Platform):
|
2020-09-28 10:35:57 +03:00
|
|
|
def __init__(self, runbook: schema.Platform) -> None:
|
|
|
|
super().__init__(runbook=runbook)
|
2020-10-26 02:52:06 +03:00
|
|
|
self.test_data = MockPlatformTestData()
|
2021-03-10 14:29:38 +03:00
|
|
|
# prevent real calls
|
|
|
|
for plugin_name, _ in plugin_manager.list_name_plugin():
|
|
|
|
plugin_manager.unregister(name=plugin_name)
|
2020-09-18 06:31:02 +03:00
|
|
|
|
2020-09-16 07:15:41 +03:00
|
|
|
@classmethod
|
2020-09-28 10:35:57 +03:00
|
|
|
def type_name(cls) -> str:
|
2020-09-16 07:15:41 +03:00
|
|
|
return constants.PLATFORM_MOCK
|
|
|
|
|
2020-09-23 13:32:53 +03:00
|
|
|
@classmethod
|
|
|
|
def supported_features(cls) -> List[Type[Feature]]:
|
|
|
|
return []
|
|
|
|
|
2020-09-16 07:15:41 +03:00
|
|
|
def set_test_config(
|
2020-09-18 06:31:02 +03:00
|
|
|
self,
|
|
|
|
return_prepared: bool = True,
|
|
|
|
deploy_success: bool = True,
|
2020-11-09 08:14:42 +03:00
|
|
|
deployed_status: EnvironmentStatus = EnvironmentStatus.Deployed,
|
2020-09-18 06:31:02 +03:00
|
|
|
wait_more_resource_error: bool = False,
|
2020-09-16 07:15:41 +03:00
|
|
|
) -> None:
|
2020-10-26 02:52:06 +03:00
|
|
|
self.initialize()
|
|
|
|
self._mock_runbook.return_prepared = return_prepared
|
|
|
|
self._mock_runbook.deploy_success = deploy_success
|
2020-11-09 08:14:42 +03:00
|
|
|
self._mock_runbook.deployed_status = deployed_status
|
2020-10-26 02:52:06 +03:00
|
|
|
self._mock_runbook.wait_more_resource_error = wait_more_resource_error
|
|
|
|
|
|
|
|
def _initialize(self, *args: Any, **kwargs: Any) -> None:
|
2021-05-05 21:03:44 +03:00
|
|
|
self._mock_runbook: MockPlatformSchema = self.runbook.get_extended_runbook(
|
2020-10-26 02:52:06 +03:00
|
|
|
MockPlatformSchema, constants.PLATFORM_MOCK
|
|
|
|
)
|
2020-09-16 07:15:41 +03:00
|
|
|
|
|
|
|
def _prepare_environment(self, environment: Environment, log: Logger) -> bool:
|
2020-10-26 02:52:06 +03:00
|
|
|
self.test_data.prepared_envs.append(environment.name)
|
2020-09-29 11:39:08 +03:00
|
|
|
requirements = environment.runbook.nodes_requirement
|
2020-10-26 02:52:06 +03:00
|
|
|
if self._mock_runbook.return_prepared and requirements:
|
2020-09-29 11:39:08 +03:00
|
|
|
min_capabilities: List[schema.NodeSpace] = []
|
|
|
|
for node_space in requirements:
|
|
|
|
min_capabilities.append(node_space.generate_min_capability(node_space))
|
|
|
|
environment.runbook.nodes_requirement = min_capabilities
|
2020-10-26 02:52:06 +03:00
|
|
|
return self._mock_runbook.return_prepared
|
2020-09-16 07:15:41 +03:00
|
|
|
|
|
|
|
def _deploy_environment(self, environment: Environment, log: Logger) -> None:
|
2020-10-26 02:52:06 +03:00
|
|
|
if self._mock_runbook.wait_more_resource_error:
|
2020-09-18 06:31:02 +03:00
|
|
|
raise WaitMoreResourceError("wait more resource")
|
2020-10-26 02:52:06 +03:00
|
|
|
if not self._mock_runbook.deploy_success:
|
2020-09-16 07:15:41 +03:00
|
|
|
raise LisaException("mock deploy failed")
|
2020-10-26 02:52:06 +03:00
|
|
|
if self._mock_runbook.return_prepared and environment.runbook.nodes_requirement:
|
2020-09-16 07:15:41 +03:00
|
|
|
requirements = environment.runbook.nodes_requirement
|
|
|
|
for node_space in requirements:
|
2021-07-31 01:05:42 +03:00
|
|
|
environment.create_node_from_requirement(node_requirement=node_space)
|
2020-10-09 14:10:31 +03:00
|
|
|
for node in environment.nodes.list():
|
2020-11-09 08:14:42 +03:00
|
|
|
node._is_initialized = True
|
2020-10-26 02:52:06 +03:00
|
|
|
self.test_data.deployed_envs.append(environment.name)
|
2020-11-09 08:14:42 +03:00
|
|
|
if self._mock_runbook.deployed_status not in [
|
|
|
|
EnvironmentStatus.Deployed,
|
|
|
|
EnvironmentStatus.Connected,
|
|
|
|
]:
|
|
|
|
raise LisaException(
|
|
|
|
f"expected status is {self._mock_runbook.deployed_status}, "
|
|
|
|
f"deployment should be failed"
|
|
|
|
)
|
2020-09-16 07:15:41 +03:00
|
|
|
|
|
|
|
def _delete_environment(self, environment: Environment, log: Logger) -> None:
|
2020-10-26 02:52:06 +03:00
|
|
|
self.test_data.deleted_envs.append(environment.name)
|
2020-09-16 07:15:41 +03:00
|
|
|
self.delete_called = True
|
|
|
|
|
|
|
|
|
|
|
|
def generate_platform(
|
2021-04-23 20:37:38 +03:00
|
|
|
keep_environment: Optional[Union[str, bool]] = False,
|
2021-03-02 09:32:43 +03:00
|
|
|
admin_password: str = "do not use for real",
|
2020-09-16 07:15:41 +03:00
|
|
|
admin_key_file: str = "",
|
|
|
|
) -> MockPlatform:
|
|
|
|
runbook_data = {
|
|
|
|
constants.TYPE: constants.PLATFORM_MOCK,
|
2021-04-23 20:37:38 +03:00
|
|
|
"keep_environment": keep_environment,
|
2021-02-01 07:23:01 +03:00
|
|
|
"admin_password": admin_password,
|
|
|
|
"admin_private_key_file": admin_key_file,
|
2020-09-16 07:15:41 +03:00
|
|
|
}
|
2021-08-18 03:07:43 +03:00
|
|
|
runbook = schema.load_by_type(schema.Platform, runbook_data)
|
2020-09-16 07:15:41 +03:00
|
|
|
platform = load_platform([runbook])
|
2021-05-14 23:02:09 +03:00
|
|
|
platform.initialize()
|
2020-09-18 06:31:02 +03:00
|
|
|
try:
|
|
|
|
assert isinstance(platform, MockPlatform), f"actual: {type(platform)}"
|
|
|
|
except AssertionError:
|
2021-03-02 09:32:43 +03:00
|
|
|
# as UT imported from tests package, instead of from lisa.tests package
|
2020-09-18 06:31:02 +03:00
|
|
|
# ignore by assign type from current package
|
2020-09-28 10:35:57 +03:00
|
|
|
platform = MockPlatform(runbook)
|
2020-09-18 06:31:02 +03:00
|
|
|
return platform
|
2020-09-16 07:15:41 +03:00
|
|
|
|
|
|
|
|
|
|
|
def generate_environments() -> Environments:
|
|
|
|
envs_runbook = generate_env_runbook(local=True, requirement=True)
|
|
|
|
envs = load_environments(envs_runbook)
|
|
|
|
|
|
|
|
return envs
|
|
|
|
|
|
|
|
|
|
|
|
class PlatformTestCase(TestCase):
|
2021-06-11 09:56:19 +03:00
|
|
|
def setUp(self) -> None:
|
|
|
|
lisa.environment._global_environment_id = 0
|
|
|
|
|
2021-03-10 09:11:12 +03:00
|
|
|
def test_prepared_env_not_success_with_exception(self) -> None:
|
2020-09-16 07:15:41 +03:00
|
|
|
platform = generate_platform()
|
|
|
|
platform.set_test_config(return_prepared=False)
|
|
|
|
envs = generate_environments()
|
|
|
|
self.assertEqual(2, len(envs))
|
2021-03-10 09:11:12 +03:00
|
|
|
with self.assertRaises(LisaException) as cm:
|
|
|
|
[platform.prepare_environment(env) for env in envs.values()]
|
|
|
|
self.assertEqual(
|
|
|
|
"no capability found for environment: "
|
|
|
|
"Environment(name='customized_0', topology='subnet', "
|
|
|
|
"nodes_raw=None, nodes_requirement=None)",
|
|
|
|
str(cm.exception),
|
|
|
|
)
|
2020-09-16 07:15:41 +03:00
|
|
|
|
|
|
|
def test_prepared_env_success(self) -> None:
|
|
|
|
platform = generate_platform()
|
|
|
|
platform.set_test_config(return_prepared=True)
|
|
|
|
envs = generate_environments()
|
|
|
|
self.assertEqual(2, len(envs))
|
2021-03-10 09:11:12 +03:00
|
|
|
prepared_environments = [
|
|
|
|
platform.prepare_environment(env) for env in envs.values()
|
|
|
|
]
|
2020-09-16 07:15:41 +03:00
|
|
|
self.assertEqual(2, len(prepared_environments))
|
|
|
|
|
|
|
|
def test_prepared_env_sorted_predefined_first(self) -> None:
|
|
|
|
platform = generate_platform()
|
|
|
|
platform.set_test_config()
|
|
|
|
envs = generate_environments()
|
|
|
|
|
|
|
|
# verify init as expected
|
2020-10-19 15:24:55 +03:00
|
|
|
self.assertListEqual(["customized_0", "customized_1"], [x for x in envs])
|
2020-09-16 07:15:41 +03:00
|
|
|
self.assertListEqual([True, True], [x.is_predefined for x in envs.values()])
|
|
|
|
|
|
|
|
# verify stable sort
|
2020-10-19 15:24:55 +03:00
|
|
|
envs["customized_1"].is_predefined = False
|
2021-03-10 09:11:12 +03:00
|
|
|
prepared_environments = [
|
|
|
|
platform.prepare_environment(env) for env in envs.values()
|
|
|
|
]
|
|
|
|
prepared_environments.sort(key=lambda x: (not x.is_predefined, x.cost))
|
2020-09-16 07:15:41 +03:00
|
|
|
self.assertListEqual(
|
2020-10-19 15:24:55 +03:00
|
|
|
["customized_0", "customized_1"], [x.name for x in prepared_environments]
|
2020-09-16 07:15:41 +03:00
|
|
|
)
|
|
|
|
self.assertListEqual(
|
|
|
|
[True, False], [x.is_predefined for x in prepared_environments]
|
|
|
|
)
|
|
|
|
|
|
|
|
# verify reverse sort
|
2020-10-19 15:24:55 +03:00
|
|
|
envs["customized_0"].is_predefined = False
|
|
|
|
envs["customized_1"].is_predefined = True
|
2021-03-10 09:11:12 +03:00
|
|
|
prepared_environments = [
|
|
|
|
platform.prepare_environment(env) for env in envs.values()
|
|
|
|
]
|
|
|
|
prepared_environments.sort(key=lambda x: (not x.is_predefined, x.cost))
|
2020-09-16 07:15:41 +03:00
|
|
|
self.assertListEqual(
|
2020-10-19 15:24:55 +03:00
|
|
|
["customized_1", "customized_0"],
|
2020-09-24 02:42:14 +03:00
|
|
|
[x.name for x in prepared_environments],
|
2020-09-16 07:15:41 +03:00
|
|
|
)
|
|
|
|
self.assertListEqual(
|
|
|
|
[True, False], [x.is_predefined for x in prepared_environments]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_prepared_env_sorted_by_cost(self) -> None:
|
|
|
|
platform = generate_platform()
|
|
|
|
envs = generate_environments()
|
|
|
|
platform.set_test_config()
|
|
|
|
|
2020-10-19 15:24:55 +03:00
|
|
|
self.assertListEqual(["customized_0", "customized_1"], [x for x in envs])
|
2020-09-16 07:15:41 +03:00
|
|
|
self.assertListEqual([0, 0], [x.cost for x in envs.values()])
|
|
|
|
|
2020-10-19 15:24:55 +03:00
|
|
|
envs["customized_0"].cost = 1
|
|
|
|
envs["customized_1"].cost = 2
|
2021-03-10 09:11:12 +03:00
|
|
|
prepared_environments = [
|
|
|
|
platform.prepare_environment(env) for env in envs.values()
|
|
|
|
]
|
|
|
|
prepared_environments.sort(key=lambda x: (not x.is_predefined, x.cost))
|
2020-09-16 07:15:41 +03:00
|
|
|
self.assertListEqual(
|
2020-10-19 15:24:55 +03:00
|
|
|
["customized_0", "customized_1"], [x.name for x in prepared_environments]
|
2020-09-16 07:15:41 +03:00
|
|
|
)
|
|
|
|
self.assertListEqual([1, 2], [x.cost for x in prepared_environments])
|
|
|
|
|
2020-10-19 15:24:55 +03:00
|
|
|
envs["customized_0"].cost = 2
|
|
|
|
envs["customized_1"].cost = 1
|
2021-03-10 09:11:12 +03:00
|
|
|
prepared_environments = [
|
|
|
|
platform.prepare_environment(env) for env in envs.values()
|
|
|
|
]
|
|
|
|
prepared_environments.sort(key=lambda x: (not x.is_predefined, x.cost))
|
2020-09-16 07:15:41 +03:00
|
|
|
self.assertListEqual(
|
2020-10-19 15:24:55 +03:00
|
|
|
["customized_1", "customized_0"], [x.name for x in prepared_environments]
|
2020-09-16 07:15:41 +03:00
|
|
|
)
|
|
|
|
self.assertListEqual([1, 2], [x.cost for x in prepared_environments])
|
|
|
|
|
|
|
|
def test_prepared_env_deploy_failed(self) -> None:
|
|
|
|
platform = generate_platform()
|
|
|
|
envs = generate_environments()
|
|
|
|
platform.set_test_config(deploy_success=False)
|
|
|
|
for env in envs.values():
|
|
|
|
with self.assertRaises(LisaException) as cm:
|
|
|
|
platform.deploy_environment(env)
|
|
|
|
self.assertEqual("mock deploy failed", str(cm.exception))
|
|
|
|
|
|
|
|
def test_prepared_env_deleted_not_ready(self) -> None:
|
|
|
|
platform = generate_platform()
|
|
|
|
envs = generate_environments()
|
|
|
|
platform.set_test_config()
|
|
|
|
for env in envs.values():
|
|
|
|
platform.deploy_environment(env)
|
2020-11-09 08:14:42 +03:00
|
|
|
self.assertEqual(EnvironmentStatus.Deployed, env.status)
|
2020-09-16 07:15:41 +03:00
|
|
|
platform.delete_environment(env)
|
2020-11-09 08:14:42 +03:00
|
|
|
self.assertEqual(EnvironmentStatus.Deleted, env.status)
|