rubylinuxrspecdevopsserverspec

Serverspec doesn't check package version correctly


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?


Solution

  • 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