Skip to content

Add function ensure_resource and defined_with_params #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 15, 2012

Conversation

bodepd
Copy link

@bodepd bodepd commented Aug 8, 2012

This commit adds 2 new functions with unit tests.

defined_with_params works similarily to puppet's defined
function, except it allows you to also specify a hash of
params. defined_with_params will return true if a resource
also exists that matches the specified type/title (just like
with defined) as well as all of the specified params.

ensure_resource is a function that basically combines defined_with_params
with create_resources to conditionally create resources only if the
specified resource (title, type, params) does not already exist.

These functions are created to serve as an alternative to using
defined as follows:

if ! defined(Package['some_package']) {
  package { 'some_package': ensure => present,
}

The issue with this usage is that there is no guarentee about
what parameters were set in the previous definition of the package
that made its way into the catalog.

ensure_resource could be used instead, as:

ensure_resource('package', 'some_package', { 'ensure' => 'present' })

This will creat the package resources only if another resource does
not exist with the specified parameters.

@bodepd
Copy link
Author

bodepd commented Aug 8, 2012

it looks like these tests won't pass until this pull request gets merged into rspec puppet:

rodjek/rspec-puppet#36

@bodepd
Copy link
Author

bodepd commented Aug 9, 2012

the test dependencies are now merged into rspec-puppet and available in version: 0.1.4

@jeffmccune
Copy link
Contributor

@bodepd

I have a number of concerns that need to be addressed before this gets merged. I've put some effort into addressing my own concerns with discrete commits. Please bring in the following topic branch and review my commits for concrete examples: https://github.com/jeffmccune/puppetlabs-stdlib/tree/feature/2.4.x/ensure_resource_function

Here they are:

Housekeeping related:

  1. This pull request can target master but it should be merged into 2.4.x or whatever the next minor version of stdlib is.
  2. Avoid calling module methods with :: and use . instead. See 7a7cf55.
  3. Please review inline documentation for small typos. (Hash instead of has)
  4. Please reflow inline documentation to 80 characters.

Conceptual:

Reading the code, it appears that the ensure_resource function will not raise an error if defined_with_params returns false. I assume the implementation relies on create_resources() to raise a duplicate resource exception or something. If this assumption is true it should be included as a comment inline with the call to create_resources(). If the assumption is false, please add an inline comment to let the reader know what's going on.

Implementation:

If the user calls this function with a variable set to undef as the third (params) argument, what happens? I'm thinking something like this:

$rsrc_params = $::osfamily? {
  RedHat => { ensure => present },
  default => undef,
}
ensure_resource('user', 'root', $rsrc_params)

@ghost ghost assigned bodepd Aug 13, 2012
@jeffmccune
Copy link
Contributor

Also, I'm not sure the spec tests are working. I have rspec-puppet 0.1.4 and I'm getting this:

$ rspec spec/functions 
/Users/jeff/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': iconv will be deprecated in the future, use String#encode instead.
.Could not load confine test 'operatingsystem': cannot load such file -- puppet/provider/confine/operatingsystem
Could not load confine test 'operatingsystem': cannot load such file -- puppet/provider/confine/operatingsystem
Could not load confine test 'operatingsystem': cannot load such file -- puppet/provider/confine/operatingsystem
Could not load confine test 'operatingsystem': cannot load such file -- puppet/provider/confine/operatingsystem
Could not load confine test 'operatingsystem': cannot load such file -- puppet/provider/confine/operatingsystem
FFFFF

Failures:

  1) defined_with_params when compared against a resource with no attributes 
     Failure/Error: should run.with_params('User[dan]', {}).and_return(true)
     NameError:
       undefined method `function_defined_with_params' for class `Puppet::Parser::Scope'
     # /workspace/a/src/rspec-puppet/lib/rspec-puppet/example/function_example_group.rb:31:in `method'
     # /workspace/a/src/rspec-puppet/lib/rspec-puppet/example/function_example_group.rb:31:in `subject'
     # ./spec/functions/defined_with_params_spec.rb:14:in `block (3 levels) in <top (required)>'

  2) defined_with_params when comparted against a resource with attributes 
     Failure/Error: should run.with_params('User[dan]', {}).and_return(true)
     NameError:
       undefined method `function_defined_with_params' for class `Puppet::Parser::Scope'
     # /workspace/a/src/rspec-puppet/lib/rspec-puppet/example/function_example_group.rb:31:in `method'
     # /workspace/a/src/rspec-puppet/lib/rspec-puppet/example/function_example_group.rb:31:in `subject'
     # ./spec/functions/defined_with_params_spec.rb:25:in `block (3 levels) in <top (required)>'

  3) ensure_resource when a type or title is not specified 
     Failure/Error: should run.with_params().and_raise_error(ArgumentError)
     NameError:
       undefined method `function_ensure_resource' for class `Puppet::Parser::Scope'
     # /workspace/a/src/rspec-puppet/lib/rspec-puppet/example/function_example_group.rb:31:in `method'
     # /workspace/a/src/rspec-puppet/lib/rspec-puppet/example/function_example_group.rb:31:in `subject'
     # ./spec/functions/ensure_resource_spec.rb:8:in `block (3 levels) in <top (required)>'

  4) ensure_resource when compared against a resource with no attributes 
     Failure/Error: should run.with_params('user', 'dan', {})
     NameError:
       undefined method `function_ensure_resource' for class `Puppet::Parser::Scope'
     # /workspace/a/src/rspec-puppet/lib/rspec-puppet/example/function_example_group.rb:31:in `method'
     # /workspace/a/src/rspec-puppet/lib/rspec-puppet/example/function_example_group.rb:31:in `subject'     # ./spec/functions/ensure_resource_spec.rb:17:in `block (3 levels) in <top (required)>'  5) ensure_resource when comparted against a resource with attributes      Failure/Error: should run.with_params('User', 'dan', {})     NameError:       undefined method `function_ensure_resource' for class `Puppet::Parser::Scope'     # /workspace/a/src/rspec-puppet/lib/rspec-puppet/example/function_example_group.rb:31:in `method'     # /workspace/a/src/rspec-puppet/lib/rspec-puppet/example/function_example_group.rb:31:in `subject'     # ./spec/functions/ensure_resource_spec.rb:28:in `block (3 levels) in <top (required)>'Finished in 0.14981 seconds6 examples, 5 failuresFailed examples:

