How to use patched_print_debug method in molecule

Best Python code snippet using molecule_python

test_util.py

Source:test_util.py Github

copy

Full Screen

1# Copyright (c) 2015-2018 Cisco Systems, Inc.2#3# Permission is hereby granted, free of charge, to any person obtaining a copy4# of this software and associated documentation files (the "Software"), to5# deal in the Software without restriction, including without limitation the6# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or7# sell copies of the Software, and to permit persons to whom the Software is8# furnished to do so, subject to the following conditions:9#10# The above copyright notice and this permission notice shall be included in11# all copies or substantial portions of the Software.12#13# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE16# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER17# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING18# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER19# DEALINGS IN THE SOFTWARE.20from __future__ import print_function21import binascii22import io23import os24import colorama25import pytest26import sh27from molecule import util28colorama.init(autoreset=True)29def test_print_debug(capsys):30 util.print_debug('test_title', 'test_data')31 result, _ = capsys.readouterr()32 title = [33 colorama.Back.WHITE, colorama.Style.BRIGHT, colorama.Fore.BLACK,34 'DEBUG: test_title', colorama.Fore.RESET, colorama.Back.RESET,35 colorama.Style.RESET_ALL36 ]37 print(''.join(title))38 data = [39 colorama.Fore.BLACK, colorama.Style.BRIGHT, 'test_data',40 colorama.Style.RESET_ALL, colorama.Fore.RESET41 ]42 print(''.join(data))43 x, _ = capsys.readouterr()44 assert x == result45def test_print_environment_vars(capsys):46 env = {47 'ANSIBLE_FOO': 'foo',48 'ANSIBLE_BAR': 'bar',49 'ANSIBLE': None,50 'MOLECULE_FOO': 'foo',51 'MOLECULE_BAR': 'bar',52 'MOLECULE': None53 }54 util.print_environment_vars(env)55 result, _ = capsys.readouterr()56 # Ansible Environment57 title = [58 colorama.Back.WHITE, colorama.Style.BRIGHT, colorama.Fore.BLACK,59 'DEBUG: ANSIBLE ENVIRONMENT', colorama.Fore.RESET, colorama.Back.RESET,60 colorama.Style.RESET_ALL61 ]62 print(''.join(title))63 data = [64 colorama.Fore.BLACK, colorama.Style.BRIGHT,65 util.safe_dump({66 'ANSIBLE_FOO': 'foo',67 'ANSIBLE_BAR': 'bar'68 }), colorama.Style.RESET_ALL, colorama.Fore.RESET69 ]70 print(''.join(data))71 # Molecule Environment72 title = [73 colorama.Back.WHITE, colorama.Style.BRIGHT, colorama.Fore.BLACK,74 'DEBUG: MOLECULE ENVIRONMENT', colorama.Fore.RESET,75 colorama.Back.RESET, colorama.Style.RESET_ALL76 ]77 print(''.join(title))78 data = [79 colorama.Fore.BLACK, colorama.Style.BRIGHT,80 util.safe_dump({81 'MOLECULE_FOO': 'foo',82 'MOLECULE_BAR': 'bar'83 }), colorama.Style.RESET_ALL, colorama.Fore.RESET84 ]85 print(''.join(data))86 # Shell Replay87 title = [88 colorama.Back.WHITE, colorama.Style.BRIGHT, colorama.Fore.BLACK,89 'DEBUG: SHELL REPLAY', colorama.Fore.RESET, colorama.Back.RESET,90 colorama.Style.RESET_ALL91 ]92 print(''.join(title))93 data = [94 colorama.Fore.BLACK, colorama.Style.BRIGHT,95 'ANSIBLE_BAR=bar ANSIBLE_FOO=foo MOLECULE_BAR=bar MOLECULE_FOO=foo',96 colorama.Style.RESET_ALL, colorama.Fore.RESET97 ]98 print(''.join(data))99 print()100 x, _ = capsys.readouterr()101 assert x == result102def test_sysexit():103 with pytest.raises(SystemExit) as e:104 util.sysexit()105 assert 1 == e.value.code106def test_sysexit_with_custom_code():107 with pytest.raises(SystemExit) as e:108 util.sysexit(2)109 assert 2 == e.value.code110def test_sysexit_with_message(patched_logger_critical):111 with pytest.raises(SystemExit) as e:112 util.sysexit_with_message('foo')113 assert 1 == e.value.code114 patched_logger_critical.assert_called_once_with('foo')115def test_sysexit_with_message_and_custom_code(patched_logger_critical):116 with pytest.raises(SystemExit) as e:117 util.sysexit_with_message('foo', 2)118 assert 2 == e.value.code119 patched_logger_critical.assert_called_once_with('foo')120def test_run_command():121 cmd = sh.ls.bake()122 x = util.run_command(cmd)123 assert 0 == x.exit_code124def test_run_command_with_debug(mocker, patched_print_debug):125 cmd = sh.ls.bake(_env={'ANSIBLE_FOO': 'foo', 'MOLECULE_BAR': 'bar'})126 util.run_command(cmd, debug=True)127 x = [128 mocker.call('ANSIBLE ENVIRONMENT', '---\nANSIBLE_FOO: foo\n'),129 mocker.call('MOLECULE ENVIRONMENT', '---\nMOLECULE_BAR: bar\n'),130 mocker.call('SHELL REPLAY', 'ANSIBLE_FOO=foo MOLECULE_BAR=bar'),131 mocker.call('COMMAND', sh.which('ls'))132 ]133 assert x == patched_print_debug.mock_calls134def test_run_command_with_debug_handles_no_env(mocker, patched_print_debug):135 cmd = sh.ls.bake()136 util.run_command(cmd, debug=True)137 x = [138 mocker.call('ANSIBLE ENVIRONMENT', '--- {}\n'),139 mocker.call('MOLECULE ENVIRONMENT', '--- {}\n'),140 mocker.call('SHELL REPLAY', ''),141 mocker.call('COMMAND', sh.which('ls'))142 ]143 assert x == patched_print_debug.mock_calls144def test_os_walk(temp_dir):145 scenarios = ['scenario1', 'scenario2', 'scenario3']146 molecule_directory = pytest.helpers.molecule_directory()147 for scenario in scenarios:148 scenario_directory = os.path.join(molecule_directory, scenario)149 molecule_file = pytest.helpers.get_molecule_file(scenario_directory)150 os.makedirs(scenario_directory)151 util.write_file(molecule_file, '')152 result = [f for f in util.os_walk(molecule_directory, 'molecule.yml')]153 assert 3 == len(result)154def test_render_template():155 template = "{{ foo }} = {{ bar }}"156 "foo = bar" == util.render_template(template, foo='foo', bar='bar')157def test_write_file(temp_dir):158 dest_file = os.path.join(temp_dir.strpath, 'test_util_write_file.tmp')159 contents = binascii.b2a_hex(os.urandom(15)).decode('utf-8')160 util.write_file(dest_file, contents)161 with util.open_file(dest_file) as stream:162 data = stream.read()163 x = '# Molecule managed\n\n{}'.format(contents)164 assert x == data165def molecule_prepender(content):166 x = '# Molecule managed\n\nfoo bar'167 assert x == util.file_prepender('foo bar')168def test_safe_dump():169 x = """170---171foo: bar172""".lstrip()173 assert x == util.safe_dump({'foo': 'bar'})174def test_safe_dump_with_increase_indent():175 data = {176 'foo': [{177 'foo': 'bar',178 'baz': 'zzyzx',179 }],180 }181 x = """182---183foo:184 - baz: zzyzx185 foo: bar186""".lstrip()187 assert x == util.safe_dump(data)188def test_safe_load():189 assert {'foo': 'bar'} == util.safe_load('foo: bar')190def test_safe_load_returns_empty_dict_on_empty_string():191 assert {} == util.safe_load('')192def test_safe_load_exits_when_cannot_parse():193 data = """194---195%foo:196""".strip()197 with pytest.raises(SystemExit) as e:198 util.safe_load(data)199 assert 1 == e.value.code200def test_safe_load_file(temp_dir):201 path = os.path.join(temp_dir.strpath, 'foo')202 util.write_file(path, 'foo: bar')203 assert {'foo': 'bar'} == util.safe_load_file(path)204def test_open_file(temp_dir):205 path = os.path.join(temp_dir.strpath, 'foo')206 util.write_file(path, 'foo: bar')207 with util.open_file(path) as stream:208 try:209 file_types = (file, io.IOBase)210 except NameError:211 file_types = io.IOBase212 assert isinstance(stream, file_types)213def test_instance_with_scenario_name():214 assert 'foo-bar' == util.instance_with_scenario_name('foo', 'bar')215def test_strip_ansi_escape():216 string = 'ls\r\n\x1b[00m\x1b[01;31mfoo\x1b[00m\r\n\x1b[01;31m'217 assert 'ls\r\nfoo\r\n' == util.strip_ansi_escape(string)218def test_strip_ansi_color():219 s = 'foo\x1b[0m\x1b[0m\x1b[0m\n\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m'220 assert 'foo\n' == util.strip_ansi_color(s)221def test_verbose_flag():222 options = {'verbose': True, 'v': True}223 assert ['-v'] == util.verbose_flag(options)224 assert {} == options225def test_verbose_flag_extra_verbose():226 options = {'verbose': True, 'vvv': True}227 assert ['-vvv'] == util.verbose_flag(options)228 assert {} == options229def test_verbose_flag_preserves_verbose_option():230 options = {'verbose': True}231 assert [] == util.verbose_flag(options)232 assert {'verbose': True} == options233def test_filter_verbose_permutation():234 options = {235 'v': True,236 'vv': True,237 'vvv': True,238 'vfoo': True,239 'foo': True,240 'bar': True,241 }242 x = {243 'vfoo': True,244 'foo': True,245 'bar': True,246 }247 assert x == util.filter_verbose_permutation(options)248def test_title():249 assert 'Foo' == util.title('foo')250 assert 'Foo Bar' == util.title('foo_bar')251def test_abs_path(temp_dir):252 x = os.path.abspath(253 os.path.join(os.getcwd(), os.path.pardir, 'foo', 'bar'))254 assert x == util.abs_path(os.path.join(os.path.pardir, 'foo', 'bar'))255def test_camelize():256 assert 'Foo' == util.camelize('foo')257 assert 'FooBar' == util.camelize('foo_bar')258 assert 'FooBarBaz' == util.camelize('foo_bar_baz')259def test_underscore():260 assert 'foo' == util.underscore('Foo')261 assert 'foo_bar' == util.underscore('FooBar')262 assert 'foo_bar_baz' == util.underscore('FooBarBaz')263def test_merge_dicts():264 # example taken from python-anyconfig/anyconfig/__init__.py265 a = {'b': [{'c': 0}, {'c': 2}], 'd': {'e': 'aaa', 'f': 3}}266 b = {'a': 1, 'b': [{'c': 3}], 'd': {'e': 'bbb'}}267 x = {'a': 1, 'b': [{'c': 3}], 'd': {'e': "bbb", 'f': 3}}...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run molecule automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful