Skip to content

Commit edfa79b

Browse files
lsimonswilderrodrigues
authored andcommitted
junit report output for vagrant systemvm tests
1 parent 3811b8d commit edfa79b

3 files changed

Lines changed: 318 additions & 6 deletions

File tree

tools/vagrant/systemvm/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,6 @@ boxes/*
4747
# Systemvm ISO
4848
systemvm.iso
4949
iso/*
50+
51+
rspec.xml
52+
vagrant_ssh_config

tools/vagrant/systemvm/bootstrap.sh

100644100755
Lines changed: 99 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,107 @@ set -x
66
# script invoked by Test-Kitchen shell provisioner to further
77
# customize the VM prior to running tests
88

9-
# for internet access
10-
cat >>/etc/network/interfaces <<END
9+
function setup_networking() {
10+
# for internet access
11+
if [[ ! `grep eth1 /etc/network/interfaces` ]]; then
12+
cat >>/etc/network/interfaces <<END
1113
1214
iface eth1 inet dhcp
1315
auto eth1
1416
END
15-
ifup eth1
17+
fi
18+
ifup eth1
19+
}
1620

17-
# /opt/chef/embedded/bin/gem, etc, expected by test-kitchen
18-
apt-get install curl
19-
curl -L https://www.opscode.com/chef/install.sh | bash
21+
function install_packages() {
22+
export DEBIAN_FRONTEND=noninteractive
23+
export DEBIAN_PRIORITY=critical
24+
25+
local apt_get="apt-get -q -y --force-yes"
26+
27+
${apt_get} update
28+
29+
if [[ ! `which curl` ]]; then
30+
${apt_get} install curl
31+
fi
32+
if [[ ! `which patch` ]]; then
33+
${apt_get} install patch
34+
fi
35+
if [[ ! `which git` ]]; then
36+
${apt_get} install git
37+
fi
38+
}
39+
40+
function install_chef() {
41+
if [[ ! -f '/opt/chef/embedded/bin/gem' ]]; then
42+
curl -L https://www.opscode.com/chef/install.sh | bash
43+
fi
44+
}
45+
function add_junit_reports_to_serverspec() {
46+
local gem="/opt/chef/embedded/bin/gem"
47+
local gem_install="${gem} install --no-rdoc --no-ri"
48+
49+
${gem_install} rspec rspec_junit_formatter
50+
51+
if [[ ! -d 'serverspec' ]]; then
52+
git clone https://github.com/serverspec/serverspec.git
53+
(cd serverspec; git submodule update --init --recursive)
54+
fi
55+
cd serverspec
56+
git reset --hard
57+
cat >serverspec.patch <<END
58+
diff -u serverspec.gemspec serverspec-spec3.gemspec
59+
--- serverspec.gemspec
60+
+++ serverspec.gemspec
61+
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
62+
spec.require_paths = ["lib"]
63+
64+
spec.add_runtime_dependency "net-ssh"
65+
- spec.add_runtime_dependency "rspec", "~> 2.99"
66+
+ spec.add_runtime_dependency "rspec", [">= 2.99", '< 4.0']
67+
spec.add_runtime_dependency "rspec-its"
68+
spec.add_runtime_dependency "highline"
69+
spec.add_runtime_dependency "specinfra", "~> 1.22"
70+
END
71+
patch -p0 <serverspec.patch
72+
73+
${gem} build serverspec.gemspec
74+
${gem_install} serverspec-*.gem
75+
cd ..
76+
77+
78+
if [[ ! -d 'busser-serverspec' ]]; then
79+
git clone https://github.com/test-kitchen/busser-serverspec.git
80+
fi
81+
cd busser-serverspec
82+
git reset --hard
83+
cat >busser-serverspec.patch <<END
84+
diff -u lib/busser/serverspec/runner.rb lib/busser/serverspec/runner-spec3.rb
85+
--- lib/busser/serverspec/runner.rb
86+
+++ lib/busser/serverspec/runner.rb
87+
@@ -42,7 +42,7 @@ RSpec::Core::RakeTask.new(:spec) do |t|
88+
end
89+
90+
t.rspec_path = rspec_bin if rspec_bin
91+
- t.rspec_opts = ['--color', '--format documentation']
92+
+ t.rspec_opts = ['--color', '--format documentation', '--format RspecJunitFormatter', '--out /tmp/rspec.xml']
93+
t.ruby_opts = "-I#{base_path}"
94+
t.pattern = "#{base_path}/**/*_spec.rb"
95+
end
96+
END
97+
98+
patch -p0 <busser-serverspec.patch
99+
${gem} build busser-serverspec.gemspec
100+
${gem_install} busser-serverspec-*.gem
101+
cd ..
102+
}
103+
104+
function main() {
105+
setup_networking
106+
install_packages
107+
install_chef
108+
add_junit_reports_to_serverspec
109+
}
110+
111+
# we only run main() if not source-d
112+
return 2>/dev/null || main