rspec ./spec/functions/defined_with_params_spec.rb:13 # defined_with_params when compared against a resource with no attributes rspec ./spec/functions/defined_with_params_spec.rb:24 # defined_with_params when comparted against a resource with attributes 
rspec ./spec/functions/ensure_resource_spec.rb:7 # ensure_resource when a type or title is not specified 
rspec ./spec/functions/ensure_resource_spec.rb:16 # ensure_resource when compared against a resource with no attributes 
rspec ./spec/functions/ensure_resource_spec.rb:26 # ensure_resource when comparted against a resource with attributes 

Here's my integration matrix:

$ rake
(in /workspace/a)
 * puppet                   2.7.x                                  2.7.19rc2-28-g4c91b65 4c91b65
 * facter                   1.6.x                                  1.6.11-4-g10314ad     10314ad
 * hiera                    1.x                                    1.0.0rc4-4-g46279a3   46279a3
 * rspec-puppet             master                                 v0.1.4                8d4c964
 * puppetlabs_spec_helper   master                                 0.2.0-10-gda76515     da76515
 * fog                      master                                 v1.5.0-79-gdfe1334    dfe1334
 * rbvmomi                  master                                 N/A                   5dc0ca3
 * stdlib                   feature/2.4.x/ensure_resource_function 2.3.3-35-g2888c01     2888c01
 * hiera_puppet             1.0rc                                  1.0.0rc2-2-gab0e254   ab0e254
 * mount_providers          master                                 0.0.1                 2d0dabe
 * pe_mcollective           master                                 0.0.45-2-g64c6523     64c6523
 * pe_accounts              master                                 1.0.3                 0725c16
 * pe_compliance            master                                 0.0.5                 dddc001
 * cloud_provisioner        master                                 1.0.5-2-g91e6b91      91e6b91
 * cloud_provisioner_vmware master                                 v1.0.0-5-g6eaab64     6eaab64
 * hiera_puppet             1.0rc                                  1.0.0rc2-2-gab0e254   ab0e254
 * mocha                    gem                                    0.10.5                N/A    
 * rack                     gem                                    1.4.1                 N/A    
 * rspec                    gem                                    2.10.0                N/A    
 * rspec-core               gem                                    2.10.1                N/A    
 * rspec-expectations       gem                                    2.10.0                N/A    
 * rspec-mocks              gem                                    2.10.1                N/A    

