Merge pull request #212 from jimzucker/currentPlugins

Reference #206 - Added example to get a list of plugins in the required format
diff --git a/Dockerfile b/Dockerfile
index beb7f1d..3221c9a 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -5,10 +5,16 @@
 ENV JENKINS_HOME /var/jenkins_home
 ENV JENKINS_SLAVE_AGENT_PORT 50000
 
+ARG user=jenkins
+ARG group=jenkins
+ARG uid=1000
+ARG gid=1000
+
 # Jenkins is run with user `jenkins`, uid = 1000
 # If you bind mount a volume from the host or a data container, 
 # ensure you use the same uid
-RUN useradd -d "$JENKINS_HOME" -u 1000 -m -s /bin/bash jenkins
+RUN groupadd -g ${gid} ${group} \
+    && useradd -d "$JENKINS_HOME" -u ${uid} -g ${gid} -m -s /bin/bash ${user}
 
 # Jenkins home directory is a volume, so configuration and build history 
 # can be persisted and survive image upgrades
@@ -22,22 +28,24 @@
 ENV TINI_SHA 066ad710107dc7ee05d3aa6e4974f01dc98f3888
 
 # Use tini as subreaper in Docker container to adopt zombie processes 
-RUN curl -fL https://github.com/krallin/tini/releases/download/v0.5.0/tini-static -o /bin/tini && chmod +x /bin/tini \
+RUN curl -fsSL https://github.com/krallin/tini/releases/download/v0.5.0/tini-static -o /bin/tini && chmod +x /bin/tini \
   && echo "$TINI_SHA /bin/tini" | sha1sum -c -
 
 COPY init.groovy /usr/share/jenkins/ref/init.groovy.d/tcp-slave-agent-port.groovy
 
-ENV JENKINS_VERSION 1.642.2
-ENV JENKINS_SHA e72e06e64d23eefb13090459f517b0697aad7be0
+ARG JENKINS_VERSION
+ENV JENKINS_VERSION ${JENKINS_VERSION:-1.642.4}
+ARG JENKINS_SHA
+ENV JENKINS_SHA ${JENKINS_SHA:-3cb37dde64b1aca9952c7a4f98f3c0b71d02cd8b}
 
 
 # could use ADD but this one does not check Last-Modified header 
 # see https://github.com/docker/docker/issues/8331
-RUN curl -fL http://repo.jenkins-ci.org/public/org/jenkins-ci/main/jenkins-war/${JENKINS_VERSION}/jenkins-war-${JENKINS_VERSION}.war -o /usr/share/jenkins/jenkins.war \
+RUN curl -fsSL http://repo.jenkins-ci.org/public/org/jenkins-ci/main/jenkins-war/${JENKINS_VERSION}/jenkins-war-${JENKINS_VERSION}.war -o /usr/share/jenkins/jenkins.war \
   && echo "$JENKINS_SHA /usr/share/jenkins/jenkins.war" | sha1sum -c -
 
 ENV JENKINS_UC https://updates.jenkins-ci.org
-RUN chown -R jenkins "$JENKINS_HOME" /usr/share/jenkins/ref
+RUN chown -R ${user} "$JENKINS_HOME" /usr/share/jenkins/ref
 
 # for main web interface:
 EXPOSE 8080
@@ -47,7 +55,7 @@
 
 ENV COPY_REFERENCE_FILE_LOG $JENKINS_HOME/copy_reference_file.log
 
-USER jenkins
+USER ${user}
 
 COPY jenkins.sh /usr/local/bin/jenkins.sh
 ENTRYPOINT ["/bin/tini", "--", "/usr/local/bin/jenkins.sh"]
diff --git a/Jenkinsfile b/Jenkinsfile
new file mode 100644
index 0000000..f1f5202
--- /dev/null
+++ b/Jenkinsfile
@@ -0,0 +1,11 @@
+node('docker') {
+  stage 'Checkout'
+  checkout scm
+
+  stage 'Build'
+  docker.build('jenkins')
+
+  stage 'Test'
+  sh "git checkout https://github.com/sstephenson/bats.git"
+  sh "bats/bin/bats tests/tests.bats"
+}
diff --git a/README.md b/README.md
index 4692e6b..21e1e1e 100644
--- a/README.md
+++ b/README.md
@@ -204,6 +204,18 @@
 
 As always - please ensure that you know how to drive docker - especially volume handling!
 
