Module: MiniTest::Chef::Assertions

Included in:
Spec, TestCase
Defined in:
lib/minitest-chef-handler/assertions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.resource_exists(name, options) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/minitest-chef-handler/assertions.rb', line 7

def self.resource_exists(name, options)
  options[:description] = name unless options[:description]
  define_method("assert_#{name}_exists") do |resource|
    refute resource.send(options[:field]).nil?,
      "Expected #{options[:description]} '#{resource.name}' to exist"
    resource
  end
  define_method("refute_#{name}_exists") do |resource|
    assert resource.send(options[:field]).nil?,
      "Expected #{options[:description]} '#{resource.name}' to not exist"
    resource
  end
end

Instance Method Details

#assert_acl(file, owner, group, mode) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/minitest-chef-handler/assertions.rb', line 141

def assert_acl(file, owner, group, mode)
  unless File.directory?(file)
    file(file).
      must_have(:owner, owner).
      must_have(:group, group).
      must_have(:mode, mode)
  else
    directory(file).
      must_have(:owner, owner).
      must_have(:group, group).
      must_have(:mode, mode)
  end
end

#assert_directory(dir, *args) ⇒ Object



131
132
133
134
# File 'lib/minitest-chef-handler/assertions.rb', line 131

def assert_directory(dir, *args)
  assert File.directory?(dir), "Expected #{dir} to be a directory"
  assert_acl(dir, *args)
end

#assert_enabled(service) ⇒ Object



27
28
29
30
# File 'lib/minitest-chef-handler/assertions.rb', line 27

def assert_enabled(service)
  assert service.enabled, "Expected service '#{service.name}' to be enabled"
  service
end

#assert_file(file, *args) ⇒ Object



136
137
138
139
# File 'lib/minitest-chef-handler/assertions.rb', line 136

def assert_file(file, *args)
  assert File.file?(file), "Expected #{file} to be a file"
  assert_acl(file, *args)
end

#assert_group_includes(members, group) ⇒ Object



37
38
39
40
41
42
# File 'lib/minitest-chef-handler/assertions.rb', line 37

def assert_group_includes(members, group)
  members = Set.new(Array(members))
  assert Set.new(group.members) & members == members,
    "Expected group '#{group.name}' to include members: #{members.to_a.join(', ')}"
  group
end

#assert_includes_content(file, content) ⇒ Object



51
52
53
54
# File 'lib/minitest-chef-handler/assertions.rb', line 51

def assert_includes_content(file, content)
  assert File.read(file.path).include?(content), "Expected file '#{file.path}' to include the specified content"
  file
end

#assert_installed(package) ⇒ Object



61
62
63
64
# File 'lib/minitest-chef-handler/assertions.rb', line 61

def assert_installed(package)
  refute package.version.nil?, "Expected package '#{package.name}' to be installed"
  package
end

#assert_logrotate(file) ⇒ Object



167
168
169
170
# File 'lib/minitest-chef-handler/assertions.rb', line 167

def assert_logrotate(file)
  assert_file file, "root", "root", 0644
  assert_sh "logrotate -d #{file}", "Expected #{file} to pass logrotate validation"
end

#assert_matches_content(file, regexp) ⇒ Object



71
72
73
74
# File 'lib/minitest-chef-handler/assertions.rb', line 71

def assert_matches_content(file, regexp)
  assert File.read(file.path).match(regexp), "Expected the contents of file '#{file.path}' to match the regular expression '#{regexp}'"
  file
end

#assert_modified_after(file_or_dir, time) ⇒ Object



81
82
83
84
# File 'lib/minitest-chef-handler/assertions.rb', line 81

def assert_modified_after(file_or_dir, time)
  assert File.mtime(file_or_dir.path).to_i >= time.to_i, "Expected the file '#{file_or_dir.path}' to have been modified after '#{time}'"
  file_or_dir
end

#assert_mount_enabled(mount) ⇒ Object



101
102
103
104
# File 'lib/minitest-chef-handler/assertions.rb', line 101

def assert_mount_enabled(mount)
  assert mount.enabled, "Expected mount point '#{mount.name}' to be enabled"
  mount
end

#assert_mounted(mount) ⇒ Object



91
92
93
94
# File 'lib/minitest-chef-handler/assertions.rb', line 91

def assert_mounted(mount)
  assert mount.mounted, "Expected mount point '#{mount.name}' to be mounted"
  mount
end

#assert_path_exists(file_or_dir) ⇒ Object



111
112
113
114
# File 'lib/minitest-chef-handler/assertions.rb', line 111

def assert_path_exists(file_or_dir)
  assert File.exists?(file_or_dir.path), "Expected path '#{file_or_dir.path}' to exist"
  file_or_dir
