From d211b7ed4a284688f37152bad2f5bd7fbfd77813 Mon Sep 17 00:00:00 2001
From: Adam Williamson <awilliam@redhat.com>
Date: Tue, 17 Feb 2015 23:24:08 -0800
Subject: [PATCH] add an 'all' mode to try and run against everything we can

---
 tools/openqa_trigger/openqa_trigger.py | 135 +++++++++++++++++++++------------
 1 file changed, 86 insertions(+), 49 deletions(-)

diff --git a/tools/openqa_trigger/openqa_trigger.py b/tools/openqa_trigger/openqa_trigger.py
index 9a43b6c..4f43ed4 100755
--- a/tools/openqa_trigger/openqa_trigger.py
+++ b/tools/openqa_trigger/openqa_trigger.py
@@ -8,6 +8,7 @@ import os.path
 import sys
 import subprocess
 import argparse
+import datetime
 # We can at least find images and run OpenQA jobs without wikitcms
 try:
     import wikitcms.wiki
@@ -77,12 +78,15 @@ def run_openqa_jobs(isoname, arch, image_version):
     else:
         return []
 
-# run OpenQA on current compose if it is newer version since last run
-def run_current(args, wiki):
+def jobs_from_current(wiki):
+    """Schedule jobs against the 'current' release validation event
+    (according to wikitcms) if we have not already. Returns a tuple,
+    first value is the job list, second is the current event.
+    """
     if not wiki:
-        sys.exit("python-wikitcms is required for --current. Try "
-                 "--compose to run against today's Rawhide nightly "
-                 "without wiki result submission.")
+        print("python-wikitcms is required for current validation event "
+              "discovery.")
+        return ([], None)
     last_versions, json_parsed = read_last()
     currev = wiki.current_event
     print("Current event: {0}".format(currev.version))
@@ -101,14 +105,49 @@ def run_current(args, wiki):
     f.write(json.dumps(json_parsed))
     f.close()
 
+    return (jobs, currev)
+
+def jobs_from_fedfind(ff_release, arches=VERSIONS):
+    """Given a fedfind.Release object, find the ISOs we want and run
+    jobs on them. arches is an iterable of arches to run on, if not
+    specified, we'll use our constant.
+    """
+    # Find boot.iso images for our arches; third query is a bit of a
+    # bodge till I know what 22 TCs/RCs will actually look like,
+    # ideally we want a query that will reliably return one image per
+    # arch without us having to filter further, but we can always just
+    # take the first image for each arch if necessary
+    jobs = []
+    queries = (
+        fedfind.release.Query('imagetype', ('boot',)),
+        fedfind.release.Query('arch', arches),
+        fedfind.release.Query('payload', ('server', 'generic')))
+
+    for image in ff_release.find_images(queries):
+        print("{0} {1}".format(image.url, image.desc))
+        isoname = download_image(image)
+        version = '_'.join(
+            (ff_release.release, ff_release.milestone, ff_release.compose))
+        job_ids = run_openqa_jobs(isoname, image.arch, version)
+        jobs.extend(job_ids)
+    return jobs
+
+## SUB-COMMAND FUNCTIONS
+
+def run_current(args, wiki):
+    """run OpenQA for current release validation event, if we have
+    not already done it.
+    """
+    jobs = jobs_from_current(wiki)[0]
     # wait for jobs to finish and display results
-    print jobs
-    report_results(jobs)
+    if jobs:
+        print jobs
+        report_results(jobs)
     sys.exit()
 
 def run_compose(args, wiki=None):
     """run OpenQA on a specified compose, optionally reporting results
-    if a matching wikitcms ValidationEvent can be found.
+    if a matching wikitcms ValidationEvent is found by relval/wikitcms
     """
     # get the fedfind release object
     try:
@@ -118,26 +157,6 @@ def run_compose(args, wiki=None):
     except ValueError as err:
         sys.exit(err[0])
 
