Skip to content

Commit

Permalink
test/: PEP8 compliancy (ansible#24803)
Browse files Browse the repository at this point in the history
* test/: PEP8 compliancy

- Make PEP8 compliant

* Python3 chokes on casting int to bytes (ansible#24952)

But if we tell the formatter that the var is a number, it works
  • Loading branch information
dagwieers authored and gundalow committed May 30, 2017
1 parent 31c59ad commit 4efec41
Show file tree
Hide file tree
Showing 110 changed files with 1,697 additions and 1,542 deletions.
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
from setuptools import setup, find_packages
except ImportError:
print("Ansible now needs setuptools in order to build. Install it using"
" your package manager (usually python-setuptools) or via pip (pip"
" install setuptools).")
" your package manager (usually python-setuptools) or via pip (pip"
" install setuptools).")
sys.exit(1)

with open('requirements.txt') as requirements_file:
install_requirements = requirements_file.read().splitlines()
if not install_requirements:
print("Unable to read requirements from the requirements.txt file"
"That indicates this copy of the source code is incomplete.")
"That indicates this copy of the source code is incomplete.")
sys.exit(2)

SYMLINKS = {'ansible': frozenset(('ansible-console',
Expand Down Expand Up @@ -49,7 +49,7 @@
# Ansible will also make use of a system copy of python-six and
# python-selectors2 if installed but use a Bundled copy if it's not.
install_requires=install_requirements,
package_dir={ '': 'lib' },
package_dir={'': 'lib'},
packages=find_packages('lib'),
package_data={
'': [
Expand Down
1 change: 0 additions & 1 deletion test/integration/cleanup_azure.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

88 changes: 55 additions & 33 deletions test/integration/cleanup_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@
Please use caution, you can easily delete you're *ENTIRE* EC2 infrastructure.
'''

import os
import re
import sys
import boto
import boto.ec2.elb
import optparse
import yaml
import os
import os.path
import boto.ec2.elb
import re
import sys
import time
import yaml


def delete_aws_resources(get_func, attr, opts):
for item in get_func():
val = getattr(item, attr)
if re.search(opts.match_re, val):
prompt_and_delete(item, "Delete matching %s? [y/n]: " % (item,), opts.assumeyes)


def delete_autoscaling_group(get_func, attr, opts):
assumeyes = opts.assumeyes
group_name = None
Expand Down Expand Up @@ -49,7 +51,8 @@ def delete_autoscaling_group(get_func, attr, opts):
group.delete()
while len(asg.get_all_groups(names=[group_name])):
time.sleep(5)
print ("Terminated ASG: %s" % group_name)
print("Terminated ASG: %s" % group_name)


def delete_aws_eips(get_func, attr, opts):

Expand All @@ -65,22 +68,25 @@ def delete_aws_eips(get_func, attr, opts):
if val in eip_log:
prompt_and_delete(item, "Delete matching %s? [y/n]: " % (item,), opts.assumeyes)


def delete_aws_instances(reservation, opts):
for list in reservation:
for item in list.instances:
prompt_and_delete(item, "Delete matching %s? [y/n]: " % (item,), opts.assumeyes)


def prompt_and_delete(item, prompt, assumeyes):
if not assumeyes:
assumeyes = raw_input(prompt).lower() == 'y'
assert hasattr(item, 'delete') or hasattr(item, 'terminate') , "Class <%s> has no delete or terminate attribute" % item.__class__
assert hasattr(item, 'delete') or hasattr(item, 'terminate'), "Class <%s> has no delete or terminate attribute" % item.__class__
if assumeyes:
if hasattr(item, 'delete'):
item.delete()
print ("Deleted %s" % item)
print("Deleted %s" % item)
if hasattr(item, 'terminate'):
item.terminate()
print ("Terminated %s" % item)
print("Terminated %s" % item)


def parse_args():
# Load details from credentials.yml
Expand All @@ -94,63 +100,79 @@ def parse_args():
if default_aws_secret_key is None:
default_aws_secret_key = credentials['ec2_secret_key']

parser = optparse.OptionParser(usage="%s [options]" % (sys.argv[0],),
description=__doc__)
parser.add_option("--access",
parser = optparse.OptionParser(
usage="%s [options]" % (sys.argv[0], ),
description=__doc__
)
parser.add_option(
"--access",
action="store", dest="ec2_access_key",
default=default_aws_access_key,
help="Amazon ec2 access id. Can use EC2_ACCESS_KEY environment variable, or a values from credentials.yml.")
parser.add_option("--secret",
help="Amazon ec2 access id. Can use EC2_ACCESS_KEY environment variable, or a values from credentials.yml."
)
parser.add_option(
"--secret",
action="store", dest="ec2_secret_key",
default=default_aws_secret_key,
help="Amazon ec2 secret key. Can use EC2_SECRET_KEY environment variable, or a values from credentials.yml.")
parser.add_option("--eip-log",
help="Amazon ec2 secret key. Can use EC2_SECRET_KEY environment variable, or a values from credentials.yml."
)
parser.add_option(
"--eip-log",
action="store", dest="eip_log",
default = None,
help = "Path to log of EIPs created during test.")
parser.add_option("--integration-config",
default=None,
help="Path to log of EIPs created during test."
)
parser.add_option(
"--integration-config",
action="store", dest="int_config",
default = "integration_config.yml",
help = "path to integration config")
parser.add_option("--credentials", "-c",
default="integration_config.yml",
help="path to integration config"
)
parser.add_option(
"--credentials", "-c",
action="store", dest="credential_file",
default="credentials.yml",
help="YAML file to read cloud credentials (default: %default)")
parser.add_option("--yes", "-y",
help="YAML file to read cloud credentials (default: %default)"
)
parser.add_option(
"--yes", "-y",
action="store_true", dest="assumeyes",
default=False,
help="Don't prompt for confirmation")
parser.add_option("--match",
help="Don't prompt for confirmation"
)
parser.add_option(
"--match",
action="store", dest="match_re",
default="^ansible-testing-",
help="Regular expression used to find AWS resources (default: %default)")
help="Regular expression used to find AWS resources (default: %default)"
)

(opts, args) = parser.parse_args()
for required in ['ec2_access_key', 'ec2_secret_key']:
if getattr(opts, required) is None:
parser.error("Missing required parameter: --%s" % required)


return (opts, args)


if __name__ == '__main__':

(opts, args) = parse_args()

int_config = yaml.load(open(opts.int_config).read())
if not opts.eip_log:
output_dir = os.path.expanduser(int_config["output_dir"])
opts.eip_log = output_dir + '/' + opts.match_re.replace('^','') + '-eip_integration_tests.log'
opts.eip_log = output_dir + '/' + opts.match_re.replace('^', '') + '-eip_integration_tests.log'

# Connect to AWS
aws = boto.connect_ec2(aws_access_key_id=opts.ec2_access_key,
aws_secret_access_key=opts.ec2_secret_key)
aws_secret_access_key=opts.ec2_secret_key)

elb = boto.connect_elb(aws_access_key_id=opts.ec2_access_key,
aws_secret_access_key=opts.ec2_secret_key)
aws_secret_access_key=opts.ec2_secret_key)

asg = boto.connect_autoscale(aws_access_key_id=opts.ec2_access_key,
aws_secret_access_key=opts.ec2_secret_key)
aws_secret_access_key=opts.ec2_secret_key)

try:
# Delete matching keys
Expand All @@ -172,7 +194,7 @@ def parse_args():
delete_aws_eips(aws.get_all_addresses, 'public_ip', opts)

# Delete temporary instances
filters = {"tag:Name":opts.match_re.replace('^',''), "instance-state-name": ['running', 'pending', 'stopped' ]}
filters = {"tag:Name": opts.match_re.replace('^', ''), "instance-state-name": ['running', 'pending', 'stopped']}
delete_aws_instances(aws.get_all_instances(filters=filters), opts)

except KeyboardInterrupt as e:
Expand Down
39 changes: 26 additions & 13 deletions test/integration/cleanup_gce.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@
Please use caution, you can easily delete your *ENTIRE* GCE infrastructure.
'''

import optparse
import os
import re
import sys
import optparse
import yaml

try:
from libcloud.compute.types import Provider
from libcloud.common.google import (
GoogleBaseError,
QuotaExceededError,
ResourceExistsError,
ResourceInUseError,
ResourceNotFoundError,
)
from libcloud.compute.providers import get_driver
from libcloud.common.google import GoogleBaseError, QuotaExceededError, \
ResourceExistsError, ResourceInUseError, ResourceNotFoundError
from libcloud.compute.types import Provider
_ = Provider.GCE
except ImportError:
print("failed=True " + \
"msg='libcloud with GCE support (0.13.3+) required for this module'")
print("failed=True msg='libcloud with GCE support (0.13.3+) required for this module'")
sys.exit(1)

import gce_credentials
Expand All @@ -30,26 +34,34 @@ def delete_gce_resources(get_func, attr, opts):
if re.search(opts.match_re, val, re.IGNORECASE):
prompt_and_delete(item, "Delete matching %s? [y/n]: " % (item,), opts.assumeyes)


def prompt_and_delete(item, prompt, assumeyes):
if not assumeyes:
assumeyes = raw_input(prompt).lower() == 'y'
assert hasattr(item, 'destroy'), "Class <%s> has no delete attribute" % item.__class__
if assumeyes:
item.destroy()
print ("Deleted %s" % item)
print("Deleted %s" % item)


def parse_args():
parser = optparse.OptionParser(usage="%s [options]" % (sys.argv[0],),
description=__doc__)
parser = optparse.OptionParser(
usage="%s [options]" % sys.argv[0],
description=__doc__
)
gce_credentials.add_credentials_options(parser)
parser.add_option("--yes", "-y",
parser.add_option(
"--yes", "-y",
action="store_true", dest="assumeyes",
default=False,
help="Don't prompt for confirmation")
parser.add_option("--match",
help="Don't prompt for confirmation"
)
parser.add_option(
"--match",
action="store", dest="match_re",
default="^ansible-testing-",
help="Regular expression used to find GCE resources (default: %default)")
help="Regular expression used to find GCE resources (default: %default)"
)

(opts, args) = parser.parse_args()
gce_credentials.check_required(opts, parser)
Expand All @@ -65,6 +77,7 @@ def parse_args():
try:
# Delete matching instances
delete_gce_resources(gce.list_nodes, 'name', opts)

# Delete matching snapshots
def get_snapshots():
for volume in gce.list_volumes():
Expand Down
24 changes: 12 additions & 12 deletions test/integration/cleanup_rax.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ def prompt_and_delete(item, prompt, assumeyes):
if not assumeyes:
assumeyes = raw_input(prompt).lower() == 'y'
assert hasattr(item, 'delete') or hasattr(item, 'terminate'), \
"Class <%s> has no delete or terminate attribute" % item.__class__
"Class <%s> has no delete or terminate attribute" % item.__class__
if assumeyes:
if hasattr(item, 'delete'):
item.delete()
print ("Deleted %s" % item)
print("Deleted %s" % item)
if hasattr(item, 'terminate'):
item.terminate()
print ("Terminated %s" % item)
print("Terminated %s" % item)


def delete_rax(args):
"""Function for deleting CloudServers"""
print ("--- Cleaning CloudServers matching '%s'" % args.match_re)
print("--- Cleaning CloudServers matching '%s'" % args.match_re)
search_opts = dict(name='^%s' % args.match_re)
for region in pyrax.identity.services.compute.regions:
cs = pyrax.connect_to_cloudservers(region=region)
Expand All @@ -80,7 +80,7 @@ def delete_rax(args):

def delete_rax_clb(args):
"""Function for deleting Cloud Load Balancers"""
print ("--- Cleaning Cloud Load Balancers matching '%s'" % args.match_re)
print("--- Cleaning Cloud Load Balancers matching '%s'" % args.match_re)
for region in pyrax.identity.services.load_balancer.regions:
clb = pyrax.connect_to_cloud_loadbalancers(region=region)
for lb in rax_list_iterator(clb):
Expand All @@ -92,7 +92,7 @@ def delete_rax_clb(args):

def delete_rax_keypair(args):
"""Function for deleting Rackspace Key pairs"""
print ("--- Cleaning Key Pairs matching '%s'" % args.match_re)
print("--- Cleaning Key Pairs matching '%s'" % args.match_re)
for region in pyrax.identity.services.compute.regions:
cs = pyrax.connect_to_cloudservers(region=region)
for keypair in cs.keypairs.list():
Expand All @@ -104,7 +104,7 @@ def delete_rax_keypair(args):

def delete_rax_network(args):
"""Function for deleting Cloud Networks"""
print ("--- Cleaning Cloud Networks matching '%s'" % args.match_re)
print("--- Cleaning Cloud Networks matching '%s'" % args.match_re)
for region in pyrax.identity.services.network.regions:
cnw = pyrax.connect_to_cloud_networks(region=region)
for network in cnw.list():
Expand All @@ -116,7 +116,7 @@ def delete_rax_network(args):

def delete_rax_cbs(args):
"""Function for deleting Cloud Networks"""
print ("--- Cleaning Cloud Block Storage matching '%s'" % args.match_re)
print("--- Cleaning Cloud Block Storage matching '%s'" % args.match_re)
for region in pyrax.identity.services.network.regions:
cbs = pyrax.connect_to_cloud_blockstorage(region=region)
for volume in cbs.list():
Expand All @@ -128,7 +128,7 @@ def delete_rax_cbs(args):

def delete_rax_cdb(args):
"""Function for deleting Cloud Databases"""
print ("--- Cleaning Cloud Databases matching '%s'" % args.match_re)
print("--- Cleaning Cloud Databases matching '%s'" % args.match_re)
for region in pyrax.identity.services.database.regions:
cdb = pyrax.connect_to_cloud_databases(region=region)
for db in rax_list_iterator(cdb):
Expand All @@ -146,7 +146,7 @@ def wrapped(uri):

def delete_rax_scaling_group(args):
"""Function for deleting Autoscale Groups"""
print ("--- Cleaning Autoscale Groups matching '%s'" % args.match_re)
print("--- Cleaning Autoscale Groups matching '%s'" % args.match_re)
for region in pyrax.identity.services.autoscale.regions:
asg = pyrax.connect_to_autoscale(region=region)
for group in rax_list_iterator(asg):
Expand All @@ -170,11 +170,11 @@ def main():
try:
func(args)
except Exception as e:
print ("---- %s failed (%s)" % (func.__name__, e.message))
print("---- %s failed (%s)" % (func.__name__, e.message))


if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print ('\nExiting...')
print('\nExiting...')
3 changes: 1 addition & 2 deletions test/integration/gce_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
from libcloud.compute.providers import get_driver
_ = Provider.GCE
except ImportError:
print("failed=True " + \
"msg='libcloud with GCE support (0.13.3+) required for this module'")
print("failed=True msg='libcloud with GCE support (0.13.3+) required for this module'")
sys.exit(1)


Expand Down
Loading

0 comments on commit 4efec41

Please sign in to comment.