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



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

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



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

def assert_acl(file, owner, group, mode)
  file(file).
    must_have(:owner, owner).
    must_have(:group, group).
    must_have(:mode, mode)
end

#assert_directory(dir, *args) ⇒ Object



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

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

#assert_enabled(service) ⇒ Object



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

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

#assert_file(file, *args) ⇒ Object



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

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

#assert_group_includes(members, group) ⇒ Object



35
36
37
38
39
# File 'lib/minitest-chef-handler/assertions.rb', line 35

def assert_group_includes(members, group)
  members = [members] unless members.respond_to?(:&)
  assert group.members & members == members, "Expected group '#{group.name}' to include members: #{members.join(', ')}"
  group
end

#assert_includes_content(file, content) ⇒ Object



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

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



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

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

#assert_logrotate(file) ⇒ Object



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

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



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

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



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

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



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

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

#assert_mounted(mount) ⇒ Object



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

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

#assert_path_exists(file_or_dir) ⇒ Object



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

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



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

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

#assert_sh(command, text = nil) ⇒ Object



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

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



150
151
152
153
154
# File 'lib/minitest-chef-handler/assertions.rb', line 150

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



144
145
146
147
148
# File 'lib/minitest-chef-handler/assertions.rb', line 144

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



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

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

#refute_group_includes(members, group) ⇒ Object



41
42
43
44
45
# File 'lib/minitest-chef-handler/assertions.rb', line 41

def refute_group_includes(members, group)
  members = [members] unless members.respond_to?(:&)
  refute group.members & members == members, "Expected group '#{group.name}' not to include members: #{members.join(', ')}"
  group
end

#refute_includes_content(file, content) ⇒ Object



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

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



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

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

#refute_matches_content(file, regexp) ⇒ Object



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

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



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

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



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

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

#refute_mounted(mount) ⇒ Object



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

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



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

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



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

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