def test_utf8_output(self): self.cmd_out[sentinel.stdout] = BytesIO(u'Žarn§'.encode('utf-8')) self.cmd_out[sentinel.stderr] = BytesIO(u'لرئيسية'.encode('utf-8')) (rc, stdout, stderr) = self.module.run_command('/bin/something_ugly') self.assertEqual(rc, 0) self.assertEqual(stdout.decode('utf-8'), u'Žarn§') self.assertEqual(stderr.decode('utf-8'), u'لرئيسية')
def test_prompt_match_wo_data(self): self.cmd_out[sentinel.stdout] = BytesIO( b'Authentication required!\nEnter password: '******'foo', prompt_regex=r'[pP]assword:', data=None) self.assertEqual(rc, 257)
def setUp(self): self.cmd_out = { # os.read() is returning 'bytes', not strings sentinel.stdout: BytesIO(), sentinel.stderr: BytesIO(), } def mock_os_read(fd, nbytes): return self.cmd_out[fd].read(nbytes) def mock_select(rlist, wlist, xlist, timeout=1): return (rlist, [], []) def mock_os_chdir(path): if path == '/inaccessible': raise OSError(errno.EPERM, "Permission denied: '/inaccessible'") basic.MODULE_COMPLEX_ARGS = '{}' self.module = AnsibleModule(argument_spec=dict()) self.module.fail_json = MagicMock(side_effect=SystemExit) self.os = patch('ansible.module_utils.basic.os').start() self.os.path.expandvars.side_effect = lambda x: x self.os.path.expanduser.side_effect = lambda x: x self.os.environ = {'PATH': '/bin'} self.os.getcwd.return_value = '/home/foo' self.os.path.isdir.return_value = True self.os.chdir.side_effect = mock_os_chdir self.os.read.side_effect = mock_os_read self.subprocess = patch( 'ansible.module_utils.basic.subprocess').start() self.cmd = Mock() self.cmd.returncode = 0 self.cmd.stdin = OpenStringIO() self.cmd.stdout.fileno.return_value = sentinel.stdout self.cmd.stderr.fileno.return_value = sentinel.stderr self.subprocess.Popen.return_value = self.cmd self.select = patch('ansible.module_utils.basic.select').start() self.select.select.side_effect = mock_select self.addCleanup(patch.stopall)
def test_ascii_stdout(self): self.cmd_out[sentinel.stdout] = BytesIO(b'hello') (rc, stdout, stderr) = self.module.run_command('/bin/cat hello.txt') self.assertEqual(rc, 0) self.assertEqual(stdout, 'hello')
def test_prompt_no_match(self): self.cmd_out[sentinel.stdout] = BytesIO(b'hello') (rc, _, _) = self.module.run_command('foo', prompt_regex='[pP]assword:') self.assertEqual(rc, 0)