Ejemplo n.º 1
0
    def test_bytes(self):
        b_text = u'tréma'.encode('utf-8')
        unsafe_object = AnsibleUnsafeBytes(b_text)
        yaml_out = self._dump_string(unsafe_object, dumper=self.dumper)

        stream = self._build_stream(yaml_out)
        loader = self._loader(stream)

        data_from_yaml = loader.get_single_data()

        result = b_text
        if PY2:
            # https://pyyaml.org/wiki/PyYAMLDocumentation#string-conversion-python-2-only
            # pyyaml on Python 2 can return either unicode or bytes when given byte strings.
            # We normalize that to always return unicode on Python2 as that's right most of the
            # time.  However, this means byte strings can round trip through yaml on Python3 but
            # not on Python2.  To make this code work the same on Python2 and Python3 (we want
            # the Python3 behaviour) we need to change the methods in Ansible to:
            # (1) Let byte strings pass through yaml without being converted on Python2
            # (2) Convert byte strings to text strings before being given to pyyaml  (Without this,
            #       strings would end up as byte strings most of the time which would mostly be wrong)
            # In practice, we mostly read bytes in from files and then pass that to pyyaml, for which
            # the present behavior is correct.
            # This is a workaround for the current behavior.
            result = u'tr\xe9ma'

        self.assertEqual(result, data_from_yaml)
Ejemplo n.º 2
0
    def ask_passwords():
        ''' prompt for connection and become passwords if needed '''

        op = context.CLIARGS
        sshpass = None
        becomepass = None
        become_prompt = ''

        become_prompt_method = "BECOME" if C.AGNOSTIC_BECOME_PROMPT else op[
            'become_method'].upper()

        try:
            if op['ask_pass']:
                sshpass = getpass.getpass(prompt="SSH password: "******"%s password[defaults to SSH password]: " % become_prompt_method
                if sshpass:
                    sshpass = to_bytes(sshpass,
                                       errors='strict',
                                       nonstring='simplerepr')
            else:
                become_prompt = "%s password: " % become_prompt_method

            if op['become_ask_pass']:
                becomepass = getpass.getpass(prompt=become_prompt)
                if op['ask_pass'] and becomepass == '':
                    becomepass = sshpass
                if becomepass:
                    becomepass = to_bytes(becomepass)
        except EOFError:
            pass

        # we 'wrap' the passwords to prevent templating as
        # they can contain special chars and trigger it incorrectly
        if sshpass:
            sshpass = AnsibleUnsafeBytes(sshpass)
        if becomepass:
            becomepass = AnsibleUnsafeBytes(becomepass)

        return (sshpass, becomepass)
Ejemplo n.º 3
0
 def test_task_executor_run_clean_res(self):
     te = TaskExecutor(None, MagicMock(), None, None, None, None, None,
                       None)
     te._get_loop_items = MagicMock(return_value=[1])
     te._run_loop = MagicMock(
         return_value=[{
             'unsafe_bytes': AnsibleUnsafeBytes(b'{{ $bar }}'),
             'unsafe_text': AnsibleUnsafeText(u'{{ $bar }}'),
             'bytes': b'bytes',
             'text': u'text',
             'int': 1,
         }])
     res = te.run()
     data = res['results'][0]
     self.assertIsInstance(data['unsafe_bytes'], AnsibleUnsafeText)
     self.assertIsInstance(data['unsafe_text'], AnsibleUnsafeText)
     self.assertIsInstance(data['bytes'], text_type)
     self.assertIsInstance(data['text'], text_type)
     self.assertIsInstance(data['int'], int)
Ejemplo n.º 4
0
def test_to_bytes_unsafe():
    assert isinstance(to_bytes(AnsibleUnsafeText(u'foo')), AnsibleUnsafeBytes)
    assert to_bytes(AnsibleUnsafeText(u'foo')) == AnsibleUnsafeBytes(b'foo')
Ejemplo n.º 5
0
def test_wrap_var_unsafe_bytes():
    assert isinstance(wrap_var(AnsibleUnsafeBytes(b'foo')), AnsibleUnsafeBytes)
Ejemplo n.º 6
0
def test_AnsibleUnsafeBytes():
    assert isinstance(AnsibleUnsafeBytes(b'foo'), AnsibleUnsafe)