Best Python code snippet using avocado_python
ssh-handler.py
Source:ssh-handler.py
...54 port = ''55 if parameters['port']:56 port = ' -p %d' % parameters['port']57 return 'ssh %s%s%s' % (user, parameters['host'], port)58def get_command_args(config, parameters):59 args = [config['path']]60 if config['options']:61 args += config['options'].split()62 if config['terminal'] in ('gnome-terminal', 'terminator'):63 args += ['--title', '"%s"' % parameters['host']]64 args += [65 '--command', '"%s"' % get_ssh_command(parameters),66 ]67 if config['terminal'] == 'putty':68 if parameters['user']:69 args += ['-l', parameters['user']]70 if parameters['port']:71 args += ['-P', str(parameters['port'])]72 args.append(parameters['host'])73 return args74def get_default_config(terminal):75 lines = [76 '[DEFAULT]',77 'terminal = %s' % terminal,78 '# path = %s' % PATH[terminal],79 '# options = %s' % OPTIONS[terminal],80 ]81 return '\n'.join(lines)82class TestParseUrl(unittest.TestCase):83 def test_wrong_protocol(self):84 with self.assertRaises(RuntimeError):85 parse_url('scp://root@www.example.com')86 def test_wrong_syntax(self):87 with self.assertRaises(RuntimeError):88 parse_url('ssh:/www.example.com')89 with self.assertRaises(RuntimeError):90 parse_url('ssh//www.example.com')91 def test_domain(self):92 self.assertEqual(93 parse_url('ssh://www.example.com'),94 dict(user=None, host='www.example.com', port=None)95 )96 def test_user_domain(self):97 self.assertEqual(98 parse_url('ssh://root@www.example.com'),99 dict(user='root', host='www.example.com', port=None)100 )101 def test_user_domain_port(self):102 self.assertEqual(103 parse_url('ssh://root@www.example.com:3022'),104 dict(user='root', host='www.example.com', port=3022)105 )106 def test_domain_port(self):107 self.assertEqual(108 parse_url('ssh://www.example.com:3022'),109 dict(user=None, host='www.example.com', port=3022)110 )111 def test_ip(self):112 self.assertEqual(113 parse_url('ssh://192.168.1.1'),114 dict(user=None, host='192.168.1.1', port=None)115 )116 def test_user_ip(self):117 self.assertEqual(118 parse_url('ssh://root@192.168.1.1'),119 dict(user='root', host='192.168.1.1', port=None)120 )121 def test_user_ip_port(self):122 self.assertEqual(123 parse_url('ssh://root@192.168.1.1:3022'),124 dict(user='root', host='192.168.1.1', port=3022)125 )126 def test_ip_port(self):127 self.assertEqual(128 parse_url('ssh://192.168.1.1:3022'),129 dict(user=None, host='192.168.1.1', port=3022)130 )131class TestGetCommandArgs(unittest.TestCase):132 def test_gnome_terminal_domain(self):133 self.assertEqual(134 get_command_args(135 dict(terminal='gnome-terminal', path='gnome-terminal',136 options='--tab --active'),137 dict(user=None, host='www.example.com', port=None),138 ),139 ['gnome-terminal', '--tab', '--active',140 '--title', '"www.example.com"',141 '--command', '"ssh www.example.com"']142 )143 def test_gnome_terminal_user_domain(self):144 self.assertEqual(145 get_command_args(146 dict(terminal='gnome-terminal', path='gnome-terminal',147 options='--tab'),148 dict(user='root', host='www.example.com', port=None)149 ),150 ['gnome-terminal', '--tab',151 '--title', '"www.example.com"',152 '--command', '"ssh root@www.example.com"']153 )154 def test_gnome_terminal_user_domain_port(self):155 self.assertEqual(156 get_command_args(157 dict(terminal='gnome-terminal', path='gnome-terminal',158 options=''),159 dict(user='root', host='www.example.com', port=3022)160 ),161 ['gnome-terminal', '--title', '"www.example.com"',162 '--command', '"ssh root@www.example.com -p 3022"']163 )164 def test_terminator_domain(self):165 self.assertEqual(166 get_command_args(167 dict(terminal='terminator', path='terminator',168 options='--new-tab --maximise'),169 dict(user=None, host='www.example.com', port=None),170 ),171 ['terminator', '--new-tab', '--maximise',172 '--title', '"www.example.com"',173 '--command', '"ssh www.example.com"']174 )175 def test_terminator_user_domain(self):176 self.assertEqual(177 get_command_args(178 dict(terminal='terminator', path='terminator',179 options='--new-tab'),180 dict(user='root', host='www.example.com', port=None)181 ),182 ['terminator', '--new-tab', '--title', '"www.example.com"',183 '--command', '"ssh root@www.example.com"']184 )185 def test_terminator_user_domain_port(self):186 self.assertEqual(187 get_command_args(188 dict(terminal='terminator', path='/usr/bin/terminator',189 options=''),190 dict(user='root', host='www.example.com', port=3022)191 ),192 ['/usr/bin/terminator', '--title', '"www.example.com"',193 '--command', '"ssh root@www.example.com -p 3022"']194 )195 def test_putty_domain(self):196 self.assertEqual(197 get_command_args(198 dict(terminal='putty', path='putty',199 options='-i ~/.ssh/putty_rsa'),200 dict(user=None, host='www.example.com', port=None),201 ),202 ['putty', '-i', '~/.ssh/putty_rsa', 'www.example.com']203 )204 def test_putty_user_domain(self):205 self.assertEqual(206 get_command_args(207 dict(terminal='putty', path='putty', options=''),208 dict(user='root', host='www.example.com', port=None)209 ),210 ['putty', '-l', 'root', 'www.example.com']211 )212 def test_putty_user_domain_port(self):213 self.assertEqual(214 get_command_args(215 dict(terminal='putty', path='putty', options=''),216 dict(user='root', host='www.example.com', port=3022)217 ),218 ['putty', '-l', 'root', '-P', '3022', 'www.example.com']219 )220if __name__ == '__main__':221 from optparse import OptionParser222 default_config = os.path.expanduser(os.path.join('~', '.handler-ssh'))223 usage = 'usage: %prog [options] url'224 parser = OptionParser(usage)225 parser.add_option('-c', '--config', dest='config',226 help='Configuration file. Default: %s' % default_config,227 metavar='FILE', default=default_config)228 parser.add_option('-t', '--terminal', dest='terminal', metavar='TERM',229 choices=TERMINAL_CHOICES,230 help='Terminal client. Choices: %s' % ', '.join(TERMINAL_CHOICES))231 parser.add_option('-v', '--verbose',232 action='store_true', dest='verbose', default=False,233 help='Verbose output')234 parser.add_option('--run-tests',235 action='store_true', dest='tests', default=False,236 help='Run unit tests and exit')237 parser.add_option('--print-config', dest='print_config', metavar='TERM',238 help='Print default configuration. Choices: %s' % ', '.join(TERMINAL_CHOICES))239 (options, args) = parser.parse_args()240 if options.tests:241 sys.argv = sys.argv[0:1]242 exit(unittest.main(argv=None))243 if options.print_config:244 print get_default_config(options.print_config)245 exit(0)246 if len(args) == 0:247 parser.print_usage()248 exit(1)249 parameters = parse_url(args[0])250 config = get_config(options.config, terminal=options.terminal)251 command_args = get_command_args(config, parameters)252 if options.verbose:253 print 'Configuration:', config254 print 'Parameters:', parameters255 print 'Command line:', ' '.join(command_args)...
__init__.py
Source:__init__.py
...71@sw_matcher.handle()72async def _(event: MessageEvent, foo: Message = CommandArg()):73 qid = event.get_user_id()74 ater = get_message_ater(event.raw_message)75 args = get_command_args(foo.extract_plain_text())76 await handle_sw_command(sw_matcher, smmdb, qid, ater, args)77@id_matcher.handle()78async def _(event: MessageEvent, foo: Message = CommandArg()):79 qid = event.get_user_id()80 ater = get_message_ater(event.raw_message)81 args = get_command_args(foo.extract_plain_text())82 await handle_id_command(sw_matcher, smmdb, qid, ater, args)83@bind_matcher.handle()84async def _(event: MessageEvent, foo: Message = CommandArg()):85 qid = event.get_user_id()86 ater = get_message_ater(event.raw_message)87 args = get_command_args(foo.extract_plain_text())88 await handle_bind_command(bind_matcher, smmdb, qid, ater, args)89@default_matcher.handle()90async def _(event: MessageEvent, foo: Message = CommandArg()):91 qid = event.get_user_id()92 ater = get_message_ater(event.raw_message)93 args = get_command_args(foo.extract_plain_text())94 await handle_default_command(default_matcher, smmdb, qid, ater, args)95@card_matcher.handle()96async def _(bot: Bot, event: MessageEvent, foo: Message = CommandArg()):97 qid = event.get_user_id()98 if event.message_type == 'group':99 gid = event.group_id100 else:101 gid = 0102 ater = get_message_ater(event.raw_message)103 args = get_command_args(foo.extract_plain_text())104 sender_nickname = event.sender.card if event.sender.card != '' else event.sender.nickname105 await handle_card_command(bot, card_matcher, smmdb, gid, qid, sender_nickname, ater, args)106@info_matcher.handle()107async def _(bot: Bot, event: MessageEvent, foo: Message = CommandArg()):108 qid = event.get_user_id()109 ater = get_message_ater(event.raw_message)110 args = get_command_args(foo.extract_plain_text())111 if event.message_type == 'group':112 gid = event.group_id113 else:114 gid = 0115 nickname = event.sender.card if event.sender.card != '' else event.sender.nickname116 err = cant_query(smmdb, qid, nickname)117 if err:118 await info_matcher.send(err)119 return120 await handle_info_command(bot, info_matcher, smmdb, qid, ater, args, 1)121@info2_matcher.handle()122async def _(bot: Bot, event: MessageEvent, foo: Message = CommandArg()):123 qid = event.get_user_id()124 ater = get_message_ater(event.raw_message)125 args = get_command_args(foo.extract_plain_text())126 if event.message_type == 'group':127 gid = event.group_id128 else:129 gid = 0130 nickname = event.sender.card if event.sender.card != '' else event.sender.nickname131 err = cant_query(smmdb, qid, nickname)132 if err:133 await info_matcher.send(err)134 return135 await handle_info_command(bot, info2_matcher, smmdb, qid, ater, args, 2)136@detail_matcher.handle()137async def _(bot: Bot, event: MessageEvent, foo: Message = CommandArg()):138 qid = event.get_user_id()139 ater = get_message_ater(event.raw_message)140 args = get_command_args(foo.extract_plain_text())141 if event.message_type == 'group':142 gid = event.group_id143 else:144 gid = 0145 nickname = event.sender.card if event.sender.card != '' else event.sender.nickname146 err = cant_query(smmdb, qid, nickname)147 if err:148 await info_matcher.send(err)149 return150 await handle_detail_command(bot, detail_matcher, smmdb, qid, ater, args)151@post_matcher.handle()152async def _(bot: Bot, event: MessageEvent, foo: Message = CommandArg()):153 qid = event.get_user_id()154 ater = get_message_ater(event.raw_message)155 args = get_command_args(foo.extract_plain_text())156 if event.message_type == 'group':157 gid = event.group_id158 else:159 gid = 0160 nickname = event.sender.card if event.sender.card != '' else event.sender.nickname161 err = cant_query(smmdb, qid, nickname)162 if err:163 await info_matcher.send(err)164 return165 await handle_course_command(bot, post_matcher, smmdb, qid, ater, args, 'post')166@play_matcher.handle()167async def _(bot: Bot, event: MessageEvent, foo: Message = CommandArg()):168 qid = event.get_user_id()169 ater = get_message_ater(event.raw_message)170 args = get_command_args(foo.extract_plain_text())171 if event.message_type == 'group':172 gid = event.group_id173 else:174 gid = 0175 nickname = event.sender.card if event.sender.card != '' else event.sender.nickname176 err = cant_query(smmdb, qid, nickname)177 if err:178 await info_matcher.send(err)179 return180 await handle_course_command(bot, play_matcher, smmdb, qid, ater, args, 'play')181@like_matcher.handle()182async def _(bot: Bot, event: MessageEvent, foo: Message = CommandArg()):183 qid = event.get_user_id()184 ater = get_message_ater(event.raw_message)185 args = get_command_args(foo.extract_plain_text())186 if event.message_type == 'group':187 gid = event.group_id188 else:189 gid = 0190 nickname = event.sender.card if event.sender.card != '' else event.sender.nickname191 err = cant_query(smmdb, qid, nickname)192 if err:193 await info_matcher.send(err)194 return195 await handle_course_command(bot, like_matcher, smmdb, qid, ater, args, 'like')196@first_matcher.handle()197async def _(bot: Bot, event: MessageEvent, foo: Message = CommandArg()):198 qid = event.get_user_id()199 ater = get_message_ater(event.raw_message)200 args = get_command_args(foo.extract_plain_text())201 if event.message_type == 'group':202 gid = event.group_id203 else:204 gid = 0205 nickname = event.sender.card if event.sender.card != '' else event.sender.nickname206 err = cant_query(smmdb, qid, nickname)207 if err:208 await info_matcher.send(err)209 return210 await handle_course_command(bot, first_matcher, smmdb, qid, ater, args, 'first')211@record_matcher.handle()212async def _(bot: Bot, event: MessageEvent, foo: Message = CommandArg()):213 qid = event.get_user_id()214 ater = get_message_ater(event.raw_message)215 args = get_command_args(foo.extract_plain_text())216 if event.message_type == 'group':217 gid = event.group_id218 else:219 gid = 0220 nickname = event.sender.card if event.sender.card != '' else event.sender.nickname221 err = cant_query(smmdb, qid, nickname)222 if err:223 await info_matcher.send(err)224 return225 await handle_course_command(bot, record_matcher, smmdb, qid, ater, args, 'record')226@view_matcher.handle()227async def _(bot: Bot, event: MessageEvent, foo: Message = CommandArg()):228 qid = event.get_user_id()229 ater = get_message_ater(event.raw_message)230 args = get_command_args(foo.extract_plain_text())231 if event.message_type == 'group':232 gid = event.group_id233 else:234 gid = 0235 nickname = event.sender.card if event.sender.card != '' else event.sender.nickname236 err = cant_query(smmdb, qid, nickname)237 if err:238 await info_matcher.send(err)239 return240 await handle_view_command(bot, view_matcher, smmdb, qid, ater, args)241@help_matcher.handle()242async def _():...
test_playbooks.py
Source:test_playbooks.py
...23 super(TestArgProcessing, self).setUp()24 self.conf = self.useFixture(oslo_fixture.Config(cfg.CONF))25 self.conf.config(group='paths', playbooks_dir='data/ardana-ansible')26 def test_dashes(self):27 args = playbooks.get_command_args({28 "key1": "val1",29 "--key2": "val2"})30 self.assertEqual("val1", args["--key1"])31 self.assertEqual("val2", args["--key2"])32 self.assertNotIn("key1", args)33 def test_extra_vars_as_a_list(self):34 args = playbooks.get_command_args({35 "extra-vars": ["key1=var1", "key2=var2"]})36 expected = json.loads('{"key1": "var1", "key2": "var2"}')37 actual = json.loads(args["--extra-vars"])38 self.assertEqual(expected, actual)39 def test_extra_vars_as_a_dict(self):40 d = {"key1": "var1", "key2": "var2"}41 args = playbooks.get_command_args({"extra-vars": d})42 actual = json.loads(args["--extra-vars"])43 self.assertEqual(d, actual)44 def test_default_inventory(self):45 args = playbooks.get_command_args({"foo": "bar"})46 self.assertEqual("hosts/verb_hosts", args['--inventory'])47 def test_pre_inventory(self):48 args = playbooks.get_command_args({"foo": "bar"},49 CONF.paths.pre_playbooks_dir)50 self.assertEqual("hosts/localhost", args['--inventory'])51 def test_force_omit_inventory(self):52 args = playbooks.get_command_args({'inventory': None})53 self.assertNotIn('--inventory', args)54 def test_override_inventory(self):55 args = playbooks.get_command_args({'--inventory': 'foo'})56 self.assertEqual("foo", args['--inventory'])57 def test_vault_pass_extra_vars_as_dict(self):58 dict = {"encrypt": "testdddd"}59 args = playbooks.get_command_args({"extra-vars": dict})60 self.assertIn('--vault-password-file', args)61 def test_no_vault_pass_extra_vars_as_dict(self):62 dict = {"encrypt": ""}63 args = playbooks.get_command_args({"extra-vars": dict})64 self.assertNotIn('--vault-password-file', args)65 def test_vault_pass_extra_vars_as_list(self):66 list = ["encrypt=testdddd"]67 args = playbooks.get_command_args({"extra-vars": list})68 self.assertIn('--vault-password-file', args)69 def test_no_vault_pass_extra_vars_as_list(self):70 list = ["encrypt="]71 args = playbooks.get_command_args({"extra-vars": list})72 self.assertNotIn('--vault-password-file', args)73 def test_vault_pass_deprecated_0(self):74 args = playbooks.get_command_args({"encryptionKey": "testabcd"})75 self.assertIn('--vault-password-file', args)76 def test_vault_pass_deprecated_1(self):77 args = playbooks.get_command_args({"encryption-key": "testabce"})78 self.assertIn('--vault-password-file', args)79 def test_vault_pass_deprecated_empty_0(self):80 args = playbooks.get_command_args({"encryptionKey": ""})81 self.assertNotIn('--vault-password-file', args)82 def test_vault_pass_deprecated_empty_1(self):83 args = playbooks.get_command_args({"encryption-key": ""})84 self.assertNotIn('--vault-password-file', args)85 def test_has_no_verbose(self):86 args = playbooks.get_command_args({"verbose": "0"})87 self.assertNotIn("--verbose", args)88 def test_has_verbose(self):89 args = playbooks.get_command_args({"verbose": "4"})90 self.assertEqual("4", args['--verbose'])91 def test_verbose_command(self):92 args = playbooks.get_command_args({"verbose": "3"})93 cmdline = playbooks.build_command_line('testcommand', 'testplaybook',94 args)95 num = cmdline.count('--verbose')...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!