Code

integration tests: Make sure test_lib always knows about the socket file.
[sysdb.git] / t / integration / simple_query.sh
1 #! /bin/bash
2 #
3 # SysDB -- t/integration/simple_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 simple 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 LoadBackend mock_plugin
42 <Backend "mock_plugin">
43 </Backend>
44 EOF
46 run_sysdbd -D -C "$SYSDBD_CONF"
48 wait_for_sysdbd
49 sleep 3
51 # On parse errors, expect a non-zero exit code.
52 output="$( run_sysdb -H "$SOCKET_FILE" -c INVALID )" && exit 1
53 echo "$output" | grep "Failed to parse query 'INVALID'"
54 echo "$output" | grep "parse error: syntax error"
56 # Simple, successful commands.
57 output="$( run_sysdb -H "$SOCKET_FILE" -c 'LIST hosts' )"
58 echo "$output" \
59         | grep -F '"host1.example.com"' \
60         | grep -F '"host2.example.com"' \
61         | grep -F '"localhost"' \
62         | grep -F '"other.host.name"' \
63         | grep -F '"some.host.name"'
65 output="$( echo 'LIST hosts;' | run_sysdb -H "$SOCKET_FILE" )" || echo $?
66 echo "$output" \
67         | grep -F '"host1.example.com"' \
68         | grep -F '"host2.example.com"' \
69         | grep -F '"localhost"' \
70         | grep -F '"other.host.name"' \
71         | grep -F '"some.host.name"'
73 output="$( run_sysdb -H "$SOCKET_FILE" -c 'LIST services' )"
74 echo "$output" \
75         | grep -F '"host1.example.com"' \
76         | grep -F '"host2.example.com"' \
77         | grep -F '"localhost"' \
78         | grep -F '"some.host.name"' \
79         | grep -F '"mock service"' \
80         | grep -F '"other service"' \
81         | grep -F '"database"' \
82         | grep -F '"example service one"' \
83         | grep -F '"example service two"' \
84         | grep -F '"example service three"'
86 output="$( run_sysdb -H "$SOCKET_FILE" -c "FETCH host 'host1.example.com'" )"
87 echo "$output" \
88         | grep -F '"host1.example.com"' \
89         | grep -F '"mock service"' \
90         | grep -E '"other attribute".*"special value"'
91 echo "$output" | grep -F 'host2.example.com' && exit 1
92 echo "$output" | grep -F 'localhost' && exit 1
93 echo "$output" | grep -F 'other.host.name' && exit 1
94 echo "$output" | grep -F 'some.host.name' && exit 1
96 (echo 'LIST hosts;'; sleep 1; echo "FETCH host 'host1.example.com'") \
97         | run_sysdb -H "$SOCKET_FILE"
99 # When requesting information for unknown hosts, expect a non-zero exit code.
100 output="$( run_sysdb -H "$SOCKET_FILE" -c "FETCH host 'does.not.exist'" )" \
101         && exit 1
102 echo "$output" | grep -F 'not found'
104 output="$( run_sysdb -H "$SOCKET_FILE" \
105         -c "LOOKUP hosts MATCHING metric = 'foo/bar/qux'" )"
106 echo "$output" \
107         | grep -F '"some.host.name"' \
108         | grep -F '"other.host.name"'
109 echo "$output" | grep -F 'localhost' && exit 1
110 echo "$output" | grep -F 'host1.example.com' && exit 1
111 echo "$output" | grep -F 'host2.example.com' && exit 1
113 output="$( run_sysdb -H "$SOCKET_FILE" \
114         -c "LOOKUP hosts MATCHING service = 'mock service'" )"
115 echo "$output" \
116         | grep -F '"some.host.name"' \
117         | grep -F '"host1.example.com"' \
118         | grep -F '"host2.example.com"'
119 echo "$output" | grep -F 'localhost' && exit 1
120 echo "$output" | grep -F 'other.host.name' && exit 1
122 output="$( run_sysdb -H "$SOCKET_FILE" \
123         -c "LOOKUP hosts MATCHING attribute[architecture] = 'x42'" )"
124 echo "$output" \
125         | grep -F '"host1.example.com"' \
126         | grep -F '"host2.example.com"'
127 echo "$output" | grep -F 'localhost' && exit 1
128 echo "$output" | grep -F 'other.host.name' && exit 1
129 echo "$output" | grep -F 'some.host.name' && exit 1
131 output="$( run_sysdb -H "$SOCKET_FILE" \
132         -c "LOOKUP hosts MATCHING attribute != 'architecture'" )"
133 echo "$output" \
134         | grep -F '"some.host.name"' \
135         | grep -F '"localhost"'
136 echo "$output" | grep -F 'other.host.name' && exit 1
137 echo "$output" | grep -F 'host1.example.com' && exit 1
138 echo "$output" | grep -F 'host2.example.com' && exit 1
140 output="$( run_sysdb -H "$SOCKET_FILE" \
141         -c "LOOKUP hosts MATCHING attribute != 'architecture' 
142                 FILTER .age >= 0s" )"
143 echo "$output" \
144         | grep -F '"some.host.name"' \
145         | grep -F '"localhost"'
146 echo "$output" | grep -F 'other.host.name' && exit 1
147 echo "$output" | grep -F 'host1.example.com' && exit 1
148 echo "$output" | grep -F 'host2.example.com' && exit 1
150 output="$( run_sysdb -H "$SOCKET_FILE" \
151         -c "LOOKUP hosts MATCHING attribute != 'architecture' 
152                 FILTER .last_update < 2Y" )"
153 echo $output | grep -E '^\[\]$'
155 output="$( run_sysdb -H "$SOCKET_FILE" \
156         -c "LOOKUP hosts FILTER .backend = 'backend::mock_plugin'" )"
157 echo "$output" \
158         | grep -F '"host1.example.com"' \
159         | grep -F '"host2.example.com"' \
160         | grep -F '"localhost"' \
161         | grep -F '"other.host.name"' \
162         | grep -F '"some.host.name"'
163 output="$( run_sysdb -H "$SOCKET_FILE" \
164         -c "LOOKUP hosts FILTER .backend = 'invalid'" )"
165 echo $output | grep -E '^\[\]$'
167 output="$( run_sysdb -H "$SOCKET_FILE" \
168         -c "LOOKUP hosts MATCHING service = 'sysdbd'" )"
169 echo "$output" | grep -F '"localhost"'
170 echo "$output" | grep -F 'some.host.name' && exit 1
171 echo "$output" | grep -F 'other.host.name' && exit 1
172 echo "$output" | grep -F 'host1.example.com' && exit 1
173 echo "$output" | grep -F 'host2.example.com' && exit 1
175 output="$( run_sysdb -H "$SOCKET_FILE" \
176         -c "LOOKUP hosts MATCHING host =~ 'example.com'" )"
177 echo "$output" \
178         | grep -F '"host1.example.com"' \
179         | grep -F '"host2.example.com"'
180 echo "$output" | grep -F 'some.host.name' && exit 1
181 echo "$output" | grep -F 'other.host.name' && exit 1
182 echo "$output" | grep -F 'localhost' && exit 1
184 # When querying hosts that don't exist, expect a zero exit code.
185 output="$( run_sysdb -H "$SOCKET_FILE" \
186         -c "LOOKUP hosts MATCHING attribute[invalid] = 'none'" )"
187 echo $output | grep -E '^\[\]$'
189 run_sysdb -H "$SOCKET_FILE" \
190                 -c "TIMESERIES 'invalid.host'.'invalid-metric'" && exit 1
192 # Does not work yet since there is no fetcher plugin.
193 run_sysdb -H "$SOCKET_FILE" \
194                 -c "TIMESERIES 'some.host.name'.'foo/bar/qux'" && exit 1
196 stop_sysdbd