3

I have a problem with serverspec. I'm trying to check installed package version on ubuntu.

I use this code:

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 run dpkg-query command to check package but escapes tilda character and it doesn't work. serverspec runs:

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

instead of

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

How can I fix this problem?

esio
  • 1,592
  • 3
  • 17
  • 30

1 Answers1

4

The problem is here: https://github.com/mizzy/specinfra/blob/92ccc19714ead956589127c40e3cd65faf38cb8b/lib/specinfra/command/debian/base/package.rb#L6.

Specinfra is escaping the characters in the with_version chain as #{Regexp.escape(escape(version))} instead of #{Regexp.escape(version)). This would require a PR to Specinfra to fix due to the Specinfra/Serverspec contribution policy. I can put this on my list of things to do and notify you when finished, since I keep an up-to-date Specinfra fork around and am a contributor to both so I know the codebase.

In the meantime, you would have to do a command matcher workaround.

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

Matthew Schuchard
  • 25,172
  • 3
  • 47
  • 67
  • Good, thanks It will be grat if this will be fixed. :) – esio Feb 23 '17 at 14:57
  • It looks like this was fixed in https://github.com/mizzy/serverspec/pull/316/files in ServerSpec and appears to be working for me. Not sure if it's possible to do a `>=` or if it only supports > or <. – dragon788 Oct 11 '17 at 21:10
  • @dragon788 Please note the link in the answer to the PR where I fixed it. – Matthew Schuchard Oct 12 '17 at 11:16
  • Aha, I knew I saw it somewhere! It also works with `cmp` in InSpec. It's a shame that fuzzy versions aren't fully supported in all the Chef package resources yet. – dragon788 Oct 12 '17 at 12:20