2

From the docs it seems like using Serverspec to verify that packages are installed should be pretty straight-forward, but I'm having some interesting problems with vim and ag (the_silver_searcher).

I am using Test Kitchen with the kitchen-vagrant plugin and have two platforms: ubuntu-1404 and centos-72. All of my specs pass for Ubuntu, and two of them fail for Centos: vim and ag.

vim

The Chef code that handles this installation is super simple:

package "vim"

And here is the spec:

describe "Vim" do
  describe package("vim") do
    it { should be_installed }
  end
end

Again, very straight-forward. However, it fails on my Centos build with this error:

 2) Vim Package "vim" should be installed
    Failure/Error: it { should be_installed }
      expected Package "vim" to be installed
      /bin/sh -c rpm\ -q\ vim
      package vim is not installed

Yet if I login to the server, it most definitely is installed:

▶ kitchen login all-centos-72
Last login: Sat Jul  2 17:53:30 2016 from 10.0.2.2
[vagrant@all-centos-72 ~]$ vim --version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Jun 10 2014 06:55:55)
[vagrant@all-centos-72 ~]$ which vim
/usr/bin/vim
[vagrant@all-centos-72 ~]$ sudo yum install -y vim
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: distro.ibiblio.org
 * extras: mirror.us.leaseweb.net
 * updates: mirror.eboundhost.com
Package 2:vim-enhanced-7.4.160-1.el7.x86_64 already installed and latest version
Nothing to do

ag

ag is more complicated in that the installation requires building from source on Centos whereas on Ubuntu it's available with apt-get. Here is the relevant part of the recipe:

  bash "install Development Tools" do
    code "yum -y groupinstall \"Development Tools\""
  end

  package %w(pcre-devel xz-devel)

  target_dir = File.join("/", "usr", "local", "the_silver_searcher")

  git "clone the ag repo" do
    repo "https://github.com/ggreer/the_silver_searcher/"
    revision "master"
    destination target_dir
  end

  bash "install ag" do
    not_if system("hash ag")

    cwd target_dir
    code <<-EOF
      ./build.sh
      make install
    EOF
  end

And here is the spec:

describe "The Silver Searcher" do    
  if host_inventory["platform"] == "ubuntu"
    describe package("silversearcher-ag") do
      it { should be_installed }
    end
  else
    describe package("the_silver_searcher") do
      it { should be_installed }
    end
  end
end

The Centos failure:

 1) The Silver Searcher Package "the_silver_searcher" should be installed
    Failure/Error: it { should be_installed }
      expected Package "the_silver_searcher" to be installed
      /bin/sh -c rpm\ -q\ the_silver_searcher
      package the_silver_searcher is not installed

Likewise, if I log into the Centos VM I can use ag:

[vagrant@all-centos-72 ~]$ ag --version
ag version 0.32.0
[vagrant@all-centos-72 ~]$ which ag
/usr/local/bin/ag

These commands also work if I switch to root user.

I've tried to cheat the system by writing my specs in different ways for the Centos platform:

  describe command("ag") do
    its(:stderr) { should match /Usage: ag/ }
  end

The above also doesn't work, even though typing ag when logged in (exit status 1) does produce that usage content. My last attempt was:

describe file("/usr/local/bin/ag") do
  it { should exist }
end

This works but feels super hacky and like it shouldn't be necessary.

Anyone have recommendations here? Is there something I'm missing/doing wrong with these packages? I initially thought that the ag problem was only because it was installed from source rather than a package manager, but vim was installed with a package manager and still has the same problem as ag.

