def test_find_json(self): test_sample_json = """ { "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": ["GML", "XML"] }, "GlossSee": "markup" } } } } } """ expected_ret = { "glossary": { "GlossDiv": { "GlossList": { "GlossEntry": { "GlossDef": { "GlossSeeAlso": ["GML", "XML"], "para": "A meta-markup language, used to create markup languages such as DocBook.", }, "GlossSee": "markup", "Acronym": "SGML", "GlossTerm": "Standard Generalized Markup Language", "SortAs": "SGML", "Abbrev": "ISO 8879:1986", "ID": "SGML", } }, "title": "S", }, "title": "example glossary", } } # First test the valid JSON ret = utils.find_json(test_sample_json) self.assertDictEqual(ret, expected_ret) # Now pre-pend some garbage and re-test garbage_prepend_json = "{0}{1}".format(LORUM_IPSUM, test_sample_json) ret = utils.find_json(garbage_prepend_json) self.assertDictEqual(ret, expected_ret) # Test to see if a ValueError is raised if no JSON is passed in self.assertRaises(ValueError, utils.find_json, LORUM_IPSUM)
def test_find_json(self): test_sample_json = ''' { "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": ["GML", "XML"] }, "GlossSee": "markup" } } } } } ''' expected_ret = {'glossary': {'GlossDiv': {'GlossList': {'GlossEntry': { 'GlossDef': {'GlossSeeAlso': ['GML', 'XML'], 'para': 'A meta-markup language, used to create markup languages such as DocBook.'}, 'GlossSee': 'markup', 'Acronym': 'SGML', 'GlossTerm': 'Standard Generalized Markup Language', 'SortAs': 'SGML', 'Abbrev': 'ISO 8879:1986', 'ID': 'SGML'}}, 'title': 'S'}, 'title': 'example glossary'}} # First test the valid JSON ret = utils.find_json(test_sample_json) self.assertDictEqual(ret, expected_ret) # Now pre-pend some garbage and re-test garbage_prepend_json = '{0}{1}'.format(LORUM_IPSUM, test_sample_json) ret = utils.find_json(garbage_prepend_json) self.assertDictEqual(ret, expected_ret) # Test to see if a ValueError is raised if no JSON is passed in self.assertRaises(ValueError, utils.find_json, LORUM_IPSUM)
def __call__(self, argv, help): parser = argparse.ArgumentParser( prog="%s salt" % self.ctrl.progname, description=help, ) instances = self.ctrl.get_instances(command='init_ssh_key') parser.add_argument("-r", "--raw", "--raw-shell", dest="raw_shell", default=False, action="store_true", help="Don't execute a salt routine on the targets," " execute a raw shell command") parser.add_argument("instance", nargs=1, metavar="instance", help="Name of the instance from the config.", choices=list(instances)) parser.add_argument("arguments", nargs='+', metavar="arguments", help="Arguments for salt.") args = parser.parse_args(argv) instance = instances[args.instance[0]] from salt.client.ssh import Single from salt.output import display_output from salt.utils import find_json salt_path = os.path.join(self.ctrl.config.path, 'salt') opts = dict( cython_enable=False, cachedir=os.path.join(salt_path, 'cache'), extension_modules=os.path.join(salt_path, 'extmods'), known_hosts_file=self.ctrl.known_hosts, raw_shell=args.raw_shell) single = Single( opts, args.arguments, instance.id, host=instance.get_host(), port=instance.get_port()) single.shell = Shell(instance) (stdout, stderr, retcode) = single.run() ret = { 'stdout': stdout, 'stderr': stderr, 'retcode': retcode, } try: data = find_json(stdout) if len(data) < 2 and 'local' in data: ret = data['local'] except Exception: pass if not isinstance(ret, dict): p_data = {single.id: ret} elif 'return' not in ret: p_data = ret else: p_data = {single.id: ret.get('return', {})} display_output(p_data, 'nested', opts)