forked from hitachienergy/epiphany
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sorting entries in the inventory file (hitachienergy#1076)
* Add sorting hostnames for `Azure` and `Any` provider in the inventory file
- Loading branch information
Showing
8 changed files
with
325 additions
and
8 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 |
---|---|---|
@@ -1,4 +1,20 @@ | ||
class AnsibleHostModel: | ||
def __init__(self, name, ip): | ||
self.name = name | ||
self.ip = ip | ||
def __init__(self, name: str, ip: str): | ||
self.name: str = name | ||
self.ip: str = ip | ||
|
||
def __eq__(self, other) -> bool: | ||
return (self.name == other.name and | ||
self.ip == other.ip) | ||
|
||
def __lt__(self, other) -> bool: | ||
pass | ||
|
||
|
||
class AnsibleOrderedHostModel(AnsibleHostModel): | ||
""" | ||
Sortable variant of AnsibleHostModel | ||
""" | ||
|
||
def __lt__(self, other) -> bool: | ||
return self.name < other.name |
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,33 @@ | ||
from unittest import mock, TestCase | ||
|
||
from cli.engine.providers.any.APIProxy import APIProxy | ||
from cli.models.AnsibleHostModel import AnsibleOrderedHostModel | ||
from tests.unit.engine.providers.data.APIProxy_data import CLUSTER_MODEL, CONFIG_DOC | ||
|
||
|
||
class APIProxyTest(TestCase): | ||
""" Tests for `any` provider """ | ||
|
||
def setUp(self): | ||
self.maxDiff = None | ||
|
||
|
||
def test_get_ips_for_feature(self): | ||
""" | ||
Make sure that hostnames in inventory are sorted. | ||
""" | ||
|
||
with mock.patch('cli.engine.providers.any.APIProxy.Log', return_value='mock_Log') as mock_Log: | ||
proxy = APIProxy(CLUSTER_MODEL('any'), CONFIG_DOC()) | ||
|
||
EXPECTED_RESULT = [ | ||
AnsibleOrderedHostModel('service-vm-0', '20.73.105.240'), | ||
AnsibleOrderedHostModel('service-vm-1', '20.73.105.188'), | ||
AnsibleOrderedHostModel('service-vm-2', '20.73.105.18'), | ||
AnsibleOrderedHostModel('service-vm-3', '20.73.105.33'), | ||
AnsibleOrderedHostModel('service-vm-4', '20.73.105.54') | ||
] | ||
|
||
result = proxy.get_ips_for_feature('service') | ||
|
||
self.assertListEqual(EXPECTED_RESULT, result) |
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,33 @@ | ||
from unittest import mock, TestCase | ||
|
||
from cli.engine.providers.azure.APIProxy import APIProxy | ||
from cli.models.AnsibleHostModel import AnsibleOrderedHostModel | ||
from tests.unit.engine.providers.data.APIProxy_data import CLUSTER_MODEL, RUNNING_INSTANCES | ||
|
||
|
||
class APIProxyTest(TestCase): | ||
""" Tests for `azure` provider """ | ||
|
||
def setUp(self): | ||
self.maxDiff = None | ||
|
||
def test_get_ips_for_feature(self): | ||
""" | ||
Make sure that hostnames in inventory are sorted. | ||
""" | ||
|
||
with mock.patch('cli.engine.providers.azure.APIProxy.Log', return_value='mock_Log') as mock_Log: | ||
proxy = APIProxy(CLUSTER_MODEL('azure'), RUNNING_INSTANCES) | ||
proxy.run = (lambda *args: RUNNING_INSTANCES) # mock run with prepared data | ||
|
||
EXPECTED_RESULT = [ | ||
AnsibleOrderedHostModel('prefix-cluster-service-vm-0', '20.73.105.240'), | ||
AnsibleOrderedHostModel('prefix-cluster-service-vm-1', '20.73.105.188'), | ||
AnsibleOrderedHostModel('prefix-cluster-service-vm-2', '20.73.105.18'), | ||
AnsibleOrderedHostModel('prefix-cluster-service-vm-3', '20.73.105.33'), | ||
AnsibleOrderedHostModel('prefix-cluster-service-vm-4', '20.73.105.54') | ||
] | ||
|
||
result = proxy.get_ips_for_feature('service') | ||
|
||
self.assertListEqual(EXPECTED_RESULT, result) |
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,193 @@ | ||
from typing import Dict, List | ||
|
||
from cli.helpers.ObjDict import ObjDict | ||
from cli.helpers.objdict_helpers import dict_to_objdict | ||
|
||
|
||
def CONFIG_DOC() -> ObjDict: | ||
return dict_to_objdict([ | ||
{ | ||
'kind': 'infrastructure/machine', | ||
'title': 'Virtual Machine Infra', | ||
'provider': 'any', | ||
'name': 'service-0', | ||
'specification': { | ||
'ip': '20.73.105.18', | ||
'hostname': 'service-vm-2' | ||
}, | ||
'version': '1.3.0dev' | ||
}, | ||
{ | ||
'kind': 'infrastructure/machine', | ||
'title': 'Virtual Machine Infra', | ||
'provider': 'any', | ||
'name': 'service-1', | ||
'specification': { | ||
'ip': '20.73.105.54', | ||
'hostname': 'service-vm-4' | ||
}, | ||
'version': '1.3.0dev' | ||
}, | ||
{ | ||
'kind': 'infrastructure/machine', | ||
'title': 'Virtual Machine Infra', | ||
'provider': 'any', | ||
'name': 'service-2', | ||
'specification': { | ||
'ip': '20.73.105.188', | ||
'hostname': 'service-vm-1' | ||
}, | ||
'version': '1.3.0dev' | ||
}, | ||
{ | ||
'kind': 'infrastructure/machine', | ||
'title': 'Virtual Machine Infra', | ||
'provider': 'any', | ||
'name': 'service-3', | ||
'specification': { | ||
'ip': '20.73.105.240', | ||
'hostname': 'service-vm-0' | ||
}, | ||
'version': '1.3.0dev' | ||
}, | ||
{ | ||
'kind': 'infrastructure/machine', | ||
'title': 'Virtual Machine Infra', | ||
'provider': 'any', | ||
'name': 'service-4', | ||
'specification': { | ||
'ip': '20.73.105.33', | ||
'hostname': 'service-vm-3' | ||
}, | ||
'version': '1.3.0dev' | ||
}, | ||
]) | ||
|
||
|
||
def CLUSTER_MODEL(provider: str) -> ObjDict: | ||
return dict_to_objdict({ | ||
'kind': 'epiphany-cluster', | ||
'title': 'Epiphany cluster Config', | ||
'provider': f'{provider}', | ||
'name': 'default', | ||
'specification': { | ||
'prefix': 'prefix', | ||
'name': 'cluster', | ||
'admin_user': { | ||
'name': 'username', | ||
'key_path': '/path/to/key' | ||
}, | ||
'cloud': { | ||
'k8s_as_cloud_service': False, | ||
'subscription_name': 'Subscription Name', | ||
'vnet_address_pool': '10.1.0.0/20', | ||
'use_public_ips': True, | ||
'use_service_principal': False, | ||
'region': 'West Europe', | ||
'network': {'use_network_security_groups': True}, | ||
'default_os_image': 'default' | ||
}, | ||
'components': { | ||
'service': { | ||
'count': 5, | ||
'machine': 'service-machine', | ||
'configuration': 'default', | ||
'subnets': [{'address_pool': '10.1.8.0/24'}], | ||
'machines': ['service-0', | ||
'service-1', | ||
'service-2', | ||
'service-3', | ||
'service-4'] | ||
} | ||
} | ||
}, | ||
'version': '1.3.0dev' | ||
}) | ||
|
||
RUNNING_INSTANCES: List[List[Dict]] = [ | ||
[ | ||
{'virtualMachine': { | ||
'name': 'prefix-cluster-service-vm-0', | ||
'network': { | ||
'privateIpAddresses': ['10.1.8.6'], | ||
'publicIpAddresses': [ | ||
{'id': '/subscriptions/subscription_hash/resourceGroups/prefix-cluster-rg/providers/Microsoft.Network/publicIPAddresses/prefix-cluster-service-pubip-0', | ||
'ipAddress': '20.73.105.240', | ||
'ipAllocationMethod': 'Static', | ||
'name': 'prefix-cluster-service-pubip-0', | ||
'resourceGroup': 'prefix-cluster-rg', | ||
'zone': '1'} | ||
] | ||
}, | ||
'resourceGroup': 'prefix-cluster-rg'} | ||
} | ||
], | ||
[ | ||
{'virtualMachine': { | ||
'name': 'prefix-cluster-service-vm-2', | ||
'network': { | ||
'privateIpAddresses': ['10.1.8.5'], | ||
'publicIpAddresses': [ | ||
{'id': '/subscriptions/subscription_hash/resourceGroups/prefix-cluster-rg/providers/Microsoft.Network/publicIPAddresses/prefix-cluster-service-pubip-2', | ||
'ipAddress': '20.73.105.18', | ||
'ipAllocationMethod': 'Static', | ||
'name': 'prefix-cluster-service-pubip-2', | ||
'resourceGroup': 'prefix-cluster-rg', | ||
'zone': '1'} | ||
] | ||
}, | ||
'resourceGroup': 'prefix-cluster-rg'} | ||
} | ||
], | ||
[ | ||
{'virtualMachine': { | ||
'name': 'prefix-cluster-service-vm-1', | ||
'network': { | ||
'privateIpAddresses': ['10.1.8.4'], | ||
'publicIpAddresses': [ | ||
{'id': '/subscriptions/subscription_hash/resourceGroups/prefix-cluster-rg/providers/Microsoft.Network/publicIPAddresses/prefix-cluster-service-pubip-2', | ||
'ipAddress': '20.73.105.188', | ||
'ipAllocationMethod': 'Static', | ||
'name': 'prefix-cluster-service-pubip-1', | ||
'resourceGroup': 'prefix-cluster-rg', | ||
'zone': '1'} | ||
] | ||
}, | ||
'resourceGroup': 'prefix-cluster-rg'} | ||
} | ||
], | ||
[ | ||
{'virtualMachine': { | ||
'name': 'prefix-cluster-service-vm-4', | ||
'network': { | ||
'privateIpAddresses': ['10.1.8.3'], | ||
'publicIpAddresses': [ | ||
{'id': '/subscriptions/subscription_hash/resourceGroups/prefix-cluster-rg/providers/Microsoft.Network/publicIPAddresses/prefix-cluster-service-pubip-2', | ||
'ipAddress': '20.73.105.54', | ||
'ipAllocationMethod': 'Static', | ||
'name': 'prefix-cluster-service-pubip-4', | ||
'resourceGroup': 'prefix-cluster-rg', | ||
'zone': '1'} | ||
] | ||
}, | ||
'resourceGroup': 'prefix-cluster-rg'} | ||
} | ||
], | ||
[ | ||
{'virtualMachine': { | ||
'name': 'prefix-cluster-service-vm-3', | ||
'network': { | ||
'privateIpAddresses': ['10.1.8.2'], | ||
'publicIpAddresses': [ | ||
{'id': '/subscriptions/subscription_hash/resourceGroups/prefix-cluster-rg/providers/Microsoft.Network/publicIPAddresses/prefix-cluster-service-pubip-2', | ||
'ipAddress': '20.73.105.33', | ||
'ipAllocationMethod': 'Static', | ||
'name': 'prefix-cluster-service-pubip-3', | ||
'resourceGroup': 'prefix-cluster-rg', | ||
'zone': '1'} | ||
] | ||
}, | ||
'resourceGroup': 'prefix-cluster-rg'} | ||
} | ||
] | ||
] |
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,35 @@ | ||
from typing import List | ||
from unittest import TestCase | ||
|
||
from cli.models.AnsibleHostModel import AnsibleOrderedHostModel | ||
|
||
|
||
class AnsibleHostModelTest(TestCase): | ||
|
||
def setUp(self): | ||
self.maxDiff = None | ||
|
||
def test_sort(self): | ||
""" | ||
Test the `less` operator | ||
""" | ||
|
||
EXPECTED_HOSTS: List[AnsibleOrderedHostModel] = [ | ||
AnsibleOrderedHostModel('prefix-cluster-service-vm-0', '20.82.14.10'), | ||
AnsibleOrderedHostModel('prefix-cluster-service-vm-1', '20.82.14.34'), | ||
AnsibleOrderedHostModel('prefix-cluster-service-vm-2', '20.82.14.101'), | ||
AnsibleOrderedHostModel('prefix-cluster-service-vm-3', '20.82.14.67'), | ||
AnsibleOrderedHostModel('prefix-cluster-service-vm-4', '20.82.14.11'), | ||
] | ||
|
||
unordered_hosts: List[AnsibleOrderedHostModel] = [ | ||
AnsibleOrderedHostModel('prefix-cluster-service-vm-4', '20.82.14.11'), | ||
AnsibleOrderedHostModel('prefix-cluster-service-vm-1', '20.82.14.34'), | ||
AnsibleOrderedHostModel('prefix-cluster-service-vm-3', '20.82.14.67'), | ||
AnsibleOrderedHostModel('prefix-cluster-service-vm-0', '20.82.14.10'), | ||
AnsibleOrderedHostModel('prefix-cluster-service-vm-2', '20.82.14.101') | ||
] | ||
|
||
unordered_hosts.sort() | ||
|
||
self.assertListEqual(EXPECTED_HOSTS, unordered_hosts) |