end

#assert_running(service) ⇒ Object



121
122
123
124
# File 'lib/minitest-chef-handler/assertions.rb', line 121

def assert_running(service)
  assert service.running, "Expected service '#{service.name}' to be running"
  service
end

#assert_sh(command, text = nil) ⇒ Object



172
173
174
175
176
177
# File 'lib/minitest-chef-handler/assertions.rb', line 172

def assert_sh(command, text=nil)
  text ||= "Expected #{command} to succeed"
  out = `#{command} 2>&1`
  assert $?.success?, "#{text}, but failed with: #{out}"
  out
end

#assert_symlinked_directory(directory, *args) ⇒ Object



161
162
163
164
165
# File 'lib/minitest-chef-handler/assertions.rb', line 161

def assert_symlinked_directory(directory, *args)
  assert File.symlink?(directory), "Expected #{directory} to be a symlink"
  assert_sh "ls #{directory}/", "Expected #{directory} to link to an existing directory"
  assert_acl directory, *args
end

#assert_symlinked_file(file, *args) ⇒ Object



155
156
157
158
159
# File 'lib/minitest-chef-handler/assertions.rb', line 155

def assert_symlinked_file(file, *args)
  assert File.symlink?(file), "Expected #{file} to be a symlink"
  assert File.read(file, 1), "Expected #{file} to be linked to an existing file"
  assert_acl file, *args
end

#refute_enabled(service) ⇒ Object



32
33
34
35
# File 'lib/minitest-chef-handler/assertions.rb', line 32

def refute_enabled(service)
  refute service.enabled, "Expected service '#{service.name}' to not be enabled"
  service
end

#refute_group_includes(members, group) ⇒ Object



44
45
46
47
48
49
# File 'lib/minitest-chef-handler/assertions.rb', line 44

def refute_group_includes(members, group)
  members = Set.new(Array(members))
  refute Set.new(group.members) & members == members,
    "Expected group '#{group.name}' not to include members: #{members.to_a.join(', ')}"
  group
end

#refute_includes_content(file, content) ⇒ Object



56
57
58
59
# File 'lib/minitest-chef-handler/assertions.rb', line 56

def refute_includes_content(file, content)
  refute File.read(file.path).include?(content), "Expected file '#{file.path}' not to include the specified content"
  file
end

#refute_installed(package) ⇒ Object



66
67
68
69
# File 'lib/minitest-chef-handler/assertions.rb', line 66

def refute_installed(package)
  assert package.version.nil?, "Expected package '#{package.name}' to not be installed"
  package
end

#refute_matches_content(file, regexp) ⇒ Object



76
77
78
79
# File 'lib/minitest-chef-handler/assertions.rb', line 76

def refute_matches_content(file, regexp)
  refute File.read(file.path).match(regexp), "Expected the contents of file '#{file.path}' not to match the regular expression '#{regexp}'"
  file
end

#refute_modified_after(file_or_dir, time) ⇒ Object



86
87
88
89
# File 'lib/minitest-chef-handler/assertions.rb', line 86

def refute_modified_after(file_or_dir, time)
  refute File.mtime(file_or_dir.path) >= time, "Expected the file '#{file_or_dir.path}' not to have been modified after '#{time}'"
  file_or_dir
end

#refute_mount_enabled(mount) ⇒ Object



106
107
108
109
# File 'lib/minitest-chef-handler/assertions.rb', line 106

def refute_mount_enabled(mount)
  refute mount.enabled, "Expected mount point '#{mount.name}' to not be enabled"
  mount
end

#refute_mounted(mount) ⇒ Object



96
97
98
99
# File 'lib/minitest-chef-handler/assertions.rb', line 96

def refute_mounted(mount)
  refute mount.mounted, "Expected mount point '#{mount.name}' to not be mounted"
  mount
end

#refute_path_exists(file_or_dir) ⇒ Object



116
117
118
119
# File 'lib/minitest-chef-handler/assertions.rb', line 116

def refute_path_exists(file_or_dir)
  refute File.exists?(file_or_dir.path), "Expected path '#{file_or_dir.path}' not to exist"
  file_or_dir
end

#refute_running(service) ⇒ Object



126
127
128
129
# File 'lib/minitest-chef-handler/assertions.rb', line 126

def refute_running(service)
  refute service.running, "Expected service '#{service.name}' not to be running"
  service
end

#refute_sh(command, text = nil) ⇒ Object



179
180
181
182
183
184
# File 'lib/minitest-chef-handler/assertions.rb', line 179

def refute_sh(command, text=nil)
  text ||= "Expected #{command} not to succeed"
  out = `#{command} 2>&1`
  assert !$?.success?, "#{text}, but succeeded with: #{out}"
  out
end