tenks/tests/utils.py
Will Szumski d4e7e58619 Wait for resources to become available
This will wait for any resources that are created to become available
before exiting the script. This allows you to avoid a race condition
where a server could be created before the resource tracker had been
updated with the new resources; server creation would fail.

Change-Id: I57f8c93cb1ebbc284b96ef1ced2c4edd59b27795
Story: 2004274
Task: 27823
Depends-On: https://review.openstack.org/617642
2018-11-19 16:40:43 +00:00

61 lines
1.6 KiB
Python

import json
import unittest
from ansible.module_utils import basic
from ansible.module_utils._text import to_bytes
# Python 2/3 compatibility.
try:
from unittest.mock import MagicMock, patch
except ImportError:
from mock import MagicMock, patch # noqa
def set_module_args(args):
if '_ansible_remote_tmp' not in args:
args['_ansible_remote_tmp'] = '/tmp'
if '_ansible_keep_remote_files' not in args:
args['_ansible_keep_remote_files'] = False
args = json.dumps({'ANSIBLE_MODULE_ARGS': args})
basic._ANSIBLE_ARGS = to_bytes(args)
class AnsibleExitJson(Exception):
pass
class AnsibleFailJson(Exception):
pass
def exit_json(*args, **kwargs):
if 'changed' not in kwargs:
kwargs['changed'] = False
raise AnsibleExitJson(kwargs)
def fail_json(*args, **kwargs):
kwargs['failed'] = True
raise AnsibleFailJson(kwargs)
class ModuleTestCase(unittest.TestCase):
def __init__(self, *args, **kwargs):
super(ModuleTestCase, self).__init__(*args, **kwargs)
# Python 2 / 3 compatibility. assertRaisesRegexp was renamed to
# assertRaisesRegex in python 3.1.
if hasattr(self, 'assertRaisesRegexp') and \
not hasattr(self, 'assertRaisesRegex'):
self.assertRaisesRegex = self.assertRaisesRegexp
def setUp(self):
self.mock_module = patch.multiple(basic.AnsibleModule,
exit_json=exit_json,
fail_json=fail_json)
self.mock_module.start()
set_module_args({})
self.addCleanup(self.mock_module.stop)