def test_mock_build(): two_days_ago = bsh.epochSeconds("-2d") mock = bsh.MockJenkinsBuild("fukebane", 13, "Yuge Disaxter", two_days_ago, 34) assert (mock.name) == "fukebane" assert (mock.status) == "Yuge Disaxter"
def test_compare_regular_build_time(): two_days_ago = bsh.epochSeconds("-2d") mock_build = bsh.MockJenkinsBuild("fukebane", 13, "Yuge Disaxter", two_days_ago, 34) ref_time = time.strptime("2015-10-23T10:23:45Z", '%Y-%m-%dT%H:%M:%SZ') logger = bsh.ActivityLogger('test.log') konf = bsh.Konfabulator('config/trumpkin.yaml', logger) jenkins_conf = konf.topLevel('Jenkins') jc = bsh.JenkinsConnection(jenkins_conf, logger) result = mock_build.id_as_ts < ref_time assert result == False
def test_create_build_with_no_commits(): filename = "config/buildorama.yml" logger, konf = sh.setup_config(filename) konf.topLevel('AgileCentral')['Project'] = 'Jenkins' agicen = bsh.AgileCentralConnection(konf.topLevel('AgileCentral'), logger) agicen.other_name = 'Jenkins' agicen.connect() agicen.validateProjects(['Jenkins']) build_start = int((datetime.now() - timedelta(minutes=60)).timestamp()) build_name = 'Willy Wonka stirs the chocolate' job_name, build_number, status = '%s' % build_name, 532, 'SUCCESS' started, duration = build_start, 231 commits = [] build = bsh.MockJenkinsBuild(job_name, build_number, status, started, duration, commits) build.url = "http://jenkey.dfsa.com:8080/job/bashfulmonkies/532" build_job_uri = "/".join(build.url.split('/')[:-2]) build_defn = agicen.ensureBuildDefinitionExists(job_name, 'Jenkins', build_job_uri) assert build_defn is not None changesets = agicen.matchToChangesets(commits) assert len(changesets) == 0 binfo = OrderedDict(build.as_tuple_data()) binfo['BuildDefinition'] = build_defn agicen_build = agicen.createBuild(binfo) assert agicen_build is not None assert int(agicen_build.Number) == 532 query = 'BuildDefinition.Name = "%s"' % build_name workspace = konf.topLevel('AgileCentral')['Workspace'] response = agicen.agicen.get('Build', fetch='ObjectID,Name,BuildDefinition,Number', query=query, workspace=workspace, project=None) assert response.resultCount > 0 a_build = response.next() assert a_build.BuildDefinition.Name == build_name assert int(a_build.Number) == 532
def test_namesake_projects(): # have a config that mentions m-e-p Project for the AgileCentral_Project # inhale the config # mock up a build for the 'australopithicus' Job which will trigger the necessity of creating an AC BuildDefinition record for it filename = "config/platypus.yml" logger, konf = sh.setup_config( filename, jenkins_structure=PLATYPUS_JENKINS_STRUCTURE, services=PLATYPUS_SERVICES) bc = bsh.BLDConnector(konf, logger) agiconn = bc.agicen_conn # use agiconn.agicen to clear out any Builds/BuildDefinition that match our intended actions jobs_projs = { 'centaur-mordant': agiconn.agicen.getProject('Jenkins // Salamandra').oid, 'australopithicus': agiconn.agicen.getProject('Jenkins // Corral // Salamandra').oid, } for job, project_oid in jobs_projs.items(): criteria = [ 'BuildDefinition.Name = "%s"' % job, 'BuildDefinition.Project.ObjectID = %d' % project_oid ] response = agiconn.agicen.get('Build', fetch="ObjectID,Name", query=criteria) for build in response: agiconn.agicen.delete('Build', build) criteria = ['Name = "%s"' % job, 'Project.ObjectID = %d' % project_oid] response = agiconn.agicen.get('BuildDefinition', fetch="ObjectID,Name", query=criteria) for buildef in response: agiconn.agicen.delete('BuildDefinition', buildef) assert 'Jenkins' in agiconn._project_cache.keys() assert 'Jenkins // Salamandra' in agiconn._project_cache.keys() assert 'Jenkins // Corral // Salamandra' in agiconn._project_cache.keys() target_project = "Jenkins // Corral // Salamandra" tp = agiconn.agicen.getProject(target_project) assert target_project in bc.target_projects # create some mock builds for associated with the mep Projects # throw those against #builds = createMockBuilds(['centaur-mordant', 'australopithicus']) job_name = 'australopithicus' build_start = int( (datetime.datetime.now() - datetime.timedelta(minutes=60)).timestamp()) build_number, status = 532, 'SUCCESS' started, duration = build_start, 231 commits = [] build = bsh.MockJenkinsBuild(job_name, build_number, status, started, duration, commits) build.changeSets = [] build.url = "http://jenkey.dfsa.com:8080/job/bashfulmonkies/532" build_data = build.as_tuple_data() info = OrderedDict(build_data) assert 'Project' not in info build_job_uri = "/".join(build.url.split('/')[:-2]) build_defn = agiconn.ensureBuildDefinitionExists( job_name, 'Jenkins // Corral // Salamandra', build_job_uri) assert build_defn.Name == job_name assert build_defn.Project.ObjectID == tp.oid if agiconn.buildExists(build_defn, build.number): agiconn.log.debug( 'Build #{0} for {1} already recorded, skipping...'.format( build.number, job_name)) # pull out any build.changeSets commit IDs and see if they match up with AgileCentral Changeset items Revision attribute # if so, get all such commit IDs and their associated Changeset ObjectID, then # add that "collection" as the Build's Changesets collection agicen_build, status = bc.postBuildToAgileCentral(build_defn, build, [], job_name) assert agicen_build.BuildDefinition.ref == build_defn.ref
def test_create_build_having_commits(): ref_time = datetime.now().utcnow().strftime("%Y-%m-%dT%H:%M:%SZ") # get a konf and a logger, filename = "config/buildorama.yml" logger, konf = sh.setup_config(filename) konf.topLevel('AgileCentral')['Project'] = 'Jenkins' # get an AgileCentralConnection agicen = bsh.AgileCentralConnection(konf.topLevel('AgileCentral'), logger) agicen.other_name = 'Jenkins' agicen.connect() agicen.validateProjects(['Jenkins']) # mock up a Build build_start = int((datetime.now() - timedelta(minutes=60)).timestamp()) build_name = 'Hungry kids devour our tasty product' job_name, build_number, status = '%s' % build_name, 74, 'SUCCESS' started, duration = build_start, 43 # use some SHA values corresponding to some actual Agile Central Changeset items in our target Workspace commits = ['CHOCOLATE', 'MORE CHOCOLATE'] # these are actual shas random_scm_repo_name = "Velocirat" random_scm_type = "abacus" #scm_repo = create_scm_repo(agicen, random_scm_repo_name, random_scm_type) scm_repo = agicen.ensureSCMRepositoryExists(random_scm_repo_name, random_scm_type) for sha in commits: changeset_payload = { 'SCMRepository': scm_repo.ref, 'Revision': sha, 'CommitTimestamp': '2016-12-31' } try: changeset = agicen.agicen.create('Changeset', changeset_payload) except Exception as msg: raise Exception("Could not create Changeset %s" % msg) build = bsh.MockJenkinsBuild(job_name, build_number, status, started, duration, commits) build.url = "http://jenkey.dfsa.com:8080/job/hugrycats/%s" % build_number build_job_uri = "/".join(build.url.split('/')[:-2]) # run the same code as above to matchToChangesets # assert that some were found build_defn = agicen.ensureBuildDefinitionExists(job_name, 'Jenkins', build_job_uri) assert build_defn is not None changesets = agicen.matchToChangesets(commits) assert len(changesets) == 2 # confabulate a build_info dict with Changesets = list of matching AgileCentral Changesets # call agicen.createBuild(build_info) binfo = OrderedDict(build.as_tuple_data()) binfo['BuildDefinition'] = build_defn binfo['Changesets'] = changesets agicen_build = agicen.createBuild(binfo) # assert that the Build was created assert agicen_build is not None # assert that the build number is correct assert int(agicen_build.Number) == build_number # use agicen.agicen.get('Build',...) to find the newly created Build item query = ['BuildDefinition.Name = "%s"' % build_name] query.append('Number = "%s"' % build_number) query.append('CreationDate >= %s' % ref_time) workspace = konf.topLevel('AgileCentral')['Workspace'] response = agicen.agicen.get( 'Build', fetch='ObjectID,Name,BuildDefinition,Number,Changesets', query=query, workspace=workspace, project=None) # assert that the build has a Changesets attribute value that has a collection attribute assert response.resultCount > 0 a_build = response.next() assert a_build.BuildDefinition.Name == build_name assert int(a_build.Number) == build_number # assert that the Changesets returned are indeed the target Changesets (no more, no less) changesets = [cs for cs in a_build.Changesets] assert len(changesets) >= 2 assert ('MORE CHOCOLATE' in [cs.Revision for cs in changesets]) == True print("loons are crazy %s" % len(changesets))