Skip to content

Instantly share code, notes, and snippets.

@anthroprose
Created February 9, 2014 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthroprose/8903310 to your computer and use it in GitHub Desktop.
Save anthroprose/8903310 to your computer and use it in GitHub Desktop.
Basic Vagrant + Chef_Zero + Test-Kitchen Workflow
describe command('/opt/corp/brain-env/bin/runtests') do
it { should return_exit_status 0 }
end

Here's how you can quickly get testing or developing against the cookbook thanks to Vagrant and Berkshelf.

export OPSCODE_USER=""
export AWS_ACCESS_KEY_ID=""
export AWS_SECRET_ACCESS_KEY=""
apt-get install ruby-dev
vagrant plugin install vagrant-berkshelf
vagrant plugin install vagrant-omnibus
vagrant plugin install vagrant-chef-zero
git clone git://github.com/corp/chef-corp.git
cd chef-corp
mkdir -p ./test/integration/default/data_bags/secrets
knife data bag show secrets brain -f json >> ./test/integration/default/data_bags/secrets/brain.json
vagrant up

You can then SSH into the running VM using the vagrant ssh command.

You can specify the Git Branch to checkout by using BRANCH="test-feature" vagrant up

The VM can easily be stopped and deleted with the vagrant destroy or vagrant destroy -f command. Please see the official Vagrant documentation for a more in depth explanation of available commands.


Development

Open up your favorite code editor to: ./src of the directory you checked out the repo. Either use your code editor or the terminal LOCAL to your machine, not the VM to commit code back upstream, the VM only has PULL permissions and does not have your github credentials.

You can connect to the VM in order to run or debug code by doing:

vagrant up
vagrant ssh
vagrant@centos6:~/$ sudo su - corp-brain
corp-brain@centos6:~/$ cd /opt/corp/brain-env/brain

You can then runtests or braindebug or workerdebug these commands are shortcuts for commands such as: /opt/corp/brain-env/bin/env-wrapper.sh /opt/corp/brain-env/bin/python /opt/corp/brain-env/brain/brain.py debug and make sure you have the correct environment variables loaded when using things like sudo to access privileged ports of 80 or 443.

Unit Tests

vagrant up
vagrant ssh
vagrant@centos6:~/$ sudo su - corp-brain
corp-brain@centos6:~/$ cd /opt/corp/brain-env/brain
corp-brain@centos6:~/$ runtests

Integration Testing

apt-get install ruby19 ruby19-dev
gem19 install foodcritic
gem19 install test-kitchen
bundle install
bundle exec berks install
BRANCH="test-feature" bundle exec strainer test
BRANCH="test-feature" kitchen test
# You can run this with `RECIPE="engine" vagrant up`
# or `vagrant up` to default to the brain
recipe = 'brain'
branch = 'master'
if ENV['RECIPE'] then
recipe = ENV['RECIPE']
end
if ENV['BRANCH'] then
branch = ENV['BRANCH']
end
if ARGV[0] == 'up' or ARGV[0] == 'provision' then
puts "Loading Recipe: " + recipe + "\n"
puts "Checking out Branch: " + branch + "\n"
end
Vagrant.configure("2") do |config|
config.berkshelf.enabled = true
config.omnibus.chef_version = :latest
config.chef_zero.enabled = true
config.chef_zero.data_bags = "test/integration/default/data_bags"
config.vm.define :centos6 do |centos6|
centos6.vm.box = 'opscode-centos-6.5'
centos6.vm.box_url = 'http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_centos-6.5_chef-provisionerless.box'
centos6.vm.hostname = "corp-centos-6"
end
config.vm.synced_folder "src/", "/opt/corp/brain-env/brain", create: true, mount_options: ["dmode=777,fmode=777"]
config.vm.network :private_network, ip: '192.168.50.10'
config.vm.network "forwarded_port", guest: 80, host: 9000
config.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--memory", 1024]
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
end
config.vm.provision "shell", inline: "mkdir -p /etc/chef"
config.vm.provision :chef_client do |chef|
chef.log_level = :info
chef.json = {
"role" => "brain",
"nginx" => {
"default_domain" => "localhost",
"default_site_enabled" => false
},
"aws" => {
"key" => "#{ENV['AWS_ACCESS_KEY_ID']}",
"secret" => "#{ENV['AWS_SECRET_ACCESS_KEY']}",
"services" => {
"elasticache" => {
"nodes" => ""
},
"sns" => {
"alert-topic" => ""
}
}
},
"corp" => {
"brain" => {
"version" => branch,
"reference" => branch
}
},
"authorization" => {
"sudo" => {
"passwordless" => true,
"include_sudoers_d" => true,
"users" => ["vagrant", "corp", "corp-brain"]
}
}
}
chef.run_list = [
"recipe[python::pip]",
"recipe[corp]",
"recipe[corp::#{recipe}]"
]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment