diff options
author | Scott Moser <smoser@ubuntu.com> | 2012-03-08 13:35:55 -0500 |
---|---|---|
committer | Scott Moser <smoser@ubuntu.com> | 2012-03-08 13:35:55 -0500 |
commit | 61011d088ca011c2bc3a9f519519a833064cc922 (patch) | |
tree | 8e7772f9d43ae8f41b534f09c4e055417b8c6bf0 /tests/unittests/test_datasource/test_maas.py | |
parent | 0756ed017d02916fac240a07173a4bbb27eb74c0 (diff) | |
download | vyos-cloud-init-61011d088ca011c2bc3a9f519519a833064cc922.tar.gz vyos-cloud-init-61011d088ca011c2bc3a9f519519a833064cc922.zip |
add a test for read_maas_seed_url
Diffstat (limited to 'tests/unittests/test_datasource/test_maas.py')
-rw-r--r-- | tests/unittests/test_datasource/test_maas.py | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/tests/unittests/test_datasource/test_maas.py b/tests/unittests/test_datasource/test_maas.py index a090f873..3da2b0b3 100644 --- a/tests/unittests/test_datasource/test_maas.py +++ b/tests/unittests/test_datasource/test_maas.py @@ -2,15 +2,19 @@ from unittest import TestCase from tempfile import mkdtemp from shutil import rmtree import os +import urllib2 +from StringIO import StringIO from copy import copy from cloudinit.DataSourceMaaS import ( MaasSeedDirNone, MaasSeedDirMalformed, read_maas_seed_dir, + read_maas_seed_url, ) +from mocker import MockerTestCase -class TestMaasDataSource(TestCase): +class TestMaasDataSource(MockerTestCase): def setUp(self): super(TestMaasDataSource, self).setUp() @@ -94,7 +98,37 @@ class TestMaasDataSource(TestCase): def test_seed_url_valid(self): """Verify that valid seed_url is read as such""" - pass + valid = {'meta-data/instance-id': 'i-instanceid', + 'meta-data/local-hostname': 'test-hostname', 'user-data': 'foodata'} + + my_seed = "http://example.com/xmeta" + my_ver = "1999-99-99" + my_headers = {'header1': 'value1', 'header2': 'value2'} + + def my_headers_cb(url): + return(my_headers) + + mock_request = self.mocker.replace("urllib2.Request", passthrough=False) + mock_urlopen = self.mocker.replace("urllib2.urlopen", passthrough=False) + + for (key, val) in valid.iteritems(): + mock_request("%s/%s/%s" % (my_seed, my_ver, key), + data=None, headers=my_headers) + self.mocker.nospec() + self.mocker.result("fake-request-%s" % key) + mock_urlopen("fake-request-%s" % key, timeout=None) + self.mocker.result(StringIO(val)) + + self.mocker.replay() + + (userdata, metadata) = read_maas_seed_url(my_seed, + header_cb=my_headers_cb, version=my_ver) + + self.assertEqual("foodata", userdata) + self.assertEqual(metadata['instance-id'], + valid['meta-data/instance-id']) + self.assertEqual(metadata['local-hostname'], + valid['meta-data/local-hostname']) def test_seed_url_invalid(self): """Verify that invalid seed_url raises MaasSeedDirMalformed""" |