tools/vagrant/systemvm/test.sh

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
#!/bin/bash -l
2+
# note: the -l is needed here for bash to always make a login shell and load rvm if it hasn't been loaded
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one
5+
# or more contributor license agreements. See the NOTICE file
6+
# distributed with this work for additional information
7+
# regarding copyright ownership. The ASF licenses this file
8+
# to you under the Apache License, Version 2.0 (the
9+
# "License"); you may not use this file except in compliance
10+
# with the License. You may obtain a copy of the License at
11+
#
12+
# http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing,
15+
# software distributed under the License is distributed on an
16+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
# KIND, either express or implied. See the License for the
18+
# specific language governing permissions and limitations
19+
# under the License.
20+
21+
# build script which wraps around test-kitchen to test the systemvm
22+
23+
function usage() {
24+
cat <<END
25+
Usage:
26+
./build.sh
27+
END
28+
exit 0
29+
}
30+
echo $@ | grep help >/dev/null && usage
31+
echo $@ | grep '\-h' >/dev/null && usage
32+
33+
set -e
34+
35+
###
36+
### Configuration
37+
###
38+
# whether to show DEBUG logs
39+
DEBUG="${DEBUG:-}"
40+
# whether to have other commands trace their actions
41+
TRACE="${TRACE:-0}"
42+
JENKINS_HOME=${JENKINS_HOME:-}
43+
if [[ ! -z "${JENKINS_HOME}" ]]; then
44+
DEBUG=1
45+
fi
46+
47+
kitchen_args=
48+
if [[ "${DEBUG}" == "1" ]]; then
49+
kitchen_args="-l debug"
50+
fi
51+
52+
# optional (jenkins) build number tag to put into the image filename
53+
VPC_IP="${VPC_IP:-192.168.56.30}"
54+
export VPC_IP
55+
56+
###
57+
### Generic helper functions
58+
###
59+
60+
# how to tell sed to use extended regular expressions
61+
os=`uname`
62+
sed_regex_option="-E"
63+
if [ "${os}" == "Linux" ]; then
64+
sed_regex_option="-r"
65+
fi
66+
67+
# logging support
68+
if [[ "${DEBUG}" == "1" ]]; then
69+
set -x
70+
fi
71+
72+
function log() {
73+
local level=${1?}
74+
shift
75+
76+
if [[ "${DEBUG}" != "1" && "${level}" == "DEBUG" ]]; then
77+
return
78+
fi
79+
80+
local code=
81+
local line="[$(date '+%F %T')] $level: $*"
82+
if [ -t 2 ]
83+
then
84+
case "$level" in
85+
INFO) code=36 ;;
86+
DEBUG) code=30 ;;
87+
WARN) code=33 ;;
88+
ERROR) code=31 ;;
89+
*) code=37 ;;
90+
esac
91+
echo -e "\033[${code}m${line}\033[0m"
92+
else
93+
echo "$line"
94+
fi >&2
95+
}
96+
97+
function error() {
98+
log ERROR $@
99+
exit 1
100+
}
101+
102+
# cleanup code support
103+
declare -a on_exit_items
104+
105+
function on_exit() {
106+
for (( i=${#on_exit_items[@]}-1 ; i>=0 ; i-- )) ; do
107+
sleep 2
108+
log DEBUG "on_exit: ${on_exit_items[i]}"
109+
eval ${on_exit_items[i]}
110+
done
111+
}
112+
113+
function add_on_exit() {
114+
local n=${#on_exit_items[*]}
115+
on_exit_items[${n}]="$*"
116+
if [ ${n} -eq 0 ]; then
117+
log DEBUG "Setting trap"
118+
trap on_exit EXIT
119+
fi
120+
}
121+
122+
# retry code support
123+
function retry() {
124+
local times=$1
125+
shift
126+
local count=0
127+
while [ ${count} -lt ${times} ]; do
128+
"$@" && break
129+
count=$(( $count + 1 ))
130+
sleep ${count}
131+
done
132+
133+
if [ ${count} -eq ${times} ]; then
134+
error "Failed ${times} times: $@"
135+
fi
136+
}
137+
138+
###
139+
### Script logic
140+
###
141+
142+
function setup_ruby() {
143+
local bundle_args=
144+
if [[ ! -z "${JENKINS_HOME}" ]]; then
145+
# inspired by https://github.com/CloudBees-community/rubyci-clickstart/blob/master/bin/run-ci
146+
# also see https://rvm.io/integration/jenkins
147+
# .rvmrc won't get trusted/auto-loaded by jenkins by default
148+
export VAGRANT_HOME=$HOME/.vagrant.d-release-cloudstack
149+
rvm use ruby-1.9.3@vagrant-release-cloudstack --create
150+
# do not use --deployment since that requires Gemfile.lock...and we prefer an up-to-date veewee
151+
bundle_args="--path vendor/bundle"
152+
fi
153+
bundle check || bundle install ${bundle_args}
154+
}
155+
156+
function prepare() {
157+
log INFO "preparing for build"
158+
setup_ruby
159+
rm -f systemvm.iso
160+
}
161+
162+
function box_update() {
163+
log INFO "invoking vagrant box update"
164+
vagrant box update
165+
log INFO "vagrant box update complete"
166+
}
167+
168+
function converge_kitchen() {
169+
log INFO "invoking test-kitchen converge"
170+
kitchen create ${kitchen_args}
171+
kitchen converge ${kitchen_args}
172+
log INFO "test-kitchen complete"
173+
}
174+
175+
function verify_kitchen() {
176+
log INFO "invoking test-kitchen verify"
177+
178+
kitchen verify ${kitchen_args}
179+
180+
# re-run busser test with patched serverspec gem to get a rspec.xml
181+
kitchen exec ${kitchen_args} -c '
182+
BUSSER_ROOT="/tmp/busser" GEM_HOME="/tmp/busser/gems" GEM_PATH="/tmp/busser/gems" GEM_CACHE="/tmp/busser/gems/cache"
183+
export BUSSER_ROOT GEM_HOME GEM_PATH GEM_CACHE
184+
/tmp/kitchen/bootstrap.sh
185+
sudo -E /tmp/busser/bin/busser test
186+
'
187+
188+
# ssh to machine ourselves to avoid kitchen output
189+
(cd .kitchen/kitchen-vagrant/default-systemvm; vagrant ssh-config) > vagrant_ssh_config
190+
add_on_exit rm -f vagrant_ssh_config
191+
scp -F vagrant_ssh_config default:/tmp/rspec.xml rspec.xml
192+
log INFO "test results in rspec.xml"
193+
194+
log INFO "test-kitchen complete"
195+
}
196+
197+
function destroy_kitchen() {
198+
log INFO "invoking test-kitchen destroy"
199+
kitchen destroy ${kitchen_args}
200+
log INFO "test-kitchen destroy complete"
201+
}
202+
###
203+
### Main invocation
204+
###
205+
206+
function main() {
207+
prepare
208+
box_update
209+
add_on_exit destroy_kitchen
210+
converge_kitchen
211+
verify_kitchen
212+
add_on_exit log INFO "BUILD SUCCESSFUL"
213+
}
214+
215+
# we only run main() if not source-d
216+
return 2>/dev/null || main

0 commit comments

Comments
 (0)