Code

e7118974fd95b4cbfe4b65800ff19c1017dfbc91
[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 -e
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 "test::integration::mock">
43 </Backend>
44 EOF
46 $SYSDBD -D -C "$SYSDBD_CONF" &
47 sysdbd_pid=$!
49 wait_for_sysdbd
50 sleep 3
52 # On parse errors, expect a non-zero exit code.
53 output="$( $SYSDB -H "$SOCKET_FILE" -c INVALID )" && exit 1
54 echo "$output" | grep "Failed to parse query 'INVALID'"
55 echo "$output" | grep "parse error: syntax error"
57 output="$( $SYSDB -H "$SOCKET_FILE" -c LIST )"
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="$( $SYSDB -H "$SOCKET_FILE" -c "FETCH 'host1.example.com'" )"
66 echo "$output" \
67         | grep -F '"host1.example.com"' \
68         | grep -F '"mock service"' \
69         | grep -E '"other attribute".*"special value"'
70 echo "$output" | grep -F 'host2.example.com' && exit 1
71 echo "$output" | grep -F 'localhost' && exit 1
72 echo "$output" | grep -F 'other.host.name' && exit 1
73 echo "$output" | grep -F 'some.host.name' && exit 1
75 # When requesting information for unknown hosts, expect a non-zero exit code.
76 output="$( $SYSDB -H "$SOCKET_FILE" -c "FETCH 'does.not.exist'" )" \
77         && exit 1
78 echo "$output" | grep -F 'not found'
80 output="$( $SYSDB -H "$SOCKET_FILE" \
81         -c "LOOKUP hosts WHERE attribute.architecture = 'x42'" )"
82 echo "$output" \
83         | grep -F '"host1.example.com"' \
84         | grep -F '"host2.example.com"'
85 echo "$output" | grep -F 'localhost' && exit 1
86 echo "$output" | grep -F 'other.host.name' && exit 1
87 echo "$output" | grep -F 'some.host.name' && exit 1
89 output="$( $SYSDB -H "$SOCKET_FILE" \
90         -c "LOOKUP hosts WHERE attribute.name != 'architecture'" )"
91 echo "$output" \
92         | grep -F '"some.host.name"' \
93         | grep -F '"localhost"'
94 echo "$output" | grep -F 'other.host.name' && exit 1
95 echo "$output" | grep -F 'host1.example.com' && exit 1
96 echo "$output" | grep -F 'host2.example.com' && exit 1
98 output="$( $SYSDB -H "$SOCKET_FILE" \
99         -c "LOOKUP hosts WHERE service.name = 'sysdbd'" )"
100 echo "$output" | grep -F '"localhost"'
101 echo "$output" | grep -F 'some.host.name' && exit 1
102 echo "$output" | grep -F 'other.host.name' && exit 1
103 echo "$output" | grep -F 'host1.example.com' && exit 1
104 echo "$output" | grep -F 'host2.example.com' && exit 1
106 output="$( $SYSDB -H "$SOCKET_FILE" \
107         -c "LOOKUP hosts WHERE host.name =~ 'example.com'" )"
108 echo "$output" \
109         | grep -F '"host1.example.com"' \
110         | grep -F '"host2.example.com"'
111 echo "$output" | grep -F 'some.host.name' && exit 1
112 echo "$output" | grep -F 'other.host.name' && exit 1
113 echo "$output" | grep -F 'localhost' && exit 1
115 # When querying hosts that don't exist, expect a zero exit code.
116 output="$( $SYSDB -H "$SOCKET_FILE" \
117         -c "LOOKUP hosts WHERE attribute.invalid = 'none'" )"
118 echo $output | grep -E '^\[\]$'
120 kill $sysdbd_pid
121 wait $sysdbd_pid