inspec/test/unit/resources/packages_test.rb

93 lines
3.2 KiB
Ruby
Raw Normal View History

add "packages" resource This pull request adds a packages resource so that we can check for pattern matches against all the packages on a system. This initially implements only dpkg support for debian-based platforms so we can cover this use case: ```ruby describe packages(/^xserver-xorg.*/) do its("list") { should be_empty } end ``` This uses FilterTable so we can supply additional queries, too. ```ruby describe packages(/vi.+/).where { status != 'installed' } do its('statuses') { should be_empty } end ``` Users can specify the name as a string or a regular expression. If it is a string, we will escape it and convert it to a regular expression to use in matching against the full returned list of packages. If it is a regular expression, we take that as is and use it to filter the results. While some package management systems such as `dpkg` can take a shell glob argument to filter their results, we eschew this and require a regular expression to match multiple package names because we will need this to work across other platforms in the future. This means that the following: ```ruby packages("vim") ``` Will return *all* the "vim" packages on the system. The `packages` resource will take `"vim"`, turn it into `/vim/`, and greedily match anything with "vim" in the name. To match only a single package named `vim`, it needs to be an anchored regular expression. ```ruby packages(/^vim$/) ``` Signed-off-by: Joshua Timberman <joshua@chef.io> Use entries instead of list Added a few more tests and non installed package in output Signed-off-by: Alex Pop <apop@chef.io> fix lint Signed-off-by: Alex Pop <apop@chef.io> Signed-off-by: Joshua Timberman <joshua@chef.io>
2017-02-03 20:42:55 +00:00
# encoding: utf-8
# author: Joshua Timberman
require 'helper'
require 'inspec/resource'
describe 'Inspec::Resources::Packages' do
it 'verify packages resource' do
resource = MockLoader.new(:ubuntu1604).load_resource('packages', /^vim$/)
_(resource.entries.length).must_equal 1
_(resource.entries[0].to_h).must_equal({
status: 'installed',
name: 'vim',
version: '7.4.1689-3ubuntu1.2',
architecture: 'amd64',
add "packages" resource This pull request adds a packages resource so that we can check for pattern matches against all the packages on a system. This initially implements only dpkg support for debian-based platforms so we can cover this use case: ```ruby describe packages(/^xserver-xorg.*/) do its("list") { should be_empty } end ``` This uses FilterTable so we can supply additional queries, too. ```ruby describe packages(/vi.+/).where { status != 'installed' } do its('statuses') { should be_empty } end ``` Users can specify the name as a string or a regular expression. If it is a string, we will escape it and convert it to a regular expression to use in matching against the full returned list of packages. If it is a regular expression, we take that as is and use it to filter the results. While some package management systems such as `dpkg` can take a shell glob argument to filter their results, we eschew this and require a regular expression to match multiple package names because we will need this to work across other platforms in the future. This means that the following: ```ruby packages("vim") ``` Will return *all* the "vim" packages on the system. The `packages` resource will take `"vim"`, turn it into `/vim/`, and greedily match anything with "vim" in the name. To match only a single package named `vim`, it needs to be an anchored regular expression. ```ruby packages(/^vim$/) ``` Signed-off-by: Joshua Timberman <joshua@chef.io> Use entries instead of list Added a few more tests and non installed package in output Signed-off-by: Alex Pop <apop@chef.io> fix lint Signed-off-by: Alex Pop <apop@chef.io> Signed-off-by: Joshua Timberman <joshua@chef.io>
2017-02-03 20:42:55 +00:00
})
end
it 'package name matches with output (string)' do
resource = MockLoader.new(:ubuntu1604).load_resource('packages', 'xserver-xorg')
_(resource.to_s).must_equal 'Packages /xserver\\-xorg/'
end
it 'packages using where filters' do
resource = MockLoader.new(:ubuntu1604).load_resource('packages', /.+root$/)
_(resource.entries.length).must_equal 3
_(resource.where { status != 'installed' }.names).must_equal(['fakeroot', 'libfakeroot'])
_(resource.where { version =~ /^0\.2.+/ }.entries[0].to_h).must_equal({
status: "installed",
name: "overlayroot",
version: "0.27ubuntu1.2",
architecture: 'amd64',
add "packages" resource This pull request adds a packages resource so that we can check for pattern matches against all the packages on a system. This initially implements only dpkg support for debian-based platforms so we can cover this use case: ```ruby describe packages(/^xserver-xorg.*/) do its("list") { should be_empty } end ``` This uses FilterTable so we can supply additional queries, too. ```ruby describe packages(/vi.+/).where { status != 'installed' } do its('statuses') { should be_empty } end ``` Users can specify the name as a string or a regular expression. If it is a string, we will escape it and convert it to a regular expression to use in matching against the full returned list of packages. If it is a regular expression, we take that as is and use it to filter the results. While some package management systems such as `dpkg` can take a shell glob argument to filter their results, we eschew this and require a regular expression to match multiple package names because we will need this to work across other platforms in the future. This means that the following: ```ruby packages("vim") ``` Will return *all* the "vim" packages on the system. The `packages` resource will take `"vim"`, turn it into `/vim/`, and greedily match anything with "vim" in the name. To match only a single package named `vim`, it needs to be an anchored regular expression. ```ruby packages(/^vim$/) ``` Signed-off-by: Joshua Timberman <joshua@chef.io> Use entries instead of list Added a few more tests and non installed package in output Signed-off-by: Alex Pop <apop@chef.io> fix lint Signed-off-by: Alex Pop <apop@chef.io> Signed-off-by: Joshua Timberman <joshua@chef.io>
2017-02-03 20:42:55 +00:00
})
end
it 'package name matches with output (regex)' do
resource = MockLoader.new(:ubuntu1604).load_resource('packages', /vim/)
_(resource.to_s).must_equal 'Packages /vim/'
end
it 'returns a list of packages with a wildcard' do
resource = MockLoader.new(:ubuntu1604).load_resource('packages', /^xserver-xorg.*/)
_(resource.statuses).must_equal ['installed']
_(resource.entries.length).must_equal 3
end
it 'all packages on Ubuntu' do
resource = MockLoader.new(:ubuntu1604).load_resource('packages', /.+/)
_(resource.entries.length).must_equal 14
end
it 'can find packages with same name but different architectures on Ubuntu' do
resource = MockLoader.new(:ubuntu1604).load_resource('packages', /libc6/)
_(resource.architectures).must_include 'amd64'
_(resource.architectures).must_include 'i386'
end
it 'can find packages with same name but different architectures on CentOS' do
resource = MockLoader.new(:centos6).load_resource('packages', /libstdc/)
_(resource.architectures).must_include 'x86_64'
_(resource.architectures).must_include 'i686'
end
it 'all packages on CentOS' do
resource = MockLoader.new(:centos6).load_resource('packages', /.+/)
_(resource.entries.length).must_equal 12
end
it 'packages on CentOS' do
resource = MockLoader.new(:centos6).load_resource('packages', /^chef\-.+/)
_(resource.entries.length).must_equal 1
_(resource.where { status == 'installed' }.names).must_equal(['chef-compliance'])
_(resource.entries[0].to_h).must_equal({
status: "installed",
name: "chef-compliance",
version: "1.3.1-1.el6",
architecture: "x86_64",
})
end
add "packages" resource This pull request adds a packages resource so that we can check for pattern matches against all the packages on a system. This initially implements only dpkg support for debian-based platforms so we can cover this use case: ```ruby describe packages(/^xserver-xorg.*/) do its("list") { should be_empty } end ``` This uses FilterTable so we can supply additional queries, too. ```ruby describe packages(/vi.+/).where { status != 'installed' } do its('statuses') { should be_empty } end ``` Users can specify the name as a string or a regular expression. If it is a string, we will escape it and convert it to a regular expression to use in matching against the full returned list of packages. If it is a regular expression, we take that as is and use it to filter the results. While some package management systems such as `dpkg` can take a shell glob argument to filter their results, we eschew this and require a regular expression to match multiple package names because we will need this to work across other platforms in the future. This means that the following: ```ruby packages("vim") ``` Will return *all* the "vim" packages on the system. The `packages` resource will take `"vim"`, turn it into `/vim/`, and greedily match anything with "vim" in the name. To match only a single package named `vim`, it needs to be an anchored regular expression. ```ruby packages(/^vim$/) ``` Signed-off-by: Joshua Timberman <joshua@chef.io> Use entries instead of list Added a few more tests and non installed package in output Signed-off-by: Alex Pop <apop@chef.io> fix lint Signed-off-by: Alex Pop <apop@chef.io> Signed-off-by: Joshua Timberman <joshua@chef.io>
2017-02-03 20:42:55 +00:00
it 'skips on non debian platforms' do
resource = MockLoader.new(:hpux).load_resource('packages', 'bash')
_(resource.resource_exception_message).must_equal 'The packages resource is not yet supported on OS hpux'
add "packages" resource This pull request adds a packages resource so that we can check for pattern matches against all the packages on a system. This initially implements only dpkg support for debian-based platforms so we can cover this use case: ```ruby describe packages(/^xserver-xorg.*/) do its("list") { should be_empty } end ``` This uses FilterTable so we can supply additional queries, too. ```ruby describe packages(/vi.+/).where { status != 'installed' } do its('statuses') { should be_empty } end ``` Users can specify the name as a string or a regular expression. If it is a string, we will escape it and convert it to a regular expression to use in matching against the full returned list of packages. If it is a regular expression, we take that as is and use it to filter the results. While some package management systems such as `dpkg` can take a shell glob argument to filter their results, we eschew this and require a regular expression to match multiple package names because we will need this to work across other platforms in the future. This means that the following: ```ruby packages("vim") ``` Will return *all* the "vim" packages on the system. The `packages` resource will take `"vim"`, turn it into `/vim/`, and greedily match anything with "vim" in the name. To match only a single package named `vim`, it needs to be an anchored regular expression. ```ruby packages(/^vim$/) ``` Signed-off-by: Joshua Timberman <joshua@chef.io> Use entries instead of list Added a few more tests and non installed package in output Signed-off-by: Alex Pop <apop@chef.io> fix lint Signed-off-by: Alex Pop <apop@chef.io> Signed-off-by: Joshua Timberman <joshua@chef.io>
2017-02-03 20:42:55 +00:00
end
it 'fails if the packages name is not a string or regexp' do
proc {
resources = MockLoader.new(:ubuntu1604).load_resource('packages', [:a, :b])
resources.send(:entries, nil)
}.must_raise(RuntimeError)
end
end