diff options
author | Scott Moser <smoser@ubuntu.com> | 2014-03-04 14:35:29 -0500 |
---|---|---|
committer | Scott Moser <smoser@ubuntu.com> | 2014-03-04 14:35:29 -0500 |
commit | 3997ae9f49caede4bea7da4c0cd1c7d30d36fe0a (patch) | |
tree | ecdcf5232c9b5d2d0d0145cf1ce5ea3d7170d567 | |
parent | 5c95d6817a4aea17054addceef5d955c75390aa1 (diff) | |
parent | a9c7562ff3be9c552d757f24cfa61ef8985fe2e2 (diff) | |
download | vyos-cloud-init-3997ae9f49caede4bea7da4c0cd1c7d30d36fe0a.tar.gz vyos-cloud-init-3997ae9f49caede4bea7da4c0cd1c7d30d36fe0a.zip |
seed_random: do not capture command output, provide env RANDOM_SEED_FILE
call the command without capturing output, and provide RANDOM_SEED_FILE
to the environment that it is run in.
-rw-r--r-- | cloudinit/config/cc_seed_random.py | 9 | ||||
-rw-r--r-- | tests/unittests/test_handler/test_handler_seed_random.py | 22 |
2 files changed, 21 insertions, 10 deletions
diff --git a/cloudinit/config/cc_seed_random.py b/cloudinit/config/cc_seed_random.py index 599280f6..49a6b3e8 100644 --- a/cloudinit/config/cc_seed_random.py +++ b/cloudinit/config/cc_seed_random.py @@ -20,6 +20,7 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. import base64 +import os from StringIO import StringIO from cloudinit.settings import PER_INSTANCE @@ -43,7 +44,7 @@ def _decode(data, encoding=None): raise IOError("Unknown random_seed encoding: %s" % (encoding)) -def handle_random_seed_command(command, required): +def handle_random_seed_command(command, required, env=None): if not command and required: raise ValueError("no command found but required=true") elif not command: @@ -57,7 +58,7 @@ def handle_random_seed_command(command, required): else: LOG.debug("command '%s' not found for seed_command", cmd) return - util.subp(command) + util.subp(command, env=env, capture=False) def handle(name, cfg, cloud, log, _args): @@ -84,7 +85,9 @@ def handle(name, cfg, cloud, log, _args): command = mycfg.get('command', ['pollinate', '-q']) req = mycfg.get('command_required', False) try: - handle_random_seed_command(command=command, required=req) + env = os.environ.copy() + env['RANDOM_SEED_FILE'] = seed_path + handle_random_seed_command(command=command, required=req, env=env) except ValueError as e: log.warn("handling random command [%s] failed: %s", command, e) raise e diff --git a/tests/unittests/test_handler/test_handler_seed_random.py b/tests/unittests/test_handler/test_handler_seed_random.py index 00c50fc1..be2fa4a4 100644 --- a/tests/unittests/test_handler/test_handler_seed_random.py +++ b/tests/unittests/test_handler/test_handler_seed_random.py @@ -61,8 +61,11 @@ class TestRandomSeed(t_help.TestCase): def _which(self, program): return self.whichdata.get(program) - def _subp(self, args): - self.subp_called.append(tuple(args)) + def _subp(self, *args, **kwargs): + # supports subp calling with cmd as args or kwargs + if 'args' not in kwargs: + kwargs['args'] = args[0] + self.subp_called.append(kwargs) return def _compress(self, text): @@ -173,7 +176,8 @@ class TestRandomSeed(t_help.TestCase): self.whichdata = {'pollinate': '/usr/bin/pollinate'} cc_seed_random.handle('test', {}, c, LOG, []) - self.assertEquals(self.subp_called, [('pollinate', '-q')]) + subp_args = [f['args'] for f in self.subp_called] + self.assertIn(['pollinate', '-q'], subp_args) def test_seed_command_not_provided_pollinate_not_available(self): c = self._get_cloud('ubuntu', {}) @@ -195,15 +199,19 @@ class TestRandomSeed(t_help.TestCase): cfg = {'random_seed': {'command_required': True, 'command': ['foo']}} cc_seed_random.handle('test', cfg, c, LOG, []) - self.assertEquals(self.subp_called, [('foo',)]) + self.assertIn(['foo'], [f['args'] for f in self.subp_called]) - def test_seed_command_non_default(self): + def test_file_in_environment_for_command(self): c = self._get_cloud('ubuntu', {}) self.whichdata = {'foo': 'foo'} - cfg = {'random_seed': {'command_required': True, 'command': ['foo']}} + cfg = {'random_seed': {'command_required': True, 'command': ['foo'], + 'file': self._seed_file}} cc_seed_random.handle('test', cfg, c, LOG, []) - self.assertEquals(self.subp_called, [('foo',)]) + # this just instists that the first time subp was called, + # RANDOM_SEED_FILE was in the environment set up correctly + subp_env = [f['env'] for f in self.subp_called] + self.assertEqual(subp_env[0].get('RANDOM_SEED_FILE'), self._seed_file) def apply_patches(patches): |