blob: 0dc388891466e4cc59149cc67b75ec1d90cfc813 [file] [log] [blame]
Thomas LEVEIL58695bb2015-09-04 21:51:10 +00001#!/usr/bin/env bats
2
3SUT_IMAGE=bats-jenkins
4SUT_CONTAINER=bats-jenkins
5
Carlos Sanchez67055a22016-06-27 17:08:15 +02006load 'test_helper/bats-support/load'
7load 'test_helper/bats-assert/load'
Thomas LEVEIL58695bb2015-09-04 21:51:10 +00008load test_helpers
9
10@test "build image" {
Carlos Sanchez5fc9cd02016-02-28 15:25:18 +010011 cd $BATS_TEST_DIRNAME/..
12 docker build -t $SUT_IMAGE .
Thomas LEVEIL58695bb2015-09-04 21:51:10 +000013}
14
15@test "clean test containers" {
Carlos Sanchez5fc9cd02016-02-28 15:25:18 +010016 cleanup $SUT_CONTAINER
17}
18
19@test "test multiple JENKINS_OPTS" {
20 # running --help --version should return the version, not the help
Carlos Sanchez0c987822016-03-16 08:42:03 +010021 local version=$(grep 'ENV JENKINS_VERSION' Dockerfile | sed -e 's/.*:-\(.*\)}/\1/')
Carlos Sanchez2d68b192016-04-20 13:26:59 +020022 # need the last line of output
23 assert "${version}" docker run --rm -ti -e JENKINS_OPTS="--help --version" --name $SUT_CONTAINER -P $SUT_IMAGE | tail -n 1
Carlos Sanchez5fc9cd02016-02-28 15:25:18 +010024}
25
26@test "test jenkins arguments" {
27 # running --help --version should return the version, not the help
Carlos Sanchez0c987822016-03-16 08:42:03 +010028 local version=$(grep 'ENV JENKINS_VERSION' Dockerfile | sed -e 's/.*:-\(.*\)}/\1/')
Carlos Sanchez2d68b192016-04-20 13:26:59 +020029 # need the last line of output
30 assert "${version}" docker run --rm -ti --name $SUT_CONTAINER -P $SUT_IMAGE --help --version | tail -n 1
Thomas LEVEIL58695bb2015-09-04 21:51:10 +000031}
32
33@test "create test container" {
Carlos Sanchezc24ef822016-02-29 12:10:31 +010034 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
Thomas LEVEIL58695bb2015-09-04 21:51:10 +000035}
36
37@test "test container is running" {
Carlos Sanchez5fc9cd02016-02-28 15:25:18 +010038 sleep 1 # give time to eventually fail to initialize
39 retry 3 1 assert "true" docker inspect -f {{.State.Running}} $SUT_CONTAINER
Thomas LEVEIL58695bb2015-09-04 21:51:10 +000040}
41
42@test "Jenkins is initialized" {
43 retry 30 5 test_url /api/json
44}
Carlos Sanchez5fc9cd02016-02-28 15:25:18 +010045
Carlos Sanchezc24ef822016-02-29 12:10:31 +010046@test "JAVA_OPTS are set" {
47 local sed_expr='s/<wbr>//g;s/<td class="pane">.*<\/td><td class.*normal">//g;s/<t.>//g;s/<\/t.>//g'
48 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;;' \
Carlos Sanchez5a51d0a2016-04-21 13:50:25 +020049 bash -c "curl -fsSL --user \"admin:$(get_jenkins_password)\" $(get_jenkins_url)/systemInfo | sed 's/<\/tr>/<\/tr>\'$'\n/g' | grep '<td class=\"pane\">hudson.model.DirectoryBrowserSupport.CSP</td>' | sed -e '${sed_expr}'"
Carlos Sanchezc24ef822016-02-29 12:10:31 +010050 assert 'Europe/Madrid' \
Carlos Sanchez5a51d0a2016-04-21 13:50:25 +020051 bash -c "curl -fsSL --user \"admin:$(get_jenkins_password)\" $(get_jenkins_url)/systemInfo | sed 's/<\/tr>/<\/tr>\'$'\n/g' | grep '<td class=\"pane\">user.timezone</td>' | sed -e '${sed_expr}'"
Carlos Sanchez5fc9cd02016-02-28 15:25:18 +010052}
Carlos Sanchez177b25f2016-02-29 13:53:22 +010053
Carlos Sanchez67055a22016-06-27 17:08:15 +020054@test "plugins are installed with plugins.sh" {
Carlos Sanchez45a62852016-06-27 23:28:48 +020055 run docker build -t $SUT_IMAGE-plugins $BATS_TEST_DIRNAME/plugins
Carlos Sanchez67055a22016-06-27 17:08:15 +020056 assert_success
57 # replace DOS line endings \r\n
Carlos Sanchezd01bd512016-07-11 15:42:09 +020058 run bash -c "docker run -ti --rm $SUT_IMAGE-plugins ls --color=never -1 /var/jenkins_home/plugins | tr -d '\r'"
Carlos Sanchez67055a22016-06-27 17:08:15 +020059 assert_success
Carlos Sanchez114dc702016-06-27 23:39:34 +020060 refute_line 'maven-plugin.jpi'
61 refute_line 'maven-plugin.jpi.pinned'
Carlos Sanchez45a62852016-06-27 23:28:48 +020062 assert_line 'ant.jpi'
63 assert_line 'ant.jpi.pinned'
Carlos Sanchez67055a22016-06-27 17:08:15 +020064}
65
66@test "plugins are installed with install-plugins.sh" {
67 run docker build -t $SUT_IMAGE-install-plugins $BATS_TEST_DIRNAME/install-plugins
68 assert_success
69 # replace DOS line endings \r\n
Carlos Sanchezd01bd512016-07-11 15:42:09 +020070 run bash -c "docker run -ti --rm $SUT_IMAGE-install-plugins ls --color=never -1 /var/jenkins_home/plugins | tr -d '\r'"
Carlos Sanchez67055a22016-06-27 17:08:15 +020071 assert_success
72 assert_line 'maven-plugin.jpi'
73 assert_line 'maven-plugin.jpi.pinned'
Carlos Sanchez45a62852016-06-27 23:28:48 +020074 assert_line 'ant.jpi'
75 assert_line 'ant.jpi.pinned'
Carlos Sanchez82272772016-07-18 09:58:29 +020076 assert_line 'credentials.jpi'
77 assert_line 'credentials.jpi.pinned'
78 assert_line 'mesos.jpi'
79 assert_line 'mesos.jpi.pinned'
Carlos Sanchez3e8e9482016-04-20 13:04:48 +020080}
81
Carlos Sanchez177b25f2016-02-29 13:53:22 +010082@test "clean test containers" {
83 cleanup $SUT_CONTAINER
84}
Vincent Latombebb8f8c92016-07-13 18:18:14 +020085
86@test "plugins are getting upgraded but not downgraded" {
87 run docker build -t $SUT_IMAGE-install-plugins $BATS_TEST_DIRNAME/install-plugins
88 assert_success
89 local work; work="$BATS_TEST_DIRNAME/upgrade-plugins/work"
90 # Image contains maven-plugin 2.7.1 and ant-plugin 1.3
91 run bash -c "docker run -ti -v $work:/var/jenkins_home --rm $SUT_IMAGE-install-plugins true"
92 assert_success
93 run bash -c "unzip -p $work/plugins/maven-plugin.jpi META-INF/MANIFEST.MF | tr -d '\r'"
94 assert_line 'Plugin-Version: 2.7.1'
95 run bash -c "unzip -p $work/plugins/ant.jpi META-INF/MANIFEST.MF | tr -d '\r'"
96 assert_line 'Plugin-Version: 1.3'
97 run docker build -t $SUT_IMAGE-upgrade-plugins $BATS_TEST_DIRNAME/upgrade-plugins
98 assert_success
99 # Images contains maven-plugin 2.13 and ant-plugin 1.2
100 run bash -c "docker run -ti -v $work:/var/jenkins_home --rm $SUT_IMAGE-upgrade-plugins true"
101 assert_success
102 run bash -c "unzip -p $work/plugins/maven-plugin.jpi META-INF/MANIFEST.MF | tr -d '\r'"
103 assert_success
104 # Should be updated
105 assert_line 'Plugin-Version: 2.13'
106 run bash -c "unzip -p $work/plugins/ant.jpi META-INF/MANIFEST.MF | tr -d '\r'"
107 # 1.2 is older than the existing 1.3, so keep 1.3
108 assert_line 'Plugin-Version: 1.3'
109}
110
111@test "clean work directory" {
112 run bash -c "rm -rf $BATS_TEST_DIRNAME/upgrade-plugins/work"
113}
114
115@test "do not upgrade if plugin has been manually updated" {
116 run docker build -t $SUT_IMAGE-install-plugins $BATS_TEST_DIRNAME/install-plugins
117 assert_success
118 local work; work="$BATS_TEST_DIRNAME/upgrade-plugins/work"
119 # Image contains maven-plugin 2.7.1 and ant-plugin 1.3
120 run bash -c "docker run -ti -v $work:/var/jenkins_home --rm $SUT_IMAGE-install-plugins curl --connect-timeout 5 --retry 5 --retry-delay 0 --retry-max-time 60 -s -f -L https://updates.jenkins.io/download/plugins/maven-plugin/2.12.1/maven-plugin.hpi -o /var/jenkins_home/plugins/maven-plugin.jpi"
121 assert_success
122 run bash -c "unzip -p $work/plugins/maven-plugin.jpi META-INF/MANIFEST.MF | tr -d '\r'"
123 assert_line 'Plugin-Version: 2.12.1'
124 run docker build -t $SUT_IMAGE-upgrade-plugins $BATS_TEST_DIRNAME/upgrade-plugins
125 assert_success
126 # Images contains maven-plugin 2.13 and ant-plugin 1.2
127 run bash -c "docker run -ti -v $work:/var/jenkins_home --rm $SUT_IMAGE-upgrade-plugins true"
128 assert_success
129 run bash -c "unzip -p $work/plugins/maven-plugin.jpi META-INF/MANIFEST.MF | tr -d '\r'"
130 assert_success
131 # Shouldn't be updated
132 refute_line 'Plugin-Version: 2.13'
133}
134
135@test "clean work directory" {
136 run bash -c "rm -rf $BATS_TEST_DIRNAME/upgrade-plugins/work"
137}