@jeffmccune
Copy link
Contributor

Finally, the tests added in this pull request appear to have behavior that depends on the current working directory of the process. While I got 5 of 6 failures above, I get 6 of 6 failures with undefined run if I cd spec and re-execute rspec:

$ cd spec/
$ rspec functions/
/Users/jeff/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': iconv will be deprecated in the future, use String#encode instead.
FFFFFF

Failures:

  1) defined_with_params when a resource is not specified 
     Failure/Error: it { should run.with_params().and_raise_error(ArgumentError) }
     NameError:
       undefined local variable or method `run' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fe35d2f8a80>
     # ./functions/defined_with_params_spec.rb:7:in `block (3 levels) in <top (required)>'

  2) defined_with_params when compared against a resource with no attributes 
     Failure/Error: should run.with_params('User[dan]', {}).and_return(true)
     NameError:
       undefined local variable or method `run' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2:0x007fe35d315338>
     # ./functions/defined_with_params_spec.rb:14:in `block (3 levels) in <top (required)>'

  3) defined_with_params when comparted against a resource with attributes 
     Failure/Error: should run.with_params('User[dan]', {}).and_return(true)
     NameError:
       undefined local variable or method `run' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_3:0x007fe35d329ec8>
     # ./functions/defined_with_params_spec.rb:25:in `block (3 levels) in <top (required)>'

  4) ensure_resource when a type or title is not specified 
     Failure/Error: should run.with_params().and_raise_error(ArgumentError)
     NameError:
       undefined local variable or method `run' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x007fe35d336718>
     # ./functions/ensure_resource_spec.rb:8:in `block (3 levels) in <top (required)>'

  5) ensure_resource when compared against a resource with no attributes 
     Failure/Error: should run.with_params('user', 'dan', {})
     NameError:
       undefined local variable or method `run' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_2:0x007fe35b94a958>
     # ./functions/ensure_resource_spec.rb:17:in `block (3 levels) in <top (required)>'

  6) ensure_resource when comparted against a resource with attributes 
     Failure/Error: should run.with_params('User', 'dan', {})
     NameError:
       undefined local variable or method `run' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_3:0x007fe35b9cec80>
     # ./functions/ensure_resource_spec.rb:28:in `block (3 levels) in <top (required)>'

Finished in 0.00436 seconds
6 examples, 6 failures

Failed examples:

rspec ./functions/defined_with_params_spec.rb:7 # defined_with_params when a resource is not specified 
rspec ./functions/defined_with_params_spec.rb:13 # defined_with_params when compared against a resource with no attributes 
rspec ./functions/defined_with_params_spec.rb:24 # defined_with_params when comparted against a resource with attributes 
rspec ./functions/ensure_resource_spec.rb:7 # ensure_resource when a type or title is not specified 
rspec ./functions/ensure_resource_spec.rb:16 # ensure_resource when compared against a resource with no attributes 
rspec ./functions/ensure_resource_spec.rb:26 # ensure_resource when comparted against a resource with attributes 

@bodepd bodepd closed this Aug 13, 2012
@bodepd bodepd reopened this Aug 13, 2012
Dan Bode added 4 commits August 13, 2012 17:09
This commit adds 2 new functions with unit tests.

