def test_jenkins_for_invalid_items(): config_file = ('bad_jenk_items.yml') runner = BuildConnectorRunner([config_file]) runner.run() log = "log/{}.log".format(config_file.replace('.yml', '')) with open(log, 'r') as f: log_content = f.readlines() target_line = "Jenkins section of the config contained these invalid entries" match = [line for line in log_content if target_line in line][0] assert re.search(r'%s' % target_line, match)
def test_jenkins_for_invalid_items(): config_file = ('bad_jenk_items.yml') runner = BuildConnectorRunner([config_file]) runner.run() log = "log/{}.log".format(config_file.replace('.yml', '')) with open(log, 'r') as f: log_content = f.readlines() target_line = "Jenkins section of the config contained these invalid entries" match = [line for line in log_content if target_line in line][0] assert re.search(r'%s' % target_line, match)
def test_jenkins_for_empty_folders(): config_file = ('bad_jenk_empty_folders.yml') runner = BuildConnectorRunner([config_file]) runner.run() log = "log/{}.log".format(config_file.replace('.yml', '')) with open(log, 'r') as f: log_content = f.readlines() target_line = "Folders section of the config is empty" match = [line for line in log_content if target_line in line][0] assert re.search(r'%s' % target_line, match)
def test_jenkins_for_empty_folders(): config_file = ('bad_jenk_empty_folders.yml') runner = BuildConnectorRunner([config_file]) runner.run() log = "log/{}.log".format(config_file.replace('.yml', '')) with open(log, 'r') as f: log_content = f.readlines() target_line = "Folders section of the config is empty" match = [line for line in log_content if target_line in line][0] assert re.search(r'%s' % target_line, match)
def test_builds_same_repo(): #default_lookback = 3600 # 1 hour in seconds config_lookback = 7200 # this is in seconds, even though in the config file the units are minutes config_file = 'same_scmrepo.yml' z = "2017-01-24 17:17:10 Z" last_run_zulu = create_time_file(config_file, zulu_time=z, minutes=60) t = int(time.mktime(time.strptime(last_run_zulu, '%Y-%m-%d %H:%M:%S Z'))) last_run_minus_lookback_zulu = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.localtime(t)) args = [config_file] runner = BuildConnectorRunner(args) assert runner.first_config == config_file runner.run() target_projects = runner.connector.target_projects assert 'Jenkins // Salamandra' in target_projects assert 'Jenkins // Corral // Salamandra' in target_projects
def test_dont_duplicate_builds(): job_name = 'truculent elk medallions' assert util.delete_ac_builds(job_name) == [] config_file = 'truculent.yml' ymlfile = open("config/{}".format(config_file), 'r') conf = yaml.load(ymlfile) project = conf['JenkinsBuildConnector']['Jenkins']['Jobs'][0]['AgileCentral_Project'] create_time_file(config_file, minutes=2) args = [config_file] runner = BuildConnectorRunner(args) assert runner.first_config == config_file last_run_zulu = '2016-12-01 00:00:00 Z' time_file_name = "{}_time.file".format(config_file.replace('.yml', '')) with open("log/{}".format(time_file_name), 'w') as tf: tf.write(last_run_zulu) runner.run() jenk_conn = runner.connector.bld_conn all_naked_jobs = jenk_conn.inventory.jobs targeted = [job for job in all_naked_jobs if job.name == job_name] target_job = targeted[0] build_defs = util.get_build_definition(target_job.fully_qualified_path(), project=project) assert len(build_defs) == 1 assert build_defs[0].Project.Name == project builds = util.get_ac_builds(build_defs[0], project=project) #assert len(builds) == 10 assert [build for build in builds if build.Number in ['8', '9', '10']] assert [build for build in builds if build.Number == '8' and build.Status == 'SUCCESS'] assert [build for build in builds if build.Number == '9' and build.Status == 'FAILURE'] last_run_zulu = '2016-10-31 00:00:00 Z' time_file_name = "{}_time.file".format(config_file.replace('.yml', '')) with open("log/{}".format(time_file_name), 'w') as tf: tf.write(last_run_zulu) runner.run() build_defs = util.get_build_definition(target_job.fully_qualified_path(), project=project) assert len(build_defs) == 1 builds = util.get_ac_builds(build_defs[0], project=project) assert len(builds) == 10 assert len([build for build in builds if build.Number == '1']) == 1 assert len([build for build in builds if build.Number in ['8', '9', '10']]) == 3 assert [build for build in builds if build.Number == '5' and build.Status == 'SUCCESS'] assert [build for build in builds if build.Number == '1' and build.Status == 'FAILURE']
def test_builds_same_repo(): #default_lookback = 3600 # 1 hour in seconds config_lookback = 7200 # this is in seconds, even though in the config file the units are minutes config_file = 'same_scmrepo.yml' z = "2017-01-24 17:17:10 Z" last_run_zulu = create_time_file(config_file, zulu_time=z, minutes=60) t = int(time.mktime(time.strptime(last_run_zulu, '%Y-%m-%d %H:%M:%S Z'))) last_run_minus_lookback_zulu = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.localtime(t)) args = [config_file] runner = BuildConnectorRunner(args) assert runner.first_config == config_file runner.run() target_projects = runner.connector.target_projects assert 'Jenkins // Salamandra' in target_projects assert 'Jenkins // Corral // Salamandra' in target_projects
def test_log_for_config_vetting(): config_file = BAD_CONFIG1 args = [config_file] runner = BuildConnectorRunner(args) assert runner.first_config == config_file runner.run() log = "log/{}.log".format(config_file.replace('.yml', '')) assert runner.logfile_name == log with open(log, 'r') as f: log_content = f.readlines() #error = "these jobs: Parkour, pillage-and-plunder, torment were not present in the Jenkins inventory of Jobs" # the log contained the same error, but the order of job names is different: # "these jobs: 'pillage-and-plunder', 'torment', 'Parkour' were not present in the Jenkins inventory of Jobs" error = "were not present in the Jenkins inventory of Jobs" match = [line for line in log_content if "{}".format(error) in line][0] assert re.search(r'%s' % error, match)
def test_bld_connector_runner(): #default_lookback = 3600 # 1 hour in seconds config_lookback = 7200 # this is in seconds, even though in the config file the units are minutes config_file = 'wombat.yml' last_run_zulu = create_time_file(config_file, minutes=60) #t = int(time.mktime(time.strptime(last_run_zulu, '%Y-%m-%d %H:%M:%S Z'))) - default_lookback t = int(time.mktime(time.strptime( last_run_zulu, '%Y-%m-%d %H:%M:%S Z'))) - config_lookback last_run_minus_lookback_zulu = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.localtime(t)) args = [config_file] runner = BuildConnectorRunner(args) assert runner.first_config == config_file runner.run() assert config_file in runner.config_file_names assert 'AgileCentral' in runner.connector.config.topLevels() assert 'Static' in runner.connector.target_projects log = "log/{}.log".format(config_file.replace('.yml', '')) assert runner.logfile_name == log with open(log, 'r') as f: log_content = f.readlines() line1 = "Connected to Jenkins server" line2 = "Connected to Agile Central" line3 = "Got a BLDConnector instance" line4 = "recent Builds query: CreationDate >= {}".format( last_run_minus_lookback_zulu) match1 = [line for line in log_content if "{}".format(line1) in line][-1] match2 = [line for line in log_content if "{}".format(line2) in line][-1] match3 = [line for line in log_content if "{}".format(line3) in line][-1] match4 = [line for line in log_content if "{}".format(line4) in line][-1] assert last_run_minus_lookback_zulu in match4 assert re.search(r'%s' % line1, match1) assert re.search(r'%s' % line2, match2) assert re.search(r'%s' % line3, match3) assert re.search(r'%s' % line4, match4)
def main(args): try: connector_runner = BuildConnectorRunner(args) connector_runner.run() except ConfigurationError as msg: # raising a ConfigurationError will cause an ERROR to be logged sys.stderr.write('ERROR: %s detected a fatal configuration error. See log file.\n' % PROG) sys.exit(1) except Exception as msg: sys.stderr.write('ERROR: %s encountered an ERROR condition.\n' % PROG) # blurt out a formatted stack trace excp_type, excp_value, tb = sys.exc_info() traceback.print_tb(tb) mo = re.search(r"'(?P<ex_name>.+)'", str(excp_type)) if mo: excp_type = mo.group('ex_name').replace('exceptions.', '').replace('bldeif.utils.', '') sys.stderr.write('%s: %s\n' % (excp_type, str(excp_value))) sys.exit(2) sys.exit(0)
def test_reflect_builds(): config_file = 'wombat.yml' folder = "immovable wombats" my_job = "Top" ymlfile = open("config/{}".format(config_file), 'r') data = yaml.load(ymlfile) jenkins = data['JenkinsBuildConnector']['Jenkins'] username = jenkins['Username'] api_token = jenkins['API_Token'] protocol = jenkins['Protocol'] server = jenkins['Server'] port = jenkins['Port'] headers = {'Content-Type': 'application/json'} jenkins_base_url = "{}://{}:{}".format(protocol, server, port) url = "{}/job/{}/job/{}/build".format(jenkins_base_url, folder, my_job) r = requests.post(url, auth=(username, api_token), headers=headers) assert r.status_code in [200, 201] create_time_file(config_file, minutes=2) args = [config_file] runner = BuildConnectorRunner(args) assert runner.first_config == config_file runner.run() assert config_file in runner.config_file_names assert 'AgileCentral' in runner.connector.config.topLevels() assert 'Static' in runner.connector.target_projects log = "log/{}.log".format(config_file.replace('.yml', '')) assert runner.logfile_name == log with open(log, 'r') as f: log_content = f.readlines() job_build_signature = "Created Build: tiema03-u183073.ca.com:8080/job/immovable wombats/job/Top" #job_build_signature = "Created Build: {}/job/{}/job/{}".format(jenkins_base_url, folder, my_job) job_url_lines = [ line for line in log_content if job_build_signature in line ] assert job_url_lines
def test_bld_connector_runner(): #default_lookback = 3600 # 1 hour in seconds config_lookback = 7200 # this is in seconds, even though in the config file the units are minutes config_file = 'wombat.yml' last_run_zulu = create_time_file(config_file, minutes=60) #t = int(time.mktime(time.strptime(last_run_zulu, '%Y-%m-%d %H:%M:%S Z'))) - default_lookback t = int(time.mktime(time.strptime(last_run_zulu, '%Y-%m-%d %H:%M:%S Z'))) - config_lookback last_run_minus_lookback_zulu = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.localtime(t)) args = [config_file] runner = BuildConnectorRunner(args) assert runner.first_config == config_file runner.run() assert config_file in runner.config_file_names assert 'AgileCentral' in runner.connector.config.topLevels() assert 'Static' in runner.connector.target_projects log = "log/{}.log".format(config_file.replace('.yml', '')) assert runner.logfile_name == log with open(log, 'r') as f: log_content = f.readlines() line1 = "Connected to Jenkins server" line2 = "Connected to Agile Central" line3 = "Got a BLDConnector instance" line4 = "recent Builds query: CreationDate >= {}".format(last_run_minus_lookback_zulu) match1 = [line for line in log_content if "{}".format(line1) in line][-1] match2 = [line for line in log_content if "{}".format(line2) in line][-1] match3 = [line for line in log_content if "{}".format(line3) in line][-1] match4 = [line for line in log_content if "{}".format(line4) in line][-1] assert last_run_minus_lookback_zulu in match4 assert re.search(r'%s' % line1, match1) assert re.search(r'%s' % line2, match2) assert re.search(r'%s' % line3, match3) assert re.search(r'%s' % line4, match4)
def test_reflect_builds(): config_file = 'wombat.yml' folder = "immovable wombats" my_job = "Top" ymlfile = open("config/{}".format(config_file), 'r') data = yaml.load(ymlfile) jenkins = data['JenkinsBuildConnector']['Jenkins'] username = jenkins['Username'] api_token = jenkins['API_Token'] protocol = jenkins['Protocol'] server = jenkins['Server'] port = jenkins['Port'] headers = {'Content-Type': 'application/json'} jenkins_base_url = "{}://{}:{}".format(protocol, server, port) url = "{}/job/{}/job/{}/build".format(jenkins_base_url, folder, my_job) r = requests.post(url, auth=(username, api_token), headers=headers) assert r.status_code in [200, 201] create_time_file(config_file, minutes=2) args = [config_file] runner = BuildConnectorRunner(args) assert runner.first_config == config_file runner.run() assert config_file in runner.config_file_names assert 'AgileCentral' in runner.connector.config.topLevels() assert 'Static' in runner.connector.target_projects log = "log/{}.log".format(config_file.replace('.yml', '')) assert runner.logfile_name == log with open(log, 'r') as f: log_content = f.readlines() job_build_signature = "Created Build: tiema03-u183073.ca.com:8080/job/immovable wombats/job/Top" #job_build_signature = "Created Build: {}/job/{}/job/{}".format(jenkins_base_url, folder, my_job) job_url_lines = [line for line in log_content if job_build_signature in line] assert job_url_lines
def test_special_chars(): config_file = 'aouch.yml' z = "2017-01-24 17:17:10 Z" last_run_zulu = create_time_file(config_file, zulu_time=z, minutes=60) t = int(time.mktime(time.strptime(last_run_zulu, '%Y-%m-%d %H:%M:%S Z'))) args = [config_file] runner = BuildConnectorRunner(args) assert runner.first_config == config_file runner.run() log = "log/{}.log".format(config_file.replace('.yml', '')) assert runner.logfile_name == log with open(log, 'r', encoding='utf-8') as f: log_content = f.readlines() target_line = "showQualifiedJobs - áâèüSørençñ" match = [line for line in log_content if target_line in line][0] assert re.search(r'%s' % target_line, match) target_line = "東方青龍" match = [line for line in log_content if target_line in line][0] assert re.search(r'%s' % target_line, match)
def test_special_chars(): config_file = 'aouch.yml' z = "2017-01-24 17:17:10 Z" last_run_zulu = create_time_file(config_file, zulu_time=z, minutes=60) t = int(time.mktime(time.strptime(last_run_zulu, '%Y-%m-%d %H:%M:%S Z'))) args = [config_file] runner = BuildConnectorRunner(args) assert runner.first_config == config_file runner.run() log = "log/{}.log".format(config_file.replace('.yml', '')) assert runner.logfile_name == log with open(log, 'r', encoding='utf-8') as f: log_content = f.readlines() target_line = "showQualifiedJobs - áâèüSørençñ" match = [line for line in log_content if target_line in line][0] assert re.search(r'%s' % target_line, match) target_line = "東方青龍" match = [line for line in log_content if target_line in line][0] assert re.search(r'%s' % target_line, match)
def test_dont_duplicate_builds(): job_name = 'truculent elk medallions' assert util.delete_ac_builds(job_name) == [] config_file = 'truculent.yml' ymlfile = open("config/{}".format(config_file), 'r') conf = yaml.load(ymlfile) project = conf['JenkinsBuildConnector']['Jenkins']['Jobs'][0][ 'AgileCentral_Project'] create_time_file(config_file, minutes=2) args = [config_file] runner = BuildConnectorRunner(args) assert runner.first_config == config_file last_run_zulu = '2016-12-01 00:00:00 Z' time_file_name = "{}_time.file".format(config_file.replace('.yml', '')) with open("log/{}".format(time_file_name), 'w') as tf: tf.write(last_run_zulu) runner.run() jenk_conn = runner.connector.bld_conn all_naked_jobs = jenk_conn.inventory.jobs targeted = [job for job in all_naked_jobs if job.name == job_name] target_job = targeted[0] build_defs = util.get_build_definition(target_job.fully_qualified_path(), project=project) assert len(build_defs) == 1 assert build_defs[0].Project.Name == project builds = util.get_ac_builds(build_defs[0], project=project) #assert len(builds) == 10 assert [build for build in builds if build.Number in ['8', '9', '10']] assert [ build for build in builds if build.Number == '8' and build.Status == 'SUCCESS' ] assert [ build for build in builds if build.Number == '9' and build.Status == 'FAILURE' ] last_run_zulu = '2016-10-31 00:00:00 Z' time_file_name = "{}_time.file".format(config_file.replace('.yml', '')) with open("log/{}".format(time_file_name), 'w') as tf: tf.write(last_run_zulu) runner.run() build_defs = util.get_build_definition(target_job.fully_qualified_path(), project=project) assert len(build_defs) == 1 builds = util.get_ac_builds(build_defs[0], project=project) assert len(builds) == 10 assert len([build for build in builds if build.Number == '1']) == 1 assert len([build for build in builds if build.Number in ['8', '9', '10']]) == 3 assert [ build for build in builds if build.Number == '5' and build.Status == 'SUCCESS' ] assert [ build for build in builds if build.Number == '1' and build.Status == 'FAILURE' ]