<s> import eventlet <EOL> from mock import patch , DEFAULT <EOL> import pytest <EOL> from nameko . cli . backdoor import main <EOL> from nameko . cli . main import setup_parser <EOL> from nameko . cli . run import setup_backdoor <EOL> from nameko . exceptions import CommandError <EOL> @ pytest . fixture <EOL> def running_backdoor ( ) : <EOL> runner = object ( ) <EOL> green_socket , gt = setup_backdoor ( runner , <NUM_LIT:0> ) <EOL> eventlet . sleep ( <NUM_LIT:0> ) <EOL> socket_name = green_socket . fd . getsockname ( ) <EOL> return socket_name <EOL> def test_no_telnet ( ) : <EOL> parser = setup_parser ( ) <EOL> args = parser . parse_args ( [ '<STR_LIT>' , '<STR_LIT:0>' ] ) <EOL> with patch ( '<STR_LIT>' ) as mock_os : <EOL> mock_os . system . return_value = - <NUM_LIT:1> <EOL> with pytest . raises ( CommandError ) as exc : <EOL> main ( args ) <EOL> assert '<STR_LIT>' in str ( exc ) <EOL> def test_no_running_backdoor ( ) : <EOL> parser = setup_parser ( ) <EOL> args = parser . parse_args ( [ '<STR_LIT>' , '<STR_LIT:0>' ] ) <EOL> with patch . multiple ( <EOL> '<STR_LIT>' , call = DEFAULT , os = DEFAULT <EOL> ) as mocks : <EOL> mocks [ '<STR_LIT>' ] . system . return_value = <NUM_LIT:0> <EOL> mocks [ '<STR_LIT>' ] . return_value = - <NUM_LIT:1> <EOL> with pytest . raises ( CommandError ) as exc : <EOL> main ( args ) <EOL> assert '<STR_LIT>' in str ( exc ) <EOL> def test_basic ( running_backdoor ) : <EOL> socket_arg = '<STR_LIT>' . format ( * running_backdoor ) <EOL> parser = setup_parser ( ) <EOL> args = parser . parse_args ( [ '<STR_LIT>' , socket_arg ] ) <EOL> with patch . multiple ( <EOL> '<STR_LIT>' , call = DEFAULT , os = DEFAULT <EOL> ) as mocks : <EOL> mock_call = mocks [ '<STR_LIT>' ] <EOL> mocks [ '<STR_LIT>' ] . system . return_value = <NUM_LIT:0> <EOL> mock_call . return_value = <NUM_LIT:0> <EOL> main ( args ) <EOL> ( cmd , ) , _ = mock_call . call_args <EOL> expected = ( <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] + <EOL> list ( map ( str , running_backdoor ) ) + <EOL> [ '<STR_LIT>' ] <EOL> ) <EOL> assert cmd == expected <EOL> def test_default_host ( running_backdoor ) : <EOL> _ , port = running_backdoor <EOL> parser = setup_parser ( ) <EOL> args = parser . parse_args ( [ '<STR_LIT>' , str ( port ) ] ) <EOL> with patch . multiple ( <EOL> '<STR_LIT>' , call = DEFAULT , os = DEFAULT <EOL> ) as mocks : <EOL> mock_call = mocks [ '<STR_LIT>' ] <EOL> mocks [ '<STR_LIT>' ] . system . return_value = <NUM_LIT:0> <EOL> mock_call . return_value = <NUM_LIT:0> <EOL> main ( args ) <EOL> ( cmd , ) , _ = mock_call . call_args <EOL> expected = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:localhost>' ] + [ str ( port ) ] + [ '<STR_LIT>' ] <EOL> assert cmd == expected <EOL> def test_stop ( running_backdoor ) : <EOL> _ , port = running_backdoor <EOL> parser = setup_parser ( ) <EOL> args = parser . parse_args ( [ '<STR_LIT>' , str ( port ) ] ) <EOL> with patch . multiple ( <EOL> '<STR_LIT>' , call = DEFAULT , os = DEFAULT <EOL> ) as mocks : <EOL> mocks [ '<STR_LIT>' ] . system . side_effect = [ - <NUM_LIT:1> , - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> mocks [ '<STR_LIT>' ] . side_effect = [ KeyboardInterrupt , <NUM_LIT:0> ] <EOL> main ( args ) </s>
