274be6de55
This is a re-factor of element_dependencies to achieve two things -- centralising override policy and storing path names. Firstly we want to make the override policy for elements completely explicit. Currently, elements that wish to copy parts of other elements walk ELEMENTS_PATH themselves and look for elements in IMAGE_ELEMENT. How they handle duplicate elements can differ, leading to inconsistent behaviour. We introduce logic in element-info to find elements in each of the directories in ELEMENT_PATHS in *reverse* order -- that is to say, earlier entries in the paths will overwrite later ones. For example ELEMENT_PATHS=foo:bar:baz will mean that "foo/element" will override "baz/element", since "foo" is first. This should be sane to anyone familiar with $PATH. Documentation is clarified around this point and a test-case is added. The second thing is that we want to keep the complete path of the elements we have chosen. We want the aforementioned elements that walk the element list to use these canonical paths to pickup files; this way they don't need to make local decisions about element overrides, but can simply iterate a list and copy/merge files if they exist. A follow-on change (I7092e1845942f249175933d67ab121188f3511fd) will expose this data in a separate variable that can be parsed by elements (a further follow-on I0a64b45e9f2cfa28e84b2859d76b065a6c4590f0 modifies the elements to use this information). Thus this does not change the status-quo -- elements that are walking ELEMENTS_PATH themselves and can/will continue doing that. Change-Id: I2a29861c67de2d25c595cb35d850e92807d26ac6
180 lines
7.4 KiB
Python
180 lines
7.4 KiB
Python
# 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.
|
|
|
|
import logging
|
|
import os
|
|
|
|
import fixtures
|
|
import testtools
|
|
|
|
from diskimage_builder import element_dependencies
|
|
|
|
data_dir = os.path.abspath(
|
|
os.path.join(os.path.dirname(__file__), 'test-elements'))
|
|
|
|
|
|
def _populate_element(element_dir, element_name, element_deps=[], provides=[]):
|
|
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))
|
|
provides_path = os.path.join(element_home, 'element-provides')
|
|
with open(provides_path, 'w') as provides_file:
|
|
provides_file.write("\n".join(provides))
|
|
|
|
|
|
class TestElementDeps(testtools.TestCase):
|
|
|
|
def setUp(self):
|
|
super(TestElementDeps, self).setUp()
|
|
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)
|
|
|
|
self.log_fixture = self.useFixture(
|
|
fixtures.FakeLogger(level=logging.DEBUG))
|
|
_populate_element(self.element_dir, 'requires-foo', ['foo'])
|
|
_populate_element(self.element_dir,
|
|
'foo',
|
|
[],
|
|
['operating-system'])
|
|
_populate_element(self.element_dir,
|
|
'requires-requires-foo',
|
|
['requires-foo'])
|
|
_populate_element(self.element_dir, 'self', ['self'])
|
|
_populate_element(self.element_dir,
|
|
'provides_virtual',
|
|
[],
|
|
['virtual'])
|
|
_populate_element(self.element_dir,
|
|
'requires_virtual',
|
|
['virtual'],
|
|
['operating-system'])
|
|
_populate_element(self.element_dir, 'virtual', ['extra_dependency'])
|
|
_populate_element(self.element_dir, 'extra_dependency', [])
|
|
_populate_element(self.element_dir,
|
|
'circular1',
|
|
['circular2'],
|
|
['operating-system'])
|
|
_populate_element(self.element_dir, 'circular2', ['circular1'])
|
|
_populate_element(self.element_dir,
|
|
'provides_new_virtual',
|
|
[],
|
|
['new_virtual', 'operating-system'])
|
|
_populate_element(self.element_dir,
|
|
'requires_new_virtual',
|
|
['new_virtual'])
|
|
|
|
# second element should override the first one here
|
|
_populate_element(self.element_dir, 'override_element', [])
|
|
_populate_element(self.element_override_dir, 'override_element', [])
|
|
|
|
self.all_elements = element_dependencies.find_all_elements(
|
|
[self.element_dir, self.element_override_dir])
|
|
|
|
def test_non_transitive_deps(self):
|
|
result = element_dependencies.expand_dependencies(
|
|
['requires-foo'], self.all_elements)
|
|
self.assertEqual(set(['requires-foo', 'foo']), result)
|
|
|
|
def test_missing_deps(self):
|
|
self.assertRaises(SystemExit,
|
|
element_dependencies.expand_dependencies, ['fake'],
|
|
self.all_elements)
|
|
self.assertIn("Element 'fake' not found",
|
|
self.log_fixture.output)
|
|
|
|
def test_transitive_deps(self):
|
|
result = element_dependencies.expand_dependencies(
|
|
['requires-requires-foo'], self.all_elements)
|
|
self.assertEqual(set(['requires-requires-foo',
|
|
'requires-foo',
|
|
'foo']), result)
|
|
|
|
def test_no_deps(self):
|
|
result = element_dependencies.expand_dependencies(
|
|
['foo'], self.all_elements)
|
|
self.assertEqual(set(['foo']), result)
|
|
|
|
def test_self(self):
|
|
result = element_dependencies.expand_dependencies(
|
|
['self', 'foo'], self.all_elements)
|
|
self.assertEqual(set(['self', 'foo']), result)
|
|
|
|
def test_circular(self):
|
|
result = element_dependencies.expand_dependencies(
|
|
['circular1'], self.all_elements)
|
|
self.assertEqual(set(['circular1', 'circular2']), result)
|
|
|
|
def test_provide(self):
|
|
result = element_dependencies.expand_dependencies(
|
|
['provides_virtual', 'requires_virtual'],
|
|
self.all_elements)
|
|
self.assertEqual(set(['requires_virtual', 'provides_virtual']), result)
|
|
|
|
def test_provide_conflict(self):
|
|
self.assertRaises(SystemExit,
|
|
element_dependencies.expand_dependencies,
|
|
['virtual', 'provides_virtual'],
|
|
self.element_dir)
|
|
|
|
def test_provide_virtual_ordering(self):
|
|
result = element_dependencies.expand_dependencies(
|
|
['requires_new_virtual', 'provides_new_virtual'],
|
|
self.all_elements)
|
|
self.assertEqual(set(['requires_new_virtual', 'provides_new_virtual']),
|
|
result)
|
|
|
|
def test_no_os_element(self):
|
|
self.assertRaises(SystemExit,
|
|
element_dependencies.expand_dependencies,
|
|
['provides_virtual'],
|
|
self.all_elements)
|
|
self.assertIn("Please include an operating system element",
|
|
self.log_fixture.output)
|
|
|
|
def test_duplicated_os_passed_as_element(self):
|
|
self.assertRaises(SystemExit,
|
|
element_dependencies.expand_dependencies,
|
|
['circular1', 'operating-system'],
|
|
self.all_elements)
|
|
# ensure we get the error message about what's providing the
|
|
# conflicting package
|
|
self.assertIn("operating-system : already provided by ['circular1']",
|
|
self.log_fixture.output)
|
|
|
|
def test_element_override(self):
|
|
# make sure we picked up "override_element" from the override dir,
|
|
# not the base dir
|
|
self.assertTrue('override_element' in self.all_elements)
|
|
self.assertEqual(os.path.join(self.element_override_dir,
|
|
'override_element'),
|
|
self.all_elements['override_element'].path)
|
|
|
|
|
|
class TestElements(testtools.TestCase):
|
|
def test_depends_on_env(self):
|
|
self.useFixture(
|
|
fixtures.EnvironmentVariable('ELEMENTS_PATH', '/foo/bar'))
|
|
self.assertEqual('/foo/bar', element_dependencies.get_elements_dir())
|
|
|
|
def test_env_not_set(self):
|
|
self.useFixture(fixtures.EnvironmentVariable('ELEMENTS_PATH', ''))
|
|
self.assertRaises(Exception, element_dependencies.get_elements_dir, ())
|