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))
def hash_pass(self, password, username=None): if not username: username = self.username credentials = '%s:%s' % (username, password) if is_py3(): # convert credentials into bytes credentials = credentials.encode('utf-8') encoded_credentials = base64.b64encode(credentials).strip() if is_py3(): # convert back to string encoded_credentials = str(encoded_credentials, 'utf-8') return 'Basic ' + encoded_credentials
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)
def _loglevel(lvl, msg): lvl_int = topt.log.getEffectiveLevel() if is_py3(): lvl_name = logging.getLevelName(lvl_int) else: lvl_name = [ k for k, v in logging._levelNames.items() if v == lvl_int ][0] self.assertEqual(lvl_int, fancylogger.getLevelInt(lvl), msg="%s (expected %s got %s)" % (msg, lvl, lvl_name))
def test_is_string(self): """Tests for is_string function.""" for item in ['foo', u'foo', "hello world", """foo\nbar""", '']: self.assertTrue(is_string(item)) for item in [1, None, ['foo'], ('foo', ), {'foo': 'bar'}]: self.assertFalse(is_string(item)) if is_py3(): self.assertFalse(is_string(b'bytes_are_not_a_string')) else: # in Python 2, b'foo' is really just a regular string self.assertTrue(is_string(b'foo'))
def send_all(p, data): """ Send data to process p """ allsent = 0 # in Python 3, we must use a bytestring if is_py3(): data = data.encode() while len(data): sent = p.send(data) if sent is None: raise Exception(MESSAGE) allsent += sent if is_py3(): data = memoryview(data)[sent:] else: data = buffer( data, sent ) # noqa (to avoid prospector failing on undefined 'buffer' in Python 3) return allsent
def request(self, method, url, body, headers, content_type=None): """Low-level networking. All HTTP-method methods call this""" # format headers if headers is None: headers = {} if content_type is not None: headers['Content-Type'] = content_type if self.auth_header is not None: headers['Authorization'] = self.auth_header headers['User-Agent'] = self.user_agent # censor contents of 'Authorization' part of header, to avoid leaking tokens or passwords in logs secret_items = ['Authorization', 'X-Auth-Token'] headers_censored = self.censor_request(secret_items, headers) if body and not is_string(body): # censor contents of body to avoid leaking passwords secret_items = ['password'] body_censored = self.censor_request(secret_items, body) # serialize body in all cases body = json.dumps(body) else: # assume serialized bodies are already clear of secrets fancylogger.getLogger().debug( "Request with pre-serialized body, will not censor secrets") body_censored = body fancylogger.getLogger().debug('cli request: %s, %s, %s, %s', method, url, body_censored, headers_censored) # TODO: in recent python: Context manager conn = self.get_connection(method, url, body, headers) status = conn.code if method == self.HEAD: pybody = conn.headers else: body = conn.read() if is_py3(): body = body.decode('utf-8') # byte encoded response try: pybody = json.loads(body) except ValueError: pybody = body fancylogger.getLogger().debug('reponse len: %s ', len(pybody)) conn.close() return status, pybody
def _init_input(self): """Handle input, if any in a simple way""" if self.input is not None: # allow empty string (whatever it may mean) # in Python 3, stdin.write requires a bytestring if is_py3() and is_string(self.input): inp = bytes(self.input, encoding='utf-8') else: inp = self.input try: self._process.stdin.write(inp) except Exception: self.log.raiseException( "_init_input: Failed write input %s to process" % self.input) if self.INIT_INPUT_CLOSE: self._process.stdin.close() self.log.debug("_init_input: process stdin closed") else: self.log.debug("_init_input: process stdin NOT closed")
def test_deprecated(self): """Test deprecated log function.""" # log message logger = fancylogger.getLogger('deprecated_test') logger.setLevel('DEBUG') max_ver = "1.0" # test whether deprecation works msgre_tpl_error = r"DEPRECATED\s*\(since v%s\).*%s" % (max_ver, MSG) self.assertErrorRegex(Exception, msgre_tpl_error, logger.deprecated, MSG, "1.1", max_ver) self.assertErrorRegex(Exception, msgre_tpl_error, logger.deprecated, MSG, "1.0", max_ver) #self.mk_empty_log() logger.error("hoohoo") # test whether deprecated warning works # no deprecation if current version is lower than max version logger.deprecated(MSG, "0.9", max_ver) msgre_warning = re.compile( r"WARNING.*Deprecated.* will no longer work in v%s:.*%s" % (max_ver, MSG)) txt = self.read_log() self.assertTrue( msgre_warning.search(txt), "Pattern '%s' found in: %s" % (msgre_warning.pattern, txt)) self.mk_empty_log() callback_cache = [] def test_log_callback(msg, cache=callback_cache): """Log callback function to log warning message and print to stderr.""" cache.append(msg) # test use of log_callback logger.deprecated("test callback", "0.9", max_ver, log_callback=test_log_callback) self.assertEqual( callback_cache[-1], "Deprecated functionality, will no longer work in v1.0: test callback" ) self.mk_empty_log() # test handling of non-UTF8 chars msg = MSG + u'\x81' if is_py3(): # Python 3: unicode is supported in regular string values (no special unicode type) msgre_tpl_error = r"DEPRECATED\s*\(since v%s\).*\x81" % max_ver msgre_warning = re.compile( r"WARNING.*Deprecated.* will no longer work in v%s:.*\x81" % max_ver) else: # Python 2: extra \xc2 character appears for unicode strings msgre_tpl_error = r"DEPRECATED\s*\(since v%s\).*\xc2\x81" % max_ver msgre_warning = re.compile( r"WARNING.*Deprecated.* will no longer work in v%s:.*\xc2\x81" % max_ver) self.assertErrorRegex(Exception, msgre_tpl_error, logger.deprecated, msg, "1.1", max_ver) self.mk_empty_log() logger.deprecated(msg, "0.9", max_ver) txt = self.read_log() self.assertTrue(msgre_warning.search(txt))