[Azure] Glance store unit tests
Change-Id: I9ca869cd63ed87a35eea976c2e1e8611f17a01fc
This commit is contained in:
parent
60cf0c8296
commit
232c9badbe
|
@ -52,10 +52,15 @@ class StoreLocation(location.StoreLocation):
|
|||
def _parse_attrs(self, attrs_info):
|
||||
attrs_list = attrs_info.strip('/').split('/')
|
||||
self.glance_id = attrs_list.pop()
|
||||
attrs_dict = {
|
||||
attrs_list[i].lower(): attrs_list[i + 1]
|
||||
for i in range(0, len(attrs_list), 2)
|
||||
}
|
||||
try:
|
||||
attrs_dict = {
|
||||
attrs_list[i].lower(): attrs_list[i + 1]
|
||||
for i in range(0, len(attrs_list), 2)
|
||||
}
|
||||
except IndexError:
|
||||
raise exceptions.BadStoreUri(
|
||||
message="Image URI should contain required attributes")
|
||||
|
||||
if self._sorted_uri_attrs != sorted(attrs_dict.keys()):
|
||||
raise exceptions.BadStoreUri(
|
||||
message="Image URI should contain required attributes")
|
||||
|
@ -75,7 +80,6 @@ class StoreLocation(location.StoreLocation):
|
|||
pieces = urllib.parse.urlparse(uri)
|
||||
self.scheme = pieces.scheme
|
||||
self._parse_attrs(pieces.netloc + pieces.path)
|
||||
self.image_name = self.images
|
||||
|
||||
|
||||
class Store(driver.Store):
|
||||
|
@ -120,7 +124,7 @@ class Store(driver.Store):
|
|||
return '%s://generic' % self.scheme, self.get_size(location, context)
|
||||
|
||||
def get_size(self, location, context=None):
|
||||
image_name = location.store_location.image_name.split("/")[-1]
|
||||
image_name = location.store_location.images.split("/")[-1]
|
||||
response = utils.get_image(self.azure_client, self.resource_group,
|
||||
image_name)
|
||||
size = response.storage_profile.os_disk.disk_size_gb
|
||||
|
|
|
@ -0,0 +1,97 @@
|
|||
"""
|
||||
Copyright (c) 2017 Platform9 Systems Inc.
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
not use this file except in compliance with the License. You may obtain
|
||||
a copy of the License at
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
WARRANTIES OR CONDITIONS OF ANY KIND, either expressed or implied. See the
|
||||
License for the specific language governing permissions and limitations
|
||||
under the License.
|
||||
"""
|
||||
|
||||
import mock
|
||||
import os
|
||||
|
||||
from azure.mgmt.compute import models
|
||||
from devtools_testutils.mgmt_testcase import fake_settings
|
||||
from glance_store._drivers.azure.store import Store
|
||||
from glance_store._drivers.azure.store import StoreLocation
|
||||
from glance_store import exceptions
|
||||
from glance_store import location
|
||||
from glance_store.location import Location
|
||||
from glance_store.tests import base
|
||||
from oslo_config import cfg
|
||||
|
||||
DATA_DIR = os.path.dirname(os.path.abspath(__file__)) + '/data'
|
||||
|
||||
RESOURCE_GROUP = 'omni_test_group'
|
||||
CLIENT_SECRET = 'fake_key'
|
||||
|
||||
|
||||
def fake_get_credentials(tenant_id, client_id, client_secret):
|
||||
return fake_settings.get_credentials()
|
||||
|
||||
|
||||
def fake_get_image(compute, resource_group, name):
|
||||
storage_profile = models.ImageStorageProfile(
|
||||
models.ImageOSDisk('Linux', 'Generalized'))
|
||||
_image = models.Image(location='eastus')
|
||||
_image.storage_profile = storage_profile
|
||||
return _image
|
||||
|
||||
|
||||
class AzureGlanceTestCase(base.StoreBaseTest):
|
||||
def setUp(self):
|
||||
super(AzureGlanceTestCase, self).setUp()
|
||||
self.creds_patcher = mock.patch(
|
||||
'glance_store._drivers.azure.utils.get_credentials').start()
|
||||
mock_creds = self.creds_patcher.start()
|
||||
mock_creds.side_effect = fake_get_credentials
|
||||
self.addCleanup(self.creds_patcher.stop)
|
||||
self.store = Store(cfg.CONF)
|
||||
self.store.tenant_id = fake_settings.TENANT_ID
|
||||
self.store.subscription_id = fake_settings.SUBSCRIPTION_ID
|
||||
self.store.client_id = fake_settings.CLIENT_OID
|
||||
self.store.client_secret = CLIENT_SECRET
|
||||
self.store.resource_group = RESOURCE_GROUP
|
||||
self.scheme = "azure"
|
||||
|
||||
@mock.patch('glance_store._drivers.azure.utils.get_image')
|
||||
def test_get_size(self, mock_get):
|
||||
mock_get.side_effect = fake_get_image
|
||||
store_specs = {}
|
||||
attrs_values = [
|
||||
self.store.subscription_id, 'Microsoft.Compute',
|
||||
self.store.resource_group, 'myImage'
|
||||
]
|
||||
for attr, value in zip(StoreLocation.uri_attrs, attrs_values):
|
||||
store_specs[attr] = value
|
||||
location = Location("azure", StoreLocation, cfg.CONF,
|
||||
store_specs=store_specs)
|
||||
self.assertEqual(location.store_location.images, "myImage")
|
||||
size = self.store.get_size(location)
|
||||
self.assertIsInstance(size, int)
|
||||
self.assertEqual(1, size)
|
||||
|
||||
def test_store_location_initialization(self):
|
||||
location.SCHEME_TO_CLS_MAP['azure'] = {}
|
||||
location.SCHEME_TO_CLS_MAP['azure']['location_class'] = StoreLocation
|
||||
_uri_path = []
|
||||
attrs_values = [
|
||||
self.store.subscription_id, 'Microsoft.Compute',
|
||||
self.store.resource_group, 'myImage'
|
||||
]
|
||||
for attr, value in zip(StoreLocation.uri_attrs, attrs_values):
|
||||
_uri_path.extend([attr, value])
|
||||
uri = "{0}://{1}/{2}".format(self.scheme, "/".join(_uri_path),
|
||||
"fake_glance_id")
|
||||
self.assertIsInstance(location.get_location_from_uri(uri), Location)
|
||||
|
||||
def test_store_location_initialization_with_invalid_url(self):
|
||||
location.SCHEME_TO_CLS_MAP["scheme"] = {}
|
||||
location.SCHEME_TO_CLS_MAP['scheme']['location_class'] = StoreLocation
|
||||
uri = "scheme:///fake_image_id"
|
||||
self.assertRaises(exceptions.BadStoreUri,
|
||||
location.get_location_from_uri, uri)
|
|
@ -1,6 +1,7 @@
|
|||
azure-mgmt-compute==1.0.0
|
||||
azure-mgmt-network==1.0.0
|
||||
azure-mgmt-compute==3.0.1
|
||||
azure-mgmt-network==1.5.0
|
||||
azure-mgmt-resource==1.1.0
|
||||
azure-mgmt-storage==1.4.0
|
||||
boto>=2.32.1 # MIT
|
||||
google-api-python-client>=1.4.2 # Apache-2.0
|
||||
google-api-python-client==1.6.4
|
||||
ipaddr
|
||||
|
|
|
@ -1 +1,3 @@
|
|||
moto>=1.0.1
|
||||
azure-devtools>=0.4.1
|
||||
-e git+https://github.com/Azure/azure-sdk-for-python.git#egg=azure-sdk-testutils&subdirectory=azure-sdk-testutils
|
||||
|
|
Loading…
Reference in New Issue