def setUp(self):
        self.process_mock = Mock()
        self.stdout_mock = mock_enter_exit_self()
        self.stderr_mock = mock_enter_exit_self()
        self.convert_mock = Mock()
        self.sleep_mock = Mock()
        self.output_writer_mock = Mock()

        self.popen_patcher = patch(
            'cloudshell.cm.ansible.domain.ansible_command_executor.Popen')
        self.stdout_patcher = patch(
            'cloudshell.cm.ansible.domain.ansible_command_executor.StdoutAccumulator'
        )
        self.stderr_patcher = patch(
            'cloudshell.cm.ansible.domain.ansible_command_executor.StderrAccumulator'
        )
        self.convert_patcher = patch(
            'cloudshell.cm.ansible.domain.ansible_command_executor.UnixToHtmlColorConverter.convert'
        )
        self.sleep_patcher = patch(
            'cloudshell.cm.ansible.domain.ansible_command_executor.time.sleep')

        self.popen_patcher.start().return_value = self.process_mock
        self.stdout_patcher.start().return_value = self.stdout_mock
        self.stderr_patcher.start().return_value = self.stderr_mock
        self.convert_mock = self.convert_patcher.start()
        self.sleep_mock = self.sleep_patcher.start()

        self.convert_mock.side_effect = (lambda x: x)
        self.sleep_mock.side_effect = (lambda x: 0)

        self.executor = AnsibleCommandExecutor()
Ejemplo n.º 2
0
    def test_ansible_config_file(self):
        with patch('cloudshell.cm.ansible.ansible_shell.AnsibleConfigFile'
                   ) as file:
            m = mock_enter_exit_self()
            file.return_value = m

            self._execute_playbook()

            m.ignore_ssh_key_checking.assert_called_once()
            m.force_color.assert_called_once()
            m.set_retry_path.assert_called_once_with("." + os.pathsep)
Ejemplo n.º 3
0
    def test_host_vars_file_with_custom_vars(self):
        with patch('cloudshell.cm.ansible.ansible_shell.HostVarsFile') as file:
            m = mock_enter_exit_self()
            file.return_value = m
            host1 = HostConfiguration()
            host1.ip = 'host1'
            host1.connection_method = AnsibleConnectionHelper.CONNECTION_METHOD_WIN_RM
            self.conf.hosts_conf.append(host1)

            self._execute_playbook()

            m.add_vars.assert_called_once()
Ejemplo n.º 4
0
    def test_host_vars_file_with_ssh(self):
        with patch('cloudshell.cm.ansible.ansible_shell.HostVarsFile') as file:
            m = mock_enter_exit_self()
            file.return_value = m
            host1 = HostConfiguration()
            host1.ip = 'host1'
            host1.connection_method = 'ssh'
            self.conf.hosts_conf.append(host1)

            self._execute_playbook()

            m.add_connection_type.assert_called_once_with('ssh')
Ejemplo n.º 5
0
    def test_host_vars_file_with_winrm_http(self):
        with patch('cloudshell.cm.ansible.ansible_shell.HostVarsFile') as file:
            m = mock_enter_exit_self()
            file.return_value = m
            host1 = HostConfiguration()
            host1.ip = 'host1'
            host1.connection_method = 'winrm'
            host1.connection_secured = True
            self.conf.hosts_conf.append(host1)

            self._execute_playbook()

            m.add_connection_type.assert_called_once_with('winrm')
            m.add_port.assert_called_once_with('5986')
            m.add_ignore_winrm_cert_validation.assert_called_once()
Ejemplo n.º 6
0
    def test_host_vars_file_with_username_and_password(self):
        with patch('cloudshell.cm.ansible.ansible_shell.HostVarsFile') as file:
            m = mock_enter_exit_self()
            file.return_value = m
            host1 = HostConfiguration()
            host1.ip = 'host1'
            host1.connection_method = AnsibleConnectionHelper.CONNECTION_METHOD_WIN_RM
            host1.username = '******'
            host1.password = '******'
            self.conf.hosts_conf.append(host1)

            self._execute_playbook()

            m.add_conn_file.assert_not_called()
            m.add_username.assert_called_once_with('admin')
            m.add_password.assert_called_once_with('1234')
Ejemplo n.º 7
0
    def test_host_vars_file_with_access_key(self):
        with patch('cloudshell.cm.ansible.ansible_shell.HostVarsFile') as file:
            m = mock_enter_exit_self()
            file.return_value = m
            host1 = HostConfiguration()
            host1.ip = 'host1'
            host1.access_key = 'data1234'
            self.conf.hosts_conf.append(host1)
            host1.connection_method = AnsibleConnectionHelper.CONNECTION_METHOD_WIN_RM
            self._execute_playbook()

            self.file_system.create_file.assert_any_call(
                'host1_access_key.pem', 0400)
            m.add_conn_file.assert_called_once_with('host1_access_key.pem')
            m.add_username.assert_called_once()
            m.add_password.assert_not_called()
Ejemplo n.º 8
0
    def test_inventory_file(self):
        with patch(
                'cloudshell.cm.ansible.ansible_shell.InventoryFile') as file:
            m = mock_enter_exit_self()
            file.return_value = m
            host1 = HostConfiguration()
            host1.ip = 'host1'
            host1.groups = ['group1']
            host1.connection_method = AnsibleConnectionHelper.CONNECTION_METHOD_SSH
            host2 = HostConfiguration()
            host2.ip = 'host2'
            host2.connection_method = AnsibleConnectionHelper.CONNECTION_METHOD_WIN_RM
            host2.groups = ['group2']
            self.conf.hosts_conf.append(host1)
            self.conf.hosts_conf.append(host2)

            self._execute_playbook()

            m.add_host_and_groups.assert_any_call('host1', ['group1'])
            m.add_host_and_groups.assert_any_call('host2', ['group2'])