class TestAnsibleModuleExitJson(unittest.TestCase): def setUp(self): self.COMPLEX_ARGS = basic.MODULE_COMPLEX_ARGS basic.MODULE_COMPLEX_ARGS = '{}' self.old_stdout = sys.stdout self.fake_stream = StringIO() sys.stdout = self.fake_stream self.module = basic.AnsibleModule(argument_spec=dict()) def tearDown(self): basic.MODULE_COMPLEX_ARGS = self.COMPLEX_ARGS sys.stdout = self.old_stdout def test_exit_json_no_args_exits(self): with self.assertRaises(SystemExit) as ctx: self.module.exit_json() if isinstance(ctx.exception, int): # Python2.6... why does sys.exit behave this way? self.assertEquals(ctx.exception, 0) else: self.assertEquals(ctx.exception.code, 0) return_val = json.loads(self.fake_stream.getvalue()) self.assertEquals(return_val, dict(changed=False, invocation=empty_invocation)) def test_exit_json_args_exits(self): with self.assertRaises(SystemExit) as ctx: self.module.exit_json(msg='message') if isinstance(ctx.exception, int): # Python2.6... why does sys.exit behave this way? self.assertEquals(ctx.exception, 0) else: self.assertEquals(ctx.exception.code, 0) return_val = json.loads(self.fake_stream.getvalue()) self.assertEquals( return_val, dict(msg="message", changed=False, invocation=empty_invocation)) def test_fail_json_exits(self): with self.assertRaises(SystemExit) as ctx: self.module.fail_json(msg='message') if isinstance(ctx.exception, int): # Python2.6... why does sys.exit behave this way? self.assertEquals(ctx.exception, 1) else: self.assertEquals(ctx.exception.code, 1) return_val = json.loads(self.fake_stream.getvalue()) self.assertEquals( return_val, dict(msg="message", failed=True, invocation=empty_invocation)) def test_exit_json_proper_changed(self): with self.assertRaises(SystemExit) as ctx: self.module.exit_json(changed=True, msg='success') return_val = json.loads(self.fake_stream.getvalue()) self.assertEquals( return_val, dict(changed=True, msg='success', invocation=empty_invocation))
def setUp(self): self.COMPLEX_ARGS = basic.MODULE_COMPLEX_ARGS basic.MODULE_COMPLEX_ARGS = '{}' self.old_stdout = sys.stdout self.fake_stream = StringIO() sys.stdout = self.fake_stream self.module = basic.AnsibleModule(argument_spec=dict())
def _clean_data(self, orig_data): ''' remove jinja2 template tags from a string ''' if not isinstance(orig_data, string_types): return orig_data with contextlib.closing(StringIO(orig_data)) as data: # these variables keep track of opening block locations, as we only # want to replace matched pairs of print/block tags print_openings = [] block_openings = [] for mo in self._clean_regex.finditer(orig_data): token = mo.group(0) token_start = mo.start(0) if token[0] == self.variable_start[0]: if token == self.block_start: block_openings.append(token_start) elif token == self.variable_start: print_openings.append(token_start) elif token[1] == self.variable_end[1]: prev_idx = None if token == self.block_end and block_openings: prev_idx = block_openings.pop() elif token == self.variable_end and print_openings: prev_idx = print_openings.pop() prev_idx = print_openings.pop() if prev_idx is not None: # replace the opening data.seek(prev_idx, os.SEEK_SET) data.write(self.environment.comment_start_string) # replace the closing data.seek(token_start, os.SEEK_SET) data.write(self.environment.comment_end_string) else: raise AnsibleError("Error while cleaning data for safety: unhandled regex match") return data.getvalue()
def cfgparser(): CFGDATA = StringIO(""" [defaults] defaults_one = 'data_defaults_one' [level1] level1_one = 'data_level1_one' """) p = configparser.ConfigParser() p.readfp(CFGDATA) return p
def test_exit_json_removes_values(self): for args, return_val, expected in self.dataset: sys.stdout = StringIO() basic.MODULE_COMPLEX_ARGS = json.dumps(args) module = basic.AnsibleModule(argument_spec=dict( username=dict(), password=dict(no_log=True), token=dict(no_log=True), ), ) with self.assertRaises(SystemExit) as ctx: self.assertEquals(module.exit_json(**return_val), expected) self.assertEquals(json.loads(sys.stdout.getvalue()), expected)
def test_fail_json_removes_values(self): self.maxDiff = None for args, return_val, expected in self.dataset: expected = copy.deepcopy(expected) del expected['changed'] expected['failed'] = True sys.stdout = StringIO() basic.MODULE_COMPLEX_ARGS = json.dumps(args) module = basic.AnsibleModule(argument_spec=dict( username=dict(), password=dict(no_log=True), token=dict(no_log=True), ), ) with self.assertRaises(SystemExit) as ctx: self.assertEquals(module.fail_json(**return_val), expected) self.assertEquals(json.loads(sys.stdout.getvalue()), expected)