This file is indexed.

/usr/sbin/ocft is in resource-agents 1:3.9.2-5ubuntu4.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
#!/bin/bash

# Copyright (c) 2010 Novell Inc, John Shi
#           All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like.  Any license provided herein, whether implied or
# otherwise, applies only to this software file.  Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.

die()
{
  local str
  str="$1"

  echo "ERROR: $str" >&2
  exit 1
}

warn()
{
  local str
  str="$1"

  echo "WARNING: $str" >&2
}

parse_die()
{
  local str
  str="$1"

  die "${agent}: line ${line_num}: ${str}"
}

# add quotes to string for Here Documents
add_quotes()
{
  local typ str a b
  typ="$1"
  str="$2"

  case "$typ" in
    1) a=\'; b=\";;
    2) a=\"; b=\';;
  esac

  echo "$str" | sed "s/$a/$a$b$a$b$a/g; 1 s/^/$a/; $ s/$/$a/"
}

# split strings
explode()
{
  local str
  str="$1"

  echo "$str" | awk -F'"' '{
    if (NF > 0 && NF%2 == 0)
      exit(1);
    for (i=1; i<=NF; i++) {
      if (i%2 == 0)
        print $i;
      else {
        len = split($i, str, /[ \t]+/);
        for (j=1; j<=len; j++) {
          sb = sub(/#.*/, "", str[j]);
          if (str[j] != "")
            print str[j];
          if (sb)
            exit(0);
        }
      }
    }
  }'
}

# phase 1: parse the string to 'command' and 'argument collection'.
line2trunk()
{
  trunk[0]="${line%%[[:blank:]]*}"
  trunk[1]="${line#*[[:blank:]]}"
}

# phase 2: split the argument collection.
trunk2branch()
{
  local IFS

  # Some of statements need one parameter at least.
  if [ "$line" = "${trunk[0]}" ]; then
    parse_die "missing parameter."
  fi

  IFS=$'\n'
  branch=($(explode "${trunk[1]}"))
  if [ $? -ne 0 ]; then
    parse_die "missing '\"'."
  fi
}

preparse_cfg()
{
  local agent line trunk branch macro num host
  agent="$1"

  if [ ! -r "$opt_cfgsdir/$agent" ]; then
    die "${agent}: configuration file not found."
  fi

  rm -f $CASES_DIR/${agent}_macro.*
  rm -f $CASES_DIR/${agent}.preparse

  line_num=0
  while read -r line; do
    let line_num++
    num=" $line_num"

    case "$line" in
      ""|\#*) continue;;
    esac

    line2trunk
    case "${trunk[0]}" in
      CASE-BLOCK)
        trunk2branch
        macro="$CASES_DIR/${agent}_macro.${branch[0]}"
        continue
        ;;
      Include|Include@*)
        host=$(echo "${trunk[0]}" | awk -F@ '{print $2}')
        trunk2branch
        if [ ! -r "$CASES_DIR/${agent}_macro.${branch[0]}" ]; then
          parse_die "Macro '${branch[0]}' not found."
        fi
        if [ -n "$host" ]; then
          line="$(sed -e 's/^\([^[:blank:]]*\)@[^[:blank:]]*/\1/' -e "s/^[^[:blank:]]*/&@$host/" "$CASES_DIR/${agent}_macro.${branch[0]}")"
        else
          line="$(<"$CASES_DIR/${agent}_macro.${branch[0]}")"
        fi
        num=
        ;;
      *[!A-Z-]*)
        :
        ;;
      *)
        macro=
        ;;
    esac

    if [ -n "$macro" ]; then
      if ! touch "$macro"; then
        die "No permission to create macro file: ${macro}."
      fi
      echo "$line$num" >>"$macro"
    else
      echo "$line$num" >>"$CASES_DIR/${agent}.preparse"
    fi
  done <"$opt_cfgsdir/$agent"

  rm -f $CASES_DIR/${agent}_macro.*
}

