Skip to content

Commit

Permalink
Added kitchen testing, reorganized resource teseting
Browse files Browse the repository at this point in the history
  • Loading branch information
flatrocks committed May 22, 2015
1 parent 7577f76 commit 0978497
Show file tree
Hide file tree
Showing 23 changed files with 200 additions and 42 deletions.
13 changes: 9 additions & 4 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ provisioner:
name: chef_solo

platforms:
- name: ubuntu-12.04
- name: centos-6.4
- name: ubuntu-14.04

suites:
- name: default
- name: test
run_list:
- recipe[s3_asset_backup::default]
- recipe[apt]
- recipe[test::kitchen_file_test]
- recipe[test::kitchen_mysql_test]
attributes:
test_s3_region: <%= ENV['TEST_S3_REGION'] %>
test_s3_bucket: <%= ENV['TEST_S3_BUCKET'] %>
test_s3_access_key_id: <%= ENV['TEST_S3_ACCESS_KEY_ID'] %>
test_s3_secret_access_key: <%= ENV['TEST_S3_SECRET_ACCESS_KEY'] %>
4 changes: 4 additions & 0 deletions Berksfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
source "https://supermarket.chef.io"

metadata

group :integration do
cookbook 'test', :path => './test/fixtures/cookbooks/test'
end
38 changes: 25 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,21 +232,33 @@ Note that this may not be up to date with the current AWS API and you may need t
take additional steps to secure your backups.
```json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:ListBucket",
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::<bucket_name_here>/*"
]
}
]
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:ListBucket",
"s3:GetBucketLocation"
],
"Resource": [
"arn:aws:s3:::<bucket_name_here>"
]
},
{
"Effect": "Allow",
"Action": [
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::<bucket_name_here>/*"
]
}
]
}
```
Note that the Resource for the first block is just the bucket name,
but for the second block, it ends with "/*" indicating that it applies to the
contents of the bucket.

If it meets your needs, you can use the same AWS credentials for multiple
resources, for example you may want to back up both files and MySQL data
Expand Down
17 changes: 0 additions & 17 deletions attributes/default.rb

This file was deleted.

3 changes: 2 additions & 1 deletion libraries/provider_s3_backup_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def action_create

user new_resource.user do
home user_home
manage_home true
action :create
end

