Code

46568569a57c307d9672a66472ce5f5456679ded
[sysdb.git] / t / integration / query.sh
1 #! /bin/bash
2 #
3 # SysDB -- t/integration/basic_query.sh
4 # Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 # 1. Redistributions of source code must retain the above copyright
11 #    notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 #    notice, this list of conditions and the following disclaimer in the
14 #    documentation and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #
29 # Integration tests using basic queries.
30 #
32 set -ex
34 source "$( dirname "$0" )/test_lib.sh"
36 cat <<EOF > "$SYSDBD_CONF"
37 Listen "$SOCKET_FILE"
38 PluginDir "$PLUGIN_DIR"
39 Interval 2
41 LoadPlugin mock_timeseries
42 EOF
44 run_sysdbd -D -C "$SYSDBD_CONF"
45 wait_for_sysdbd
47 cat <<EOF > "${SYSDBD_CONF}.sender"
48 Listen "${SOCKET_FILE}.sender"
49 PluginDir "$PLUGIN_DIR"
50 Interval 2
52 LoadPlugin "store::network"
53 <Plugin "store::network">
54   Server "$SOCKET_FILE"
55 </Plugin>
57 LoadBackend mock_plugin
58 <Backend "mock_plugin">
59 </Backend>
60 EOF
62 run_sysdbd_foreground -D -C "${SYSDBD_CONF}.sender" &
63 SYSDBD_PID2=$!
64 trap "kill \$SYSDBD_PID2" EXIT
65 wait_for_sysdbd "${SOCKET_FILE}.sender"
67 # wait for initial data
68 sleep 3
70 # Invalid user.
71 output="$( run_sysdb_nouser -H "$SOCKET_FILE" \
72   -U $SYSDB_USER-invalid -c 'LIST hosts' 2>&1 )" && exit 1
73 echo "$output" | grep -F 'Access denied'
75 # On parse errors, expect a non-zero exit code.
76 output="$( run_sysdb -H "$SOCKET_FILE" -c INVALID 2>&1 )" && exit 1
77 echo "$output" | grep "Failed to parse query 'INVALID'"
78 echo "$output" | grep "parse error: syntax error"
80 # Empty query.
81 output="$( run_sysdb -H "$SOCKET_FILE" -c '' )"
82 test -z "$output"
84 # Simple, successful commands.
85 output="$( run_sysdb -H "$SOCKET_FILE" -c 'LIST hosts' )"
86 echo "$output" \
87         | grep -F '"host1.example.com"' \
88         | grep -F '"host2.example.com"' \
89         | grep -F '"localhost"' \
90         | grep -F '"other.host.name"' \
91         | grep -F '"some.host.name"'
93 output="$( echo 'LIST hosts;' | run_sysdb -H "$SOCKET_FILE" )" || echo $?
94 echo "$output" \
95         | grep -F '"host1.example.com"' \
96         | grep -F '"host2.example.com"' \
97         | grep -F '"localhost"' \
98         | grep -F '"other.host.name"' \
99         | grep -F '"some.host.name"'
101 output="$( run_sysdb -H "$SOCKET_FILE" -c 'LIST services' )"
102 echo "$output" \
103         | grep -F '"host1.example.com"' \
104         | grep -F '"host2.example.com"' \
105         | grep -F '"localhost"' \
106         | grep -F '"some.host.name"' \
107         | grep -F '"mock service"' \
108         | grep -F '"other service"' \
109         | grep -F '"database"' \
110         | grep -F '"example service one"' \
111         | grep -F '"example service two"' \
112         | grep -F '"example service three"'
114 output="$( run_sysdb -H "$SOCKET_FILE" -c "FETCH host 'host1.example.com'" )"
115 echo "$output" \
116         | grep -F '"host1.example.com"' \
117         | grep -F '"mock service"' \
118         | grep -E '"other attribute".*"special value"'
119 echo "$output" | grep -F 'host2.example.com' && exit 1
120 echo "$output" | grep -F 'localhost' && exit 1
121 echo "$output" | grep -F 'other.host.name' && exit 1
122 echo "$output" | grep -F 'some.host.name' && exit 1
124 output="$( run_sysdb -H "$SOCKET_FILE" \
125   -c "FETCH host 'host1.example.com' FILTER last_update < 0s" 2>&1 )" \
126   && exit 1
127 echo "$output" | grep -F 'not found'
129 (echo 'LIST hosts;'; sleep 1; echo "FETCH host 'host1.example.com'") \
130         | run_sysdb -H "$SOCKET_FILE"
132 output="$( run_sysdb -H "$SOCKET_FILE" \
133         -c "FETCH host 'host1.example.com' FILTER age < 0s" 2>&1 )" && exit 1
134 echo "$output" | grep -F 'not found'
136 # When requesting information for unknown hosts, expect a non-zero exit code.
137 output="$( run_sysdb -H "$SOCKET_FILE" \
138         -c "FETCH host 'does.not.exist'" 2>&1 )" && exit 1
139 echo "$output" | grep -F 'not found'
141 run_sysdb -H "$SOCKET_FILE" \
142                 -c "TIMESERIES 'invalid.host'.'invalid-metric'" && exit 1
144 # Does not work yet since there is no fetcher plugin.
145 output="$( run_sysdb -H "$SOCKET_FILE" \
146         -c "TIMESERIES 'some.host.name'.'foo/bar/qux'" )"
147 echo "$output" \
148         | grep -F '"value": "1.000000"' \
149         | grep -F '"value": "2.000000"' \
150         | grep -F '"value": "3.000000"' \
151         | grep -F '"value": "4.000000"' \
152         | grep -F '"value": "5.000000"' \
153         | grep -F '"value": "6.000000"' \
154         | grep -F '"value": "7.000000"' \
155         | grep -F '"value": "8.000000"' \
156         | grep -F '"value": "9.000000"' \
157         | grep -F '"value": "10.000000"'
159 stop_sysdbd
161 # vim: set tw=78 sw=4 ts=4 noexpandtab :