def test_run_command_file_not_found(self): command = '/bin/doesnotexist' expected_msg = '\* Could not run command \(return code= %s\)\n' % 127 expected_msg += '\* Error was:\n.*: %s: (not found|No such file or directory)\n' % command expected_msg += '\* Command was:\n%s\n' % command expected_msg += '\* Output was:\n\n' expected_msg += 'Check if y/our path is correct: %s' % os.getenv('PATH') with self.assertRaisesRegexp(utils.PynagError, expected_msg): utils.runCommand(command, raise_error_on_fail=True)
def test_run_command_file_not_found(self): command = '/bin/doesnotexist' expected_msg = '\* Could not run command \(return code= %s\)\n' % 127 expected_msg += '\* Error was:\n.*: %s: (not found|No such file or directory)\n' % command expected_msg += '\* Command was:\n%s\n' % command expected_msg += '\* Output was:\n\n' expected_msg += 'Check if y/our path is correct: %s' % os.getenv( 'PATH') with self.assertRaisesRegexp(utils.PynagError, expected_msg): utils.runCommand(command, raise_error_on_fail=True)
def test_gitrepo_deprecated_methods(self): """ Delete this class as deprecated methods are removed. """ repo = utils.GitRepo(directory=self.tmp_dir, auto_init=True) testfilename = 'testfile.name.txt' with patch('pynag.Utils.GitRepo.add') as add_method_mock: repo._git_add(testfilename) add_method_mock.assert_called_once_with(testfilename) with patch('pynag.Utils.GitRepo.commit') as commit_method_mock: repo._git_commit(filename=testfilename, message='test') commit_method_mock.assert_called_once_with(message='test', filelist=[testfilename]) commit_method_mock.reset_mock() repo._git_commit(filename=None, message='test', filelist=[testfilename]) commit_method_mock.assert_called_once_with(message='test', filelist=[testfilename]) commit_method_mock.reset_mock() repo._git_commit(filename=testfilename, message='test', filelist=[testfilename]) commit_method_mock.assert_called_once_with( message='test', filelist=[testfilename, testfilename])
def verify_configuration(): """ Verifies nagios configuration and returns the output of nagios -v nagios.cfg """ binary = adagios.settings.nagios_binary config = adagios.settings.nagios_config command = "%s -v '%s'" % (binary, config) code, stdout, stderr = Utils.runCommand(command) result = {} result['return_code'] = code result['output'] = stdout result['errors'] = stderr return result total_errors = 0 total_warnings = 0 for line in stdout.splitlines(): output = {} output['tags'] = tags = [] output['content'] = line if line.lower().startswith('warning'): tags.append('warning') total_warnings += 1 print line if line.lower().startswith('error'): tags.append('error') total_errors += 1 result['output'].append(output) result['error_count'] = total_errors result['warning_count'] = total_warnings return result
def verify_configuration(): """ Verifies nagios configuration and returns the output of nagios -v nagios.cfg """ binary = adagios.settings.nagios_binary config = adagios.settings.nagios_config command = "%s -v '%s'" % (binary, config) code, stdout, stderr = Utils.runCommand(command) result = {} result['return_code'] = code result['output'] = stdout result['errors'] = stderr return result total_errors = 0 total_warnings = 0 for line in stdout.splitlines(): output = {} output['tags'] = tags = [] output['content'] = line if line.lower().startswith('warning'): tags.append('warning') total_warnings += 1 if line.lower().startswith('error'): tags.append('error') total_errors += 1 result['output'].append(output) result['error_count'] = total_errors result['warning_count'] = total_warnings return result
def test_gitrepo_init_with_files(self): tmp_file = tempfile.mkstemp(dir=self.tmp_dir) # If pynag defaults will fail, correctly, adjust for test author_email = None from getpass import getuser from platform import node nodename = node() if nodename.endswith('.(none)'): nodename[:-7] + '.example.com' author_email = '%s@%s' % (getuser(), nodename) repo = utils.GitRepo(directory=self.tmp_dir, auto_init=True, author_name=None, author_email=author_email) # Check that there is an initial commit expected_email = '%s@%s' % (getuser(), nodename) self.assertEquals(len(repo.log()), 1) self.assertEquals(repo.log()[0]['comment'], 'Initial Commit') self.assertEquals(repo.log()[0]['author_name'], 'Pynag User') self.assertEquals(repo.log()[0]['author_email'], expected_email) # Test kwargs functionality self.assertEquals( repo.log(author_email=expected_email)[0]['author_email'], expected_email) self.assertEquals( repo.log(comment__contains='Initial')[0]['comment'], 'Initial Commit') self.assertEquals(len(repo.log(comment__contains='nothing')), 0) # Test show method initial_hash = repo.log()[0]['hash'] initial_hash_valid_commits = repo.get_valid_commits()[0] self.assertEquals(initial_hash, initial_hash_valid_commits) gitrunpatcher = patch('pynag.Utils.GitRepo._run_command') validcommitspatcher = patch('pynag.Utils.GitRepo.get_valid_commits') gitrunpatch = gitrunpatcher.start() validcommitspatch = validcommitspatcher.start() validcommitspatch.return_value = [initial_hash] repo.show(initial_hash) gitrunpatch.assert_called_once_with('git show %s' % initial_hash) gitrunpatcher.stop() validcommitspatcher.stop() invalid_hash = '123' self.assertRaisesRegexp(PynagError, '123 is not a valid commit id', repo.show, '123') # Add file tmp_file_2 = tempfile.mkstemp(dir=self.tmp_dir) self.assertEquals(len(repo.get_uncommited_files()), 1) self.assertEquals(repo.is_up_to_date(), False) # Commit file repo.commit(filelist=repo.get_uncommited_files()[0]['filename']) self.assertEquals(repo.is_up_to_date(), True) self.assertEquals(len(repo.get_uncommited_files()), 0) self.assertEquals(len(repo.get_valid_commits()), 2) log_entry = repo.log()[0] self.assertEquals(log_entry['comment'], 'commited by pynag')
def test_gitrepo_init_empty(self): from getpass import getuser from platform import node repo = utils.GitRepo(directory=self.tmp_dir, auto_init=True, author_name=None, author_email=None) self.assertEquals(repo.author_name, 'Pynag User') expected_email = '<%s@%s>' % (getuser(), node()) self.assertEquals(repo.author_email, expected_email)
def test_gitrepo_init_empty(self): from getpass import getuser from platform import node emptyish = [None, '', ' ', '\n '] for x in emptyish: repo = utils.GitRepo(directory=self.tmp_dir, auto_init=True, author_name=x, author_email=x) self.assertEquals(repo.author_name, 'Pynag User') expected_email = '%s@%s' % (getuser(), node()) self.assertEquals(repo.author_email, expected_email)
def test_gitrepo_diff(self): """ Test git diff works as expected """ # Create repo and write one test commit git = utils.GitRepo(directory=self.tmp_dir, auto_init=True) tmp_filename = "%s/%s" % (self.tmp_dir, 'testfile.txt') open(tmp_filename, 'w').write('test data\n') git.commit() # First try diff with no changes made: diff = git.diff() self.assertEquals(diff, '') # Now append to our file and see the difference: extra_data = 'extra data\n' open(tmp_filename, 'a').write(extra_data) # Call diff with no params, check if extra_data is in the diff diff = git.diff() self.assertTrue(diff.find(extra_data) > 0) # Call diff with filename as parameter, check if extra_data is in the # diff diff = git.diff(commit_id_or_filename=tmp_filename) self.assertTrue(diff.find(extra_data) > 0) # Call commit again and confirm there is no diff git.commit() diff = git.diff() self.assertEquals(diff, '') # Call a diff against first commit, see if we find our changes in the # commit. all_commits = git.get_valid_commits() first_commit = all_commits.pop() diff = git.diff(commit_id_or_filename=first_commit) self.assertTrue(diff.find(extra_data) > 0) # Revert latest change, and make sure diff is gone. last_commit = all_commits.pop(0) git.revert(last_commit) diff = git.diff(commit_id_or_filename=first_commit) self.assertTrue(diff.find(extra_data) == -1) # At last try to diff against an invalid commit id try: git.diff('invalid commit id') self.assertTrue( False, "we wanted exception when calling diff on invalid commit id") except PynagError: pass
def test_gitrepo_init_with_author(self): tmp_file = tempfile.mkstemp(dir=self.tmp_dir) author_name = 'Git Owner' author_email = '*****@*****.**' repo = utils.GitRepo(directory=self.tmp_dir, auto_init=True, author_name=author_name, author_email=author_email) self.assertEquals(repo.author_name, author_name) self.assertEquals(repo.author_email, author_email) self.assertEquals(len(repo.log()), 1) self.assertEquals(repo.log()[0]['author_name'], author_name) self.assertEquals(repo.log()[0]['author_email'], author_email)
def restart_monitoring(): global Conf global Model try: res = Utils.runCommand(Conf.service + ' restart', raise_error_on_fail=True) push_progress('success', 'Relance du logiciel de supervision', 40) time.sleep(1) push_progress('end', 'Synchronization complete', 100) except Exception, e: push_progress('failed', 'Echec du redémarrage du logiciel de supervision', 40) # push_progress('end', 'end', 100) raise e
def verify_configuration(): """ Verifies nagios configuration and returns the output of nagios -v nagios.cfg """ binary = adagios.settings.nagios_binary config = adagios.settings.nagios_config command = "%s -v '%s'" % (binary, config) code, stdout, stderr = Utils.runCommand(command) result = {} result['return_code'] = code result['output'] = stdout result['errors'] = stderr return result
def pre_flight_check(): ######### ## @ToDo ## Reprendre le confPath et binPath du fichier de conf de l'agent ######### try: res = Utils.runCommand('/usr/local/shinken/bin/shinken-arbiter -v -c /usr/local/shinken/etc/nagios.cfg -c /usr/local/shinken/etc/shinken-specific.cfg', raise_error_on_fail=True) push_progress('success', 'Vérification de la nouvelle configuration', 30) restart_monitoring() except Exception, e: push_progress('failed', 'Nouvelle configuration de supervision incorrecte', 30) push_progress('end', 'end', 100) raise e
def parse(self): from pynag import Utils plugin_output = self.cleaned_data['plugin_output'] output = Utils.PluginOutput(plugin_output) self.results = output