def test_normalize(reference_form: Dict[str, Any], alternate_forms: Tuple[Dict[str, Any]]) -> None: """Test that tasks specified differently are normalized same way.""" normal_form = utils.normalize_task(reference_form, 'tasks.yml') for form in alternate_forms: assert normal_form == utils.normalize_task(form, 'tasks.yml')
def test_normalize_task_is_idempotent(self): tasks = list() tasks.append(dict(name="hello", action={'module': 'ec2', 'region': 'us-east1', 'etc': 'whatever'})) tasks.append(dict(name="hello", ec2={'region': 'us-east1', 'etc': 'whatever'})) tasks.append(dict(name="hello", ec2="region=us-east1 etc=whatever")) tasks.append(dict(name="hello", action="ec2 region=us-east1 etc=whatever")) for task in tasks: normalized_task = utils.normalize_task(task) self.assertEquals(normalized_task, utils.normalize_task(normalized_task))
def test_normalize_task_is_idempotent(self): tasks = list() tasks.append(dict(name="hello", action={"module": "ec2", "region": "us-east1", "etc": "whatever"})) tasks.append(dict(name="hello", ec2={"region": "us-east1", "etc": "whatever"})) tasks.append(dict(name="hello", ec2="region=us-east1 etc=whatever")) tasks.append(dict(name="hello", action="ec2 region=us-east1 etc=whatever")) for task in tasks: normalized_task = utils.normalize_task(task) self.assertEqual(normalized_task, utils.normalize_task(normalized_task))
def test_normalize_task_is_idempotent(self): tasks = list() tasks.append(dict(name="hello", action={'module': 'ec2', 'region': 'us-east1', 'etc': 'whatever'})) tasks.append(dict(name="hello", ec2={'region': 'us-east1', 'etc': 'whatever'})) tasks.append(dict(name="hello", ec2="region=us-east1 etc=whatever")) tasks.append(dict(name="hello", action="ec2 region=us-east1 etc=whatever")) for task in tasks: normalized_task = utils.normalize_task(task) self.assertEqual(normalized_task, utils.normalize_task(normalized_task))
def yaml_form_rather_than_key_value(candidate, settings): with codecs.open(candidate.path, mode='rb', encoding='utf-8') as f: content = parse_yaml_linenumbers(f.read(), candidate.path) errors = [] if content: fileinfo = dict(type=candidate.filetype, path=candidate.path) for task in get_action_tasks(content, fileinfo): normal_form = normalize_task(task, candidate.path) action = normal_form['action']['__ansible_module__'] arguments = normal_form['action']['__ansible_arguments__'] # Cope with `set_fact` where task['set_fact'] is None if not task.get(action): continue if isinstance(task[action], dict): continue # allow skipping based on tag e.g. if using splatting # https://docs.ansible.com/ansible/devel/reference_appendices\ # /faq.html#argsplat-unsafe if 'skip_ansible_lint' in (task.get('tags') or []): continue # strip additional newlines off task[action] if task[action].strip().split() != arguments: errors.append(Error(task['__line__'], "Task arguments appear " "to be in key value rather " "than YAML format")) return Result(candidate.path, errors)
def test_normalize_task_is_idempotent(self): tasks = list() tasks.append(dict(name="hello", action={'module': 'ec2', 'region': 'us-east1', 'etc': 'whatever'})) tasks.append(dict(name="hello", ec2={'region': 'us-east1', 'etc': 'whatever'})) tasks.append(dict(name="hello", ec2="region=us-east1 etc=whatever")) tasks.append(dict(name="hello", action="ec2 region=us-east1 etc=whatever")) for task in tasks: normalized_task = utils.normalize_task(task, 'tasks.yml') normalized_task['action']['module'] = normalized_task['action']['__ansible_module__'] del normalized_task['action']['__ansible_module__'] renormalized_task = utils.normalize_task(dict(normalized_task), 'tasks.yml') renormalized_task['action']['module'] = renormalized_task['action']['__ansible_module__'] del renormalized_task['action']['__ansible_module__'] self.assertEqual(normalized_task, renormalized_task)
def test_normalize_args(self): task1 = { 'git': { 'version': 'abc' }, 'args': { 'repo': 'blah', 'dest': 'xyz' } } task2 = {'git': {'version': 'abc', 'repo': 'blah', 'dest': 'xyz'}} task3 = {"git": 'version=abc repo=blah dest=xyz'} task4 = { "git": None, "args": { 'repo': 'blah', 'dest': 'xyz', 'version': 'abc' } } self.assertEqual(utils.normalize_task(task1, 'tasks.yml'), utils.normalize_task(task2, 'tasks.yml')) self.assertEqual(utils.normalize_task(task1, 'tasks.yml'), utils.normalize_task(task3, 'tasks.yml')) self.assertEqual(utils.normalize_task(task1, 'tasks.yml'), utils.normalize_task(task4, 'tasks.yml'))
def test_normalize_complex_command(self): task1 = dict(name="hello", action={"module": "ec2", "region": "us-east1", "etc": "whatever"}) task2 = dict(name="hello", ec2={"region": "us-east1", "etc": "whatever"}) task3 = dict(name="hello", ec2="region=us-east1 etc=whatever") task4 = dict(name="hello", action="ec2 region=us-east1 etc=whatever") self.assertEqual(utils.normalize_task(task1), utils.normalize_task(task2)) self.assertEqual(utils.normalize_task(task2), utils.normalize_task(task3)) self.assertEqual(utils.normalize_task(task3), utils.normalize_task(task4))
def test_normalize_complex_command(self): task1 = dict(name="hello", action={'module': 'ec2', 'region': 'us-east1', 'etc': 'whatever'}) task2 = dict(name="hello", ec2={'region': 'us-east1', 'etc': 'whatever'}) task3 = dict(name="hello", ec2="region=us-east1 etc=whatever") task4 = dict(name="hello", action="ec2 region=us-east1 etc=whatever") self.assertEquals(utils.normalize_task(task1), utils.normalize_task(task2)) self.assertEquals(utils.normalize_task(task2), utils.normalize_task(task3)) self.assertEquals(utils.normalize_task(task3), utils.normalize_task(task4))
def test_normalize_args(self): task1 = dict(git={'version': 'abc'}, args={'repo': 'blah', 'dest': 'xyz'}) task2 = dict(git={'version': 'abc', 'repo': 'blah', 'dest': 'xyz'}) task3 = dict(git='version=abc repo=blah dest=xyz') task4 = dict(git=None, args={'repo': 'blah', 'dest': 'xyz', 'version': 'abc'}) self.assertEqual(utils.normalize_task(task1, 'tasks.yml'), utils.normalize_task(task2, 'tasks.yml')) self.assertEqual(utils.normalize_task(task1, 'tasks.yml'), utils.normalize_task(task3, 'tasks.yml')) self.assertEqual(utils.normalize_task(task1, 'tasks.yml'), utils.normalize_task(task4, 'tasks.yml'))
def test_normalize_complex_command(): task1 = dict(name="hello", action={'module': 'pip', 'name': 'df', 'editable': 'false'}) task2 = dict(name="hello", pip={'name': 'df', 'editable': 'false'}) task3 = dict(name="hello", pip="name=df editable=false") task4 = dict(name="hello", action="pip name=df editable=false") assert utils.normalize_task(task1, 'tasks.yml') == utils.normalize_task(task2, 'tasks.yml') assert utils.normalize_task(task2, 'tasks.yml') == utils.normalize_task(task3, 'tasks.yml') assert utils.normalize_task(task3, 'tasks.yml') == utils.normalize_task(task4, 'tasks.yml')
def test_normalize_complex_command(self): task1 = dict(name="hello", action={'module': 'ec2', 'region': 'us-east1', 'etc': 'whatever'}) task2 = dict(name="hello", ec2={'region': 'us-east1', 'etc': 'whatever'}) task3 = dict(name="hello", ec2="region=us-east1 etc=whatever") task4 = dict(name="hello", action="ec2 region=us-east1 etc=whatever") self.assertEqual(utils.normalize_task(task1, 'tasks.yml'), utils.normalize_task(task2, 'tasks.yml')) self.assertEqual(utils.normalize_task(task2, 'tasks.yml'), utils.normalize_task(task3, 'tasks.yml')) self.assertEqual(utils.normalize_task(task3, 'tasks.yml'), utils.normalize_task(task4, 'tasks.yml'))
def test_normalize_complex_command(): """Test that tasks specified differently are normalized same way.""" task1 = dict(name="hello", action={'module': 'pip', 'name': 'df', 'editable': 'false'}) task2 = dict(name="hello", pip={'name': 'df', 'editable': 'false'}) task3 = dict(name="hello", pip="name=df editable=false") task4 = dict(name="hello", action="pip name=df editable=false") assert utils.normalize_task(task1, 'tasks.yml') == utils.normalize_task(task2, 'tasks.yml') assert utils.normalize_task(task2, 'tasks.yml') == utils.normalize_task(task3, 'tasks.yml') assert utils.normalize_task(task3, 'tasks.yml') == utils.normalize_task(task4, 'tasks.yml')
def yaml_form_rather_than_key_value(candidate, settings): with codecs.open(candidate.path, mode='rb', encoding='utf-8') as f: content = parse_yaml_linenumbers(f.read(), candidate.path) errors = [] if content: fileinfo = dict(type=candidate.filetype, path=candidate.path) for task in get_action_tasks(content, fileinfo): normal_form = normalize_task(task, candidate.path) action = normal_form['action']['__ansible_module__'] arguments = normal_form['action']['__ansible_arguments__'] # FIXME: This is a bug - perhaps when connection is local # or similar if action not in task: continue if isinstance(task[action], dict): continue if task[action] != ' '.join(arguments): errors.append(Error(task['__line__'], "Task arguments appear " "to be in key value rather " "than YAML format")) return Result(candidate.path, errors)
def yaml_form_rather_than_key_value(candidate, settings): with codecs.open(candidate.path, mode='rb', encoding='utf-8') as f: content = parse_yaml_linenumbers(f.read(), candidate.path) errors = [] if content: fileinfo = dict(type=candidate.filetype, path=candidate.path) for task in get_action_tasks(content, fileinfo): normal_form = normalize_task(task, candidate.path) action = normal_form['action']['__ansible_module__'] arguments = normal_form['action']['__ansible_arguments__'] # Cope with `set_fact` where task['set_fact'] is None if not task.get(action): continue if isinstance(task[action], dict): continue # strip additional newlines off task[action] if task[action].strip().split() != arguments: errors.append(Error(task['__line__'], "Task arguments appear " "to be in key value rather " "than YAML format")) return Result(candidate.path, errors)
def yaml_form_rather_than_key_value(candidate, settings): with codecs.open(candidate.path, mode='rb', encoding='utf-8') as f: content = parse_yaml_linenumbers(f.read(), candidate.path) errors = [] if content: fileinfo = dict(type=candidate.filetype, path=candidate.path) for task in get_action_tasks(content, fileinfo): normal_form = normalize_task(task, candidate.path) action = normal_form['action']['__ansible_module__'] arguments = normal_form['action']['__ansible_arguments__'] # Cope with `set_fact` where task['set_fact'] is None if not task.get(action): continue if isinstance(task[action], dict): continue # strip additional newlines off task[action] if task[action].strip().split() != arguments: errors.append( Error( task['__line__'], "Task arguments appear " "to be in key value rather " "than YAML format")) return Result(candidate.path, errors)
def yaml_form_rather_than_key_value(candidate, settings): with codecs.open(candidate.path, mode='rb', encoding='utf-8') as f: content = parse_yaml_linenumbers(f.read(), candidate.path) errors = [] if content: fileinfo = dict(type=candidate.filetype, path=candidate.path) for task in get_action_tasks(content, fileinfo): normal_form = normalize_task(task, candidate.path) action = normal_form['action']['__ansible_module__'] arguments = normal_form['action']['__ansible_arguments__'] # FIXME: This is a bug - perhaps when connection is local # or similar if action not in task: continue if isinstance(task[action], dict): continue if task[action] != ' '.join(arguments): errors.append( Error( task['__line__'], "Task arguments appear " "to be in key value rather " "than YAML format")) return Result(candidate.path, errors)
def test_task_to_str_unicode(): task = dict(fail=dict(msg=u"unicode é ô à")) result = utils.task_to_str(utils.normalize_task(task, 'filename.yml')) assert result == u"fail msg=unicode é ô à"
def test_normalize_simple_command(self): task1 = dict(name="hello", action="command chdir=abc echo hello world") task2 = dict(name="hello", command="chdir=abc echo hello world") self.assertEqual(utils.normalize_task(task1, 'tasks.yml'), utils.normalize_task(task2, 'tasks.yml'))
def test_task_to_str_unicode(self): task = dict(fail=dict(msg=u"unicode é ô à")) result = utils.task_to_str(utils.normalize_task(task, 'filename.yml')) self.assertEqual(result, u"fail msg=unicode é ô à")
def test_normalize(reference_form, alternate_forms): """Test that tasks specified differently are normalized same way.""" normal_form = utils.normalize_task(reference_form, 'tasks.yml') for form in alternate_forms: assert normal_form == utils.normalize_task(form, 'tasks.yml')
def test_task_to_str_unicode(): """Ensure that extracting messages from tasks preserves Unicode.""" task = dict(fail=dict(msg=u"unicode é ô à")) result = utils.task_to_str(utils.normalize_task(task, 'filename.yml')) assert result == u"fail msg=unicode é ô à"
def test_normalize(reference_form, alternate_forms): normal_form = utils.normalize_task(reference_form, 'tasks.yml') for form in alternate_forms: assert normal_form == utils.normalize_task(form, 'tasks.yml')