uri: Add unit tests, fix some bugs

This commit is contained in:
Cole Robinson 2014-12-09 10:15:27 -05:00
parent 1c79b936f5
commit 3ac272e635
3 changed files with 51 additions and 5 deletions

View File

@ -25,6 +25,8 @@ import sys
import unittest
import xml.etree.ElementTree as ET
from virtinst import URISplit
_badmodules = ["gi.repository.Gtk", "gi.repository.Gdk"]
@ -129,3 +131,43 @@ class TestMisc(unittest.TestCase):
raise AssertionError(_("osdict._aliases changed size. It "
"should never be extended, since it is only for back "
"compat with pre-libosinfo osdict."))
class TestURI(unittest.TestCase):
"""
Test virtinst URISplit module
"""
def _compare(self, uri, scheme='',
transport='', port='', username='', path='',
hostname='', query='', fragment='',
is_ipv6=False, host_is_ipv4_string=False):
uriinfo = URISplit(uri)
self.assertEquals(scheme, uriinfo.scheme)
self.assertEquals(transport, uriinfo.transport)
self.assertEquals(port, uriinfo.port)
self.assertEquals(username, uriinfo.username)
self.assertEquals(path, uriinfo.path)
self.assertEquals(hostname, uriinfo.hostname)
self.assertEquals(query, uriinfo.query)
self.assertEquals(fragment, uriinfo.fragment)
self.assertEquals(is_ipv6, uriinfo.is_ipv6)
self.assertEquals(host_is_ipv4_string, uriinfo.host_is_ipv4_string)
self.assertEquals(uri, uriinfo.rebuild_uri())
def testURIs(self):
self._compare("lxc://", scheme="lxc")
self._compare("qemu:///session", scheme="qemu", path="/session")
self._compare("http://foobar.com:5901/my/example.path#my-frag",
scheme="http", hostname="foobar.com",
port="5901", path='/my/example.path',
fragment="my-frag")
self._compare(
"gluster+tcp://[1:2:3:4:5:6:7:8]:24007/testvol/dir/a.img",
scheme="gluster", transport="tcp",
hostname="1:2:3:4:5:6:7:8", port="24007",
path="/testvol/dir/a.img", is_ipv6=True)
self._compare(
"qemu+ssh://root@192.168.2.3/system?no_verify=1",
scheme="qemu", transport="ssh", username="root",
hostname="192.168.2.3", path="/system",
query="no_verify=1", host_is_ipv4_string=True)

View File

@ -184,6 +184,8 @@ class _StorageBase(object):
raise NotImplementedError()
def validate(self, disk):
raise NotImplementedError()
def is_network(self):
raise NotImplementedError()
# Storage creation routines
def is_size_conflict(self):
@ -280,6 +282,8 @@ class _StorageCreator(_StorageBase):
return None
def exists(self):
return False
def is_network(self):
return False
class CloneStorageCreator(_StorageCreator):

View File

@ -44,7 +44,7 @@ class URISplit(object):
elif ":" in self.hostname:
self.hostname, self.port = self.hostname.split(":", 1)
self.host_is_ipv4_string = bool(re.match(self.hostname, "[0-9.]+"))
self.host_is_ipv4_string = bool(re.match("^[0-9.]+$", self.hostname))
###################
@ -61,7 +61,7 @@ class URISplit(object):
delim = len(url)
return url[start:delim], url[delim:]
username = netloc = query = fragment = ''
scheme = username = netloc = query = fragment = ''
i = uri.find(":")
if i > 0:
scheme, uri = uri[:i].lower(), uri[i + 1:]
@ -75,8 +75,6 @@ class URISplit(object):
uri, fragment = uri.split('#', 1)
if '?' in uri:
uri, query = uri.split('?', 1)
else:
scheme = uri.lower()
return scheme, username, netloc, uri, query, fragment
@ -87,7 +85,7 @@ class URISplit(object):
def rebuild_uri(self):
ret = self.scheme
if self.transport:
ret += "+" % self.transport
ret += "+" + self.transport
ret += "://"
if self.username:
ret += self.username + "@"
@ -98,6 +96,8 @@ class URISplit(object):
ret += host
if self.port:
ret += ":" + self.port
if self.path:
ret += self.path
if self.query:
ret += "?" + self.query
if self.fragment: