0

私は完全に初心者です。

文字列「Ok」の出現回数をカウントし、次の出力からすべての出現に対してアクションを実行するテスト ケースの期待スクリプトを作成しています。

Reloading configuration on all nodes
Reloading configuration on node 1 (node1-c5)
OK
Reloading configuration on node 2 (node2-c5)
OK
Reloading configuration on node 3 (node3-c5)
OK
Reloading configuration on node 4 (node4-c5)
OK

私の期待ブロックはどのように見えるでしょうか?

4

2 に答える 2

3

whileループを削除するためにコードを書き直します。

set number_ok 0
set node_patt "(Reloading configuration on node (\\d+?) \\((\[^)]+?)\\))\r\n(.+?)\r\n"

send "cluster config -r -a\r"
expect {
    ERROR {cluster_exit 1}
    -re $node_patt {
        set line "<$cmdline> $expect_out(1,string)"
        set node_num  $expect_out(2,string)
        set node_name $expect_out(3,string)
        set result    $expect_out(4,string)

        switch -regexp $result {
            "Node .+ not found" {
                ok 0 "$line (not found)"
            }
            "Node .+ not responding, skipped" {
                ok 0 "$line (not responding)"
            }
            OK {
                ok 1 $line
                incr number_ok
            }
        }
        exp_continue   ;# loop back to top of expect block
    }
    $ROOT_PROMPT  ;# no action, so fall out of the expect block
}

Tcl 正規表現は、完全に貪欲であるか、完全に非貪欲であることに注意してください。\r\n(.+)\r\n「ノードで設定をリロードしています...」に続く行をキャプチャするために使用します。ただし、その.+部分に改行を含めてはならないため、貪欲ではない必要があります。したがって、すべての量指定子はnode_patt非貪欲でなければなりません。

于 2010-09-21T15:55:26.730 に答える
1

コードは次のようになりました(単純なループ):

send "cluster config -r -a \r"
set done 0
set number_ok 0
while {$done == 0} {
   set done 1
   expect {
      $ROOT_PROMPT { set done 1 }
      "ERROR" { cluster_exit 1 }
      -re "Reloading configuration on node.*\r" {
         set line "<$cmdline> $expect_out(0,string)"
         expect {
            $ROOT_PROMPT { set done 1 }
            "ERROR" { cluster_exit 1 }
            -re "Node * not found" { ok 0 "$line (not found)" }
            -re "Node * not responding, skipped" { ok 0 "$line (not responding)" }
            "OK" {
               ok 1 "$line"
               set number_ok [expr $number_ok + 1] 
               set done 0
            }
         }
      }
   }
}
diag "Done $done"
diag "Count $number_ok"
于 2010-09-21T13:33:51.293 に答える