Skip to content
Browse files

rspec test cases for new functions, with dummy files to test

  • Loading branch information...
1 parent 605004d commit 90caf24d8f9575555f2b4ce9246d8c681a8483de Jenkins CI committed Jul 30, 2012
View
1 Gemfile
@@ -17,4 +17,5 @@ group :development do
gem "mixlib-log", ">= 0"
gem "git", ">= 1.2.5"
gem "rake", ">= 0"
+ gem "foodcritic", "~> 1.4.0"
end
View
2 lib/chef/application/jenkins.rb
@@ -56,7 +56,7 @@ class Chef::Application::Jenkins < Chef::Application
option :test,
:short => "-t TESTS",
:long => "--test TESTS",
- :description => "Add test(s) before uploading to chef server"
+ :description => "Add test(s) before uploading to chef server; -t ruby,foodcritic"
option :cookbook_freeze,
:short => "-f",
View
80 lib/chef/jenkins.rb
@@ -48,6 +48,8 @@ def initialize
@git.config("user.email", Chef::Config[:jenkins][:git_email])
end
+ # Automatically bump patch level if this is not done by any user.
+ # New added cookbook will not trigger auto bump.
def bump_patch_level(metadatarb, cookbook_name)
File.open(metadatarb, 'r+') do |f|
lines = f.readlines
@@ -77,6 +79,7 @@ def bump_patch_level(metadatarb, cookbook_name)
end
end
+ # Find all cookbooks from cookbook_path that configure by user
def find_all_cookbooks(cookbook_path=Chef::Config[:cookbook_path])
changed_cookbooks = []
cookbook_path.each do |path|
@@ -91,6 +94,8 @@ def find_all_cookbooks(cookbook_path=Chef::Config[:cookbook_path])
changed_cookbooks.uniq
end
+ # Find changed cookbooks between two versions
+ # Will return cookbook name, not a path
def find_changed_cookbooks(sha1, sha2, cookbook_path=Chef::Config[:cookbook_path], repo_path=Chef::Config[:jenkins][:repo_dir])
changed_cookbooks = []
@git.diff(sha1, sha2).each do |diff_file|
@@ -104,6 +109,8 @@ def find_changed_cookbooks(sha1, sha2, cookbook_path=Chef::Config[:cookbook_path
changed_cookbooks.uniq
end
+ # Find all roles from configured role_path
+ # Will return full path
def find_all_roles(role_path=Chef::Config[:role_path])
changed_roles = []
role_path.each do |path|
@@ -118,6 +125,8 @@ def find_all_roles(role_path=Chef::Config[:role_path])
changed_roles.uniq
end
+ # Find changed roles between two versions
+ # Will return full path
def find_changed_roles(sha1, sha2, role_path=Chef::Config[:role_path], repo_path=Chef::Config[:jenkins][:repo_dir])
changed_roles = []
@git.diff(sha1, sha2).each do |diff_file|
@@ -131,6 +140,8 @@ def find_changed_roles(sha1, sha2, role_path=Chef::Config[:role_path], repo_path
changed_roles.uniq
end
+ # Find all data_bag items between two versions
+ # Will return full path
def find_all_data_bags(data_bag_path=Chef::Config[:data_bag_path])
changed_data_bags = []
Dir[File.join(File.expand_path(data_bag_path[0]), '*')].each do |path|
@@ -147,6 +158,8 @@ def find_all_data_bags(data_bag_path=Chef::Config[:data_bag_path])
changed_data_bags.uniq
end
+ # Find changed data_bag items between two versions
+ # Will return full path
def find_changed_data_bags(sha1, sha2, data_bag_path=Chef::Config[:data_bag_path], repo_path=Chef::Config[:jenkins][:repo_dir])
changed_data_bags = []
@git.diff(sha1, sha2).each do |diff_file|
@@ -164,6 +177,7 @@ def current_commit
@git.log(1)
end
+ # Write current commit (made by chef-jenkins) into a file, which will read by next build
def write_current_commit(path=Chef::Config[:jenkins][:repo_dir])
File.open(File.join(path, ".chef_jenkins_last_commit"), "w") do |f|
f.print(current_commit)
@@ -173,6 +187,7 @@ def write_current_commit(path=Chef::Config[:jenkins][:repo_dir])
true
end
+ # Read the last commit made by chef-jenkins
def read_last_commit(path=Chef::Config[:jenkins][:repo_dir])
if File.exists?(File.join(path, ".chef_jenkins_last_commit"))
IO.read(File.join(path, ".chef_jenkins_last_commit"))
@@ -181,6 +196,8 @@ def read_last_commit(path=Chef::Config[:jenkins][:repo_dir])
end
end
+ # If automatically bumped patch level, commit those changes
+ # Commit changed env.json file(s)
def commit_cookbook_changes(cookbook_list=[])
begin
@git.commit("#{cookbook_list.length} cookbooks patch levels updated by Chef Jenkins\n\n" + cookbook_list.join("\n"), :add_all => true)
@@ -201,6 +218,7 @@ def git_branch(branch_name)
@git.branch(branch_name).checkout
end
+ # Make sure using the right upstream from config file
def add_upstream(upstream_url=Chef::Config[:jenkins][:repo_url])
begin
@git.add_remote("upstream", upstream_url)
@@ -209,10 +227,13 @@ def add_upstream(upstream_url=Chef::Config[:jenkins][:repo_url])
end
end
+ # Push the changes back to upstream, after chef-jenkins job made changes
+ # like bump version or updated env.json
def push_to_upstream(branch=Chef::Config[:jenkins][:branch])
@git.push("upstream", "HEAD:#{branch}")
end
+ # Upload cookbooks to chef server
def upload_cookbooks(cookbooks=[])
unless cookbooks.empty?
cu = Chef::Knife::CookbookUpload.new
@@ -224,6 +245,8 @@ def upload_cookbooks(cookbooks=[])
end
end
+ # Upload roles to chef server
+ # Input roles expecting full path to the role
def upload_roles(roles=[])
unless roles.empty?
cu = Chef::Knife::RoleFromFile.new
@@ -232,6 +255,8 @@ def upload_roles(roles=[])
end
end
+ # Upload data_bags to chef server
+ # Input data_bags expecting full path to the data_bag
def upload_data_bags(data_bags=[])
unless data_bags.empty?
data_bags.each do |data_bag_full_path|
@@ -245,6 +270,8 @@ def upload_data_bags(data_bags=[])
end
end
+ # * After a cookbook's version has been bumped, update that version to env.json too.
+ # * When propagating env, write the env_to.json with content of env_from.json.
def save_environment_file(env_to=Chef::Config[:jenkins][:env_to])
Chef::Log.info("Saving environmnent #{env_to} to #{env_to}.json")
dir = Chef::Config[:jenkins][:repo_dir]
@@ -259,17 +286,28 @@ def save_environment_file(env_to=Chef::Config[:jenkins][:env_to])
@git.commit("Updating #{env_to} with the latest cookbook versions", :allow_empty => true)
end
+ # Use the knife cookbook_test function provided by chef gem,
+ # result will be printed to STDOUT
+ # Expecting input,cookbooks, as a list of names, not paths
def knife_cookbook_test(cookbooks=[], cookbook_path=Chef::Config[:cookbook_path])
- Chef::Log.info("---- knife cookbook test ----")
+ puts "-------------------"
+ puts "knife cookbook test"
+ puts "-------------------"
cookbook_test = Chef::Knife::CookbookTest.new
cookbook_test.config[:cookbook_path] = cookbook_path
cookbook_test.config[:all] = false
cookbook_test.name_args = cookbooks
cookbook_test.run
- Chef::Log.info("==== Knife cookbook test passed ====")
+ puts "--------------------------"
+ puts "Knife cookbook test passed"
+ puts "--------------------------"
end
+ # Run foodcritic test
+ # Expecting input,cookbooks, as a list of names, not paths
def foodcritic_test(cookbooks=[], cookbook_path=Chef::Config[:cookbook_path])
+
+ # Convert names into full_paths, as foodcritic is expecting full_paths
full_path_cookbooks = []
cookbook_path.each do |path|
cookbooks.each do |cookbook|
@@ -278,22 +316,29 @@ def foodcritic_test(cookbooks=[], cookbook_path=Chef::Config[:cookbook_path])
end
end
- print "\n"
- Chef::Log.info("---- foodcritic ----")
+ puts "---------------"
+ puts "foodcritic test"
+ puts "---------------"
options = {}
options[:fail_tags] = Chef::Config[:jenkins][:foodcritic][:fail_tags]
options[:tags] = Chef::Config[:jenkins][:foodcritic][:tags]
options[:include_rules] = Chef::Config[:jenkins][:foodcritic][:include_rules]
- Chef::Log.info("foodcritic options: #{options}")
- Chef::Log.info("#{options.class}")
+ puts "foodcritic options: #{options}"
review = FoodCritic::Linter.new.check(full_path_cookbooks, options)
- FoodCritic::ContextOutput.new.output(review)
+ FoodCritic::SummaryOutput.new.output(review)
+
if review.failed?
- Chef::Log.info("==== Foodcritic failed ====")
- exit 0
+ puts "----------------------"
+ puts "Foodcritic test failed"
+ puts "----------------------"
+ exit 1
end
+ puts "----------------------"
+ puts "Foodcritic test passed"
+ puts "----------------------"
end
+ # Propagate cookbook version(s) from one environment to another
def prop(env_from=Chef::Config[:jenkins][:env_from], env_to=Chef::Config[:jenkins][:env_to])
add_upstream
@@ -311,6 +356,7 @@ def prop(env_from=Chef::Config[:jenkins][:env_from], env_to=Chef::Config[:jenkin
push_to_upstream
end
+ # Sync cookbooks, roles, and data_bags to chef_server while pushing changes to git repo
def sync(cookbook_path=Chef::Config[:cookbook_path], role_path=Chef::Config[:role_path], repo_dir=Chef::Config[:jenkins][:repo_dir])
add_upstream
@@ -331,36 +377,38 @@ def sync(cookbook_path=Chef::Config[:cookbook_path], role_path=Chef::Config[:rol
data_bags_to_change = find_all_data_bags
end
+ puts "==============================="
+ puts "Chef Jenkins output starts here"
+ puts "==============================="
if cookbooks_to_change.length == 0 || cookbooks_to_change.nil?
- Chef::Log.info("No cookbooks have changed")
+ puts "* No cookbooks have changed"
no_cookbook_change = true
end
if roles_to_change.length == 0 || roles_to_change.nil?
- Chef::Log.info("No roles have changed")
+ puts "* No roles have changed"
no_role_change = true
end
if data_bags_to_change.length == 0 || data_bags_to_change.nil?
- Chef::Log.info("No data_bags have changed")
+ puts "* No data_bags have changed"
no_data_bag_change = true
end
if no_cookbook_change and no_role_change and no_data_bag_change
- Chef::Log.info("Nothing to do, exit")
+ puts "* Nothing to do, exit"
exit 0
end
-
unless no_cookbook_change
# Run tests
tests = Chef::Config[:test]
if tests
print "\n"
- Chef::Log.info("==== Testing Now ====")
+ puts "## Testing Start"
knife_cookbook_test(cookbooks_to_change) if tests.include?("ruby")
foodcritic_test(cookbooks_to_change) if tests.include?("foodcritic")
- Chef::Log.info("==== Testing End ====")
+ puts "## Testing End"
end
# Bump cookbook patch version
View
25 spec/assets/config.rb
@@ -1,10 +1,27 @@
current_dir = File.dirname(__FILE__)
log_level :info
log_location STDOUT
-node_name "adam"
-client_key "#{current_dir}/adam.pem"
-chef_server_url "https://api.opscode.com/organizations/opscode"
+node_name "jenkins"
+client_key "#{ENV['HOME']}/.chef/jenkins.pem"
+chef_server_url "https://testing.chef-jenkins.org"
cache_type 'BasicFile'
cache_options( :path => "#{ENV['HOME']}/.chef/checksums" )
-cookbook_path ["#{current_dir}/cookbooks"]
+cookbook_path ["#{current_dir}/cookbooks", "#{current_dir}/site-cookbooks"]
+role_path ["#{current_dir}/roles"]
+data_bag_path ["#{current_dir}/data_bags"]
jenkins[:repo_path] = File.expand_path("#{current_dir}/../../")
+
+jenkins({
+ :repo_dir => current_dir,
+ :repo_url => 'ssh://git@git.promethost.com/chef',
+ :git_user => "Jenkins CI",
+ :git_email => "jenkins@promethost.com",
+ :env_to => "ops",
+ :branch => "master",
+ :foodcritic => {
+ :fail_tags => ["correctness"],
+ :tags => [],
+ :include_rules => []
+ }
+})
+
View
4 spec/assets/data_bags/groups/dev.json
@@ -0,0 +1,4 @@
+{
+ "gid": 320,
+ "id": "dev"
+}
View
4 spec/assets/data_bags/groups/ops.json
@@ -0,0 +1,4 @@
+{
+ "gid": 300,
+ "id": "ops"
+}
View
10 spec/assets/data_bags/users/foobar.json
@@ -0,0 +1,10 @@
+{
+ "groups": [
+ "ops"
+ ],
+ "comment": "Foo Bar",
+ "id": "foo",
+ "ssh_key": "test",
+ "password": "test"
+}
+
View
10 spec/assets/roles/apache2.rb
@@ -0,0 +1,10 @@
+name "apache2"
+description "Apache2/PHP role"
+run_list(
+ "recipe[apache2]",
+ "recipe[apache2::mod_php5]",
+ "recipe[apache2::mod_rewrite]",
+ "recipe[apache2::mod_expires]",
+)
+default_attributes(
+)
View
9 spec/assets/roles/vagrant.rb
@@ -0,0 +1,9 @@
+name "vagrant"
+description "Vagrant role"
+run_list(
+ "recipe[test::apt_update]"
+)
+
+default_attributes(
+ :active_sudo_users => ["vagrant"]
+)
View
54 spec/chef-jenkins_spec.rb
@@ -41,7 +41,7 @@
describe "bump_patch_level" do
it "updates metadata.rb to have an incremented patch version" do
- @cj.bump_patch_level(AH.file("metadata.rb"))
+ @cj.bump_patch_level(AH.file("metadata.rb"), "apache2")
has_correct_version = false
IO.foreach(AH.file("metadata.rb")) do |line|
if line =~ /^version '0\.99\.5'$/
@@ -63,12 +63,13 @@
end
end
- describe "commit_changes" do
+ describe "commit_cookbook_changes" do
it "commits changes to git, with the number and list of cookbooks" do
cookbook_list = [ "apache2", "ntp" ]
+ cr = "\n"
@cj.git.stub!(:commit).and_return(true)
- @cj.git.should_receive(:commit).with("2 cookbooks patch levels updated by Chef Jenkins\n#{cookbook_list.join}", :add_all => true)
- @cj.commit_changes(cookbook_list)
+ @cj.git.should_receive(:commit).with("2 cookbooks patch levels updated by Chef Jenkins\n\n#{cookbook_list.join(cr)}", :add_all => true)
+ @cj.commit_cookbook_changes(cookbook_list)
end
end
@@ -92,11 +93,48 @@
end
describe "find_changed_cookbooks" do
- it "prints a list of cookbooks changed since two commits" do
- cblist = @cj.find_changed_cookbooks('5979a584b0c4e10df0a868609c0b4f0f74058860', '013162d426ed0b627470dbb3209ae7af5d7ef216', ["#{AH::ASSET_DIR}/cookbooks"])
+ it "prints a list of cookbooks changed since last commit" do
+ system("echo '#test' >> #{AH::INFLIGHT}/cookbooks/apache2/metadata.rb")
+ system("cd #{AH::INFLIGHT}; git commit -am 'changed cookbook apache2';")
+ cblist = @cj.find_changed_cookbooks('HEAD^', 'HEAD', ["#{AH::INFLIGHT}/cookbooks"])
cblist.include?("apache2").should == true
- cblist.include?("ntp").should == true
+ cblist.include?("ntp").should == false
+ end
+ end
+
+ describe "find_changed_roles" do
+ it "prints a list of roles changed since last commit" do
+ system("echo '#test' >> #{AH::INFLIGHT}/roles/apache2.rb")
+ system("echo '#test' >> #{AH::INFLIGHT}/roles/vagrant.rb")
+ system("cd #{AH::INFLIGHT}; git commit -am 'changed 2 roles';")
+ role_list = @cj.find_changed_roles('HEAD^', 'HEAD', ["#{AH::INFLIGHT}/roles"])
+ role_list = role_list.map {|i| File.basename(i)}
+ role_list.include?("apache2.rb").should == true
+ role_list.include?("vagrant.rb").should == true
end
end
-end
+ describe "find_changed_data_bags" do
+ it "prints a list of data_bags changed since last commit" do
+ system("echo '#test' >> #{AH::INFLIGHT}/data_bags/users/foobar.json")
+ system("echo '#test' >> #{AH::INFLIGHT}/data_bags/groups/ops.json")
+ system("cd #{AH::INFLIGHT}; git commit -am 'changed 2 databags';")
+ data_bag_list = @cj.find_changed_data_bags('HEAD^', 'HEAD', ["#{AH::INFLIGHT}/data_bags"])
+ data_bag_list = data_bag_list.map {|i| File.basename(i)}
+ data_bag_list.include?("foobar.json").should == true
+ data_bag_list.include?("ops.json").should == true
+ end
+ end
+
+ describe "knife cookbook test" do
+ it "test cookbook(s) with knife cookbook test" do
+ @cj.knife_cookbook_test(["ntp","apache2"])
+ end
+ end
+
+ describe "foodcritic test" do
+ it "test cookbook(s) foodcritic" do
+ @cj.foodctitic_test(["ntp","apache2"])
+ end
+ end
+end
View
2 spec/spec_helper.rb
@@ -16,6 +16,8 @@ def self.reset!
system("rm -rf #{INFLIGHT}")
system("mkdir -p #{INFLIGHT}")
system("cp -r #{File.expand_path(File.join(ASSET_DIR, "*"))} #{INFLIGHT}")
+ #
+ system("cd #{INFLIGHT}; git init; git add .; git commit -m 'init';")
true
end

0 comments on commit 90caf24

Please sign in to comment.
Something went wrong with that request. Please try again.