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, &block) ⇒ CreateGeneric

Returns a new instance of CreateGeneric.



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

def initialize(*args, &block)

  @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



94
95
96
97
# File 'lib/rspec-puppet/matchers/type_matchers.rb', line 94

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

#descriptionObject



120
121
122
# File 'lib/rspec-puppet/matchers/type_matchers.rb', line 120

def description
  "be a valid type"
end

#failure_messageObject



124
125
126
# File 'lib/rspec-puppet/matchers/type_matchers.rb', line 124

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

#match_default_provider(resource) ⇒ Object

checks that the expected provider is set



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rspec-puppet/matchers/type_matchers.rb', line 102

def match_default_provider(resource)
  if @exp_provider
    if resource[:provider] == @exp_provider
      return true
    else
      @errors.push("Expected provider: #{@exp_provider} does not match: #{resource[:provider]}")
      return false
    end
  else
    return true
  end
end

#match_default_values(resource) ⇒ Object



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

def match_default_values(resource)
  # TODO FINISH
  true
end

#match_features(type) ⇒ Object

checks that the specified features exist



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

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

#match_params(type) ⇒ Object

checks that the specified params exist



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

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

#match_props(type) ⇒ Object

checks that the specified properties exist



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

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)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rspec-puppet/matchers/type_matchers.rb', line 61

def matches?(type_title_and_params)
  type   = type_title_and_params[0]
  title  = type_title_and_params[1]
  params = type_title_and_params[2]
  unless match_params(type) && match_props(type) && match_features(type)
   return false
  end
  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



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

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

#with_features(features) ⇒ Object

ensure the type has the list of features



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

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

#with_parameters(params) ⇒ Object

ensures the listed parameters are valid



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

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

#with_properties(props) ⇒ Object

ensures the listed properties are valid



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

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

#with_provider(name) ⇒ Object

specifies a provider to validate



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

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



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

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