1
# Copyright (C) 2013 Hewlett-Packard Development Company, L.P.
3
# Author: Juerg Haefliger <juerg.haefliger@hp.com>
5
# Based on test_handler_set_hostname.py
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License version 3, as
9
# published by the Free Software Foundation.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
from cloudinit.config import cc_seed_random
25
from StringIO import StringIO
27
from cloudinit import cloud
28
from cloudinit import distros
29
from cloudinit import helpers
30
from cloudinit import util
32
from cloudinit.sources import DataSourceNone
34
from tests.unittests import helpers as t_help
38
LOG = logging.getLogger(__name__)
41
class TestRandomSeed(t_help.TestCase):
43
super(TestRandomSeed, self).setUp()
44
self._seed_file = tempfile.mktemp()
47
util.del_file(self._seed_file)
49
def _compress(self, text):
51
gz_fh = gzip.GzipFile(mode='wb', fileobj=contents)
54
return contents.getvalue()
56
def _get_cloud(self, distro, metadata=None):
57
paths = helpers.Paths({})
58
cls = distros.fetch(distro)
59
ubuntu_distro = cls(distro, {}, paths)
60
ds = DataSourceNone.DataSourceNone({}, ubuntu_distro, paths)
62
ds.metadata = metadata
63
return cloud.Cloud(ds, paths, {}, ubuntu_distro, None)
65
def test_append_random(self):
68
'file': self._seed_file,
69
'data': 'tiny-tim-was-here',
72
cc_seed_random.handle('test', cfg, self._get_cloud('ubuntu'), LOG, [])
73
contents = util.load_file(self._seed_file)
74
self.assertEquals("tiny-tim-was-here", contents)
76
def test_append_random_unknown_encoding(self):
77
data = self._compress("tiny-toe")
80
'file': self._seed_file,
82
'encoding': 'special_encoding',
85
self.assertRaises(IOError, cc_seed_random.handle, 'test', cfg,
86
self._get_cloud('ubuntu'), LOG, [])
88
def test_append_random_gzip(self):
89
data = self._compress("tiny-toe")
92
'file': self._seed_file,
97
cc_seed_random.handle('test', cfg, self._get_cloud('ubuntu'), LOG, [])
98
contents = util.load_file(self._seed_file)
99
self.assertEquals("tiny-toe", contents)
101
def test_append_random_gz(self):
102
data = self._compress("big-toe")
105
'file': self._seed_file,
110
cc_seed_random.handle('test', cfg, self._get_cloud('ubuntu'), LOG, [])
111
contents = util.load_file(self._seed_file)
112
self.assertEquals("big-toe", contents)
114
def test_append_random_base64(self):
115
data = base64.b64encode('bubbles')
118
'file': self._seed_file,
120
'encoding': 'base64',
123
cc_seed_random.handle('test', cfg, self._get_cloud('ubuntu'), LOG, [])
124
contents = util.load_file(self._seed_file)
125
self.assertEquals("bubbles", contents)
127
def test_append_random_b64(self):
128
data = base64.b64encode('kit-kat')
131
'file': self._seed_file,
136
cc_seed_random.handle('test', cfg, self._get_cloud('ubuntu'), LOG, [])
137
contents = util.load_file(self._seed_file)
138
self.assertEquals("kit-kat", contents)
140
def test_append_random_metadata(self):
143
'file': self._seed_file,
144
'data': 'tiny-tim-was-here',
147
c = self._get_cloud('ubuntu', {'random_seed': '-so-was-josh'})
148
cc_seed_random.handle('test', cfg, c, LOG, [])
149
contents = util.load_file(self._seed_file)
150
self.assertEquals('tiny-tim-was-here-so-was-josh', contents)