poise / poise-derived

A Chef cookbook for defining lazily evaluated node attributes.
Apache License 2.0
22 stars 5 forks source link

Alias lazy method to something else #1

Open szymonpk opened 7 years ago

szymonpk commented 7 years ago

I'm using chef 12.13.37, chefspec 4.7.0.

It's just an idea, today I've encountered strange issue, when I'm testing (using chefspec) cookbook which depends on cookbook where I'm using lazy in attributes, I'm getting ArgumentError exception. Looks like upstream lazy method takes precedence over poise-derived. I wasn't able to isolate it to separate code yet, but maybe it would be good to use different method name - derived?

     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/mixin/params_validate.rb:116:in `lazy'
     # ./cookbooks/generic_server/attributes/openssl.rb:2:in `from_file'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/mixin/from_file.rb:30:in `instance_eval'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/mixin/from_file.rb:30:in `from_file'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/dsl/include_attribute.rb:39:in `block in include_attribute'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/dsl/include_attribute.rb:31:in `each'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/dsl/include_attribute.rb:31:in `include_attribute'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/run_context/cookbook_compiler.rb:181:in `load_attribute_file'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/run_context/cookbook_compiler.rb:174:in `block in load_attributes_from_cookbook'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/run_context/cookbook_compiler.rb:173:in `each'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/run_context/cookbook_compiler.rb:173:in `load_attributes_from_cookbook'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/run_context/cookbook_compiler.rb:110:in `block in compile_attributes'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/run_context/cookbook_compiler.rb:109:in `each'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/run_context/cookbook_compiler.rb:109:in `compile_attributes'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/run_context/cookbook_compiler.rb:72:in `compile'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/run_context.rb:176:in `load'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/policy_builder/expand_node_object.rb:97:in `setup_run_context'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/client.rb:510:in `setup_run_context'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chefspec-4.7.0/lib/chefspec/solo_runner.rb:118:in `converge'
coderanger commented 7 years ago

Hmm, I specifically accounted for that. Are you doing anything odd in your attributes file?

szymonpk commented 7 years ago

It's example we've talked yesterday on slack, if I change full method calls with just lazy and run chef spec on cookbook (postgresql_server) which depends on this one (generic_server), it fails as described above, these attributes aren't even used in postgresql_wrapper.

default["generic_server"]["openssl"]["url"] = PoiseDerived::LazyAttribute.new(
  node,
  "https://www.openssl.org/source/openssl-%{generic_server.openssl.version}.tar.gz"
)
default["generic_server"]["openssl"]["checksum"] = "e7aff292be21c259c6af26469c7a9b3ba26e9abaaffd325e3dccc9785256c431"
default["generic_server"]["openssl"]["version"]  = "1.0.2j"
default["generic_server"]["openssl"]["source_directory"] = PoiseDerived::LazyAttribute.new(
  node,
  "/usr/src/openssl-%{generic_server.openssl.version}"
)
coderanger commented 7 years ago

Something smells fishy here. Using the class directly (or the internal form _lazy_attribute instead of lazy) should definitely work in this case. Check for recipe sync issues?

szymonpk commented 7 years ago

Sorry maybe my example wasn't clear PoiseDerived::LazyAttribute.new... works - no exception, lazy doesn't - exception is present.

What do you mean by recipe sync issues?

coderanger commented 7 years ago

Never mind, if the class works then it isn't what I was thinking of. If you run with -l debug, do you see a log line [poise-derived] Installing node DSL? That is the point where it activates the override for lazy. More generally, if you just grep the debug output for poise-derived it would give us some info to work with.

szymonpk commented 7 years ago

Looks like something isn't loading when testing wrapper cookbook.

If I'm testing postgresql wrapper:

[2016-09-30T11:01:32+02:00] DEBUG: Cookbooks to compile: [:compat_resource, :apt, :"build-essential", :chef_handler, :windows, :seven_zip, :ark, :bsw_gpg, :"chef-sugar", :openssl, :postgresql, :database, :yum, :datadog, :ohai, :aws, :cron, :logrotate, :"chef-client", :"chef-vault", :"yum-epel", :fail2ban, :homebrew, :java, :dpkg_autostart, :ubuntu, :"monit-ng", :ntp, :omnibus_updater, :"poise-derived", :poise, :"poise-archive", :"poise-languages", :"poise-python", :postfix, :rsyslog, :"ssh-keys", :sudo, :sysctl, :hostsfile, :system, :ulimit, :generic_server, :postgresql_server]
[2016-09-30T11:01:32+02:00] DEBUG: Loading cookbook poise-derived's library file: /home/szymon/site-cookbooks/poise-derived/libraries/default.rb

When testing generic_server directly

[2016-09-30T11:05:19+02:00] DEBUG: Cookbooks to compile: [:compat_resource, :apt, :"build-essential", :chef_handler, :windows, :seven_zip, :ark, :ohai, :aws, :cron, :logrotate, :"chef-client", :"chef-sugar", :"chef-vault", :yum, :datadog, :"yum-epel", :fail2ban, :homebrew, :java, :dpkg_autostart, :ubuntu, :"monit-ng", :ntp, :omnibus_updater, :"poise-derived", :poise, :"poise-archive", :"poise-languages", :"poise-python", :postfix, :rsyslog, :"ssh-keys", :sudo, :sysctl, :hostsfile, :system, :ulimit, :generic_server]
[2016-09-30T11:05:19+02:00] DEBUG: Loading cookbook poise-derived's library file: /home/szymon/site-cookbooks/poise-derived/libraries/default.rb
[2016-09-30T11:05:19+02:00] DEBUG: [poise-derived] Installing event handler
[2016-09-30T11:05:19+02:00] DEBUG: [poise-derived] Installing node DSL
[2016-09-30T11:05:19+02:00] DEBUG: [poise-derived] Uninstalling node DSL

In metadata.rb only generic_server depends on poise-derived and postgresql_server wrapper depends on generic_server.

coderanger commented 7 years ago

What kind of testing is this, ChefSpec or Test Kitchen?

szymonpk commented 7 years ago

ChefSpec with SoloRunner.

coderanger commented 7 years ago

Does your postgresql_server cookbook work under Test Kitchen? That would at least narrow it to an interaction between ChefSpec and my code.

szymonpk commented 7 years ago

Yes, everything works fine when running TestKitchen.

coderanger commented 7 years ago

Okay, so some kind of interaction with ChefSpec. I'll see if I can repro tomorrow. Not sure I see how it could be failing to at least install the handler, even if the handler doesn't activate but I'm sure it will be clearer with a repro I can poke at :)

wduncanfraser commented 7 years ago

I'm seeing the same in when trying to run chefspec with lazy attributes. Converges in test kitchen just fine.

Example attributes default['mta_mantis']['mantis']['version'] = '2.3.1' default['mta_mantis']['mantis']['name'] = lazy 'mantis-%{mta_mantis.mantis.version}'

Error:

1) mta_mantis::firewall When all attributes are set to default on a supported platform_family converges successfully
     Failure/Error: chef_run.converge(described_recipe)

     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/chefspec20170426-4044-1istpmbfile_cache_path/cookbooks/mta_mantis/attributes/default.rb:7:in `from_file'
     # ./spec/unit/firewall_spec.rb:12:in `block (3 levels) in <top (required)>'

Using _lazy_attribute works just fine.