【问题标题】:Running ntpq -p command through rspec test for chef通过厨师的 rspec 测试运行 ntpq -p 命令
【发布时间】:2020-07-08 19:30:55
【问题描述】:

我正在尝试测试 ntp 设置。 我已经配置了一个安装了 ntp 服务的 VM。 我能够登录到 vm 并运行验证 ntp 确实已安装并限制查询的命令:

vagrant@vagrant:~> sudo ntpq -p
localhost: timed out, nothing received
***Request timed out

但是,当我尝试在测试中测试这个确切的命令时,它返回空... 我显然在这里遗漏了一些重要的东西,但我不知道是什么。

这是我的测试:

control 'ntp configuration' do
  describe command('sudo nptq -p') do
    its('stdout') { should match('localhost: timed out, nothing received') }
  end
end

还有错误:

  ×  ntp configuration: Command: `sudo nptq -p`
     ×  Command: `sudo nptq -p` stdout is expected to match "localhost: timed out, nothing received"
     expected "" to match "localhost: timed out, nothing received"

我试过stdoutstderr,都没有用。

但是,我确实注意到的一件事是,从 vm 本身的命令行运行此命令需要几秒钟才能返回消息。 然而,该测试以明显更短的时间返回一个空字符串。

有人能解释一下吗? :)

【问题讨论】:

  • 错误可能在stderr,而不是stdout。
  • 我实际上尝试了stderr和stdout。
  • 我认为你的测试中有错字 - nptq,但应该是 ntpq。测试中也不需要sudo,因为测试已经以root身份运行。
  • 谢谢你们,显然这是两者的结合。错字显然破坏了命令,而我正在寻找的结果确实是在 stderr 中定义的。

标签: ruby bash chef-infra ntp


【解决方案1】:

错误在于:

control 'ntp configuration' do
  describe command('sudo nptq -p') do
    its('stdout') { should match('localhost: timed out, nothing received') }
  end
end

命令中的错字 -> nptq 必须是 ntpq,sudo 也可以省略。

正在检查的消息实际上是在stderr而不是stdout中返回的

工作代码:

  describe command 'ntpq -p' do
      its('stderr') { should match(/localhost: timed out, nothing received/) }
  end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    • 2019-03-06
    • 2020-09-04
    • 1970-01-01
    相关资源
    最近更新 更多