Skip to content

Commit

Permalink
Final fix for chef-492 - makes the context for the template provider …
Browse files Browse the repository at this point in the history
…always be distinct from the data we feed it in the variables method, to make sure we don't inadvertantly leak the node object into other persistent data.
  • Loading branch information
adamhjk authored and AJ Christensen committed Sep 4, 2009
1 parent ca6dd91 commit 3ead7a2
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ namespace :features do
end

namespace :provider do
Cucumber::Rake::Task.new(:template) do |t|
t.profile = "provider_template"
end

Cucumber::Rake::Task.new(:remote_file) do |t|
t.profile = "provider_remote_file"
end
Expand Down
8 changes: 8 additions & 0 deletions chef/lib/chef/node/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,14 @@ def method_missing(symbol, *args)
end
end

def to_hash
result = determine_value(current_override, current_attribute, current_default)
if result.class == Hash
result
else
result.to_hash
end
end

end
end
Expand Down
7 changes: 4 additions & 3 deletions chef/lib/chef/provider/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ def action_create
Chef::Log.debug("Updating template for #{@new_resource} in the cache")
Chef::FileCache.move_to(raw_template_file.path, cache_file_name)
end

context = @new_resource.variables

context = {}
context.merge!(@new_resource.variables)
context[:node] = @node
template_file = render_template(Chef::FileCache.load(cache_file_name), context)

Expand Down Expand Up @@ -137,4 +138,4 @@ def action_create_if_missing

end
end
end
end
12 changes: 12 additions & 0 deletions chef/spec/unit/node/attribute_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,18 @@
end
end

describe "to_hash" do
it "should convert to a hash" do
@attributes.to_hash.class.should == Hash
end

it "should convert to a hash based on current state" do
hash = @attributes["hot"].to_hash
hash.class.should == Hash
hash["day"].should == "sunday"
end
end

describe "has_key?" do
it "should return true if an attribute exists" do
@attributes.has_key?("music").should == true
Expand Down
1 change: 1 addition & 0 deletions cucumber.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ search: --tags search --format pretty -r features/steps -r features/support feat
language: --tags language --format pretty -r features/steps -r features/support features
recipe_inclusion: --tags recipe_inclusion --format pretty -r features/steps -r features/support features
provider_remote_file: --tags provider,remote_file --format pretty -r features/steps -r features/support features
provider_template: --tags template --format pretty -r features/steps -r features/support features
provider_package_macports: --tags macports --format pretty -r features/steps -r features/support features

8 changes: 8 additions & 0 deletions features/data/cookbooks/template/README.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
= DESCRIPTION:

= REQUIREMENTS:

= ATTRIBUTES:

= USAGE:

6 changes: 6 additions & 0 deletions features/data/cookbooks/template/metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
maintainer "Opscode"
maintainer_email "do_not_reply@opscode.com"
license "Apache 2.0"
description "Installs/Configures template"
long_description IO.read(File.join(File.dirname(__FILE__), 'README.rdoc'))
version "0.1"
26 changes: 26 additions & 0 deletions features/data/cookbooks/template/recipes/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
# Cookbook Name:: template
# Recipe:: default
#
# Copyright 2009, Opscode
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

node.set[:markov][:test] = { "value" => "sauce" }

template "#{node[:tmpdir]}/template.txt" do
source "template.txt.erb"
variables node[:markov][:test]
end

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= @value %>
13 changes: 13 additions & 0 deletions features/provider/template/template.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@provider @template
Feature: Templates
In order to easily manage many systems at once
As a Developer
I want to manage the contents of files programatically

Scenario: Render a template from a cookbook
Given a validated node
And it includes the recipe 'template'
When I run the chef-client
Then the run should exit '0'
And a file named 'template.txt' should contain 'sauce'

0 comments on commit 3ead7a2

Please sign in to comment.