Skip to content

Instantly share code, notes, and snippets.

@rberger
Forked from charlesjohnson/attributes_default.rb
Created March 7, 2014 11:10
Show Gist options
  • Save rberger/9409655 to your computer and use it in GitHub Desktop.
Save rberger/9409655 to your computer and use it in GitHub Desktop.
default['myface']['database']['host'] = 'localhost'
default['myface']['database']['username'] = 'root'
default['myface']['database']['password'] = node['mysql']['server_root_password']
default['myface']['database']['dbname'] = 'myface'
default['myface']['database']['seed_file'] = "/tmp/myface-init.sql"
require File.expand_path('../support/helpers', __FILE__)
describe 'myface::database' do
include Helpers::Myface
# Example spec tests can be found at http://git.io/Fahwsw
# Verify that MySQL is installed & enabled:
it "Enables and starts the mysql daemon" do
service(node['mysql']['service_name']).must_be_running
service(node['mysql']['service_name']).must_be_enabled
end
# Verify that the myface database has a user table:
it "Seeds the myface database" do
myface_tables.must_include "users"
end
end
module Helpers
module Myface
include MiniTest::Chef::Assertions
include MiniTest::Chef::Context
include MiniTest::Chef::Resources
# This function returns an array of all tables in the myface database
def myface_tables
require 'mysql'
connection = ::Mysql.new node['myface']['database']['host'], node['myface']['database']['username'], node['myface']['database']['password']
mfts = connection.select_db(node['myface']['database']['dbname']).list_tables
end
end
end
#
# Cookbook Name:: myface
# Recipe:: database
#
# Copyright (C) 2013 Charles Johnson
#
# All rights reserved - Do Not Redistribute
#
include_recipe "mysql::server"
#Include the mysql Ruby library for Chef
include_recipe "mysql::ruby"
# Create mysql connection binding
mysql_connection_info = {:host => node['myface']['database']['host'],
:username => node['myface']['database']['username'],
:password => node['myface']['database']['password']}
# Metadata dependency on the database cookbook provides
# access to the mysql_database providers.
mysql_database node['myface']['database']['dbname'] do
connection mysql_connection_info
action :create
end
# Write schema seed file to filesystem
cookbook_file node['myface']['database']['seed_file'] do
source "myface-init.sql"
owner "root"
group "root"
mode "0600"
end
# Seed database with test data
execute "initialize myface database" do
command "mysql -h #{node['myface']['database']['host']} -u #{node['myface']['database']['username']} -p#{node['myface']['database']['password']} -D #{node['myface']['database']['dbname']} < #{node['myface']['database']['seed_file']}"
not_if "mysql -h #{node['myface']['database']['host']} -u #{node['myface']['database']['username']} -p#{node['myface']['database']['password']} -D #{node['myface']['database']['dbname']} -e 'describe users;'"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment