1#!/bin/bash
2
3function print_ps {
4    set +e
5    echo "CFEngine processes:"
6    ps aux | grep [c]f-
7
8    echo "Valgrind processes:"
9    ps aux | grep [v]algrind
10    set -e
11}
12
13function stop_daemons {
14    echo "Stopping cfengine daemons"
15    pkill -f cf-serverd  || echo "Did not kill cf-serverd"
16    pkill -f cf-execd    || echo "Did not kill cf-execd"
17    pkill -f cf-monitord || echo "Did not kill cf-monitord"
18}
19
20function no_errors {
21    set +e
22    grep -i "error" $1
23    grep -q -i "error" $1 && exit 1
24    set -e
25}
26
27function check_daemon_output {
28    echo "Examining $1:"
29    no_errors $1
30}
31
32function check_valgrind_output {
33    set -e
34    if [ ! -f "$1" ]; then
35        echo "$1 does not exists!"
36        exit 1
37    fi
38    echo "Looking for problems in $1:"
39    grep -i "ERROR SUMMARY: 0 errors" "$1"
40    cat $1 | sed -e "/ 0 errors/d" -e "/and suppressed error/d" -e "/a memory error detector/d" > filtered.txt
41    no_errors filtered.txt
42    set +e
43    grep -i "at 0x" filtered.txt
44    grep -i -q "at 0x" filtered.txt && exit 1
45    grep -i "by 0x" filtered.txt
46    grep -i -q "by 0x" filtered.txt && exit 1
47    grep -i "Failed to connect" filtered.txt
48    grep -i -q "Failed to connect" filtered.txt && exit 1
49    set -e
50}
51
52function check_masterfiles_and_inputs {
53    set -e
54    echo "Comparing promises.cf from inputs and masterfiles:"
55    diff -u /var/cfengine/inputs/promises.cf /var/cfengine/masterfiles/promises.cf
56}
57
58set -e
59set -x
60
61# Assume we are in core directory
62if [ -f ./configure ] ; then
63  ./configure -C --enable-debug
64else
65  ./autogen.sh -C --enable-debug
66fi
67make
68make install
69
70cd ../masterfiles
71if [ -f ./configure ] ; then
72  ./configure -C --enable-debug
73else
74  ./autogen.sh -C --enable-debug
75fi
76make
77make install
78
79/var/cfengine/bin/cf-agent --version
80
81VG_OPTS="--leak-check=full --track-origins=yes --error-exitcode=1"
82BOOTSTRAP_IP="127.0.0.1"
83
84valgrind $VG_OPTS /var/cfengine/bin/cf-key 2>&1 | tee cf-key.txt
85check_valgrind_output cf-key.txt
86valgrind $VG_OPTS /var/cfengine/bin/cf-agent -B $BOOTSTRAP_IP 2>&1 | tee bootstrap.txt
87check_valgrind_output bootstrap.txt
88
89echo "Running cf-check diagnose --validate on all databases:"
90valgrind $VG_OPTS /var/cfengine/bin/cf-check diagnose --validate 2>&1 | tee cf_check_validate_all_1.txt
91check_valgrind_output cf_check_validate_all_1.txt
92
93check_masterfiles_and_inputs
94
95print_ps
96
97echo "Stopping before relaunch under valgrind"
98stop_daemons
99sleep 10
100print_ps
101
102echo "Starting cf-serverd with valgrind in background:"
103valgrind $VG_OPTS --log-file=serverd.txt /var/cfengine/bin/cf-serverd --no-fork 2>&1 > serverd_output.txt &
104server_pid="$!"
105sleep 20
106
107echo "Starting cf-execd with valgrind in background:"
108valgrind $VG_OPTS --log-file=execd.txt /var/cfengine/bin/cf-execd --no-fork 2>&1 > execd_output.txt &
109exec_pid="$!"
110sleep 10
111
112print_ps
113
114echo "Running cf-net:"
115valgrind $VG_OPTS /var/cfengine/bin/cf-net GET /var/cfengine/masterfiles/promises.cf 2>&1 | tee get.txt
116check_valgrind_output get.txt
117
118echo "Checking promises.cf diff (from cf-net GET):"
119diff -u ./promises.cf /var/cfengine/masterfiles/promises.cf
120
121echo "Running update.cf:"
122valgrind $VG_OPTS /var/cfengine/bin/cf-agent -K -f update.cf 2>&1 | tee update.txt
123check_valgrind_output update.txt
124check_masterfiles_and_inputs
125echo "Running update.cf without local copy:"
126valgrind $VG_OPTS /var/cfengine/bin/cf-agent -K -f update.cf -D mpf_skip_local_copy_optimization 2>&1 | tee update2.txt
127check_valgrind_output update2.txt
128check_masterfiles_and_inputs
129echo "Running promises.cf:"
130valgrind $VG_OPTS /var/cfengine/bin/cf-agent -K -f promises.cf 2>&1 | tee promises.txt
131check_valgrind_output promises.txt
132
133# Dump all databases, use grep to filter the JSON lines
134# (optional whitespace then double quote or curly brackets).
135# Some of the databases have strings containing "error"
136# which check_valgrind_output greps for.
137echo "Running cf-check dump:"
138valgrind $VG_OPTS /var/cfengine/bin/cf-check dump 2>&1 | grep -E '\s*[{}"]' --invert-match | tee cf_check_dump.txt
139check_valgrind_output cf_check_dump.txt
140
141echo "Running cf-check diagnose on all databases"
142valgrind $VG_OPTS /var/cfengine/bin/cf-check diagnose 2>&1 | tee cf_check_diagnose.txt
143check_valgrind_output cf_check_diagnose.txt
144
145echo "Running cf-check diagnose --validate on all databases:"
146valgrind $VG_OPTS /var/cfengine/bin/cf-check diagnose --validate 2>&1 | tee cf_check_validate_all_2.txt
147check_valgrind_output cf_check_validate_all_2.txt
148
149echo "Checking that bootstrap ID doesn't change"
150/var/cfengine/bin/cf-agent --show-evaluated-vars | grep bootstrap_id > id_a
151/var/cfengine/bin/cf-agent -K --show-evaluated-vars | grep bootstrap_id > id_b
152cat id_a
153diff id_a id_b
154
155echo "Checking that bootstrap ID has expected length"
156[ `cat id_a | awk '{print $2}' | wc -c` -eq 41 ]
157
158print_ps
159
160echo "cf-execd outputs:"
161tail execd_output.txt
162tail execd.txt
163
164echo "cf-serverd outputs:"
165tail serverd_output.txt
166tail serverd.txt
167
168echo "Checking that serverd and execd PIDs are still correct/alive:"
169ps -p $exec_pid
170ps -p $server_pid
171
172echo "Killing valgrind cf-execd"
173kill $exec_pid
174echo "Killing valgrind cf-serverd"
175kill $server_pid
176
177wait $exec_pid
178wait $server_pid
179
180echo "Output from cf-execd in valgrind:"
181cat execd.txt
182check_valgrind_output execd.txt
183check_daemon_output execd_output.txt
184
185echo "Output from cf-serverd in valgrind:"
186cat serverd.txt
187check_valgrind_output serverd.txt
188check_daemon_output serverd_output.txt
189
190stop_daemons
191
192echo "Done killing"
193sleep 10
194print_ps
195
196echo "Check that bootstrap was successful"
197grep "This host assumes the role of policy server" bootstrap.txt
198grep "completed successfully!" bootstrap.txt
199
200echo "valgrind_health_check successful! (valgrind.sh)"
201