2017-05-23 01:21:02 +00:00
|
|
|
# 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
|
|
|
|
|
2017-05-26 00:32:59 +00:00
|
|
|
from diskimage_builder.block_device.config import config_tree_to_graph
|
|
|
|
from diskimage_builder.block_device.config import create_graph
|
2017-05-17 05:54:02 +00:00
|
|
|
from diskimage_builder.block_device.exception import \
|
|
|
|
BlockDeviceSetupException
|
2017-05-30 02:06:41 +00:00
|
|
|
from diskimage_builder.block_device.tests.test_base import TestBase
|
2017-05-23 01:21:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2017-05-30 02:06:41 +00:00
|
|
|
class TestConfig(TestBase):
|
2017-05-23 01:21:02 +00:00
|
|
|
"""Helper for setting up and reading a config"""
|
|
|
|
def setUp(self):
|
|
|
|
super(TestConfig, self).setUp()
|
2017-06-01 05:53:23 +00:00
|
|
|
# previously we mocked some globals here ...
|
2017-05-23 01:21:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestGraphGeneration(TestConfig):
|
|
|
|
"""Extra helper class for testing graph generation"""
|
|
|
|
def setUp(self):
|
|
|
|
super(TestGraphGeneration, self).setUp()
|
|
|
|
|
|
|
|
self.fake_default_config = {
|
|
|
|
'build-dir': '/fake',
|
|
|
|
'image-size': '1000',
|
|
|
|
'image-dir': '/fake',
|
|
|
|
'mount-base': '/fake',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-17 05:54:02 +00:00
|
|
|
class TestConfigParsing(TestConfig):
|
2017-05-23 01:21:02 +00:00
|
|
|
"""Test parsing config file into a graph"""
|
|
|
|
|
2017-05-23 23:57:32 +00:00
|
|
|
# test an entry in the config not being a valid plugin
|
2017-05-23 01:21:02 +00:00
|
|
|
def test_config_bad_plugin(self):
|
2017-05-17 05:54:02 +00:00
|
|
|
config = self.load_config_file('bad_plugin.yaml')
|
|
|
|
self.assertRaises(BlockDeviceSetupException,
|
|
|
|
config_tree_to_graph,
|
|
|
|
config)
|
2017-05-23 01:21:02 +00:00
|
|
|
|
2017-05-23 23:57:32 +00:00
|
|
|
# test a config that has multiple keys for a top-level entry
|
|
|
|
def test_config_multikey_node(self):
|
|
|
|
config = self.load_config_file('multi_key_node.yaml')
|
2017-06-03 05:57:24 +00:00
|
|
|
self.assertRaisesRegex(BlockDeviceSetupException,
|
|
|
|
"Config entry top-level should be a single "
|
|
|
|
"dict:",
|
|
|
|
config_tree_to_graph,
|
|
|
|
config)
|
2017-05-23 23:57:32 +00:00
|
|
|
|
2017-05-23 01:21:02 +00:00
|
|
|
# a graph should remain the same
|
|
|
|
def test_graph(self):
|
|
|
|
graph = self.load_config_file('simple_graph.yaml')
|
2017-05-17 05:54:02 +00:00
|
|
|
parsed_graph = config_tree_to_graph(graph)
|
2020-07-07 03:11:28 +00:00
|
|
|
self.assertCountEqual(parsed_graph, graph)
|
2017-05-23 01:21:02 +00:00
|
|
|
|
|
|
|
# equivalence of simple tree to graph
|
|
|
|
def test_simple_tree(self):
|
|
|
|
tree = self.load_config_file('simple_tree.yaml')
|
|
|
|
graph = self.load_config_file('simple_graph.yaml')
|
2017-05-17 05:54:02 +00:00
|
|
|
parsed_graph = config_tree_to_graph(tree)
|
2020-07-07 03:11:28 +00:00
|
|
|
self.assertCountEqual(parsed_graph, graph)
|
2017-05-23 01:21:02 +00:00
|
|
|
|
|
|
|
# equivalence of a deeper tree to graph
|
|
|
|
def test_deep_tree(self):
|
|
|
|
tree = self.load_config_file('deep_tree.yaml')
|
|
|
|
graph = self.load_config_file('deep_graph.yaml')
|
2017-05-17 05:54:02 +00:00
|
|
|
parsed_graph = config_tree_to_graph(tree)
|
2020-07-07 03:11:28 +00:00
|
|
|
self.assertCountEqual(parsed_graph, graph)
|
2017-05-23 01:21:02 +00:00
|
|
|
|
|
|
|
# equivalence of a complicated multi-partition tree to graph
|
|
|
|
def test_multipart_tree(self):
|
|
|
|
tree = self.load_config_file('multiple_partitions_tree.yaml')
|
|
|
|
graph = self.load_config_file('multiple_partitions_graph.yaml')
|
2017-05-17 05:54:02 +00:00
|
|
|
parsed_graph = config_tree_to_graph(tree)
|
2017-05-23 01:21:02 +00:00
|
|
|
logger.debug(parsed_graph)
|
2020-07-07 03:11:28 +00:00
|
|
|
self.assertCountEqual(parsed_graph, graph)
|
2017-05-23 01:21:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestCreateGraph(TestGraphGeneration):
|
|
|
|
|
2017-05-23 23:57:32 +00:00
|
|
|
# Test a graph with bad edge pointing to an invalid node
|
|
|
|
def test_invalid_missing(self):
|
|
|
|
config = self.load_config_file('bad_edge_graph.yaml')
|
2017-06-03 05:57:24 +00:00
|
|
|
self.assertRaisesRegex(BlockDeviceSetupException,
|
|
|
|
"Edge not defined: this_is_not_a_node",
|
|
|
|
create_graph,
|
2017-06-01 04:31:49 +00:00
|
|
|
config, self.fake_default_config, {})
|
2017-05-23 23:57:32 +00:00
|
|
|
|
|
|
|
# Test a graph with bad edge pointing to an invalid node
|
|
|
|
def test_duplicate_name(self):
|
|
|
|
config = self.load_config_file('duplicate_name.yaml')
|
2017-06-03 05:57:24 +00:00
|
|
|
self.assertRaisesRegex(BlockDeviceSetupException,
|
|
|
|
"Duplicate node name: "
|
|
|
|
"this_is_a_duplicate",
|
|
|
|
create_graph,
|
2017-06-01 04:31:49 +00:00
|
|
|
config, self.fake_default_config, {})
|
2017-05-23 23:57:32 +00:00
|
|
|
|
2017-05-23 01:21:02 +00:00
|
|
|
# Test digraph generation from deep_graph config file
|
|
|
|
def test_deep_graph_generator(self):
|
|
|
|
config = self.load_config_file('deep_graph.yaml')
|
|
|
|
|
2017-06-01 04:31:49 +00:00
|
|
|
graph, call_order = create_graph(config, self.fake_default_config, {})
|
2017-05-23 01:21:02 +00:00
|
|
|
|
|
|
|
call_order_list = [n.name for n in call_order]
|
|
|
|
|
|
|
|
# manually created from deep_graph.yaml
|
|
|
|
# Note unlike below, the sort here is stable because the graph
|
|
|
|
# doesn't have multiple paths with only one partition
|
|
|
|
call_order_names = ['image0', 'root', 'mkfs_root',
|
|
|
|
'mount_mkfs_root',
|
|
|
|
'fstab_mount_mkfs_root']
|
|
|
|
|
|
|
|
self.assertListEqual(call_order_list, call_order_names)
|
|
|
|
|
2017-05-31 04:16:17 +00:00
|
|
|
# Test multiple partition digraph generation
|
2017-05-23 01:21:02 +00:00
|
|
|
def test_multiple_partitions_graph_generator(self):
|
|
|
|
config = self.load_config_file('multiple_partitions_graph.yaml')
|
|
|
|
|
2017-06-01 04:31:49 +00:00
|
|
|
graph, call_order = create_graph(config, self.fake_default_config, {})
|
2017-05-23 01:21:02 +00:00
|
|
|
call_order_list = [n.name for n in call_order]
|
|
|
|
|
|
|
|
# The sort creating call_order_list is unstable.
|
|
|
|
|
|
|
|
# We want to ensure we see the "partitions" object in
|
|
|
|
# root->var->var_log order
|
|
|
|
root_pos = call_order_list.index('root')
|
|
|
|
var_pos = call_order_list.index('var')
|
|
|
|
var_log_pos = call_order_list.index('var_log')
|
|
|
|
self.assertGreater(var_pos, root_pos)
|
|
|
|
self.assertGreater(var_log_pos, var_pos)
|
|
|
|
|
|
|
|
# Ensure mkfs happens after partition
|
|
|
|
mkfs_root_pos = call_order_list.index('mkfs_root')
|
|
|
|
self.assertLess(root_pos, mkfs_root_pos)
|
|
|
|
mkfs_var_pos = call_order_list.index('mkfs_var')
|
|
|
|
self.assertLess(var_pos, mkfs_var_pos)
|
|
|
|
mkfs_var_log_pos = call_order_list.index('mkfs_var_log')
|
|
|
|
self.assertLess(var_log_pos, mkfs_var_log_pos)
|