def test_action(self): """ Test simple action execution :return: None """ self.print_header() a = Action() if os.name == 'nt': a.command = r'libexec\\dummy_command.cmd' else: a.command = "libexec/dummy_command.sh" assert a.got_shell_characters() == False # Run the action script a.execute() assert 'launched' == a.status # Wait action execution end self.wait_finished(a) assert 0 == a.exit_status assert 'done' == a.status assert "Hi, I'm for testing only. Please do not use me directly, really" == a.output assert "" == a.long_output assert "Hip=99% Bob=34mm" == a.perf_data
def test_huge_output(self): """ Test huge output We got problems on LARGE output, more than 64K in fact. We try to solve it with the fcntl and non blocking read instead of "communicate" mode. So here we try to get a 100K output. Should NOT be in a timeout :return: None """ # Set max output length max_output_length = 131072 a = Action() a.timeout = 15 a.command = r"""python -u -c 'print("."*%d)'""" % max_output_length ### ### 1 - output is less than the max output ### # Run the action script a.execute() assert 'launched' == a.status # Wait action execution end and set the max output we want for the command self.wait_finished(a, size=max_output_length + 1) assert 0 == a.exit_status assert 'done' == a.status assert "."*max_output_length == a.output assert "" == a.long_output assert "" == a.perf_data ### ### 2 - output is equal to the max output ### # Run the action script a.execute() assert 'launched' == a.status # Wait action execution end and set the max output we want for the command self.wait_finished(a, size=max_output_length) assert 0 == a.exit_status assert 'done' == a.status assert "."*max_output_length == a.output assert "" == a.long_output assert "" == a.perf_data ### ### 3 - output is more than the max output ### # Run the action script a.execute() assert 'launched' == a.status # Wait action execution end and set the max output we want for the command self.wait_finished(a, size=max_output_length - 10) assert 0 == a.exit_status assert 'done' == a.status assert "."*(max_output_length - 10) == a.output assert "" == a.long_output assert "" == a.perf_data
def test_got_unclosed_quote(self): """ Test unclosed quote in the command :return: None """ self.print_header() # https://github.com/naparuba/shinken/issues/155 a = Action() a.command = "libexec/dummy_command_nobang.sh -a 'wwwwzzzzeeee" # Run the action script a.execute() if sys.version_info < (2, 7): # cygwin: /bin/sh: -c: line 0: unexpected EOF while looking for matching' # ubuntu: /bin/sh: Syntax error: Unterminated quoted string print("Status: %s" % a.status) print("Output: %s" % a.output) print("Exit code: %s" % a.exit_status) # Do not wait for end because it did not really started ... # Todo: Python 2.6 different behavior ... but it will be deprecated soon, # so do not care with this now assert 'launched' == a.status assert "" == a.output assert 3 == a.exit_status else: # Do not wait for end because it did not really started ... assert 'done' == a.status assert 'Not a valid shell command: No closing quotation' == a.output assert 3 == a.exit_status
def test_noshell_bang_command(self): """ Test no shebang in the command script Some commands are shell without bangs! (like in Centreon...) We can detect it in the launch, and it should be managed :return: None """ self.print_header() a = Action() a.command = "libexec/dummy_command_nobang.sh" assert False == a.got_shell_characters() a.execute() # Run the action script a.execute() assert 'launched' == a.status # Wait action execution end self.wait_finished(a) assert 0 == a.exit_status assert 'done' == a.status assert "Hi, I'm for testing only. Please do not use me directly, really" == a.output assert "" == a.long_output assert "Hip=99% Bob=34mm" == a.perf_data
def test_huge_output(self): """ Test huge output We got problems on LARGE output, more than 64K in fact. We try to solve it with the fcntl and non blocking read instead of "communicate" mode. So here we try to get a 100K output. Should NOT be in a timeout :return: None """ # Set max output length max_output_length = 131072 a = Action() a.timeout = 15 a.command = r"""python -u -c 'print("."*%d)'""" % max_output_length ### ### 1 - output is less than the max output ### # Run the action script a.execute() assert 'launched' == a.status # Wait action execution end and set the max output we want for the command self.wait_finished(a, size=max_output_length + 1) assert 0 == a.exit_status assert 'done' == a.status assert "." * max_output_length == a.output assert "" == a.long_output assert "" == a.perf_data ### ### 2 - output is equal to the max output ### # Run the action script a.execute() assert 'launched' == a.status # Wait action execution end and set the max output we want for the command self.wait_finished(a, size=max_output_length) assert 0 == a.exit_status assert 'done' == a.status assert "." * max_output_length == a.output assert "" == a.long_output assert "" == a.perf_data ### ### 3 - output is more than the max output ### # Run the action script a.execute() assert 'launched' == a.status # Wait action execution end and set the max output we want for the command self.wait_finished(a, size=max_output_length - 10) assert 0 == a.exit_status assert 'done' == a.status assert "." * (max_output_length - 10) == a.output assert "" == a.long_output assert "" == a.perf_data
def test_non_zero_exit_status_empty_output_but_non_empty_stderr(self): a = Action() a.command = "echo hooo >&2 ; exit 1" a.timeout = 10 a.env = {} # :fixme: this sould be pre-set in Action.__init__() a.execute() self.wait_finished(a) self.assertEqual(a.output, "hooo")
def test_action_timeout(self): """ Test simple action execution - fail on timeout :return: None """ # Normal esxecution # ----------------- a = Action() # Expect no more than 30 seconds execution time a.timeout = 30 # Action is sleeping for 10 seconds a.command = "libexec/sleep_command.sh 10" # Run the action script a.execute() assert 'launched' == a.status # Wait action execution end, not more than 5 secondes self.wait_finished(a, timeout=30) assert 0 == a.exit_status assert 'done' == a.status assert "I start sleeping for 10 seconds..." == a.output assert "I awoke after sleeping 10 seconds" == a.long_output assert "sleep=10" == a.perf_data # Too long esxecution # ------------------- a = Action() # Expect no more than 5 seconds execution time a.timeout = 5 # Action is sleeping for 10 seconds a.command = "libexec/sleep_command.sh 10" # Run the action script a.execute() assert 'launched' == a.status # Wait action execution end, not more than 5 secondes self.wait_finished(a, timeout=10) assert 3 == a.exit_status assert 'timeout' == a.status assert "I start sleeping for 10 seconds..." == a.output assert "" == a.long_output assert "" == a.perf_data
def test_action(self): a = Action() a.timeout = 10 a.env = {} if os.name == 'nt': a.command = r'libexec\\dummy_command.cmd' else: a.command = "libexec/dummy_command.sh" self.assertEqual(False, a.got_shell_characters()) a.execute() self.assertEqual('launched', a.status) # Give also the max output we want for the command self.wait_finished(a) self.assertEqual(0, a.exit_status) self.assertEqual('done', a.status) print a.output self.assertEqual("Hi, I'm for testing only. Please do not use me directly, really", a.output) self.assertEqual("Hip=99% Bob=34mm", a.perf_data)
def test_execve_fail_with_utf8(self): if os.name == 'nt': return a = Action() a.timeout = 10 a.env = {} # :fixme: this sould be pre-set in Action.__init__() a.command = u"/bin/echo Wiadomo\u015b\u0107" a.execute() self.wait_finished(a) #print a.output self.assertEqual(a.output.decode('utf8'), u"Wiadomo\u015b\u0107")
def test_noshell_bang_command(self): a = Action() a.timeout = 10 a.command = "libexec/dummy_command_nobang.sh" a.env = {} if os.name == 'nt': return self.assertEqual(False, a.got_shell_characters()) a.execute() self.assertEqual('launched', a.status) self.wait_finished(a) print "F**k", a.status, a.output self.assertEqual(0, a.exit_status) self.assertEqual('done', a.status)
def test_huge_output(self): a = Action() a.timeout = 5 a.env = {} if os.name == 'nt': a.command = r"""python -c 'print "A"*1000000'""" # FROM NOW IT4S FAIL ON WINDOWS :( return else: a.command = r"""python -u -c 'print "A"*100000'""" print "EXECUTE" a.execute() print "EXECUTE FINISE" self.assertEqual('launched', a.status) # Give also the max output we want for the command self.wait_finished(a, 10000000000) print "Status?", a.exit_status self.assertEqual(0, a.exit_status) print "Output", len(a.output) self.assertEqual(0, a.exit_status) self.assertEqual('done', a.status) self.assertEqual("A"*100000, a.output) self.assertEqual("", a.perf_data)
def test_got_pipe_shell_characters(self): a = Action() a.timeout = 10 a.command = "libexec/dummy_command_nobang.sh | grep 'Please do not use me directly'" a.env = {} if os.name == 'nt': return self.assertEqual(True, a.got_shell_characters()) a.execute() self.assertEqual('launched', a.status) self.wait_finished(a) print "F**k", a.status, a.output self.assertEqual(0, a.exit_status) self.assertEqual('done', a.status)
def test_got_unclosed_quote(self): """ Test unclosed quote in the command :return: None """ # https://github.com/naparuba/shinken/issues/155 a = Action() a.command = "libexec/dummy_command_nobang.sh -a 'wwwwzzzzeeee" # Run the action script with pytest.raises(ActionError): a.execute() # Do not wait for end because it did not really started ... assert 'done' == a.status assert 'Not a valid shell command: No closing quotation' == a.output assert 3 == a.exit_status
def test_grep_for_environment_variables(self): """ Test grep for environment variables :return: None """ a = Action() a.command = "/usr/bin/env | grep ALIGNAK_TEST_VARIABLE" assert 'ALIGNAK_TEST_VARIABLE' not in a.get_local_environnement() a.env = {'ALIGNAK_TEST_VARIABLE': 'is now existing and defined'} assert 'ALIGNAK_TEST_VARIABLE' in a.get_local_environnement() assert a.get_local_environnement()['ALIGNAK_TEST_VARIABLE'] == 'is now existing and defined' # Execute action a.execute() self.wait_finished(a) assert a.output == 'ALIGNAK_TEST_VARIABLE=is now existing and defined'
def test_grep_for_environment_variables(self): if os.name == 'nt': return a = Action() a.timeout = 10 a.env = {} # :fixme: this sould be pre-set in Action.__init__() a.command = "/usr/bin/env | grep TITI" self.assertNotIn('TITI', a.get_local_environnement()) a.env = {'TITI': 'est en vacance'} self.assertIn('TITI', a.get_local_environnement()) self.assertEqual(a.get_local_environnement()['TITI'], 'est en vacance' ) a.execute() self.wait_finished(a) self.assertEqual(a.output, 'TITI=est en vacance')
def test_grep_for_environment_variables(self): """ Test grep for environment variables :return: None """ a = Action() a.command = "/usr/bin/env | grep ALIGNAK_TEST_VARIABLE" assert 'ALIGNAK_TEST_VARIABLE' not in a.get_local_environnement() a.env = {'ALIGNAK_TEST_VARIABLE': 'is now existing and defined'} assert 'ALIGNAK_TEST_VARIABLE' in a.get_local_environnement() assert a.get_local_environnement( )['ALIGNAK_TEST_VARIABLE'] == 'is now existing and defined' # Execute action a.execute() self.wait_finished(a) assert a.output == 'ALIGNAK_TEST_VARIABLE=is now existing and defined'
def test_non_zero_exit_status_empty_output_but_non_empty_stderr(self): """ Test catch stdout and stderr :return: None """ a = Action() a.command = "echo Output to stderr >&2 ; exit 1" # Run the action script a.execute() assert 'launched' == a.status # Wait action execution end and set the max output we want for the command self.wait_finished(a) assert 1 == a.exit_status assert 'done' == a.status assert "Output to stderr" == a.output assert "" == a.long_output assert "" == a.perf_data
def test_echo_environment_variables(self): """ Test echo environment variables :return: None """ self.print_header() a = Action() a.command = "echo $ALIGNAK_TEST_VARIABLE" assert 'ALIGNAK_TEST_VARIABLE' not in a.get_local_environnement() a.env = {'ALIGNAK_TEST_VARIABLE': 'is now existing and defined'} assert 'ALIGNAK_TEST_VARIABLE' in a.get_local_environnement() assert a.get_local_environnement( )['ALIGNAK_TEST_VARIABLE'] == 'is now existing and defined' # Execute action a.execute() self.wait_finished(a) assert a.output == 'is now existing and defined'
def test_got_pipe_shell_characters(self): """ Test pipe shell character in the command :return: None """ a = Action() a.command = "libexec/dummy_command_nobang.sh | grep 'I will not match this search!'" assert True == a.got_shell_characters() # Run the action script a.execute() assert 'launched' == a.status # Wait action execution end self.wait_finished(a) assert 1 == a.exit_status assert 'done' == a.status assert "" == a.output assert "" == a.long_output assert "" == a.perf_data
def test_got_unclosed_quote(self): # https://github.com/naparuba/shinken/issues/155 a = Action() a.timeout = 10 a.command = "libexec/dummy_command_nobang.sh -a 'wwwwzzzzeeee" a.env = {} if os.name == 'nt': return a.execute() self.wait_finished(a) self.assertEqual('done', a.status) print "F**k", a.status, a.output if sys.version_info < (2, 7): # cygwin: /bin/sh: -c: line 0: unexpected EOF while looking for matching' # ubuntu: /bin/sh: Syntax error: Unterminated quoted string self.assertTrue(a.output.startswith("/bin/sh")) self.assertEqual(3, a.exit_status) else: self.assertEqual('Not a valid shell command: No closing quotation', a.output) self.assertEqual(3, a.exit_status)
def test_got_shell_characters(self): """ Test shell characters in the command (&>...) :return: None """ a = Action() a.command = "libexec/dummy_command_nobang.sh && echo finished ok" assert True == a.got_shell_characters() # Run the action script a.execute() assert 'launched' == a.status # Wait action execution end self.wait_finished(a) assert 0 == a.exit_status assert 'done' == a.status assert "Hi, I'm for testing only. Please do not use me directly, really" == a.output assert "finished ok" == a.long_output assert "Hip=99% Bob=34mm" == a.perf_data
def test_execve_fail_with_utf8(self): """ Test execve fail with utf8 :return: None """ self.print_header() a = Action() a.command = u"/bin/echo Wiadomo\u015b\u0107" # Run the action script a.execute() assert 'launched' == a.status # Wait action execution end and set the max output we want for the command self.wait_finished(a) assert 0 == a.exit_status assert 'done' == a.status assert u"Wiadomo\u015b\u0107" == a.output assert "" == a.long_output assert "" == a.perf_data
def test_action(self): """ Test simple action execution :return: None """ a = Action() a.command = "libexec/dummy_command.sh" assert a.got_shell_characters() == False # Run the action script a.execute() assert 'launched' == a.status # Wait action execution end self.wait_finished(a) assert 3 == a.exit_status assert 'done' == a.status assert "Hi, I'm for testing only. Please do not use me directly, really" == a.output assert "" == a.long_output assert "Hip=99% Hop=34mm" == a.perf_data
def test_noshell_bang_command(self): """ Test no shebang in the command script Some commands are shell without bangs! (like in Centreon...) We can detect it in the launch, and it should be managed :return: None """ a = Action() a.command = "libexec/dummy_command_nobang.sh" assert False == a.got_shell_characters() a.execute() # Run the action script a.execute() assert 'launched' == a.status # Wait action execution end self.wait_finished(a) assert 0 == a.exit_status assert 'done' == a.status assert "Hi, I'm for testing only. Please do not use me directly, really" == a.output assert "" == a.long_output assert "Hip=99% Bob=34mm" == a.perf_data
def test_start_do_not_fail_with_utf8(self): """ Test command process do not fail with utf8 :return: None """ # 1 - French a = Action() # A French text - note the double quotes escaping! a.command = u"/bin/echo \"Les naïfs ægithales hâtifs pondant à Noël où il gèle sont sûrs " \ u"d'être déçus en voyant leurs drôles d'œufs abîmés.\"" # Run the action script a.execute() # Wait action execution end and set the max output we want for the command self.wait_finished(a) assert 0 == a.exit_status assert 'done' == a.status assert u"Les naïfs ægithales hâtifs pondant à Noël où il gèle sont sûrs " \ u"d'être déçus en voyant leurs drôles d'œufs abîmés." == a.output assert "" == a.long_output assert "" == a.perf_data # 2 - Russian sentence a = Action() # A russian text a.command = u"/bin/echo На берегу пустынных волн" # Run the action script a.execute() # Wait action execution end and set the max output we want for the command self.wait_finished(a) assert 0 == a.exit_status assert 'done' == a.status assert u"На берегу пустынных волн" == a.output assert "" == a.long_output assert "" == a.perf_data # 3 - Russian text a = Action() # A russian text (long output) a.command = u"/bin/echo 'На берегу пустынных волн\n" \ u"Стоял он, дум великих полн,\n" \ u"И вдаль глядел. Пред ним широко\n" \ u"Река неслася; бедный чёлн\n" \ u"По ней стремился одиноко.\n" \ u"По мшистым, топким берегам\n" \ u"Чернели избы здесь и там,\n" \ u"Приют убогого чухонца;\n" \ u"И лес, неведомый лучам\n" \ u"В тумане спрятанного солнца,\n" \ u"Кругом шумел.'" # Run the action script a.execute() assert 'launched' == a.status # Wait action execution end and set the max output we want for the command self.wait_finished(a) assert 0 == a.exit_status assert 'done' == a.status assert u"На берегу пустынных волн" == a.output assert u"Стоял он, дум великих полн,\n" \ u"И вдаль глядел. Пред ним широко\n" \ u"Река неслася; бедный чёлн\n" \ u"По ней стремился одиноко.\n" \ u"По мшистым, топким берегам\n" \ u"Чернели избы здесь и там,\n" \ u"Приют убогого чухонца;\n" \ u"И лес, неведомый лучам\n" \ u"В тумане спрятанного солнца,\n" \ u"Кругом шумел." == a.long_output assert "" == a.perf_data