From 61752c2ce6348fc7141a05bf12e97566e30f9c20 Mon Sep 17 00:00:00 2001
From: Andrej Manduch <amanduch@redhat.com>
Date: Dec 12 2017 02:38:46 +0000
Subject: [PATCH 1/2] Added docker support for shim


Patch enables running test for requested koji_build using docker
byt installing that package in to docker and then running test.

Reported-By: Rachel Sibley <rasibley@redhat.com>
Signed-off-by: Andrej Manduch <amanduch@redhat.com>

---

diff --git a/runtask.yml b/runtask.yml
index 957928b..e2687ff 100644
--- a/runtask.yml
+++ b/runtask.yml
@@ -39,6 +39,21 @@ actions:
           artifactsdir: ${artifactsdir}
       export: standard_interface_output
 
-    - name: report results to resultsdb
+    - name: run container interface tests
+      python:
+          file: standard-interface-shim.py
+          callable: run_docker_interface_test
+          workdir: ${workdir}
+          koji_build: ${koji_build}
+          artifactsdir: ${artifactsdir}
+          docker_image: "docker.io/library/fedora:rawhide"
+          rpms: ${rpm_data}
+      export: container_interface_output
+
+    - name: report standart results to resultsdb
       resultsdb:
           results: ${standard_interface_output}
+
+    - name: report container results to resultsdb
+      resultsdb:
+          results: ${container_interface_output}
diff --git a/standard-interface-shim.py b/standard-interface-shim.py
index 550b8a3..65c7a4f 100644
--- a/standard-interface-shim.py
+++ b/standard-interface-shim.py
@@ -1,7 +1,10 @@
 import logging
 from collections import namedtuple
 import subprocess
-import os
+import os, platform
+
+import re
+import urllib
 
 from libtaskotron import check
 from libtaskotron.ext.fedora import rpm_utils
@@ -44,33 +47,24 @@ def clone_git_repo(koji_build, workdir):
 def create_results_file():
     pass
 
-def run_standard_interface_test(koji_build, rpms, workdir='.', artifactsdir='artifacts'):
-    outcome = 'PASSED'
-
-    name = rpm_utils.rpmformat(koji_build, 'n')
-
-    # set envirnment vars
+def __set_env_vars(name, workdir='.', artifactsdir='artifacts'):
     environment = {}
+
     inventory_filename = os.path.join(workdir, name, 'inventory')
     if os.path.exists(inventory_filename):
         environment['ANSIBLE_INVENTORY'] = inventory_filename
     else:
         environment['ANSIBLE_INVENTORY'] = '/usr/share/ansible/inventory'
 
-    environment['TEST_SUBJECTS'] = ' '.join(rpms['downloaded_rpms'])
     environment['TEST_ARTIFACTS'] = artifactsdir
 
     environment['HOME'] = os.environ.get('HOME', '/')
     environment['PATH'] = os.environ.get('PATH', '') + ':/usr/bin:/usr/local/bin'
 
-    # create the command
-    testdir = os.path.join(workdir, name)
-    command = ['ansible-playbook', '-e', 'artifacts='+artifactsdir, '--tags', 'classic', 'tests.yml']
+    return environment
 
-    log.debug("sending environment: {}".format(environment))
-
-
-    # execute the tests
+def __execute_test(command, testdir, environment):
+    outcome = 'PASSED'
     log.info('Running command: {} in {}'.format(' '.join(command), testdir))
     try:
         output, _ = os_utils.popen_rt(' '.join(command), shell=True, cwd=testdir, env=environment)
