hive-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From br...@apache.org
Subject svn commit: r1665580 - in /hive/trunk: dev-support/ testutils/metastore/ testutils/metastore/dbs/ testutils/metastore/dbs/mysql/ testutils/ptest2/src/main/java/org/apache/hive/ptest/execution/
Date Tue, 10 Mar 2015 15:42:35 GMT
Author: brock
Date: Tue Mar 10 15:42:34 2015
New Revision: 1665580

URL: http://svn.apache.org/r1665580
Log:
HIVE-9800 - Create scripts to do metastore upgrade tests on Jenkins (Sergio Pena via Brock)

Added:
    hive/trunk/dev-support/jenkins-execute-hms-test.sh
    hive/trunk/testutils/metastore/
    hive/trunk/testutils/metastore/dbs/
    hive/trunk/testutils/metastore/dbs/mysql/
    hive/trunk/testutils/metastore/dbs/mysql/execute.sh
    hive/trunk/testutils/metastore/dbs/mysql/prepare.sh
    hive/trunk/testutils/metastore/execute-test-on-lxc.sh
    hive/trunk/testutils/metastore/metastore-upgrade-test.sh
Modified:
    hive/trunk/testutils/ptest2/src/main/java/org/apache/hive/ptest/execution/JIRAService.java

Added: hive/trunk/dev-support/jenkins-execute-hms-test.sh
URL: http://svn.apache.org/viewvc/hive/trunk/dev-support/jenkins-execute-hms-test.sh?rev=1665580&view=auto
==============================================================================
--- hive/trunk/dev-support/jenkins-execute-hms-test.sh (added)
+++ hive/trunk/dev-support/jenkins-execute-hms-test.sh Tue Mar 10 15:42:34 2015
@@ -0,0 +1,171 @@
+#!/bin/bash
+
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This script executes all hive metastore upgrade scripts on an specific
+# database server in order to verify that upgrade scripts are working
+# properly.
+
+# This script is run on jenkins only, and it creates some LXC containers
+# in order to execute the metastore-upgrade-tests for different
+# server configurations.
+
+set -e -x
+. jenkins-common.sh
+
+test -n "$BRANCH" || fail "BRANCH must be specified"
+test -n "$ISSUE_NUM" || fail "ISSUE_NUM must be specified"
+test -n "$SSH_HOST" || fail "SSH_HOST must be specified"
+test -n "$SSH_KEY" || fail "SSH_KEY must be specified"
+test -n "$LOG_ENDPOINT" || fail "LOG_ENDPOINT must be specified"
+test -n "$JENKINS_URL" || fail "JENKINS_URL must be specified"
+test -n "$API_PASSWORD" || fail "API_PASSWORD must be specified"
+
+test -n "$WORKSPACE" || WORKSPACE="$HOME"
+
+export JIRA_NAME="HIVE-${ISSUE_NUM}"
+export ROOT=$PWD
+export JIRA_ROOT_URL="https://issues.apache.org"
+export BUILD_TAG="${BUILD_TAG##jenkins-}"
+
+process_jira
+
+PUBLISH_VARS=(
+	BUILD_STATUS=buildStatus
+	BUILD_TAG=buildTag
+	LOG_ENDPOINT=logsURL
+	JENKINS_URL=jenkinsURL
+	PATCH_URL=patchUrl
+	JIRA_NAME=jiraName
+	JIRA_URL=jiraUrl
+	BRANCH=branch
+	REPO=repository
+	REPO_NAME=repositoryName
+	TESTS_EXECUTED=numTestsExecuted
+	FAILED_TESTS=failedTests
+	MESSAGES=messages
+)
+
+BUILD_STATUS=0
+PATCH_URL="${JIRA_ROOT_URL}${PATCH_URL}"
+JIRA_URL="${JIRA_ROOT_URL}/jira"
+REPO_NAME="trunk"
+REPO="trunk"
+TESTS_EXECUTED=0
+FAILED_TESTS=()
+MESSAGES=()
+
+build_ptest2() {
+	local path="$1"
+	local curpath="$PWD"
+
+	test -d $path || mkdir -p $path
+	rm -rf $path/ptest2
+	svn co http://svn.apache.org/repos/asf/hive/trunk/testutils/ptest2/ $path/ptest2
+	cd $path/ptest2
+	mvn clean package -DskipTests -Drat.numUnapprovedLicenses=1000 -Dmaven.repo.local=$WORKSPACE/.m2
+
+	cd $curpath
+}
+
+add_json_object() {
+	echo -n "\"$2\" : \"$3\"" >> $1
+}
+
+add_json_array() {
+	echo -n "\"$2\" : [ $3 ]" >> $1
+}
+
+create_publish_file() {
+	local json_file=$(mktemp)
+	local arr_length=${#PUBLISH_VARS[@]}
+
+	vars_added=0
+	echo "{" > $json_file
+	for i in ${PUBLISH_VARS[@]}
+	do
+		var=${i%=*}
+		key=${i#*=}
+		val=
+
+		# Treat this as an array
+		if [[ $var = "FAILED_TESTS" ]]; then
+			if [[ ${#FAILED_TESTS[@]} -gt 0 ]]; then
+				val=$(printf "\"%s\","  "${FAILED_TESTS[@]}")
+				val=${val%?}
+			fi
+			add_json_array $json_file $key "$val"
+		elif [[ $var = "MESSAGES" ]]; then
+			if [[ ${#MESSAGES[@]} -gt 0 ]]; then
+				val=$(printf "\"%s\","  "${MESSAGES[@]}")
+				val=${val%?}
+			fi
+			add_json_array $json_file $key "$val"
+		else
+			val=${!var}
+			add_json_object $json_file $key $val
+		fi
+
+		vars_added=$((vars_added+1))
+
+		if [[ $vars_added -lt $arr_length ]]; then
+			echo "," >> $json_file
+		else
+			echo >> $json_file
+		fi
+	done
+	echo "}" >> $json_file
+
+	echo $json_file
+}
+
+wget "${PATCH_URL}" -O /tmp/${JIRA_NAME}.patch
+if cat /tmp/${JIRA_NAME}.patch | grep "^diff.*metastore/scripts/upgrade/" >/dev/null;
then
+	ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i $SSH_KEY $SSH_HOST "
+		rm -rf metastore/ &&
+		svn co http://svn.apache.org/repos/asf/hive/trunk/testutils/metastore metastore &&
+		sudo bash -x metastore/execute-test-on-lxc.sh --patch \"${PATCH_URL}\"
+	"
+	ret=$?
+	if [[ $ret = 0 ]]; then
+		scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i $SSH_KEY ${SSH_HOST}:/tmp/execute-test-on-lxc.sh.log
/tmp || exit 1
+		TESTS_EXECUTED=$(cat /tmp/execute-test-on-lxc.sh.log  | grep "Executing sql test" | wc
-l)
+
+		while read line
+		do
+			if echo $line | grep 'Test failed' > /dev/null; then
+				FAILED_TESTS+=("$line")
+			fi
+
+			MESSAGES+=("$line")
+		done < /tmp/execute-test-on-lxc.sh.log
+	fi
+
+	# Publish results to JIRA only if there were errors during the tests
+	BUILD_STATUS=$ret
+	if [[ $BUILD_STATUS -gt 0 ]] || [[ ${#FAILED_TESTS[@]} -gt 0 ]]; then
+		build_ptest2 "hive/build"
+
+		json_file=$(create_publish_file)
+		java -cp "hive/build/ptest2/target/hive-ptest-1.0-classes.jar:hive/build/ptest2/target/lib/*"
org.apache.hive.ptest.execution.JIRAService \
+			--user "hive" \
+			--password "$API_PASSWORD" \
+			--file $json_file
+		ret=$?
+	fi
+
+	exit $ret
+fi

Added: hive/trunk/testutils/metastore/dbs/mysql/execute.sh
URL: http://svn.apache.org/viewvc/hive/trunk/testutils/metastore/dbs/mysql/execute.sh?rev=1665580&view=auto
==============================================================================
--- hive/trunk/testutils/metastore/dbs/mysql/execute.sh (added)
+++ hive/trunk/testutils/metastore/dbs/mysql/execute.sh Tue Mar 10 15:42:34 2015
@@ -0,0 +1,26 @@
+#!/bin/bash
+
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This script executes all hive metastore upgrade scripts on an specific
+# database server in order to verify that upgrade scripts are working
+# properly.
+
+cd $(dirname $1)
+
+echo "Executing script: $1"
+
+mysql hive_hms_testing < $1

Added: hive/trunk/testutils/metastore/dbs/mysql/prepare.sh
URL: http://svn.apache.org/viewvc/hive/trunk/testutils/metastore/dbs/mysql/prepare.sh?rev=1665580&view=auto
==============================================================================
--- hive/trunk/testutils/metastore/dbs/mysql/prepare.sh (added)
+++ hive/trunk/testutils/metastore/dbs/mysql/prepare.sh Tue Mar 10 15:42:34 2015
@@ -0,0 +1,28 @@
+#!/bin/bash
+
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This script executes all hive metastore upgrade scripts on an specific
+# database server in order to verify that upgrade scripts are working
+# properly.
+
+echo "Installing MySQL dependencies:"
+export DEBIAN_FRONTEND=noninteractive
+apt-get install -y mysql-server
+
+echo "Preparing MySQL environment:"
+mysql mysql -Bse "DROP DATABASE IF EXISTS hive_hms_testing"
+mysql mysql -Bse "CREATE DATABASE hive_hms_testing"

Added: hive/trunk/testutils/metastore/execute-test-on-lxc.sh
URL: http://svn.apache.org/viewvc/hive/trunk/testutils/metastore/execute-test-on-lxc.sh?rev=1665580&view=auto
==============================================================================
--- hive/trunk/testutils/metastore/execute-test-on-lxc.sh (added)
+++ hive/trunk/testutils/metastore/execute-test-on-lxc.sh Tue Mar 10 15:42:34 2015
@@ -0,0 +1,142 @@
+#!/bin/bash
+
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This script executes all hive metastore upgrade scripts on an specific
+# database server in order to verify that upgrade scripts are working
+# properly.
+
+# This script is run on jenkins only, and it creates some LXC containers
+# in order to execute the metastore-upgrade-tests for different
+# server configurations.
+
+set -e
+cd $(dirname $0)
+
+OUT_LOG="/tmp/$(basename $0).log"
+rm -f $OUT_LOG
+
+log() {
+        echo "$@"
+        echo "$@" >> $OUT_LOG
+}
+
+
+fail() {
+	echo $@
+	exit 1
+}
+
+[[ $# != 2 ]] && fail "Usage: $0 --patch PATH_URL"
+
+PATCH_URL=
+while [[ $# -gt 0 ]]; do
+	if [[ $1 = "--patch" ]]; then
+		PATCH_URL=$2
+	fi
+
+	shift 2
+done
+
+test -n "$PATCH_URL" || fail "--patch value is required."
+
+get_supported_dbs() {
+	ls dbs/ -1
+}
+
+lxc_get_ip() {
+	 lxc-ls -f "^$1$" | tail -1 | awk '{print $3}' | tr -d ,
+}
+
+lxc_exists() {
+	lxc-ls "^$1$" | grep $1 >/dev/null
+}
+
+lxc_create() {
+	lxc-create -n $1 -t download -- --dist "ubuntu" --release "trusty" --arch "amd64"
+	lxc-attach -n $1 -- apt-get update
+	lxc-attach -n $1 -- apt-get install -y openssh-server subversion
+	printf "root\nroot" | sudo lxc-attach -n $1 -- passwd
+	lxc-attach -n $1 -- sed -i /etc/ssh/sshd_config 's/^PermitRootLogin without-password/PermitRootLogin
yes/'
+	lxc-attach -n $1 -- service ssh restart
+}
+
+lxc_running() {
+	lxc-ls -f "^$1$" | tail -1 | awk '{print $2}' | grep "RUNNING" >/dev/null
+}
+
+lxc_start() {
+	lxc-start -n $1 --daemon
+	lxc-wait -n mysql -s RUNNING
+	sleep 10 # wait a little longer
+}
+
+lxc_prepare() {
+	lxc-attach -n $1 -- rm -rf /tmp/hive
+	lxc-attach -n $1 -- mkdir /tmp/hive
+
+	# TODO: Copy hive code to the container using SCP.
+	lxc-attach -n $1 -- svn co http://svn.apache.org/repos/asf/hive/trunk /tmp/hive
+
+	lxc-attach -n $1 -- wget $PATCH_URL -O /tmp/hive/hms.patch
+	lxc-attach -n $1 -- patch -d /tmp/hive -p1 -i /tmp/hive/hms.patch
+}
+
+lxc_print_metastore_log() {
+	lxc-attach -n $1 -- cat /tmp/metastore-upgrade-test.sh.log
+}
+
+run_tests() {
+	#lxc-attach -n $1 -- bash /tmp/dev-support/tests/metastore-upgrade/metastore-upgrade-test.sh
--db $1
+	lxc-attach -n $1 -- bash /tmp/hive/testutils/metastore/metastore-upgrade-test.sh --db $1
+}
+
+# Install LXC packages if needed
+if ! which lxc-create >/dev/null; then
+	apt-get update
+	apt-get -y install lxc || exit 1
+fi
+
+for d in $(get_supported_dbs)
+do
+	name=$(basename $d)
+
+	# Create container
+	if ! lxc_exists $name; then
+		log "LXC $name is not found. Creating new container..."
+		lxc_create $name || exit 1
+		log "Container created."
+	else
+		log "LXC $name found."
+	fi
+
+	# Start container
+	if ! lxc_running $name; then
+		log "LXC $name is not started. Starting container..."
+		lxc_start $name || exit 1
+		log "Container started."
+	fi
+
+	# Prepare container
+	log "Preparing $name container..."
+	lxc_prepare $name || exit 1
+	log "Container prepared."
+
+	# Execute metastore upgrade tests
+	echo "Running metastore upgrade tests for $name..."
+	run_tests $name
+	log "$(lxc_print_metastore_log $name)"
+done

Added: hive/trunk/testutils/metastore/metastore-upgrade-test.sh
URL: http://svn.apache.org/viewvc/hive/trunk/testutils/metastore/metastore-upgrade-test.sh?rev=1665580&view=auto
==============================================================================
--- hive/trunk/testutils/metastore/metastore-upgrade-test.sh (added)
+++ hive/trunk/testutils/metastore/metastore-upgrade-test.sh Tue Mar 10 15:42:34 2015
@@ -0,0 +1,126 @@
+#!/bin/bash
+
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This script executes all hive metastore upgrade scripts on an specific
+# database server in order to verify that upgrade scripts are working
+# properly.
+
+set -e
+
+cd $(dirname $0)
+
+usage() {
+  echo "Usage: "
+  echo "  $0 --db DB_SERVER"
+  echo
+  echo "Options"
+  echo "  --db DB_SERVER  Specifies a database server where to run the"
+  echo "                  upgrade tests."
+  echo
+}
+
+if [ $# != 2 ]; then
+  usage && exit 1
+fi
+
+if [ $1 != "--db" ]; then
+  echo "Error: unrecognized $1 option."
+  echo
+  usage && exit 1
+fi
+
+#
+# These tests work by calling special commands from specific
+# servers that are stored as scripts in a directory name
+# related to the server to test.
+#
+
+OUT_LOG="/tmp/$(basename $0).log"
+rm -f $OUT_LOG
+
+log() {
+	echo $@
+	echo $@ >> $OUT_LOG
+}
+
+DB_SERVER="$2"
+if [ ! -d "dbs/$DB_SERVER" ]; then
+	log "Error: $DB_SERVER upgrade tests is not supported yet."
+	echo
+	echo "You must select from any of the following tests:"
+  ls dbs/
+  exit 1
+fi
+
+SCRIPT_PREPARE="$(pwd)/dbs/$DB_SERVER/prepare.sh"
+SCRIPT_EXECUTE="$(pwd)/dbs/$DB_SERVER/execute.sh"
+
+[ ! -e "$SCRIPT_PREPARE" ] && log "Error: $SCRIPT_PREPARE does not exist." &&
exit 1
+[ ! -e "$SCRIPT_EXECUTE" ] && log "Error: $SCRIPT_EXECUTE does not exist." &&
exit 1
+
+HMS_UPGRADE_DIR="$(pwd)/../../metastore/scripts/upgrade"
+if [ ! -d "$HMS_UPGRADE_DIR/$DB_SERVER" ]; then
+	log "Error: $DB_SERVER upgrade scripts are not found on $HMS_UPGRADE_DIR."
+	exit 1
+fi
+
+#
+# Before running any test, we need to prepare the environment.
+# This is done by calling the prepare script. This script must
+# install, configure and start the database server where to run
+# the upgrade script.
+#
+
+log "Calling $SCRIPT_PREPARE ..."
+bash -e -x $SCRIPT_PREPARE || exit 1
+log "Server prepared."
+
+#
+# Now we execute each of the .sql scripts on the database server.
+#
+
+execute_test() {
+	log "Executing sql test: $DB_SERVER/$(basename $1)"
+	if ! bash -x -e $SCRIPT_EXECUTE $1; then
+		log "Test failed: $DB_SERVER/$(basename $1)"
+		return 1
+	fi
+}
+
+VERSION_BASE="0.10.0"
+
+HIVE_SCHEMA_BASE="$HMS_UPGRADE_DIR/$DB_SERVER/hive-schema-$VERSION_BASE.$DB_SERVER.sql"
+if [ ! -f $HIVE_SCHEMA_BASE ]; then
+	log "Error: $HIVE_SCHEMA_BASE is not found."
+	exit 1
+fi
+
+if ! execute_test $HIVE_SCHEMA_BASE; then
+	echo "Error: Cannot execute SQL file: $HIVE_SCHEMA_BASE"
+fi
+
+begin_upgrade_test="false"
+find $HMS_UPGRADE_DIR/$DB_SERVER/upgrade-* |  sort -V | while read script
+do
+	name=$(basename $script)
+	if [ $begin_upgrade_test = "true" ] || echo $name | grep "upgrade-$VERSION_BASE"; then
+		begin_upgrade_test="true"
+		if ! execute_test $script; then
+			echo "Error: Cannot execute SQL file: $script"
+		fi
+	fi
+done

Modified: hive/trunk/testutils/ptest2/src/main/java/org/apache/hive/ptest/execution/JIRAService.java
URL: http://svn.apache.org/viewvc/hive/trunk/testutils/ptest2/src/main/java/org/apache/hive/ptest/execution/JIRAService.java?rev=1665580&r1=1665579&r2=1665580&view=diff
==============================================================================
--- hive/trunk/testutils/ptest2/src/main/java/org/apache/hive/ptest/execution/JIRAService.java
(original)
+++ hive/trunk/testutils/ptest2/src/main/java/org/apache/hive/ptest/execution/JIRAService.java
Tue Mar 10 15:42:34 2015
@@ -18,13 +18,12 @@
  */
 package org.apache.hive.ptest.execution;
 
+import java.io.File;
 import java.io.IOException;
 import java.net.URL;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
-import java.util.SortedSet;
+import java.util.*;
 
+import org.apache.commons.cli.*;
 import org.apache.commons.io.FilenameUtils;
 import org.apache.hive.ptest.api.server.TestLogger;
 import org.apache.hive.ptest.execution.conf.Context;
@@ -50,13 +49,15 @@ import org.apache.http.protocol.BasicHtt
 import org.apache.http.protocol.ExecutionContext;
 import org.apache.http.protocol.HttpContext;
 import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.JsonFactory;
+import org.codehaus.jackson.JsonParser;
+import org.codehaus.jackson.JsonToken;
 import org.slf4j.Logger;
 
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Joiner;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
-import com.google.common.collect.Sets;
 
 class JIRAService {
   static final int MAX_MESSAGES = 200;
@@ -71,6 +72,15 @@ class JIRAService {
   private final String mJenkinsURL;
   private final String mLogsURL;
 
+  private static final String OPT_HELP_SHORT = "h";
+  private static final String OPT_HELP_LONG = "help";
+  private static final String OPT_USER_SHORT = "u";
+  private static final String OPT_USER_LONG = "user";
+  private static final String OPT_PASS_SHORT = "p";
+  private static final String OPT_PASS_LONG = "password";
+  private static final String OPT_FILE_SHORT = "f";
+  private static final String OPT_FILE_LONG = "file";
+
   public JIRAService(Logger logger, TestConfiguration configuration, String buildTag) {
     mLogger = logger;
     mName = configuration.getJiraName();
@@ -277,22 +287,148 @@ class JIRAService {
     }
     return result.trim();
   }
+
+  private static void assertRequired(CommandLine commandLine, String[] requiredOptions) throws
IllegalArgumentException {
+    for(String requiredOption : requiredOptions) {
+      if(!commandLine.hasOption(requiredOption)) {
+        throw new IllegalArgumentException("--" + requiredOption + " is required");
+      }
+    }
+  }
+
+  private static final String FIELD_BUILD_STATUS = "buildStatus";
+  private static final String FIELD_BUILD_TAG = "buildTag";
+  private static final String FIELD_LOGS_URL = "logsURL";
+  private static final String FIELD_JENKINS_URL = "jenkinsURL";
+  private static final String FIELD_PATCH_URL = "patchUrl";
+  private static final String FIELD_JIRA_NAME = "jiraName";
+  private static final String FIELD_JIRA_URL = "jiraUrl";
+  private static final String FIELD_REPO = "repository";
+  private static final String FIELD_REPO_NAME = "repositoryName";
+  private static final String FIELD_REPO_BRANCH = "branch";
+  private static final String FIELD_NUM_TESTS_EXECUTED = "numTestsExecuted";
+  private static final String FIELD_FAILED_TESTS = "failedTests";
+  private static final String FIELD_MESSAGES = "messages";
+  private static final String FIELD_JIRA_USER = "jiraUser";
+  private static final String FIELD_JIRA_PASS= "jiraPassword";
+
+  private static Map<String, Class> supportedJsonFields = new HashMap<String, Class>()
{
+    {
+      put(FIELD_BUILD_STATUS, Integer.class);
+      put(FIELD_BUILD_TAG, String.class);
+      put(FIELD_LOGS_URL, String.class);
+      put(FIELD_JENKINS_URL, String.class);
+      put(FIELD_PATCH_URL, String.class);
+      put(FIELD_JIRA_NAME, String.class);
+      put(FIELD_JIRA_URL, String.class);
+      put(FIELD_REPO, String.class);
+      put(FIELD_REPO_NAME, String.class);
+      put(FIELD_REPO_BRANCH, String.class);
+      put(FIELD_NUM_TESTS_EXECUTED, Integer.class);
+      put(FIELD_FAILED_TESTS, SortedSet.class);
+      put(FIELD_MESSAGES, List.class);
+    }
+  };
+
+  private static Map<String, Object> parseJsonFile(String jsonFile) throws IOException
{
+    JsonFactory jsonFactory = new JsonFactory();
+    JsonParser jsonParser = jsonFactory.createJsonParser(new File(jsonFile));
+    Map<String, Object> values = new HashMap<String, Object>();
+
+    while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
+      String fieldName = jsonParser.getCurrentName();
+      if (supportedJsonFields.containsKey(fieldName)) {
+        jsonParser.nextToken();
+
+        Class clazz = supportedJsonFields.get(fieldName);
+        if (clazz == String.class) {
+          values.put(fieldName, jsonParser.getText());
+        } else if (clazz == Integer.class) {
+          values.put(fieldName, Integer.valueOf(jsonParser.getText()));
+        } else if (clazz == SortedSet.class) {
+          SortedSet<String> failedTests = new TreeSet<String>();
+          while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
+            failedTests.add(jsonParser.getText());
+          }
+
+          values.put(fieldName, failedTests);
+        } else if (clazz == List.class) {
+          List<String> messages = new ArrayList<String>();
+          while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
+            messages.add(jsonParser.getText());
+          }
+
+          values.put(fieldName, messages);
+        }
+      }
+    }
+
+    jsonParser.close();
+    return values;
+  }
+
+  private static CommandLine parseCommandLine(String[] args) throws ParseException {
+    CommandLineParser parser = new GnuParser();
+
+    Options options = new Options();
+    options.addOption(OPT_HELP_SHORT, OPT_HELP_LONG, false, "Display help text and exit");
+    options.addOption(OPT_USER_SHORT, OPT_USER_LONG, true, "Jira username.");
+    options.addOption(OPT_PASS_SHORT, OPT_PASS_LONG, true, "Jira password.");
+    options.addOption(OPT_FILE_SHORT, OPT_FILE_LONG, true, "Pathname to file (JSON format)
that will be post as Jira comment.");
+
+    CommandLine cmd = parser.parse(options, args);
+
+    // If help option is requested, then display help and exit
+    if (cmd.hasOption(OPT_HELP_LONG)) {
+      new HelpFormatter().printHelp(JIRAService.class.getName(), options, true);
+      return null;
+    }
+
+    assertRequired(cmd, new String[]{
+        OPT_USER_LONG,
+        OPT_PASS_LONG,
+        OPT_FILE_LONG
+    });
+
+    return cmd;
+  }
+
   public static void main(String[] args) throws Exception {
-    TestLogger logger = new TestLogger(System.err, TestLogger.LEVEL.TRACE);
+    CommandLine cmd = null;
+
+    try {
+      cmd = parseCommandLine(args);
+    } catch(ParseException e) {
+      System.out.println("Error parsing command arguments: " + e.getMessage());
+      System.exit(1);
+    }
+
+    // If null is returned, then help message was displayed in parseCommandLine method
+    if (cmd == null) {
+      System.exit(0);
+    }
+
+    Map<String, Object> jsonValues = parseJsonFile(cmd.getOptionValue(OPT_FILE_LONG));
+
     Map<String, String> context = Maps.newHashMap();
-    context.put("jiraUrl", "https://issues.apache.org/jira");
-    context.put("jiraUser", "hiveqa");
-    context.put("jiraPassword", "password goes here");
-    context.put("branch", "trunk");
-    context.put("repository", "repo");
-    context.put("repositoryName", "repoName");
-    context.put("antArgs", "-Dsome=thing");
-    context.put("logsURL", "http://ec2-174-129-184-35.compute-1.amazonaws.com/logs");
+    context.put(FIELD_JIRA_URL, (String)jsonValues.get(FIELD_JIRA_URL));
+    context.put(FIELD_JIRA_USER, cmd.getOptionValue(OPT_USER_LONG));
+    context.put(FIELD_JIRA_PASS, cmd.getOptionValue(OPT_PASS_LONG));
+    context.put(FIELD_LOGS_URL, (String)jsonValues.get(FIELD_LOGS_URL));
+    context.put(FIELD_REPO, (String)jsonValues.get(FIELD_REPO));
+    context.put(FIELD_REPO_NAME, (String)jsonValues.get(FIELD_REPO_NAME));
+    context.put(FIELD_REPO_BRANCH, (String)jsonValues.get(FIELD_REPO_BRANCH));
+    context.put(FIELD_JENKINS_URL, (String)jsonValues.get(FIELD_JENKINS_URL));
+
+    TestLogger logger = new TestLogger(System.err, TestLogger.LEVEL.TRACE);
     TestConfiguration configuration = new TestConfiguration(new Context(context), logger);
-    configuration.setJiraName("HIVE-4892");
-    JIRAService service = new JIRAService(logger, configuration, "test-123");
-    List<String> messages = Lists.newArrayList("msg1", "msg2");
-    SortedSet<String> failedTests = Sets.newTreeSet(Collections.singleton("failed"));
-    service.postComment(false, 5, failedTests, messages);
+    configuration.setJiraName((String)jsonValues.get(FIELD_JIRA_NAME));
+    configuration.setPatch((String)jsonValues.get(FIELD_PATCH_URL));
+
+    JIRAService service = new JIRAService(logger, configuration, (String)jsonValues.get(FIELD_BUILD_TAG));
+    List<String> messages = (List)jsonValues.get(FIELD_MESSAGES);
+    SortedSet<String> failedTests = (SortedSet)jsonValues.get(FIELD_FAILED_TESTS);
+    boolean error = (Integer)jsonValues.get(FIELD_BUILD_STATUS) == 0 ? false : true;
+    service.postComment(error, (Integer)jsonValues.get(FIELD_NUM_TESTS_EXECUTED), failedTests,
messages);
   }
 }
\ No newline at end of file



Mime
View raw message