module Umami::Helper::InSpec

Public Instance Methods

desciption(resource) click to toggle source

Call on a resource's identity method to help describe the resource. This saves us from having to know/code the identity attribute for each resource (i.e. File is :path, User is :username, etc).

# File lib/chef-umami/helpers/inspec.rb, line 21
def desciption(resource)
  if identity.is_a? Hash # #identity could return a Hash. Take the first value.
    identity = identity.values.first
  else
    identity = resource.identity
  end
  "describe #{resource.declared_type}('#{identity}') do"
end
test_chef_gem(resource) click to toggle source
# File lib/chef-umami/helpers/inspec.rb, line 66
def test_chef_gem(resource)
  test_gem_package(resource, ':chef')
end
test_cookbook_file(resource)
Alias for: test_file
test_cron(resource) click to toggle source
# File lib/chef-umami/helpers/inspec.rb, line 70
def test_cron(resource)
  test = [desciption(resource)]
  cron_entry = "#{resource.minute} "  \
               "#{resource.hour} "    \
               "#{resource.day} "     \
               "#{resource.month} "   \
               "#{resource.weekday} " \
               "#{resource.command}"
  test << "it { should have_entry('#{cron_entry}').with_user('#{resource.user}') }"
  test << 'end'
  test.join("\n")
end
test_directory(resource)
Alias for: test_file
test_file(resource) click to toggle source
# File lib/chef-umami/helpers/inspec.rb, line 83
def test_file(resource)
  test = ["describe file('#{resource.path}') do"]
  if resource.declared_type =~ /directory/
    test << 'it { should be_directory }'
  else
    test << 'it { should be_file }'
  end
  # Sometimes we see GIDs instead of group names.
  unless resource.group.nil?
    unless resource.group.is_a?(String) && resource.group.empty?
      test << "it { should be_grouped_into '#{resource.group}' }"
    end
  end
  # Guard for UIDs versus usernames as well.
  unless resource.owner.nil?
    unless resource.owner.is_a?(String) && resource.owner.empty?
      test << "it { should be_owned_by '#{resource.owner}' }"
    end
  end
  unless resource.mode.nil?
    unless resource.mode.is_a?(String) && !resource.mode.empty?
      test << "it { should be_mode '#{resource.mode}' }"
    end
  end
  test << 'end'
  test.join("\n")
end
test_gem_package(resource, gem_binary = nil) click to toggle source

InSpec can evaluate if a gem is installed via the system `gem` (default) or via some other `gem` binary, defined by either the path to the gem binary of a symbol representing that context.

# File lib/chef-umami/helpers/inspec.rb, line 47
def test_gem_package(resource, gem_binary = nil)
  package_name = resource.package_name
  if gem_binary
    if gem_binary.is_a? Symbol
      gem_binary = gem_binary.inspect # Stringify the symbol.
    else
      gem_binary = "'#{gem_binary}'"
    end
    test = ["Gem '#{package_name}' is installed via the #{gem_binary} gem"]
    test << "describe gem('#{package_name}', #{gem_binary}) do"
  else
    test = ["Gem '#{package_name}' is installed via the #{gem_binary} gem"]
    test << "describe gem('#{package_name}') do"
  end
  test << 'it { should be_installed }'
  test << 'end'
  test.join("\n")
end
test_group(resource) click to toggle source
# File lib/chef-umami/helpers/inspec.rb, line 116
def test_group(resource)
  test = [desciption(resource)]
  test << 'it { should exist }'
  test << 'end'
  test.join("\n")
end
test_package(resource) click to toggle source
# File lib/chef-umami/helpers/inspec.rb, line 123
def test_package(resource)
  test = [desciption(resource)]
  if !resource.version.nil? && !resource.version.empty?
    test << "it { should be_installed.with_version('#{resource.version}') }"
  else
    test << 'it { should be_installed }'
  end
  test << 'end'
  test.join("\n")
end
test_remote_directory(resource)
Alias for: test_file
test_remote_file(resource)
Alias for: test_file
test_template(resource)
Alias for: test_file
test_user(resource) click to toggle source
# File lib/chef-umami/helpers/inspec.rb, line 134
def test_user(resource)
  test = [desciption(resource)]
  test << 'it { should exist }'
  # Guard for GIDs rather than strings. Chef aliases the #group method
  # to the #gid method.
  unless resource.gid.nil?
    unless resource.gid.is_a?(String) && !resource.gid.empty?
      test << "it { should belong_to_primary_group '#{resource.gid}' }"
    end
  end
  if !resource.home.nil? && !resource.home.empty?
    test << "it { should have_home_directory '#{resource.home}' }"
  end
  test << 'end'
  test.join("\n")
end