Dennis Dmitriev | 8cb2f41 | 2016-10-18 14:53:58 +0300 | [diff] [blame] | 1 | # Copyright 2016 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 | |
| 15 | import os |
Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [diff] [blame] | 16 | |
Dennis Dmitriev | 8cb2f41 | 2016-10-18 14:53:58 +0300 | [diff] [blame] | 17 | import pytest |
Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [diff] [blame] | 18 | import yaml |
Dennis Dmitriev | 8cb2f41 | 2016-10-18 14:53:58 +0300 | [diff] [blame] | 19 | |
| 20 | from tcp_tests import logger |
| 21 | from tcp_tests.helpers import ext |
| 22 | from tcp_tests import settings |
Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [diff] [blame] | 23 | from tcp_tests.managers import saltmanager |
Dennis Dmitriev | 8cb2f41 | 2016-10-18 14:53:58 +0300 | [diff] [blame] | 24 | |
| 25 | LOG = logger.logger |
| 26 | |
| 27 | |
| 28 | @pytest.fixture(scope='function') |
Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [diff] [blame] | 29 | def salt_actions(config, underlay): |
Dennis Dmitriev | 2cbf235 | 2016-11-11 15:34:21 +0200 | [diff] [blame^] | 30 | """Fixture that provides various actions for salt |
Dennis Dmitriev | 8cb2f41 | 2016-10-18 14:53:58 +0300 | [diff] [blame] | 31 | |
| 32 | :param config: fixture provides oslo.config |
| 33 | :param underlay: fixture provides underlay manager |
Dennis Dmitriev | 2cbf235 | 2016-11-11 15:34:21 +0200 | [diff] [blame^] | 34 | :rtype: SaltManager |
Dennis Dmitriev | 8cb2f41 | 2016-10-18 14:53:58 +0300 | [diff] [blame] | 35 | """ |
Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [diff] [blame] | 36 | return saltmanager.SaltManager(config, underlay) |
Dennis Dmitriev | 8cb2f41 | 2016-10-18 14:53:58 +0300 | [diff] [blame] | 37 | |
| 38 | |
| 39 | @pytest.fixture(scope='function') |
Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [diff] [blame] | 40 | def salt_deployed(revert_snapshot, request, config, |
| 41 | hardware, underlay, salt_actions): |
Dennis Dmitriev | 2cbf235 | 2016-11-11 15:34:21 +0200 | [diff] [blame^] | 42 | """Fixture to get or install salt service on environment |
Dennis Dmitriev | 8cb2f41 | 2016-10-18 14:53:58 +0300 | [diff] [blame] | 43 | |
Dennis Dmitriev | 2cbf235 | 2016-11-11 15:34:21 +0200 | [diff] [blame^] | 44 | :param revert_snapshot: fixture that reverts snapshot that is specified |
| 45 | in test with @pytest.mark.revert_snapshot(<name>) |
Dennis Dmitriev | 8cb2f41 | 2016-10-18 14:53:58 +0300 | [diff] [blame] | 46 | :param request: fixture provides pytest data |
| 47 | :param config: fixture provides oslo.config |
| 48 | :param hardware: fixture provides enviromnet manager |
| 49 | :param underlay: fixture provides underlay manager |
Dennis Dmitriev | 2cbf235 | 2016-11-11 15:34:21 +0200 | [diff] [blame^] | 50 | :param salt_actions: fixture provides SaltManager instance |
| 51 | :rtype: SaltManager |
Dennis Dmitriev | 8cb2f41 | 2016-10-18 14:53:58 +0300 | [diff] [blame] | 52 | |
Dennis Dmitriev | 2cbf235 | 2016-11-11 15:34:21 +0200 | [diff] [blame^] | 53 | If config.salt.salt_master_host is not set, this fixture assumes that |
| 54 | the salt was not installed, and do the following: |
| 55 | - install salt master and salt minions |
| 56 | - make snapshot with name 'salt_deployed' |
| 57 | - return SaltManager |
Dennis Dmitriev | 8cb2f41 | 2016-10-18 14:53:58 +0300 | [diff] [blame] | 58 | |
Dennis Dmitriev | 2cbf235 | 2016-11-11 15:34:21 +0200 | [diff] [blame^] | 59 | If config.salt.salt_master_host was set, this fixture assumes that the |
| 60 | salt was already deployed, and do the following: |
| 61 | - return SaltManager instance |
Dennis Dmitriev | 8cb2f41 | 2016-10-18 14:53:58 +0300 | [diff] [blame] | 62 | |
Dennis Dmitriev | 2cbf235 | 2016-11-11 15:34:21 +0200 | [diff] [blame^] | 63 | If you want to revert 'salt_deployed' snapshot, please use mark: |
| 64 | @pytest.mark.revert_snapshot("salt_deployed") |
Dennis Dmitriev | 8cb2f41 | 2016-10-18 14:53:58 +0300 | [diff] [blame] | 65 | """ |
| 66 | # If no snapshot was reverted, then try to revert the snapshot |
| 67 | # that belongs to the fixture. |
| 68 | # Note: keep fixtures in strict dependences from each other! |
| 69 | if not revert_snapshot: |
Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [diff] [blame] | 70 | if hardware.has_snapshot(ext.SNAPSHOT.salt_deployed) and \ |
| 71 | hardware.has_snapshot_config(ext.SNAPSHOT.salt_deployed): |
| 72 | hardware.revert_snapshot(ext.SNAPSHOT.salt_deployed) |
Dennis Dmitriev | 8cb2f41 | 2016-10-18 14:53:58 +0300 | [diff] [blame] | 73 | |
Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [diff] [blame] | 74 | # Create Salt cluster |
| 75 | if config.salt.salt_master_host == '0.0.0.0': |
| 76 | with underlay.yaml_editor( |
| 77 | config.salt_deploy.salt_steps_path) as commands: |
| 78 | salt_actions.install(commands.content) |
| 79 | hardware.create_snapshot(ext.SNAPSHOT.salt_deployed) |
Dennis Dmitriev | 8cb2f41 | 2016-10-18 14:53:58 +0300 | [diff] [blame] | 80 | |
| 81 | else: |
| 82 | # 1. hardware environment created and powered on |
| 83 | # 2. config.underlay.ssh contains SSH access to provisioned nodes |
| 84 | # (can be passed from external config with TESTS_CONFIGS variable) |
| 85 | # 3. config.tcp.* options contain access credentials to the already |
| 86 | # installed TCP API endpoint |
| 87 | pass |
| 88 | |
Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [diff] [blame] | 89 | return salt_actions |