defined_with_params works similarily to puppet's defined
function, except it allows you to also specify a hash of
params. defined_with_params will return true if a resource
also exists that matches the specified type/title (just like
with defined) as well as all of the specified params.

ensure_resource is a function that basically combines defined_with_params
with create_resources to conditionally create resources only if the
specified resource (title, type, params) does not already exist.

These functions are created to serve as an alternative to using
defined as follows:

    if ! defined(Package['some_package']) {
      package { 'some_package': ensure => present,
    }

The issue with this usage is that there is no guarentee about
what parameters were set in the previous definition of the package
that made its way into the catalog.

ensure_resource could be used instead, as:

    ensure_resource('package', 'some_package', { 'ensure' => 'present' })

This will creat the package resources only if another resource does
not exist with the specified parameters.
This commit adds better handling of the case where
undef is passed as the parameter value.

This works by converting '' into []
This commit adds better inline documentation
explaining how replicate resource definitions can
occur if the resource exists and does not have
matching parameters.
This commit refactors to ensure 80 character lines.
@bodepd
Copy link
Author

bodepd commented Aug 14, 2012

the following commits resolve most of the above mentioned problems. The first commit has been modified to include the previous changes that existed in your branch.

The additional commits in this pull request handle other points brought up in this discussion.

  • the first issue related to testing failures can be resolved by the following pull request into rspec-puppet (I actually don't exactly understand how anything worked without this patch)
    Explicitly load functions for scope instance rodjek/rspec-puppet#38
  • the second issue related to testing failures may actually be another bug in rspec-puppet. it may take a few weeks before I have time to circle around to it.

@jeffmccune
Copy link
Contributor

@bodepd

This still seems to be breaking, even when I'm using the pull request you mentioned on rspec-puppet. Here's what I'm getting:

$ rake
(in /workspace/a)
 * puppet                   master                                     2.7.19rc2-1218-g281d9f8 281d9f8
 * facter                   2.x                                        2.0.0rc4-137-gcc8f574   cc8f574
 * hiera                    1.x                                        1.0.0rc4-4-g46279a3     46279a3
 * rspec-puppet             bodepd-load_functions_explicitly           v0.1.3-2-g263998c       263998c
 * puppetlabs_spec_helper   master                                     0.2.0-10-gda76515       da76515
 * fog                      master                                     v1.5.0-79-gdfe1334      dfe1334
 * rbvmomi                  master                                     N/A                     5dc0ca3
 * stdlib                   feature/2.4.x/dan_ensure_resource_function 2.3.3-35-g9cf88e7       9cf88e7
 * hiera_puppet             1.0rc                                      1.0.0rc2-2-gab0e254     ab0e254
 * mount_providers          master                                     0.0.1                   2d0dabe
 * pe_mcollective           master                                     0.0.45-2-g64c6523       64c6523
 * pe_accounts              master                                     1.0.3                   0725c16
 * pe_compliance            master                                     0.0.5                   dddc001
 * cloud_provisioner        master                                     1.0.5-2-g91e6b91        91e6b91
 * cloud_provisioner_vmware master                                     v1.0.0-5-g6eaab64       6eaab64
 * hiera_puppet             1.0rc                                      1.0.0rc2-2-gab0e254     ab0e254
 * mocha                    gem                                        0.10.5                  N/A    
 * multi_json               gem                                        1.3.6                   N/A    
 * rack                     gem                                        1.4.1                   N/A    
 * rspec                    gem                                        2.10.0                  N/A    
 * rspec-core               gem                                        2.10.1                  N/A    
 * rspec-expectations       gem                                        2.10.0                  N/A     * rspec-mocks              gem                                        2.10.1                  N/A    

Could we pair on this quickly? I think I can help get this merged in today if I'm able to drive for a short while.

-Jeff

@jeffmccune
Copy link
Contributor

This is after modifying the Rakefile to also run tests against the functions sub directory:

diff --git a/Rakefile b/Rakefile
index 01b2a31..cbe8f28 100644
--- a/Rakefile
+++ b/Rakefile
@@ -5,7 +5,7 @@ task :default => [:test]

 desc 'Run RSpec'
 RSpec::Core::RakeTask.new(:test) do |t|
-  t.pattern = 'spec/{unit}/**/*.rb'
+  t.pattern = 'spec/{unit,functions}/**/*.rb'
   t.rspec_opts = ['--color']
 end


/Users/jeff/.rvm/rubies/ruby-1.9.3-p194/bin/ruby -S rspec spec/unit/facter/root_home_spec.rb spec/unit/facter/util/puppet_settings_spec.rb spec/unit/puppet/parser/functions/abs_spec.rb spec/unit/puppet/parser/functions/bool2num_spec.rb spec/unit/puppet/parser/functions/capitalize_spec.rb spec/unit/puppet/parser/functions/chomp_spec.rb spec/unit/puppet/parser/functions/chop_spec.rb spec/unit/puppet/parser/functions/delete_at_spec.rb spec/unit/puppet/parser/functions/delete_spec.rb spec/unit/puppet/parser/functions/downcase_spec.rb spec/unit/puppet/parser/functions/empty_spec.rb spec/unit/puppet/parser/functions/flatten_spec.rb spec/unit/puppet/parser/functions/fqdn_rotate_spec.rb spec/unit/puppet/parser/functions/get_module_path_spec.rb spec/unit/puppet/parser/functions/getvar_spec.rb spec/unit/puppet/parser/functions/grep_spec.rb spec/unit/puppet/parser/functions/has_key_spec.rb spec/unit/puppet/parser/functions/hash_spec.rb spec/unit/puppet/parser/functions/is_array_spec.rb spec/unit/puppet/parser/functions/is_domain_name_spec.rb spec/unit/puppet/parser/functions/is_float_spec.rb spec/unit/puppet/parser/functions/is_hash_spec.rb spec/unit/puppet/parser/functions/is_integer_spec.rb spec/unit/puppet/parser/functions/is_ip_address_spec.rb spec/unit/puppet/parser/functions/is_mac_address_spec.rb spec/unit/puppet/parser/functions/is_numeric_spec.rb spec/unit/puppet/parser/functions/is_string_spec.rb spec/unit/puppet/parser/functions/join_spec.rb spec/unit/puppet/parser/functions/keys_spec.rb spec/unit/puppet/parser/functions/lstrip_spec.rb spec/unit/puppet/parser/functions/member_spec.rb spec/unit/puppet/parser/functions/merge_spec.rb spec/unit/puppet/parser/functions/num2bool_spec.rb spec/unit/puppet/parser/functions/parsejson_spec.rb spec/unit/puppet/parser/functions/parseyaml_spec.rb spec/unit/puppet/parser/functions/prefix_spec.rb spec/unit/puppet/parser/functions/range_spec.rb spec/unit/puppet/parser/functions/reverse_spec.rb spec/unit/puppet/parser/functions/rstrip_spec.rb spec/unit/puppet/parser/functions/shuffle_spec.rb spec/unit/puppet/parser/functions/size_spec.rb spec/unit/puppet/parser/functions/sort_spec.rb spec/unit/puppet/parser/functions/squeeze_spec.rb spec/unit/puppet/parser/functions/str2bool_spec.rb spec/unit/puppet/parser/functions/str2saltedsha512_spec.rb spec/unit/puppet/parser/functions/strftime_spec.rb spec/unit/puppet/parser/functions/strip_spec.rb spec/unit/puppet/parser/functions/swapcase_spec.rb spec/unit/puppet/parser/functions/time_spec.rb spec/unit/puppet/parser/functions/to_bytes_spec.rb spec/unit/puppet/parser/functions/type_spec.rb spec/unit/puppet/parser/functions/unique_spec.rb spec/unit/puppet/parser/functions/upcase_spec.rb spec/unit/puppet/parser/functions/validate_absolute_path_spec.rb spec/unit/puppet/parser/functions/validate_array_spec.rb spec/unit/puppet/parser/functions/validate_bool_spec.rb spec/unit/puppet/parser/functions/validate_hash_spec.rb spec/unit/puppet/parser/functions/validate_re_spec.rb spec/unit/puppet/parser/functions/validate_slength_spec.rb spec/unit/puppet/parser/functions/validate_string_spec.rb spec/unit/puppet/parser/functions/values_at_spec.rb spec/unit/puppet/parser/functions/values_spec.rb spec/unit/puppet/parser/functions/zip_spec.rb spec/unit/puppet/provider/file_line/ruby_spec.rb spec/unit/puppet/type/anchor_spec.rb spec/unit/puppet/type/file_line_spec.rb spec/functions/defined_with_params_spec.rb spec/functions/ensure_resource_spec.rb --color
..............................................................................................................................................................................................................................................................................................................................................................FFFFFF

Failures:

  1) defined_with_params when a resource is not specified 
     Failure/Error: it { should run.with_params().and_raise_error(ArgumentError) }
     Puppet::DevError:
       you must pass a compiler instance to a new scope object
     # /workspace/a/src/puppet/lib/puppet/parser/scope.rb:142:in `initialize'
     # /workspace/a/src/rspec-puppet/lib/rspec-puppet/example/function_example_group.rb:29:in `new'
     # /workspace/a/src/rspec-puppet/lib/rspec-puppet/example/function_example_group.rb:29:in `subject'
     # ./spec/functions/defined_with_params_spec.rb:7:in `block (3 levels) in <top (required)>'

  2) defined_with_params when compared against a resource with no attributes 
     Failure/Error: should run.with_params('User[dan]', {}).and_return(true)
     Puppet::DevError:
       you must pass a compiler instance to a new scope object
     # /workspace/a/src/puppet/lib/puppet/parser/scope.rb:142:in `initialize'
     # /workspace/a/src/rspec-puppet/lib/rspec-puppet/example/function_example_group.rb:29:in `new'
     # /workspace/a/src/rspec-puppet/lib/rspec-puppet/example/function_example_group.rb:29:in `subject'
     # ./spec/functions/defined_with_params_spec.rb:14:in `block (3 levels) in <top (required)>'

  3) defined_with_params when compared against a resource with attributes 
     Failure/Error: should run.with_params('User[dan]', {}).and_return(true)
     Puppet::DevError:
       you must pass a compiler instance to a new scope object
     # /workspace/a/src/puppet/lib/puppet/parser/scope.rb:142:in `initialize'
     # /workspace/a/src/rspec-puppet/lib/rspec-puppet/example/function_example_group.rb:29:in `new'
     # /workspace/a/src/rspec-puppet/lib/rspec-puppet/example/function_example_group.rb:29:in `subject'
     # ./spec/functions/defined_with_params_spec.rb:25:in `block (3 levels) in <top (required)>'

  4) ensure_resource when a type or title is not specified 
     Failure/Error: should run.with_params().and_raise_error(ArgumentError)
     Puppet::DevError:
       you must pass a compiler instance to a new scope object
     # /workspace/a/src/puppet/lib/puppet/parser/scope.rb:142:in `initialize'
     # /workspace/a/src/rspec-puppet/lib/rspec-puppet/example/function_example_group.rb:29:in `new'
     # /workspace/a/src/rspec-puppet/lib/rspec-puppet/example/function_example_group.rb:29:in `subject'
     # ./spec/functions/ensure_resource_spec.rb:8:in `block (3 levels) in <top (required)>'

  5) ensure_resource when compared against a resource with no attributes 
     Failure/Error: should run.with_params('user', 'dan', {})
     Puppet::DevError:
       you must pass a compiler instance to a new scope object
     # /workspace/a/src/puppet/lib/puppet/parser/scope.rb:142:in `initialize'
     # /workspace/a/src/rspec-puppet/lib/rspec-puppet/example/function_example_group.rb:29:in `new'
     # /workspace/a/src/rspec-puppet/lib/rspec-puppet/example/function_example_group.rb:29:in `subject'
     # ./spec/functions/ensure_resource_spec.rb:17:in `block (3 levels) in <top (required)>'

  6) ensure_resource when compared against a resource with attributes 
     Failure/Error: should run.with_params('User', 'dan', {})
     Puppet::DevError:
       you must pass a compiler instance to a new scope object
     # /workspace/a/src/puppet/lib/puppet/parser/scope.rb:142:in `initialize'
     # /workspace/a/src/rspec-puppet/lib/rspec-puppet/example/function_example_group.rb:29:in `new'
     # /workspace/a/src/rspec-puppet/lib/rspec-puppet/example/function_example_group.rb:29:in `subject'
     # ./spec/functions/ensure_resource_spec.rb:28:in `block (3 levels) in <top (required)>'

