summaryrefslogtreecommitdiff
path: root/cpp/src/tests/cluster_authentication_soak.cpp
blob: a3271701c381397658e621b0935c7e11e51e2e90 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 *
 * 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.
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>

#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>

#include <string>
#include <iostream>
#include <sstream>
#include <vector>

#include <boost/assign.hpp>

#include "qpid/framing/Uuid.h"

#include <ForkedBroker.h>
#include <qpid/client/Connection.h>

#include <sasl/sasl.h>

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif




using namespace std;
using boost::assign::list_of;
using namespace qpid::framing;
using namespace qpid::client;


namespace qpid {
namespace tests {

vector<pid_t> brokerPids;

typedef vector<ForkedBroker *> brokerVector;





int  runSilent    = 1;
int  newbiePort   = 0;


void
makeClusterName ( string & s ) {
    stringstream ss;
    ss << "authenticationSoakCluster_" << Uuid(true).str();
    s = ss.str();
}



void
startBroker ( brokerVector & brokers , int brokerNumber, string const & clusterName ) {
    stringstream prefix, clusterArg;
    prefix << "soak-" << brokerNumber;
    clusterArg << "--cluster-name=" << clusterName;

    std::vector<std::string> argv;

    argv.push_back ("../qpidd");
    argv.push_back ("--no-module-dir");
    argv.push_back ("--load-module=../.libs/cluster.so");
    argv.push_back (clusterArg.str());
    argv.push_back ("--cluster-username=zig");
    argv.push_back ("--cluster-password=zig");
    argv.push_back ("--cluster-mechanism=PLAIN");
    argv.push_back ("--sasl-config=./sasl_config");
    argv.push_back ("--auth=yes");
    argv.push_back ("--mgmt-enable=yes");
    argv.push_back ("--log-prefix");
    argv.push_back (prefix.str());
    argv.push_back ("--log-to-file");
    argv.push_back (prefix.str()+".log");
    argv.push_back ("TMP_DATA_DIR");

    ForkedBroker * newbie = new ForkedBroker (argv);
    newbiePort = newbie->getPort();
    brokers.push_back ( newbie );
}




bool
runPerftest ( bool hangTest ) {
    stringstream portSs;
    portSs << newbiePort;
    string portStr = portSs.str();
    char const *  path = "./qpid-perftest";

    vector<char const *> argv;
    argv.push_back ( "./qpid-perftest" );
    argv.push_back ( "-p" );
    argv.push_back ( portStr.c_str() );
    argv.push_back ( "--username" );
    argv.push_back ( "zig" );
    argv.push_back ( "--password" );
    argv.push_back ( "zig" );
    argv.push_back ( "--mechanism" );
    argv.push_back ( "DIGEST-MD5" );
    argv.push_back ( "--count" );
    argv.push_back ( "20000" );
    argv.push_back ( 0 );

    pid_t pid = fork();

    if ( ! pid ) {
        int i=open("/dev/null",O_RDWR);
        dup2 ( i, fileno(stdout) );
        dup2 ( i, fileno(stderr) );

        execv ( path, const_cast<char * const *>(&argv[0]) );
        // The exec failed: we are still in parent process.
        perror ( "error running qpid-perftest: " );
        return false;
    }
    else {
	if ( hangTest ) {
	    if ( ! runSilent )
	        cerr << "Pausing perftest " << pid << endl;
	    kill ( pid, 19 );
	}

        struct timeval startTime,
                       currentTime,
                       duration;

        gettimeofday ( & startTime, 0 );

        while ( 1 ) {
          sleep ( 2 );
          int status;
          int returned_pid = waitpid ( pid, &status, WNOHANG );
          if ( returned_pid == pid ) {
              int exit_status = WEXITSTATUS(status);
              if ( exit_status ) {
                  cerr << "qpid-perftest failed. exit_status was: " << exit_status << endl;
                return false;
              }
              else {
                return true; // qpid-perftest succeeded.
              }
          }
          else  {  // qpid-perftest has not yet completed.
              gettimeofday ( & currentTime, 0 );
              timersub ( & currentTime, & startTime, & duration );
              if ( duration.tv_sec > 60 ) {
                kill ( pid, 9 );
                cerr << "qpid-perftest pid " << pid << " hanging: killed.\n";
                return false;
              }
          }
        }
                       
    }
}



bool
allBrokersAreAlive ( brokerVector & brokers ) {
    for ( unsigned int i = 0; i < brokers.size(); ++ i )
        if ( ! brokers[i]->isRunning() ) 
	    return false;

    return true;
}





void
killAllBrokers ( brokerVector & brokers ) {
    for ( unsigned int i = 0; i < brokers.size(); ++ i ) {
        brokers[i]->kill ( 9 );
    }
}




void
killOneBroker ( brokerVector & brokers ) {
    int doomedBroker = getpid() % brokers.size();
    cout << "Killing broker " << brokers[doomedBroker]->getPID() << endl;
    brokers[doomedBroker]->kill ( 9 );
    sleep ( 2 );
}




}} // namespace qpid::tests

