[test] Packaging test for filesystem scripts

Adds a tests for loading scripts from the filesystem for search templates
and for search filters.

Closes #13184
This commit is contained in:
Nik Everett 2015-09-01 22:12:18 -04:00
parent 29165c59f5
commit 8d02efd088
12 changed files with 209 additions and 115 deletions

View File

@ -34,6 +34,8 @@ load tar
setup() {
skip_not_tar_gz
export ESHOME=/tmp/elasticsearch
export_elasticsearch_paths
}
##################################
@ -74,6 +76,11 @@ setup() {
# Check that Elasticsearch is working
##################################
@test "[TAR] test elasticsearch" {
# Install scripts used to test script filters and search templates before
# starting Elasticsearch so we don't have to wait for elasticsearch to scan for
# them.
install_elasticsearch_test_scripts
start_elasticsearch_service
run_elasticsearch_tests

View File

@ -31,10 +31,12 @@
# Load test utilities
load packaging_test_utils
load os_package
# Cleans everything for the 1st execution
setup() {
skip_not_dpkg
export_elasticsearch_paths
}
##################################
@ -79,8 +81,11 @@ setup() {
}
@test "[DEB] test elasticsearch" {
# Install scripts used to test script filters and search templates before
# starting Elasticsearch so we don't have to wait for elasticsearch to scan for
# them.
install_elasticsearch_test_scripts
start_elasticsearch_service
run_elasticsearch_tests
}
@ -133,6 +138,8 @@ setup() {
}
@test "[DEB] purge package" {
# User installed scripts aren't removed so we'll just get them ourselves
rm -rf $ESSCRIPTS
dpkg --purge 'elasticsearch'
}

View File

@ -30,10 +30,12 @@
# Load test utilities
load packaging_test_utils
load os_package
# Cleans everything for the 1st execution
setup() {
skip_not_rpm
export_elasticsearch_paths
}
##################################
@ -74,12 +76,17 @@ setup() {
}
@test "[RPM] test elasticsearch" {
# Install scripts used to test script filters and search templates before
# starting Elasticsearch so we don't have to wait for elasticsearch to scan for
# them.
install_elasticsearch_test_scripts
start_elasticsearch_service
run_elasticsearch_tests
}
@test "[RPM] remove package" {
# User installed scripts aren't removed so we'll just get them ourselves
rm -rf $ESSCRIPTS
rpm -e 'elasticsearch'
}

View File

@ -30,11 +30,13 @@
# Load test utilities
load packaging_test_utils
load os_package
# Cleans everything for the 1st execution
setup() {
skip_not_systemd
skip_not_dpkg_or_rpm
export_elasticsearch_paths
}
@test "[SYSTEMD] install elasticsearch" {
@ -61,10 +63,12 @@ setup() {
}
@test "[SYSTEMD] start" {
# Install scripts used to test script filters and search templates before
# starting Elasticsearch so we don't have to wait for elasticsearch to scan for
# them.
install_elasticsearch_test_scripts
systemctl start elasticsearch.service
wait_for_elasticsearch_status
assert_file_exist "/var/run/elasticsearch/elasticsearch.pid"
}

View File

@ -30,11 +30,13 @@
# Load test utilities
load packaging_test_utils
load os_package
# Cleans everything for the 1st execution
setup() {
skip_not_sysvinit
skip_not_dpkg_or_rpm
export_elasticsearch_paths
}
@test "[INIT.D] remove any leftover configuration to start elasticsearch on restart" {
@ -64,10 +66,12 @@ setup() {
}
@test "[INIT.D] start" {
# Install scripts used to test script filters and search templates before
# starting Elasticsearch so we don't have to wait for elasticsearch to scan for
# them.
install_elasticsearch_test_scripts
service elasticsearch start
wait_for_elasticsearch_status
assert_file_exist "/var/run/elasticsearch/elasticsearch.pid"
}

View File

@ -32,6 +32,7 @@
# Load test utilities
load packaging_test_utils
load os_package
# Cleans everything for the 1st execution
setup() {
@ -50,7 +51,7 @@ setup() {
@test "[UPGRADE] check elasticsearch version is old version" {
curl -s localhost:9200 | grep \"number\"\ :\ \"$(cat upgrade_from_version)\" || {
echo "Installed an unexpected version:"
curl localhost:9200
curl -s localhost:9200
false
}
}

View File

@ -0,0 +1 @@
_source['title'] == 'Elasticsearch - The Definitive Guide'

View File

@ -0,0 +1,7 @@
{
"query": {
"script": {
"script_file": "is_guide"
}
}
}

View File

@ -0,0 +1,109 @@
#!/bin/sh
# This file contains some utilities to test the elasticsearch scripts with
# the .deb/.rpm packages.
# WARNING: This testing file must be executed as root and can
# dramatically change your system. It removes the 'elasticsearch'
# user/group and also many directories. Do not execute this file
# unless you know exactly what you are doing.
# Licensed to Elasticsearch under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch 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.
# Export some useful paths.
export_elasticsearch_paths() {
export ESHOME="/usr/share/elasticsearch"
export ESPLUGINS="$ESHOME/plugins"
export ESCONFIG="/etc/elasticsearch"
export ESSCRIPTS="$ESCONFIG/scripts"
export ESDATA="/var/lib/elasticsearch"
export ESLOG="/var/log/elasticsearch"
export ESPIDDIR="/var/run/elasticsearch"
}
# Install the rpm or deb package.
# -u upgrade rather than install. This only matters for rpm.
# -v the version to upgrade to. Defaults to the version under test.
install_package() {
local version=$(cat version)
local rpmCommand='-i'
while getopts ":uv:" opt; do
case $opt in
u)
rpmCommand='-U'
;;
v)
version=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
if is_rpm; then
rpm $rpmCommand elasticsearch-$version.rpm
elif is_dpkg; then
dpkg -i elasticsearch-$version.deb
else
skip "Only rpm or deb supported"
fi
}
# Checks that all directories & files are correctly installed after a deb or
# rpm install.
verify_package_installation() {
id elasticsearch
getent group elasticsearch
assert_file "$ESHOME" d root 755
assert_file "$ESHOME/bin" d root 755
assert_file "$ESHOME/lib" d root 755
assert_file "$ESCONFIG" d root 755
assert_file "$ESCONFIG/elasticsearch.yml" f root 644
assert_file "$ESCONFIG/logging.yml" f root 644
assert_file "$ESDATA" d elasticsearch 755
assert_file "$ESLOG" d elasticsearch 755
assert_file "$ESPLUGINS" d elasticsearch 755
assert_file "$ESPIDDIR" d elasticsearch 755
assert_file "$ESHOME/NOTICE.txt" f root 644
assert_file "$ESHOME/README.textile" f root 644
if is_dpkg; then
# Env file
assert_file "/etc/default/elasticsearch" f root 644
# Doc files
assert_file "/usr/share/doc/elasticsearch" d root 755
assert_file "/usr/share/doc/elasticsearch/copyright" f root 644
fi
if is_rpm; then
# Env file
assert_file "/etc/sysconfig/elasticsearch" f root 644
# License file
assert_file "/usr/share/elasticsearch/LICENSE.txt" f root 644
fi
if is_systemd; then
assert_file "/usr/lib/systemd/system/elasticsearch.service" f root 644
assert_file "/usr/lib/tmpfiles.d/elasticsearch.conf" f root 644
assert_file "/usr/lib/sysctl.d/elasticsearch.conf" f root 644
fi
}

View File

@ -177,106 +177,6 @@ assert_output() {
echo "$output" | grep -E "$1"
}
# Checks that all directories & files are correctly installed
# after a package (deb/rpm) install
verify_package_installation() {
run id elasticsearch
[ "$status" -eq 0 ]
run getent group elasticsearch
[ "$status" -eq 0 ]
# Home dir
assert_file "/usr/share/elasticsearch" d root 755
# Bin dir
assert_file "/usr/share/elasticsearch/bin" d root 755
assert_file "/usr/share/elasticsearch/lib" d root 755
# Conf dir
assert_file "/etc/elasticsearch" d root 755
assert_file "/etc/elasticsearch/elasticsearch.yml" f root 644
assert_file "/etc/elasticsearch/logging.yml" f root 644
# Data dir
assert_file "/var/lib/elasticsearch" d elasticsearch 755
# Log dir
assert_file "/var/log/elasticsearch" d elasticsearch 755
# Plugins dir
assert_file "/usr/share/elasticsearch/plugins" d elasticsearch 755
# PID dir
assert_file "/var/run/elasticsearch" d elasticsearch 755
# Readme files
assert_file "/usr/share/elasticsearch/NOTICE.txt" f root 644
assert_file "/usr/share/elasticsearch/README.textile" f root 644
if is_dpkg; then
# Env file
assert_file "/etc/default/elasticsearch" f root 644
# Doc files
assert_file "/usr/share/doc/elasticsearch" d root 755
assert_file "/usr/share/doc/elasticsearch/copyright" f root 644
fi
if is_rpm; then
# Env file
assert_file "/etc/sysconfig/elasticsearch" f root 644
# License file
assert_file "/usr/share/elasticsearch/LICENSE.txt" f root 644
fi
if is_systemd; then
assert_file "/usr/lib/systemd/system/elasticsearch.service" f root 644
assert_file "/usr/lib/tmpfiles.d/elasticsearch.conf" f root 644
assert_file "/usr/lib/sysctl.d/elasticsearch.conf" f root 644
fi
}
# Install the rpm or deb package.
# -u upgrade rather than install. This only matters for rpm.
# -v the version to upgrade to. Defaults to the version under test.
install_package() {
local version=$(cat version)
local rpmCommand='-i'
while getopts ":uv:" opt; do
case $opt in
u)
rpmCommand='-U'
;;
v)
version=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
if is_rpm; then
rpm $rpmCommand elasticsearch-$version.rpm
elif is_dpkg; then
dpkg -i elasticsearch-$version.deb
else
skip "Only rpm or deb supported"
fi
}
# Checks that all directories & files are correctly installed
# after a archive (tar.gz/zip) install
verify_archive_installation() {
assert_file "$ESHOME" d
assert_file "$ESHOME/bin" d
assert_file "$ESHOME/bin/elasticsearch" f
assert_file "$ESHOME/bin/elasticsearch.in.sh" f
assert_file "$ESHOME/bin/plugin" f
assert_file "$ESCONFIG" d
assert_file "$ESCONFIG/elasticsearch.yml" f
assert_file "$ESCONFIG/logging.yml" f
assert_file "$ESHOME/lib" d
assert_file "$ESHOME/NOTICE.txt" f
assert_file "$ESHOME/LICENSE.txt" f
assert_file "$ESHOME/README.textile" f
}
# Deletes everything before running a test file
clean_before_test() {
@ -417,7 +317,7 @@ wait_for_elasticsearch_status() {
wget -O - --retry-connrefused --waitretry=1 --timeout=60 --tries 60 http://localhost:9200 || {
echo "Looks like elasticsearch never started. Here is its log:"
if [ -r "/tmp/elasticsearch/elasticsearch.pid" ]; then
cat /tmp/elasticsearch/log/elasticsearch.log
cat /tmp/elasticsearch/logs/elasticsearch.log
else
if [ -e '/var/log/elasticsearch/elasticsearch.log' ]; then
cat /var/log/elasticsearch/elasticsearch.log
@ -451,7 +351,12 @@ wait_for_elasticsearch_status() {
}
}
# Executes some very basic Elasticsearch tests
install_elasticsearch_test_scripts() {
install_script is_guide.groovy
install_script is_guide.mustache
}
# Executes some basic Elasticsearch tests
run_elasticsearch_tests() {
# TODO this assertion is the same the one made when waiting for
# elasticsearch to start
@ -463,8 +368,22 @@ run_elasticsearch_tests() {
"title": "Elasticsearch - The Definitive Guide"
}'
curl -s -XGET 'http://localhost:9200/_cat/count?h=count&v=false&pretty' |
grep -w "1"
curl -s -XGET 'http://localhost:9200/_count?pretty' |
grep \"count\"\ :\ 1
curl -s -XPOST 'http://localhost:9200/library/book/_count?pretty' -d '{
"query": {
"script": {
"script_file": "is_guide"
}
}
}' | grep \"count\"\ :\ 1
curl -s -XGET 'http://localhost:9200/library/book/_search/template?pretty' -d '{
"template": {
"file": "is_guide"
}
}' | grep \"total\"\ :\ 1
curl -s -XDELETE 'http://localhost:9200/_all'
}
@ -480,3 +399,12 @@ move_config() {
chown -R elasticsearch:elasticsearch "$ESCONFIG"
assert_file_exist "$ESCONFIG/elasticsearch.yml"
}
# Copies a script into the Elasticsearch install.
install_script() {
local name=$1
mkdir -p $ESSCRIPTS
local script="$BATS_TEST_DIRNAME/example/scripts/$name"
echo "Installing $script to $ESSCRIPTS"
cp $script $ESSCRIPTS
}

