Ejemplo n.º 1
0
    def test_is_py_ver(self):
        """Tests for is_py_ver, is_py2, is_py3 functions."""

        maj_ver = sys.version_info[0]
        min_ver = sys.version_info[1]

        if maj_ver >= 3:
            self.assertFalse(is_py2())
            self.assertFalse(is_py_ver(2))
            self.assertFalse(is_py_ver(2, 4))
            self.assertFalse(is_py_ver(2, min_ver=6))
            self.assertTrue(is_py3())
            self.assertTrue(is_py_ver(3))
            self.assertTrue(is_py_ver(3, min_ver))
            self.assertTrue(is_py_ver(3, min_ver=min_ver))
            if min_ver >= 6:
                self.assertTrue(is_py_ver(3, 6))
                self.assertTrue(is_py_ver(3, min_ver=6))
                self.assertFalse(is_py_ver(3, 99))
                self.assertFalse(is_py_ver(3, min_ver=99))
        else:
            # must be Python 2.6 or more recent Python 2 nowdays
            self.assertTrue(is_py2())
            self.assertTrue(is_py_ver(2))
            self.assertTrue(is_py_ver(2, 4))
            self.assertTrue(is_py_ver(2, min_ver=6))
            self.assertFalse(is_py3())
            self.assertFalse(is_py_ver(3))
            self.assertFalse(is_py_ver(3, 6))
            self.assertFalse(is_py_ver(3, min_ver=6))
Ejemplo n.º 2
0
    def test_noshell_async_stdout_glob(self):

        for msg in ['ok', "message with spaces", 'this -> ¢ <- is unicode']:

            self.mock_stderr(True)
            self.mock_stdout(True)
            ec, output = async_to_stdout("/bin/echo %s" % msg)
            stderr, stdout = self.get_stderr(), self.get_stdout()
            self.mock_stderr(False)
            self.mock_stdout(False)

            # there should be no output to stderr
            self.assertFalse(stderr)

            # in Python 2, we need to ensure the 'msg' input doesn't include any Unicode
            # before comparing with 'output' (which also has Unicode characters stripped out)
            if is_py2():
                msg = msg.decode('ascii', 'ignore')
            # in Python 3, Unicode characters get replaced with backslashed escape sequences
            elif is_py3():
                msg = msg.replace('¢', '\\xc2\\xa2')

            self.assertEqual(ec, 0)
            self.assertEqual(output, msg + '\n')
            self.assertEqual(output, stdout)
Ejemplo n.º 3
0
    def test_sync_altered_accounts_unicode_pubkey(self, mock_add_or_update):
        """Test the sync_altered accounts function with a pubkey containing unicode"""
        mock_client = mock.MagicMock()
        test_account = mkVscAccount(test_account_1)
        mock_client.account[test_account.vsc_id] = mock.MagicMock()
        mock_client.account.modified[1].get.return_value = (200,
                                                            [test_account_1])
        mock_client.account[test_account.vsc_id].usergroup.get.return_value = (
            200, test_usergroup_1)
        mock_client.get_public_keys.return_value = [
            mkVscAccountPubkey(p) for p in test_unicode_pubkeys
        ]
        mock_client.account[test_account.vsc_id].quota.get.return_value = (
            200, test_quota)

        mock_add_or_update.return_value = UPDATED
        ldapsyncer = LdapSyncer(mock_client)
        accounts = ldapsyncer.sync_altered_accounts(1)
        self.assertEqual(
            accounts, {
                'error': set([]),
                'new': set([]),
                'updated': set([test_account.vsc_id])
            })
        if is_py2():
            expected_key = 'some pubkey \\[email protected]\\u201d'
        else:
            expected_key = 'some pubkey \\xe2\\x80\\[email protected]\\xe2\\x80\\x9d'
        ldap_attrs = {
            'status': ['active'],
            'scratchDirectory': ['/user/scratch/gent/vsc400/vsc40075'],
            'dataDirectory': ['/user/data/gent/vsc400/vsc40075'],
            'cn': 'vsc40075',
            'homeQuota': ['5242880'],
            'institute': ['gent'],
            'loginShell': ['/bin/bash'],
            'uidNumber': ['2540075'],
            'researchField': ['Bollocks'],
            'gidNumber': ['2540075'],
            'gecos': ['Foo Bar'],
            'dataQuota': ['1'],
            'homeDirectory': ['/user/home/gent/vsc400/vsc40075'],
            'mail': ['*****@*****.**'],
            'scratchQuota': ['1'],
            'pubkey': [expected_key],
            'instituteLogin': ['foobar'],
            'uid': ['vsc40075']
        }
        mock_add_or_update.assert_called_with(VscLdapUser, test_account.vsc_id,
                                              ldap_attrs, True)
Ejemplo n.º 4
0
    def test_ensure_ascii_string(self):
        """Tests for ensure_ascii_string function."""

        unicode_txt = 'this -> ¢ <- is unicode'

        test_cases = [
            ('', ''),
            ('foo', 'foo'),
            ([1, 2, 3], "[1, 2, 3]"),
            (['1', '2', '3'], "['1', '2', '3']"),
            ({
                'one': 1
            }, "{'one': 1}"),
            # in both Python 2 & 3, Unicode characters that are part of a non-string value get escaped
            ([unicode_txt], "['this -> \\xc2\\xa2 <- is unicode']"),
            ({
                'foo': unicode_txt
            }, "{'foo': 'this -> \\xc2\\xa2 <- is unicode'}"),
        ]
        if is_py2():
            test_cases.extend([
                # Unicode characters from regular strings are stripped out in Python 2
                (unicode_txt, 'this ->  <- is unicode'),
                # also test with unicode-type values (only exists in Python 2)
                (unicode('foo'), 'foo'),
                (unicode(unicode_txt,
                         encoding='utf-8'), 'this -> \\xa2 <- is unicode'),
            ])
        else:
            # in Python 3, Unicode characters are replaced by backslashed escape sequences in string values
            expected_unicode_out = 'this -> \\xc2\\xa2 <- is unicode'
            test_cases.extend([
                (unicode_txt, expected_unicode_out),
                # also test with bytestring-type values (only exists in Python 3)
                (bytes('foo', encoding='utf-8'), 'foo'),
                (bytes(unicode_txt, encoding='utf-8'), expected_unicode_out),
            ])

        for inp, out in test_cases:
            res = ensure_ascii_string(inp)
            self.assertTrue(is_string(res))
            self.assertEqual(res, out)
Ejemplo n.º 5
0
# poor man's enum
Colorize = namedtuple('Colorize', 'AUTO ALWAYS NEVER')('auto', 'always',
                                                       'never')

APOCALYPTIC = 'APOCALYPTIC'
logging.addLevelName(logging.CRITICAL * 2 + 1, APOCALYPTIC)

# register QUIET, EXCEPTION and FATAL alias
LOG_LEVEL_ALIASES = {
    'EXCEPTION': logging.ERROR,
    'FATAL': logging.CRITICAL,
    'QUIET': logging.WARNING,
}

if is_py2():
    levelnames = logging._levelNames
else:
    # logging._levelNames no longer exists in Python 3
    # logging.addLevelName is not a real replacement (it overwrites existing level names)
    levelnames = logging._nameToLevel

for key in LOG_LEVEL_ALIASES:
    levelnames[key] = LOG_LEVEL_ALIASES[key]

# mpi rank support
_MPIRANK = MPIRANK_NO_MPI
if not _env_to_boolean('FANCYLOGGER_IGNORE_MPI4PY'):
    try:
        from mpi4py import MPI
        if MPI.Is_initialized():