Fix: Ensure known clouds are always in cloud config (#2029)

* Ensure known clouds are always in cloud config

* Fix failing test

* Add comment
This commit is contained in:
Derek Bekoe 2017-02-10 16:00:13 -08:00 коммит произвёл GitHub
Родитель d6bf96d38a
Коммит 71995260f0
2 изменённых файлов: 24 добавлений и 11 удалений

Просмотреть файл

@ -209,11 +209,20 @@ def get_custom_clouds():
return [c for c in get_clouds() if c.name not in known_cloud_names] return [c for c in get_clouds() if c.name not in known_cloud_names]
def get_clouds(): def _init_known_clouds():
if not os.path.isfile(CLOUD_CONFIG_FILE): config = configparser.SafeConfigParser()
for c in KNOWN_CLOUDS: config.read(CLOUD_CONFIG_FILE)
stored_cloud_names = config.sections()
for c in KNOWN_CLOUDS:
if c.name not in stored_cloud_names:
_save_cloud(c) _save_cloud(c)
def get_clouds():
# ensure the known clouds are always in cloud config
_init_known_clouds()
clouds = [] clouds = []
# load the config again as it may have changed
config = configparser.SafeConfigParser() config = configparser.SafeConfigParser()
config.read(CLOUD_CONFIG_FILE) config.read(CLOUD_CONFIG_FILE)
for section in config.sections(): for section in config.sections():

Просмотреть файл

@ -6,6 +6,7 @@
import tempfile import tempfile
import unittest import unittest
import mock import mock
from six.moves import configparser
from azure.cli.core.cloud import (Cloud, from azure.cli.core.cloud import (Cloud,
CloudEndpoints, CloudEndpoints,
@ -29,22 +30,25 @@ class TestCloud(unittest.TestCase):
@mock.patch('azure.cli.core.cloud.get_custom_clouds', lambda: []) @mock.patch('azure.cli.core.cloud.get_custom_clouds', lambda: [])
def test_add_get_delete_custom_cloud(self): def test_add_get_delete_custom_cloud(self):
endpoints = CloudEndpoints(management='http://management.contoso.com') endpoint_rm = 'http://management.contoso.com'
suffixes = CloudSuffixes(storage_endpoint='core.contoso.com') suffix_storage = 'core.contoso.com'
endpoints = CloudEndpoints(resource_manager=endpoint_rm)
suffixes = CloudSuffixes(storage_endpoint=suffix_storage)
c = Cloud('MyOwnCloud', endpoints=endpoints, suffixes=suffixes) c = Cloud('MyOwnCloud', endpoints=endpoints, suffixes=suffixes)
expected_config_file_result = '[MyOwnCloud]\nendpoint_management = ' \
'http://management.contoso.com\nsuffix_storage_endpoint = ' \
'core.contoso.com\n\n'
with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]) as\ with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]) as\
config_file: config_file:
with mock.patch('azure.cli.core.cloud.get_custom_clouds', lambda: []): with mock.patch('azure.cli.core.cloud.get_custom_clouds', lambda: []):
add_cloud(c) add_cloud(c)
with open(config_file, 'r') as cf: config = configparser.SafeConfigParser()
self.assertEqual(cf.read(), expected_config_file_result) config.read(config_file)
self.assertTrue(c.name in config.sections())
self.assertEqual(config.get(c.name, 'endpoint_resource_manager'), endpoint_rm)
self.assertEqual(config.get(c.name, 'suffix_storage_endpoint'), suffix_storage)
custom_clouds = get_custom_clouds() custom_clouds = get_custom_clouds()
self.assertEqual(len(custom_clouds), 1) self.assertEqual(len(custom_clouds), 1)
self.assertEqual(custom_clouds[0].name, c.name) self.assertEqual(custom_clouds[0].name, c.name)
self.assertEqual(custom_clouds[0].endpoints.management, c.endpoints.management) self.assertEqual(custom_clouds[0].endpoints.resource_manager,
c.endpoints.resource_manager)
self.assertEqual(custom_clouds[0].suffixes.storage_endpoint, self.assertEqual(custom_clouds[0].suffixes.storage_endpoint,
c.suffixes.storage_endpoint) c.suffixes.storage_endpoint)
with mock.patch('azure.cli.core.cloud._get_cloud', lambda _: c): with mock.patch('azure.cli.core.cloud._get_cloud', lambda _: c):