summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/tests/federated_cluster_test
blob: ef0db4cb8e1bf52c3f83ff0cd5027bc5060e0ab2 (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
132
#!/bin/sh

#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

# Test reliability of the replication feature in the face of link
# failures:
srcdir=`dirname $0`
PYTHON_DIR=${srcdir}/../../../python

trap stop_brokers EXIT

fail() {
    echo $1
    exit 1
}

stop_brokers() {
    if [[ $BROKER_A ]] ; then
        ../qpidd -q --port $BROKER_A
        unset BROKER_A
    fi      
    if [[ $NODE_1 ]] ; then
        ./stop_cluster
        unset NODE_1
    fi
}

start_brokers() {
    #start single node...
    ../qpidd --daemon --port 0 --no-data-dir --no-module-dir --auth no --log-enable info+ > fed.port.tmp
    BROKER_A=`cat fed.port.tmp`

    #...and start cluster
    $srcdir/start_cluster 2 || fail "Could not start cluster"
    NODE_1=$(head -1 cluster.ports)
    NODE_2=$(tail -1 cluster.ports)
}

setup() {
    export PYTHONPATH=$PYTHON_DIR
    #create exchange on both cluster and single broker
    $PYTHON_DIR/commands/qpid-config -a "localhost:$BROKER_A" add exchange direct test-exchange
    $PYTHON_DIR/commands/qpid-config -a "localhost:$NODE_1" add exchange direct test-exchange

    #create dynamic routes for test exchange
    $PYTHON_DIR/commands/qpid-route dynamic add "localhost:$NODE_1" "localhost:$BROKER_A" test-exchange
    $PYTHON_DIR/commands/qpid-route dynamic add "localhost:$BROKER_A" "localhost:$NODE_1" test-exchange

    #create test queue on cluster and bind it to the test exchange
    $PYTHON_DIR/commands/qpid-config -a "localhost:$NODE_1" add queue test-queue
    $PYTHON_DIR/commands/qpid-config -a "localhost:$NODE_1" bind test-exchange test-queue to-cluster

    #create test queue on single broker and bind it to the test exchange
    $PYTHON_DIR/commands/qpid-config -a "localhost:$BROKER_A" add queue test-queue
    $PYTHON_DIR/commands/qpid-config -a "localhost:$BROKER_A" bind test-exchange test-queue from-cluster
}

run_test_pull_to_cluster() {
    #start consumers on each of the two nodes of the cluster
    ./receiver --port $NODE_1 --queue test-queue --credit-window 1 > fed1.out.tmp &
    ./receiver --port $NODE_2 --queue test-queue --credit-window 1 > fed2.out.tmp &

    #send stream of messages to test exchange on single broker
    for i in `seq 1 1000`; do echo Message $i >> fed.in.tmp; done
    ./sender --port $BROKER_A --exchange test-exchange --routing-key to-cluster --send-eos 2 < fed.in.tmp

    #combine output of the two consumers, sort it and compare with the expected stream
    wait
    sort -g -k 2 fed1.out.tmp fed2.out.tmp > fed.out.tmp
    diff fed.in.tmp fed.out.tmp || fail "federated link to cluster failed: expectations not met!"
        
    rm -f fed*.tmp #cleanup
}

run_test_pull_from_cluster() {
    #start consumer on single broker
    ./receiver --port $BROKER_A --queue test-queue --credit-window 1 > fed.out.tmp &

    #send stream of messages to test exchange on cluster
    for i in `seq 1 1000`; do echo Message $i >> fed.in.tmp; done
    ./sender --port $NODE_2 --exchange test-exchange --routing-key from-cluster --send-eos 1 < fed.in.tmp

    #verify all messages are received
    wait
    diff fed.in.tmp fed.out.tmp || fail "federated link from cluster failed: expectations not met!"
        
    rm -f fed*.tmp #cleanup
}


if test -d ${PYTHON_DIR}; then
    id -nG | grep '\<ais\>' >/dev/null || \
        NOGROUP="You are not a member of the ais group."
    ps -u root | grep 'aisexec\|corosync' >/dev/null || \
        NOAISEXEC="The aisexec or corosync daemon is not running as root"

    if test -n "$NOGROUP" -o -n "$NOAISEXEC"; then
        cat <<EOF
Not running federation to cluster test because:
    $NOGROUP
    $NOAISEXEC
EOF
        exit 0;
    fi

    rm -f fed*.tmp #cleanup any files left from previous run
    start_brokers
    echo "brokers started"
    setup
    echo "setup completed"
    run_test_pull_to_cluster    
    echo "federated link to cluster verified"
    run_test_pull_from_cluster    
    echo "federated link from cluster verified"
fi