using namespace qpid::tests;



/*
 *  Please note that this test has self-test capability.
 *  It is intended to detect 
 *    1. perftest hangs.
 *    2. broker deaths
 *  Both of these condtions can be forced when running manually
 *  to ensure that the test really does detect them.
 *  See command-line arguments 3 and 4.
 */
int
main ( int argc, char ** argv )
{
    // I need the SASL_PATH_TYPE_CONFIG feature, which did not appear until SASL 2.1.22
#if (SASL_VERSION_FULL < ((2<<16)|(1<<8)|22))
    cout << "Skipping SASL test, SASL version too low." << endl;
    return 0;
#endif

    int n_iterations = argc > 1 ? atoi(argv[1]) : 1;
        runSilent    = argc > 2 ? atoi(argv[2]) : 1;  // default to silent
    int killBroker   = argc > 3 ? atoi(argv[3]) : 0;  // Force the kill of one broker.
    int hangTest     = argc > 4 ? atoi(argv[4]) : 0;  // Force the first perftest to hang.
    int n_brokers = 3;
    brokerVector brokers;

    srand ( getpid() );
    string clusterName;
    makeClusterName ( clusterName );
    for ( int i = 0; i < n_brokers; ++ i ) {
        startBroker ( brokers, i, clusterName );
    }

    sleep ( 3 );

    /* Run all qpid-perftest iterations, and only then check for brokers
     * still being up.  If you just want a quick check for the failure 
     * mode in which a single iteration would kill all brokers except 
     * the client-connected one, just run it with the iterations arg
     * set to 1.
    */
    for ( int iteration = 0; iteration < n_iterations; ++ iteration ) {
        if ( ! runPerftest ( hangTest ) ) {
	    if ( ! runSilent )
	        cerr << "qpid-perftest " << iteration << " failed.\n";
            return 1;
        }
        if ( ! ( iteration % 10 ) ) {
	    if ( ! runSilent )
                cerr << "qpid-perftest " << iteration << " complete. -------------- \n";
        }
    }
    if ( ! runSilent )
        cerr << "\nqpid-perftest " << n_iterations << " iterations complete. -------------- \n\n";

    /* If the command-line tells us to kill a broker, do
     * it now.  Use this option to prove that this test
     * really can detect broker-deaths.
     */
    if ( killBroker ) {
        killOneBroker ( brokers );
    }

    if ( ! allBrokersAreAlive ( brokers ) ) {
        if ( ! runSilent ) 
            cerr << "not all brokers are alive.\n";
	killAllBrokers ( brokers );
        return 2;
    }

    killAllBrokers ( brokers );
    if ( ! runSilent )
        cout << "success.\n";

    return 0;
}