improve test coverage

add some tests for safeyaml and test_url_helper

Change-Id: I3a88f52e6c02f6f1f8f31363886f6fab223c14e3
This commit is contained in:
Scott Moser 2015-07-17 16:46:12 -04:00
parent 2856779179
commit 303afb7e74
2 changed files with 35 additions and 0 deletions

View File

@ -6,6 +6,8 @@
from cloudinit import safeyaml as yaml
from cloudinit import tests
import tempfile
class TestSafeYaml(tests.TestCase):
def test_simple(self):
@ -27,3 +29,19 @@ class TestSafeYaml(tests.TestCase):
# in the past this type was allowed, but not now, so explicit test.
blob = "{k1: !!python/unicode 'my unicode', k2: my string}"
self.assertRaises(yaml.YAMLError, yaml.loads, blob)
def test_dumps_returns_string(self):
self.assertTrue(
isinstance(yaml.dumps(867 - 5309), (str,)))
def test_dumps_is_loadable(self):
mydata = {'a': 'hey', 'b': ['bee', 'Bea']}
self.assertEqual(yaml.loads(yaml.dumps(mydata)), mydata)
def test_load(self):
valid_yaml = "foo: bar"
expected = {'foo': 'bar'}
with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmpf:
tmpf.write(valid_yaml)
tmpf.close()
self.assertEqual(yaml.load(tmpf.name), expected)

View File

@ -80,6 +80,23 @@ class UrlHelperFetchTest(tests.TestCase):
self.assertEqual(b"it worked!", resp.contents)
self.assertEqual(url_helper.OK, resp.status_code)
@httpretty.activate
def test_no_protocol_url(self):
body = b'it worked!'
no_proto = 'www.yahoo.com'
httpretty.register_uri(httpretty.GET, "http://" + no_proto, body=body)
resp = url_helper.read_url(no_proto)
self.assertTrue(resp.url.startswith("http://"))
@httpretty.activate
def test_response_has_url(self):
body = b'it worked!'
url = 'http://www.yahoo.com/'
httpretty.register_uri(httpretty.GET, url, body=body)
resp = url_helper.read_url(url)
self.assertEqual(resp.url, url)
self.assertEqual(body, resp.contents)
@httpretty.activate
def test_retry_url_fetch(self):
httpretty.register_uri(httpretty.GET,