Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test uploaded images with a non-admin user #5

Open
wants to merge 2 commits into
base: feature/cah
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
cfg.BoolOpt('yes-i-really-know-what-i-do', help='Really delete images', default=False),
cfg.BoolOpt('use-os-hidden', help='Use the os_hidden property', default=False),
cfg.BoolOpt('test-latest-image', help='Test the latest uploaded image', default=False),
cfg.BoolOpt('use-floating-ip', help='Attach a floating IP to the test image', default=True),
cfg.StrOpt('network', help='Network which should be used for the test instances', default='floating-IPv4'),
cfg.StrOpt('flavor', help='Flavor which should be used for the test instances', default='S'),
cfg.StrOpt('security-group', help='Security group which should used for the test instance', default='ssh'),
cfg.StrOpt('cloud', help='Cloud name in clouds.yaml', default='images'),
cfg.StrOpt('name', help='Image name to process', default=None),
cfg.StrOpt('images', help='Path to the images.yml file', default='etc/images.yml'),
Expand Down Expand Up @@ -75,6 +77,14 @@ def create_keypair(conn, keypair_name):
return keypair


def create_security_group(conn, security_group_name, port, protocol):
security_group = conn.create_security_group(name=security_group_name,
description="sg for login into a test instance")
conn.create_security_group_rule(security_group.id, protocol=protocol,
port_range_min=port, port_range_max=port)
return security_group


def delete_keypair(conn, keypair_name):
keypair = conn.compute.find_keypair(keypair_name)
keypair_file = str(pathlib.Path.cwd() / keypair_name)
Expand All @@ -97,7 +107,11 @@ def test_image(conn, image, name):
keypair_name = 'test_key'
keypair_file = str(pathlib.Path.cwd() / keypair_name)
keypair = create_keypair(conn, keypair_name)
security_group = conn.network.find_security_group('ssh')
security_group = conn.network.find_security_group(CONF.security_group)

if not security_group:
# create a fallback security_group
security_group = create_security_group(conn, CONF.security_group, 22, 'tcp')

server = conn.compute.create_server(
name='test instance', image_id=image.id, flavor_id=flavor.id,
Expand All @@ -112,13 +126,16 @@ def test_image(conn, image, name):
return 'failure'

conn.compute.add_security_group_to_server(server, security_group)
server_ip = server.addresses[CONF.network][0]['addr']
if CONF.use_floating_ip:
server_ip = conn.add_auto_ip(server)
else:
server_ip = server.addresses[CONF.network][0]['addr']

time.sleep(60.0)
test_process = subprocess.run(["ssh", "-o", "StrictHostKeyChecking no", "-i",
keypair_file, login+"@"+server_ip, "ls"])

conn.compute.delete_server(server)
conn.compute.delete_server(server.id, delete_ips=True)
delete_keypair(conn, keypair_name)

if test_process.returncode == 0:
Expand Down