未验证 提交 830e0c86 编写于 作者: C Cleber Rosa

Merge remote-tracking branch 'cacarrara/opensuse-vmimage-provider'

Signed-off-by: NCleber Rosa <crosa@redhat.com>
......@@ -83,6 +83,10 @@ class ImageProviderBase(object):
def version(self):
return self._best_version or self.get_version()
@property
def version_pattern(self):
return '^%s/$' % self._version
def _feed_html_parser(self, url, parser):
try:
data = urlopen(url).read()
......@@ -97,8 +101,7 @@ class ImageProviderBase(object):
"""
Probes the higher version available for the current parameters.
"""
pattern = '^%s/$' % self._version
parser = VMImageHtmlParser(pattern)
parser = VMImageHtmlParser(self.version_pattern)
self._feed_html_parser(self.url_versions, parser)
......@@ -275,6 +278,38 @@ class JeosImageProvider(ImageProviderBase):
self.image_pattern = 'jeos-{version}-{arch}.qcow2.xz$'
class OpenSUSEImageProvider(ImageProviderBase):
"""
OpenSUSE Image Provider
"""
HTML_ENCODING = 'iso-8859-1'
name = 'OpenSUSE'
def __init__(self, version='[0-9]{2}.[0-9]{1}', build=None, arch=os.uname()[4]):
super(OpenSUSEImageProvider, self).__init__(version, build, arch)
self.url_versions = 'https://download.opensuse.org/repositories/Cloud:/Images:/'
self.url_images = self.url_versions + 'Leap_{version}/images/'
if not build:
self.image_pattern = 'openSUSE-Leap-{version}-OpenStack.{arch}-((.)*).qcow2$'
else:
self.image_pattern = 'openSUSE-Leap-{version}-OpenStack.{arch}-{build}.qcow2$'
@property
def version_pattern(self):
return '^Leap_%s' % self._version
def get_best_version(self, versions):
# versions pattern equals Leap_15.0, Leap_42.0, Leap_XY.Z
version_numbers = [float(v.split('_')[1]) for v in versions]
if self._version.startswith('4'):
version_numbers = [v for v in version_numbers if v >= 40.0]
else:
version_numbers = [v for v in version_numbers if v < 40.0]
return max(version_numbers)
class Image(object):
def __init__(self, name, url, version, arch, checksum, algorithm,
cache_dir, snapshot_dir=None):
......
......@@ -113,5 +113,55 @@ class ImageProviderBase(unittest.TestCase):
self.assertIn('attributes are required to get image url', exc.exception.args[0])
class OpenSUSEImageProvider(unittest.TestCase):
def setUp(self):
self.suse_available_versions = ['Leap_15.0', 'Leap_42.1', 'Leap_42.2', 'Leap_42.3']
self.base_images_url = 'https://download.opensuse.org/repositories/Cloud:/Images:/Leap_15.0/images/'
@staticmethod
def get_html_with_image_link(image_link):
return '''
<a href="openSUSE-Leap-15.0-OpenStack.x86_64-0.0.4-Buildlp150.12.30.packages"></a>
<a href="%s"></a>
<a href="openSUSE-Leap-15.0-OpenStack.x86_64-0.0.4-Buildlp150.12.30.qcow2.sha256"></a>
''' % image_link
def test_get_best_version_default(self):
suse_latest_version = 15.0
suse_provider = vmimage.OpenSUSEImageProvider()
self.assertEqual(suse_provider.get_best_version(self.suse_available_versions),
suse_latest_version)
def test_get_best_version_leap_4_series(self):
suse_latest_version = 42.3
suse_provider = vmimage.OpenSUSEImageProvider(version='4(.)*')
self.assertEqual(suse_provider.get_best_version(self.suse_available_versions),
suse_latest_version)
@mock.patch('avocado.utils.vmimage.urlopen')
def test_get_image_url(self, urlopen_mock):
image = 'openSUSE-Leap-15.0-OpenStack.x86_64-0.0.4-Buildlp150.12.30.qcow2'
html_fixture = self.get_html_with_image_link(image)
urlread_mocked = mock.Mock(return_value=html_fixture)
urlopen_mock.return_value = mock.Mock(read=urlread_mocked)
expected_image_url = self.base_images_url + image
suse_provider = vmimage.OpenSUSEImageProvider()
suse_provider.get_version = mock.Mock(return_value='15.0')
self.assertEqual(suse_provider.get_image_url(), expected_image_url)
@mock.patch('avocado.utils.vmimage.urlopen')
def test_get_image_url_defining_build(self, urlopen_mock):
image = 'openSUSE-Leap-15.0-OpenStack.x86_64-1.1.1-Buildlp111.11.11.qcow2'
html_fixture = self.get_html_with_image_link(image)
urlread_mocked = mock.Mock(return_value=html_fixture)
urlopen_mock.return_value = mock.Mock(read=urlread_mocked)
expected_image_url = self.base_images_url + image
suse_provider = vmimage.OpenSUSEImageProvider(build='1.1.1-Buildlp111.11.11')
suse_provider.get_version = mock.Mock(return_value='15.0')
self.assertEqual(suse_provider.get_image_url(), expected_image_url)
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册