salt/tests/integration/output/test_output.py

112 lines
3.3 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Nicole Thomas <nicole@saltstack.com>`
'''
# Import Salt Libs
from __future__ import absolute_import
2014-10-11 16:37:24 +00:00
import os
import traceback
# Import Salt Testing Libs
from tests.support.case import ShellCase
from tests.support.mixins import RUNTIME_VARS
# Import Salt libs
import salt.config
2015-02-25 09:50:53 +00:00
from salt.output import display_output
class OutputReturnTest(ShellCase):
'''
Integration tests to ensure outputters return their expected format.
Tests against situations where the loader might not be returning the
right outputter even though it was explicitly requested.
'''
def test_output_json(self):
'''
Tests the return of json-formatted data
'''
ret = self.run_call('test.ping --out=json')
self.assertIn('{', ret)
self.assertIn('"local": true', ''.join(ret))
self.assertIn('}', ''.join(ret))
def test_output_nested(self):
'''
Tests the return of nested-formatted data
'''
expected = ['local:', ' True']
ret = self.run_call('test.ping --out=nested')
self.assertEqual(ret, expected)
def test_output_quiet(self):
'''
Tests the return of an out=quiet query
'''
expected = []
ret = self.run_call('test.ping --out=quiet')
self.assertEqual(ret, expected)
def test_output_pprint(self):
'''
Tests the return of pprint-formatted data
'''
expected = ["{'local': True}"]
ret = self.run_call('test.ping --out=pprint')
self.assertEqual(ret, expected)
def test_output_raw(self):
'''
Tests the return of raw-formatted data
'''
expected = ["{'local': True}"]
ret = self.run_call('test.ping --out=raw')
self.assertEqual(ret, expected)
def test_output_txt(self):
'''
Tests the return of txt-formatted data
'''
expected = ['local: True']
ret = self.run_call('test.ping --out=txt')
self.assertEqual(ret, expected)
def test_output_yaml(self):
'''
Tests the return of yaml-formatted data
'''
expected = ['local: true']
ret = self.run_call('test.ping --out=yaml')
self.assertEqual(ret, expected)
2014-10-11 16:37:24 +00:00
def test_output_unicodebad(self):
'''
Tests outputter reliability with utf8
'''
opts = salt.config.minion_config(os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'minion'))
2014-10-11 16:37:24 +00:00
opts['output_file'] = os.path.join(
RUNTIME_VARS.TMP,
'outputtest'
)
2014-10-11 16:37:24 +00:00
data = {'foo': {'result': False,
'aaa': 'azerzaeréééé',
'comment': u'ééééàààà'}}
try:
# this should not raises UnicodeEncodeError
display_output(data, opts=opts)
2014-10-12 10:15:58 +00:00
except Exception:
2014-10-12 09:37:17 +00:00
# display trace in error message for debugging on jenkins
2014-10-12 10:15:58 +00:00
trace = traceback.format_exc()
Python 3 Fixes (Pt. 2) (#39397) * Typo in comment * First convert to string if not already a string. Then to bytes under Py3. The reason being that jids from the CLI, at least the one fed in integration.runners.jobs.ManageTest.test_loopup_jid is loaded as an integer, and, while the Py2 code converts JIDs to strings, under Py3, we assume JID's are already strings. * Mark tests which require root permissions to run * Allow declaring that the function IS a class method. ``` Python 3.5.3 (default, Jan 21 2017, 00:29:12) [GCC 6.3.1 20170109] on linux Type "help", "copyright", "credits" or "license" for more information. >>> class Foo: ... def bar(self): ... print('bar') ... >>> import inspect >>> inspect.ismethod(Foo.bar) False >>> inspect.ismethod(Foo().bar) True ``` On Python 2, `inspect.ismethod` returns `True` for bound and unbound methods while on Python 3 it only returns `True` for bound methods. The explicit `is_class_method` is to avoid instantiating the class just to get the function signature. * Always decode responses to the Python version native string implementation * Just compare the objects as matching list. Asserting same item count doesn't make that much sense. * Py3 compatibility * Fix saltnado tests under Py3 * Python 3 compatibility * Show me the full traceback * Revert "Convert fileserver data from bytes to strings" This reverts commit e53972f8c6464c0fdeffb875c785b3ce530c7c5e. * Revert "Under Py3, we get `bytes` when using the roots backend directly" This reverts commit 9f73b240c1d68d785a04d898fab84837c154a5ae. * Convert from bytes to str if not a binary file * Py3 compatibility fixes. Convert file contents from bytes to string if not a binary file
2017-02-14 23:20:56 +00:00
sentinel = object()
old_max_diff = getattr(self, 'maxDiff', sentinel)
try:
self.maxDiff = None
self.assertEqual(trace, '')
finally:
if old_max_diff is sentinel:
delattr(self, 'maxDiff')
else:
self.maxDiff = old_max_diff