Skip to content

Instantly share code, notes, and snippets.

@vshank77
Last active February 9, 2018 00:04
Show Gist options
  • Save vshank77/82e158be7b8cebad0579c812116256ac to your computer and use it in GitHub Desktop.
Save vshank77/82e158be7b8cebad0579c812116256ac to your computer and use it in GitHub Desktop.
es-integration-ant
<?xml version="1.0" encoding="UTF-8"?>
<project name="integration-tests" xmlns:if="ant:if" xmlns:unless="ant:unless">
<!-- our pid file for easy cleanup -->
<property name="integ.pidfile" location="${project.build.directory}/integration-tests/run/es.pid"/>
<!-- if this exists, ES is running (maybe) -->
<available property="integ.pidfile.exists" file="${integ.pidfile}"/>
<!-- name of our cluster, maybe needs changing -->
<property name="integ.http.port" value="9200"/>
<property name="integ.transport.port" value="9300"/>
<property name="integ.cluster.name" value="steeleye-cluster"/>
<!-- runs an OS script -->
<macrodef name="run-script">
<attribute name="script"/>
<attribute name="spawn" default="false"/>
<element name="nested" optional="true"/>
<sequential>
<local name="failonerror"/>
<condition property="failonerror">
<isfalse value="@{spawn}"/>
</condition>
<!-- create a temp CWD, to enforce that commands don't rely on CWD -->
<local name="temp.cwd"/>
<tempfile property="temp.cwd" destDir="${project.build.directory}/integration-tests/run/tmp"
deleteonexit="true"/>
<mkdir dir="${temp.cwd}"/>
<!-- print commands we run -->
<local name="script.base"/>
<basename file="@{script}" property="script.base"/>
<!-- crappy way to output, but we need it. make it nice later -->
<echoxml>
<exec script="${script.base}">
<nested/>
</exec>
</echoxml>
<exec executable="cmd" osfamily="winnt" dir="${temp.cwd}" failonerror="${failonerror}" spawn="@{spawn}"
taskname="${script.base}">
<arg value="/c"/>
<arg value="&quot;"/>
<arg value="@{script}.bat"/>
<nested/>
<arg value="&quot;"/>
</exec>
<exec executable="bash" osfamily="unix" dir="${temp.cwd}" failonerror="${failonerror}" spawn="@{spawn}"
taskname="${script.base}">
<env key="ES_JAVA_OPTS" value="-Xms512m -Xmx512m"/>
<arg value="@{script}"/>
<nested/>
</exec>
</sequential>
</macrodef>
<!-- extracts PID from file -->
<macrodef name="extract-pid">
<attribute name="file"/>
<attribute name="property"/>
<sequential>
<loadfile srcFile="@{file}" property="@{property}">
<filterchain>
<striplinebreaks/>
</filterchain>
</loadfile>
</sequential>
</macrodef>
<!-- applies transformations to src and stores in dst -->
<macrodef name="filter-property">
<attribute name="src"/>
<attribute name="dest"/>
<element name="chain"/>
<sequential>
<loadresource property="@{dest}">
<propertyresource name="@{src}"/>
<filterchain>
<tokenfilter>
<chain/>
</tokenfilter>
</filterchain>
</loadresource>
</sequential>
</macrodef>
<!-- waits for elasticsearch to start -->
<macrodef name="waitfor-elasticsearch">
<attribute name="port"/>
<attribute name="timeoutproperty"/>
<sequential>
<echo>Waiting for elasticsearch to become available on port @{port}...</echo>
<waitfor maxwait="30" maxwaitunit="second"
checkevery="500" checkeveryunit="millisecond"
timeoutproperty="@{timeoutproperty}">
<http url="http://localhost:@{port}"/>
</waitfor>
</sequential>
</macrodef>
<scriptdef name="isGreater" language="javascript">
<attribute name="v1"/>
<attribute name="v2"/>
<![CDATA[
var i, l, d, s = false;
a = attributes.get("v1").split('.');
b = attributes.get("v2").split('.');
l = Math.min(a.length, b.length);
for (i=0; i<l; i++) {
d = parseInt(a[i], 10) - parseInt(b[i], 10);
if (d !== 0) {
project.setProperty("compare-result", d > 0);
s = true;
break;
}
}
if(!s){
d = a.length - b.length;
project.setProperty("compare-result", d >= 0);
}
]]>
</scriptdef>
<!-- start elasticsearch and wait until its ready -->
<macrodef name="startup-elasticsearch">
<attribute name="home"
default="${project.build.directory}/integration-tests/run/elasticsearch-${elasticsearch.version}"/>
<attribute name="spawn" default="true"/>
<attribute name="es.cluster.name" default="${integ.cluster.name}"/>
<attribute name="es.http.port" default="${integ.http.port}"/>
<attribute name="es.transport.tcp.port" default="${integ.transport.port}"/>
<attribute name="es.pidfile" default="${integ.pidfile}"/>
<element name="additional-args" optional="true"/>
<sequential>
<!-- make sure no elasticsearch instance is currently running and listening on the port we need -->
<fail message="This test expects port @{es.http.port} to be free but an elasticsearch instance is already running and listening on that port.
Maybe the last test run did not manage to shut down the node correctly?
You must kill it before tests can run.">
<condition>
<socket server="localhost" port="@{es.http.port}"/>
</condition>
</fail>
<!-- run bin/elasticsearch with args -->
<echo>Starting up external cluster...</echo>
<isGreater v1="${elasticsearch.version}" v2="5.0.0"/>
<echo if:true="${compare-result}">running Elasticsearch 5.0.0 or superior</echo>
<echo unless:true="${compare-result}">running Elasticsearch &lt; 5.0.0</echo>
<run-script script="@{home}/bin/elasticsearch"
spawn="@{spawn}">
<nested>
<arg value="-Des.pidfile=@{es.pidfile}" unless:true="${compare-result}"/>
<arg value="-Des.cluster.name=@{es.cluster.name}" unless:true="${compare-result}"/>
<arg value="-Des.http.port=@{es.http.port}" unless:true="${compare-result}"/>
<arg value="-Des.transport.tcp.port=@{es.transport.tcp.port}" unless:true="${compare-result}"/>
<arg value="-Des.network.host=127.0.0.1" unless:true="${compare-result}"/>
<arg value="-Epidfile=@{es.pidfile}" if:true="${compare-result}"/>
<arg value="-Ecluster.name=@{es.cluster.name}" if:true="${compare-result}"/>
<arg value="-Ehttp.port=@{es.http.port}" if:true="${compare-result}"/>
<arg value="-Etransport.tcp.port=@{es.transport.tcp.port}" if:true="${compare-result}"/>
<arg value="-Enetwork.host=127.0.0.1" if:true="${compare-result}"/>
<additional-args/>
</nested>
</run-script>
<!-- wait for startup -->
<local name="failed.to.start"/>
<waitfor-elasticsearch port="@{es.http.port}"
timeoutproperty="failed.to.start"/>
<!-- best effort, print console log. useful if it fails especially -->
<local name="log.contents"/>
<loadfile srcFile="@{home}/logs/@{es.cluster.name}.log"
property="log.contents"
failonerror="false"/>
<echo message="${log.contents}" taskname="elasticsearch"/>
<fail message="ES instance did not start" if="failed.to.start"/>
<local name="integ.pid"/>
<extract-pid file="@{es.pidfile}" property="integ.pid"/>
<echo>External node started PID ${integ.pid}</echo>
</sequential>
</macrodef>
<macrodef name="stop-node">
<attribute name="es.pidfile" default="${integ.pidfile}"/>
<sequential>
<local name="integ.pid"/>
<extract-pid file="@{es.pidfile}" property="integ.pid"/>
<echo>Shutting down external node PID ${integ.pid}</echo>
<!-- verify with jps that this actually is the correct pid.
See if we can find the line "pid org.elasticsearch.bootstrap.Elasticsearch" in the output of jps -l.-->
<local name="jps.pidline"/>
<local name="jps.executable"/>
<local name="environment"/>
<property environment="environment"/>
<property name="jps.executable" location="${environment.JAVA_HOME}/bin/jps"/>
<exec executable="${jps.executable}" failonerror="true">
<arg value="-l"/>
<redirector outputproperty="jps.pidline">
<outputfilterchain>
<linecontains>
<contains value="${integ.pid} org.elasticsearch.bootstrap.Elasticsearch"/>
</linecontains>
</outputfilterchain>
</redirector>
</exec>
<fail
message="pid file at @{es.pidfile} is ${integ.pid} but jps -l did not report any process with org.elasticsearch.bootstrap.Elasticsearch and this pid.
Did you run mvn clean? Maybe an old pid file is still lying around.">
<condition>
<equals arg1="${jps.pidline}" arg2=""/>
</condition>
</fail>
<exec executable="taskkill" failonerror="true" osfamily="winnt">
<arg value="/F"/>
<arg value="/PID"/>
<arg value="${integ.pid}"/>
</exec>
<exec executable="kill" failonerror="true" osfamily="unix">
<arg value="-9"/>
<arg value="${integ.pid}"/>
</exec>
<delete file="@{es.pidfile}"/>
</sequential>
</macrodef>
<target name="stop-external-cluster" if="integ.pidfile.exists">
<stop-node/>
</target>
<target name="setup-workspace" depends="stop-external-cluster">
<sequential>
<delete dir="${project.build.directory}/integration-tests/run"/>
<unzip src="${project.build.directory}/integration-tests/binaries/elasticsearch-${elasticsearch.version}.zip"
dest="${project.build.directory}/integration-tests/run"/>
</sequential>
</target>
<target name="start-external-cluster" depends="setup-workspace">
<install-plugin name="ingest-attachment"
file="${project.build.directory}/integration-tests/binaries/ingest-attachment-${elasticsearch.version}.zip"/>
<startup-elasticsearch/>
</target>
<!-- unzip integ test artifact, install plugin, then start ES -->
<target name="start-external-cluster-with-plugin" depends="setup-workspace">
<install-plugin name="${project.artifactId}"
file="${project.build.directory}/releases/${project.artifactId}-${project.version}.zip"/>
<startup-elasticsearch/>
</target>
<!-- installs a plugin into elasticsearch -->
<macrodef name="install-plugin">
<attribute name="home"
default="${project.build.directory}/integration-tests/run/elasticsearch-${elasticsearch.version}"/>
<attribute name="name"/>
<attribute name="file"/>
<sequential>
<local name="url"/>
<makeurl property="url" file="@{file}"/>
<isGreater v1="${elasticsearch.version}" v2="5.0.0"/>
<property name="commandline" value="@{home}/bin/plugin" unless:true="${compare-result}"/>
<property name="commandline" value="@{home}/bin/elasticsearch-plugin" if:true="${compare-result}"/>
<!-- install plugin -->
<echo>Installing plugin @{name}...</echo>
<run-script script="${commandline}">
<nested>
<arg value="install"/>
<arg value="${url}"/>
</nested>
</run-script>
<fail message="did not find plugin installed as @{name}">
<condition>
<not>
<resourceexists>
<file file="@{home}/plugins/@{name}"/>
</resourceexists>
</not>
</condition>
</fail>
</sequential>
</macrodef>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment