blob: e223a2b13e4910e8b194a5fb18ef05e86d7162b0 [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
Dmitry Tyzhnenko35413c02018-03-05 14:12:37 +020074 hardware.create_snapshot(ext.SNAPSHOT.day1_underlay)
75
76 else:
77 # 1. hardware environment created and powered on
78 # 2. config.underlay.ssh contains SSH access to provisioned nodes
79 # (can be passed from external config with TESTS_CONFIGS variable)
80 underlay = underlay_ssh_manager.UnderlaySSHManager(config)
81
82 return underlay
83
84
85@pytest.mark.revert_snapshot(ext.SNAPSHOT.cfg_configured)
86@pytest.fixture(scope='function')
87def day1_cfg_config(revert_snapshot, request, config, hardware, underlay,
88 salt_actions, snapshot, grab_versions):
89 """Fixture to get or install cfg node from day1 image on environment
90
91 :param revert_snapshot: fixture that reverts snapshot that is specified
92 in test with @pytest.mark.revert_snapshot(<name>)
93 :param request: fixture provides pytest data
94 :param config: fixture provides oslo.config
95 :param hardware: fixture provides enviromnet manager
96 :param day1_underlay: fixture provides underlay manager
97 :param salt_actions: fixture provides SaltManager instance
98 :rtype: SaltManager
99
100 If config.salt.salt_master_host is not set, this fixture assumes that
101 the salt was not installed, and do the following:
102 - install salt master and salt minions
103 - make snapshot with name 'cfg_configured'
104 - return SaltManager
105
106 If config.salt.salt_master_host was set, this fixture assumes that the
107 salt was already deployed, and do the following:
108 - return SaltManager instance
109
110 If you want to revert 'cfg_configured' snapshot, please use mark:
111 @pytest.mark.revert_snapshot("cfg_configured")
112 """
113 # Create Salt cluster
114 if config.salt.salt_master_host == '0.0.0.0':
115 # Temporary workaround. Underlay should be extended with roles
116 config.salt.salt_master_host = \
117 underlay.host_by_node_role(
118 node_role=ext.UNDERLAY_NODE_ROLES.salt_master)
119
120 commands = underlay.read_template(
121 config.day1_cfg_config.configure_steps_path)
122 LOG.info("############ Executing command ####### {0}".format(commands))
123 salt_actions.install(commands)
124
125 salt_nodes = salt_actions.get_ssh_data()
126 config.underlay.ssh = config.underlay.ssh + \
127 [node for node in salt_nodes
128 if not any(node['node_name'] == n['node_name']
129 for n in config.underlay.ssh)]
130
131 hardware.create_snapshot(ext.SNAPSHOT.cfg_configured)
132 salt_actions.sync_time()
133
134 else:
135 # 1. hardware environment created and powered on
136 # 2. config.underlay.ssh contains SSH access to provisioned nodes
137 # (can be passed from external config with TESTS_CONFIGS variable)
138 # 3. config.tcp.* options contain access credentials to the already
139 # installed TCP API endpoint
140 pass
141
142 salt_actions.sync_time()
143
144 Collection = namedtuple(
145 'Collection', ['salt', 'underlay', 'config'], verbose=True)
146
147 return Collection(salt_actions, underlay, config)
148
149
150@pytest.fixture(scope='function')
151def day1_salt_actions(config, day1_underlay):
152 """Fixture that provides various actions for salt
153
154 :param config: fixture provides oslo.config
155 :param day1_underlay: fixture provides underlay manager
156 :rtype: SaltManager
157 """
158 return saltmanager.SaltManager(config, day1_underlay)