Finished in 1.27 seconds
356 examples, 6 failures

Failed examples:

rspec ./spec/functions/defined_with_params_spec.rb:7 # defined_with_params when a resource is not specified 
rspec ./spec/functions/defined_with_params_spec.rb:13 # defined_with_params when compared against a resource with no attributes 
rspec ./spec/functions/defined_with_params_spec.rb:24 # defined_with_params when compared against a resource with attributes 
rspec ./spec/functions/ensure_resource_spec.rb:7 # ensure_resource when a type or title is not specified 
rspec ./spec/functions/ensure_resource_spec.rb:16 # ensure_resource when compared against a resource with no attributes 
rspec ./spec/functions/ensure_resource_spec.rb:26 # ensure_resource when compared against a resource with attributes 

jeffmccune pushed a commit that referenced this pull request Aug 15, 2012
Add function ensure_resource and defined_with_params
@jeffmccune jeffmccune merged commit 42ddd7f into puppetlabs:master Aug 15, 2012
@jeffmccune
Copy link
Contributor

@bodepd

OK, I went ahead and merged this in, but it's failing now against Puppet 2.6 so I'm going to revert this.

Please try and fix up the error logged here

Failures:

  1) ensure_resource when compared against a resource with attributes 
     Failure/Error: ).and_raise_error(Puppet::Error)
       expected ensure_resource("User", "dan", {"managehome"=>false, "ensure"=>"absent"}) to have raised Puppet::Error
     # ./spec/functions/ensure_resource_spec.rb:37

