test-kitchen / test-kitchen

Test Kitchen is an integration tool for developing and testing infrastructure code and software on isolated target platforms
Other
1.87k stars 583 forks source link

Can't add start/report/exception handlers to kitchen's client.rb #452

Open nBerg opened 10 years ago

nBerg commented 10 years ago

I'm trying to add a handler to the test-kitchen run. I tried adding it by doing some variations on this:

provisioner:
  name: chef_zero
  client_rb:
    require: "my_handler"
    start_handlers: "<< MyHandler::Handler.new"

Can't seem to get it to work. Is there a way to do this?

I just need it to add the lines require "my_handler" and start_handlers << MyHandler::Handler.new to the client.rb

sethvargo commented 10 years ago

Why?

nBerg commented 10 years ago

We're using a custom start handler that can affect the way a chef-client run happens. We'd like to test that.

For now, we're telling the kitchen client.rb to include our own file that sets up the start handler, but its not pretty.

If adding support for handlers isn't going to happen, adding something like

    Dir.glob(File.join('/etc/chef', 'client.d', '*.rb')).each do |conf|
      Chef::Config.from_file(conf)
    end

to the client.rb could be helpful.

sethvargo commented 10 years ago

If you're using a handler, that shouldn't affect the way a Chef Client run executes. If that's the case, it's a bug and should be fixed in Chef. If you are testing a Chef handler, you should do that using a recipe. I'm not really seeing the use case here.

spheromak commented 10 years ago

@sethvargo Start handlers run before the run_context loads the recipes https://github.com/opscode/chef/blob/master/lib/chef/client.rb#L425-L429 So to get the start handler to be tested you would have to converge 2 times, or set it up in kitchen. Maybe you can hup the client to trigger a new run.. Not sure.

spheromak commented 10 years ago

@sethvargo I guess you're saying @nBerg can test the handler independant from the chef-run, and that makes sense.

nBerg commented 10 years ago

We can test the handler separately, and we're working on that as well, but we use the handler to modify some attributes based on environmental factors.

We need these attributes to be set before any other recipes are compiled. We could set these attributes manually, but modifying the attributes of every machine we provision with chef would be time consuming. This handler is a quick and easy method to programmatically take care of that for us.

Yes we can set the attributes manually for the tests, and yes we can test the handler by itself, but we would like to also verify that everything works together the way we think it should. If I get a chance I can write a pull request implementing the feature, I was just checking to see if it already existed.

ko-be commented 10 years ago

@sethvargo We are using start handlers to set some Chef attributes that our cookbooks depend on. We are trying to add these two lines to the /tmp/kitchen/client.rb file that Kitchen creates:

require "my_handler"
start_handlers << MyHandler::Handler.new

But we are unable to add these lines, as Kitchen doesn't allow us to add the start_handlers line to the client.rb. Kitchen always adds double quotes to the "<< MyHandler::Handler.new" portion of the client.rb file in format_config_file() : https://github.com/test-kitchen/test-kitchen/blob/master/lib/kitchen/provisioner/chef_zero.rb#L106 https://github.com/test-kitchen/test-kitchen/blob/master/lib/kitchen/provisioner/chef_base.rb#L142

sethvargo commented 10 years ago

Yup, that's a known issue

karcaw commented 8 years ago

Is anyone working on it? I want to turn on a basic updated resource handler.