From 7b2fc4e014e6b7a43e6b8fc719d82c3d0b0a8bed Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Wed, 4 Apr 2012 21:35:32 -0400 Subject: initial checkin of kernel commandline cloud-config url support --- tests/unittests/test__init__.py | 51 +++++++++++++++++++++++++++++++++++++++-- tests/unittests/test_util.py | 18 ++++++++++++++- 2 files changed, 66 insertions(+), 3 deletions(-) (limited to 'tests/unittests') diff --git a/tests/unittests/test__init__.py b/tests/unittests/test__init__.py index e157fa77..4f60f0ea 100644 --- a/tests/unittests/test__init__.py +++ b/tests/unittests/test__init__.py @@ -2,8 +2,8 @@ from mocker import MockerTestCase, ANY, ARGS, KWARGS import os from cloudinit import (partwalker_handle_handler, handler_handle_part, - handler_register) -from cloudinit.util import write_file, logexc + handler_register, get_cmdline_url) +from cloudinit.util import write_file, logexc, readurl class TestPartwalkerHandleHandler(MockerTestCase): @@ -193,3 +193,50 @@ class TestHandlerHandlePart(MockerTestCase): handler_handle_part(mod_mock, self.data, self.ctype, self.filename, self.payload, self.frequency) + + +class TestCmdlineUrl(MockerTestCase): + def test_invalid_content(self): + url = "http://example.com/foo" + key = "mykey" + payload = "0" + cmdline = "ro %s=%s bar=1" % (key, url) + + mock_readurl = self.mocker.replace(readurl, passthrough=False) + mock_readurl(url) + self.mocker.result(payload) + + self.mocker.replay() + + self.assertEqual((key, url, None), + get_cmdline_url(names=[key], starts="xxxxxx", cmdline=cmdline)) + + def test_valid_content(self): + url = "http://example.com/foo" + key = "mykey" + payload = "xcloud-config\nmydata: foo\nbar: wark\n" + cmdline = "ro %s=%s bar=1" % (key, url) + + mock_readurl = self.mocker.replace(readurl, passthrough=False) + mock_readurl(url) + self.mocker.result(payload) + + self.mocker.replay() + + self.assertEqual((key, url, payload), + get_cmdline_url(names=[key], starts="xcloud-config", + cmdline=cmdline)) + + def test_no_key_found(self): + url = "http://example.com/foo" + key = "mykey" + cmdline = "ro %s=%s bar=1" % (key, url) + + self.mocker.replace(readurl, passthrough=False) + self.mocker.replay() + + self.assertEqual((None, None, None), + get_cmdline_url(names=["does-not-appear"], + starts="#cloud-config", cmdline=cmdline)) + +# vi: ts=4 expandtab diff --git a/tests/unittests/test_util.py b/tests/unittests/test_util.py index ca96bc60..e8f5885c 100644 --- a/tests/unittests/test_util.py +++ b/tests/unittests/test_util.py @@ -6,7 +6,8 @@ import os import stat from cloudinit.util import (mergedict, get_cfg_option_list_or_str, write_file, - delete_dir_contents) + delete_dir_contents, get_cmdline, + keyval_str_to_dict) class TestMergeDict(TestCase): @@ -248,3 +249,18 @@ class TestDeleteDirContents(TestCase): delete_dir_contents(self.tmp) self.assertDirEmpty(self.tmp) + + +class TestKeyValStrings(TestCase): + def test_keyval_str_to_dict(self): + expected = {'1': 'one', '2': 'one+one', 'ro': True} + cmdline = "1=one ro 2=one+one" + self.assertEqual(expected, keyval_str_to_dict(cmdline)) + + +class TestGetCmdline(TestCase): + def test_cmdline_reads_debug_env(self): + os.environ['DEBUG_PROC_CMDLINE'] = 'abcd 123' + self.assertEqual(os.environ['DEBUG_PROC_CMDLINE'], get_cmdline()) + +# vi: ts=4 expandtab -- cgit v1.2.3