Chef / Vagrant / Serverspec:确保软件包安装失败的规范,但它们已安装

时间:2016-07-02 18:11:56

标签: ruby centos vagrant chef serverspec

从文档看来,使用Serverspec来验证软件包的安装应该非常简单,但我在vimag({{1}时遇到了一些有趣的问题}})。

我正在使用带有the_silver_searcher插件的Test Kitchen,并有两个平台:kitchen-vagrantubuntu-1404。我的所有规范都通过了Ubuntu,其中两个失败了Centos:centos-72vim

VIM

处理此安装的Chef代码非常简单:

ag

这是规范:

package "vim"

再次,非常直截了当。但是,我的Centos版本失败了,出现此错误:

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

然而,如果我登录到服务器,它肯定是安装的:

 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

AG

▶ 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 更复杂,因为安装需要在Centos上从源代码构建,而在Ubuntu上需要ag。以下是食谱的相关部分:

apt-get

这是规范:

  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

Centos失败:

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

同样,如果我登录Centos VM,我可以使用 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

ag

如果我切换到[vagrant@all-centos-72 ~]$ ag --version ag version 0.32.0 [vagrant@all-centos-72 ~]$ which ag /usr/local/bin/ag 用户,这些命令也可以使用。

我试图通过以不同方式为Centos平台编写规范来欺骗系统:

root

即使登录时输入 describe command("ag") do its(:stderr) { should match /Usage: ag/ } end (退出状态ag)确实产生了该使用内容,上述内容也无效。我的最后一次尝试是:

1

这有效,但感觉超级hacky,并且它不应该是必要的。

有人在这里有推荐吗?这些包装中是否有我遗漏/做错的事情?我最初认为describe file("/usr/local/bin/ag") do it { should exist } end 问题只是 ,因为它是从源代码而不是软件包管理器安装的,但ag 与软件包一起安装的经理仍然遇到与vim相同的问题。

2 个答案:

答案 0 :(得分:2)

问题的答案有两部分,一部分涉及Serverspec的工作方式,另一部分涉及各种Linux发行版处理包的方式。

1)Serverspec用户不应假设任何package资源名称没有明确暗示的行为。通过系统包管理器以外的任何方式安装的应用程序将不会被选中,并且应通过其他方式测试其成功安装。这可能包括,如在问题中,测试二进制文件的存在。

2)当开发人员已经安装了包管理器的应用程序时,他/她必须知道不同Linux发行版上的包管理器有时(通常是?)对同一个包具有不同的名称。一个典型的例子是Debian系统上的apache2与RedHat系统上的httpd。在问题中提到的特定情况下,vim在CentOS上被识别为vim-enhanced,即使yum在安装时接受vim作为名称(感谢@ matt-schuchard)指出它们是相互关联的)。

还想借助@Karen B帮助我在问题的评论中得出这些结论。

答案 1 :(得分:0)

你不是通过一个软件包安装ag,所以它不是“hack”,因为它不起作用。