...
1# Functions for setting up CT personalities in Trillian integration tests
2# Requires github.com/google/trillian/integration/functions.sh
3
4declare -a CT_SERVER_PIDS
5CT_SERVERS=
6CT_CFG=
7CT_LIFECYCLE_CFG=
8CT_COMBINED_CFG=
9PROMETHEUS_CFGDIR=
10readonly CT_GO_PATH=$(go list -f '{{.Dir}}' github.com/google/certificate-transparency-go)
11
12# ct_prep_test prepares a set of running processes for a CT test.
13# Parameters:
14# - number of CT personality instances to run
15# Populates:
16# - CT_SERVERS : list of HTTP addresses (comma separated)
17# - CT_SERVER_1 : first HTTP address
18# - CT_METRICS_SERVERS : list of HTTP addresses (comma separated) serving metrics
19# - CT_SERVER_PIDS : bash array of CT HTTP server pids
20# in addition to the variables populated by log_prep_test.
21# If etcd and Prometheus are configured, it also populates:
22# - PROMETHEUS_PID : pid of local Prometheus server
23# - PROMETHEUS_CFGDIR : Prometheus configuration directory
24ct_prep_test() {
25 # Default to one of everything.
26 local http_server_count=${1:-1}
27
28 echo "PREP: Trillian: ${RPC_SERVER_1} [${RPC_SERVERS}]"
29
30 echo "Building CT personality code"
31 go build github.com/google/certificate-transparency-go/trillian/ctfe/ct_server
32
33 echo "Provisioning logs for CT"
34 ct_provision "${RPC_SERVER_1}"
35
36 echo "Launching CT personalities"
37 for ((i=0; i < http_server_count; i++)); do
38 local port=$(pick_unused_port)
39 CT_SERVERS="${CT_SERVERS},localhost:${port}"
40 local metrics_port=$(pick_unused_port ${port})
41 CT_METRICS_SERVERS="${CT_METRICS_SERVERS},localhost:${metrics_port}"
42 if [[ $i -eq 0 ]]; then
43 CT_SERVER_1="localhost:${port}"
44 fi
45
46 echo "Starting CT HTTP server on localhost:${port}, metrics on localhost:${metrics_port}"
47 ./ct_server ${ETCD_OPTS} --log_config="${CT_COMBINED_CFG}" --log_rpc_server="${RPC_SERVERS}" --http_endpoint="localhost:${port}" --metrics_endpoint="localhost:${metrics_port}" ${CTFE_OPTS} &
48 pid=$!
49 CT_SERVER_PIDS+=(${pid})
50 wait_for_server_startup ${port}
51 done
52 CT_SERVERS="${CT_SERVERS:1}"
53 CT_METRICS_SERVERS="${CT_METRICS_SERVERS:1}"
54
55 if [[ ! -z "${ETCD_OPTS}" ]]; then
56 echo "Registered HTTP endpoints"
57 ETCDCTL_API=3 etcdctl get trillian-ctfe-http/ --prefix
58 ETCDCTL_API=3 etcdctl get trillian-ctfe-metrics-http/ --prefix
59 fi
60
61 if [[ -x "${PROMETHEUS_DIR}/prometheus" ]]; then
62 if [[ ! -z "${ETCD_OPTS}" ]]; then
63 PROMETHEUS_CFGDIR="$(mktemp -d ${TMPDIR}/ct-prometheus-XXXXXX)"
64 echo "Launching Prometheus (default location localhost:9090)"
65 ${PROMETHEUS_DIR}/prometheus --config.file=${CT_GO_PATH}/trillian/integration/prometheus.yml \
66 --web.console.templates=${CT_GO_PATH}/trillian/integration/consoles \
67 --web.console.libraries=${CT_GO_PATH}/third_party/prometheus/console_libs &
68 PROMETHEUS_PID=$!
69 fi
70 fi
71}
72
73# ct_provision generates a CT configuration file and provisions the trees for it.
74# Parameters:
75# - location of admin server instance
76# Populates:
77# - CT_CFG : configuration file for CT integration test
78# - CT_LIFECYCLE_CFG : configuration file for CT lifecycle test
79# - CT_COMBINED_CFG : the above configs concatenated together
80ct_provision() {
81 local admin_server="$1"
82
83 # Build config files with absolute paths
84 CT_CFG=$(mktemp ${TMPDIR}/ct-XXXXXX)
85 sed "s!@TESTDATA@!${CT_GO_PATH}/trillian/testdata!" ${CT_GO_PATH}/trillian/integration/ct_integration_test.cfg > "${CT_CFG}"
86
87 CT_LIFECYCLE_CFG=$(mktemp ${TMPDIR}/ct-XXXXXX)
88 sed "s!@TESTDATA@!${CT_GO_PATH}/trillian/testdata!" ${CT_GO_PATH}/trillian/integration/ct_lifecycle_test.cfg > "${CT_LIFECYCLE_CFG}"
89
90 echo 'Building createtree'
91 go build github.com/google/trillian/cmd/createtree/
92
93 echo 'Provisioning Integration Logs'
94 ct_provision_cfg ${admin_server} ${CT_CFG}
95 echo 'Provisioning Lifecycle Logs'
96 ct_provision_cfg ${admin_server} ${CT_LIFECYCLE_CFG}
97
98 CT_COMBINED_CFG=$(mktemp ${TMPDIR}/ct-XXXXXX)
99 cat ${CT_CFG} ${CT_LIFECYCLE_CFG} > ${CT_COMBINED_CFG}
100
101 echo "CT Integration Configuration in ${CT_CFG}:"
102 cat "${CT_CFG}"
103 echo "CT Lifecycle Configuration in ${CT_LIFECYCLE_CFG}:"
104 cat "${CT_LIFECYCLE_CFG}"
105 echo
106}
107
108# ct_provision_cfg provisions trees for the logs in a specified config file.
109# Parameters:
110# - location of admin server instance
111# - the config file to be provisioned for
112ct_provision_cfg() {
113 local admin_server="$1"
114 local cfg="$2"
115
116 num_logs=$(grep -c '@TREE_ID@' ${cfg})
117 for i in $(seq ${num_logs}); do
118 tree_id=$(./createtree --admin_server="${admin_server}")
119 echo "Created tree ${tree_id}"
120 # Need suffix for sed -i to cope with both GNU and non-GNU (e.g. OS X) sed.
121 sed -i'.bak' "1,/@TREE_ID@/s/@TREE_ID@/${tree_id}/" "${cfg}"
122 rm -f "${cfg}.bak"
123 done
124}
125
126# ct_stop_test closes the running processes for a CT test.
127# Assumes the following variables are set, in addition to those needed by logStopTest:
128# - CT_SERVER_PIDS : bash array of CT HTTP server pids
129ct_stop_test() {
130 local pids
131 if [[ "${PROMETHEUS_PID}" != "" ]]; then
132 pids+=" ${PROMETHEUS_PID}"
133 fi
134 echo "Stopping CT HTTP servers (pids ${CT_SERVER_PIDS[@]})"
135 pids+=" ${CT_SERVER_PIDS[@]}"
136 kill_pid ${pids}
137}
View as plain text