+# Building
+
+Build with the usual
+
+    docker build -t jenkins .
+
+Tests are written using [bats](https://github.com/sstephenson/bats) under the `tests` dir
+
+    bats tests
+
+Bats can be easily installed with `brew install bats` on OS X
+
 # Questions?
 
 Jump on irc.freenode.net and the #jenkins room. Ask!
diff --git a/jenkins.sh b/jenkins.sh
index dd28123..2fa42cd 100755
--- a/jenkins.sh
+++ b/jenkins.sh
@@ -2,7 +2,7 @@
 
 set -e
 
-# Copy files from /usr/share/jenkins/ref into /var/jenkins_home
+# Copy files from /usr/share/jenkins/ref into $JENKINS_HOME
 # So the initial JENKINS-HOME is set with expected content.
 # Don't override, as this is just a reference setup, and use from UI
 # can then change this, upgrade plugins, etc.
@@ -13,15 +13,16 @@
 	rel="${b:23}"
 	dir=$(dirname "${b}")
 	echo " $f -> $rel" >> "$COPY_REFERENCE_FILE_LOG"
-	if [[ ! -e /var/jenkins_home/${rel} || $f = *.override ]]
+	if [[ ! -e $JENKINS_HOME/${rel} || $f = *.override ]]
 	then
 		echo "copy $rel to JENKINS_HOME" >> "$COPY_REFERENCE_FILE_LOG"
-		mkdir -p "/var/jenkins_home/${dir:23}"
-		cp -r "${f}" "/var/jenkins_home/${rel}";
+		mkdir -p "$JENKINS_HOME/${dir:23}"
+		cp -r "${f}" "$JENKINS_HOME/${rel}";
 		# pin plugins on initial copy
-		[[ ${rel} == plugins/*.jpi ]] && touch "/var/jenkins_home/${rel}.pinned"
+		[[ ${rel} == plugins/*.jpi ]] && touch "$JENKINS_HOME/${rel}.pinned"
 	fi;
 }
+: ${JENKINS_HOME:="/var/jenkins_home"}
 export -f copy_reference_file
 touch "${COPY_REFERENCE_FILE_LOG}" || (echo "Can not write to ${COPY_REFERENCE_FILE_LOG}. Wrong volume permissions?" && exit 1)
 echo "--- Copying files at $(date)" >> "$COPY_REFERENCE_FILE_LOG"
@@ -29,7 +30,7 @@
 
 # if `docker run` first argument start with `--` the user is passing jenkins launcher arguments
 if [[ $# -lt 1 ]] || [[ "$1" == "--"* ]]; then
-  exec java $JAVA_OPTS -jar /usr/share/jenkins/jenkins.war $JENKINS_OPTS "$@"
+  eval "exec java $JAVA_OPTS -jar /usr/share/jenkins/jenkins.war $JENKINS_OPTS \"\$@\""
 fi
 
 # As argument is not jenkins, assume user want to run his own process, for sample a `bash` shell to explore this image
diff --git a/tests/test_helpers.bash b/tests/test_helpers.bash
index 4ee7ed9..7a3d891 100644
--- a/tests/test_helpers.bash
+++ b/tests/test_helpers.bash
@@ -11,7 +11,8 @@
     shift
     local actual_output=$("$@")
     if ! [ "$actual_output" = "$expected_output" ]; then
-        echo "expected: \"$expected_output\", actual: \"$actual_output\""
+        echo "expected: \"$expected_output\""
+        echo "actual:   \"$actual_output\""
         false
     fi
 }
@@ -37,7 +38,7 @@
 }
 
 function get_jenkins_url {
-    if [ -z $DOCKER_HOST]; then
+    if [ -z "${DOCKER_HOST}" ]; then
         DOCKER_IP=localhost
     else
         DOCKER_IP=$(echo $DOCKER_HOST | sed -e 's|tcp://\(.*\):[0-9]*|\1|')
diff --git a/tests/tests.bats b/tests/tests.bats
index 1e8ec44..bc42afc 100644
--- a/tests/tests.bats
+++ b/tests/tests.bats
@@ -16,22 +16,22 @@
 
 @test "test multiple JENKINS_OPTS" {
   # running --help --version should return the version, not the help
-  local version=$(grep 'ENV JENKINS_VERSION' Dockerfile | sed -e 's/ENV JENKINS_VERSION //')
+  local version=$(grep 'ENV JENKINS_VERSION' Dockerfile | sed -e 's/.*:-\(.*\)}/\1/')
   # need the last line of output, removing the last char
   local actual_version=$(docker run --rm -ti -e JENKINS_OPTS="--help --version" --name $SUT_CONTAINER -P $SUT_IMAGE | tail -n 1)
-  assert "${version}" echo ${actual_version::-1}
+  assert "${version}" echo "${actual_version::-1}"
 }
 
 @test "test jenkins arguments" {
   # running --help --version should return the version, not the help
-  local version=$(grep 'ENV JENKINS_VERSION' Dockerfile | sed -e 's/ENV JENKINS_VERSION //')
+  local version=$(grep 'ENV JENKINS_VERSION' Dockerfile | sed -e 's/.*:-\(.*\)}/\1/')
   # need the last line of output, removing the last char
   local actual_version=$(docker run --rm -ti --name $SUT_CONTAINER -P $SUT_IMAGE --help --version | tail -n 1)
-  assert "${version}" echo ${actual_version::-1}
+  assert "${version}" echo "${actual_version::-1}"
 }
 
 @test "create test container" {
-    docker run -d --name $SUT_CONTAINER -P $SUT_IMAGE
+    docker run -d -e JAVA_OPTS="-Duser.timezone=Europe/Madrid -Dhudson.model.DirectoryBrowserSupport.CSP=\"default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline';\"" --name $SUT_CONTAINER -P $SUT_IMAGE
 }
 
 @test "test container is running" {
@@ -43,6 +43,14 @@
     retry 30 5 test_url /api/json
 }
 
+@test "JAVA_OPTS are set" {
+    local sed_expr='s/<wbr>//g;s/<td class="pane">.*<\/td><td class.*normal">//g;s/<t.>//g;s/<\/t.>//g'
+    assert 'default-src &#039;self&#039;; script-src &#039;self&#039; &#039;unsafe-inline&#039; &#039;unsafe-eval&#039;; style-src &#039;self&#039; &#039;unsafe-inline&#039;;' \
+      bash -c "curl -fsSL $(get_jenkins_url)/systemInfo | sed 's/<\/tr>/<\/tr>\'$'\n/g' | grep '<td class=\"pane\">hudson.model.DirectoryBrowserSupport.CSP</td>' | sed -e '${sed_expr}'"
+    assert 'Europe/Madrid' \
+      bash -c "curl -fsSL $(get_jenkins_url)/systemInfo | sed 's/<\/tr>/<\/tr>\'$'\n/g' | grep '<td class=\"pane\">user.timezone</td>' | sed -e '${sed_expr}'"
+}
+
 @test "clean test containers" {
     cleanup $SUT_CONTAINER
 }