-
Notifications
You must be signed in to change notification settings - Fork 49
Feat: Added functionalities to remove duplicate parameters and overwrite existing ones. #506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
DannyLiCom
wants to merge
8
commits into
AI-Hypercomputer:develop
from
CIeNET-International:lidanny/feature/Extended_Subnet_Ranges
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
91b3ae1
feat: Adding a de-dupe function
DannyLiCom bc47601
Refactored GKE cluster creation to use a dictionary for argument mana…
DannyLiCom 85f0982
feat: Add process_gcloud_args to handle custom gcloud arguments.
DannyLiCom ab3cd89
feat: Added two unit tests.
DannyLiCom 928f693
Update test_gcloud_arg_processor.py
DannyLiCom e95b91f
feat: Updated the functionality of the process_gcloud_args function. …
DannyLiCom 1b7442c
Resolve lint issue
DannyLiCom 2a787ff
Merge branch 'develop' into lidanny/feature/Extended_Subnet_Ranges
DannyLiCom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,58 @@ | ||
"""Unit tests for the arg_parser module in xpk.commands.""" | ||
|
||
import unittest | ||
from src.xpk.commands.cluster import parse_command_args_to_dict | ||
|
||
|
||
class TestParseCommandArgsToDict(unittest.TestCase): | ||
"""Tests the parse_command_args_to_dict function from the cluster module.""" | ||
|
||
def test_empty_string(self): | ||
self.assertEqual(parse_command_args_to_dict(''), {}) | ||
|
||
def test_simple_key_value_pairs(self): | ||
result = parse_command_args_to_dict('--key1=value1 --key2=value2') | ||
self.assertEqual(result, {'--key1': 'value1', '--key2': 'value2'}) | ||
|
||
def test_flag_with_space_value(self): | ||
result = parse_command_args_to_dict('--key1 value1 --key2 value2') | ||
self.assertEqual(result, {'--key1': 'value1', '--key2': 'value2'}) | ||
|
||
def test_boolean_flags(self): | ||
result = parse_command_args_to_dict('--enable-feature --no-logs') | ||
self.assertEqual(result, {'--enable-feature': True, '--no-logs': True}) | ||
|
||
def test_mixed_formats(self): | ||
result = parse_command_args_to_dict( | ||
'--project=my-project --zone us-central1 --dry-run' | ||
) | ||
self.assertEqual( | ||
result, | ||
{'--project': 'my-project', '--zone': 'us-central1', '--dry-run': True}, | ||
) | ||
|
||
def test_quoted_values(self): | ||
result = parse_command_args_to_dict( | ||
'--description "My cluster with spaces" --name=test-cluster' | ||
) | ||
self.assertEqual( | ||
result, | ||
{'--description': 'My cluster with spaces', '--name': 'test-cluster'}, | ||
) | ||
|
||
def test_no_double_hyphen_flags(self): | ||
result = parse_command_args_to_dict('random-word -f --flag') | ||
self.assertEqual(result, {'--flag': True}) # Only --flag should be parsed | ||
|
||
def test_duplicate_keys_last_one_wins(self): | ||
result = parse_command_args_to_dict('--key=value1 --key=value2') | ||
self.assertEqual(result, {'--key': 'value2'}) | ||
|
||
def test_hyphenated_keys(self): | ||
result = parse_command_args_to_dict('--api-endpoint=some-url') | ||
self.assertEqual(result, {'--api-endpoint': 'some-url'}) | ||
|
||
|
||
if __name__ == '__main__': | ||
# Run python3 -m src.xpk.commands.tests.unit.test_arg_parser under the xpk folder. | ||
unittest.main() |
118 changes: 118 additions & 0 deletions
118
src/xpk/commands/tests/unit/test_gcloud_arg_processor.py
This file contains hidden or 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,118 @@ | ||
"""Unit tests for the gcloud_arg_parser module in xpk.commands.""" | ||
|
||
import unittest | ||
from src.xpk.commands.cluster import process_gcloud_args | ||
|
||
|
||
class TestProcessGcloudArgs(unittest.TestCase): | ||
"""Tests the process_gcloud_args function from the cluster module.""" | ||
|
||
def test_add_new_argument(self): | ||
final_args = {'--existing-key': 'existing-value'} | ||
user_args = {'--new-key': 'new-value'} | ||
process_gcloud_args(user_args, final_args) | ||
self.assertEqual( | ||
final_args, | ||
{'--existing-key': 'existing-value', '--new-key': 'new-value'}, | ||
) | ||
|
||
def test_override_existing_argument(self): | ||
final_args = {'--common-key': 'old-value'} | ||
user_args = {'--common-key': 'new-value'} | ||
process_gcloud_args(user_args, final_args) | ||
self.assertEqual(final_args, {'--common-key': 'new-value'}) | ||
|
||
def test_no_enable_flag_overrides_enable(self): | ||
final_args = {'--enable-logging': True} | ||
user_args = {'--no-enable-logging': True} | ||
process_gcloud_args(user_args, final_args) | ||
self.assertEqual(final_args, {'--no-enable-logging': True}) | ||
self.assertNotIn('--enable-logging', final_args) | ||
|
||
def test_enable_flag_overrides_no_enable(self): | ||
final_args = {'--no-enable-monitoring': True} | ||
user_args = {'--enable-monitoring': True} | ||
process_gcloud_args(user_args, final_args) | ||
self.assertEqual(final_args, {'--enable-monitoring': True}) | ||
self.assertNotIn('--no-enable-monitoring', final_args) | ||
|
||
def test_no_conflict(self): | ||
final_args = {'--param1': 'value1'} | ||
user_args = {'--param2': 'value2'} | ||
process_gcloud_args(user_args, final_args) | ||
self.assertEqual(final_args, {'--param1': 'value1', '--param2': 'value2'}) | ||
|
||
def test_empty_user_args(self): | ||
final_args = {'--param1': 'value1'} | ||
user_args = {} | ||
process_gcloud_args(user_args, final_args) | ||
self.assertEqual(final_args, {'--param1': 'value1'}) | ||
|
||
def test_complex_overrides(self): | ||
final_args = { | ||
'--zone': 'us-east1-b', | ||
'--enable-ip-alias': True, | ||
'--machine-type': 'n1-standard-4', | ||
'--no-enable-public-ip': ( | ||
True # This will be removed if --enable-public-ip is set | ||
), | ||
} | ||
user_args = { | ||
'--zone': 'us-central1-a', # Overrides | ||
'--no-enable-ip-alias': True, # Overrides --enable-ip-alias | ||
'--disk-size': '200GB', # New | ||
'--enable-public-ip': True, # Overrides --no-enable-public-ip | ||
} | ||
process_gcloud_args(user_args, final_args) | ||
self.assertEqual( | ||
final_args, | ||
{ | ||
'--zone': 'us-central1-a', | ||
'--no-enable-ip-alias': True, | ||
'--machine-type': 'n1-standard-4', # Not affected | ||
'--disk-size': '200GB', | ||
'--enable-public-ip': True, | ||
}, | ||
) | ||
self.assertNotIn('--enable-ip-alias', final_args) | ||
self.assertNotIn('--no-enable-public-ip', final_args) | ||
|
||
def test_disable_flag_is_added(self): | ||
""" | ||
Tests that a --disable- flag from user_args is simply added to final_args | ||
when no conflicting --enable- or --no-enable- flag exists. | ||
""" | ||
final_args = {'--existing-flag': 'value'} | ||
user_args = {'--disable-dataplane-v2': True} | ||
process_gcloud_args(user_args, final_args) | ||
self.assertEqual( | ||
final_args, {'--existing-flag': 'value', '--disable-dataplane-v2': True} | ||
) | ||
|
||
def test_enable_flag_overrides_disable(self): | ||
""" | ||
Tests that an --enable- flag from user_args overrides a --disable- flag | ||
present in final_args. | ||
""" | ||
final_args = {'--disable-logging': True} # Existing disable flag | ||
user_args = {'--enable-logging': True} # User wants to enable | ||
process_gcloud_args(user_args, final_args) | ||
self.assertEqual(final_args, {'--enable-logging': True}) | ||
self.assertNotIn('--disable-logging', final_args) | ||
|
||
def test_disable_flag_overrides_enable_from_user_args_order(self): | ||
""" | ||
Tests that if --enable- is in final_args and --disable- is in user_args, | ||
the --disable- from user_args takes precedence and removes the --enable-. | ||
This is implied by the order of processing (user_args overwrite/remove final_args). | ||
""" | ||
final_args = {'--enable-some-feature': True} | ||
user_args = {'--disable-some-feature': True} | ||
process_gcloud_args(user_args, final_args) | ||
self.assertEqual(final_args, {'--disable-some-feature': True}) | ||
self.assertNotIn('--enable-some-feature', final_args) | ||
|
||
|
||
if __name__ == '__main__': | ||
# Run python3 -m src.xpk.commands.tests.unit.test_gcloud_arg_processor under the xpk folder. | ||
unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.