Auth configuration for each node
This patch allows configuring separate ssh auth parameters for each node. Change-Id: Ib055c2f4f2eea0870ba546e873c8b544e0b29424
This commit is contained in:
parent
77ba7fd26f
commit
3beb80e0af
|
@ -130,11 +130,10 @@ class AnsibleRunner(object):
|
|||
|
||||
ssh_common_args = SSH_COMMON_ARGS
|
||||
if jump_host:
|
||||
ssh_common_args += (
|
||||
' -o ProxyCommand="ssh -i %(key)s -W %%h:%%p %(ssh_args)s '
|
||||
'%(user)s@%(host)s"'
|
||||
% dict(key=private_key_file, user=jump_user or remote_user,
|
||||
host=jump_host, ssh_args=SSH_COMMON_ARGS))
|
||||
ssh_common_args += self._build_proxy_arg(
|
||||
jump_host=jump_host,
|
||||
jump_user=jump_user or remote_user,
|
||||
private_key_file=private_key_file)
|
||||
|
||||
self.passwords = dict(conn_pass=password, become_pass=password)
|
||||
self.options = Options(
|
||||
|
@ -148,7 +147,15 @@ class AnsibleRunner(object):
|
|||
verbosity=100, check=False)
|
||||
self.serial = serial or 10
|
||||
|
||||
def _run_play(self, play_source):
|
||||
@staticmethod
|
||||
def _build_proxy_arg(jump_user, jump_host, private_key_file=None):
|
||||
key = '-i ' + private_key_file if private_key_file else ''
|
||||
return (' -o ProxyCommand="ssh %(key)s -W %%h:%%p %(ssh_args)s '
|
||||
'%(user)s@%(host)s"'
|
||||
% dict(key=key, user=jump_user,
|
||||
host=jump_host, ssh_args=SSH_COMMON_ARGS))
|
||||
|
||||
def _run_play(self, play_source, host_vars):
|
||||
host_list = play_source['hosts']
|
||||
|
||||
loader = dataloader.DataLoader()
|
||||
|
@ -158,6 +165,13 @@ class AnsibleRunner(object):
|
|||
host_list=host_list)
|
||||
variable_manager.set_inventory(inventory_inst)
|
||||
|
||||
for host, variables in host_vars.items():
|
||||
host_inst = inventory_inst.get_host(host)
|
||||
for var_name, value in variables.items():
|
||||
if value is not None:
|
||||
variable_manager.set_host_variable(
|
||||
host_inst, var_name, value)
|
||||
|
||||
storage = []
|
||||
callback = MyCallback(storage)
|
||||
|
||||
|
@ -183,13 +197,13 @@ class AnsibleRunner(object):
|
|||
|
||||
return storage
|
||||
|
||||
def run_playbook(self, playbook):
|
||||
def run_playbook(self, playbook, host_vars):
|
||||
result = []
|
||||
|
||||
for play_source in playbook:
|
||||
play_source['gather_facts'] = 'no'
|
||||
|
||||
result += self._run_play(play_source)
|
||||
result += self._run_play(play_source, host_vars)
|
||||
|
||||
return result
|
||||
|
||||
|
@ -207,8 +221,11 @@ class AnsibleRunner(object):
|
|||
LOG.debug('Executing task: %s on hosts: %s with serial: %s',
|
||||
task, hosts, self.serial)
|
||||
|
||||
task_play = {'hosts': hosts, 'tasks': [task], 'serial': self.serial}
|
||||
result = self.run_playbook([task_play])
|
||||
host_vars = {h.ip: self._build_host_vars(h) for h in hosts}
|
||||
task_play = {'hosts': [h.ip for h in hosts],
|
||||
'tasks': [task],
|
||||
'serial': self.serial}
|
||||
result = self.run_playbook([task_play], host_vars)
|
||||
|
||||
log_result = copy.deepcopy(result)
|
||||
LOG.debug('Execution completed with %s result(s):' % len(log_result))
|
||||
|
@ -240,3 +257,25 @@ class AnsibleRunner(object):
|
|||
raise ek(msg)
|
||||
|
||||
return result
|
||||
|
||||
def _build_host_vars(self, host):
|
||||
if not host.auth:
|
||||
return {}
|
||||
|
||||
ssh_common_args = None
|
||||
if 'jump' in host.auth:
|
||||
ssh_common_args = SSH_COMMON_ARGS
|
||||
ssh_common_args += self._build_proxy_arg(
|
||||
jump_host=host.auth['jump']['host'],
|
||||
jump_user=host.auth['jump'].get(
|
||||
'username', self.options.remote_user),
|
||||
private_key_file=host.auth['jump'].get(
|
||||
'private_key_file', self.options.private_key_file))
|
||||
|
||||
return {
|
||||
'ansible_user': host.auth.get('username'),
|
||||
'ansible_ssh_pass': host.auth.get('password'),
|
||||
'ansible_become': host.auth.get('sudo'),
|
||||
'ansible_ssh_private_key_file': host.auth.get('private_key_file'),
|
||||
'ansible_ssh_common_args': ssh_common_args,
|
||||
}
|
||||
|
|
|
@ -26,11 +26,12 @@ class Host(utils.ComparableMixin, utils.ReprMixin):
|
|||
|
||||
ATTRS = ('ip', 'mac', 'fqdn', 'libvirt_name')
|
||||
|
||||
def __init__(self, ip, mac=None, fqdn=None, libvirt_name=None):
|
||||
def __init__(self, ip, mac=None, fqdn=None, libvirt_name=None, auth=None):
|
||||
self.ip = ip
|
||||
self.mac = mac
|
||||
self.fqdn = fqdn
|
||||
self.libvirt_name = libvirt_name
|
||||
self.auth = auth
|
||||
|
||||
|
||||
class NodeCollection(utils.ReprMixin):
|
||||
|
@ -131,7 +132,7 @@ class NodeCollection(utils.ReprMixin):
|
|||
"""
|
||||
LOG.info('Run task: %s on nodes: %s', task, self)
|
||||
return self.cloud_management.execute_on_cloud(
|
||||
self.get_ips(), task, raise_on_error=raise_on_error)
|
||||
self.hosts, task, raise_on_error=raise_on_error)
|
||||
|
||||
@public
|
||||
def reboot(self):
|
||||
|
@ -140,7 +141,7 @@ class NodeCollection(utils.ReprMixin):
|
|||
"""
|
||||
LOG.info('Reboot nodes: %s', self)
|
||||
task = {'command': 'reboot now'}
|
||||
self.cloud_management.execute_on_cloud(self.get_ips(), task)
|
||||
self.cloud_management.execute_on_cloud(self.hosts, task)
|
||||
|
||||
@public
|
||||
def oom(self):
|
||||
|
|
|
@ -88,27 +88,16 @@ class ServiceAsProcess(service.Service):
|
|||
self.port = self.config.get('port')
|
||||
|
||||
def _run_task(self, task, nodes):
|
||||
ips = nodes.get_ips()
|
||||
if not ips:
|
||||
if len(nodes) == 0:
|
||||
raise error.ServiceError('Node collection is empty')
|
||||
|
||||
results = self.cloud_management.execute_on_cloud(ips, task)
|
||||
err = False
|
||||
for result in results:
|
||||
if result.status != executor.STATUS_OK:
|
||||
LOG.error(
|
||||
'Task {} failed on node {}'.format(task, result.host))
|
||||
err = True
|
||||
if err:
|
||||
raise error.ServiceError('Task failed on some nodes')
|
||||
return results
|
||||
return self.cloud_management.execute_on_cloud(nodes.hosts, task)
|
||||
|
||||
def discover_nodes(self):
|
||||
nodes = self.cloud_management.get_nodes()
|
||||
ips = nodes.get_ips()
|
||||
cmd = 'bash -c "ps ax | grep -v grep | grep \'{}\'"'.format(self.grep)
|
||||
results = self.cloud_management.execute_on_cloud(
|
||||
ips, {'command': cmd}, False)
|
||||
nodes.hosts, {'command': cmd}, False)
|
||||
success_ips = [r.host for r in results
|
||||
if r.status == executor.STATUS_OK]
|
||||
hosts = [h for h in nodes.hosts if h.ip in success_ips]
|
||||
|
|
|
@ -238,16 +238,17 @@ class DevStackManagement(cloud_management.CloudManagement,
|
|||
password=cloud_management_params.get('password'),
|
||||
become=False, serial=self.serial)
|
||||
|
||||
self.hosts = [self.address]
|
||||
self.hosts = [node_collection.Host(ip=self.address)]
|
||||
if self.slaves:
|
||||
self.hosts.extend(self.slaves)
|
||||
self.hosts.extend([node_collection.Host(ip=h)
|
||||
for h in self.slaves])
|
||||
self.nodes = None
|
||||
|
||||
def verify(self):
|
||||
"""Verify connection to the cloud."""
|
||||
nodes = self.get_nodes()
|
||||
task = {'shell': 'screen -ls | grep -P "\\d+\\.stack"'}
|
||||
results = self.execute_on_cloud(nodes.get_ips(), task)
|
||||
results = self.execute_on_cloud(nodes.hosts, task)
|
||||
hostnames = [result.host for result in results]
|
||||
LOG.debug('DevStack hostnames: %s', hostnames)
|
||||
LOG.info('Connected to cloud successfully')
|
||||
|
|
|
@ -31,7 +31,7 @@ class FuelNodeCollection(node_collection.NodeCollection):
|
|||
'network_name': network_name,
|
||||
'operation': 'up',
|
||||
}}
|
||||
self.cloud_management.execute_on_cloud(self.get_ips(), task)
|
||||
self.cloud_management.execute_on_cloud(self.hosts, task)
|
||||
|
||||
def disconnect(self, network_name):
|
||||
LOG.info("Disconnect network '%s' on nodes: %s",
|
||||
|
@ -40,7 +40,7 @@ class FuelNodeCollection(node_collection.NodeCollection):
|
|||
'network_name': network_name,
|
||||
'operation': 'down',
|
||||
}}
|
||||
self.cloud_management.execute_on_cloud(self.get_ips(), task)
|
||||
self.cloud_management.execute_on_cloud(self.hosts, task)
|
||||
|
||||
|
||||
class PcsService(service.ServiceAsProcess):
|
||||
|
@ -514,6 +514,7 @@ class FuelManagement(cloud_management.CloudManagement,
|
|||
self.node_discover = self # supports discovering
|
||||
|
||||
self.master_node_address = cloud_management_params['address']
|
||||
self._master_host = node_collection.Host(ip=self.master_node_address)
|
||||
self.username = cloud_management_params['username']
|
||||
self.private_key_file = cloud_management_params.get('private_key_file')
|
||||
self.slave_direct_ssh = cloud_management_params.get(
|
||||
|
@ -539,7 +540,7 @@ class FuelManagement(cloud_management.CloudManagement,
|
|||
LOG.debug('Cloud nodes: %s', nodes)
|
||||
|
||||
task = {'command': 'hostname'}
|
||||
task_result = self.execute_on_cloud(nodes.get_ips(), task)
|
||||
task_result = self.execute_on_cloud(nodes.hosts, task)
|
||||
LOG.debug('Hostnames of cloud nodes: %s',
|
||||
[r.payload['stdout'] for r in task_result])
|
||||
|
||||
|
@ -562,8 +563,7 @@ class FuelManagement(cloud_management.CloudManagement,
|
|||
:param task: Ansible task
|
||||
:return: Ansible execution result (list of records)
|
||||
"""
|
||||
return self.master_node_executor.execute(
|
||||
[self.master_node_address], task)
|
||||
return self.master_node_executor.execute([self._master_host], task)
|
||||
|
||||
def execute_on_cloud(self, hosts, task, raise_on_error=True):
|
||||
"""Execute task on specified hosts within the cloud.
|
||||
|
|
|
@ -17,6 +17,28 @@ from os_faults.api import node_discover
|
|||
from os_faults import utils
|
||||
|
||||
|
||||
AUTH_SCHEMA = {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'username': {'type': 'string'},
|
||||
'password': {'type': 'string'},
|
||||
'sudo': {'type': 'boolean'},
|
||||
'private_key_file': {'type': 'string'},
|
||||
'jump': {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'host': {'type': 'string'},
|
||||
'username': {'type': 'string'},
|
||||
'private_key_file': {'type': 'string'},
|
||||
},
|
||||
'required': ['host'],
|
||||
'additionalProperties': False,
|
||||
},
|
||||
},
|
||||
'additionalProperties': False,
|
||||
}
|
||||
|
||||
|
||||
class NodeListDiscover(node_discover.NodeDiscover):
|
||||
"""Node list.
|
||||
|
||||
|
@ -33,12 +55,37 @@ class NodeListDiscover(node_discover.NodeDiscover):
|
|||
mac: aa:bb:cc:dd:ee:01
|
||||
fqdn: node1.local
|
||||
libvirt_name: node1
|
||||
- ip: 10.0.0.52
|
||||
- ip: 192.168.1.50
|
||||
mac: aa:bb:cc:dd:ee:02
|
||||
fqdn: node2.local
|
||||
auth:
|
||||
username: user1
|
||||
password: secret1
|
||||
sudo: False
|
||||
jump:
|
||||
host: 10.0.0.52
|
||||
username: ubuntu
|
||||
private_key_file: /path/to/file
|
||||
- ip: 10.0.0.53
|
||||
mac: aa:bb:cc:dd:ee:03
|
||||
fqdn: node3.local
|
||||
|
||||
node parameters:
|
||||
|
||||
- **ip** - ip/host of the node
|
||||
- **mac** - MAC address of the node (optional).
|
||||
MAC address is used for libvirt driver.
|
||||
- **fqdn** - FQDN of the node (optional).
|
||||
FQDN is used for filtering only.
|
||||
- **auth** - SSH related parameters (optional):
|
||||
- **username** - SSH username (optional)
|
||||
- **password** - SSH password (optional)
|
||||
- **private_key_file** - SSH key file (optional)
|
||||
- **jump** - SSH proxy parameters (optional):
|
||||
- **host** - SSH proxy host
|
||||
- **username** - SSH proxy user
|
||||
- **private_key_file** - SSH proxy key file (optional)
|
||||
|
||||
"""
|
||||
|
||||
NAME = 'node_list'
|
||||
|
@ -50,12 +97,10 @@ class NodeListDiscover(node_discover.NodeDiscover):
|
|||
'type': 'object',
|
||||
'properties': {
|
||||
'ip': {'type': 'string'},
|
||||
'mac': {
|
||||
'type': 'string',
|
||||
'pattern': utils.MACADDR_REGEXP,
|
||||
},
|
||||
'mac': {'type': 'string', 'pattern': utils.MACADDR_REGEXP},
|
||||
'fqdn': {'type': 'string'},
|
||||
'libvirt_name': {'type': 'string'},
|
||||
'auth': AUTH_SCHEMA,
|
||||
},
|
||||
'required': ['ip'],
|
||||
'additionalProperties': False,
|
||||
|
|
|
@ -376,6 +376,7 @@ class TCPCloudManagement(cloud_management.CloudManagement,
|
|||
self.node_discover = self # supports discovering
|
||||
|
||||
self.master_node_address = cloud_management_params['address']
|
||||
self._master_host = node_collection.Host(ip=self.master_node_address)
|
||||
self.username = cloud_management_params['username']
|
||||
self.slave_username = cloud_management_params.get(
|
||||
'slave_username', self.username)
|
||||
|
@ -415,7 +416,7 @@ class TCPCloudManagement(cloud_management.CloudManagement,
|
|||
LOG.debug('Cloud nodes: %s', nodes)
|
||||
|
||||
task = {'command': 'hostname'}
|
||||
task_result = self.execute_on_cloud(nodes.get_ips(), task)
|
||||
task_result = self.execute_on_cloud(nodes.hosts, task)
|
||||
LOG.debug('Hostnames of cloud nodes: %s',
|
||||
[r.payload['stdout'] for r in task_result])
|
||||
|
||||
|
@ -459,8 +460,7 @@ class TCPCloudManagement(cloud_management.CloudManagement,
|
|||
:param task: Ansible task
|
||||
:return: Ansible execution result (list of records)
|
||||
"""
|
||||
return self.master_node_executor.execute(
|
||||
[self.master_node_address], task)
|
||||
return self.master_node_executor.execute([self._master_host], task)
|
||||
|
||||
def execute_on_cloud(self, hosts, task, raise_on_error=True):
|
||||
"""Execute task on specified hosts within the cloud.
|
||||
|
|
|
@ -15,6 +15,7 @@ import ddt
|
|||
import mock
|
||||
|
||||
from os_faults.ansible import executor
|
||||
from os_faults.api import node_collection
|
||||
from os_faults.tests.unit import test
|
||||
|
||||
|
||||
|
@ -185,23 +186,61 @@ class AnsibleRunnerTestCase(test.TestCase):
|
|||
@mock.patch.object(executor.task_queue_manager, 'TaskQueueManager')
|
||||
@mock.patch('ansible.playbook.play.Play.load')
|
||||
@mock.patch('ansible.inventory.Inventory')
|
||||
@mock.patch('ansible.vars.VariableManager.set_inventory')
|
||||
@mock.patch('os_faults.ansible.executor.VariableManager')
|
||||
@mock.patch('ansible.parsing.dataloader.DataLoader')
|
||||
def test__run_play(self, mock_dataloader, mock_vmanager, mock_inventory,
|
||||
mock_play_load, mock_taskqm):
|
||||
mock_play_load.return_value = 'my_load'
|
||||
variable_manager = mock_vmanager.return_value
|
||||
host_inst = mock_inventory.return_value.get_host.return_value
|
||||
host_vars = {
|
||||
'0.0.0.0': {
|
||||
'ansible_user': 'foo',
|
||||
'ansible_ssh_pass': 'bar',
|
||||
'ansible_become': True,
|
||||
'ansible_ssh_private_key_file': None,
|
||||
'ansible_ssh_common_args': '-o Option=yes',
|
||||
}
|
||||
}
|
||||
ex = executor.AnsibleRunner()
|
||||
ex._run_play({'hosts': ['0.0.0.0']})
|
||||
ex._run_play({'hosts': ['0.0.0.0']}, host_vars)
|
||||
|
||||
mock_taskqm.assert_called_once()
|
||||
self.assertEqual(mock_taskqm.mock_calls[1], mock.call().run('my_load'))
|
||||
self.assertEqual(mock_taskqm.mock_calls[2], mock.call().cleanup())
|
||||
|
||||
variable_manager.set_host_variable.assert_has_calls((
|
||||
mock.call(host_inst, 'ansible_user', 'foo'),
|
||||
mock.call(host_inst, 'ansible_ssh_pass', 'bar'),
|
||||
mock.call(host_inst, 'ansible_become', True),
|
||||
mock.call(host_inst, 'ansible_ssh_common_args', '-o Option=yes'),
|
||||
), any_order=True)
|
||||
|
||||
@mock.patch.object(executor.task_queue_manager, 'TaskQueueManager')
|
||||
@mock.patch('ansible.playbook.play.Play.load')
|
||||
@mock.patch('ansible.inventory.Inventory')
|
||||
@mock.patch('os_faults.ansible.executor.VariableManager')
|
||||
@mock.patch('ansible.parsing.dataloader.DataLoader')
|
||||
def test__run_play_no_host_vars(
|
||||
self, mock_dataloader, mock_vmanager, mock_inventory,
|
||||
mock_play_load, mock_taskqm):
|
||||
mock_play_load.return_value = 'my_load'
|
||||
variable_manager = mock_vmanager.return_value
|
||||
host_vars = {}
|
||||
ex = executor.AnsibleRunner()
|
||||
ex._run_play({'hosts': ['0.0.0.0']}, host_vars)
|
||||
|
||||
mock_taskqm.assert_called_once()
|
||||
self.assertEqual(mock_taskqm.mock_calls[1], mock.call().run('my_load'))
|
||||
self.assertEqual(mock_taskqm.mock_calls[2], mock.call().cleanup())
|
||||
|
||||
self.assertEqual(0, variable_manager.set_host_variable.call_count)
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner._run_play')
|
||||
def test_run_playbook(self, mock_run_play):
|
||||
ex = executor.AnsibleRunner()
|
||||
my_playbook = [{'gather_facts': 'yes'}, {'gather_facts': 'no'}]
|
||||
ex.run_playbook(my_playbook)
|
||||
ex.run_playbook(my_playbook, {})
|
||||
|
||||
self.assertEqual(my_playbook, [{'gather_facts': 'no'},
|
||||
{'gather_facts': 'no'}])
|
||||
|
@ -209,36 +248,79 @@ class AnsibleRunnerTestCase(test.TestCase):
|
|||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner.run_playbook')
|
||||
def test_execute(self, mock_run_playbook):
|
||||
my_hosts = ['0.0.0.0', '255.255.255.255']
|
||||
my_hosts = [node_collection.Host('0.0.0.0'),
|
||||
node_collection.Host('255.255.255.255')]
|
||||
my_tasks = 'my_task'
|
||||
ex = executor.AnsibleRunner()
|
||||
ex.execute(my_hosts, my_tasks)
|
||||
mock_run_playbook.assert_called_once_with(
|
||||
[{'tasks': ['my_task'],
|
||||
'hosts': ['0.0.0.0', '255.255.255.255'],
|
||||
'serial': 10}])
|
||||
'serial': 10}], {'0.0.0.0': {}, '255.255.255.255': {}})
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner.run_playbook')
|
||||
def test_execute_with_host_vars(self, mock_run_playbook):
|
||||
my_hosts = [
|
||||
node_collection.Host('0.0.0.0', auth={'username': 'foo',
|
||||
'password': 'bar',
|
||||
'sudo': True}),
|
||||
node_collection.Host('255.255.255.255',
|
||||
auth={'jump': {'host': '192.168.1.100',
|
||||
'username': 'foo'}})]
|
||||
my_tasks = 'my_task'
|
||||
ex = executor.AnsibleRunner()
|
||||
ex.execute(my_hosts, my_tasks)
|
||||
mock_run_playbook.assert_called_once_with(
|
||||
[{'tasks': ['my_task'],
|
||||
'hosts': ['0.0.0.0', '255.255.255.255'],
|
||||
'serial': 10}],
|
||||
{
|
||||
'0.0.0.0': {
|
||||
'ansible_user': 'foo',
|
||||
'ansible_ssh_pass': 'bar',
|
||||
'ansible_become': True,
|
||||
'ansible_ssh_private_key_file': None,
|
||||
'ansible_ssh_common_args': None,
|
||||
},
|
||||
'255.255.255.255': {
|
||||
'ansible_user': None,
|
||||
'ansible_ssh_pass': None,
|
||||
'ansible_become': None,
|
||||
'ansible_ssh_private_key_file': None,
|
||||
'ansible_ssh_common_args':
|
||||
'-o UserKnownHostsFile=/dev/null '
|
||||
'-o StrictHostKeyChecking=no '
|
||||
'-o ConnectTimeout=60 '
|
||||
'-o ProxyCommand="'
|
||||
'ssh -W %h:%p '
|
||||
'-o UserKnownHostsFile=/dev/null '
|
||||
'-o StrictHostKeyChecking=no '
|
||||
'-o ConnectTimeout=60 '
|
||||
'foo@192.168.1.100"'}})
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner.run_playbook')
|
||||
def test_execute_with_serial(self, mock_run_playbook):
|
||||
my_hosts = ['0.0.0.0', '255.255.255.255']
|
||||
my_hosts = [node_collection.Host('0.0.0.0'),
|
||||
node_collection.Host('255.255.255.255')]
|
||||
my_tasks = 'my_task'
|
||||
ex = executor.AnsibleRunner(serial=50)
|
||||
ex.execute(my_hosts, my_tasks)
|
||||
mock_run_playbook.assert_called_once_with(
|
||||
[{'tasks': ['my_task'],
|
||||
'hosts': ['0.0.0.0', '255.255.255.255'],
|
||||
'serial': 50}])
|
||||
'serial': 50}], {'0.0.0.0': {}, '255.255.255.255': {}})
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner.run_playbook')
|
||||
def test_execute_status_unreachable(self, mock_run_playbook):
|
||||
my_hosts = ['0.0.0.0', '255.255.255.255']
|
||||
my_hosts = [node_collection.Host('0.0.0.0'),
|
||||
node_collection.Host('255.255.255.255')]
|
||||
my_tasks = 'my_task'
|
||||
my_statuses = {executor.STATUS_FAILED,
|
||||
executor.STATUS_SKIPPED, executor.STATUS_UNREACHABLE}
|
||||
r0 = executor.AnsibleExecutionRecord(
|
||||
host=my_hosts[0], status=executor.STATUS_OK, task={}, payload={})
|
||||
host='0.0.0.0', status=executor.STATUS_OK, task={}, payload={})
|
||||
r1 = executor.AnsibleExecutionRecord(
|
||||
host=my_hosts[1], status=executor.STATUS_UNREACHABLE,
|
||||
host='255.255.255.255', status=executor.STATUS_UNREACHABLE,
|
||||
task={}, payload={})
|
||||
|
||||
mock_run_playbook.return_value = [r0, r1]
|
||||
|
@ -250,14 +332,15 @@ class AnsibleRunnerTestCase(test.TestCase):
|
|||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner.run_playbook')
|
||||
def test_execute_status_failed(self, mock_run_playbook):
|
||||
my_hosts = ['0.0.0.0', '255.255.255.255']
|
||||
my_hosts = [node_collection.Host('0.0.0.0'),
|
||||
node_collection.Host('255.255.255.255')]
|
||||
my_tasks = 'my_task'
|
||||
my_statuses = {executor.STATUS_OK, executor.STATUS_FAILED,
|
||||
executor.STATUS_SKIPPED, executor.STATUS_UNREACHABLE}
|
||||
r0 = executor.AnsibleExecutionRecord(
|
||||
host=my_hosts[0], status=executor.STATUS_OK, task={}, payload={})
|
||||
host='0.0.0.0', status=executor.STATUS_OK, task={}, payload={})
|
||||
r1 = executor.AnsibleExecutionRecord(
|
||||
host=my_hosts[1], status=executor.STATUS_UNREACHABLE,
|
||||
host='255.255.255.255', status=executor.STATUS_UNREACHABLE,
|
||||
task={}, payload={})
|
||||
|
||||
mock_run_playbook.return_value = [r0, r1]
|
||||
|
@ -279,7 +362,8 @@ class AnsibleRunnerTestCase(test.TestCase):
|
|||
mock_deepcopy.return_value = [result]
|
||||
log_result = mock_deepcopy.return_value[0]
|
||||
|
||||
my_hosts = ['0.0.0.0', '255.255.255.255']
|
||||
my_hosts = [node_collection.Host('0.0.0.0'),
|
||||
node_collection.Host('255.255.255.255')]
|
||||
my_tasks = 'my_task'
|
||||
ex = executor.AnsibleRunner()
|
||||
ex.execute(my_hosts, my_tasks)
|
||||
|
@ -298,12 +382,13 @@ class AnsibleRunnerTestCase(test.TestCase):
|
|||
task=task, payload={'foo': 'bar'})
|
||||
mock_run_playbook.return_value = [result]
|
||||
|
||||
hosts = [node_collection.Host('0.0.0.0')]
|
||||
ex = executor.AnsibleRunner()
|
||||
ex.execute([host], task)
|
||||
ex.execute(hosts, task)
|
||||
|
||||
mock_debug.assert_has_calls((
|
||||
mock.call('Executing task: %s on hosts: %s with serial: %s',
|
||||
task, [host], 10),
|
||||
task, hosts, 10),
|
||||
mock.call('Execution completed with 1 result(s):'),
|
||||
mock.call(result),
|
||||
))
|
||||
|
|
|
@ -163,8 +163,7 @@ class NodeCollectionTestCase(test.TestCase):
|
|||
expected_result = mock_execute_on_cloud.return_value
|
||||
self.assertIs(result, expected_result)
|
||||
mock_execute_on_cloud.assert_called_once_with(
|
||||
['10.0.0.2', '10.0.0.3', '10.0.0.4', '10.0.0.5'], {'foo': 'bar'},
|
||||
raise_on_error=False)
|
||||
self.hosts, {'foo': 'bar'}, raise_on_error=False)
|
||||
|
||||
def test_pick_count(self):
|
||||
two = self.node_collection.pick(count=2)
|
||||
|
@ -195,8 +194,7 @@ class NodeCollectionTestCase(test.TestCase):
|
|||
def test_reboot(self):
|
||||
self.node_collection.reboot()
|
||||
self.mock_cloud_management.execute_on_cloud.assert_called_once_with(
|
||||
['10.0.0.2', '10.0.0.3', '10.0.0.4', '10.0.0.5'],
|
||||
{'command': 'reboot now'})
|
||||
self.hosts, {'command': 'reboot now'})
|
||||
|
||||
def test_snapshot(self):
|
||||
self.node_collection.snapshot('foo')
|
||||
|
|
|
@ -48,12 +48,16 @@ class DevStackManagementTestCase(test.TestCase):
|
|||
def setUp(self):
|
||||
super(DevStackManagementTestCase, self).setUp()
|
||||
self.conf = {'address': '10.0.0.2', 'username': 'root'}
|
||||
self.host = node_collection.Host('10.0.0.2')
|
||||
self.discoverd_host = node_collection.Host(ip='10.0.0.2',
|
||||
mac='09:7b:74:90:63:c1',
|
||||
fqdn='')
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
|
||||
def test_verify(self, mock_ansible_runner):
|
||||
ansible_runner_inst = mock_ansible_runner.return_value
|
||||
ansible_runner_inst.execute.side_effect = [
|
||||
[fakes.FakeAnsibleResult(payload={'stdout': 'mac'},
|
||||
[fakes.FakeAnsibleResult(payload={'stdout': '09:7b:74:90:63:c1'},
|
||||
host='10.0.0.2')],
|
||||
[fakes.FakeAnsibleResult(payload={'stdout': ''},
|
||||
host='10.0.0.2')],
|
||||
|
@ -62,9 +66,9 @@ class DevStackManagementTestCase(test.TestCase):
|
|||
devstack_management.verify()
|
||||
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(['10.0.0.2'],
|
||||
mock.call([self.host],
|
||||
{'command': 'cat /sys/class/net/eth0/address'}),
|
||||
mock.call(['10.0.0.2'],
|
||||
mock.call([self.discoverd_host],
|
||||
{'shell': 'screen -ls | grep -P "\\d+\\.stack"'})
|
||||
])
|
||||
|
||||
|
@ -89,10 +93,20 @@ class DevStackManagementTestCase(test.TestCase):
|
|||
devstack_management = devstack.DevStackManagement(self.conf)
|
||||
devstack_management.verify()
|
||||
|
||||
hosts = [
|
||||
node_collection.Host('10.0.0.2'),
|
||||
node_collection.Host('10.0.0.3'),
|
||||
node_collection.Host('10.0.0.4')
|
||||
]
|
||||
discoverd_hosts = [
|
||||
node_collection.Host('10.0.0.2', mac='mac1', fqdn=''),
|
||||
node_collection.Host('10.0.0.3', mac='mac2', fqdn=''),
|
||||
node_collection.Host('10.0.0.4', mac='mac3', fqdn='')
|
||||
]
|
||||
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(['10.0.0.2', '10.0.0.3', '10.0.0.4'],
|
||||
{'command': 'cat /sys/class/net/eth0/address'}),
|
||||
mock.call(['10.0.0.2', '10.0.0.3', '10.0.0.4'],
|
||||
mock.call(hosts, {'command': 'cat /sys/class/net/eth0/address'}),
|
||||
mock.call(discoverd_hosts,
|
||||
{'shell': 'screen -ls | grep -P "\\d+\\.stack"'})
|
||||
])
|
||||
|
||||
|
@ -121,9 +135,8 @@ class DevStackManagementTestCase(test.TestCase):
|
|||
|
||||
devstack_management = devstack.DevStackManagement(self.conf)
|
||||
nodes = devstack_management.get_nodes()
|
||||
|
||||
ansible_runner_inst.execute.assert_called_once_with(
|
||||
['10.0.0.2'], {'command': 'cat /sys/class/net/eth0/address'})
|
||||
[self.host], {'command': 'cat /sys/class/net/eth0/address'})
|
||||
|
||||
self.assertIsInstance(nodes, devstack.DevStackNode)
|
||||
self.assertEqual(
|
||||
|
@ -144,13 +157,16 @@ class DevStackManagementTestCase(test.TestCase):
|
|||
fakes.FakeAnsibleResult(payload={'stdout': '09:7b:74:90:63:c3'},
|
||||
host='10.0.0.4')],
|
||||
]
|
||||
|
||||
hosts = [
|
||||
node_collection.Host('10.0.0.2'),
|
||||
node_collection.Host('10.0.0.3'),
|
||||
node_collection.Host('10.0.0.4')
|
||||
]
|
||||
devstack_management = devstack.DevStackManagement(self.conf)
|
||||
nodes = devstack_management.get_nodes()
|
||||
|
||||
ansible_runner_inst.execute.assert_called_once_with(
|
||||
['10.0.0.2', '10.0.0.3', '10.0.0.4'],
|
||||
{'command': 'cat /sys/class/net/eth1/address'})
|
||||
hosts, {'command': 'cat /sys/class/net/eth1/address'})
|
||||
|
||||
self.assertIsInstance(nodes, devstack.DevStackNode)
|
||||
self.assertEqual(
|
||||
|
@ -181,8 +197,8 @@ class DevStackManagementTestCase(test.TestCase):
|
|||
service.grep)
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(
|
||||
['10.0.0.2'], {'command': 'cat /sys/class/net/eth0/address'}),
|
||||
mock.call(['10.0.0.2'], {'command': cmd}, [])
|
||||
[self.host], {'command': 'cat /sys/class/net/eth0/address'}),
|
||||
mock.call([self.discoverd_host], {'command': cmd}, [])
|
||||
])
|
||||
self.assertEqual(
|
||||
[node_collection.Host(ip='10.0.0.2', mac='09:7b:74:90:63:c1',
|
||||
|
@ -200,6 +216,10 @@ class DevStackServiceTestCase(test.TestCase):
|
|||
def setUp(self):
|
||||
super(DevStackServiceTestCase, self).setUp()
|
||||
self.conf = {'address': '10.0.0.2', 'username': 'root'}
|
||||
self.host = node_collection.Host('10.0.0.2')
|
||||
self.discoverd_host = node_collection.Host(ip='10.0.0.2',
|
||||
mac='09:7b:74:90:63:c1',
|
||||
fqdn='')
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
|
||||
@ddt.data(*devstack.DevStackManagement.SERVICES.keys())
|
||||
|
@ -221,9 +241,9 @@ class DevStackServiceTestCase(test.TestCase):
|
|||
service.grep)
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(
|
||||
['10.0.0.2'], {'command': 'cat /sys/class/net/eth0/address'}),
|
||||
mock.call(['10.0.0.2'], {'command': cmd}, []),
|
||||
mock.call(['10.0.0.2'], {'shell': service.restart_cmd})
|
||||
[self.host], {'command': 'cat /sys/class/net/eth0/address'}),
|
||||
mock.call([self.discoverd_host], {'command': cmd}, []),
|
||||
mock.call([self.discoverd_host], {'shell': service.restart_cmd})
|
||||
])
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
|
||||
|
@ -246,9 +266,9 @@ class DevStackServiceTestCase(test.TestCase):
|
|||
service.grep)
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(
|
||||
['10.0.0.2'], {'command': 'cat /sys/class/net/eth0/address'}),
|
||||
mock.call(['10.0.0.2'], {'command': cmd}, []),
|
||||
mock.call(['10.0.0.2'], {'shell': service.terminate_cmd})
|
||||
[self.host], {'command': 'cat /sys/class/net/eth0/address'}),
|
||||
mock.call([self.discoverd_host], {'command': cmd}, []),
|
||||
mock.call([self.discoverd_host], {'shell': service.terminate_cmd})
|
||||
])
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
|
||||
|
@ -271,7 +291,7 @@ class DevStackServiceTestCase(test.TestCase):
|
|||
service.grep)
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(
|
||||
['10.0.0.2'], {'command': 'cat /sys/class/net/eth0/address'}),
|
||||
mock.call(['10.0.0.2'], {'command': cmd}, []),
|
||||
mock.call(['10.0.0.2'], {'shell': service.start_cmd})
|
||||
[self.host], {'command': 'cat /sys/class/net/eth0/address'}),
|
||||
mock.call([self.discoverd_host], {'command': cmd}, []),
|
||||
mock.call([self.discoverd_host], {'shell': service.start_cmd})
|
||||
])
|
||||
|
|
|
@ -27,6 +27,10 @@ class FuelManagementTestCase(test.TestCase):
|
|||
|
||||
def setUp(self):
|
||||
super(FuelManagementTestCase, self).setUp()
|
||||
self.conf = {
|
||||
'address': 'fuel.local',
|
||||
'username': 'root',
|
||||
}
|
||||
|
||||
self.fake_ansible_result = fakes.FakeAnsibleResult(
|
||||
payload={
|
||||
|
@ -34,6 +38,12 @@ class FuelManagementTestCase(test.TestCase):
|
|||
' {"ip": "10.0.0.3", "mac": "03", "fqdn": "node-3"}]'
|
||||
})
|
||||
|
||||
self.master_host = node_collection.Host('fuel.local')
|
||||
self.hosts = [
|
||||
node_collection.Host(ip='10.0.0.2', mac='02', fqdn='node-2'),
|
||||
node_collection.Host(ip='10.0.0.3', mac='03', fqdn='node-3'),
|
||||
]
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
|
||||
@ddt.data((
|
||||
dict(address='fuel.local', username='root'),
|
||||
|
@ -65,36 +75,26 @@ class FuelManagementTestCase(test.TestCase):
|
|||
[fakes.FakeAnsibleResult(payload={'stdout': ''}),
|
||||
fakes.FakeAnsibleResult(payload={'stdout': ''})],
|
||||
]
|
||||
fuel_managment = fuel.FuelManagement({
|
||||
'address': 'fuel.local',
|
||||
'username': 'root',
|
||||
})
|
||||
fuel_managment = fuel.FuelManagement(self.conf)
|
||||
fuel_managment.verify()
|
||||
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(['fuel.local'], {'command': 'fuel node --json'}),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'], {'command': 'hostname'}),
|
||||
mock.call([self.master_host], {'command': 'fuel node --json'}),
|
||||
mock.call(self.hosts, {'command': 'hostname'}),
|
||||
])
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
|
||||
def test_get_nodes(self, mock_ansible_runner):
|
||||
ansible_runner_inst = mock_ansible_runner.return_value
|
||||
ansible_runner_inst.execute.side_effect = [[self.fake_ansible_result]]
|
||||
fuel_managment = fuel.FuelManagement({
|
||||
'address': 'fuel.local',
|
||||
'username': 'root',
|
||||
})
|
||||
fuel_managment = fuel.FuelManagement(self.conf)
|
||||
nodes = fuel_managment.get_nodes()
|
||||
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(['fuel.local'], {'command': 'fuel node --json'}),
|
||||
mock.call([self.master_host], {'command': 'fuel node --json'}),
|
||||
])
|
||||
|
||||
hosts = [
|
||||
node_collection.Host(ip='10.0.0.2', mac='02', fqdn='node-2'),
|
||||
node_collection.Host(ip='10.0.0.3', mac='03', fqdn='node-3'),
|
||||
]
|
||||
self.assertEqual(nodes.hosts, hosts)
|
||||
self.assertEqual(nodes.hosts, self.hosts)
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
|
||||
def test_get_nodes_from_discover_driver(self, mock_ansible_runner):
|
||||
|
@ -107,10 +107,7 @@ class FuelManagementTestCase(test.TestCase):
|
|||
]
|
||||
node_discover_driver = mock.Mock()
|
||||
node_discover_driver.discover_hosts.return_value = hosts
|
||||
fuel_managment = fuel.FuelManagement({
|
||||
'address': 'fuel.local',
|
||||
'username': 'root',
|
||||
})
|
||||
fuel_managment = fuel.FuelManagement(self.conf)
|
||||
fuel_managment.set_node_discover(node_discover_driver)
|
||||
nodes = fuel_managment.get_nodes()
|
||||
|
||||
|
@ -125,17 +122,14 @@ class FuelManagementTestCase(test.TestCase):
|
|||
[fakes.FakeAnsibleResult(payload={'stdout': ''}),
|
||||
fakes.FakeAnsibleResult(payload={'stdout': ''})]
|
||||
]
|
||||
fuel_managment = fuel.FuelManagement({
|
||||
'address': 'fuel.local',
|
||||
'username': 'root',
|
||||
})
|
||||
fuel_managment = fuel.FuelManagement(self.conf)
|
||||
nodes = fuel_managment.get_nodes()
|
||||
result = fuel_managment.execute_on_cloud(
|
||||
nodes.get_ips(), {'command': 'mycmd'}, raise_on_error=False)
|
||||
nodes.hosts, {'command': 'mycmd'}, raise_on_error=False)
|
||||
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(['fuel.local'], {'command': 'fuel node --json'}),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'], {'command': 'mycmd'}, []),
|
||||
mock.call([self.master_host], {'command': 'fuel node --json'}),
|
||||
mock.call(self.hosts, {'command': 'mycmd'}, []),
|
||||
])
|
||||
|
||||
self.assertEqual(result,
|
||||
|
@ -146,10 +140,7 @@ class FuelManagementTestCase(test.TestCase):
|
|||
def test_get_nodes_fqdns(self, mock_ansible_runner):
|
||||
ansible_runner_inst = mock_ansible_runner.return_value
|
||||
ansible_runner_inst.execute.side_effect = [[self.fake_ansible_result]]
|
||||
fuel_managment = fuel.FuelManagement({
|
||||
'address': 'fuel.local',
|
||||
'username': 'root',
|
||||
})
|
||||
fuel_managment = fuel.FuelManagement(self.conf)
|
||||
nodes = fuel_managment.get_nodes(fqdns=['node-3'])
|
||||
|
||||
hosts = [
|
||||
|
@ -170,10 +161,7 @@ class FuelManagementTestCase(test.TestCase):
|
|||
host='10.0.0.3')]
|
||||
]
|
||||
|
||||
fuel_managment = fuel.FuelManagement({
|
||||
'address': 'fuel.local',
|
||||
'username': 'root',
|
||||
})
|
||||
fuel_managment = fuel.FuelManagement(self.conf)
|
||||
|
||||
service = fuel_managment.get_service(service_name)
|
||||
|
||||
|
@ -181,27 +169,17 @@ class FuelManagementTestCase(test.TestCase):
|
|||
cmd = 'bash -c "ps ax | grep -v grep | grep \'{}\'"'.format(
|
||||
service.grep)
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(['fuel.local'], {'command': 'fuel node --json'}),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
{'command': cmd}, []),
|
||||
mock.call([self.master_host], {'command': 'fuel node --json'}),
|
||||
mock.call(self.hosts, {'command': cmd}, []),
|
||||
])
|
||||
|
||||
hosts = [
|
||||
node_collection.Host(ip='10.0.0.3', mac='03', fqdn='node-3'),
|
||||
]
|
||||
self.assertEqual(nodes.hosts, hosts)
|
||||
self.assertEqual(nodes.hosts, [self.hosts[1]])
|
||||
|
||||
def test_get_unknown_service(self):
|
||||
fuel_managment = fuel.FuelManagement({
|
||||
'address': 'fuel.local',
|
||||
'username': 'root',
|
||||
})
|
||||
fuel_managment = fuel.FuelManagement(self.conf)
|
||||
self.assertRaises(error.ServiceError,
|
||||
fuel_managment.get_service, 'unknown')
|
||||
|
||||
def test_validate_services(self):
|
||||
fuel_managment = fuel.FuelManagement({
|
||||
'address': 'fuel.local',
|
||||
'username': 'root',
|
||||
})
|
||||
fuel_managment = fuel.FuelManagement(self.conf)
|
||||
fuel_managment.validate_services()
|
||||
|
|
|
@ -43,13 +43,11 @@ class FuelNodeCollectionTestCase(test.TestCase):
|
|||
def test_connect(self):
|
||||
self.node_collection.connect(network_name='storage')
|
||||
self.mock_cloud_management.execute_on_cloud.assert_called_once_with(
|
||||
['10.0.0.2', '10.0.0.3', '10.0.0.4', '10.0.0.5'],
|
||||
{'fuel_network_mgmt': {'operation': 'up',
|
||||
'network_name': 'storage'}})
|
||||
self.hosts, {'fuel_network_mgmt': {'operation': 'up',
|
||||
'network_name': 'storage'}})
|
||||
|
||||
def test_disconnect(self):
|
||||
self.node_collection.disconnect(network_name='storage')
|
||||
self.mock_cloud_management.execute_on_cloud.assert_called_once_with(
|
||||
['10.0.0.2', '10.0.0.3', '10.0.0.4', '10.0.0.5'],
|
||||
{'fuel_network_mgmt': {'operation': 'down',
|
||||
'network_name': 'storage'}})
|
||||
self.hosts, {'fuel_network_mgmt': {'operation': 'down',
|
||||
'network_name': 'storage'}})
|
||||
|
|
|
@ -16,6 +16,7 @@ import mock
|
|||
|
||||
from os_faults.ansible import executor
|
||||
from os_faults.api import error
|
||||
from os_faults.api import node_collection
|
||||
from os_faults.drivers import fuel
|
||||
from os_faults.tests.unit import fakes
|
||||
from os_faults.tests.unit import test
|
||||
|
@ -32,6 +33,11 @@ class FuelServiceTestCase(test.TestCase):
|
|||
'stdout': '[{"ip": "10.0.0.2", "mac": "02", "fqdn": "node-2"},'
|
||||
' {"ip": "10.0.0.3", "mac": "03", "fqdn": "node-3"}]'
|
||||
})
|
||||
self.master_host = node_collection.Host('fuel.local')
|
||||
self.hosts = [
|
||||
node_collection.Host(ip='10.0.0.2', mac='02', fqdn='node-2'),
|
||||
node_collection.Host(ip='10.0.0.3', mac='03', fqdn='node-3'),
|
||||
]
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
|
||||
@ddt.data(*fuel.FuelManagement.SERVICES.keys())
|
||||
|
@ -57,11 +63,9 @@ class FuelServiceTestCase(test.TestCase):
|
|||
get_nodes_cmd = 'bash -c "ps ax | grep -v grep | grep \'{}\'"'.format(
|
||||
service.grep)
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(['fuel.local'], {'command': 'fuel node --json'}),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
{'command': get_nodes_cmd}, []),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
{'kill': {'grep': service.grep, 'sig': 9}}),
|
||||
mock.call([self.master_host], {'command': 'fuel node --json'}),
|
||||
mock.call(self.hosts, {'command': get_nodes_cmd}, []),
|
||||
mock.call(self.hosts, {'kill': {'grep': service.grep, 'sig': 9}}),
|
||||
])
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
|
||||
|
@ -88,11 +92,9 @@ class FuelServiceTestCase(test.TestCase):
|
|||
get_nodes_cmd = 'bash -c "ps ax | grep -v grep | grep \'{}\'"'.format(
|
||||
service.grep)
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(['fuel.local'], {'command': 'fuel node --json'}),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
{'command': get_nodes_cmd}, []),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
{'kill': {'grep': service.grep, 'sig': 19}}),
|
||||
mock.call([self.master_host], {'command': 'fuel node --json'}),
|
||||
mock.call(self.hosts, {'command': get_nodes_cmd}, []),
|
||||
mock.call(self.hosts, {'kill': {'grep': service.grep, 'sig': 19}}),
|
||||
])
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
|
||||
|
@ -120,12 +122,10 @@ class FuelServiceTestCase(test.TestCase):
|
|||
get_nodes_cmd = 'bash -c "ps ax | grep -v grep | grep \'{}\'"'.format(
|
||||
service.grep)
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(['fuel.local'], {'command': 'fuel node --json'}),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
{'command': get_nodes_cmd}, []),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
{'freeze': {'grep': service.grep,
|
||||
'sec': delay_sec}}),
|
||||
mock.call([self.master_host], {'command': 'fuel node --json'}),
|
||||
mock.call(self.hosts, {'command': get_nodes_cmd}, []),
|
||||
mock.call(self.hosts, {'freeze': {'grep': service.grep,
|
||||
'sec': delay_sec}}),
|
||||
])
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
|
||||
|
@ -152,11 +152,9 @@ class FuelServiceTestCase(test.TestCase):
|
|||
get_nodes_cmd = 'bash -c "ps ax | grep -v grep | grep \'{}\'"'.format(
|
||||
service.grep)
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(['fuel.local'], {'command': 'fuel node --json'}),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
{'command': get_nodes_cmd}, []),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
{'kill': {'grep': service.grep, 'sig': 18}}),
|
||||
mock.call([self.master_host], {'command': 'fuel node --json'}),
|
||||
mock.call(self.hosts, {'command': get_nodes_cmd}, []),
|
||||
mock.call(self.hosts, {'kill': {'grep': service.grep, 'sig': 18}}),
|
||||
])
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
|
||||
|
@ -183,10 +181,9 @@ class FuelServiceTestCase(test.TestCase):
|
|||
get_nodes_cmd = 'bash -c "ps ax | grep -v grep | grep \'{}\'"'.format(
|
||||
service.grep)
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(['fuel.local'], {'command': 'fuel node --json'}),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
{'command': get_nodes_cmd}, []),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
mock.call([self.master_host], {'command': 'fuel node --json'}),
|
||||
mock.call(self.hosts, {'command': get_nodes_cmd}, []),
|
||||
mock.call(self.hosts,
|
||||
{'iptables': {'protocol': service.port[0],
|
||||
'port': service.port[1],
|
||||
'action': 'block',
|
||||
|
@ -217,10 +214,9 @@ class FuelServiceTestCase(test.TestCase):
|
|||
get_nodes_cmd = 'bash -c "ps ax | grep -v grep | grep \'{}\'"'.format(
|
||||
service.grep)
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(['fuel.local'], {'command': 'fuel node --json'}),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
{'command': get_nodes_cmd}, []),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
mock.call([self.master_host], {'command': 'fuel node --json'}),
|
||||
mock.call(self.hosts, {'command': get_nodes_cmd}, []),
|
||||
mock.call(self.hosts,
|
||||
{'iptables': {'protocol': service.port[0],
|
||||
'port': service.port[1],
|
||||
'action': 'unblock',
|
||||
|
@ -251,11 +247,9 @@ class FuelServiceTestCase(test.TestCase):
|
|||
get_nodes_cmd = 'bash -c "ps ax | grep -v grep | grep \'{}\'"'.format(
|
||||
service.grep)
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(['fuel.local'], {'command': 'fuel node --json'}),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
{'command': get_nodes_cmd}, []),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
{'shell': service.restart_cmd}),
|
||||
mock.call([self.master_host], {'command': 'fuel node --json'}),
|
||||
mock.call(self.hosts, {'command': get_nodes_cmd}, []),
|
||||
mock.call(self.hosts, {'shell': service.restart_cmd}),
|
||||
])
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
|
||||
|
@ -282,11 +276,9 @@ class FuelServiceTestCase(test.TestCase):
|
|||
get_nodes_cmd = 'bash -c "ps ax | grep -v grep | grep \'{}\'"'.format(
|
||||
service.grep)
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(['fuel.local'], {'command': 'fuel node --json'}),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
{'command': get_nodes_cmd}, []),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
{'shell': service.terminate_cmd}),
|
||||
mock.call([self.master_host], {'command': 'fuel node --json'}),
|
||||
mock.call(self.hosts, {'command': get_nodes_cmd}, []),
|
||||
mock.call(self.hosts, {'shell': service.terminate_cmd}),
|
||||
])
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
|
||||
|
@ -313,43 +305,9 @@ class FuelServiceTestCase(test.TestCase):
|
|||
get_nodes_cmd = 'bash -c "ps ax | grep -v grep | grep \'{}\'"'.format(
|
||||
service.grep)
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(['fuel.local'], {'command': 'fuel node --json'}),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
{'command': get_nodes_cmd}, []),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
{'shell': service.start_cmd}),
|
||||
])
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
|
||||
def test_run_task_error(self, mock_ansible_runner):
|
||||
ansible_runner_inst = mock_ansible_runner.return_value
|
||||
ansible_runner_inst.execute.side_effect = [
|
||||
[self.fake_ansible_result],
|
||||
[fakes.FakeAnsibleResult(payload={'stdout': ''},
|
||||
host='10.0.0.2'),
|
||||
fakes.FakeAnsibleResult(payload={'stdout': ''},
|
||||
host='10.0.0.3')],
|
||||
[fakes.FakeAnsibleResult(payload={'stdout': ''},
|
||||
host='10.0.0.2',
|
||||
status=executor.STATUS_FAILED),
|
||||
fakes.FakeAnsibleResult(payload={'stdout': ''},
|
||||
host='10.0.0.3')]
|
||||
]
|
||||
|
||||
fuel_managment = fuel.FuelManagement(self.conf)
|
||||
|
||||
service = fuel_managment.get_service('keystone')
|
||||
exception = self.assertRaises(error.ServiceError, service.restart)
|
||||
self.assertEqual('Task failed on some nodes', str(exception))
|
||||
|
||||
get_nodes_cmd = 'bash -c "ps ax | grep -v grep | grep \'{}\'"'.format(
|
||||
service.grep)
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(['fuel.local'], {'command': 'fuel node --json'}),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
{'command': get_nodes_cmd}, []),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
{'shell': service.restart_cmd}),
|
||||
mock.call([self.master_host], {'command': 'fuel node --json'}),
|
||||
mock.call(self.hosts, {'command': get_nodes_cmd}, []),
|
||||
mock.call(self.hosts, {'shell': service.start_cmd}),
|
||||
])
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
|
||||
|
@ -374,7 +332,6 @@ class FuelServiceTestCase(test.TestCase):
|
|||
get_nodes_cmd = 'bash -c "ps ax | grep -v grep | grep \'{}\'"'.format(
|
||||
service.grep)
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(['fuel.local'], {'command': 'fuel node --json'}),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
{'command': get_nodes_cmd}, []),
|
||||
mock.call([self.master_host], {'command': 'fuel node --json'}),
|
||||
mock.call(self.hosts, {'command': get_nodes_cmd}, []),
|
||||
])
|
||||
|
|
|
@ -64,6 +64,14 @@ class TCPCloudManagementTestCase(test.TestCase):
|
|||
self.get_ips_cmd = ("salt -E '^(?!cfg|mon)' "
|
||||
"pillar.get _param:single_address --out=yaml")
|
||||
|
||||
self.master_host = node_collection.Host('tcp.local')
|
||||
self.hosts = [
|
||||
node_collection.Host(ip='10.0.0.2', mac='09:7b:74:90:63:c2',
|
||||
fqdn='cmp01.mk20.local'),
|
||||
node_collection.Host(ip='10.0.0.3', mac='09:7b:74:90:63:c3',
|
||||
fqdn='cmp02.mk20.local'),
|
||||
]
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
|
||||
@ddt.data((
|
||||
dict(address='tcp.local', username='root'),
|
||||
|
@ -143,9 +151,9 @@ class TCPCloudManagementTestCase(test.TestCase):
|
|||
get_ips_cmd = ("salt -E '(ctl*|cmp*)' "
|
||||
"pillar.get _param:single_address --out=yaml")
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(['tcp.local'], {'command': get_nodes_cmd}),
|
||||
mock.call(['tcp.local'], {'command': get_ips_cmd}),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'], {'command': 'hostname'}),
|
||||
mock.call([self.master_host], {'command': get_nodes_cmd}),
|
||||
mock.call([self.master_host], {'command': get_ips_cmd}),
|
||||
mock.call(self.hosts, {'command': 'hostname'}),
|
||||
])
|
||||
|
||||
def test_validate_services(self):
|
||||
|
@ -163,17 +171,11 @@ class TCPCloudManagementTestCase(test.TestCase):
|
|||
nodes = tcp_managment.get_nodes()
|
||||
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(['tcp.local'], {'command': self.get_nodes_cmd}),
|
||||
mock.call(['tcp.local'], {'command': self.get_ips_cmd}),
|
||||
mock.call([self.master_host], {'command': self.get_nodes_cmd}),
|
||||
mock.call([self.master_host], {'command': self.get_ips_cmd}),
|
||||
])
|
||||
|
||||
hosts = [
|
||||
node_collection.Host(ip='10.0.0.2', mac='09:7b:74:90:63:c2',
|
||||
fqdn='cmp01.mk20.local'),
|
||||
node_collection.Host(ip='10.0.0.3', mac='09:7b:74:90:63:c3',
|
||||
fqdn='cmp02.mk20.local'),
|
||||
]
|
||||
self.assertEqual(nodes.hosts, hosts)
|
||||
self.assertEqual(nodes.hosts, self.hosts)
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
|
||||
def test_get_nodes_from_discover_driver(self, mock_ansible_runner):
|
||||
|
@ -205,12 +207,12 @@ class TCPCloudManagementTestCase(test.TestCase):
|
|||
tcp_managment = tcpcloud.TCPCloudManagement(self.tcp_conf)
|
||||
nodes = tcp_managment.get_nodes()
|
||||
result = tcp_managment.execute_on_cloud(
|
||||
nodes.get_ips(), {'command': 'mycmd'}, raise_on_error=False)
|
||||
nodes.hosts, {'command': 'mycmd'}, raise_on_error=False)
|
||||
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(['tcp.local'], {'command': self.get_nodes_cmd}),
|
||||
mock.call(['tcp.local'], {'command': self.get_ips_cmd}),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'], {'command': 'mycmd'}, []),
|
||||
mock.call([self.master_host], {'command': self.get_nodes_cmd}),
|
||||
mock.call([self.master_host], {'command': self.get_ips_cmd}),
|
||||
mock.call(self.hosts, {'command': 'mycmd'}, []),
|
||||
])
|
||||
|
||||
self.assertEqual(result,
|
||||
|
@ -227,11 +229,7 @@ class TCPCloudManagementTestCase(test.TestCase):
|
|||
tcp_managment = tcpcloud.TCPCloudManagement(self.tcp_conf)
|
||||
nodes = tcp_managment.get_nodes(fqdns=['cmp02.mk20.local'])
|
||||
|
||||
hosts = [
|
||||
node_collection.Host(ip='10.0.0.3', mac='09:7b:74:90:63:c3',
|
||||
fqdn='cmp02.mk20.local'),
|
||||
]
|
||||
self.assertEqual(nodes.hosts, hosts)
|
||||
self.assertEqual(nodes.hosts, [self.hosts[1]])
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
|
||||
@ddt.data(*tcpcloud.TCPCloudManagement.SERVICES.keys())
|
||||
|
@ -254,17 +252,12 @@ class TCPCloudManagementTestCase(test.TestCase):
|
|||
cmd = 'bash -c "ps ax | grep -v grep | grep \'{}\'"'.format(
|
||||
service.grep)
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(['tcp.local'], {'command': self.get_nodes_cmd}),
|
||||
mock.call(['tcp.local'], {'command': self.get_ips_cmd}),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
{'command': cmd}, []),
|
||||
mock.call([self.master_host], {'command': self.get_nodes_cmd}),
|
||||
mock.call([self.master_host], {'command': self.get_ips_cmd}),
|
||||
mock.call(self.hosts, {'command': cmd}, []),
|
||||
])
|
||||
|
||||
hosts = [
|
||||
node_collection.Host(ip='10.0.0.3', mac='09:7b:74:90:63:c3',
|
||||
fqdn='cmp02.mk20.local'),
|
||||
]
|
||||
self.assertEqual(nodes.hosts, hosts)
|
||||
self.assertEqual(nodes.hosts, [self.hosts[1]])
|
||||
|
||||
|
||||
@ddt.ddt
|
||||
|
@ -302,6 +295,14 @@ class TcpServiceTestCase(test.TestCase):
|
|||
self.get_ips_cmd = ("salt -E '^(?!cfg|mon)' "
|
||||
"pillar.get _param:single_address --out=yaml")
|
||||
|
||||
self.master_host = node_collection.Host('tcp.local')
|
||||
self.hosts = [
|
||||
node_collection.Host(ip='10.0.0.2', mac='09:7b:74:90:63:c2',
|
||||
fqdn='cmp01.mk20.local'),
|
||||
node_collection.Host(ip='10.0.0.3', mac='09:7b:74:90:63:c3',
|
||||
fqdn='cmp02.mk20.local'),
|
||||
]
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
|
||||
@ddt.data(*tcpcloud.TCPCloudManagement.SERVICES.keys())
|
||||
def test_restart(self, service_name, mock_ansible_runner):
|
||||
|
@ -328,11 +329,11 @@ class TcpServiceTestCase(test.TestCase):
|
|||
cmd = 'bash -c "ps ax | grep -v grep | grep \'{}\'"'.format(
|
||||
service.grep)
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(['tcp.local'], {'command': self.get_nodes_cmd}),
|
||||
mock.call(['tcp.local'], {'command': self.get_ips_cmd}),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
mock.call([self.master_host], {'command': self.get_nodes_cmd}),
|
||||
mock.call([self.master_host], {'command': self.get_ips_cmd}),
|
||||
mock.call(self.hosts,
|
||||
{'command': cmd}, []),
|
||||
mock.call(['10.0.0.3'], {'shell': service.restart_cmd}),
|
||||
mock.call([self.hosts[1]], {'shell': service.restart_cmd}),
|
||||
])
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
|
||||
|
@ -361,11 +362,11 @@ class TcpServiceTestCase(test.TestCase):
|
|||
cmd = 'bash -c "ps ax | grep -v grep | grep \'{}\'"'.format(
|
||||
service.grep)
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(['tcp.local'], {'command': self.get_nodes_cmd}),
|
||||
mock.call(['tcp.local'], {'command': self.get_ips_cmd}),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
mock.call([self.master_host], {'command': self.get_nodes_cmd}),
|
||||
mock.call([self.master_host], {'command': self.get_ips_cmd}),
|
||||
mock.call(self.hosts,
|
||||
{'command': cmd}, []),
|
||||
mock.call(['10.0.0.3'], {'shell': service.terminate_cmd}),
|
||||
mock.call([self.hosts[1]], {'shell': service.terminate_cmd}),
|
||||
])
|
||||
|
||||
@mock.patch('os_faults.ansible.executor.AnsibleRunner', autospec=True)
|
||||
|
@ -394,9 +395,9 @@ class TcpServiceTestCase(test.TestCase):
|
|||
cmd = 'bash -c "ps ax | grep -v grep | grep \'{}\'"'.format(
|
||||
service.grep)
|
||||
ansible_runner_inst.execute.assert_has_calls([
|
||||
mock.call(['tcp.local'], {'command': self.get_nodes_cmd}),
|
||||
mock.call(['tcp.local'], {'command': self.get_ips_cmd}),
|
||||
mock.call(['10.0.0.2', '10.0.0.3'],
|
||||
mock.call([self.master_host], {'command': self.get_nodes_cmd}),
|
||||
mock.call([self.master_host], {'command': self.get_ips_cmd}),
|
||||
mock.call(self.hosts,
|
||||
{'command': cmd}, []),
|
||||
mock.call(['10.0.0.3'], {'shell': service.start_cmd}),
|
||||
mock.call([self.hosts[1]], {'shell': service.start_cmd}),
|
||||
])
|
||||
|
|
Loading…
Reference in New Issue