Finished in 3.37 seconds
363 examples, 1 failure, 9 pending

jeffmccune pushed a commit that referenced this pull request Aug 15, 2012
This reverts commit 42ddd7f, reversing
changes made to 5324360.
@bodepd
Copy link
Author

bodepd commented Aug 15, 2012

@jeffmccune I tried to recreate this issue (by checking out tag 2.6.17 from my local puppet installation)

I initially ran into the following error (every test failed with this error)

6) ensure_resource when compared against a resource with attributes 
   Failure/Error: Unable to find matching line from backtrace
   NameError:
     uninitialized constant Puppet::Test::LogCollector
   # /Users/danbode/dev/puppetlabs_spec_helper/lib/puppetlabs_spec_helper/puppet_spec_helper.rb:175

The issue was that the monkey patching code for that LogCollector was never evaluated b/c the namespace Puppet::Test::TestHelper existed.

I modified the conditional statement from puppet_spec_helper.rb to get around the initial issue and the saw all tests (including the one that is failing on that jenkins instance) passing on 2.6.17

# JJM Hack to make the stdlib tests run in Puppet 2.6 (See puppet comm    it cf183534)
122 if not (Puppet.constants.include? "Test" and Puppet::Test.constants.in    clude? "LogCollector" ) then
123 
124   module Puppet::Test

