为expect中的每场比赛执行一个动作

发布于 2024-09-24 00:15:59 字数 358 浏览 1 评论 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

我的期望块是什么样子?

I am a total expect noob.

I am writing a expect script for a test case where I want to count the number of occurrences of the string "Ok" and do an action for every occurrence from the following output:

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

How would my expect block look like?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听,心雨的声音 2024-10-01 00:15:59

我会重写您的代码以删除 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 中的每个量词都必须是非贪婪的。

I'd rewrite your code to remove the while loop:

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
}

Note that Tcl regular expressions are either entirely greedy or entirely non-greedy. I use \r\n(.+)\r\n to capture the line following "Reloading configuration on node ...". However the .+ part must not contain newlines, so it has to be non-greedy. Thus, every quantifier in node_patt has to be non-greedy.

单调的奢华 2024-10-01 00:15:59

代码最终看起来像这样(一个简单的循环):

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"

The code ended up looking like this (a simple loop):

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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文