blob: 5d7b757d62bb31a51cefd4502403a49975520500 [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
11inventory = """nodes:
12 # Physical nodes
13
14 kvm01.mcp11-ovs-dpdk.local:
15 reclass_storage_name: infra_kvm_node01
16 roles:
17 - infra_kvm
18 - linux_system_codename_xenial
19 interfaces:
20 enp3s0f0:
21 role: single_mgm
22 enp3s0f1:
23 role: bond0_ab_ovs_vlan_ctl
24
25 kvm02.mcp11-ovs-dpdk.local:
26 reclass_storage_name: infra_kvm_node02
27 roles:
28 - infra_kvm
29 - linux_system_codename_xenial
30 interfaces:
31 eno1:
32 role: single_mgm
33 eno2:
34 role: bond0_ab_ovs_vlan_ctl
35
36 kvm03.mcp11-ovs-dpdk.local:
37 reclass_storage_name: infra_kvm_node03
38 roles:
39 - infra_kvm
40 - linux_system_codename_xenial
41 interfaces:
42 eno1:
43 role: single_mgm
44 eno2:
45 role: bond0_ab_ovs_vlan_ctl
46"""
47
48
49def find_yaml_paths(tmp_dir, exts=None):
50 if exts is None:
51 exts = ['.yml', '.yaml']
52 print(tmp_dir, exts)
53 for root, subFolder, files in os.walk(tmp_dir):
54 for filename in files:
55 if any([filename.endswith(ext) for ext in exts]):
56 yield str(os.path.join(root, filename))
57
58
59def generate_context():
60 for context_file in find_yaml_paths('./inventory_examples'):
61 with open(context_file, 'r') as f:
62 yield yaml.load(f), "test_env-" + os.path.basename(context_file)
63
64 for interface_file in find_yaml_paths('./{# interfaces #}', exts=['']):
65 if 'readme.txt' in interface_file:
66 continue
67 interface_role = os.path.basename(interface_file)
68 node = {
69 'nodes': {
70 'test_node': {
71 'reclass_storage_name': 'test_node_01',
72 'interfaces': {
73 'eth1000': {
74 'role': interface_role,
75 'dpdk_pci': '0000:05:00.1',
76 'dpdk_mac': '00:11:22:33:44:55',
77 }
78 }
79 }
80 }
81 }
82 yield node, "test_env-interface-" + interface_role
83
84 for role_file in find_yaml_paths('./{# roles #}', exts=['']):
85 if 'readme.txt' in role_file:
86 continue
87 node_role = os.path.basename(role_file)
88 node = {
89 'nodes': {
90 'test_node': {
91 'reclass_storage_name': 'test_node_01',
92 'roles': [
93 node_role,
94 ],
95 'interfaces': {}
96 }
97 }
98 }
99 yield node, "test_env-role-" + node_role
100
101
102@pytest.mark.parametrize("environment_context, env_name", generate_context())
103@mock.patch('reclass_tools.helpers.yaml_read')
104def test_mkdir(mocked_yaml_read, environment_context, env_name):
105 def mocked_yaml_read_returner(yaml_path):
106 return environment_context
107 mocked_yaml_read.side_effect = mocked_yaml_read_returner
108
109 tmp_dir = tempfile.mkdtemp()
110 try:
111 render.render_dir('.', tmp_dir, ['1.yaml'], env_name=env_name)
112
113 for yaml_file in find_yaml_paths(tmp_dir):
114 try:
115 with open(yaml_file, 'r') as f:
116 yaml.load(f)
117 except yaml.error.YAMLError as e:
118 with open(yaml_file, 'r') as f:
119 e.note = ("\n" + "".join(
120 ["{0:5}: {1}".format(num + 1, line)
121 for num, line in enumerate(f.readlines())]))
122 raise
123 finally:
124 shutil.rmtree(tmp_dir)