@@ -88,8 +82,79 @@ def run_standard_interface_test(koji_build, rpms, workdir='.', artifactsdir='art
 
     # create results file
 
-    result = Result(outcome=outcome, output=output, errors=errors, warnings=warnings)
+    return Result(outcome=outcome, output=output, errors=errors, warnings=warnings)
+
+def install_rpms_docker_playbook(rpms, artifactsdir='artifacts'):
+    output_yml = """
+- hosts: localhost
+  tags:
+  - container
+  tasks:
+"""
+    install_template = """
+  - copy:
+      src: RPM_PATH
+      dest: /root/RPM_NAME
+  - name: install RPM_NAME
+    dnf: name=/root/RPM_NAME state=present
+"""
+    path_replace = re.compile("RPM_PATH", re.MULTILINE)
+    name_replace = re.compile("RPM_NAME", re.MULTILINE)
+    for path in rpms['downloaded_rpms']:
+        tmp_template = install_template
+        rpm_name = os.path.basename(path)
+        tmp_template = path_replace.sub(path, tmp_template)
+        tmp_template = name_replace.sub(rpm_name, tmp_template)
+        output_yml += tmp_template
+
+    inst_yml_file = os.path.join(artifactsdir, "install_rpms.yml")
+    y_file = open(inst_yml_file, "w")
+    y_file.write(output_yml)
+    y_file.close()
+
+    return inst_yml_file
+
+
+def run_docker_interface_test(koji_build, rpms, compose=None, workdir='.', \
+    artifactsdir='artifacts', docker_image="docker.io/library/fedora:rawhide"):
+
+    name = rpm_utils.rpmformat(koji_build, 'n')
+    environment = __set_env_vars(name, workdir, artifactsdir)
+    testdir = os.path.join(workdir, name)
+    yaml_rpm_install = install_rpms_docker_playbook(rpms, workdir)
+    command = ['ansible-playbook', '-e', 'artifacts='+artifactsdir, '-e', \
+    'subjects=docker:'+docker_image, '--tags', 'container', yaml_rpm_install, 'tests.yml']
+
+    log.debug("sending environment: {}".format(environment))
+
+    result = __execute_test(command, testdir, environment)
+    # compute overall results
+    detail = check.CheckDetail(koji_build, check.ReportType.KOJI_BUILD, result.outcome)
+    detail.update_outcome(result.outcome)
+    detail.note = 'dist-git tests for {} finished with result {}'.format(name, detail.outcome)
+
+    # print some summary to console
+    summary = 'dist-git tests %s for %s (%s)' % (detail.outcome, koji_build, detail.note)
+    log.info(summary)
+
+    output = check.export_YAML(detail)
+    return output
+
+def run_standard_interface_test(koji_build, rpms, workdir='.', artifactsdir='artifacts'):
+    name = rpm_utils.rpmformat(koji_build, 'n')
+
+    # set envirnment vars
+    environment = __set_env_vars(name, workdir, artifactsdir)
+
+    environment['TEST_SUBJECTS'] = ' '.join(rpms['downloaded_rpms'])
+
+    # create the command
+    testdir = os.path.join(workdir, name)
+    command = ['ansible-playbook', '-e', 'artifacts='+artifactsdir, '--tags', 'classic', 'tests.yml']
+
+    log.debug("sending environment: {}".format(environment))
 
+    result = __execute_test(command, testdir, environment)
     # compute overall results
     detail = check.CheckDetail(koji_build, check.ReportType.KOJI_BUILD, result.outcome)
     detail.update_outcome(result.outcome)

From 0a9e6a42083019a78b703e381426e51308b2b0b5 Mon Sep 17 00:00:00 2001
From: Andrej Manduch <amanduch@redhat.com>
Date: Dec 12 2017 15:07:52 +0000
Subject: [PATCH 2/2] changed dnf->package in playbook


I just found out that there is also distro-independent package modul
for ansible, so I changed `dnf` module to `package` module so it would
work also on yum based distro.

I would prefer to merge this comit to commit `61752c2 Added docker
support for shim`. I just don't want to do force-push while that commit
is undergoing evaluation in pull-request.

Signed-off-by: Andrej Manduch <amanduch@redhat.com>

---

diff --git a/standard-interface-shim.py b/standard-interface-shim.py
index 65c7a4f..bfba86b 100644
--- a/standard-interface-shim.py
+++ b/standard-interface-shim.py
@@ -96,7 +96,7 @@ def install_rpms_docker_playbook(rpms, artifactsdir='artifacts'):
       src: RPM_PATH
       dest: /root/RPM_NAME
   - name: install RPM_NAME
-    dnf: name=/root/RPM_NAME state=present
+    package: name=/root/RPM_NAME state=present
 """
     path_replace = re.compile("RPM_PATH", re.MULTILINE)
     name_replace = re.compile("RPM_NAME", re.MULTILINE)

