def __is_service_running(self): shell_command = "systemctl status {0} | grep -E '^ Active: active \(running\)'".format( self.mock_service_name) try: shell_command_results = Shell.execute_shell_command(shell_command) return True except ShellCommandException: return False
def test__demote__success__service_not_running(self): if self.__is_service_running(): self.__stop_mock_service() shell_command = "bash {0} demote".format(self.shell_script_path) shell_command_results = Shell.execute_shell_command(shell_command, env=self.env) self.assertEqual(0, shell_command_results.ExitCode) self.assertFalse(self.__is_service_running()) if self.__is_service_running(): self.__stop_mock_service()
def test__stop__success__already_started(self): if not self.__is_service_running(): self.__start_mock_service() shell_command = "bash {0} stop".format(self.shell_script_path) shell_command_results = Shell.execute_shell_command(shell_command, env=self.env) self.assertEqual(0, shell_command_results.ExitCode) self.assertTrue( "Stopping the service succeeded" in shell_command_results.Stderr) self.assertFalse(self.__is_service_running()) shell_command_results = Shell.execute_shell_command(shell_command, env=self.env) self.assertEqual(0, shell_command_results.ExitCode) self.assertTrue( "Stopping the service succeeded" in shell_command_results.Stderr) self.assertFalse(self.__is_service_running()) if self.__is_service_running(): self.__stop_mock_service()
def test__demote__success__service_running(self): if not self.__is_service_running(): self.__start_mock_service() shell_command = "bash {0} demote".format(self.shell_script_path) shell_command_results = Shell.execute_shell_command(shell_command, env=self.env) self.assertEqual(0, shell_command_results.ExitCode) self.assertTrue("Demoting to slave" in shell_command_results.Stdout) self.assertTrue(self.__is_service_running()) if self.__is_service_running(): self.__stop_mock_service()
def test__monitor__success__slave(self): if not self.__is_service_running(): self.__start_mock_service() tmp_env = self.env.copy() self.env["OCF_RESKEY_monitor_script"] = os.path.join( self.current_dir, "MockService", "monitor-slave.sh") shell_command = "bash {0} monitor".format(self.shell_script_path) shell_command_results = Shell.execute_shell_command(shell_command, env=self.env) self.assertEqual(0, shell_command_results.ExitCode) if self.__is_service_running(): self.__stop_mock_service()
def test__usage__success__no_args_supplied(self): if self.__is_service_running(): self.__stop_mock_service() try: shell_command = "bash {0}".format(self.shell_script_path) shell_command_results = Shell.execute_shell_command(shell_command, env=self.env) except ShellCommandException as scex: self.assertEqual(2, scex.ExitCode) self.assertTrue( "start|stop|monitor|meta-data|promote|demote" in scex.Stdout) self.assertEqual("", scex.Stderr) self.assertFalse(self.__is_service_running()) if self.__is_service_running(): self.__stop_mock_service()
def test__meta_data__success(self): if self.__is_service_running(): self.__stop_mock_service() shell_command = "bash {0} meta-data".format(self.shell_script_path) shell_command_results = Shell.execute_shell_command(shell_command, env=self.env) with open(os.path.join(self.root_dir, 'src', 'pfra-metadata.xml'), 'r') as xml_file: xml = xml_file.read() self.assertEqual(0, shell_command_results.ExitCode) self.assertEqual(xml, shell_command_results.Stdout) self.assertFalse(self.__is_service_running()) if self.__is_service_running(): self.__stop_mock_service()
def test__monitor__success__master(self): if not self.__is_service_running(): self.__start_mock_service() tmp_env = self.env.copy() self.env["OCF_RESKEY_monitor_script"] = os.path.join( self.current_dir, "MockService", "monitor-master.sh") shell_command = "bash {0} monitor".format(self.shell_script_path) with self.assertRaises( ShellCommandException) as shell_command_exception_context: shell_command_results = Shell.execute_shell_command(shell_command, env=self.env) shell_command_exception = shell_command_exception_context.exception self.assertEqual(8, shell_command_exception.ExitCode) if self.__is_service_running(): self.__stop_mock_service()
def test__demote__Failure__script_returns_non_zero_exit(self): if not self.__is_service_running(): self.__start_mock_service() tmp_env = dict(self.env) tmp_env["OCF_RESKEY_demote_script"] = os.path.join( self.current_dir, "MockService", "demote-non-zero-exit.sh") shell_command = "bash {0} demote".format(self.shell_script_path) with self.assertRaises(ShellCommandException) as scex_context: shell_command_results = Shell.execute_shell_command(shell_command, env=tmp_env) scex = scex_context.exception self.assertEqual(12, scex.ExitCode) self.assertTrue(self.__is_service_running()) if self.__is_service_running(): self.__stop_mock_service() self.env["OCF_RESKEY_promote_script"] = self.promote_script_path
def test__demote__Failure__script_does_not_exist(self): if self.__is_service_running(): self.__stop_mock_service() tmp_env = self.env.copy() tmp_env["OCF_RESKEY_demote_script"] = os.path.join( 'tmp', "this_does_not_exist.sh") shell_command = "bash {0} demote".format(self.shell_script_path) with self.assertRaises(ShellCommandException) as scex_context: shell_command_results = Shell.execute_shell_command(shell_command, env=tmp_env) scex = scex_context.exception self.assertEqual(1, scex.ExitCode) self.assertTrue("script does not exist" in scex.Stderr) self.assertFalse(self.__is_service_running()) if self.__is_service_running(): self.__stop_mock_service() self.env["OCF_RESKEY_demote_script"] = self.demote_script_path
def __start_mock_service(self): Shell.execute_shell_command("systemctl start {0}".format( self.mock_service_name))