2727@dataclass
2828class _Mocks :
2929 common_print_mock : MagicMock
30- common_exit_mock : MagicMock
30+ commands_print_mock : MagicMock
31+ commands_get_reservation_deployment_type : MagicMock
3132
3233
3334@pytest .fixture
@@ -36,12 +37,17 @@ def mock_common_print_and_exit(mocker):
3637 'xpk.commands.common.xpk_print' ,
3738 return_value = None ,
3839 )
39- common_exit_mock = mocker .patch (
40- 'xpk.commands.common.xpk_exit' ,
41- return_value = None ,
40+ commands_print_mock = mocker .patch (
41+ 'xpk.commands.cluster.xpk_print' , return_value = None
42+ )
43+ commands_get_reservation_deployment_type = mocker .patch (
44+ 'xpk.commands.cluster.get_reservation_deployment_type' ,
45+ return_value = 'DENSE' ,
4246 )
4347 return _Mocks (
44- common_print_mock = common_print_mock , common_exit_mock = common_exit_mock
48+ common_print_mock = common_print_mock ,
49+ commands_get_reservation_deployment_type = commands_get_reservation_deployment_type ,
50+ commands_print_mock = commands_print_mock ,
4551 )
4652
4753
@@ -61,32 +67,82 @@ def test_validate_cluster_create_args_for_correct_args_pass(
6167 _validate_cluster_create_args (args , DEFAULT_TEST_SYSTEM )
6268
6369 assert mock_common_print_and_exit .common_print_mock .call_count == 0
64- assert mock_common_print_and_exit .common_exit_mock .call_count == 0
6570
6671
6772def test_validate_cluster_create_args_for_correct_sub_slicing_args_pass (
6873 mock_common_print_and_exit : _Mocks ,
6974):
7075 FeatureFlags .SUB_SLICING_ENABLED = True
71- args = Namespace (sub_slicing = True )
76+ args = Namespace (
77+ sub_slicing = True ,
78+ reservation = 'test-reservation' ,
79+ project = 'project' ,
80+ zone = 'zone' ,
81+ )
7282
7383 _validate_cluster_create_args (args , SUB_SLICING_SYSTEM )
7484
7585 assert mock_common_print_and_exit .common_print_mock .call_count == 0
76- assert mock_common_print_and_exit .common_exit_mock .call_count == 0
7786
7887
7988def test_validate_cluster_create_args_for_not_supported_system_throws (
8089 mock_common_print_and_exit : _Mocks ,
8190):
8291 FeatureFlags .SUB_SLICING_ENABLED = True
83- args = Namespace (sub_slicing = True )
92+ args = Namespace (
93+ sub_slicing = True ,
94+ reservation = 'test-reservation' ,
95+ project = 'project' ,
96+ zone = 'zone' ,
97+ )
8498
85- _validate_cluster_create_args (args , DEFAULT_TEST_SYSTEM )
99+ with pytest .raises (SystemExit ):
100+ _validate_cluster_create_args (args , DEFAULT_TEST_SYSTEM )
86101
87102 assert mock_common_print_and_exit .common_print_mock .call_count == 1
88103 assert (
89104 mock_common_print_and_exit .common_print_mock .call_args [0 ][0 ]
90105 == 'Error: l4-1 does not support Sub-slicing.'
91106 )
92- assert mock_common_print_and_exit .common_exit_mock .call_count == 1
107+
108+
109+ def test_validate_cluster_create_args_for_missing_reservation (
110+ mock_common_print_and_exit : _Mocks ,
111+ ):
112+ FeatureFlags .SUB_SLICING_ENABLED = True
113+ args = Namespace (
114+ sub_slicing = True , project = 'project' , zone = 'zone' , reservation = None
115+ )
116+
117+ with pytest .raises (SystemExit ):
118+ _validate_cluster_create_args (args , SUB_SLICING_SYSTEM )
119+
120+ assert mock_common_print_and_exit .commands_print_mock .call_count == 1
121+ assert (
122+ 'Validation failed: Sub-slicing cluster creation requires'
123+ in mock_common_print_and_exit .commands_print_mock .call_args [0 ][0 ]
124+ )
125+
126+
127+ def test_validate_cluster_create_args_for_invalid_reservation (
128+ mock_common_print_and_exit : _Mocks ,
129+ ):
130+ FeatureFlags .SUB_SLICING_ENABLED = True
131+ args = Namespace (
132+ sub_slicing = True ,
133+ project = 'project' ,
134+ zone = 'zone' ,
135+ reservation = 'test-reservation' ,
136+ )
137+ mock_common_print_and_exit .commands_get_reservation_deployment_type .return_value = (
138+ 'SPARSE'
139+ )
140+
141+ with pytest .raises (SystemExit ):
142+ _validate_cluster_create_args (args , SUB_SLICING_SYSTEM )
143+
144+ assert mock_common_print_and_exit .commands_print_mock .call_count == 5
145+ assert (
146+ 'Refer to the documentation for more information on creating Cluster'
147+ in mock_common_print_and_exit .commands_print_mock .call_args [0 ][0 ]
148+ )
0 commit comments