>From 3dd8ad7e4ca08b663a721c4da3cdace7e02d0a38 Mon Sep 17 00:00:00 2001
From: Abhishek Koneru <akoneru@redhat.com>
Date: Wed, 6 Nov 2013 16:08:39 -0500
Subject: [PATCH] Provide compose scripts for tests.

Provide a compose script for building the test rpm and
creating the job xml (by updating the job template with custom values
provided in a job xml config file). Also add a new option
--createrepo to the compose_pki_core_package script to create a repository
of the built rpms at the location specified in a config file.

Tickets #657, 722,723,724
---
 scripts/compose_functions                          | 84 +++++++++++++++++++++-
 scripts/compose_pki_core_packages                  |  7 +-
 scripts/compose_pki_test_package                   | 80 +++++++++++++++++++++
 scripts/run_tests                                  | 28 ++++++++
 scripts/scp_the_repo.exp                           | 16 +++++
 ....x86_64.f19.xml => beakerjob.rhcs.xml.template} | 12 ++--
 tests/dogtag/make-package.sh                       | 16 +++--
 tests/dogtag/update_beaker_job.py                  | 52 ++++++++++++++
 8 files changed, 279 insertions(+), 16 deletions(-)
 create mode 100755 scripts/compose_pki_test_package
 create mode 100755 scripts/run_tests
 create mode 100755 scripts/scp_the_repo.exp
 rename tests/dogtag/{beakerjob.rhcs.x86_64.f19.xml => beakerjob.rhcs.xml.template} (69%)
 create mode 100755 tests/dogtag/update_beaker_job.py

diff --git a/scripts/compose_functions b/scripts/compose_functions
index 3c08453e736c968dbf7df8d385ad87d1980b21d0..49ca67f488e374200c947f5dcaef7766aa60eb8e 100644
--- a/scripts/compose_functions
+++ b/scripts/compose_functions
@@ -109,6 +109,9 @@ Usage()
 	printf "                           ${MESSAGE}\n\n"
 	printf "Options:\n"
 	printf "  --without-javadoc        do not build Javadoc RPMS\n\n"
+        printf "  --createrepo=<repository_location_details_file> create a \n"
+        printf "                           repository with the rpms built."
+        printf "                           Provide the configuration file with the details. \n\n"
 }
 
 
@@ -236,12 +239,90 @@ Fetch_Source_Tarball()
 	done
 }
 
+###
+#   Create repository using the details in the config file ###
+#   Function parameters - $1 - repository file path.
+#   Sample repository file :
+#   ### Location where the files have to be copied ###
+#   ### Same parameter for both local and remote ###
+#   REPOSITORY_LOCATION=/var/www/html/pki/
+#
+#   ### Remote host details###
+#   REMOTE_HOSTNAME_OR_IP=vm-051.idm.lab.bos.redhat.com
+#   USERNAME=root
+#   PASSWORD=********
+###
+#
+Create_repo_after_build()
+{
+	if [ $# -eq 0 ]; then
+             echo "No repository configuration provided. Cannot create a repo."
+             exit -1
+	fi
+
+	if [ ! -f $1 ]; then
+             echo "$1 does not exist."
+             exit -1
+	fi
+
+	source $1
+
+	### Remove leading and trailing whitespaces ###
+	repo_location=`echo $REPOSITORY_LOCATION |sed -e 's/^ *//g' -e 's/ *$//g'`
+	destination=`echo $REMOTE_HOSTNAME_OR_IP|sed -e 's/^ *//g' -e 's/ *$//g'`
+	uname=`echo $USERNAME |sed -e 's/^ *//g' -e 's/ *$//g'`
+	pwd=`echo $PASSWORD |sed -e 's/^ *//g' -e 's/ *$//g'`
+
+	if [ "$repo_location" != "" ] ; then
+                if [ "$destination" != "" ] ; then
+                        createrepo ./RPMS/noarch/
+                        createrepo ./RPMS/x86_64/
+                        createrepo ./SOURCES/
+                        createrepo ./SRPMS/
+                        createrepo ./SPECS/
+                        if [ $uname == "" ] ; then
+                              echo "No username provided in the configuration file."
+                              echo "Enter the username:"
+                              read $uname
+                        fi
+                        if [ $pwd == "" ] ; then
+                              echo "No password provided in the configuration file."
+                              echo "Enter the password:"
+                              read -s $pwd
+                        fi
+                        url="$uname@$destination:$repo_location"
+                        echo $url
+                        ./scp_the_repo.exp "RPMS/" $url $PASSWORD
+                        ./scp_the_repo.exp "SRPMS/" $url $PASSWORD
+                        ./scp_the_repo.exp "SOURCES/" $url $PASSWORD
+                        ./scp_the_repo.exp "SPECS/" $url $PASSWORD
+                        if [ $? -ne 0 ]; then
+                             echo "Cannot copy the repositories to $destination\n"
+                             exit -1
+                        fi
+                        echo "Copied the repo to the location on the destination"
+                        exit 0
+                fi
+                if [ ! -e $repo_location ] ; then
+                       mkdir -p $repo_location
+                fi
+                cp -r RPMS SOURCES SRPMS SPECS $repo_location
+                createrepo $REPOSITORY_LOCATION/RPMS/noarch/
+                createrepo $REPOSITORY_LOCATION/RPMS/x86_64/
+                createrepo $REPOSITORY_LOCATION/SOURCES/
+                createrepo $REPOSITORY_LOCATION/SRPMS/
+                createrepo $REPOSITORY_LOCATION/SPECS/
+                echo "Build repository created at $repo_location."
+                exit 0
+	fi
+	echo "Cannot create repository. $repo_location has empty value in repository.cfg"
+}
 
 ##
 ## Check for command line argument validity
 ##
 
-GETOPT=`getopt -o '' -l without-javadoc,work-dir: -n "$0" -- "$@"`
+GETOPT=`getopt -o '' -l without-javadoc,work-dir:,createrepo: -n "$0" -- "$@"`
 
 if [ $? != 0 ] ; then
     Usage
@@ -254,6 +335,7 @@ while true ; do
     case "$1" in
         --without-javadoc) JAVADOC="--without javadoc" ; shift ;;
         --work-dir) WORK_DIR="$2" ; shift 2 ;;
