def test_get_job_health(self): """Test all the different possible inputs to the get_job_health static method.""" health = JenkinsJob._get_job_health(failed_builds=3, builds_checked=3) self.assertEqual(health, 'BAD') health = JenkinsJob._get_job_health(failed_builds=2, builds_checked=3) self.assertEqual(health, 'BAD') health = JenkinsJob._get_job_health(failed_builds=1, builds_checked=3) self.assertEqual(health, 'FAIR') health = JenkinsJob._get_job_health(failed_builds=0, builds_checked=3) self.assertEqual(health, 'GOOD') health = JenkinsJob._get_job_health(failed_builds=2, builds_checked=2) self.assertEqual(health, 'BAD') health = JenkinsJob._get_job_health(failed_builds=1, builds_checked=2) self.assertEqual(health, 'FAIR') health = JenkinsJob._get_job_health(failed_builds=0, builds_checked=2) self.assertEqual(health, 'GOOD') health = JenkinsJob._get_job_health(failed_builds=1, builds_checked=1) self.assertEqual(health, 'BAD') health = JenkinsJob._get_job_health(failed_builds=0, builds_checked=1) self.assertEqual(health, 'GOOD') health = JenkinsJob._get_job_health(failed_builds=0, builds_checked=0) self.assertEqual(health, 'N/A')
def test_creation_of_jenkins_job_with_failed_build(self): """Test that a new Jenkins job can be created with a build with FAILURE status.""" builds = self.get_builds_from_file('builds/build_FAILURE.dat') builds_dict = self.create_builds_dict(builds) jenkins_job = JenkinsJob(self.job_name, builds) self.apply_assertions(jenkins_job, builds_dict)
def test_creation_of_jenkins_job_with_builds(self): """Test that a new Jenkins job can be created with a list of builds.""" builds = self.get_builds_from_file('builds/builds.dat') builds_dict = self.create_builds_dict(builds) jenkins_job = JenkinsJob(self.job_name, builds) self.apply_assertions(jenkins_job, builds_dict)
def test_creation_of_jenkins_job_with_no_builds(self): """Test that a new Jenkins job can be created with no builds.""" builds = [] builds_dict = {} jenkins_job = JenkinsJob(self.job_name, builds) self.apply_assertions(jenkins_job, builds_dict)
def test_creation_of_jenkins_job_with_successful_build(self): """Test that a new Jenkins job can be created with a build with SUCCESS status.""" builds = self.get_builds_from_file('builds/build_SUCCESS.dat') builds_dict = self.create_builds_dict(builds) jenkins_job = JenkinsJob(self.job_name, builds) self.apply_assertions(jenkins_job, builds_dict)
def load_connection_from_file( self, file_name: str) -> Optional[JenkinsConnection]: try: with open(file_name, 'r') as data_file: connection_name = data_file.readline().rstrip() jenkins_connection = self.get_connection(connection_name) while True: try: jenkins_job = JenkinsJob.deserialize(data_file) jenkins_connection.jenkins_jobs.update( {jenkins_job.name: jenkins_job}) except EOFError: break except ConfigError as ce: print( 'Failed to load cached data for connection from config file: {}' .format(file_name)) print('Error: {}'.format(ce)) return None print('Loaded connection [{}] with {} jobs cached.'.format( jenkins_connection.name, len(jenkins_connection.jenkins_jobs))) return jenkins_connection
def test_get_build_failures(self): """Tests that the correct number of build failures are counted by get_build_failures.""" builds = self.get_builds_from_file( 'builds/builds_SUCCESS_and_FAILURE.dat') failed_builds, _ = JenkinsJob._get_build_failures(builds) self.assertEqual( failed_builds, 8, 'The number of failed builds was not counted correctly.')
def test_pickled_jenkins_job_equals_new_jenkins_job(self): """ Tests that a pickled jenkins_job loaded from disk is identical to a new jenkins_job created from a job_instance pulled from the jenkins server. """ file_path = os.path.join(self.JENKINS_JOBS_DIR, self.build_data_file('job_with_no_builds')) with open(file_path, 'rb') as data_file: pickled_jenkins_job = JenkinsJob.deserialize(data_file) url = 'http://jenkins-cassandra.datastax.lan/' job_name = 'mshuler-9608-java9-trunk-cqlsh-tests' jenkins_obj = Jenkins(url) jenkins_builds = download_builds(jenkins_obj, job_name) jenkins_job = JenkinsJob(job_name, jenkins_builds) self.assertEqual(pickled_jenkins_job, jenkins_job, 'The pickled jenkins job does not match the new jenkins job.')
def download_single_job(self, job_name: str) -> bool: """ Download a single job. Not to be used with threading. :param job_name: Name of the job to be downloaded :return: True if a job was successfully downloaded, else False """ try: print('Downloading job: {}'.format(job_name)) builds = download_builds(self.jenkins_obj, job_name) jenkins_job = JenkinsJob(job_name, builds) jenkins_job_name = jenkins_job.name self.jenkins_jobs[jenkins_job_name] = jenkins_job return True except custom_exceptions.UnknownJob: print('Job not found, please try again.') return False
def download_job_worker(self, job_name: str, job_num: int, total_jobs: int) -> None: """ Download a single job. For use with threading. :param job_name: Name of the job to be downloaded :param job_num: The number of the current job being downloaded in the worker pool :param total_jobs: The total jobs being downloaded in the worker pool :return: None """ try: sys.stdout.write('Downloading job {} of {}: {}\n'.format( job_num, total_jobs, job_name)) builds = download_builds(self.jenkins_obj, job_name) jenkins_job = JenkinsJob(job_name, builds) jenkins_job_name = jenkins_job.name self.jenkins_jobs[jenkins_job_name] = jenkins_job except custom_exceptions.UnknownJob: sys.stdout.write('Job not found, please try again.\n')
def load_job_data(self, file_name): try: with open(file_name, 'rb') as data_file: connection_name = get_connection_name(data_file.name) jenkins_connection = self.get_connection(connection_name) while True: try: jenkins_job = JenkinsJob.deserialize(data_file) jenkins_connection.jenkins_jobs.update( {jenkins_job.name: jenkins_job}) except EOFError: break except ConfigError: print( 'Failed to load cached data for connection from config file: {}' .format(file_name)) traceback.print_exc() return None print('Loaded connection [{}] with {} jobs cached.'.format( jenkins_connection.name, len(jenkins_connection.jenkins_jobs))) return jenkins_connection
# See the License for the specific language governing permissions and # limitations under the License. import os import dill from jenkinsapi.jenkins import Jenkins from src.jenkins_interface import download_builds from src.jenkins_job import JenkinsJob from tests.argus_test import Tester FILENAME = '{}.dat'.format('job_with_no_builds') url = 'http://jenkins-cassandra.datastax.lan/' job_name = 'mshuler-9608-java9-trunk-cqlsh-tests' jenkins_obj = Jenkins(url) job_instance = jenkins_obj.get_job(job_name) builds = download_builds(jenkins_obj, job_name) # Use dill to create exact copy of job_instance from server with open(os.path.join(Tester.JOB_INSTANCES_DIR, FILENAME), 'wb') as data_file: dill.dump(job_instance, data_file) jenkins_job = JenkinsJob(job_name, builds) # Use serialize() method to store jenkins_job as argus does with open(os.path.join(Tester.JENKINS_JOBS_DIR, FILENAME), 'wb') as data_file: jenkins_job.serialize(data_file)