pegleg/tests/conftest.py
Ian H. Pittwood 4480ab5574 Restructure usage of test fixtures
Pytest includes a fixture that can be used to generate temporary
directories. Previously Pegleg had implemented a hombrewed version of a
temporary directory fixture. This change removes the homebrewed version
and replaces it with the tmpdir fixture.

Implement tmpdir fixture in tests

Upgrade all testing packages to use the latest features

Removes unused imports and organizes import lists

Removes mock package requirement and uses unittest.mock, included in
python >3.3

Implements a slightly cleaner method to get proxy info

Change-Id: If66e1cfba858d5fb8948529deb8fb2d32345f630
2019-07-29 11:37:36 -05:00

168 lines
4.8 KiB
Python

# Copyright 2018 AT&T Intellectual Property. All other rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
import copy
import os
import pytest
import yaml
from pegleg import config
from pegleg.engine.util import files
TEST_DOCUMENT = """
---
schema: deckhand/Passphrase/v1
metadata:
schema: metadata/Document/v1
name: %(name)s
storagePolicy: %(storagePolicy)s
layeringDefinition:
abstract: False
layer: %(layer)s
data: %(name)s-password
...
"""
@pytest.fixture(autouse=True)
def restore_config():
"""Used for ensuring the original global context is reset in memory
following each test execution.
"""
original_global_context = copy.deepcopy(config.GLOBAL_CONTEXT)
try:
yield
finally:
config.GLOBAL_CONTEXT = original_global_context
def _gen_document(**kwargs):
if "storagePolicy" not in kwargs:
kwargs["storagePolicy"] = "cleartext"
test_document = TEST_DOCUMENT % kwargs
return yaml.safe_load(test_document)
@pytest.fixture
def temp_deployment_files(tmpdir):
"""Fixture that creates a temporary directory structure."""
sitenames = ['cicd', 'lab']
SITE_TEST_STRUCTURE = {
'directories': {
'secrets': {
'directories': {
'passphrases': {
'files': {}
},
},
},
'software': {
'directories': {
'charts': {
'files': {}
},
},
},
},
'files': {}
}
p = tmpdir.mkdir("deployment_files")
config.set_site_repo(str(p))
# Create global directories and files.
files._create_tree(
os.path.join(str(p), 'global'),
tree={
'directories': {
'common': {
'files': {
'global-common.yaml': _gen_document(
name="global-common", layer='global')
}
},
'v1.0': {
'files': {
'global-v1.0.yaml': _gen_document(
name="global-v1.0", layer='global')
}
}
}
})
# Create type directories and files.
files._create_tree(
os.path.join(str(p), 'type'),
tree={
'directories': {
site: {
'directories': {
'common': {
'files': {
'%s-type-common.yaml' % site: _gen_document(
name="%s-type-common" % site, layer='type')
}
},
'v1.0': {
'files': {
'%s-type-v1.0.yaml' % site: _gen_document(
name="%s-type-v1.0" % site, layer='type')
}
}
}
}
for site in sitenames
}
})
# Create site directories and files.
for site in sitenames:
site_definition = """
---
data:
repositories:
global:
revision: v1.0
url: http://nothing.com
site_type: %s
metadata:
layeringDefinition: {abstract: false, layer: site}
name: %s
schema: metadata/Document/v1
storagePolicy: cleartext
schema: pegleg/SiteDefinition/v1
""" % (site, site)
test_structure = SITE_TEST_STRUCTURE.copy()
test_structure['directories']['secrets']['directories']['passphrases'][
'files'] = {
'%s-passphrase.yaml' % site: _gen_document(
name="%s-passphrase" % site, layer='site')
}
test_structure['directories']['software']['directories']['charts'][
'files'] = {
'%s-chart.yaml' % site: _gen_document(
name="%s-chart" % site, layer='site')
}
test_structure['files']['site-definition.yaml'] = yaml.safe_load(
site_definition)
cicd_path = os.path.join(str(p), files._site_path(site))
files._create_tree(cicd_path, tree=test_structure)
return tmpdir