Dennis Dmitriev | f3fbcd9 | 2017-11-22 18:53:49 +0200 | [diff] [blame] | 1 | import pytest |
| 2 | import mock |
| 3 | import os |
| 4 | import shutil |
| 5 | import tempfile |
| 6 | import yaml |
| 7 | |
| 8 | from reclass_tools import render |
| 9 | |
| 10 | |
Dennis Dmitriev | f3fbcd9 | 2017-11-22 18:53:49 +0200 | [diff] [blame] | 11 | def 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 | |
| 21 | def 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 | } |
obutenko | 1cf1150 | 2018-11-29 13:28:17 +0200 | [diff] [blame] | 42 | }, |
| 43 | 'default_context': { |
| 44 | 'manila_enabled': 'False', |
| 45 | 'barbican_enabled': 'False', |
| 46 | }, |
Dennis Dmitriev | f3fbcd9 | 2017-11-22 18:53:49 +0200 | [diff] [blame] | 47 | } |
| 48 | yield node, "test_env-interface-" + interface_role |
| 49 | |
| 50 | for role_file in find_yaml_paths('./{# roles #}', exts=['']): |
| 51 | if 'readme.txt' in role_file: |
| 52 | continue |
| 53 | node_role = os.path.basename(role_file) |
| 54 | node = { |
| 55 | 'nodes': { |
| 56 | 'test_node': { |
| 57 | 'reclass_storage_name': 'test_node_01', |
| 58 | 'roles': [ |
| 59 | node_role, |
| 60 | ], |
| 61 | 'interfaces': {} |
| 62 | } |
obutenko | 1cf1150 | 2018-11-29 13:28:17 +0200 | [diff] [blame] | 63 | }, |
| 64 | 'default_context': { |
| 65 | 'manila_enabled': 'False', |
| 66 | 'barbican_enabled': 'False', |
| 67 | }, |
Dennis Dmitriev | f3fbcd9 | 2017-11-22 18:53:49 +0200 | [diff] [blame] | 68 | } |
| 69 | yield node, "test_env-role-" + node_role |
| 70 | |
| 71 | |
| 72 | @pytest.mark.parametrize("environment_context, env_name", generate_context()) |
| 73 | @mock.patch('reclass_tools.helpers.yaml_read') |
| 74 | def test_mkdir(mocked_yaml_read, environment_context, env_name): |
| 75 | def mocked_yaml_read_returner(yaml_path): |
| 76 | return environment_context |
| 77 | mocked_yaml_read.side_effect = mocked_yaml_read_returner |
| 78 | |
| 79 | tmp_dir = tempfile.mkdtemp() |
| 80 | try: |
| 81 | render.render_dir('.', tmp_dir, ['1.yaml'], env_name=env_name) |
| 82 | |
| 83 | for yaml_file in find_yaml_paths(tmp_dir): |
| 84 | try: |
| 85 | with open(yaml_file, 'r') as f: |
| 86 | yaml.load(f) |
| 87 | except yaml.error.YAMLError as e: |
| 88 | with open(yaml_file, 'r') as f: |
| 89 | e.note = ("\n" + "".join( |
| 90 | ["{0:5}: {1}".format(num + 1, line) |
| 91 | for num, line in enumerate(f.readlines())])) |
| 92 | raise |
| 93 | finally: |
| 94 | shutil.rmtree(tmp_dir) |