Skip to content

Commit 234e45b

Browse files
author
Hao Yuan
committed
add unittest
1 parent 04357b5 commit 234e45b

File tree

3 files changed

+271
-17
lines changed

3 files changed

+271
-17
lines changed

src/aks-preview/azext_aks_preview/agentpool_decorator.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
AKSAgentPoolUpdateDecorator,
1818
)
1919
from azure.cli.core.azclierror import (
20+
CLIInternalError,
2021
InvalidArgumentValueError,
2122
MutuallyExclusiveArgumentError,
2223
)
@@ -1079,6 +1080,10 @@ def set_up_managed_system_mode(self, agentpool: AgentPool) -> AgentPool:
10791080
"""
10801081
mode = self.context.raw_param.get("mode")
10811082
if mode == CONST_NODEPOOL_MODE_MANAGEDSYSTEM:
1083+
# Raise error if agentpool is None
1084+
if agentpool is None:
1085+
raise CLIInternalError("agentpool cannot be None for ManagedSystem mode")
1086+
10821087
# Instead of creating a new instance, modify the existing one
10831088
# Keep name and set mode to ManagedSystem
10841089
name = agentpool.name

src/aks-preview/azext_aks_preview/tests/latest/test_agentpool_decorator.py

Lines changed: 231 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
CONST_DEFAULT_WINDOWS_VMS_VM_SIZE,
3535
CONST_GPU_DRIVER_INSTALL,
3636
CONST_GPU_DRIVER_NONE,
37+
CONST_NODEPOOL_MODE_MANAGEDSYSTEM,
3738
)
3839
from azure.cli.command_modules.acs.agentpool_decorator import AKSAgentPoolParamDict
3940
from azure.cli.command_modules.acs.tests.latest.mocks import (
@@ -424,7 +425,7 @@ def common_get_driver_type(self):
424425
driver_type="CUDA"
425426
)
426427
)
427-
428+
428429
ctx_1.attach_agentpool(agentpool_1)
429430
self.assertEqual(ctx_1.get_driver_type(), "CUDA")
430431

@@ -645,7 +646,7 @@ def common_get_disable_vtpm(self):
645646
)
646647
ctx_1.attach_agentpool(agentpool_1)
647648
self.assertEqual(ctx_1.get_disable_vtpm(), True)
648-
649+
649650
def common_get_enable_fips_image(self):
650651
# default
651652
ctx_1 = AKSPreviewAgentPoolContext(
@@ -659,7 +660,7 @@ def common_get_enable_fips_image(self):
659660
agentpool = self.create_initialized_agentpool_instance(enable_fips=True)
660661
ctx_1.attach_agentpool(agentpool)
661662
self.assertEqual(ctx_1.get_enable_fips_image(), True)
662-
663+
663664
# default
664665
ctx_2 = AKSPreviewAgentPoolContext(
665666
self.cmd,
@@ -782,7 +783,7 @@ def common_get_node_vm_size(self):
782783
ctx_4.get_node_vm_size()
783784
else:
784785
self.assertEqual(ctx_4.get_node_vm_size(), CONST_DEFAULT_WINDOWS_NODE_VM_SIZE)
785-
786+
786787
# if --node-vm-size is not specified, but --sku automatic is explicitly specified
787788
ctx_5 = AKSPreviewAgentPoolContext(
788789
self.cmd,
@@ -928,10 +929,10 @@ def test_get_enable_vtpm(self):
928929

929930
def test_get_disable_vtpm(self):
930931
self.common_get_disable_vtpm()
931-
932+
932933
def common_get_enable_fips_image(self):
933934
self.common_get_enable_fips_image()
934-
935+
935936
def common_get_disable_fips_image(self):
936937
self.common_get_disable_fips_image()
937938

@@ -958,6 +959,7 @@ def setUp(self):
958959
self.models = AKSPreviewAgentPoolModels(
959960
self.cmd, self.resource_type, self.agentpool_decorator_mode
960961
)
962+
self.client = MockClient()
961963

962964
def test_get_zones(self):
963965
self.common_get_zones()
@@ -1000,7 +1002,7 @@ def test_get_disable_secure_boot(self):
10001002

10011003
def test_get_enable_vtpm(self):
10021004
self.common_get_enable_vtpm()
1003-
1005+
10041006
def common_get_enable_fips_image(self):
10051007
self.common_get_enable_fips_image()
10061008

@@ -1013,6 +1015,80 @@ def test_get_gateway_prefix_size(self):
10131015
def test_get_vm_sizes(self):
10141016
self.common_get_vm_sizes()
10151017

1018+
def test_construct_agentpool_profile_preview(self):
1019+
import inspect
1020+
1021+
from azext_aks_preview.custom import aks_create
1022+
1023+
optional_params = {}
1024+
positional_params = []
1025+
for _, v in inspect.signature(aks_create).parameters.items():
1026+
if v.default != v.empty:
1027+
optional_params[v.name] = v.default
1028+
else:
1029+
positional_params.append(v.name)
1030+
ground_truth_positional_params = [
1031+
"cmd",
1032+
"client",
1033+
"resource_group_name",
1034+
"name",
1035+
"ssh_key_value",
1036+
]
1037+
self.assertEqual(positional_params, ground_truth_positional_params)
1038+
1039+
# prepare a dictionary of default parameters
1040+
raw_param_dict = {
1041+
"resource_group_name": "test_rg_name",
1042+
"name": "test_cluster_name",
1043+
"ssh_key_value": None,
1044+
}
1045+
raw_param_dict.update(optional_params)
1046+
1047+
# default value in `aks nodepool add`
1048+
dec_1 = AKSPreviewAgentPoolAddDecorator(
1049+
self.cmd,
1050+
self.client,
1051+
raw_param_dict,
1052+
self.resource_type,
1053+
self.agentpool_decorator_mode,
1054+
)
1055+
1056+
with patch(
1057+
"azure.cli.command_modules.acs.agentpool_decorator.cf_agent_pools",
1058+
return_value=Mock(list=Mock(return_value=[])),
1059+
):
1060+
dec_agentpool_1 = dec_1.construct_agentpool_profile_preview()
1061+
1062+
upgrade_settings_1 = self.models.AgentPoolUpgradeSettings()
1063+
# CLI will create sshAccess=localuser by default
1064+
ground_truth_security_profile = self.models.AgentPoolSecurityProfile()
1065+
ground_truth_security_profile.ssh_access = CONST_SSH_ACCESS_LOCALUSER
1066+
ground_truth_agentpool_1 = self.create_initialized_agentpool_instance(
1067+
nodepool_name="nodepool1",
1068+
orchestrator_version="",
1069+
vm_size=CONST_DEFAULT_NODE_VM_SIZE,
1070+
os_type=CONST_DEFAULT_NODE_OS_TYPE,
1071+
enable_node_public_ip=False,
1072+
enable_auto_scaling=False,
1073+
count=3,
1074+
node_taints=[],
1075+
node_initialization_taints=[],
1076+
os_disk_size_gb=0,
1077+
upgrade_settings=upgrade_settings_1,
1078+
type=CONST_VIRTUAL_MACHINE_SCALE_SETS,
1079+
enable_encryption_at_host=False,
1080+
enable_ultra_ssd=False,
1081+
enable_fips=False,
1082+
mode=CONST_NODEPOOL_MODE_SYSTEM,
1083+
workload_runtime=CONST_WORKLOAD_RUNTIME_OCI_CONTAINER,
1084+
enable_custom_ca_trust=False,
1085+
network_profile=self.models.AgentPoolNetworkProfile(),
1086+
security_profile=ground_truth_security_profile,
1087+
)
1088+
self.assertEqual(dec_agentpool_1, ground_truth_agentpool_1)
1089+
1090+
dec_1.context.raw_param.print_usage_statistics()
1091+
10161092

10171093
class AKSPreviewAgentPoolAddDecoratorCommonTestCase(unittest.TestCase):
10181094
def _remove_defaults_in_agentpool(self, agentpool):
@@ -1333,7 +1409,7 @@ def common_set_up_virtual_machines_profile(self):
13331409
)
13341410
)
13351411
self.assertEqual(dec_agentpool_1, ground_truth_agentpool_1)
1336-
1412+
13371413
dec_2 = AKSPreviewAgentPoolAddDecorator(
13381414
self.cmd,
13391415
self.client,
@@ -1347,6 +1423,142 @@ def common_set_up_virtual_machines_profile(self):
13471423
with self.assertRaises(InvalidArgumentValueError):
13481424
dec_2.set_up_virtual_machines_profile(agentpool_2)
13491425

1426+
def common_set_up_managed_system_mode(self):
1427+
"""Test the set_up_managed_system_mode method in AKSPreviewAgentPoolAddDecorator"""
1428+
1429+
# Test case 1: mode is ManagedSystem - should reset all properties except name and mode
1430+
dec_1 = AKSPreviewAgentPoolAddDecorator(
1431+
self.cmd,
1432+
self.client,
1433+
{"mode": CONST_NODEPOOL_MODE_MANAGEDSYSTEM},
1434+
self.resource_type,
1435+
self.agentpool_decorator_mode,
1436+
)
1437+
1438+
# fail on passing the wrong agentpool object
1439+
with self.assertRaises(CLIInternalError):
1440+
dec_1.set_up_managed_system_mode(None)
1441+
1442+
# Create an agentpool with various properties set
1443+
agentpool_1 = self.create_initialized_agentpool_instance(
1444+
restore_defaults=False,
1445+
count=3,
1446+
vm_size="Standard_D2s_v3",
1447+
os_type="Linux",
1448+
enable_auto_scaling=True,
1449+
min_count=1,
1450+
max_count=5,
1451+
)
1452+
1453+
# Store the original name for verification
1454+
original_name = agentpool_1.name
1455+
1456+
dec_1.context.attach_agentpool(agentpool_1)
1457+
dec_agentpool_1 = dec_1.set_up_managed_system_mode(agentpool_1)
1458+
1459+
# Verify that mode is set to ManagedSystem
1460+
self.assertEqual(dec_agentpool_1.mode, CONST_NODEPOOL_MODE_MANAGEDSYSTEM)
1461+
1462+
# Verify that name is preserved
1463+
self.assertEqual(dec_agentpool_1.name, original_name)
1464+
1465+
# Verify that all other properties are reset to None
1466+
for attr_name in vars(dec_agentpool_1):
1467+
if attr_name not in ['name', 'mode'] and not attr_name.startswith('_'):
1468+
attr_value = getattr(dec_agentpool_1, attr_name)
1469+
self.assertIsNone(attr_value,
1470+
f"Attribute '{attr_name}' should be None but was '{attr_value}'")
1471+
1472+
# Test case 2: mode is not ManagedSystem - should return agentpool unchanged
1473+
dec_2 = AKSPreviewAgentPoolAddDecorator(
1474+
self.cmd,
1475+
self.client,
1476+
{"mode": CONST_NODEPOOL_MODE_SYSTEM},
1477+
self.resource_type,
1478+
self.agentpool_decorator_mode,
1479+
)
1480+
1481+
agentpool_2 = self.create_initialized_agentpool_instance(
1482+
restore_defaults=False,
1483+
count=3,
1484+
vm_size="Standard_D2s_v3",
1485+
mode=CONST_NODEPOOL_MODE_SYSTEM,
1486+
)
1487+
1488+
# Store reference to compare
1489+
original_agentpool_2 = agentpool_2
1490+
1491+
dec_2.context.attach_agentpool(agentpool_2)
1492+
dec_agentpool_2 = dec_2.set_up_managed_system_mode(agentpool_2)
1493+
1494+
# Verify that agentpool is returned unchanged
1495+
self.assertEqual(dec_agentpool_2, original_agentpool_2)
1496+
1497+
# Test case 3: mode is None (default) - should return agentpool unchanged
1498+
dec_3 = AKSPreviewAgentPoolAddDecorator(
1499+
self.cmd,
1500+
self.client,
1501+
{}, # No mode specified
1502+
self.resource_type,
1503+
self.agentpool_decorator_mode,
1504+
)
1505+
1506+
agentpool_3 = self.create_initialized_agentpool_instance(
1507+
restore_defaults=False,
1508+
count=3,
1509+
vm_size="Standard_D2s_v3",
1510+
)
1511+
1512+
original_agentpool_3 = agentpool_3
1513+
1514+
dec_3.context.attach_agentpool(agentpool_3)
1515+
dec_agentpool_3 = dec_3.set_up_managed_system_mode(agentpool_3)
1516+
1517+
# Verify that agentpool is returned unchanged
1518+
self.assertEqual(dec_agentpool_3, original_agentpool_3)
1519+
1520+
def common_construct_agentpool_profile_preview_with_managed_system_mode(self):
1521+
"""Test that construct_agentpool_profile_preview properly handles ManagedSystem mode"""
1522+
1523+
# Test that when mode is ManagedSystem, only name and mode are preserved,
1524+
# and all other property setup methods are bypassed
1525+
dec = AKSPreviewAgentPoolAddDecorator(
1526+
self.cmd,
1527+
self.client,
1528+
{
1529+
"nodepool_name": "testnp",
1530+
"mode": CONST_NODEPOOL_MODE_MANAGEDSYSTEM,
1531+
# Add some parameters that would normally set properties
1532+
"node_count": 3,
1533+
"node_vm_size": "Standard_D2s_v3",
1534+
"enable_custom_ca_trust": True,
1535+
"crg_id": "test_crg_id",
1536+
"enable_artifact_streaming": True,
1537+
},
1538+
self.resource_type,
1539+
self.agentpool_decorator_mode,
1540+
)
1541+
1542+
# Construct the agentpool profile with mocked Azure API calls
1543+
with patch(
1544+
"azext_aks_preview.agentpool_decorator.cf_agent_pools",
1545+
return_value=Mock(list=Mock(return_value=[])),
1546+
):
1547+
agentpool = dec.construct_agentpool_profile_preview()
1548+
1549+
# Verify that mode is set to ManagedSystem
1550+
self.assertEqual(agentpool.mode, CONST_NODEPOOL_MODE_MANAGEDSYSTEM)
1551+
1552+
# Verify that name is preserved
1553+
self.assertEqual(agentpool.name, "testnp")
1554+
1555+
# Verify that all other properties are None (bypassed)
1556+
for attr_name in vars(agentpool):
1557+
if attr_name not in ['name', 'mode'] and not attr_name.startswith('_'):
1558+
attr_value = getattr(agentpool, attr_name)
1559+
self.assertIsNone(attr_value,
1560+
f"Attribute '{attr_name}' should be None but was '{attr_value}' when mode is ManagedSystem")
1561+
13501562

13511563
class AKSPreviewAgentPoolAddDecoratorStandaloneModeTestCase(
13521564
AKSPreviewAgentPoolAddDecoratorCommonTestCase
@@ -1399,6 +1611,9 @@ def test_set_up_agentpool_gateway_profile(self):
13991611
def test_set_up_virtual_machines_profile(self):
14001612
self.common_set_up_virtual_machines_profile()
14011613

1614+
def test_set_up_managed_system_mode(self):
1615+
self.common_set_up_managed_system_mode()
1616+
14021617
def test_construct_agentpool_profile_preview(self):
14031618
import inspect
14041619

@@ -1473,6 +1688,9 @@ def test_construct_agentpool_profile_preview(self):
14731688

14741689
dec_1.context.raw_param.print_usage_statistics()
14751690

1691+
def test_construct_agentpool_profile_preview_with_managed_system_mode(self):
1692+
self.common_construct_agentpool_profile_preview_with_managed_system_mode()
1693+
14761694

14771695
class AKSPreviewAgentPoolAddDecoratorManagedClusterModeTestCase(
14781696
AKSPreviewAgentPoolAddDecoratorCommonTestCase
@@ -1518,10 +1736,13 @@ def test_set_up_agentpool_windows_profile(self):
15181736

15191737
def test_set_up_agentpool_gateway_profile(self):
15201738
self.common_set_up_agentpool_gateway_profile()
1521-
1739+
15221740
def test_set_up_virtual_machines_profile(self):
15231741
self.common_set_up_virtual_machines_profile()
15241742

1743+
def test_set_up_managed_system_mode(self):
1744+
self.common_set_up_managed_system_mode()
1745+
15251746
def test_construct_agentpool_profile_preview(self):
15261747
import inspect
15271748

@@ -1885,7 +2106,7 @@ def common_update_fips_image(self):
18852106
# fail on passing the wrong agentpool object
18862107
with self.assertRaises(CLIInternalError):
18872108
dec_2.update_fips_image(None)
1888-
2109+
18892110
agentpool_2 = self.create_initialized_agentpool_instance(enable_fips=True)
18902111
dec_2.context.attach_agentpool(agentpool_2)
18912112
dec_agentpool_2 = dec_2.update_fips_image(agentpool_2)

0 commit comments

Comments
 (0)