
This patch adds executil.BaseCommand, which contains some scaffolding for building generic and simple CLI commands. Also, executil.py contains a couple of predefined commands, such as Python, Shell, Powershell etc. fileexecutils, as well as userdatautils, were rewritten to use the new command framework instead, which greatly increases usability and simplifies cross platform issues. Change-Id: Ifcd91da95a272ef68d56491f90bc213826eb7679
111 lines
3.6 KiB
Python
111 lines
3.6 KiB
Python
# Copyright 2014 Cloudbase Solutions Srl
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
|
|
import os
|
|
import unittest
|
|
|
|
import mock
|
|
|
|
from cloudbaseinit.plugins.common import executil
|
|
from cloudbaseinit.tests import testutils
|
|
|
|
|
|
def _remove_file(filepath):
|
|
try:
|
|
os.remove(filepath)
|
|
except OSError:
|
|
pass
|
|
|
|
|
|
@mock.patch('cloudbaseinit.osutils.factory.get_os_utils')
|
|
class ExecUtilTest(unittest.TestCase):
|
|
|
|
def test_from_data(self, _):
|
|
command = executil.BaseCommand.from_data(b"test")
|
|
|
|
self.assertIsInstance(command, executil.BaseCommand)
|
|
|
|
# Not public API, though.
|
|
self.assertTrue(os.path.exists(command._target_path),
|
|
command._target_path)
|
|
self.addCleanup(_remove_file, command._target_path)
|
|
|
|
with open(command._target_path) as stream:
|
|
data = stream.read()
|
|
|
|
self.assertEqual("test", data)
|
|
command._cleanup()
|
|
self.assertFalse(os.path.exists(command._target_path),
|
|
command._target_path)
|
|
|
|
def test_args(self, _):
|
|
class FakeCommand(executil.BaseCommand):
|
|
command = mock.sentinel.command
|
|
|
|
with testutils.create_tempfile() as tmp:
|
|
fake_command = FakeCommand(tmp)
|
|
self.assertEqual([mock.sentinel.command, tmp],
|
|
fake_command.args)
|
|
|
|
fake_command = executil.BaseCommand(tmp)
|
|
self.assertEqual([tmp], fake_command.args)
|
|
|
|
def test_from_data_extension(self, _):
|
|
class FakeCommand(executil.BaseCommand):
|
|
command = mock.sentinel.command
|
|
extension = ".test"
|
|
|
|
command = FakeCommand.from_data(b"test")
|
|
self.assertIsInstance(command, FakeCommand)
|
|
|
|
self.addCleanup(os.remove, command._target_path)
|
|
self.assertTrue(command._target_path.endswith(".test"))
|
|
|
|
def test_execute_normal_command(self, mock_get_os_utils):
|
|
mock_osutils = mock_get_os_utils()
|
|
|
|
with testutils.create_tempfile() as tmp:
|
|
command = executil.BaseCommand(tmp)
|
|
command.execute()
|
|
|
|
mock_osutils.execute_process.assert_called_once_with(
|
|
[command._target_path],
|
|
shell=command.shell)
|
|
|
|
# test __call__ API.
|
|
mock_osutils.execute_process.reset_mock()
|
|
command()
|
|
|
|
mock_osutils.execute_process.assert_called_once_with(
|
|
[command._target_path],
|
|
shell=command.shell)
|
|
|
|
def test_execute_powershell_command(self, mock_get_os_utils):
|
|
mock_osutils = mock_get_os_utils()
|
|
|
|
with testutils.create_tempfile() as tmp:
|
|
command = executil.Powershell(tmp)
|
|
command.execute()
|
|
|
|
mock_osutils.execute_powershell_script.assert_called_once_with(
|
|
command._target_path, command.sysnative)
|
|
|
|
def test_execute_cleanup(self, _):
|
|
with testutils.create_tempfile() as tmp:
|
|
cleanup = mock.Mock()
|
|
command = executil.BaseCommand(tmp, cleanup=cleanup)
|
|
command.execute()
|
|
|
|
cleanup.assert_called_once_with()
|