View File

@ -53,7 +53,6 @@ setup() {
if [ $BATS_TEST_NUMBER == 1 ] ||
[[ $BATS_TEST_NAME =~ install_jvm.*example ]] ||
[ ! -d "$ESHOME" ]; then
echo "cleaning" >> /tmp/ss
clean_before_test
install
fi
@ -69,14 +68,13 @@ if [[ "$BATS_TEST_FILENAME" =~ 25_tar_plugins.bats$ ]]; then
export ESHOME=/tmp/elasticsearch
export_elasticsearch_paths
else
load os_package
if is_rpm; then
GROUP='RPM PLUGINS'
elif is_dpkg; then
GROUP='DEB PLUGINS'
fi
export ESHOME="/usr/share/elasticsearch"
export ESPLUGINS="$ESHOME/plugins"
export ESCONFIG="/etc/elasticsearch"
export_elasticsearch_paths
install() {
install_package
verify_package_installation

View File

@ -70,4 +70,25 @@ move_elasticsearch() {
export_elasticsearch_paths() {
export ESPLUGINS="$ESHOME/plugins"
export ESCONFIG="$ESHOME/config"
export ESSCRIPTS="$ESCONFIG/scripts"
export ESDATA="$ESHOME/data"
export ESLOG="$ESHOME/logs"
}
# Checks that all directories & files are correctly installed
# after a archive (tar.gz/zip) install
verify_archive_installation() {
assert_file "$ESHOME" d
assert_file "$ESHOME/bin" d
assert_file "$ESHOME/bin/elasticsearch" f
assert_file "$ESHOME/bin/elasticsearch.in.sh" f
assert_file "$ESHOME/bin/plugin" f
assert_file "$ESCONFIG" d
assert_file "$ESCONFIG/elasticsearch.yml" f
assert_file "$ESCONFIG/logging.yml" f
assert_file "$ESHOME/lib" d
assert_file "$ESHOME/NOTICE.txt" f
assert_file "$ESHOME/LICENSE.txt" f
assert_file "$ESHOME/README.textile" f
}