Skip to content

Commit

Permalink
Rework test_openstack.py to use the testing.helpers utilities.
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardomurri committed Sep 2, 2016
1 parent a42d057 commit a2a32d7
Showing 1 changed file with 33 additions and 32 deletions.
65 changes: 33 additions & 32 deletions gc3libs/backends/tests/test_openstack.py
Expand Up @@ -33,30 +33,17 @@
import gc3libs.config
import gc3libs.exceptions

from gc3libs.testing.helpers import temporary_config

def _setup_config_file(confstr):
(fd, name) = tempfile.mkstemp()
f = os.fdopen(fd, 'w+')
f.write(confstr)
f.close()
return name

class TestOpenStackLrms(object):

def test_openstack_variables_are_optionals():
tmpfile = _setup_config_file("""
class const:
_CFG0 = """
[auth/gc3user_ssh]
type = ssh
username = gc3-user
[auth/hobbes]
type=openstack
# Mandatory fields that are set via environment variable
# os_username=USERNAME
# os_password=PASSWORD
# os_project_name=TENANT
[resource/hobbes]
# Mandatory fields that are set via environment variable
# os_auth_url=http://cloud.gc3.uzh.ch:5000/v2.0
Expand All @@ -77,25 +64,39 @@ def test_openstack_variables_are_optionals():
image_id=a9e98055-5aa3-4636-8fc5-f3b2b4ea66bb
security_group_name=gc3pie_ssh
security_group_rules=tcp:22:22:0.0.0.0/0, icmp:-1:-1:0.0.0.0/0
""")
cfgvalues = ['username', 'password', 'tenant_name', 'auth_url']
for name in cfgvalues:
os.environ['OS_' + name.upper()] = name
try:
cfg = gc3libs.config.Configuration(tmpfile)
resources = cfg.make_resources()
assert 'hobbes' in resources
[auth/hobbes]
type=openstack
"""

_CFG1 = """
# Mandatory fields that can be set via environment variable
os_username=USERNAME
os_password=PASSWORD
os_project_name=TENANT
"""

_CFG_WITH_AUTH = (const._CFG0 + const._CFG1)

for name in cfgvalues:
assert hasattr(resources['hobbes'], 'os_' + name)
assert_equal(getattr(resources['hobbes'], 'os_' + name), name)
finally:
os.remove(tmpfile)
_CFG_NO_AUTH = const._CFG0

def test_openstack_variables_are_optionals(self):
with temporary_config(self._CFG_NO_AUTH, keep=True) as cfgfile:
# set up fake environment
vars = ['username', 'password', 'tenant_name', 'auth_url']
for name in vars:
os.environ['OS_' + name.upper()] = name

# check that resource has been correctly created
cfg = gc3libs.config.Configuration(cfgfile.name)
resources = cfg.make_resources()
assert 'hobbes' in resources

# check that values have been inherited from the environment
for name in vars:
assert hasattr(resources['hobbes'], 'os_' + name)
assert_equal(getattr(resources['hobbes'], 'os_' + name), name)

class TestOpenStackLrms(object):
pass

# main: run tests

Expand Down

0 comments on commit a2a32d7

Please sign in to comment.