blob: 07dd5701945c2f99ee2cd48be82fa6c9735e8066 [file] [log] [blame]
Dennis Dmitrievf3fbcd92017-11-22 18:53:49 +02001import pytest
2import mock
3import os
4import shutil
5import tempfile
6import yaml
7
8from reclass_tools import render
9
10
Dennis Dmitrievf3fbcd92017-11-22 18:53:49 +020011def find_yaml_paths(tmp_dir, exts=None):
12 if exts is None:
13 exts = ['.yml', '.yaml']
14 print(tmp_dir, exts)
15 for root, subFolder, files in os.walk(tmp_dir):
16 for filename in files:
17 if any([filename.endswith(ext) for ext in exts]):
18 yield str(os.path.join(root, filename))
19
20
21def generate_context():
22 for context_file in find_yaml_paths('./inventory_examples'):
23 with open(context_file, 'r') as f:
24 yield yaml.load(f), "test_env-" + os.path.basename(context_file)
25
26 for interface_file in find_yaml_paths('./{# interfaces #}', exts=['']):
27 if 'readme.txt' in interface_file:
28 continue
29 interface_role = os.path.basename(interface_file)
30 node = {
31 'nodes': {
32 'test_node': {
33 'reclass_storage_name': 'test_node_01',
34 'interfaces': {
35 'eth1000': {
36 'role': interface_role,
37 'dpdk_pci': '0000:05:00.1',
38 'dpdk_mac': '00:11:22:33:44:55',
39 }
40 }
41 }
42 }
43 }
44 yield node, "test_env-interface-" + interface_role
45
46 for role_file in find_yaml_paths('./{# roles #}', exts=['']):
47 if 'readme.txt' in role_file:
48 continue
49 node_role = os.path.basename(role_file)
50 node = {
51 'nodes': {
52 'test_node': {
53 'reclass_storage_name': 'test_node_01',
54 'roles': [
55 node_role,
56 ],
57 'interfaces': {}
58 }
59 }
60 }
61 yield node, "test_env-role-" + node_role
62
63
64@pytest.mark.parametrize("environment_context, env_name", generate_context())
65@mock.patch('reclass_tools.helpers.yaml_read')
66def test_mkdir(mocked_yaml_read, environment_context, env_name):
67 def mocked_yaml_read_returner(yaml_path):
68 return environment_context
69 mocked_yaml_read.side_effect = mocked_yaml_read_returner
70
71 tmp_dir = tempfile.mkdtemp()
72 try:
73 render.render_dir('.', tmp_dir, ['1.yaml'], env_name=env_name)
74
75 for yaml_file in find_yaml_paths(tmp_dir):
76 try:
77 with open(yaml_file, 'r') as f:
78 yaml.load(f)
79 except yaml.error.YAMLError as e:
80 with open(yaml_file, 'r') as f:
81 e.note = ("\n" + "".join(
82 ["{0:5}: {1}".format(num + 1, line)
83 for num, line in enumerate(f.readlines())]))
84 raise
85 finally:
86 shutil.rmtree(tmp_dir)