refactor old remove_instance tests

This commit is contained in:
Wayne Witzel III
2016-05-10 11:42:54 -04:00
parent 102b8a4f91
commit 2c0c8591df
2 changed files with 30 additions and 46 deletions

View File

@@ -8,7 +8,10 @@ except ImportError:
from django.core.management import call_command
from awx.main.models import Instance
from awx.main.management.commands.update_password import UpdatePassword
from awx.main.management.commands.remove_instance import Command as RemoveInstance
def run_command(name, *args, **options):
command_runner = options.pop('command_runner', call_command)
@@ -35,18 +38,38 @@ def run_command(name, *args, **options):
sys.stderr = original_stderr
return result, captured_stdout, captured_stderr
@pytest.mark.parametrize(
"command,username,password,expected,changed", [
('update_password', 'admin', 'dingleberry', 'Password updated\n', True),
('update_password', 'admin', 'admin', 'Password not updated\n', False),
('update_password', None, 'foo', 'username required', False),
('update_password', 'admin', None, 'password required', False),
"username,password,expected,changed", [
('admin', 'dingleberry', 'Password updated\n', True),
('admin', 'admin', 'Password not updated\n', False),
(None, 'foo', 'username required', False),
('admin', None, 'password required', False),
]
)
def test_run_command(mocker, command, username, password, expected, changed):
def test_update_password_command(mocker, username, password, expected, changed):
with mocker.patch.object(UpdatePassword, 'update_password', return_value=changed):
result, stdout, stderr = run_command(command, username=username, password=password)
result, stdout, stderr = run_command('update_password', username=username, password=password)
if result is None:
assert stdout == expected
else:
assert str(result) == expected
@pytest.mark.parametrize(
"primary,hostname,startswith,exception", [
(True, "127.0.0.1", "Cannot remove primary", None),
(False, "127.0.0.2", "Successfully removed", None),
(False, "127.0.0.3", "No matching instance", Instance.DoesNotExist),
]
)
def test_remove_instance_command(mocker, primary, hostname, startswith, exception):
mock_instance = mocker.MagicMock(primary=primary, enforce_unique_find=True)
with mocker.patch.object(Instance.objects, 'get', return_value=mock_instance, side_effect=exception):
with mocker.patch.object(RemoveInstance, 'include_option_hostname_uuid_find'):
with mocker.patch.object(RemoveInstance, 'get_unique_fields', return_value={'hostname':hostname, 'uuid':1}):
result, stdout, stderr = run_command("remove_instance", hostname=hostname)
if result is None:
assert stdout.startswith(startswith)
else:
assert str(result).startswith(startswith)