diff --git a/cloudbaseinit/init.py b/cloudbaseinit/init.py index 67f7b08a..3ed40e1c 100644 --- a/cloudbaseinit/init.py +++ b/cloudbaseinit/init.py @@ -21,6 +21,7 @@ from cloudbaseinit.openstack.common import log as logging from cloudbaseinit.osutils import factory as osutils_factory from cloudbaseinit.plugins.common import base as plugins_base from cloudbaseinit.plugins.common import factory as plugins_factory +from cloudbaseinit import version opts = [ cfg.BoolOpt('allow_reboot', default=True, help='Allows OS reboots ' @@ -94,6 +95,8 @@ class InitManager(object): return supported def configure_host(self): + LOG.info('Cloudbase-Init version: %s', version.get_version()) + osutils = osutils_factory.get_os_utils() osutils.wait_for_boot_completion() diff --git a/cloudbaseinit/tests/test_init.py b/cloudbaseinit/tests/test_init.py index f16932a8..5b0c3371 100644 --- a/cloudbaseinit/tests/test_init.py +++ b/cloudbaseinit/tests/test_init.py @@ -23,6 +23,7 @@ from oslo.config import cfg from cloudbaseinit import init from cloudbaseinit.plugins.common import base +from cloudbaseinit.tests import testutils CONF = cfg.CONF @@ -135,6 +136,7 @@ class InitManagerTest(unittest.TestCase): def test_check_plugin_os_requirements_other_requirenments(self): self._test_check_plugin_os_requirements(('linux', (5, 2))) + @mock.patch('cloudbaseinit.version.get_version') @mock.patch('cloudbaseinit.init.InitManager' '._check_plugin_os_requirements') @mock.patch('cloudbaseinit.init.InitManager._exec_plugin') @@ -144,16 +146,28 @@ class InitManagerTest(unittest.TestCase): def test_configure_host(self, mock_get_metadata_service, mock_get_os_utils, mock_load_plugins, mock_exec_plugin, - mock_check_os_requirements): + mock_check_os_requirements, + mock_get_version): + instance_id = 'fake id' + name = 'fake name' + version = 'version' + mock_get_version.return_value = version fake_service = mock.MagicMock() fake_plugin = mock.MagicMock() mock_load_plugins.return_value = [fake_plugin] mock_get_os_utils.return_value = self.osutils mock_get_metadata_service.return_value = fake_service - fake_service.get_name.return_value = 'fake name' - fake_service.get_instance_id.return_value = 'fake id' + fake_service.get_name.return_value = name + fake_service.get_instance_id.return_value = instance_id + expected_logging = [ + 'Cloudbase-Init version: %s' % version, + 'Metadata service loaded: %r' % name, + 'Instance id: %s' % instance_id + ] - self._init.configure_host() + with testutils.LogSnatcher('cloudbaseinit.init') as snatcher: + self._init.configure_host() + self.assertEqual(expected_logging, snatcher.output) self.osutils.wait_for_boot_completion.assert_called_once_with() mock_get_metadata_service.assert_called_once_with() diff --git a/cloudbaseinit/tests/test_version.py b/cloudbaseinit/tests/test_version.py new file mode 100644 index 00000000..8a84ce67 --- /dev/null +++ b/cloudbaseinit/tests/test_version.py @@ -0,0 +1,30 @@ +# Copyright 2015 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 unittest + +import mock + +from cloudbaseinit import version + + +class TestVersion(unittest.TestCase): + + @mock.patch('pbr.version.VersionInfo') + def test_get_version(self, mock_version_info): + package_version = version.get_version() + + mock_version_info.assert_called_once_with('cloudbase-init') + release_string = mock_version_info.return_value.release_string + self.assertEqual(release_string.return_value, package_version) diff --git a/cloudbaseinit/version.py b/cloudbaseinit/version.py new file mode 100644 index 00000000..8d50be45 --- /dev/null +++ b/cloudbaseinit/version.py @@ -0,0 +1,21 @@ +# Copyright 2015 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 pbr.version + + +def get_version(): + """Obtain the project version.""" + version = pbr.version.VersionInfo('cloudbase-init') + return version.release_string()