diff options
author | Claire McQuin <claire@getchef.com> | 2014-08-05 16:38:25 -0700 |
---|---|---|
committer | Claire McQuin <claire@getchef.com> | 2014-08-13 09:21:20 -0700 |
commit | 0bbddd62b0c65781eacd43f2ca3334a1ed23c09e (patch) | |
tree | 65060f8717c6d6e35a67bd4958238ce72b970d02 | |
parent | 790a48bb8d908d66c87bdf0ab572e6e7194770aa (diff) | |
download | chef-0bbddd62b0c65781eacd43f2ca3334a1ed23c09e.tar.gz |
Create and test cookbook on Ubuntu 12.04
21 files changed, 369 insertions, 46 deletions
diff --git a/spec/e2e/.kitchen.travis.yml b/spec/e2e/.kitchen.travis.yml index 7b256fcdb2..6ea825fc12 100644 --- a/spec/e2e/.kitchen.travis.yml +++ b/spec/e2e/.kitchen.travis.yml @@ -10,6 +10,7 @@ provisioner: github: <%= ENV['TRAVIS_REPO_SLUG'] %> branch: <%= ENV['TRAVIS_COMMIT'] %> require_chef_omnibus: true + data_path: test/fixtures platforms: - name: ubuntu-12.04 @@ -31,7 +32,8 @@ platforms: username: ec2-user suites: - - name: default + - name: webapp run_list: - - recipe[git-cookbook::default] + - recipe[apt::default] + - recipe[webapp::default] attributes: diff --git a/spec/e2e/.kitchen.yml b/spec/e2e/.kitchen.yml index 0409e3821f..651a606502 100644 --- a/spec/e2e/.kitchen.yml +++ b/spec/e2e/.kitchen.yml @@ -4,13 +4,15 @@ provisioner: github: "opscode/chef" branch: <%= %x(git rev-parse HEAD) %> require_chef_omnibus: true + data_path: test/fixtures platforms: - name: ubuntu-12.04 driver_plugin: vagrant suites: - - name: default + - name: webapp run_list: - - recipe[git-cookbook::default] + - recipe[apt::default] + - recipe[webapp::default] attributes: diff --git a/spec/e2e/Berksfile b/spec/e2e/Berksfile index fad6f7f9c0..66569ed97f 100644 --- a/spec/e2e/Berksfile +++ b/spec/e2e/Berksfile @@ -1,3 +1,3 @@ source "https://supermarket.getchef.com" -cookbook 'git-cookbook', :path => 'cookbooks/git-cookbook' +cookbook "webapp", :path => "cookbooks/webapp" diff --git a/spec/e2e/Berksfile.lock b/spec/e2e/Berksfile.lock deleted file mode 100644 index 7e950ed790..0000000000 --- a/spec/e2e/Berksfile.lock +++ /dev/null @@ -1,6 +0,0 @@ -DEPENDENCIES - git-cookbook - path: cookbooks/git-cookbook - -GRAPH - git-cookbook (0.1.0) diff --git a/spec/e2e/Gemfile b/spec/e2e/Gemfile index 996dc12f66..f9bf007ac7 100644 --- a/spec/e2e/Gemfile +++ b/spec/e2e/Gemfile @@ -1,8 +1,10 @@ source "https://rubygems.org" -gem 'berkshelf' -gem 'test-kitchen', :github => 'mcquin/test-kitchen', - :branch => 'mcquin/install_chef_from_github' +group :end_to_end do + gem 'berkshelf' + gem 'test-kitchen', :github => 'mcquin/test-kitchen', + :branch => 'mcquin/install_chef_from_github' -gem 'kitchen-vagrant' -gem 'kitchen-ec2' + gem 'kitchen-vagrant' + gem 'kitchen-ec2' +end diff --git a/spec/e2e/cookbooks/git-cookbook/metadata.rb b/spec/e2e/cookbooks/git-cookbook/metadata.rb deleted file mode 100644 index 9c2e5b8c7c..0000000000 --- a/spec/e2e/cookbooks/git-cookbook/metadata.rb +++ /dev/null @@ -1,7 +0,0 @@ -name 'git-cookbook' -maintainer '' -maintainer_email '' -license '' -description 'Installs/Configures example' -long_description 'Installs/Configures example' -version '0.1.0' diff --git a/spec/e2e/cookbooks/git-cookbook/recipes/default.rb b/spec/e2e/cookbooks/git-cookbook/recipes/default.rb deleted file mode 100644 index 1203f7465e..0000000000 --- a/spec/e2e/cookbooks/git-cookbook/recipes/default.rb +++ /dev/null @@ -1,9 +0,0 @@ -# -# Cookbook Name:: git-cookbook -# Recipe:: default -# -# Copyright (C) 2014 -# -# - -package 'git' diff --git a/spec/e2e/cookbooks/git-cookbook/test/integration/default/rspec/spec_helper.rb b/spec/e2e/cookbooks/git-cookbook/test/integration/default/rspec/spec_helper.rb deleted file mode 100644 index e69de29bb2..0000000000 --- a/spec/e2e/cookbooks/git-cookbook/test/integration/default/rspec/spec_helper.rb +++ /dev/null diff --git a/spec/e2e/cookbooks/git-cookbook/test/integration/default/rspec/unit/git_installed_spec.rb b/spec/e2e/cookbooks/git-cookbook/test/integration/default/rspec/unit/git_installed_spec.rb deleted file mode 100644 index 3a2c3f971c..0000000000 --- a/spec/e2e/cookbooks/git-cookbook/test/integration/default/rspec/unit/git_installed_spec.rb +++ /dev/null @@ -1,13 +0,0 @@ - -require 'spec_helper' - -require 'chef/mixin/shell_out' - -describe "git-cookbook cookbook" do - include Chef::Mixin::ShellOut - - it "should install git" do - so = shell_out('which git') - so.exitstatus.should == 0 - end -end diff --git a/spec/e2e/cookbooks/webapp/Berksfile b/spec/e2e/cookbooks/webapp/Berksfile new file mode 100644 index 0000000000..4b6079016e --- /dev/null +++ b/spec/e2e/cookbooks/webapp/Berksfile @@ -0,0 +1,5 @@ +source "https://api.berkshelf.com" + +metadata + +cookbook "apt" diff --git a/spec/e2e/cookbooks/git-cookbook/README.md b/spec/e2e/cookbooks/webapp/README.md index 4431091bc1..e8de6ee467 100644 --- a/spec/e2e/cookbooks/git-cookbook/README.md +++ b/spec/e2e/cookbooks/webapp/README.md @@ -1,3 +1,4 @@ -# git-cookbook +# webapp TODO: Enter the cookbook description here. + diff --git a/spec/e2e/cookbooks/webapp/attributes/default.rb b/spec/e2e/cookbooks/webapp/attributes/default.rb new file mode 100644 index 0000000000..efe06b6549 --- /dev/null +++ b/spec/e2e/cookbooks/webapp/attributes/default.rb @@ -0,0 +1,7 @@ +default['apache']['remote_host_ip'] = '127.0.0.1' + +default['mysql']['version'] = "5.5" + +default['webapp']['database'] = 'webapp' +default['webapp']['db_username'] = 'webapp' +default['webapp']['path'] = '/var/www/' diff --git a/spec/e2e/cookbooks/webapp/metadata.rb b/spec/e2e/cookbooks/webapp/metadata.rb new file mode 100644 index 0000000000..ecfb419953 --- /dev/null +++ b/spec/e2e/cookbooks/webapp/metadata.rb @@ -0,0 +1,12 @@ +name 'webapp' +maintainer '' +maintainer_email '' +license '' +description 'Installs/Configures webapp' +long_description 'Installs/Configures webapp' +version '0.1.0' + +depends 'apache2' +depends 'database' +depends 'mysql' +depends 'php' diff --git a/spec/e2e/cookbooks/webapp/recipes/default.rb b/spec/e2e/cookbooks/webapp/recipes/default.rb new file mode 100644 index 0000000000..531dde240d --- /dev/null +++ b/spec/e2e/cookbooks/webapp/recipes/default.rb @@ -0,0 +1,63 @@ +# +# Cookbook Name:: webapp +# Recipe:: default +# +# Copyright (C) 2014 +# + +include_recipe "apache2" +# include_recipe "mysql::client" +# include_recipe "mysql::server" +include_recipe "database::mysql" +include_recipe "php" + +creds = Hash.new +%w(mysql webapp).each do |item_name| + creds[item_name] = data_bag_item('passwords', item_name) +end + +apache_site "default" do + enable true +end + +mysql_service "default" do + server_root_password creds['mysql']['server_root_password'] + server_repl_password creds['mysql']['server_repl_password'] +end + +mysql_database node['webapp']['database'] do + connection ({ + :host => 'localhost', + :username => 'root', + :password => creds['mysql']['server_root_password'] + }) + action :create +end + +mysql_database_user node['webapp']['db_username'] do + connection ({ + :host => 'localhost', + :username => 'root', + :password => creds['mysql']['server_root_password'] + }) + password creds['webapp']['db_password'] + database_name node['webapp']['database'] + privileges [:select, :update, :insert, :create, :delete] + action :grant +end + +directory node['webapp']['path'] do + owner "root" + group "root" + mode "0755" + action :create + recursive true +end + +template "#{node['webapp']['path']}/index.html" do + source 'index.html.erb' +end + +template "#{node['webapp']['path']}/index.php" do + source 'index.php.erb' +end diff --git a/spec/e2e/cookbooks/webapp/templates/default/index.html.erb b/spec/e2e/cookbooks/webapp/templates/default/index.html.erb new file mode 100644 index 0000000000..6da0629b9e --- /dev/null +++ b/spec/e2e/cookbooks/webapp/templates/default/index.html.erb @@ -0,0 +1,5 @@ +<html> + <body> + <h1>Hello, World!</h1> + </body> +</html> diff --git a/spec/e2e/cookbooks/webapp/templates/default/index.php.erb b/spec/e2e/cookbooks/webapp/templates/default/index.php.erb new file mode 100644 index 0000000000..b08b076614 --- /dev/null +++ b/spec/e2e/cookbooks/webapp/templates/default/index.php.erb @@ -0,0 +1,8 @@ +<html> + <head> + <title>PHP Test</title> + </head> + <body> + <?php echo '<p>Hello, World!</p>'; ?> + </body> +</html> diff --git a/spec/e2e/data_bags/passwords/mysql.json b/spec/e2e/data_bags/passwords/mysql.json new file mode 100644 index 0000000000..af02a6a858 --- /dev/null +++ b/spec/e2e/data_bags/passwords/mysql.json @@ -0,0 +1,5 @@ +{ + "id": "mysql", + "server_root_password": "ilikerandompasswordstoo", + "server_repl_password": "itoolikerandompasswords" +} diff --git a/spec/e2e/data_bags/passwords/webapp.json b/spec/e2e/data_bags/passwords/webapp.json new file mode 100644 index 0000000000..43c0ae1ced --- /dev/null +++ b/spec/e2e/data_bags/passwords/webapp.json @@ -0,0 +1,4 @@ +{ + "id": "webapp", + "db_password": "supersecretdbpassword" +} diff --git a/spec/e2e/test/fixtures/platforms/ubuntu/12.04.json b/spec/e2e/test/fixtures/platforms/ubuntu/12.04.json new file mode 100644 index 0000000000..5e436a3cb0 --- /dev/null +++ b/spec/e2e/test/fixtures/platforms/ubuntu/12.04.json @@ -0,0 +1,42 @@ +{ + "apache": { + "package": "apache2", + "binary": "/usr/sbin/apache2", + "dir": "/etc/apache2", + "lib_dir": "/usr/lib/apache2", + "libexec_dir": "/usr/lib/apache2/modules", + "cache_dir": "/var/cache/apache2", + "cgibin_dir": "/usr/lib/cgi-bin", + "docroot_dir": "/var/www", + "conf": "/etc/apache2/apache2.conf", + "perl_pkg": "perl", + "log_dir": "/var/log/apache2", + "root_group": "root", + "service_name": "apache2", + "service_restart_command": "/usr/sbin/invoke-rc.d apache2 restart && sleep 1", + "service_reload_command": "/usr/sbin/invoke-rc.d apache2 reload && sleep 1", + "default_site_name": "000-default", + "default_site_enabled": false, + "default_modules": [ + "status", + "alias", + "auth_basic", + "authn_file", + "authz_core", + "authz_groupfile", + "authz_host", + "authz_user", + "autoindex", + "dir", + "env", + "mime", + "negotiation", + "setenvif" + ] + }, + "mysql": { + "server": { + "version": "5.5" + } + } +} diff --git a/spec/e2e/test/fixtures/serverspec_helper.rb b/spec/e2e/test/fixtures/serverspec_helper.rb new file mode 100644 index 0000000000..3a2c05f9cf --- /dev/null +++ b/spec/e2e/test/fixtures/serverspec_helper.rb @@ -0,0 +1,74 @@ +# Shamelessly copied from opscode/onehealth-cookbooks/apache2/test/fixtures/serverspec_helper.rb +# The commented-out platforms in the osmapping hash can be added once we have added them into +# our .kitchen.yml and .kitchen.travis.yml and added the appropriate JSON under test/fixtures/platforms. + +require 'serverspec' +require 'json' + +include SpecInfra::Helper::Exec +include SpecInfra::Helper::DetectOS +include SpecInfra::Helper::Properties + +# http://serverspec.org/advanced_tips.html +# os[:family] # RedHat, Ubuntu, Debian and so on +# os[:release] # OS release version (cleaned up in v2) +# os[:arch] +osmapping = { +# 'RedHat' => { +# :platform_family => 'rhel', +# :platform => 'centos', +# :platform_version => '6.5' +# }, +# 'RedHat7' => { +# :platform_family => 'rhel', +# :platform => 'centos', +# :platform_version => '7.0' +# }, +# 'Fedora' => { +# :platform_family => 'rhel', +# :platform => 'fedora', +# :platform_version => '20' +# }, + 'Ubuntu' => { + :platform_family => 'debian', + :platform => 'ubuntu', + :platform_version => '12.04' + } +# 'Debian' => { +# :platform_family => 'debian', +# :platform => 'debian', +# :platform_version => '7.4' +# }, +# 'FreeBSD' => { +# :platform_family => 'freebsd', +# :platform => 'freebsd', +# :platform_version => '9.2' +# } +} + +def ohai_platform(os, osmapping) + puts "serverspec os detected as: #{os[:family]} #{os[:release]} [#{os[:arch]}]" + ohaistub = {} + ohaistub[:platform_family] = osmapping[os[:family]][:platform_family] + ohaistub[:platform] = osmapping[os[:family]][:platform] + if os[:release] + ohaistub[:platform_version] = os[:release] + else + ohaistub[:platform_version] = osmapping[os[:family]][:platform_version] + end + ohaistub +end + +def load_nodestub(ohai) + puts "loading #{ohai[:platform]}/#{ohai[:platform_version]}" + JSON.parse(IO.read("#{ENV['BUSSER_ROOT']}/../kitchen/data/platforms/#{ohai[:platform]}/#{ohai[:platform_version]}.json"), :symbolize_names => true) +end + +RSpec.configure do |config| + set_property load_nodestub(ohai_platform(backend.check_os, osmapping)) + config.before(:all) do + # centos-59 doesn't have /sbin in the default path, + # so we must ensure it's on serverspec's path + config.path = '/sbin' + end +end diff --git a/spec/e2e/test/integration/webapp/serverspec/localhost/default_spec.rb b/spec/e2e/test/integration/webapp/serverspec/localhost/default_spec.rb new file mode 100644 index 0000000000..b6c899620e --- /dev/null +++ b/spec/e2e/test/integration/webapp/serverspec/localhost/default_spec.rb @@ -0,0 +1,126 @@ + +require "net/http" +require "uri" + +require "#{ENV['BUSSER_ROOT']}/../kitchen/data/serverspec_helper" + +describe "webapp::default" do + describe "installed packages" do + shared_examples_for "a package" do + it "is installed" do + expect(package(package_name)).to be_installed + end + end + + describe "#{property[:apache][:package]} package" do + include_examples "a package" do + let(:package_name) { property[:apache][:package] } + end + end + + describe "mysql-server-#{property[:mysql][:server][:version]} package" do + include_examples "a package" do + let(:package_name) { "mysql-server-#{property[:mysql][:server][:version]}" } + end + end + + describe "mysql-client package" do + include_examples "a package" do + let(:package_name) { "mysql-client" } + end + end + + describe "php package" do + include_examples "a package" do + let(:package_name) { "php5" } + end + end + end + + describe "enabled/running services" do + shared_examples_for "a service" do + it "is enabled" do + expect(service(service_name)).to be_enabled + end + + it "is running" do + expect(service(service_name)).to be_enabled + end + end + + describe "#{property[:apache][:service_name]} service" do + include_examples "a service" do + let(:service_name) { property[:apache][:service_name] } + end + end + + describe "mysql service" do + include_examples "a service" do + let(:service_name) { "mysql" } + end + end + + end + + describe "mysql database" do + let(:db_query) { "mysql -u root -pilikerandompasswordstoo -e \"#{statement}\"" } + let(:statement) { "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME='webapp'" } + it "creates a database called 'webapp'" do + expect(command(db_query)).to return_stdout(/webapp/) + end + + describe "mysql database user 'webapp'" do + let(:statement) { "SELECT Host, Db FROM mysql.db WHERE User='webapp'\\G" } + it "adds user 'webapp' to database 'webapp@localhost'" do + expect(command(db_query)).to return_stdout(/Host: localhost\n Db: webapp/) + end + + describe "grants" do + shared_examples_for "a privilege" do |priv| + let(:statement) { + "SELECT #{priv_query}" \ + " FROM mysql.db" \ + " WHERE Host='localhost' AND Db='webapp' AND User='webapp'\\G" + } + let(:priv_query) { "#{priv.capitalize}_priv" } + + it "has privilege #{priv} on 'webapp@localhost'" do + expect(command(db_query)).to return_stdout(/#{priv_query}: Y/) + end + end + + %w(select update insert delete create).each do |priv| + include_examples "a privilege", priv do + end + end + end + end + end + + describe "generated webpages" do + let(:get_response) { Net::HTTP.get_response(uri) } + shared_examples_for "a webpage" do + it "exists" do + expect(get_response).to be_kind_of(Net::HTTPSuccess) + end + + it "displays content" do + expect(get_response.body).to include(content) + end + end + + describe "http://localhost/index.html" do + include_examples "a webpage" do + let(:uri) { URI.parse("http://localhost/index.html") } + let(:content) { "Hello, World!" } + end + end + + describe "http://localhost/index.php" do + include_examples "a webpage" do + let(:uri) { URI.parse("http://localhost/index.php") } + let(:content) { "Hello, World!" } + end + end + end +end |