+        --createrepo) CREATEREPO="Y" ; REPO_CFG_FILE="$2" ; shift 2 ;;
         --) shift ; break ;;
         *) echo "$0: unrecognized option '$1'" 1>&2 ; exit 255 ;;
     esac
diff --git a/scripts/compose_pki_core_packages b/scripts/compose_pki_core_packages
index eb124ebb0251f02bedfcc317825451aa6cfc66af..fbd2b96b15660cc79dfa3a27f60a33487123c51d 100755
--- a/scripts/compose_pki_core_packages
+++ b/scripts/compose_pki_core_packages
@@ -11,7 +11,6 @@
 COMPOSE_PWD=`dirname $0`
 source ${COMPOSE_PWD}/compose_functions
 
-
 ## Always switch into the base directory three levels
 ## above this shell script prior to executing it so
 ## that all of its output is written to this directory
@@ -214,3 +213,9 @@ chmod 775 ${PKI_CORE_PACKAGE_SCRIPT}
 cd ${PKI_PACKAGES} ;
 bash ./package_${PKI_CORE} | tee package_${PKI_CORE}.log 2>&1
 
+if [ "$CREATEREPO" == "Y" ] ; then
+   echo "Creating a repository of the built rpms"
+   cp -r $PKI_PWD/$PKI_DIR/scripts/repository.cfg .
+   cp -r $PKI_PWD/$PKI_DIR/scripts/scp_the_repo.exp .
+   Create_repo_after_build $REPO_CFG_FILE
+fi
diff --git a/scripts/compose_pki_test_package b/scripts/compose_pki_test_package
new file mode 100755
index 0000000000000000000000000000000000000000..075495489b96fda39039313cf3bd5d5e127b23e9
--- /dev/null
+++ b/scripts/compose_pki_test_package
@@ -0,0 +1,80 @@
+#!/bin/bash
+# BEGIN COPYRIGHT BLOCK
+# (C) 2010 Red Hat, Inc.
+# All rights reserved.
+# END COPYRIGHT BLOCK
+
+check_for_dependencies()
+{
+    NOT_FOUND="FALSE"
+    for i in "expect" "beakerlib" "beaker-client" "rhts-devel"
+    do
+        rpm -qa $i
+        if [ $? -ne 0 ] ; then
+            echo "$i package required."
+            NOT_FOUND="TRUE"
+        fi
+    done
+    if [ $NOT_FOUND = "TRUE" ] ; then
+        echo "Cannot compose test rpm without installing the dependencies."
+        exit -1
+    fi
+}
+
+check_for_dependencies
+
+if [ $# -lt 2 ];
+then
+  echo "Usage: $0 User-ID_for_personalization Job_xml_config_file [--runtests]"
+  exit -1
+fi
+
+RUN_TESTS='N'
+
+if [ $# -gt 2 ];
+then
+    if [ $3 = "--runtests" ]
+    then
+        RUN_TESTS='Y'
+    fi
+fi
+
+### Directory with all the compose scripts
+COMPOSE_DIR=`dirname $0 | cd ; pwd`
+
+PKI_SOURCE_DIR=`cd $COMPOSE_DIR/..; pwd`
+
+###   Build the task rpm outside the PKI git source tree.
+###   The rhts-build-package command checks if the code is is a git repository,
+###   if yes, it will compare tags of the current branch and the master branch.
+###   If they do not match then the rpm is not built.
+
+WORK_DIR=`cd $COMPOSE_DIR/../..;pwd`
+
+BUILD_DIR="$WORK_DIR/package.tests"
+
+rm -rf $BUILD_DIR
+
+mkdir $BUILD_DIR
+
+cd $BUILD_DIR
+
+cp -r "$PKI_SOURCE_DIR/tests" .
+
+cd tests/dogtag
+
+### Passing the keyword for personalizing and an option
+### to maintain a seperate folder for each beaker job (Optional)
+./make-package.sh $1 $2
+
+mv *.rpm ../../
+
+mv *.xml ../../
+
+### Running the tasks
+
+cd $COMPOSE_DIR
+
+if [ $RUN_TESTS = "Y" ] ; then
+     ./run_tests
+fi
diff --git a/scripts/run_tests b/scripts/run_tests
new file mode 100755
index 0000000000000000000000000000000000000000..8e4d65e9f8f9b7c40b4eba38e518d74ae5f8ce88
--- /dev/null
+++ b/scripts/run_tests
@@ -0,0 +1,28 @@
+#!/bin/bash
+
+WAIT_FOR_RESULT='N'
+if [ $# -ge 1 ] ; then
+    if [ "$1" = "--wait-on-beaker-job" ] ; then
+        WAIT_FOR_RESULT="Y"
+    else
+        echo "Unrecognized option. Usage: ./runtests --wait-on-beaker-job(Optional)"
+    fi
+fi
+COMPOSE_PWD=`dirname $0 | cd; pwd`
+
+TEST_PACKAGE=`cd $COMPOSE_PWD/../../package.tests; pwd`
+
+if [ ! -d $TEST_PACKAGE ] ; then
+    echo "Cannot run tests on beaker. Please compose the test rpms."
+    exit -1
+fi
+
+cd $TEST_PACKAGE
+
+bkr task-add *.rpm
+
+if [ "$WAIT_FOR_RESULT" = "Y" ] ; then
+    bkr job-submit --wait *.xml
+else
+    bkr job-submit *.xml
+fi
diff --git a/scripts/scp_the_repo.exp b/scripts/scp_the_repo.exp
new file mode 100755
index 0000000000000000000000000000000000000000..5b96372395f16d10bcaebe7afc64a04f026fcb2b
--- /dev/null
+++ b/scripts/scp_the_repo.exp
@@ -0,0 +1,16 @@
+#!/usr/bin/expect
+
+set timeout 20
+
+set copy_dir [lindex $argv 0]
+set url [lindex $argv 1]
+
+set password [lindex $argv 2]
+
+spawn scp -r $copy_dir $url
+
+expect "*password*"
+
+send "$password\r"
+
+expect eof
diff --git a/tests/dogtag/beakerjob.rhcs.x86_64.f19.xml b/tests/dogtag/beakerjob.rhcs.xml.template
similarity index 69%
rename from tests/dogtag/beakerjob.rhcs.x86_64.f19.xml
rename to tests/dogtag/beakerjob.rhcs.xml.template
index a15417cf5cb5f0d009bac18d2134c16aea82776f..5ba50d08641bd27c08f0f2e191e656b70be65f2c 100644
--- a/tests/dogtag/beakerjob.rhcs.x86_64.f19.xml
+++ b/tests/dogtag/beakerjob.rhcs.xml.template
@@ -9,15 +9,12 @@
 			<packages/>
 			<ks_appends/>
 			<repos>
-				<repo name="repo1" url="http://mickey.dsdev.sjc.redhat.com/repos/pki/dogtag/10/F19/devel_x86_64/x86_64/"/>
-				<repo name="repo2" url="http://mickey.dsdev.sjc.redhat.com/repos/pki/dogtag/10/F19/devel_x86_64/noarch/"/>
 			</repos>
 			<distroRequires>
 				<and>
-					<distro_family op="=" value="Fedora19"/>
-					<distro_variant op="=" value="Fedora"/>
-					<distro_name op="=" value="Fedora-19"/>
-					<distro_arch op="=" value="x86_64"/>
+					<distro_family op="=" value=""/>
+					<distro_name op="=" value=""/>
+					<distro_arch op="=" value=""/>
 				</and>
 			</distroRequires>
 			<hostRequires>
@@ -27,7 +24,7 @@
 			<task name="/distribution/install" role="STANDALONE">
 				<params/>
 			</task>
-			<task name="/CoreOS/rhcs/lsunkaras_dir" role="MASTER">
+			<task name="/CoreOS/rhcs/PKI_TEST_USER_ID" role="MASTER">
 				<params>
 					<param name="TEST_ALL" value="TRUE"/>
 					<param name="QUICKINSTALL" value="FALSE"/>
@@ -35,6 +32,7 @@
 					<param name="USER_SHOW_CA" value="FALSE"/>
 					<param name="USER_FIND_CA" value="FALSE"/>
 					<param name="USER_DEL_CA" value="FALSE"/>
+                                        <param name="DEV_JAVA_TESTS" value="FALSE"/>
 				</params>
 			</task>
 			<task name="/distribution/reservesys" role="STANDALONE">
diff --git a/tests/dogtag/make-package.sh b/tests/dogtag/make-package.sh
index 98e117c36a7bd09c480539d3e7c7cb5fe975c8eb..0ba0018a50f2d55afaebaff7ee184c245e8ee5fc 100755
--- a/tests/dogtag/make-package.sh
+++ b/tests/dogtag/make-package.sh
@@ -1,9 +1,9 @@
 #!/bin/sh
 
-### Exit if there is no personalization id specified.
-if [ $# -lt 1 ];
+### Exit if there not enough parameters specified.
+if [ $# -lt 2 ];
 then
-  echo "Usage: ./make-package.sh <User-ID> <Different_subfolder_for_each_beaker_job_?(Y/N)(Optional)>"
+  echo "Usage: ./make-package.sh User-ID Job_xml_config_file [Different_subfolder_for_each_beaker_job_?(Y/N)]"
   exit -1
 fi
 
@@ -22,18 +22,20 @@ then
     fi
 fi
 
-#rpm_identifier=".$date_time"
-echo $user_id
 ### Replacing the default value with the "user_id/current_number"
-sed -e "s|PKI_TEST_USER_ID|${user_id}|g" -e "s|_RPM_IDENTIFIER|${rpm_identifier}|g" .Makefile.save >> Makefile 
+sed -e "s|PKI_TEST_USER_ID|${user_id}|g" -e "s|_RPM_IDENTIFIER|${rpm_identifier}|g" .Makefile.save >> Makefile
 
 chmod +x Makefile
 
 ### Making the rpm
 make package
 
-### Remove the current rpm and place the original back
+### Remove the current makefile and place the original back
 
 rm -rf Makefile
 
 mv .Makefile.save Makefile
+
+sed -e "s|PKI_TEST_USER_ID|${user_id}|g"  beakerjob.rhcs.xml.template >> beakerjob.rhcs.xml
+
+python update_beaker_job.py beakerjob.rhcs.xml $2
diff --git a/tests/dogtag/update_beaker_job.py b/tests/dogtag/update_beaker_job.py
new file mode 100755
index 0000000000000000000000000000000000000000..3987e30cd572911bf145ba79bd2be97d76ff7905
--- /dev/null
+++ b/tests/dogtag/update_beaker_job.py
@@ -0,0 +1,52 @@
+#! /usr/bin/python
+
+import sys
+from lxml import etree
+from ConfigParser import ConfigParser
+
+
+""" Parse the xml template and replace the values with the
+    values from the configuration file passed.
+    Usage: py modifybeakerjobxml.py <XML_TEMPLATE_PATH> <CONFIG_FILE_PATH>"""
+
+
+doc = etree.parse(str(sys.argv[1]))
+
+parser = ConfigParser()
+parser.read(str(sys.argv[2]))
+
+props=parser.defaults()
+
+repos=props['repos']
+
+if repos is not None:
+    repos = str(repos).split()
+    repos_element=doc.getroot().find('recipeSet/recipe/repos')
+    i = 1
+    for repo in repos:
+        new_child = etree.SubElement(repos_element, 'repo', name='repo'+str(i), url=repo)
+        i = i + 1
+
+element = None
+distro_family = props['distro_family']
+if distro_family is not None:
+    element=doc.find('recipeSet/recipe/distroRequires/and/distro_family')
+    element.attrib['value'] = distro_family
+
+distro_name = props['distro_name']
+if distro_name is not None:
+    element=doc.find('recipeSet/recipe/distroRequires/and/distro_name')
+    element.attrib['value'] = distro_name
+
+distro_arch = props['distro_arch']
+if distro_arch is not None:
+    element=doc.find('recipeSet/recipe/distroRequires/and/distro_arch')
+    element.attrib['value'] = distro_arch
+
+hostname = props['hostname']
+if distro_family is not None:
+    element=doc.find('recipeSet/recipe/hostRequires')
+    etree.SubElement(element, 'hostname', op='=', value=hostname)
+
+with open(str(sys.argv[1]), 'w') as outfile:
+    doc.write(outfile)
-- 
1.8.3.1

