Skip to content

Commit

Permalink
Fix opennebula test stub (ansible#40436)
Browse files Browse the repository at this point in the history
* Requesting fixture services from PYONE via environment varaibles

* moved all test functionality out of opennebula module utils
  • Loading branch information
rvalle authored and resmo committed May 25, 2018
1 parent c464419 commit c49b7d2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 88 deletions.
66 changes: 10 additions & 56 deletions lib/ansible/module_utils/opennebula.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
HAS_PYONE = True

try:
import pyone
from pyone import OneException
from pyone.tester import OneServerTester
from pyone.server import OneServer
except ImportError:
OneException = Exception
HAS_PYONE = False
Expand All @@ -25,9 +24,7 @@ class OpenNebulaModule:
"""
Base class for all OpenNebula Ansible Modules.
This is basically a wrapper of the common arguments, the pyone client and
Some utility methods. It will also create a Test client if fixtures are
to be replayed or recorded and manage that they are flush to disk when
required.
some utility methods.
"""

common_args = dict(
Expand Down Expand Up @@ -56,17 +53,11 @@ def __init__(self, argument_spec, supports_check_mode=False, mutually_exclusive=
def create_one_client(self):
"""
Creates an XMLPRC client to OpenNebula.
Dependign on environment variables it will implement a test client.
Returns: the new xmlrpc client.
"""

test_fixture = (environ.get("ONE_TEST_FIXTURE", "False").lower() in ["1", "yes", "true"])
test_fixture_file = environ.get("ONE_TEST_FIXTURE_FILE", "undefined")
test_fixture_replay = (environ.get("ONE_TEST_FIXTURE_REPLAY", "True").lower() in ["1", "yes", "true"])
test_fixture_unit = environ.get("ONE_TEST_FIXTURE_UNIT", "init")

# context required for not validating SSL, old python versions won't validate anyway.
if hasattr(ssl, '_create_unverified_context'):
no_ssl_validation_context = ssl._create_unverified_context()
Expand Down Expand Up @@ -94,36 +85,20 @@ def create_one_client(self):

session = "%s:%s" % (username, password)

if not test_fixture:
if not self.module.params.get("validate_certs") and "PYTHONHTTPSVERIFY" not in environ:
return pyone.OneServer(url, session=session, context=no_ssl_validation_context)
else:
return pyone.OneServer(url, session)
if not self.module.params.get("validate_certs") and "PYTHONHTTPSVERIFY" not in environ:
return OneServer(url, session=session, context=no_ssl_validation_context)
else:
if not self.module.params.get("validate_certs") and "PYTHONHTTPSVERIFY" not in environ:
one = OneServerTester(url,
fixture_file=test_fixture_file,
fixture_replay=test_fixture_replay,
session=session,
context=no_ssl_validation_context)
else:
one = OneServerTester(url,
fixture_file=test_fixture_file,
fixture_replay=test_fixture_replay,
session=session)
one.set_fixture_unit_test(test_fixture_unit)
return one
return OneServer(url, session)

def close_one_client(self):
"""
Closing is only require in the event of fixture recording, as fixtures will be dumped to file
Close the pyone session.
"""
if self.is_fixture_writing():
self.one._close_fixtures()
self.one.server_close()

def fail(self, msg):
"""
Utility failure method, will ensure fixtures are flushed before failing.
Utility failure method, will ensure pyone is properly closed before failing.
Args:
msg: human readable failure reason.
"""
Expand All @@ -133,7 +108,7 @@ def fail(self, msg):

def exit(self):
"""
Utility exit method, will ensure fixtures are flushed before exiting.
Utility exit method, will ensure pyone is properly closed before exiting.
"""
if hasattr(self, 'one'):
Expand Down Expand Up @@ -178,22 +153,6 @@ def get_parameter(self, name):
"""
return self.resolved_parameters.get(name)

def is_fixture_replay(self):
"""
Returns: true if we are currently running fixtures in replay mode.
"""
return (environ.get("ONE_TEST_FIXTURE", "False").lower() in ["1", "yes", "true"]) and \
(environ.get("ONE_TEST_FIXTURE_REPLAY", "True").lower() in ["1", "yes", "true"])

def is_fixture_writing(self):
"""
Returns: true if we are currently running fixtures in write mode.
"""
return (environ.get("ONE_TEST_FIXTURE", "False").lower() in ["1", "yes", "true"]) and \
(environ.get("ONE_TEST_FIXTURE_REPLAY", "True").lower() in ["0", "no", "false"])

def get_host_by_name(self, name):
'''
Returns a host given its name.
Expand Down Expand Up @@ -306,11 +265,6 @@ def wait_for_state(self, element_name, state, state_name, target_states,
if not wait_timeout:
wait_timeout = self.module.params.get("wait_timeout")

if self.is_fixture_replay():
sleep_time_ms = 0.01
else:
sleep_time_ms = 1

start_time = time.time()

while (time.time() - start_time) < wait_timeout:
Expand All @@ -326,7 +280,7 @@ def wait_for_state(self, element_name, state, state_name, target_states,
if current_state in target_states:
return True

time.sleep(sleep_time_ms)
time.sleep(self.one.server_retry_interval())

self.fail(msg="Wait timeout has expired!")

Expand Down
64 changes: 32 additions & 32 deletions test/integration/targets/one_host/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
api_token: "{{ opennebula_password }}"
validate_certs: false
environment:
ONE_TEST_FIXTURE: "{{ opennebula_test_fixture }}"
ONE_TEST_FIXTURE_FILE: /tmp/opennebula-fixtures.json.gz
ONE_TEST_FIXTURE_REPLAY: "{{ opennebula_test_fixture_replay }}"
ONE_TEST_FIXTURE_UNIT: "test_{{test_number}}_{{ item }}"
PYONE_TEST_FIXTURE: "{{ opennebula_test_fixture }}"
PYONE_TEST_FIXTURE_FILE: /tmp/opennebula-fixtures.json.gz
PYONE_TEST_FIXTURE_REPLAY: "{{ opennebula_test_fixture_replay }}"
PYONE_TEST_FIXTURE_UNIT: "test_{{test_number}}_{{ item }}"
with_items: "{{opennebula_test.hosts}}"
register: result

Expand All @@ -47,10 +47,10 @@
api_password: "{{ opennebula_password }}"
validate_certs: false
environment:
ONE_TEST_FIXTURE: "{{ opennebula_test_fixture }}"
ONE_TEST_FIXTURE_FILE: /tmp/opennebula-fixtures.json.gz
ONE_TEST_FIXTURE_REPLAY: "{{ opennebula_test_fixture_replay }}"
ONE_TEST_FIXTURE_UNIT: "test_{{test_number}}_{{item}}"
PYONE_TEST_FIXTURE: "{{ opennebula_test_fixture }}"
PYONE_TEST_FIXTURE_FILE: /tmp/opennebula-fixtures.json.gz
PYONE_TEST_FIXTURE_REPLAY: "{{ opennebula_test_fixture_replay }}"
PYONE_TEST_FIXTURE_UNIT: "test_{{test_number}}_{{item}}"
ignore_errors: true
register: result
with_items:
Expand All @@ -77,10 +77,10 @@
api_password: "{{ opennebula_password }}"
validate_certs: false
environment:
ONE_TEST_FIXTURE: "{{ opennebula_test_fixture }}"
ONE_TEST_FIXTURE_FILE: /tmp/opennebula-fixtures.json.gz
ONE_TEST_FIXTURE_REPLAY: "{{ opennebula_test_fixture_replay }}"
ONE_TEST_FIXTURE_UNIT: "test_{{test_number}}"
PYONE_TEST_FIXTURE: "{{ opennebula_test_fixture }}"
PYONE_TEST_FIXTURE_FILE: /tmp/opennebula-fixtures.json.gz
PYONE_TEST_FIXTURE_REPLAY: "{{ opennebula_test_fixture_replay }}"
PYONE_TEST_FIXTURE_UNIT: "test_{{test_number}}"
register: result

- name: "assert test_{{test_number}} worked"
Expand All @@ -102,10 +102,10 @@
api_password: "{{ opennebula_password }}"
validate_certs: false
environment:
ONE_TEST_FIXTURE: "{{ opennebula_test_fixture }}"
ONE_TEST_FIXTURE_FILE: /tmp/opennebula-fixtures.json.gz
ONE_TEST_FIXTURE_REPLAY: "{{ opennebula_test_fixture_replay }}"
ONE_TEST_FIXTURE_UNIT: "test_{{test_number}}_{{ item }}"
PYONE_TEST_FIXTURE: "{{ opennebula_test_fixture }}"
PYONE_TEST_FIXTURE_FILE: /tmp/opennebula-fixtures.json.gz
PYONE_TEST_FIXTURE_REPLAY: "{{ opennebula_test_fixture_replay }}"
PYONE_TEST_FIXTURE_UNIT: "test_{{test_number}}_{{ item }}"
with_items: "{{opennebula_test.hosts}}"
register: result

Expand All @@ -132,10 +132,10 @@
- custom
TEST_VALUE: 2
environment:
ONE_TEST_FIXTURE: "{{ opennebula_test_fixture }}"
ONE_TEST_FIXTURE_FILE: /tmp/opennebula-fixtures.json.gz
ONE_TEST_FIXTURE_REPLAY: "{{ opennebula_test_fixture_replay }}"
ONE_TEST_FIXTURE_UNIT: "test_{{test_number}}_{{ item }}"
PYONE_TEST_FIXTURE: "{{ opennebula_test_fixture }}"
PYONE_TEST_FIXTURE_FILE: /tmp/opennebula-fixtures.json.gz
PYONE_TEST_FIXTURE_REPLAY: "{{ opennebula_test_fixture_replay }}"
PYONE_TEST_FIXTURE_UNIT: "test_{{test_number}}_{{ item }}"
with_items: "{{opennebula_test.hosts}}"
register: result

Expand All @@ -162,10 +162,10 @@
attributes:
TEST_VALUE: "2"
environment:
ONE_TEST_FIXTURE: "{{ opennebula_test_fixture }}"
ONE_TEST_FIXTURE_FILE: /tmp/opennebula-fixtures.json.gz
ONE_TEST_FIXTURE_REPLAY: "{{ opennebula_test_fixture_replay }}"
ONE_TEST_FIXTURE_UNIT: "test_{{test_number}}_{{ item }}"
PYONE_TEST_FIXTURE: "{{ opennebula_test_fixture }}"
PYONE_TEST_FIXTURE_FILE: /tmp/opennebula-fixtures.json.gz
PYONE_TEST_FIXTURE_REPLAY: "{{ opennebula_test_fixture_replay }}"
PYONE_TEST_FIXTURE_UNIT: "test_{{test_number}}_{{ item }}"
with_items: "{{opennebula_test.hosts}}"
register: result

Expand All @@ -187,10 +187,10 @@
api_password: "{{ opennebula_password }}"
validate_certs: false
environment:
ONE_TEST_FIXTURE: "{{ opennebula_test_fixture }}"
ONE_TEST_FIXTURE_FILE: /tmp/opennebula-fixtures.json.gz
ONE_TEST_FIXTURE_REPLAY: "{{ opennebula_test_fixture_replay }}"
ONE_TEST_FIXTURE_UNIT: "test_{{test_number}}_{{ item }}"
PYONE_TEST_FIXTURE: "{{ opennebula_test_fixture }}"
PYONE_TEST_FIXTURE_FILE: /tmp/opennebula-fixtures.json.gz
PYONE_TEST_FIXTURE_REPLAY: "{{ opennebula_test_fixture_replay }}"
PYONE_TEST_FIXTURE_UNIT: "test_{{test_number}}_{{ item }}"
with_items: "{{opennebula_test.hosts}}"
register: result

Expand All @@ -212,10 +212,10 @@
api_password: "{{ opennebula_password }}"
validate_certs: false
environment:
ONE_TEST_FIXTURE: "{{ opennebula_test_fixture }}"
ONE_TEST_FIXTURE_FILE: /tmp/opennebula-fixtures.json.gz
ONE_TEST_FIXTURE_REPLAY: "{{ opennebula_test_fixture_replay }}"
ONE_TEST_FIXTURE_UNIT: "test_{{test_number}}_{{ item }}"
PYONE_TEST_FIXTURE: "{{ opennebula_test_fixture }}"
PYONE_TEST_FIXTURE_FILE: /tmp/opennebula-fixtures.json.gz
PYONE_TEST_FIXTURE_REPLAY: "{{ opennebula_test_fixture_replay }}"
PYONE_TEST_FIXTURE_UNIT: "test_{{test_number}}_{{ item }}"
with_items: "{{opennebula_test.hosts}}"
register: result

Expand Down

0 comments on commit c49b7d2

Please sign in to comment.