Module: Helpers

Defined in:
vendor_modules/lvm/spec/lib/helpers.rb

Constant Summary collapse

TEST_DIR =
"#{Pathname.new(__FILE__).parent}..".freeze
TYPES =
{
  pv: :physical_volume,
  lv: :logical_volume,
  vg: :volume_group,
  fs: :filesystem
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(obj) ⇒ Object



13
14
15
# File 'vendor_modules/lvm/spec/lib/helpers.rb', line 13

def self.included(obj)
  obj.instance_eval { attr_reader :valid_params }
end

Instance Method Details

#fixture(name, ext = '.txt') ⇒ Object



58
59
60
# File 'vendor_modules/lvm/spec/lib/helpers.rb', line 58

def fixture(name, ext = '.txt')
  "#{TEST_DIR}fixtures#{name}#{ext}".read
end

#should_create(type, &block) ⇒ Object

Sets up an expection that a resource for type is created



37
38
39
40
41
# File 'vendor_modules/lvm/spec/lib/helpers.rb', line 37

def should_create(type, &block)
  raise "Invalid type #{type}" unless TYPES[type]

  Puppet::Type.type(TYPES[type]).expects(:new).with(&block)
end

#should_not_create(type) ⇒ Object

Sets up an expection that a resource for type is not created



30
31
32
33
34
# File 'vendor_modules/lvm/spec/lib/helpers.rb', line 30

def should_not_create(type)
  raise "Invalid type #{type}" unless TYPES[type]

  Puppet::Type.type(TYPES[type]).expects(:new).never
end

#specifying(opts = {}, &block) ⇒ Object

Returns a lambda creating a resource (ready for use with should)



24
25
26
27
# File 'vendor_modules/lvm/spec/lib/helpers.rb', line 24

def specifying(opts = {}, &block)
  specification = -> { with(opts) }
  block ? (yield specification) : specification
end

#stub_default_provider! ⇒ Object

Stub the default provider to get around confines for testing

Raises:

  • (ArgumentError)


51
52
53
54
55
56
# File 'vendor_modules/lvm/spec/lib/helpers.rb', line 51

def stub_default_provider!
  raise ArgumentError, '@type must be set' unless defined?(@type)

  provider = @type.provider(:lvm)
  @type.stubs(defaultprovider: provider)
end

#valid_params_without(*keys) ⇒ Object

Return the @valid_params without one or more keys Note: Useful since resource types don't like it when nil is passed as a parameter value



46
47
48
# File 'vendor_modules/lvm/spec/lib/helpers.rb', line 46

def valid_params_without(*keys)
  valid_params.reject { |k, _v| keys.include?(k) }
end

#with(opts = {}, &block) ⇒ Object

Creates a new resource of type



18
19
20
21
# File 'vendor_modules/lvm/spec/lib/helpers.rb', line 18

def with(opts = {}, &block)
  resource = @type.new(opts)
  block ? (yield resource) : resource
end