Serverspec没有正确检查包版本

时间:2017-02-23 14:00:20

标签: ruby linux rspec devops serverspec

我遇到了serverspec的问题。我试图在ubuntu上检查已安装的软件包版本。

我使用此代码:

describe 'java packages' do
  it 'package openjdk-9-jre should be installed with the correct version' do
    expect(package('openjdk-9-jre')).to be_installed.with_version('9~b114-0ubuntu1')
  end
end

Serverspec运行dpkg-query命令来检查包但是转义tilda字符并且它不起作用。 serverspec运行:

dpkg-query -f '${Status} ${Version}' -W openjdk-9-jre | grep -E '^(install|hold) ok installed 9\\~b114-0ubuntu1$'

而不是

dpkg-query -f '${Status} ${Version}' -W openjdk-9-jre | grep -E '^(install|hold) ok installed 9~b114-0ubuntu1$'

如何解决此问题?

1 个答案:

答案 0 :(得分:4)

问题在于:https://github.com/mizzy/specinfra/blob/92ccc19714ead956589127c40e3cd65faf38cb8b/lib/specinfra/command/debian/base/package.rb#L6

Specinfra将with_version链中的字符转义为#{Regexp.escape(escape(version))}而不是#{Regexp.escape(version))。由于Specinfra / Serverspec贡献政策,这将需要PRs到Specinfra修复。我可以把它放在我要做的事情列表上,并在完成后通知你,因为我保留了最新的Specinfra fork并且是两者的贡献者所以我知道代码库。

与此同时,您必须执行command匹配器解决方法。

describe 'java packages' do
  it 'package openjdk-9-jre should be installed with the correct version' do
    describe command("dpkg-query -f '${Status} ${Version}' -W openjdk-9-jre") do
      its(:stdout) { is_expected.to match('^(install|hold) ok installed 9\~b114\-0ubuntu1$') }
    end
  end
end

Specinfra PR:https://github.com/mizzy/specinfra/pull/608

相关问题