blob: ff3a0b58b4959b3bfce514d8c0b057ae8f61717b [file] [log] [blame]
Dmitry Tyzhnenko35413c02018-03-05 14:12:37 +02001# Copyright 2018 Mirantis, Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15from collections import namedtuple
16import pytest
17
18from tcp_tests.helpers import ext
19from tcp_tests import logger
20from tcp_tests.managers import saltmanager
21from tcp_tests.managers import underlay_ssh_manager
22
23LOG = logger.logger
24
25
26@pytest.mark.revert_snapshot(ext.SNAPSHOT.day1_underlay)
27@pytest.fixture(scope="function")
28def day1_underlay(revert_snapshot, config, hardware):
29 """Fixture that should provide SSH access to underlay objects.
30
31 - Starts the 'hardware' environment and creates 'underlay' with required
32 configuration.
33 - Fills the following object using the 'hardware' fixture:
34 config.underlay.ssh = JSONList of SSH access credentials for nodes.
35 This list will be used for initialization the
36 model UnderlaySSHManager, see it for details.
37
38 :rtype UnderlaySSHManager: Object that encapsulate SSH credentials;
39 - provide list of underlay nodes;
40 - provide SSH access to underlay nodes using
41 node names or node IPs.
42 """
43 # Create Underlay
44 if not config.day1_underlay.ssh:
45 # If config.underlay.ssh wasn't provided from external config, then
46 # try to get necessary data from hardware manager (fuel-devops)
47
48 # for devops manager: power on nodes and wait for SSH
49 # for empty manager: do nothing
50 # for maas manager: provision nodes and wait for SSH
51 # hardware.start(underlay_node_roles=config.underlay.roles,
52 hardware.start(
53 underlay_node_roles=['salt_master'],
54 timeout=config.underlay.bootstrap_timeout)
55
56 config.day1_underlay.ssh = hardware.get_ssh_data(
57 roles=config.underlay.roles)
58
59 underlay = underlay_ssh_manager.UnderlaySSHManager(config)
60
61 LOG.info("Generate MACs for MaaS")
62 macs = {
63 n.name.split('.')[0]: {
64 "interface": {
65 "mac": n.get_interface_by_network_name('admin').mac_address}} # noqa
66 for n in hardware.slave_nodes}
67
68 config.day1_cfg_config.maas_machines_macs = {
69 "parameters": {
70 "maas": {
71 "region": {
72 "machines": macs}}}}
73
74 if not config.day1_underlay.lvm:
75 underlay.enable_lvm(hardware.lvm_storages())
76 config.day1_underlay.lvm = underlay.config_lvm
77
78 hardware.create_snapshot(ext.SNAPSHOT.day1_underlay)
79
80 else:
81 # 1. hardware environment created and powered on
82 # 2. config.underlay.ssh contains SSH access to provisioned nodes
83 # (can be passed from external config with TESTS_CONFIGS variable)
84 underlay = underlay_ssh_manager.UnderlaySSHManager(config)
85
86 return underlay
87
88
89@pytest.mark.revert_snapshot(ext.SNAPSHOT.cfg_configured)
90@pytest.fixture(scope='function')
91def day1_cfg_config(revert_snapshot, request, config, hardware, underlay,
92 salt_actions, snapshot, grab_versions):
93 """Fixture to get or install cfg node from day1 image on environment
94
95 :param revert_snapshot: fixture that reverts snapshot that is specified
96 in test with @pytest.mark.revert_snapshot(<name>)
97 :param request: fixture provides pytest data
98 :param config: fixture provides oslo.config
99 :param hardware: fixture provides enviromnet manager
100 :param day1_underlay: fixture provides underlay manager
101 :param salt_actions: fixture provides SaltManager instance
102 :rtype: SaltManager
103
104 If config.salt.salt_master_host is not set, this fixture assumes that
105 the salt was not installed, and do the following:
106 - install salt master and salt minions
107 - make snapshot with name 'cfg_configured'
108 - return SaltManager
109
110 If config.salt.salt_master_host was set, this fixture assumes that the
111 salt was already deployed, and do the following:
112 - return SaltManager instance
113
114 If you want to revert 'cfg_configured' snapshot, please use mark:
115 @pytest.mark.revert_snapshot("cfg_configured")
116 """
117 # Create Salt cluster
118 if config.salt.salt_master_host == '0.0.0.0':
119 # Temporary workaround. Underlay should be extended with roles
120 config.salt.salt_master_host = \
121 underlay.host_by_node_role(
122 node_role=ext.UNDERLAY_NODE_ROLES.salt_master)
123
124 commands = underlay.read_template(
125 config.day1_cfg_config.configure_steps_path)
126 LOG.info("############ Executing command ####### {0}".format(commands))
127 salt_actions.install(commands)
128
129 salt_nodes = salt_actions.get_ssh_data()
130 config.underlay.ssh = config.underlay.ssh + \
131 [node for node in salt_nodes
132 if not any(node['node_name'] == n['node_name']
133 for n in config.underlay.ssh)]
134
135 hardware.create_snapshot(ext.SNAPSHOT.cfg_configured)
136 salt_actions.sync_time()
137
138 else:
139 # 1. hardware environment created and powered on
140 # 2. config.underlay.ssh contains SSH access to provisioned nodes
141 # (can be passed from external config with TESTS_CONFIGS variable)
142 # 3. config.tcp.* options contain access credentials to the already
143 # installed TCP API endpoint
144 pass
145
146 salt_actions.sync_time()
147
148 Collection = namedtuple(
149 'Collection', ['salt', 'underlay', 'config'], verbose=True)
150
151 return Collection(salt_actions, underlay, config)
152
153
154@pytest.fixture(scope='function')
155def day1_salt_actions(config, day1_underlay):
156 """Fixture that provides various actions for salt
157
158 :param config: fixture provides oslo.config
159 :param day1_underlay: fixture provides underlay manager
160 :rtype: SaltManager
161 """
162 return saltmanager.SaltManager(config, day1_underlay)