# Copyright 2014 OpenStack Foundation
# All Rights Reserved.
#
# 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 express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
from six.moves import urllib
from glance.common import exception
from glance.common.scripts import utils as script_utils
import glance.tests.utils as test_utils
[docs]class TestScriptsUtils(test_utils.BaseTestCase):
[docs] def setUp(self):
super(TestScriptsUtils, self).setUp()
[docs] def test_get_task(self):
task = mock.ANY
task_repo = mock.Mock(return_value=task)
task_id = mock.ANY
self.assertEqual(task, script_utils.get_task(task_repo, task_id))
[docs] def test_set_base_image_properties(self):
properties = {}
script_utils.set_base_image_properties(properties)
self.assertIn('disk_format', properties)
self.assertIn('container_format', properties)
self.assertEqual('qcow2', properties['disk_format'])
self.assertEqual('bare', properties['container_format'])
[docs] def test_set_base_image_properties_none(self):
properties = None
script_utils.set_base_image_properties(properties)
self.assertIsNone(properties)
[docs] def test_set_base_image_properties_not_empty(self):
properties = {'disk_format': 'vmdk', 'container_format': 'bare'}
script_utils.set_base_image_properties(properties)
self.assertIn('disk_format', properties)
self.assertIn('container_format', properties)
self.assertEqual('vmdk', properties.get('disk_format'))
self.assertEqual('bare', properties.get('container_format'))
[docs] def test_validate_location_http(self):
location = 'http://example.com'
self.assertEqual(location,
script_utils.validate_location_uri(location))
[docs] def test_validate_location_https(self):
location = 'https://example.com'
self.assertEqual(location,
script_utils.validate_location_uri(location))
[docs] def test_validate_location_none_error(self):
self.assertRaises(exception.BadStoreUri,
script_utils.validate_location_uri, '')
[docs] def test_validate_location_file_location_error(self):
self.assertRaises(exception.BadStoreUri,
script_utils.validate_location_uri, "file:///tmp")
self.assertRaises(exception.BadStoreUri,
script_utils.validate_location_uri,
"filesystem:///tmp")
[docs] def test_validate_location_unsupported_error(self):
location = 'swift'
self.assertRaises(urllib.error.URLError,
script_utils.validate_location_uri, location)
location = 'swift+http'
self.assertRaises(urllib.error.URLError,
script_utils.validate_location_uri, location)
location = 'swift+https'
self.assertRaises(urllib.error.URLError,
script_utils.validate_location_uri, location)
location = 'swift+config'
self.assertRaises(urllib.error.URLError,
script_utils.validate_location_uri, location)
location = 'vsphere'
self.assertRaises(urllib.error.URLError,
script_utils.validate_location_uri, location)
location = 'sheepdog://'
self.assertRaises(urllib.error.URLError,
script_utils.validate_location_uri, location)
location = 'rbd://'
self.assertRaises(urllib.error.URLError,
script_utils.validate_location_uri, location)
location = 'cinder://'
self.assertRaises(urllib.error.URLError,
script_utils.validate_location_uri, location)