Module: Umami::Helper::InSpec

Included in:
Test::Integration
Defined in:
lib/chef-umami/helpers/inspec.rb

Instance Method Summary collapse

Instance Method Details

#desciption(resource) ⇒ Object

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).



21
22
23
24
25
26
27
28
# 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) ⇒ Object



66
67
68
# File 'lib/chef-umami/helpers/inspec.rb', line 66

def test_chef_gem(resource)
  test_gem_package(resource, ':chef')
end

#test_cron(resource) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# 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_file(resource) ⇒ Object Also known as: test_cookbook_file, test_directory, test_remote_file, test_remote_directory, test_template



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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) ⇒ Object

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.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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) ⇒ Object



116
117
118
119
120
121
# 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) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# 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_user(resource) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# 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