Merge "Remove logic for cloud-init < 0.6.0"
This commit is contained in:
commit
24c5a14114
@ -11,61 +11,19 @@
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
"true" '''\'
|
||||
# NOTE(vgridnev): ubuntu trusty by default has python3,
|
||||
# but pkg_resources can't be imported.
|
||||
echo "import pkg_resources" | python3 2>/dev/null
|
||||
has_py3=$?
|
||||
echo "import pkg_resources" | python2 2>/dev/null
|
||||
has_py2=$?
|
||||
echo "import pkg_resources" | /usr/libexec/platform-python 2>/dev/null
|
||||
has_platform-py=$?
|
||||
|
||||
if [ $has_py3 = 0 ]; then
|
||||
interpreter="python3"
|
||||
elif [ $has_py2 = 0 ]; then
|
||||
interpreter="python"
|
||||
elif [ $has_platform-py = 0 ]; then
|
||||
interpreter="/usr/libexec/platform-python"
|
||||
else
|
||||
interpreter="python"
|
||||
fi
|
||||
exec $interpreter "$0"
|
||||
'''
|
||||
|
||||
import datetime
|
||||
import errno
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from packaging import version
|
||||
import pkg_resources
|
||||
|
||||
|
||||
VAR_PATH = '/var/lib/heat-cfntools'
|
||||
LOG = logging.getLogger('heat-provision')
|
||||
|
||||
|
||||
def chk_ci_version():
|
||||
try:
|
||||
v = version.Version(
|
||||
pkg_resources.get_distribution('cloud-init').version)
|
||||
return v >= version.Version('0.6.0')
|
||||
except Exception:
|
||||
pass
|
||||
data = subprocess.Popen(['cloud-init', '--version'],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE).communicate()
|
||||
if data[0]:
|
||||
raise Exception()
|
||||
# data[1] has such format: 'cloud-init 0.7.5\n', need to parse version
|
||||
v = re.split(' |\n', data[1])[1].split('.')
|
||||
return tuple(v) >= tuple(['0', '6', '0'])
|
||||
|
||||
|
||||
def init_logging():
|
||||
LOG.setLevel(logging.INFO)
|
||||
LOG.addHandler(logging.StreamHandler())
|
||||
@ -106,17 +64,6 @@ def call(args):
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
try:
|
||||
if not chk_ci_version():
|
||||
# pre 0.6.0 - user data executed via cloudinit, not this helper
|
||||
LOG.error('Unable to log provisioning, need a newer version of '
|
||||
'cloud-init')
|
||||
return -1
|
||||
except Exception:
|
||||
LOG.warning('Can not determine the version of cloud-init. It is '
|
||||
'possible to get errors while logging provisioning.')
|
||||
|
||||
userdata_path = os.path.join(VAR_PATH, 'cfn-userdata')
|
||||
os.chmod(userdata_path, int("700", 8))
|
||||
|
||||
|
@ -21,68 +21,8 @@ from heat.cloudinit import loguserdata
|
||||
from heat.tests import common
|
||||
|
||||
|
||||
class FakeCiVersion(object):
|
||||
def __init__(self, version):
|
||||
self.version = version
|
||||
|
||||
|
||||
class LoguserdataTest(common.HeatTestCase):
|
||||
|
||||
@mock.patch('pkg_resources.get_distribution')
|
||||
def test_ci_version_with_pkg_resources(self, mock_get):
|
||||
# Setup
|
||||
returned_versions = [
|
||||
FakeCiVersion('0.5.0'),
|
||||
FakeCiVersion('0.5.9'),
|
||||
FakeCiVersion('0.6.0'),
|
||||
FakeCiVersion('0.7.0'),
|
||||
FakeCiVersion('1.0'),
|
||||
FakeCiVersion('2.0'),
|
||||
]
|
||||
mock_get.side_effect = returned_versions
|
||||
|
||||
# Test & Verify
|
||||
self.assertFalse(loguserdata.chk_ci_version())
|
||||
self.assertFalse(loguserdata.chk_ci_version())
|
||||
self.assertTrue(loguserdata.chk_ci_version())
|
||||
self.assertTrue(loguserdata.chk_ci_version())
|
||||
self.assertTrue(loguserdata.chk_ci_version())
|
||||
self.assertTrue(loguserdata.chk_ci_version())
|
||||
self.assertEqual(6, mock_get.call_count)
|
||||
|
||||
@mock.patch('pkg_resources.get_distribution')
|
||||
@mock.patch('subprocess.Popen')
|
||||
def test_ci_version_with_subprocess(self, mock_popen,
|
||||
mock_get_distribution):
|
||||
# Setup
|
||||
mock_get_distribution.side_effect = Exception()
|
||||
|
||||
popen_return = [
|
||||
[None, 'cloud-init 0.0.5\n'],
|
||||
[None, 'cloud-init 0.7.5\n'],
|
||||
]
|
||||
mock_popen.return_value = mock.MagicMock()
|
||||
mock_popen.return_value.communicate.side_effect = popen_return
|
||||
|
||||
# Test & Verify
|
||||
self.assertFalse(loguserdata.chk_ci_version())
|
||||
self.assertTrue(loguserdata.chk_ci_version())
|
||||
self.assertEqual(2, mock_get_distribution.call_count)
|
||||
|
||||
@mock.patch('pkg_resources.get_distribution')
|
||||
@mock.patch('subprocess.Popen')
|
||||
def test_ci_version_with_subprocess_exception(self, mock_popen,
|
||||
mock_get_distribution):
|
||||
# Setup
|
||||
mock_get_distribution.side_effect = Exception()
|
||||
mock_popen.return_value = mock.MagicMock()
|
||||
mock_popen.return_value.communicate.return_value = ['non-empty',
|
||||
'irrelevant']
|
||||
|
||||
# Test
|
||||
self.assertRaises(Exception, loguserdata.chk_ci_version) # noqa
|
||||
self.assertEqual(1, mock_get_distribution.call_count)
|
||||
|
||||
@mock.patch('subprocess.Popen')
|
||||
def test_call(self, mock_popen):
|
||||
# Setup
|
||||
@ -141,12 +81,10 @@ class LoguserdataTest(common.HeatTestCase):
|
||||
# Verify
|
||||
self.assertEqual(os.EX_SOFTWARE, return_code)
|
||||
|
||||
@mock.patch('pkg_resources.get_distribution')
|
||||
@mock.patch('os.chmod')
|
||||
@mock.patch('heat.cloudinit.loguserdata.call')
|
||||
def test_main(self, mock_call, mock_chmod, mock_get):
|
||||
def test_main(self, mock_call, mock_chmod):
|
||||
# Setup
|
||||
mock_get.return_value = FakeCiVersion('1.0')
|
||||
mock_call.return_value = 10
|
||||
|
||||
# Test
|
||||
@ -156,14 +94,3 @@ class LoguserdataTest(common.HeatTestCase):
|
||||
expected_path = os.path.join(loguserdata.VAR_PATH, 'cfn-userdata')
|
||||
mock_chmod.assert_called_once_with(expected_path, int('700', 8))
|
||||
self.assertEqual(10, return_code)
|
||||
|
||||
@mock.patch('pkg_resources.get_distribution')
|
||||
def test_main_failed_ci_version(self, mock_get):
|
||||
# Setup
|
||||
mock_get.return_value = FakeCiVersion('0.0.0')
|
||||
|
||||
# Test
|
||||
return_code = loguserdata.main()
|
||||
|
||||
# Verify
|
||||
self.assertEqual(-1, return_code)
|
||||
|
Loading…
Reference in New Issue
Block a user