mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

andrug
18.08.2008 35b6788e5bf43e61b0fb9b3b00558e098eee6f6a
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE stax SYSTEM "../../../shared/stax.dtd">
<!--
 ! CDDL HEADER START
 !
 ! The contents of this file are subject to the terms of the
 ! Common Development and Distribution License, Version 1.0 only
 ! (the "License").  You may not use this file except in compliance
 ! with the License.
 !
 ! You can obtain a copy of the license at
 ! trunk/opends/resource/legal-notices/OpenDS.LICENSE
 ! or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 ! See the License for the specific language governing permissions
 ! and limitations under the License.
 !
 ! When distributing Covered Code, include this CDDL HEADER in each
 ! file and include the License file at
 ! trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 ! add the following below this CDDL HEADER, with the fields enclosed
 ! by brackets "[]" replaced with your own identifying information:
 !      Portions Copyright [yyyy] [name of copyright owner]
 !
 ! CDDL HEADER END
 !
 !      Copyright 2008 Sun Microsystems, Inc.
 ! -->
<stax>
 
  <defaultcall function="scheduler"/>
 
 
  <!-- ************************************************************ -->
  <function name="scheduler" scope="local">
    <function-map-args>
      <function-arg-def name="instances" type="required">
        <function-arg-description>
          instance object
        </function-arg-description>
      </function-arg-def>
      <function-arg-def name="suffix" type="required">
        <function-arg-description>
          suffix object
        </function-arg-description>
      </function-arg-def>
      <function-arg-def name="scheduler" type="required">
        <function-arg-description>
          scheduler object
        </function-arg-description>
      </function-arg-def>
      <function-arg-def name="scenario" type="required">
        <function-arg-description>
          scenario object
        </function-arg-description>
      </function-arg-def>
    </function-map-args>
    
    <sequence>
      <!--==========  Call preambule  =========-->
      <script>
        tagAttr = []
        durationValue = '%s%s' % \
                        (scenario.getDurationTime(),scenario.getDurationUnit())
        tagAttr.append(['duration',durationValue])
      </script>
      <call function="'phasePreamble'">
      { 'phaseName' : 'scheduler' ,
        'tagAttr'   : tagAttr,
        'fileFd'    : LOG_MAIN_FD }
      </call>
      
      <!--==== Get the clients list from all modules ==============-->
      <script>
        clients = []
        for m in scheduler:
          if (m.getEnabled() == "true"):
            clients.extend(m.getClients())
      </script>
      
      
      <!--==== Copy client data into client host ====-->
      <call function="'copyClients'">
      { 
        'clients'    : clients
      }
      </call>
      <script>
        MachineList = STAXResult
      </script>
      <if expr="ERR_NUM[0] == 0">
        <sequence>
          <!--==== Start the system test and launch client's process ====-->
          <call function="'runClients'">
          { 
            'clients'   : clients,
            'instances' : instances,
            'scenario'  : scenario
          }
          </call>
          <!--==========  Merge files    ==========-->
          <call function="'mergeClientsFiles'">
          {
            'scheduler' : scheduler,
            'fileFd'    : LOG_MAIN_FD
          }
          </call>
        </sequence>
      </if>
      <!--==========  Call postphase =========-->
      <call function="'phasePostamble'">
      {
        'phaseName' : 'scheduler',
        'fileFd'    : LOG_MAIN_FD
      }
      </call>
    </sequence>
  </function>
 
 
 
  <!-- ************************************************************ -->
  <function name="copyClients" scope="local">
    <function-map-args>
      <function-arg-def name="clients" type="required">
      </function-arg-def>
    </function-map-args>
    
    <sequence>
      <!--==========  Define variables  =========-->
      <script>
        fileList = STAXGlobal([])
      </script>
      <!--==== Get the list of hosts for each client ====-->
      <script>
        list = NOT_DEFINED
        
        # for each clients get from conf.xml file
        for client in clients:          
          if list == NOT_DEFINED:
            list = '%s' % client.getHost()
          elif list.find(client.getHost()) == -1:
            list = '%s,%s' % (list,client.getHost())
        
        list = list.split(",")
      </script>
      <!--==========  Copy directory in parallel   ==========-->
      <paralleliterate in="list" var="host">
        <sequence>
          <if expr="host != NOT_DEFINED">
            <sequence>
              <!--==== open logfile ====-->
               <call function="'getLogFileName'">
                {
                  'type'   : 'host',
                  'object' : host,
                  'prefix' : 'client_copy'
                }
              </call>
              <script>
                logFile = STAXResult
                fileList.append(logFile)
                fileFd = open(logFile,'w')
              </script>
              <!--==== copy directory ====-->
              <script>
                fullHostName = '%s%s' % (host,DOMAIN[0])
              </script>
              <call function="'copyFolder'">
                {
                  'remoteHost'    : fullHostName,
                  'fromDirectory' : '%s/clients' % TESTS_DIR,
                  'toDirectory'   : '%s/clients' % LOG_DIR,
                  'fileFd'        : fileFd
                }
              </call>
              <!--==== close logfile ====-->
              <script>
                fileFd.close()
                fileFd = ''
              </script>
            </sequence>
          </if>
        </sequence>
      </paralleliterate>
      <!--==========  Merge files    ==========-->
      <call function="'mergeFiles'">
        {
          'fileList' : fileList,
          'fileFd'   : LOG_MAIN_FD
        }
      </call>
      <return>list</return>
    </sequence>
  </function>
  
  
  
  <!-- ************************************************************ -->
  <function name="runClients" scope="local">
    <function-map-args>
      <function-arg-def name="clients" type="required">
      </function-arg-def>
      <function-arg-def name="instances" type="required">
      </function-arg-def>
      <function-arg-def name="scenario" type="required">
      </function-arg-def>
    </function-map-args>
    
    <sequence>
      <!--==========  Define variables  =========-->
      <script>
        fileList = STAXGlobal([])
        finishedClients = STAXGlobal([])
      </script>
      
      
      <!--==========  Run clients in parallel  =========-->
      <paralleliterate in="clients" var="client">
        <sequence>
        
        <!--==== calculate start/stop time, open dedicated logfile ====-->
          <script>
            start = client.getStart()
            stop = client.getStop()
            dependency = client.getDependency()
            
            if stop == NOT_DEFINED:
              stop = 't100'
            
            # convert durationTime in second
            if scenario.getDurationUnit() == 's':
              durationTime = int(scenario.getDurationTime())
            elif scenario.getDurationUnit() == 'm':
              durationTime = int(scenario.getDurationTime()) * 60
            elif scenario.getDurationUnit() == 'h':
              durationTime = int(scenario.getDurationTime()) * 3600
            elif scenario.getDurationUnit() == 'd':
              durationTime = int(scenario.getDurationTime()) * 24 * 3600
            
            
            if (start != NOT_DEFINED and stop != NOT_DEFINED):
              # remove t for t10, t20...
              start = start.split('t')[1]
              stop = stop.split('t')[1]
              
              start = int(start) * int(durationTime) / 100
              startms = int(start) * 1000
              stop = int(stop) * int(durationTime) / 100
              clientDuration = stop - start
          </script>
          <!--==== open logfile ====-->
           <call function="'getLogFileName'">
            {
              'type'   : 'client',
              'object' : client,
              'prefix' : 'client'
            }
          </call>
          <script>
            logFile = STAXResult
            fileList.append(logFile)
            fileFd = open(logFile,'w')
            
            # Write start tag
            str = '\n&lt;client name=\"%s\"' % client.getName()
            str = '%s host=\"%s\"'           % (str,client.getHost())
            str = '%s start=\"%s\"'          % (str,client.getStart())
            str = '%s stop=\"%s\"'           % (str,client.getStop())
            str = '%s dependency=\"%s\"&gt;\n' % (str,client.getDependency())
            fileFd.write(str)
          </script>
          
          
          <!--== Start is NOT defined in client attribute ==-->
          <if expr="start == NOT_DEFINED">
            <sequence>
              <if expr="dependency == NOT_DEFINED">
                <sequence>
                  <script>
                    str = 'ERROR: client %s should have' % client.getName()
                    str = '%s start or dependency attribute defined' % str
                  </script>
                  <message> str </message>
                  <call function="'writeMessage'">
                  { 'fileFd'  : fileFd,
                    'content' : str
                  }
                  </call>
                  <script>ERR_NUM[0] += 1</script>
                </sequence>
              <else>
                <sequence>
                  <!--== Start is NOT defined, dependency is defined ==-->
                  <call function="'sleepForDependency'">
                  {
                    'client'       : client,
                    'clients'      : clients,
                    'durationTime' : durationTime,
                    'fileFd'       : fileFd
                  }
                  </call>
                </sequence>
              </else>
              </if>
            </sequence>
          
          <!--== Start is defined in client attribute ==-->
          <else>
            <sequence>
              <!--=== Sleep and wait to start the client when specified ===-->
              <call function="'sleep'">
              { 'location' : STAXServiceMachine,
                'sleepForMilliSeconds' : startms,
                'fileFd'   : fileFd
              }
              </call>
              
              
              <!--=== Wait if client depends on other clients ===-->
              <if expr="dependency != NOT_DEFINED">
                <sequence>
                  <call function="'sleepForDependency'">
                  {
                    'client'       : client,
                    'clients'      : clients,
                    'durationTime' : durationTime,
                    'fileFd'       : fileFd
                  }
                  </call>
                </sequence>
              </if>
            </sequence>
          </else>
          </if>
          
          
          <!--== Run the client ==-->
          <call function="'runClient'">
          {
            'client'    : client,
            'duration'  : clientDuration,
            'instances' : instances,
            'fileFd'    : fileFd
          }
          </call>
          
          
          <!--==== close logfile ====-->
          <script>
            fileFd.write('&lt;/client&gt;\n')
            fileFd.close()
            fileFd = ''
          </script>
        </sequence>
      </paralleliterate>
      
    </sequence>
  </function>
 
 
 
  <!-- ************************************************************ -->
  <function name="runClient" scope="local">
    <function-map-args>
      <function-arg-def name="client" type="required">
      </function-arg-def>
      <function-arg-def name="duration" type="required">
      </function-arg-def>
      <function-arg-def name="instances" type="required">
      </function-arg-def>
      <function-arg-def name="fileFd" type="required">
      </function-arg-def>
    </function-map-args>
    
    <sequence>
      
      <script>
        startTime=strftime("%Y%m%d@%H:%M:%S",localtime())
      </script>
      
      <!--=== log info ===-->
      <if expr="client.getHost() == NOT_DEFINED">
        <sequence>
          <script>
            str = '++ Start client %s'         % (client.getName())
            str = '%s (id=%s)'                 % (str, client.getId())
            str = '%s at %s'                   % (str, startTime)
            str = '%s, max duration is %s sec' % (str, duration)
          </script>
          <message> str </message>
          <call function="'writeMessage'">
          { 'fileFd'  : fileFd,
            'content' : str
          }
          </call>
        </sequence>
      <else>
        <sequence>
          <script>
            str = '++ Start client %s'         % (client.getName())
            str = '%s (id=%s)'                 % (str, client.getId())
            str = '%s running on %s'           % (str, client.getHost())
            str = '%s at %s'                   % (str, startTime)
            str = '%s, max duration is %s sec' % (str, duration)
          </script>
          <message> str </message>
          <call function="'writeMessage'">
          { 'fileFd'  : fileFd,
            'content' : str
          }
          </call>
        </sequence>
      </else>
      </if>
      
      <!--=== run the client, exit if time exceed duration time ===-->
      <script>
        timerKilled = TRUE
        clientXmlFile = '%s/clients/%s/%s.xml' % \
                        (TESTS_DIR,client.getName(),client.getName())
      </script>
      <timer duration="'%ss' % duration">
        <sequence>
          <!-- check if client exists before launching it-->
          <call function="'isFile'">
          {
            'location' : STAXServiceMachine,
            'fileName' : clientXmlFile
          }
          </call>
          <script>
            fileExist = STAXResult
          </script>
          <if expr="fileExist == TRUE">
            <sequence>
              <import machine="'%s' % (STAF_LOCAL_HOSTNAME)"
                      file="'%s' % clientXmlFile"/>
              <call function="'%s' % client.getName()">
              { 
                'client'     : client,
                'instances'  : instances,
                'duration'   : duration,
                'fileFd'     : fileFd
              }
              </call>
              <script>
                errNum = STAXResult
                timerKilled = FALSE
              </script>
            </sequence>
          <else>
            <message>'ERROR : cant find file %s' % clientXmlFile </message>
          </else>
          </if>
        </sequence>
      
      </timer>
      <if expr="timerKilled == TRUE">
        <call function="'writeEndTagOperation'">{'fileFd'  : fileFd}</call>
      </if>
      
      <script>
        finishedClients.append(client.getId())
      </script>
      
      <!--=== log info ===-->
      <script>
        stopTime=strftime("%Y%m%d@%H:%M:%S",localtime())
      </script>
      <message>
        '-- Stop %s client (id %s) running on %s at %s' %\
        (client.getName(),client.getId(),client.getHost(),stopTime)
      </message>
      <call function="'writeMessage'">
        {
          'content' : 'Stop at %s' % stopTime,
          'fileFd'  : fileFd
        }
      </call>
      
      <if expr="errNum == 0">
        <script>
          client.setResult('SUCCESS')
          fileFd.write('\n&lt;clientResult status=\"SUCCESS\"/&gt;\n')
        </script>
      <else>
        <script>
          client.setResult('FAIL')
          fileFd.write('\n&lt;clientResult status=\"FAIL\"/&gt;\n')
        </script>
      </else>
      </if>
      <!-- DO NOT record number of error(s) found by clients         -->
      <!-- each should use checkRC and ERR_NUM[0] is already updated -->
      <!-- <script>
        ERR_NUM[0] += errNum
      </script> -->
      
    </sequence>
  </function>
 
 
  <!-- ************************************************************ -->
  <function name="sleepForDependency" scope="local">
    <function-map-args>
      <function-arg-def name="location" type="optional" 
                        default="'%s' % STAXServiceMachine">
      </function-arg-def>
      <function-arg-def name="client" type="required">
      </function-arg-def>
      <function-arg-def name="clients" type="required">
      </function-arg-def>
      <function-arg-def name="durationTime" type="required">
      </function-arg-def>
      <function-arg-def name="fileFd" type="required">
      </function-arg-def>
    </function-map-args>
    
    <sequence>
      <!--== Convert dependencyId attribute value into a list ==-->
      <script>
        clientOfClientIds = []
        clientOfClientIdsTmp = client.getDependency()
        
        # if id separated by coma, make the list
        if clientOfClientIdsTmp.count(',') > 0:
          clientOfClientIds = clientOfClientIdsTmp.split(',')
        
        # if id separated by whitespace, make the list
        elif clientOfClientIdsTmp.count(' ') > 0:
          clientOfClientIds = clientOfClientIdsTmp.split(' ')
        
        # if id separated by ';', make the list
        elif clientOfClientIdsTmp.count(';') > 0:
          clientOfClientIds = clientOfClientIdsTmp.split(';')
        
        # sounds like only one id
        else:
          clientOfClientIds.append(clientOfClientIdsTmp)
        
        # remove whitespace characters
        i=0
        while i &lt; len(clientOfClientIds):
          clientOfClientIds[i] = clientOfClientIds[i].replace(' ','')
          i += 1
      </script>
      
      <!--== For each clientId, wait it has been completed ==-->
      <iterate in="clientOfClientIds" var="clientOfClientId">
        <sequence>
          
          <!--== Wait until clientOfClient is finished ==-->
          <!--== TBD : should not be durationTime but  ==-->
          <!--== "durationTime - time already runed"   ==-->
          <timer duration="'%ss' % durationTime">
            <loop until="clientOfClientId in finishedClients">
              <sequence>
                <call function="'sleep'">
                {
                  'location'             : location,
                  'sleepForMilliSeconds' : '3000',
                  'silentMode'           : FALSE ,
                  'fileFd'               : fileFd
                }
                </call>
              </sequence>
            </loop>
          </timer>
        </sequence>
        
      </iterate>
    </sequence>
  </function>
  
  
</stax>