sixty4bit
  • 7,422
  • 7
  • 33
  • 57
  • On your Centos server, if you log in an run `rpm -q vim` (the command the `is_installed` matcher tries), what does it output? What is the command's exit code? As for the ag example, that will fail, because you're building from source rather than using the `package` resource, so as you found, you will have to test with alternate matchers. – Karen B Jul 03 '16 at 22:04
  • Thanks for your comment. I guessed as much about `ag`, that's a shame but to be expected, I guess, so it's the `vim` thing that I really don't get. To your question: running that command on the Centos server manually produces the same message that the spec runner is getting: `package vim is not installed`. The exit status is `1`. Any idea why that would be? – sixty4bit Jul 04 '16 at 01:53
  • This line `Package 2:vim-enhanced-7.4.160-1.el7.x86_64 already installed and latest version` shows the package name is actualy `vim-enhanced`. It's been years since I had to deal with yum/CentOS/RHEL, but it looks like there's not actually a `vim` package. Either your system already had `vim-enhanced` installed through another means, or some kind of aliasing is going on. Either way, try both installing the package as `vim-enhanced` and testing for that in serverspec for CentOS. – Karen B Jul 04 '16 at 02:03
  • @KarenB thanks, that comment was very enlightening! It looks like I need to be aware of specific package names on a per-system basis. That fixes the `vim` problem. As for `ag`, through discussion in a GitHub issue I opened I've found a way to get installation via package manager to work and can now use Serverspec package resource to test. If you'd like to reformulate your last comment as an answer (with a note about it being necessary to work around the package resource if something wasn't installed with a package manager) I'll mark it correct! – sixty4bit Jul 04 '16 at 02:22
  • I just want to note that `yum` lets you fetch the package as `vim` even though it is installed on the system as `vim-enhanced` which is what allowed me to get this far in my confusion/problem. – sixty4bit Jul 04 '16 at 02:25
  • @KarenB should convert her comments into an answer, and the answer currently provided should be converted into a comment or deleted. – Matthew Schuchard Jul 06 '16 at 01:28
  • @MattSchuchard I recommended that a couple days ago but she hasn't left an answer yet. I guess I'll just answer myself and give her credit if she doesn't do it today. – sixty4bit Jul 06 '16 at 15:43
  • @sixty4bit Sounds good. Type it up and mention me and I will give it a proofread with suggestions. Then I will upvote to help it take precedence over the current answer. FYI `yum` was redirecting your `vim` query to `vim-enhanced` which is a feature of `yum`. I think it will do the same for a query on `gvim` for example. – Matthew Schuchard Jul 06 '16 at 15:53
  • @MattSchuchard can you upvote the selected answer so it will take precedence? – sixty4bit Jul 28 '16 at 01:24
  • I did. Serverspec is too unpopular a tag on StackOverflow unfortunately, despite all of the companies actually using it. – Matthew Schuchard Jul 28 '16 at 11:28

2 Answers2

2

The answer to the question has two parts, one dealing with the way Serverspec works and the other with how various Linux distributions handle packages.

1) Serverspec users shouldn't assume any behavior that isn't literally implied by the name of the package resource. Applications that are installed by any means other than the system's package manager will not be picked up and their successful installation should be tested by other means. This could include, as in the question, testing for the existence of a binary file.

2) When the developer has installed an application with a package manager, he/she must be aware that package managers on different Linux distributions sometimes (often?) have different names for the same package. A classic example of this is apache2 on Debian systems vs. httpd on RedHat systems. In the specific case mentioned in the question, vim is recognized on CentOS as vim-enhanced even though yum accepts vim as the name when installing it (thanks to @matt-schuchard for pointing out that they are linked).

Also wanted to credit @Karen B for helping me reach these conclusions in the comments to the question.

sixty4bit
  • 7,422
  • 7
  • 33
  • 57
0

You aren't installing ag via a package so it's not a "hack" for that to not work.

coderanger
  • 52,400
  • 4
  • 52
  • 75
  • 1
    Did you mean to leave this as a comment? – sixty4bit Jul 05 '16 at 23:15
  • No, it's an answer. If you aren't installing something via yum on CentOS, you don't want to use a `package()` test resource. – coderanger Jul 05 '16 at 23:19
  • Ok, I mean it's technically correct but only addresses part of the problem so I'm not sure how to handle it. The *real* solution here (I think) is that one has to be aware that just because one can install packages like `vim` with `yum install vim` doesn't mean they will be recognized by that name when doing `rpm -q vim`...as well as pointing out the (obvious except to beginner me a few days ago) fact that looking for an installed package is not the same as looking for an installed application – sixty4bit Jul 05 '16 at 23:33
  • Yep, basically the things you were calling a hack are the correct way to do this :) What you _actually_ care about in the end is that a `vim` binary exists, how it got installed is outside of the scope for an integration test. – coderanger Jul 05 '16 at 23:47