Class: RSpec::Puppet::TypeMatchers::CreateGeneric

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec-puppet/matchers/type_matchers.rb

Instance Method Summary collapse

Constructor Details

#initialize(*_args) ⇒ CreateGeneric

Returns a new instance of CreateGeneric.



6
7
8
9
10
11
12
13
14
# File 'lib/rspec-puppet/matchers/type_matchers.rb', line 6

def initialize(*_args)
  @exp_provider = nil
  @exp_parameters       = []
  @exp_properties       = []
  @exp_features         = []
  @exp_defaults         = {}
  @params_with_values   = {}
  @errors               = []
end

Instance Method Details

#be_valid_resource(type, title, params) ⇒ Object

builds the resource with the specified param values



89
90
91
92
# File 'lib/rspec-puppet/matchers/type_matchers.rb', line 89

def be_valid_resource(type, title, params)
  params[:name] ||= title
  type.new(params)
end

#descriptionObject



110
111
112
# File 'lib/rspec-puppet/matchers/type_matchers.rb', line 110

def description
  'be a valid type'
end

#failure_messageObject



114
115
116
# File 'lib/rspec-puppet/matchers/type_matchers.rb', line 114

def failure_message
  "Not a valid type #{@errors.inspect}"
end

#match_default_provider(resource) ⇒ Object

checks that the expected provider is set



97
98
99
100
101
102
103
# File 'lib/rspec-puppet/matchers/type_matchers.rb', line 97

def match_default_provider(resource)
  return true unless @exp_provider
  return true if resource[:provider] == @exp_provider

  @errors.push("Expected provider: #{@exp_provider} does not match: #{resource[:provider]}")
  false
end

#match_default_values(_resource) ⇒ Object



105
106
107
108
# File 'lib/rspec-puppet/matchers/type_matchers.rb', line 105

def match_default_values(_resource)
  # TODO: FINISH
  true
end

#match_features(type) ⇒ Object

checks that the specified features exist



84
85
86
# File 'lib/rspec-puppet/matchers/type_matchers.rb', line 84

def match_features(type)
  match_attrs(type, @exp_features, :feature)
end

#match_params(type) ⇒ Object

checks that the specified params exist



74
75
76
# File 'lib/rspec-puppet/matchers/type_matchers.rb', line 74

def match_params(type)
  match_attrs(type, @exp_parameters, :parameter)
end

#match_props(type) ⇒ Object

checks that the specified properties exist



79
80
81
# File 'lib/rspec-puppet/matchers/type_matchers.rb', line 79

def match_props(type)
  match_attrs(type, @exp_properties, :property)
end

#matches?(type_title_and_params) ⇒ Boolean

this is the method that drives all of the validation

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rspec-puppet/matchers/type_matchers.rb', line 57

def matches?(type_title_and_params)
  type   = type_title_and_params[0]
  title  = type_title_and_params[1]
  params = type_title_and_params[2]
  return false unless match_params(type) && match_props(type) && match_features(type)

  if @params_with_values != {} || @exp_provider
    # only build a resource if we are validating provider or setting
    # additional parameters
    resource = be_valid_resource(type, title, params.merge(@params_with_values))
    match_default_provider(resource) and match_default_values(resource)
  else
    true
  end
end

#with_defaults(defaults_hash) ⇒ Object



49
50
51
52
# File 'lib/rspec-puppet/matchers/type_matchers.rb', line 49

def with_defaults(defaults_hash)
  @exp_defaults.merge!(defaults_hash)
  self
end

#with_features(features) ⇒ Object

ensure the type has the list of features



35
36
37
38
# File 'lib/rspec-puppet/matchers/type_matchers.rb', line 35

def with_features(features)
  @exp_features |= Array(features)
  self
end

#with_parameters(params) ⇒ Object

ensures the listed parameters are valid



29
30
31
32
# File 'lib/rspec-puppet/matchers/type_matchers.rb', line 29

def with_parameters(params)
  @exp_parameters |= Array(params)
  self
end

#with_properties(props) ⇒ Object

ensures the listed properties are valid



23
24
25
26
# File 'lib/rspec-puppet/matchers/type_matchers.rb', line 23

def with_properties(props)
  @exp_properties |= Array(props)
  self
end

#with_provider(name) ⇒ Object

specifies a provider to validate



17
18
19
20
# File 'lib/rspec-puppet/matchers/type_matchers.rb', line 17

def with_provider(name)
  @exp_provider = name
  self
end

#with_set_attributes(params) ⇒ Object

ensures that the specified parameters with their values results in a valid resource



44
45
46
47
# File 'lib/rspec-puppet/matchers/type_matchers.rb', line 44

def with_set_attributes(params)
  @params_with_values.merge!(params)
  self
end