2013-01-22 01:35:31 +00:00
|
|
|
# Copyright 2013 Hewlett-Packard Development Company, L.P.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2016-06-10 04:04:26 +00:00
|
|
|
import logging
|
2013-01-22 01:35:31 +00:00
|
|
|
import os
|
|
|
|
|
2013-07-27 02:32:01 +00:00
|
|
|
import fixtures
|
|
|
|
import testtools
|
2013-01-22 01:35:31 +00:00
|
|
|
|
2013-11-12 14:58:00 +00:00
|
|
|
from diskimage_builder import element_dependencies
|
2013-01-22 01:35:31 +00:00
|
|
|
|
2016-12-05 19:55:38 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2013-01-24 17:42:57 +00:00
|
|
|
data_dir = os.path.abspath(
|
|
|
|
os.path.join(os.path.dirname(__file__), 'test-elements'))
|
|
|
|
|
2013-01-22 01:35:31 +00:00
|
|
|
|
2014-03-10 13:46:44 +00:00
|
|
|
def _populate_element(element_dir, element_name, element_deps=[], provides=[]):
|
2017-05-28 05:19:05 +00:00
|
|
|
logger.debug("Populate %s <%s>", element_name, element_dir)
|
2017-05-29 06:05:35 +00:00
|
|
|
element_home = os.path.join(element_dir, element_name)
|
|
|
|
os.mkdir(element_home)
|
|
|
|
deps_path = os.path.join(element_home, 'element-deps')
|
|
|
|
|
|
|
|
with open(deps_path, 'w') as deps_file:
|
|
|
|
deps_file.write("\n".join(element_deps))
|
2014-03-10 13:46:44 +00:00
|
|
|
provides_path = os.path.join(element_home, 'element-provides')
|
2017-05-29 06:05:35 +00:00
|
|
|
|
|
|
|
with open(provides_path, 'w') as provides_file:
|
|
|
|
provides_file.write("\n".join(provides))
|
2013-01-22 01:35:31 +00:00
|
|
|
|
2013-01-24 17:42:57 +00:00
|
|
|
|
2013-07-27 02:32:01 +00:00
|
|
|
class TestElementDeps(testtools.TestCase):
|
2013-01-22 01:35:31 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(TestElementDeps, self).setUp()
|
2016-06-28 05:41:50 +00:00
|
|
|
self.element_root_dir = self.useFixture(fixtures.TempDir()).path
|
|
|
|
|
|
|
|
self.element_dir = os.path.join(self.element_root_dir, 'elements')
|
|
|
|
self.element_override_dir = os.path.join(self.element_root_dir,
|
|
|
|
'element-override')
|
|
|
|
os.mkdir(self.element_dir)
|
|
|
|
os.mkdir(self.element_override_dir)
|
|
|
|
|
2016-06-10 04:04:26 +00:00
|
|
|
self.log_fixture = self.useFixture(
|
|
|
|
fixtures.FakeLogger(level=logging.DEBUG))
|
2013-01-22 01:35:31 +00:00
|
|
|
_populate_element(self.element_dir, 'requires-foo', ['foo'])
|
2014-04-30 15:59:49 +00:00
|
|
|
_populate_element(self.element_dir,
|
|
|
|
'foo',
|
|
|
|
[],
|
|
|
|
['operating-system'])
|
2013-01-22 01:35:31 +00:00
|
|
|
_populate_element(self.element_dir,
|
|
|
|
'requires-requires-foo',
|
|
|
|
['requires-foo'])
|
|
|
|
_populate_element(self.element_dir, 'self', ['self'])
|
2014-03-10 13:46:44 +00:00
|
|
|
_populate_element(self.element_dir,
|
|
|
|
'provides_virtual',
|
|
|
|
[],
|
|
|
|
['virtual'])
|
2018-01-31 02:16:57 +00:00
|
|
|
_populate_element(self.element_dir,
|
|
|
|
'also_provides_virtual',
|
|
|
|
[],
|
|
|
|
['virtual'])
|
2014-04-30 15:59:49 +00:00
|
|
|
_populate_element(self.element_dir,
|
|
|
|
'requires_virtual',
|
|
|
|
['virtual'],
|
|
|
|
['operating-system'])
|
2014-04-07 16:59:33 +00:00
|
|
|
_populate_element(self.element_dir, 'virtual', ['extra_dependency'])
|
|
|
|
_populate_element(self.element_dir, 'extra_dependency', [])
|
2014-04-30 15:59:49 +00:00
|
|
|
_populate_element(self.element_dir,
|
|
|
|
'circular1',
|
|
|
|
['circular2'],
|
|
|
|
['operating-system'])
|
2013-01-22 01:35:31 +00:00
|
|
|
_populate_element(self.element_dir, 'circular2', ['circular1'])
|
2014-04-07 16:59:33 +00:00
|
|
|
_populate_element(self.element_dir,
|
|
|
|
'provides_new_virtual',
|
|
|
|
[],
|
2014-04-30 15:59:49 +00:00
|
|
|
['new_virtual', 'operating-system'])
|
2014-04-07 16:59:33 +00:00
|
|
|
_populate_element(self.element_dir,
|
|
|
|
'requires_new_virtual',
|
|
|
|
['new_virtual'])
|
2013-01-22 01:35:31 +00:00
|
|
|
|
2016-06-28 05:41:50 +00:00
|
|
|
# second element should override the first one here
|
|
|
|
_populate_element(self.element_dir, 'override_element', [])
|
|
|
|
_populate_element(self.element_override_dir, 'override_element', [])
|
|
|
|
|
2016-12-05 19:55:38 +00:00
|
|
|
# This simulates $ELEMENTS_PATH
|
|
|
|
self.element_dirs = "%s:%s" % (self.element_override_dir,
|
|
|
|
self.element_dir)
|
|
|
|
|
|
|
|
# helper to return an (element, path) tuple from the standard dir
|
|
|
|
def _e(self, element):
|
2017-05-29 06:05:35 +00:00
|
|
|
return (element, os.path.join(self.element_dir, element))
|
2016-12-05 19:55:38 +00:00
|
|
|
|
|
|
|
# helper to return an (element, path) tuple from the override dir
|
|
|
|
def _eo(self, element):
|
2017-05-29 06:05:35 +00:00
|
|
|
return (element, os.path.join(self.element_override_dir, element))
|
2016-06-28 05:41:50 +00:00
|
|
|
|
2013-01-22 01:35:31 +00:00
|
|
|
def test_non_transitive_deps(self):
|
2016-12-05 19:55:38 +00:00
|
|
|
result = element_dependencies.get_elements(['requires-foo'],
|
|
|
|
self.element_dirs)
|
2020-07-07 03:11:28 +00:00
|
|
|
self.assertCountEqual([self._e('foo'), self._e('requires-foo')],
|
2016-12-05 19:55:38 +00:00
|
|
|
result)
|
2013-01-22 01:35:31 +00:00
|
|
|
|
2013-06-19 20:23:27 +00:00
|
|
|
def test_missing_deps(self):
|
2016-12-05 19:55:38 +00:00
|
|
|
e = self.assertRaises(element_dependencies.MissingElementException,
|
|
|
|
element_dependencies.get_elements,
|
|
|
|
['fake'],
|
|
|
|
self.element_dirs)
|
|
|
|
self.assertIn("Element 'fake' not found", str(e))
|
|
|
|
|
|
|
|
def test_invalid_element_dir(self):
|
|
|
|
e = self.assertRaises(element_dependencies.InvalidElementDir,
|
|
|
|
element_dependencies.get_elements,
|
|
|
|
['fake'],
|
|
|
|
self.element_dirs + ":/not/a/dir")
|
|
|
|
self.assertIn("ELEMENTS_PATH entry '/not/a/dir' is not a directory",
|
|
|
|
str(e))
|
2013-06-19 20:23:27 +00:00
|
|
|
|
2013-01-22 01:35:31 +00:00
|
|
|
def test_transitive_deps(self):
|
2016-12-05 19:55:38 +00:00
|
|
|
result = element_dependencies.get_elements(
|
|
|
|
['requires-requires-foo'], self.element_dirs)
|
|
|
|
|
2020-07-07 03:11:28 +00:00
|
|
|
self.assertCountEqual([self._e('requires-requires-foo'),
|
2016-12-05 19:55:38 +00:00
|
|
|
self._e('requires-foo'),
|
|
|
|
self._e('foo')], result)
|
2013-01-22 01:35:31 +00:00
|
|
|
|
|
|
|
def test_no_deps(self):
|
2016-12-05 19:55:38 +00:00
|
|
|
result = element_dependencies.get_elements(['foo'], self.element_dirs)
|
|
|
|
self.assertEqual([self._e('foo')], result)
|
2013-01-22 01:35:31 +00:00
|
|
|
|
|
|
|
def test_self(self):
|
2016-12-05 19:55:38 +00:00
|
|
|
result = element_dependencies.get_elements(['self', 'foo'],
|
|
|
|
self.element_dirs)
|
2020-07-07 03:11:28 +00:00
|
|
|
self.assertCountEqual([self._e('self'),
|
2016-12-05 19:55:38 +00:00
|
|
|
self._e('foo')], result)
|
2013-01-22 01:35:31 +00:00
|
|
|
|
|
|
|
def test_circular(self):
|
2016-12-05 19:55:38 +00:00
|
|
|
result = element_dependencies.get_elements(['circular1'],
|
|
|
|
self.element_dirs)
|
2020-07-07 03:11:28 +00:00
|
|
|
self.assertCountEqual([self._e('circular1'),
|
2016-12-05 19:55:38 +00:00
|
|
|
self._e('circular2')], result)
|
2013-02-05 17:53:33 +00:00
|
|
|
|
2014-03-10 13:46:44 +00:00
|
|
|
def test_provide(self):
|
2016-12-05 19:55:38 +00:00
|
|
|
result = element_dependencies.get_elements(
|
|
|
|
['provides_virtual', 'requires_virtual'],
|
|
|
|
self.element_dirs)
|
2020-07-07 03:11:28 +00:00
|
|
|
self.assertCountEqual([self._e('requires_virtual'),
|
2016-12-05 19:55:38 +00:00
|
|
|
self._e('provides_virtual')], result)
|
2014-03-10 13:46:44 +00:00
|
|
|
|
|
|
|
def test_provide_conflict(self):
|
2016-12-05 19:55:38 +00:00
|
|
|
self.assertRaises(element_dependencies.AlreadyProvidedException,
|
|
|
|
element_dependencies.get_elements,
|
|
|
|
['virtual', 'provides_virtual'],
|
|
|
|
self.element_dirs)
|
2014-03-10 13:46:44 +00:00
|
|
|
|
2014-04-07 16:59:33 +00:00
|
|
|
def test_provide_virtual_ordering(self):
|
2016-12-05 19:55:38 +00:00
|
|
|
result = element_dependencies.get_elements(
|
|
|
|
['requires_new_virtual', 'provides_new_virtual'],
|
|
|
|
self.element_dirs)
|
2020-07-07 03:11:28 +00:00
|
|
|
self.assertCountEqual(
|
2016-12-05 19:55:38 +00:00
|
|
|
[self._e('requires_new_virtual'),
|
|
|
|
self._e('provides_new_virtual')], result)
|
2014-04-07 16:59:33 +00:00
|
|
|
|
2018-01-31 02:16:57 +00:00
|
|
|
def test_elements_provide_same(self):
|
2020-02-22 23:25:38 +00:00
|
|
|
msg = r"virtual: already provided by \['provides_virtual'\]"
|
2018-08-27 14:39:59 +00:00
|
|
|
self.assertRaisesRegex(element_dependencies.AlreadyProvidedException,
|
|
|
|
msg,
|
|
|
|
element_dependencies.get_elements,
|
|
|
|
['provides_virtual', 'also_provides_virtual'],
|
|
|
|
self.element_dirs)
|
2018-01-31 02:16:57 +00:00
|
|
|
|
2014-04-30 15:59:49 +00:00
|
|
|
def test_no_os_element(self):
|
2016-12-05 19:55:38 +00:00
|
|
|
self.assertRaises(element_dependencies.MissingOSException,
|
|
|
|
element_dependencies.get_elements,
|
2014-04-30 15:59:49 +00:00
|
|
|
['provides_virtual'],
|
2016-12-05 19:55:38 +00:00
|
|
|
self.element_dirs)
|
2014-04-30 15:59:49 +00:00
|
|
|
|
|
|
|
def test_duplicated_os_passed_as_element(self):
|
2016-12-05 19:55:38 +00:00
|
|
|
self.assertRaises(
|
|
|
|
element_dependencies.AlreadyProvidedException,
|
|
|
|
element_dependencies.get_elements,
|
|
|
|
['circular1', 'operating-system'],
|
|
|
|
self.element_dirs)
|
2016-03-10 03:52:02 +00:00
|
|
|
# ensure we get the error message about what's providing the
|
|
|
|
# conflicting package
|
|
|
|
self.assertIn("operating-system : already provided by ['circular1']",
|
2016-06-10 04:04:26 +00:00
|
|
|
self.log_fixture.output)
|
2014-04-30 15:59:49 +00:00
|
|
|
|
2016-06-28 05:41:50 +00:00
|
|
|
def test_element_override(self):
|
|
|
|
# make sure we picked up "override_element" from the override dir,
|
|
|
|
# not the base dir
|
2016-12-05 19:55:38 +00:00
|
|
|
result = element_dependencies.get_elements(['override_element', 'foo'],
|
|
|
|
self.element_dirs)
|
2020-07-07 03:11:28 +00:00
|
|
|
self.assertCountEqual([self._e('foo'),
|
2016-12-05 19:55:38 +00:00
|
|
|
self._eo('override_element')],
|
|
|
|
result)
|
|
|
|
|
|
|
|
def test_expand_dependencies_deprecated(self):
|
|
|
|
# test the deprecated expand_dependencies call
|
|
|
|
result = element_dependencies.expand_dependencies(
|
|
|
|
['foo', 'requires-foo'], self.element_dirs)
|
2020-07-07 03:11:28 +00:00
|
|
|
self.assertCountEqual(['foo', 'requires-foo'], result)
|
2016-12-05 19:55:38 +00:00
|
|
|
|
|
|
|
def test_output_sanity(self):
|
|
|
|
# very basic output sanity test
|
|
|
|
elements = element_dependencies._get_elements(['foo', 'requires-foo'],
|
|
|
|
self.element_dirs)
|
|
|
|
element_dependencies._output_env_vars(elements)
|
2016-06-28 05:41:50 +00:00
|
|
|
|
2013-02-05 17:53:33 +00:00
|
|
|
|
2013-07-27 02:32:01 +00:00
|
|
|
class TestElements(testtools.TestCase):
|
2013-02-05 17:53:33 +00:00
|
|
|
def test_depends_on_env(self):
|
2013-07-27 02:32:01 +00:00
|
|
|
self.useFixture(
|
|
|
|
fixtures.EnvironmentVariable('ELEMENTS_PATH', '/foo/bar'))
|
2016-12-05 19:55:38 +00:00
|
|
|
self.assertEqual('/foo/bar',
|
|
|
|
element_dependencies._get_elements_dir())
|
2013-02-05 17:53:33 +00:00
|
|
|
|
|
|
|
def test_env_not_set(self):
|
2013-07-27 02:32:01 +00:00
|
|
|
self.useFixture(fixtures.EnvironmentVariable('ELEMENTS_PATH', ''))
|
2016-12-05 19:55:38 +00:00
|
|
|
self.assertRaises(Exception,
|
|
|
|
element_dependencies._get_elements_dir, ())
|