-    if args.submit_results:
-        try:
-            # sanity check, there's...some voodoo in here. but this isn't
-            # really strictly necessary, and we don't use the event object
-            # for anything.
-            event = wiki.get_validation_event(
-                release=ff_release.release, milestone=ff_release.milestone,
-                compose=ff_release.compose)
-            evff = event.ff_release
-            if evff.version != ff_release.version:
-                print("Release validation event's fedfind object does not "
-                      "match the one from fedfind's get_release(). Something's"
-                      " wrong somewhere. Result submission disabled.")
-                args.submit_results = False
-        except ValueError:
-            print("Warning: could not find validation test event for this "
-                  "compose. Continuing with OpenQA jobs, but results will "
-                  " not be submitted to the wiki.")
-            args.submit_results = False
-
     print("Running on compose: {0}".format(ff_release.version))
     if args.arch:
         jobs = jobs_from_fedfind(ff_release, [args.arch])
@@ -148,30 +167,40 @@ def run_compose(args, wiki=None):
         report_results(jobs)
     sys.exit()
 
-def jobs_from_fedfind(ff_release, arches=VERSIONS):
-    """Given a fedfind.Release object, find the ISOs we want and run
-    jobs on them. arches is an iterable of arches to run on, if not
-    specified, we'll use our constant.
+def run_all(args, wiki=None):
+    """Do everything we can: test both Rawhide and Branched nightlies
+    if they exist, and test current compose if it's different from
+    either and it's new.
     """
-    # Find boot.iso images for our arches; third query is a bit of a
-    # bodge till I know what 22 TCs/RCs will actually look like,
-    # ideally we want a query that will reliably return one image per
-    # arch without us having to filter further, but we can always just
-    # take the first image for each arch if necessary
     jobs = []
-    queries = (
-        fedfind.release.Query('imagetype', ('boot',)),
-        fedfind.release.Query('arch', arches),
-        fedfind.release.Query('payload', ('server', 'generic')))
+    skip = None
+    (currjobs, currev) = jobs_from_current(wiki)
+    print("Jobs from current validation event: {0}".format(currjobs))
+    jobs.extend(currjobs)
 
-    for image in ff_release.find_images(queries):
-        print("{0} {1}".format(image.url, image.desc))
-        isoname = download_image(image)
-        version = '_'.join(
-            (ff_release.release, ff_release.milestone, ff_release.compose))
-        job_ids = run_openqa_jobs(isoname, image.arch, version)
-        jobs.extend(job_ids)
-    return jobs
+    utcdate = datetime.datetime.utcnow()
+    day = datetime.timedelta(days=1)
+    utcdate = utcdate - day
+    if currev and currev.compose == utcdate.strftime('%Y%m%d'):
+        skip = currev.milestone
+
+    if not skip.lower() == 'rawhide':
+        rawhide_ffrel = fedfind.release.get_release(
+            release='Rawhide', compose=utcdate)
+        rawjobs = jobs_from_fedfind(rawhide_ffrel)
+        print("Jobs from {0}: {1}".format(rawhide_ffrel.version, rawjobs))
+        jobs.extend(rawjobs)
+
+    if not skip.lower() == 'branched':
+        branched_ffrel = fedfind.release.get_release(
+            release=currev.release, compose=utcdate)
+        branchjobs = jobs_from_fedfind(branched_ffrel)
+        print("Jobs from {0}: {1}".format(branched_ffrel.version, branchjobs))
+        jobs.extend(branchjobs)
+
+    if jobs:
+        report_results(jobs)
+    sys.exit()
 
 if __name__ == "__main__":
     test_help = "Operate on the staging wiki (for testing)"
@@ -213,6 +242,14 @@ if __name__ == "__main__":
         '-t', '--test', help=test_help, required=False, action='store_true')
     parser_compose.set_defaults(func=run_compose)
 
+    parser_all = subparsers.add_parser(
+        'all', description="Run for the current validation event (if needed) "
+        "and today's Rawhide and Branched nightly's (if found).")
+    parser_all.add_argument(
+        '-t', '--test', help=test_help, required=False, action='store_true')
+    parser_current.set_defaults(func=run_current)
+    parser_all.set_defaults(func=run_all)
+
     args = parser.parse_args()
 
     wiki = None
-- 
2.3.0

