diff options
| author | Scott Moser <smoser@ubuntu.com> | 2013-09-09 20:31:30 -0400 | 
|---|---|---|
| committer | Scott Moser <smoser@ubuntu.com> | 2013-09-09 20:31:30 -0400 | 
| commit | 2a07fcd6444c7deb09063dff6b2f2d6e5385f355 (patch) | |
| tree | 7cde52630c3fb03ad6b53b5339a527b1a86c278f /tests | |
| parent | 0f84bfe5e0f06872a866432178a5f19d04195d30 (diff) | |
| parent | c74d928cc681ee98d0a3893dbe1ee8ff5fe361de (diff) | |
| download | vyos-cloud-init-2a07fcd6444c7deb09063dff6b2f2d6e5385f355.tar.gz vyos-cloud-init-2a07fcd6444c7deb09063dff6b2f2d6e5385f355.zip | |
Add support for reading 'random_seed' from data source.
A new field in the metadata has emerged on openstack config drive, one
that provides a way to seed the linux random generator.
This adds a 'random_seed' config module that writes and that it to
/dev/urandom.  Also added is support for reading that data on
azure via the hyper-v acpi table data.
In config drive datasource, it rewrites parts of the on_boot code to use a
little helper class.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/unittests/test_handler/test_handler_seed_random.py | 150 | 
1 files changed, 150 insertions, 0 deletions
| diff --git a/tests/unittests/test_handler/test_handler_seed_random.py b/tests/unittests/test_handler/test_handler_seed_random.py new file mode 100644 index 00000000..1b0675bb --- /dev/null +++ b/tests/unittests/test_handler/test_handler_seed_random.py @@ -0,0 +1,150 @@ +            #    Copyright (C) 2013 Hewlett-Packard Development Company, L.P. +# +#    Author: Juerg Haefliger <juerg.haefliger@hp.com> +# +#    Based on test_handler_set_hostname.py +# +#    This program is free software: you can redistribute it and/or modify +#    it under the terms of the GNU General Public License version 3, as +#    published by the Free Software Foundation. +# +#    This program is distributed in the hope that it will be useful, +#    but WITHOUT ANY WARRANTY; without even the implied warranty of +#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the +#    GNU General Public License for more details. +# +#    You should have received a copy of the GNU General Public License +#    along with this program.  If not, see <http://www.gnu.org/licenses/>. + +from cloudinit.config import cc_seed_random + +import base64 +import tempfile +import gzip + +from StringIO import StringIO + +from cloudinit import cloud +from cloudinit import distros +from cloudinit import helpers +from cloudinit import util + +from cloudinit.sources import DataSourceNone + +from tests.unittests import helpers as t_help + +import logging + +LOG = logging.getLogger(__name__) + + +class TestRandomSeed(t_help.TestCase): +    def setUp(self): +        super(TestRandomSeed, self).setUp() +        self._seed_file = tempfile.mktemp() + +    def tearDown(self): +        util.del_file(self._seed_file) + +    def _compress(self, text): +        contents = StringIO() +        gz_fh = gzip.GzipFile(mode='wb', fileobj=contents) +        gz_fh.write(text) +        gz_fh.close() +        return contents.getvalue() + +    def _get_cloud(self, distro, metadata=None): +        paths = helpers.Paths({}) +        cls = distros.fetch(distro) +        ubuntu_distro = cls(distro, {}, paths) +        ds = DataSourceNone.DataSourceNone({}, ubuntu_distro, paths) +        if metadata: +            ds.metadata = metadata +        return cloud.Cloud(ds, paths, {}, ubuntu_distro, None) + +    def test_append_random(self): +        cfg = { +            'random_seed': { +                'file': self._seed_file, +                'data': 'tiny-tim-was-here', +            } +        } +        cc_seed_random.handle('test', cfg, self._get_cloud('ubuntu'), LOG, []) +        contents = util.load_file(self._seed_file) +        self.assertEquals("tiny-tim-was-here", contents) + +    def test_append_random_unknown_encoding(self): +        data = self._compress("tiny-toe") +        cfg = { +            'random_seed': { +                'file': self._seed_file, +                'data': data, +                'encoding': 'special_encoding', +            } +        } +        self.assertRaises(IOError, cc_seed_random.handle, 'test', cfg, +                          self._get_cloud('ubuntu'), LOG, []) + +    def test_append_random_gzip(self): +        data = self._compress("tiny-toe") +        cfg = { +            'random_seed': { +                'file': self._seed_file, +                'data': data, +                'encoding': 'gzip', +            } +        } +        cc_seed_random.handle('test', cfg, self._get_cloud('ubuntu'), LOG, []) +        contents = util.load_file(self._seed_file) +        self.assertEquals("tiny-toe", contents) + +    def test_append_random_gz(self): +        data = self._compress("big-toe") +        cfg = { +            'random_seed': { +                'file': self._seed_file, +                'data': data, +                'encoding': 'gz', +            } +        } +        cc_seed_random.handle('test', cfg, self._get_cloud('ubuntu'), LOG, []) +        contents = util.load_file(self._seed_file) +        self.assertEquals("big-toe", contents) + +    def test_append_random_base64(self): +        data = base64.b64encode('bubbles') +        cfg = { +            'random_seed': { +                'file': self._seed_file, +                'data': data, +                'encoding': 'base64', +            } +        } +        cc_seed_random.handle('test', cfg, self._get_cloud('ubuntu'), LOG, []) +        contents = util.load_file(self._seed_file) +        self.assertEquals("bubbles", contents) + +    def test_append_random_b64(self): +        data = base64.b64encode('kit-kat') +        cfg = { +            'random_seed': { +                'file': self._seed_file, +                'data': data, +                'encoding': 'b64', +            } +        } +        cc_seed_random.handle('test', cfg, self._get_cloud('ubuntu'), LOG, []) +        contents = util.load_file(self._seed_file) +        self.assertEquals("kit-kat", contents) + +    def test_append_random_metadata(self): +        cfg = { +            'random_seed': { +                'file': self._seed_file, +                'data': 'tiny-tim-was-here', +            } +        } +        c = self._get_cloud('ubuntu', {'random_seed': '-so-was-josh'}) +        cc_seed_random.handle('test', cfg, c, LOG, []) +        contents = util.load_file(self._seed_file) +        self.assertEquals('tiny-tim-was-here-so-was-josh', contents) | 