case_finish()
{
  local host

  if [ -n "$sh" ]; then
    cat >>$sh <<EOF
if [ -n "\$OCFT_VERBOSE" ]; then
  echo
fi
# Clean up and exit
EOF
    for host in $hosts; do
      echo "backbash_stop $host" >>$sh
    done
    echo "quit 0" >>$sh
  fi
  atexit_num=0
  hosts=
  sh=
}

parse_cfg()
{
  local agents i line stat sh trunk branch atexit_num host hosts

  if [ $# -eq 0 ]; then
    agents=($opt_cfgsdir/*)
  else
    agents=("$@")
  fi

  for agent in "${agents[@]}"; do
    i=0
    agent="$(basename "$agent")"
    rm -f $CASES_DIR/*_${agent}.sh
    rm -f $CASES_DIR/${agent}_setup

    echo "Making '$agent': "
    preparse_cfg "$agent"
    while read -r line; do
      line_num="${line##* }"
      line="${line% *}"
      line2trunk

      # state switch
      case "${trunk[0]}" in
        CONFIG)
          case_finish
          stat=1
          continue
          ;;
	SETUP-AGENT)
          case_finish
          stat=2
          continue
          ;;
        CASE)
          case_finish
          trunk2branch
          sh="$CASES_DIR/${i}_${agent}.sh"
          echo "    - case ${i}: ${branch[0]}"
          cat >$sh <<EOF
#!/bin/bash

# Agent:   $agent
# Summary: ${branch[0]}

. $OCFT_DIR/caselib || {
  echo "ERROR: '$OCFT_DIR/caselib' not found."
  exit 2
}

MYROOT="$cfg_agent_root"
EOF
          if [ -n "$cfg_install_package" ]; then
            cat >>$sh <<EOF
agent_install ${cfg_install_package[@]}
EOF
          fi
          if [ -r "$CASES_DIR/${agent}_setup" ]; then
            cat >>$sh <<EOF
agent_setup $agent <<'CMD'
$(cat "$CASES_DIR/${agent}_setup")
CMD
EOF
          fi
          cat >>$sh <<EOF
if [ -n "\$OCFT_VERBOSE" ]; then
  echo -e $(add_quotes 1 "Starting '\\033[33m${agent}\\033[0m' case $i '\\033[33m${branch[0]}\\033[0m':")
else
  echo -n "'${agent}' case ${i}: "
fi
EOF
          chmod a+x $sh
          let i++
          stat=3
          continue
          ;;
      esac

      case "$stat" in
        1)
          case "${trunk[0]}" in
            AgentRoot)
              trunk2branch
              cfg_agent_root="${branch[0]}"
              ;;
            InstallPackage)
              trunk2branch
              cfg_install_package=("${branch[@]}")
              ;;
            HangTimeout)
              trunk2branch
              if ! echo "${branch[0]}" | grep -qxE '[0-9]+'; then
                parse_die "numeric argument required."
              fi
              cfg_hang_timeout="${branch[0]}"
              ;;
            *)
              parse_die "unimplemented statement: ${trunk[0]}"
              ;;
          esac
          ;;
        2)
          echo "$line" >>$CASES_DIR/${agent}_setup
          ;;
        3)
          host=$(echo ${trunk[0]} | awk -F@ '{print $2}')
          if [ -n "$host" ]; then
            if ! echo "$hosts" | grep -q "$host"; then
              hosts=$hosts$'\n'$host
              cat >>$sh <<EOF
# Initialize remote shell
backbash_start $host
backbash $host <<CMD
OCFT_VERBOSE=\$OCFT_VERBOSE
showhost="${host}: "
CMD
backbash $host <$OCFT_DIR/caselib
backbash $host <<'CMD'
MYROOT="$cfg_agent_root"
EOF
              if [ -n "$cfg_install_package" ]; then
                cat >>$sh <<EOF
agent_install ${cfg_install_package[@]}
EOF
              fi
              if [ -r "$CASES_DIR/${agent}_setup" ]; then
                cat >>$sh <<EOF
agent_setup $agent <<'END'
$(cat "$CASES_DIR/${agent}_setup")
END
EOF
              fi
              echo "CMD" >>$sh
            fi
          fi

          echo "
# CASE statement: $line" >>$sh

          if [ -n "$host" ]; then
            echo "backbash $host <<'CMD'" >>$sh
          fi

          case "${trunk[0]}" in
            Var|Var@*)
              cat >>$sh <<EOF
if [ -n "\$OCFT_VERBOSE" ]; then
  echo $(add_quotes 2 "    \${showhost}Setting agent environment:    export ${trunk[1]}")
fi
export ${trunk[1]}
check_success \$? $(add_quotes 1 "export ${trunk[1]}")
EOF
              ;;
            Unvar|Unvar@*)
              cat >>$sh <<EOF
if [ -n "\$OCFT_VERBOSE" ]; then
  echo $(add_quotes 2 "    \${showhost}Removing agent environment:   unset ${trunk[1]}")
fi
unset ${trunk[1]}
check_success \$? $(add_quotes 1 "unset ${trunk[1]}")
EOF
              ;;
            AgentRun|AgentRun@*)
              trunk2branch
              if [ -z "${branch[1]}" ]; then
                if [ "${branch[0]}" = "start" ]; then
                  cat >>$sh <<EOF
agent_run $(add_quotes 1 "$agent") status $cfg_hang_timeout
rc=\$?
if [ \$rc -eq \$OCF_ERR_UNIMPLEMENTED ]; then
  agent_run $(add_quotes 1 "$agent") monitor $cfg_hang_timeout
  rc=\$?
fi

if [ \$rc -eq \$OCF_SUCCESS ]; then
  : #The status I want, so I can do nothing.
elif [ \$rc -eq \$OCF_NOT_RUNNING ]; then
  if [ -n "\$OCFT_VERBOSE" ]; then
    echo $(add_quotes 2 "    \${showhost}Running agent:                ./$agent start")
  fi
  agent_run $(add_quotes 1 "$agent") start $cfg_hang_timeout
  check_success \$? $(add_quotes 1 "./$agent start")
else
  check_success \$rc $(add_quotes 1 "./$agent status|monitor")
fi
EOF
                elif [ "${branch[0]}" = "stop" ]; then
                  cat >>$sh <<EOF
agent_run $(add_quotes 1 "$agent") status $cfg_hang_timeout
rc=\$?
if [ \$rc -eq \$OCF_ERR_UNIMPLEMENTED ]; then
  agent_run $(add_quotes 1 "$agent") monitor $cfg_hang_timeout
  rc=\$?
fi

if [ \$rc -eq \$OCF_NOT_RUNNING ]; then
  : #The status I want, so I can do nothing.
elif [ \$rc -eq \$OCF_SUCCESS ]; then
  if [ -n "\$OCFT_VERBOSE" ]; then
    echo $(add_quotes 2 "    \${showhost}Running agent:                ./$agent stop")
  fi
  agent_run $(add_quotes 1 "$agent") stop $cfg_hang_timeout
  check_success \$? $(add_quotes 1 "./$agent stop")
else
  check_success \$rc $(add_quotes 1 "./$agent status|monitor")
fi
EOF
                elif [ "${branch[0]}" = "status" -o "${branch[0]}" = "monitor" ]; then
                  cat >>$sh <<EOF
agent_run $(add_quotes 1 "$agent") $(add_quotes 1 "${branch[0]}") $cfg_hang_timeout
EOF
                else
                  cat >>$sh <<EOF
agent_run $(add_quotes 1 "$agent") $(add_quotes 1 "${branch[0]}") $cfg_hang_timeout
check_success \$? $(add_quotes 1 "./$agent ${branch[0]}")
EOF
                fi
              else
                cat >>$sh <<EOF
test -n $(add_quotes 2 "\$${branch[1]}")
check_success \$? $(add_quotes 1 "test -n \"\$${branch[1]}\"")
if [ -n "\$OCFT_VERBOSE" ]; then
  echo $(add_quotes 2 "    \${showhost}Running agent:                ./$agent ${branch[0]}")
fi
agent_run $(add_quotes 1 "$agent") $(add_quotes 1 "${branch[0]}") $cfg_hang_timeout
ret=\$?
if [ -n "\$OCFT_VERBOSE" ]; then
  echo -n "    \${showhost}Checking return value:"
fi
if [ -n "\${retval[ret]}" ]; then
  retstr="\${retval[ret]}"
else
  retstr=\$ret
fi
if [ \$ret -eq \$${branch[1]} ]; then
  if [ -n "\$OCFT_VERBOSE" ]; then
    echo -e $(add_quotes 2 "        \\033[32mOK\\033[0m. The return value '\\033[34m\$retstr\\033[0m' == '\\033[34m${branch[1]}\\033[0m'")
  else
    echo -e "\t\\033[32mOK\\033[0m."
  fi
else
  if [ -n "\$OCFT_VERBOSE" ]; then
    echo -en $(add_quotes 2 "        \\033[31mFAILED\\033[0m. The return value '\\033[34m\$retstr\\033[0m' != '\\033[34m${branch[1]}\\033[0m'. ")
  else
    echo -en "\t\\033[31mFAILED\\033[0m. Agent returns unexpected value: '\$retstr'. "
  fi
  echo "See details below:"
  cat /tmp/.ocft_runlog
  echo
  quit 1
fi
EOF
              fi
              ;;
            Bash|Bash@*)
              cat >>$sh <<EOF
if [ -n "\$OCFT_VERBOSE" ]; then
  echo $(add_quotes 2 "    \${showhost}Setting system environment:   ${trunk[1]}")
fi
echo $(add_quotes 1 "${trunk[1]}") | /bin/bash
check_success \$? $(add_quotes 1 "${trunk[1]}")
EOF
              ;;
            BashAtExit|BashAtExit@*)
              let atexit_num++
              cat >>$sh <<EOF
atexit${atexit_num}()
{
  if [ -n "\$OCFT_VERBOSE" ]; then
    echo $(add_quotes 2 "    \${showhost}Setting system environment:   ${trunk[1]}")
  fi
  echo $(add_quotes 1 "${trunk[1]}") | /bin/bash
}
let atexit_num++
EOF
              ;;
            *)
              parse_die "unimplemented statement: ${trunk[0]}"
              ;;
          esac
          if [ -n "$host" ]; then
            echo 'CMD' >>$sh
          fi
          ;;
        *)
          parse_die "unimplemented statement: ${trunk[0]}"
          ;;
      esac
    done <$CASES_DIR/${agent}.preparse
    rm -f $CASES_DIR/${agent}.preparse
    rm -f $CASES_DIR/${agent}_setup
    case_finish
  done
}

start_test()
{
  local sh shs agents line ret

  if ! cd $CASES_DIR >/dev/null 2>&1; then
    die "cases directory not found."
  fi

  export OCFT_VERBOSE=$opt_verbose

  if [ $# -eq 0 ]; then
    agents=($(ls -1 *.sh 2>/dev/null | sed 's/.*_\([^_]*\)\.sh$/\1/' | sort | uniq))
  else
    agents=("$@")
  fi

  for shs in "${agents[@]}"; do
    for sh in $(ls -1 *_${shs}.sh 2>/dev/null | sort -n); do
      ./$sh
      ret=$?
      if [ $ret -eq 3 ]; then
        die "core function failed, break all tests."
      fi
      if [ $ret -eq 2 ]; then
        warn "core function failed, break all tests of '$shs'."
        break
      fi
    done
  done | while read -r line; do
    echo "$line"
    echo "$(date '+%F %T'): $line" | cat -A |
    sed -r 's/\^\[\[[0-9]+m|\^I|.$//g' >>ocft.log
  done
}

delete_cases()
{
  local shs

  if [ $# -eq 0 ]; then
    rm -f $CASES_DIR/*.sh
  else
    for shs in "$@"; do
      rm -f $CASES_DIR/*_${shs}.sh
    done
  fi
}

usage()
{
  cat <<EOF
$0 ACTION [OPTION] [agent1 [agent2] [...]]
ACTIONs include:
     make [-d dir]   Generate the testing shell scripts.
                       -d  The directory that contains 
		           configuration of cases.
     test [-v]       Execute the testing shell scripts.
                       -v  Verbose output mode.
     clean           Delete the testing shell scripts.
     help [-v]       Show this help and exit.
                       -v  Show HOWTO and exit.
Version 0.42
See '$OCFT_DIR/README' for detail.
EOF
}

howto()
{
  cat <<EOF
HOW TO USE THIS TOOL

  - Ocft is a testing tool for resource agents. Instead of the policy of HA,
    it mainly concerns whether resource agents run correct locally. It can 
    design types of complicated environments to test the reliability of 
    resource agents. Precisely, it is to display whether resource agents can 
    return to correct or expected value. The advantage of the tool provides 
    us with competence to design conditions which can be recorded or reproduced. 
    Hence it is useful to debuggers.

* Components
    ** Test case generator (/usr/sbin/ocft)
      - Turning configuration files of test case to executable scripts.

    ** Configuration file  ($CONFIGS_DIR/)
      - Every configuration file directs only one resource agent and share the same 
        name with resource agent but contains more test cases.

    ** The testing script  ($CASES_DIR/)
      - After the generator reads configuration files and generates many testing 
        scripts and the script is underway, the test begins.

* How to customize the environment of testing
  - Ocft designs the running conditions through two ways, one is changing the 
    environment variables of resource agents (it is the interface left by OCF itself), 
    the other is modifying the OS environment of resource agents, such as altering 
    the permission of some key file or IP address of the machine.

* How to test
  - Firstly, you need to sketch the all complex and uncommon environments against 
    a certain resource agent and keep in mind what consequences may be caused by 
    these uncommon environments. 
    Secondly, write the designed conditions and foreknown consequences into 
    configuration files, and then run the generator to translate the test case to 
    executable scripts. 
    Finally, you need running these scripts to observe the output and learn 
    the running status of each test case, which will compares the predicated result 
    with the actual one. If they differ, you will be able to find the bugs of the 
    resource agent.
EOF
}


export LANG=C

# system variable
OCFT_DIR=/usr/share/resource-agents/ocft
CONFIGS_DIR=/usr/share/resource-agents/ocft/configs
CASES_DIR=/var/lib/resource-agents/ocft/cases

# global variable
agent=
line_num=

# default configuration
cfg_agent_root=
cfg_install_package=
cfg_hang_timeout=20

# default option
opt_verbose=
opt_cfgsdir=$CONFIGS_DIR

command="$1"
shift

case "$command" in
  make)
    if [ "$1" = "-d" ]; then
      if [ ! -d "$2" ]; then
        usage
        exit 1
      fi
      opt_cfgsdir="$2"
      shift 2
    fi
    if [ ! -d "$CASES_DIR" ]; then
      mkdir -p "$CASES_DIR" || die "Can not create directory: ${CASES_DIR}."
    fi
    parse_cfg "$@"
    ;;
  test)
    if [ "$1" = "-v" ]; then
      opt_verbose=1
      shift
    fi
    start_test "$@"
    ;;
  clean)
    delete_cases "$@"
    ;;
  help)
    if [ "$1" = "-v" ]; then
      howto
    else
      usage
    fi
    exit 0
    ;;
  *)
    usage
    exit 1
    ;;
esac