releases/openstack_releases/tests/fixtures.py
Doug Hellmann a97418e706 move test fixtures to their own file
This will make it easier to reuse the git repo fixture.

Change-Id: I8922029fcd60fa94dad73cb187982904289d9a3b
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
2018-06-18 16:45:22 -04:00

131 lines
4.3 KiB
Python

# All 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.
import logging
import os.path
import re
import textwrap
import fixtures
from openstack_releases import processutils
class GPGKeyFixture(fixtures.Fixture):
"""Creates a GPG key for testing.
It's recommended that this be used in concert with a unique home
directory.
"""
def setUp(self):
super(GPGKeyFixture, self).setUp()
# Force a temporary home directory with a short path so the
# gpg commands do not complain about an excessively long
# value.
self.useFixture(fixtures.TempHomeDir('/tmp'))
tempdir = self.useFixture(fixtures.TempDir('/tmp'))
gnupg_version_re = re.compile('^gpg\s.*\s([\d+])\.([\d+])\.([\d+])')
gnupg_version = processutils.check_output(
['gpg', '--version'],
cwd=tempdir.path).decode('utf-8')
for line in gnupg_version.split('\n'):
gnupg_version = gnupg_version_re.match(line)
if gnupg_version:
gnupg_version = (int(gnupg_version.group(1)),
int(gnupg_version.group(2)),
int(gnupg_version.group(3)))
break
else:
if gnupg_version is None:
gnupg_version = (0, 0, 0)
config_file = tempdir.path + '/key-config'
with open(config_file, 'wt') as f:
if gnupg_version[0] == 2 and gnupg_version[1] >= 1:
f.write(textwrap.dedent("""
%no-protection
%transient-key
"""))
f.write(textwrap.dedent("""
%no-ask-passphrase
Key-Type: RSA
Name-Real: Example Key
Name-Comment: N/A
Name-Email: example@example.com
Expire-Date: 2d
%commit
"""))
# Note that --quick-random (--debug-quick-random in GnuPG 2.x)
# does not have a corresponding preferences file setting and
# must be passed explicitly on the command line instead
if gnupg_version[0] == 1:
gnupg_random = '--quick-random'
elif gnupg_version[0] >= 2:
gnupg_random = '--debug-quick-random'
else:
gnupg_random = ''
cmd = ['gpg', '--gen-key', '--batch']
if gnupg_random:
cmd.append(gnupg_random)
cmd.append('key-config')
processutils.check_call(cmd, cwd=tempdir.path)
class GitRepoFixture(fixtures.Fixture):
logger = logging.getLogger('git')
def __init__(self, workdir, name):
self.workdir = workdir
self.name = name
self.path = os.path.join(self.workdir, self.name)
super().__init__()
def setUp(self):
super().setUp()
self.useFixture(GPGKeyFixture())
os.makedirs(self.path)
self.git('init', '.')
self.git('config', '--local', 'user.email', 'example@example.com')
self.git('config', '--local', 'user.name', 'super developer')
self.git('config', '--local', 'user.signingkey',
'example@example.com')
def git(self, *args):
self.logger.debug('$ git %s', ' '.join(args))
output = processutils.check_output(
['git'] + list(args),
cwd=self.path,
)
self.logger.debug(output)
return output
def commit(self, message='commit message'):
self.git('add', '.')
self.git('commit', '-m', message)
sha = self.git('log', '-n', '1', '--pretty=format:%H')
return sha.decode('utf-8').strip()
def add_file(self, name):
with open(os.path.join(self.path, name), 'w') as f:
f.write('adding %s\n' % name)
return self.commit('add %s' % name)
def tag(self, version):
self.git('tag', '-s', '-m', version, version)