def setUp(self): self.tmpdir = tempfile.mkdtemp() self.tmpdict = tempfile.NamedTemporaryFile(dir=self.tmpdir) self.tmpdict = open(self.tmpdir + os.sep + MAPPING_FILE_NAME, 'w') self.sh = shellRunner() #Launch eternal process p = subprocess.Popen([COMPONENT_LIVE_CMD], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, close_fds=True) #Write mapping for pid files for both live and dead process self.tmpdict.write(COMPONENT_LIVE + '=' + COMPONENT_LIVE_PID + os.linesep) self.tmpdict.write(COMPONENT_DEAD + '=' + COMPONENT_DEAD_PID + os.linesep) self.tmpdict.close() #Write pid of live process to file live_pid_file = open(self.tmpdir + os.sep + COMPONENT_LIVE_PID, 'w') self.live_pid = p.pid live_pid_file.write(str(self.live_pid)) live_pid_file.close() #Write pid of dead process to file dead_pid_file = open(self.tmpdir + os.sep + COMPONENT_DEAD_PID, 'w') dead_pid_file.write(str(DEAD_PID)) dead_pid_file.close() #Init status checker self.statusCheck = StatusCheck(self.tmpdir, self.tmpdict.name)
def test_dead(self, get_is_live_mock): statusCheck = StatusCheck(self.serviceToPidDict, self.pidPathVars, self.globalConfig, self.servicesToLinuxUser) statusCheck.pidFilesDict = self.pidFilesDict get_is_live_mock.side_effect = lambda pid_path : self.is_live_values[pid_path] status = statusCheck.getStatus(COMPONENT_DEAD) self.assertEqual(status, False)
def test_dead(self, get_is_live_mock): statusCheck = StatusCheck(self.serviceToPidDict, self.pidPathesVars, self.globalConfig, self.servicesToLinuxUser) statusCheck.pidFilesDict = self.pidFilesDict get_is_live_mock.side_effect = lambda pid_path : self.is_live_values[pid_path] status = statusCheck.getStatus(COMPONENT_DEAD) self.assertEqual(status, False)
class TestStatusCheck(TestCase): def setUp(self): self.tmpdir = tempfile.mkdtemp() self.tmpdict = tempfile.NamedTemporaryFile(dir=self.tmpdir) self.tmpdict = open(self.tmpdir + os.sep + MAPPING_FILE_NAME, 'w') self.sh = shellRunner() #Launch eternal process p = subprocess.Popen([COMPONENT_LIVE_CMD], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, close_fds=True) #Write mapping for pid files for both live and dead process self.tmpdict.write(COMPONENT_LIVE + '=' + COMPONENT_LIVE_PID + os.linesep) self.tmpdict.write(COMPONENT_DEAD + '=' + COMPONENT_DEAD_PID + os.linesep) self.tmpdict.close() #Write pid of live process to file live_pid_file = open(self.tmpdir + os.sep + COMPONENT_LIVE_PID, 'w') self.live_pid = p.pid live_pid_file.write(str(self.live_pid)) live_pid_file.close() #Write pid of dead process to file dead_pid_file = open(self.tmpdir + os.sep + COMPONENT_DEAD_PID, 'w') dead_pid_file.write(str(DEAD_PID)) dead_pid_file.close() #Init status checker self.statusCheck = StatusCheck(self.tmpdir, self.tmpdict.name) # Ensure that status checker throws exceptions on invalid params def test_exceptions(self): self.assertRaises(ValueError, StatusCheck, "tmp", "tmp") self.assertRaises(IOError, StatusCheck, self.tmpdir, "tmp") # Ensure that status checker return True for running process def test_live(self): status = self.statusCheck.getStatus(COMPONENT_LIVE) self.assertEqual(status, True) # Ensure that status checker return False for dead process def test_dead(self): status = self.statusCheck.getStatus(COMPONENT_DEAD) self.assertEqual(status, False) def tearDown(self): os.kill(self.live_pid, signal.SIGKILL) shutil.rmtree(self.tmpdir)
def test_live(self, get_is_live_mock): statusCheck = StatusCheck(self.serviceToPidDict, self.pidPathesVars, {}, AmbariConfig.linuxUserPattern) statusCheck.pidFilesDict = self.pidFilesDict get_is_live_mock.side_effect = lambda pid_path: self.is_live_values[ pid_path] status = statusCheck.getStatus(COMPONENT_LIVE) self.assertEqual(status, True)
def test_live(self, get_is_live_mock): statusCheck = StatusCheck(self.serviceToPidDict, self.pidPathVars, self.globalConfig, self.servicesToLinuxUser) self.assertTrue(StatusCheck.USER_PATTERN in self.serviceToPidDict[COMPONENT_LIVE]) self.assertTrue(StatusCheck.USER_PATTERN in self.serviceToPidDict[COMPONENT_DEAD]) statusCheck.pidFilesDict = self.pidFilesDict get_is_live_mock.side_effect = lambda pid_path : self.is_live_values[pid_path] status = statusCheck.getStatus(COMPONENT_LIVE) self.assertEqual(status, True)
def test_live(self, get_is_live_mock): statusCheck = StatusCheck(self.serviceToPidDict, self.pidPathesVars, self.globalConfig, self.servicesToLinuxUser) self.assertTrue(StatusCheck.USER_PATTERN in self.serviceToPidDict[COMPONENT_LIVE]) self.assertTrue(StatusCheck.USER_PATTERN in self.serviceToPidDict[COMPONENT_DEAD]) statusCheck.pidFilesDict = self.pidFilesDict get_is_live_mock.side_effect = lambda pid_path : self.is_live_values[pid_path] status = statusCheck.getStatus(COMPONENT_LIVE) self.assertEqual(status, True)
def test_no_user_mapping(self, error_mock, get_is_live_mock): badServiceToPidDict = self.serviceToPidDict.copy() badServiceToPidDict['BAD_COMPONENT'] = 'prefix' + StatusCheck.USER_PATTERN statusCheck = StatusCheck(badServiceToPidDict, self.pidPathVars, self.globalConfig, self.servicesToLinuxUser) statusCheck.pidFilesDict = self.pidFilesDict get_is_live_mock.side_effect = lambda pid_path : self.is_live_values[pid_path] status = statusCheck.getStatus(COMPONENT_LIVE) self.assertTrue(error_mock.called)
def test_no_user_mapping(self, error_mock, get_is_live_mock): badServiceToPidDict = self.serviceToPidDict.copy() badServiceToPidDict['BAD_COMPONENT'] = 'prefix' + StatusCheck.USER_PATTERN statusCheck = StatusCheck(badServiceToPidDict, self.pidPathesVars, self.globalConfig, self.servicesToLinuxUser) statusCheck.pidFilesDict = self.pidFilesDict get_is_live_mock.side_effect = lambda pid_path : self.is_live_values[pid_path] status = statusCheck.getStatus(COMPONENT_LIVE) self.assertTrue(error_mock.called)
def test_live_if_multiple_pids(self, get_is_live_mock): one_more_pid_file_name = string.replace(COMPONENT_LIVE_PID, StatusCheck.USER_PATTERN, 'any_other_linux_user') one_more_pid_full_path = PID_DIR + os.sep + one_more_pid_file_name self.pidFilesDict[one_more_pid_file_name] = one_more_pid_full_path self.is_live_values[one_more_pid_full_path] = False statusCheck = StatusCheck(self.serviceToPidDict, self.pidPathVars, self.globalConfig, self.servicesToLinuxUser) statusCheck.pidFilesDict = self.pidFilesDict get_is_live_mock.side_effect = lambda pid_path : self.is_live_values[pid_path] status = statusCheck.getStatus(COMPONENT_LIVE) self.assertEqual(status, True)
def test_live_if_multiple_pids(self, get_is_live_mock): one_more_pid_file_name = string.replace(COMPONENT_LIVE_PID, StatusCheck.USER_PATTERN, 'any_other_linux_user') one_more_pid_full_path = PID_DIR + os.sep + one_more_pid_file_name self.pidFilesDict[one_more_pid_file_name] = one_more_pid_full_path self.is_live_values[one_more_pid_full_path] = False statusCheck = StatusCheck(self.serviceToPidDict, self.pidPathesVars, self.globalConfig, self.servicesToLinuxUser) statusCheck.pidFilesDict = self.pidFilesDict get_is_live_mock.side_effect = lambda pid_path : self.is_live_values[pid_path] status = statusCheck.getStatus(COMPONENT_LIVE) self.assertEqual(status, True)
def test_dont_relog_serToPidDict(self, logger_info_mock): TestStatusCheck.timesLogged = 0 def my_side_effect(*args, **kwargs): TestStatusCheck.timesLogged += args[0].find('Service to pid dictionary: ')+1 logger_info_mock.side_effect = my_side_effect # call this three times statusCheck = StatusCheck(self.serviceToPidDict, self.pidPathesVars, self.globalConfig, self.servicesToLinuxUser) statusCheck = StatusCheck(self.serviceToPidDict, self.pidPathesVars, self.globalConfig, self.servicesToLinuxUser) statusCheck = StatusCheck(self.serviceToPidDict, self.pidPathesVars, self.globalConfig, self.servicesToLinuxUser) # logged not more then once self.assert_(TestStatusCheck.timesLogged <= 1, "test_dont_relog_serToPidDict logged more then once")