summaryrefslogtreecommitdiffstats
path: root/test/functional/functions.sh
blob: 7a7ec47fccd1a3f696763c3179f0dd614e638302 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
save_error() {
	_where=$1
	_reason=$2

	printf 'Failed in %s: %s\n' "$_where" "$_reason" > ${TMPDIR}/test.err
	mkdir -p /tmp/rspamd-failed-tests/${TEST_NAME} || true
	# Save logs
	RSPAMD_PID=`cat ${TMPDIR}/rspamd.pid`

	if [ F"${RSPAMD_PID}" != F"" ] ; then
		kill -USR1 ${RSPAMD_PID} > /dev/null 2>&1
		sleep 0.5
	fi
	cp -rf ${TMPDIR}/* /tmp/rspamd-failed-tests/${TEST_NAME}

	exit 1
}

run_rspamd() {
	RSPAMD_USER=${RSPAMD_USER:-"nobody"}
	RSPAMD_GROUP=${RSPAMD_GROUP:-"nogroup"}
	RSPAMD=${RSPAMD:-"$TEST_DIRNAME/../../src/rspamd"}
	STATS_BACKEND=${STATS_BACKEND:-"mmap"}
	STATS_HASH=${STATS_HASH:-"compat"}
	STATS_KEY=${STATS_KEY:-"osipg87ms5gzsis33fdrhaqn5wocp6qfofzxjbw8k1wh9yb6adty"}
	
	${RSPAMD} -c ${RSPAMD_CONFIG} -u ${RSPAMD_USER} -g ${RSPAMD_GROUP} -t \
		TMPDIR=${TMPDIR} \
		STATSDIR=${STATSDIR} \
		LUADIR=${LUADIR} \
		STATS_BACKEND=${STATS_BACKEND} \
		STATS_HASH=${STATS_HASH} \
		TESTDIR=${TEST_DIRNAME} \
		STATS_KEY=${STATS_KEY} > ${TMPDIR}/rspamd.out 2>&1

	if [ $? -ne 0 ] ; then
		save_error 'rspamd' 'cannot lint rspamd configuration'
	fi

	${RSPAMD} -c ${RSPAMD_CONFIG} -u ${RSPAMD_USER} -g ${RSPAMD_GROUP} \
		TMPDIR=${TMPDIR} \
		STATSDIR=${STATSDIR} \
		LUADIR=${LUADIR} \
		STATS_BACKEND=${STATS_BACKEND} \
		STATS_HASH=${STATS_HASH} \
		TESTDIR=${TEST_DIRNAME} \
		STATS_KEY=${STATS_KEY} >> ${TMPDIR}/rspamd.out 2>&1

	if [ $? -eq 0 ] ; then
		export RSPAMD_PID=`cat ${TMPDIR}/rspamd.pid`
	else
		save_error 'rspamd' 'cannot start rspamd'
	fi
}


teardown() {
	RSPAMD_PID=`cat ${TMPDIR}/rspamd.pid`

	if [ F"${RSPAMD_PID}" != F"" ] ; then
		kill -TERM ${RSPAMD_PID} > /dev/null 2>&1

		while [ $? -eq 0 ] ; do
			sleep 0.1
			kill -0 ${RSPAMD_PID} > /dev/null 2>&1
			if [ $? -eq 0 ] ; then
				kill -TERM ${RSPAMD_PID} > /dev/null 2>&1
			fi
			kill -0 ${RSPAMD_PID} > /dev/null 2>&1
		done
	fi

	if [ -d "${TMPDIR}" ] ; then
		(echo "${TMPDIR}" | egrep '^/tmp.*$' > /dev/null 2>&1) && rm -fr "${TMPDIR}"
	fi
}

check_output() {
	_pattern="$1"

	echo "$output" | egrep "$_pattern" > /dev/null 2>&1
	
	_saved_exit=$?
	if [ $_saved_exit -ne 0 ] ; then
		echo "$output" > ${TMPDIR}/rspamc.err
		save_error 'rspamc' "Expected pattern $_pattern is not found"
		return $_saved_exit
	fi

	return 0
}

run_rspamc() {
	_command=$1
	shift
	_rspamc="$TEST_DIRNAME/../../src/client/rspamc"

	case $_command in
		learn_spam|learn_ham|fuzzy_add|fuzzy_del|stat) _host="localhost:56790" ;;
		*) _host="localhost:56789" ;;
	esac

	output=`$_rspamc -h $_host $_command $@ 2>&1`
	echo "$output" > ${TMPDIR}/rspamc.output

	if [ $? -eq 0 ] ; then
		export output
		return 0
	else
		save_error 'rspamc' "Wrong exit code"
	fi

	return 1
}

run() {
	_command=$1
	shift

	output=`$_command $@ 2>&1`
	echo "$output" > "${TMPDIR}/${_command}.output"

	if [ $? -eq 0 ] ; then
		export output
		return 0
	else
		save_error "${_command}" "Wrong exit code"
	fi

	return 1
}