Expand Down Expand Up @@ -49,7 +50,7 @@ def action_create
'access_key_id' => new_resource.s3_access_key_id,
'secret_access_key' => new_resource.s3_secret_access_key,
'bucket' => new_resource.s3_bucket,
'key_prefix_format' => new_resource.s3_key_prefix_format
'time_prefix' => new_resource.time_prefix
},
'log' => {
'ident' => new_resource.log_ident,
Expand Down
1 change: 1 addition & 0 deletions libraries/provider_s3_file_backup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def action_create
super

cookbook_file ::File.join(user_home, "s3_file_backup.rb") do
cookbook 's3_backup'
user new_resource.user
group new_resource.user
mode "500"
Expand Down
2 changes: 2 additions & 0 deletions libraries/provider_s3_mysql_backup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def action_create
end

template ::File.join(user_home, ".my.cnf") do
cookbook 's3_backup'
source 'my.cnf.erb'
user new_resource.user
group new_resource.user
Expand All @@ -44,6 +45,7 @@ def action_create
end

cookbook_file ::File.join(user_home, "s3_mysql_backup.rb") do
cookbook 's3_backup'
user new_resource.user
group new_resource.user
mode "500"
Expand Down
4 changes: 2 additions & 2 deletions libraries/resource_s3_backup_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def s3_access_key_id(arg = nil)
def s3_secret_access_key(arg = nil)
set_or_return :s3_secret_access_key, arg, kind_of: String, required: true
end
def s3_key_prefix_format(arg = nil)
set_or_return :s3_key_prefix_format, arg, kind_of: String, default: '%d-%b-%Y'
def time_prefix(arg = nil)
set_or_return :time_prefix, arg, kind_of: String, default: '%d-%b-%Y'
end

def log_ident(arg = nil)
Expand Down
4 changes: 3 additions & 1 deletion metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
version '0.1.0'

depends 'ruby', '~>0.9'
depends 'database', '~> 4.0'
depends 'mysql', '~> 6.0'
depends 'database', '~> 4.0'
depends 'mysql2_chef_gem', '~> 1.0'
13 changes: 13 additions & 0 deletions test/fixtures/cookbooks/test/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
test CHANGELOG
==============

This file is used to list changes made in each version of the test cookbook.

0.1.0
-----
- [your_name] - Initial release of test

- - -
Check the [Markdown Syntax Guide](http://daringfireball.net/projects/markdown/syntax) for help with Markdown.

The [Github Flavored Markdown page](http://github.github.com/github-flavored-markdown/) describes the differences between markdown on github and standard markdown.
1 change: 1 addition & 0 deletions test/fixtures/cookbooks/test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cookbook to test parent
File renamed without changes.
10 changes: 10 additions & 0 deletions test/fixtures/cookbooks/test/metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name 'test'
maintainer 'YOUR_COMPANY_NAME'
maintainer_email 'YOUR_EMAIL'
license 'All rights reserved'
description 'Installs/Configures test'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version '0.1.0'

depends 'mysql', ">= 6.0"
depends 's3_backup'
8 changes: 8 additions & 0 deletions test/fixtures/cookbooks/test/recipes/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#
# Cookbook Name:: test
# Recipe:: default
#
# Copyright 2015, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#
48 changes: 48 additions & 0 deletions test/fixtures/cookbooks/test/recipes/kitchen_file_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Cookbook Name::
# Recipe::
#
# Copyright 2014, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#

# for test kitchen run to check out the whole shebang


# create some file assets
directory '/some' do
mode '777'
action :create
end
file '/some/file' do
mode '777'
content 'some test'
action :create
end

directory '/another' do
mode '777'
action :create
end
file '/another/file' do
mode '777'
content 'another test'
action :create
end

soon = Time.now + 60
soon_cron = {day: "#{soon.day}", hour: "#{soon.hour}", minute: "#{soon.min}"}
s3_file_backup 'my_file_backup' do
s3_region node['test_s3_region']
s3_bucket node['test_s3_bucket']
s3_access_key_id node['test_s3_access_key_id']
s3_secret_access_key node['test_s3_secret_access_key']
assets [
{'item' => '/some/file'},
{'item' => '/another/file', 'prefix' => 'another'}
]
groups ['group1', 'group2']
cron soon_cron
log_success_message "file test works!"
action :create
end
58 changes: 58 additions & 0 deletions test/fixtures/cookbooks/test/recipes/kitchen_mysql_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Cookbook Name::
# Recipe::
#
# Copyright 2014, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#
# required service

mysql2_chef_gem 'default' # denote default version for this platform

connection = {username: 'root', password: 'root_pw', socket: '/var/run/mysql-default/mysqld.sock'}

mysql_service 'default' do
socket connection[:socket]
initial_root_password connection[:password]
action [:create, :start]
end

mysql_database 'db1' do
connection connection
action :create
end
mysql_database 'db2' do
connection connection
action :create
end
# must use "db.table" form due to bug in database cookbook that fails to set database
mysql_database 'create db1.table1' do
connection connection
sql "CREATE TABLE IF NOT EXISTS db2.table1 (id INT);"
action [:query]
end
mysql_database 'create db1.table2' do
connection connection
sql "CREATE TABLE IF NOT EXISTS db2.table2 (id INT);"
action [:query]
end

soon = Time.now + 90 # set the cron for +90 seconds... may not be enough on slow converge
soon_cron = {day: "#{soon.day}", hour: "#{soon.hour}", minute: "#{soon.min}"}
s3_mysql_backup 'my_mysql_backup' do
mysql_connection connection
mysql_user 'user1'
mysql_password 'user1_pw'
s3_region node['test_s3_region']
s3_bucket node['test_s3_bucket']
s3_access_key_id node['test_s3_access_key_id']
s3_secret_access_key node['test_s3_secret_access_key']
assets [
{'item' => 'db1'},
{'item' => 'db2 table2', 'prefix' => 'another'}
]
groups ['group1', 'group2']
cron soon_cron
log_success_message "mysql test works!"
action :create
end
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require_relative "../spec_helper"

describe "s3_backup::_s3_file_backup_resource" do
describe "test::s3_file_backup_resource" do

subject { ChefSpec::SoloRunner.new(:step_into => ["s3_file_backup"]) do |node|
end.converge(described_recipe) }
Expand Down Expand Up @@ -66,7 +66,7 @@
" access_key_id: my_access_key_id\n" +
" secret_access_key: my_secret_access_key\n" +
" bucket: my_bucket\n" +
" key_prefix_format: '%d-%b-%Y'",
" time_prefix: '%d-%b-%Y'",
"log:\n" +
" ident: s3_file_backup\n" +
" success_message: \n",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require_relative "../spec_helper"

describe "s3_backup::_s3_mysql_backup_resource" do
describe "test::s3_mysql_backup_resource" do

subject { ChefSpec::SoloRunner.new(:step_into => ["s3_mysql_backup"]) do |node|
end.converge(described_recipe) }
Expand Down Expand Up @@ -66,7 +66,7 @@
" access_key_id: my_access_key_id\n" +
" secret_access_key: my_secret_access_key\n" +
" bucket: my_bucket\n" +
" key_prefix_format: '%d-%b-%Y'",
" time_prefix: '%d-%b-%Y'",
"log:\n" +
" ident: s3_mysql_backup\n" +
" success_message: \n",
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions test/integration/default/bats/todo.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bats

fixtures bats

@test "Test the backup scripts with real S3 access" {
skip "sometime, when we figure out how to do this without exposing credentials."
}
3 changes: 3 additions & 0 deletions tk
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
. /Volumes/RNRSecure/env_scripts/s3_test.sh
kitchen $*

0 comments on commit 0978497

Please sign in to comment.