I have a feeling that I am missing something in the dependency matrix.

@jeffmccune
Copy link
Contributor

@bodepd Ah, thanks for catching that. I'll take a look at this when I get into the office.

We added the beginnings of a "real" API in Puppet itself to initialize Puppet for testing and it smells like the conditional logic in the spec helper isn't correctly determining if that API is present or not.

@jeffmccune
Copy link
Contributor

@bodepd

Something else is wrong. I'm not seeing that issue at all and I don't need to modify the conditional with Puppet 2.6.17. Are you sure you're running the correct versions of this integration matrix?

$ rspec -fd spec/functions
Warning: you appear to be using an older version of puppet; spec_helper will use fallback compatibility mode.

defined_with_params
  when a resource is not specified
    should run
  when compared against a resource with no attributes
    should run
  when compared against a resource with attributes
    should run

ensure_resource
  when a type or title is not specified
    should run
  when compared against a resource with no attributes
    should == "User[dan]"
  when compared against a resource with attributes
    should run (FAILED - 1)

Failures:

  1) ensure_resource when compared against a resource with attributes 
     Failure/Error: ).and_raise_error(Puppet::Error)
       expected ensure_resource("User", "dan", {"ensure"=>"absent", "managehome"=>false}) to have raised Puppet::Error
     # ./spec/functions/ensure_resource_spec.rb:37

Finished in 0.59287 seconds
6 examples, 1 failure

Failed examples:

Integration:

$ rake
(in /workspace/b)
 * puppet                   7195f9913d1e3c2d5be904b83311833858b4d931 2.6.17              N/A    
 * facter                   1.6.x                                    1.6.11-7-gc2554de   c2554de
 * hiera                    1.x                                      1.0.0rc4-6-g3b89fbe 3b89fbe
 * rspec-puppet             master                                   v0.1.4-2-g4afd64e   4afd64e
 * puppetlabs_spec_helper   master                                   0.3.0               8951405
 * fog                      0.7.2                                    v0.7.2              c7ec7c9
 * rbvmomi                  master                                   N/A                 5dc0ca3
 * stdlib                   bodepd-ensure_resource                   2.3.3-33-g5ffd33e   5ffd33e
 * hiera_puppet             v0.3.0                                   v0.3.0              06e70f3
 * mount_providers          master                                   0.0.1               2d0dabe
 * pe_mcollective           master                                   0.0.45-2-g64c6523   64c6523
 * pe_accounts              master                                   1.0.3               0725c16
 * pe_compliance            master                                   0.0.5               dddc001
 * cloud_provisioner        master                                   1.0.4-2-gf68dacd    f68dacd
 * cloud_provisioner_vmware master                                   v1.0.0-5-g6eaab64   6eaab64
 * hiera_puppet             v0.3.0                                   v0.3.0              06e70f3
 * mocha                    gem                                      0.10.0              N/A    
 * rack                     gem                                      1.4.1               N/A    
 * rspec                    gem                                      2.9.0               N/A    
 * rspec-core               gem                                      2.9.0               N/A    
 * rspec-expectations       gem                                      2.9.1               N/A    
 * rspec-mocks              gem                                      2.9.0               N/A    

jeffmccune pushed a commit to jeffmccune/puppetlabs-stdlib that referenced this pull request Aug 15, 2012
@jeffmccune
Copy link
Contributor

@bodepd There's definitely an order-dependent failure here, so this is an issue with the spec helper not initializing some global state in Puppet properly.

I'll take care of this issue and update the ticket accordingly.

While I'm also in here I'll fix up the legitimate failure in ./spec/functions/ensure_resource_spec.rb:37

@jeffmccune
Copy link
Contributor

For posterity this random seed triggers 180+ false positive failures: 44365

This seed isolates the failure to the singe legit failure: 10252

To run:

rspec --seed XXXXX spec

bodepd pushed a commit to bodepd/puppetlabs-stdlib that referenced this pull request Aug 15, 2012
jeffmccune pushed a commit that referenced this pull request Aug 16, 2012
* bodepd-ensure_resource_attempt_2:
  Explicitly load functions used by ensure_resource
  Revert "Revert "Merge pull request #86 from bodepd/ensure_resource""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants