
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
86 lines
2.9 KiB
Python
86 lines
2.9 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.plugins.windows import userdatautils
|
|
|
|
|
|
def _safe_remove(filepath):
|
|
try:
|
|
os.remove(filepath)
|
|
except OSError:
|
|
pass
|
|
|
|
|
|
@mock.patch('cloudbaseinit.osutils.factory.get_os_utils')
|
|
class UserDataUtilsTest(unittest.TestCase):
|
|
|
|
def _get_command(self, data):
|
|
"""Get a command from the given data.
|
|
|
|
If a command was obtained, then a cleanup will be added in order
|
|
to remove the underlying target path of the command.
|
|
"""
|
|
command = userdatautils._get_command(data)
|
|
if command:
|
|
self.addCleanup(_safe_remove, command._target_path)
|
|
return command
|
|
|
|
def test__get_command(self, _):
|
|
command = self._get_command(b'rem cmd test')
|
|
self.assertIsInstance(command, executil.Shell)
|
|
|
|
command = self._get_command(b'#!/usr/bin/env python\ntest')
|
|
self.assertIsInstance(command, executil.Python)
|
|
|
|
command = self._get_command(b'#!/bin/bash')
|
|
self.assertIsInstance(command, executil.Bash)
|
|
|
|
command = self._get_command(b'#ps1_sysnative\n')
|
|
self.assertIsInstance(command, executil.PowershellSysnative)
|
|
|
|
command = self._get_command(b'#ps1_x86\n')
|
|
self.assertIsInstance(command, executil.Powershell)
|
|
|
|
command = self._get_command(b'unknown')
|
|
self.assertIsNone(command)
|
|
|
|
def test_execute_user_data_script_no_commands(self, _):
|
|
retval = userdatautils.execute_user_data_script(b"unknown")
|
|
self.assertEqual(0, retval)
|
|
|
|
@mock.patch('cloudbaseinit.plugins.windows.userdatautils.'
|
|
'_get_command')
|
|
def test_execute_user_data_script_fails(self, mock_get_command, _):
|
|
mock_get_command.return_value.side_effect = ValueError
|
|
retval = userdatautils.execute_user_data_script(
|
|
mock.sentinel.user_data)
|
|
|
|
self.assertEqual(0, retval)
|
|
|
|
@mock.patch('cloudbaseinit.plugins.windows.userdatautils.'
|
|
'_get_command')
|
|
def test_execute_user_data_script(self, mock_get_command, _):
|
|
mock_get_command.return_value.return_value = (
|
|
mock.sentinel.output, mock.sentinel.error, -1
|
|
)
|
|
retval = userdatautils.execute_user_data_script(
|
|
mock.sentinel.user_data)
|
|
self.assertEqual(-1, retval)
|