From 0978497324dae38c5bdff399e87b399d12d8a75c Mon Sep 17 00:00:00 2001 From: Tom Wilson Date: Fri, 22 May 2015 10:50:00 -0600 Subject: [PATCH] Added kitchen testing, reorganized resource teseting --- .kitchen.yml | 13 +++-- Berksfile | 4 ++ README.md | 38 +++++++----- attributes/default.rb | 17 ------ libraries/provider_s3_backup_base.rb | 3 +- libraries/provider_s3_file_backup.rb | 1 + libraries/provider_s3_mysql_backup.rb | 2 + libraries/resource_s3_backup_base.rb | 4 +- metadata.rb | 4 +- test/fixtures/cookbooks/test/CHANGELOG.md | 13 +++++ test/fixtures/cookbooks/test/README.md | 1 + .../test/libraries}/missing_mysql_matchers.rb | 0 test/fixtures/cookbooks/test/metadata.rb | 10 ++++ .../cookbooks/test/recipes/default.rb | 8 +++ .../test/recipes/kitchen_file_test.rb | 48 +++++++++++++++ .../test/recipes/kitchen_mysql_test.rb | 58 +++++++++++++++++++ .../test/recipes/s3_file_backup_resource.rb | 0 .../test/recipes/s3_mysql_backup_resource.rb | 0 .../recipes/s3_file_backup_resource_spec.rb | 4 +- .../recipes/s3_mysql_backup_resource_spec.rb | 4 +- .../cookbooks/test/spec}/spec_helper.rb | 0 test/integration/default/bats/todo.bats | 7 +++ tk | 3 + 23 files changed, 200 insertions(+), 42 deletions(-) delete mode 100644 attributes/default.rb create mode 100644 test/fixtures/cookbooks/test/CHANGELOG.md create mode 100644 test/fixtures/cookbooks/test/README.md rename {libraries => test/fixtures/cookbooks/test/libraries}/missing_mysql_matchers.rb (100%) create mode 100644 test/fixtures/cookbooks/test/metadata.rb create mode 100644 test/fixtures/cookbooks/test/recipes/default.rb create mode 100644 test/fixtures/cookbooks/test/recipes/kitchen_file_test.rb create mode 100644 test/fixtures/cookbooks/test/recipes/kitchen_mysql_test.rb rename recipes/_s3_file_backup_resource.rb => test/fixtures/cookbooks/test/recipes/s3_file_backup_resource.rb (100%) rename recipes/_s3_mysql_backup_resource.rb => test/fixtures/cookbooks/test/recipes/s3_mysql_backup_resource.rb (100%) rename spec/recipes/_s3_file_backup_resource_spec.rb => test/fixtures/cookbooks/test/spec/recipes/s3_file_backup_resource_spec.rb (97%) rename spec/recipes/_s3_mysql_backup_resource_spec.rb => test/fixtures/cookbooks/test/spec/recipes/s3_mysql_backup_resource_spec.rb (98%) rename {spec => test/fixtures/cookbooks/test/spec}/spec_helper.rb (100%) create mode 100644 test/integration/default/bats/todo.bats create mode 100755 tk diff --git a/.kitchen.yml b/.kitchen.yml index f62de39..5b5f41d 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -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'] %> diff --git a/Berksfile b/Berksfile index 967b9a7..03372f3 100644 --- a/Berksfile +++ b/Berksfile @@ -1,3 +1,7 @@ source "https://supermarket.chef.io" metadata + +group :integration do + cookbook 'test', :path => './test/fixtures/cookbooks/test' +end \ No newline at end of file diff --git a/README.md b/README.md index 6887d3b..199eba5 100644 --- a/README.md +++ b/README.md @@ -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:::/*" - ] - } - ] + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": [ + "s3:ListBucket", + "s3:GetBucketLocation" + ], + "Resource": [ + "arn:aws:s3:::" + ] + }, + { + "Effect": "Allow", + "Action": [ + "s3:PutObject" + ], + "Resource": [ + "arn:aws:s3:::/*" + ] + } + ] } ``` +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 diff --git a/attributes/default.rb b/attributes/default.rb deleted file mode 100644 index cc678f7..0000000 --- a/attributes/default.rb +++ /dev/null @@ -1,17 +0,0 @@ -default['s3_asset_backup']['user']['name'] = 's3_asset_backup' -default['s3_asset_backup']['db_user']['name'] = 's3_asset_backup' - -default['s3_asset_backup']['user']['groups'] = [] - -default['s3_asset_backup']['files'] = {} -default['s3_asset_backup']['directories'] = {} -default['s3_asset_backup']['databases'] = {} -default['s3_asset_backup']['database_tables'] = {} -default['s3_asset_backup']['database_patterns'] = {} - -default['s3_asset_backup']['cron'] = {'hour' => 0, 'minute' => 0} - -default['s3_asset_backup']['files'] = {} -default['s3_asset_backup']['databases'] = {} -default['s3_asset_backup']['database_tables'] = {} -default['s3_asset_backup']['database_patterns'] = {} \ No newline at end of file diff --git a/libraries/provider_s3_backup_base.rb b/libraries/provider_s3_backup_base.rb index aa775a5..c46642a 100644 --- a/libraries/provider_s3_backup_base.rb +++ b/libraries/provider_s3_backup_base.rb @@ -21,6 +21,7 @@ def action_create user new_resource.user do home user_home + manage_home true action :create end @@ -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, diff --git a/libraries/provider_s3_file_backup.rb b/libraries/provider_s3_file_backup.rb index 6e0a11b..342985b 100644 --- a/libraries/provider_s3_file_backup.rb +++ b/libraries/provider_s3_file_backup.rb @@ -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" diff --git a/libraries/provider_s3_mysql_backup.rb b/libraries/provider_s3_mysql_backup.rb index a440ba9..3dbdc6d 100644 --- a/libraries/provider_s3_mysql_backup.rb +++ b/libraries/provider_s3_mysql_backup.rb @@ -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 @@ -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" diff --git a/libraries/resource_s3_backup_base.rb b/libraries/resource_s3_backup_base.rb index 6a3850e..1c22095 100644 --- a/libraries/resource_s3_backup_base.rb +++ b/libraries/resource_s3_backup_base.rb @@ -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) diff --git a/metadata.rb b/metadata.rb index 45a025b..8ce3500 100644 --- a/metadata.rb +++ b/metadata.rb @@ -7,4 +7,6 @@ version '0.1.0' depends 'ruby', '~>0.9' -depends 'database', '~> 4.0' \ No newline at end of file +depends 'mysql', '~> 6.0' +depends 'database', '~> 4.0' +depends 'mysql2_chef_gem', '~> 1.0' diff --git a/test/fixtures/cookbooks/test/CHANGELOG.md b/test/fixtures/cookbooks/test/CHANGELOG.md new file mode 100644 index 0000000..80488f3 --- /dev/null +++ b/test/fixtures/cookbooks/test/CHANGELOG.md @@ -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. diff --git a/test/fixtures/cookbooks/test/README.md b/test/fixtures/cookbooks/test/README.md new file mode 100644 index 0000000..3d5a6fe --- /dev/null +++ b/test/fixtures/cookbooks/test/README.md @@ -0,0 +1 @@ +Cookbook to test parent \ No newline at end of file diff --git a/libraries/missing_mysql_matchers.rb b/test/fixtures/cookbooks/test/libraries/missing_mysql_matchers.rb similarity index 100% rename from libraries/missing_mysql_matchers.rb rename to test/fixtures/cookbooks/test/libraries/missing_mysql_matchers.rb diff --git a/test/fixtures/cookbooks/test/metadata.rb b/test/fixtures/cookbooks/test/metadata.rb new file mode 100644 index 0000000..b51cc1d --- /dev/null +++ b/test/fixtures/cookbooks/test/metadata.rb @@ -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' \ No newline at end of file diff --git a/test/fixtures/cookbooks/test/recipes/default.rb b/test/fixtures/cookbooks/test/recipes/default.rb new file mode 100644 index 0000000..2d6bfd0 --- /dev/null +++ b/test/fixtures/cookbooks/test/recipes/default.rb @@ -0,0 +1,8 @@ +# +# Cookbook Name:: test +# Recipe:: default +# +# Copyright 2015, YOUR_COMPANY_NAME +# +# All rights reserved - Do Not Redistribute +# diff --git a/test/fixtures/cookbooks/test/recipes/kitchen_file_test.rb b/test/fixtures/cookbooks/test/recipes/kitchen_file_test.rb new file mode 100644 index 0000000..df12661 --- /dev/null +++ b/test/fixtures/cookbooks/test/recipes/kitchen_file_test.rb @@ -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 diff --git a/test/fixtures/cookbooks/test/recipes/kitchen_mysql_test.rb b/test/fixtures/cookbooks/test/recipes/kitchen_mysql_test.rb new file mode 100644 index 0000000..f485590 --- /dev/null +++ b/test/fixtures/cookbooks/test/recipes/kitchen_mysql_test.rb @@ -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 diff --git a/recipes/_s3_file_backup_resource.rb b/test/fixtures/cookbooks/test/recipes/s3_file_backup_resource.rb similarity index 100% rename from recipes/_s3_file_backup_resource.rb rename to test/fixtures/cookbooks/test/recipes/s3_file_backup_resource.rb diff --git a/recipes/_s3_mysql_backup_resource.rb b/test/fixtures/cookbooks/test/recipes/s3_mysql_backup_resource.rb similarity index 100% rename from recipes/_s3_mysql_backup_resource.rb rename to test/fixtures/cookbooks/test/recipes/s3_mysql_backup_resource.rb diff --git a/spec/recipes/_s3_file_backup_resource_spec.rb b/test/fixtures/cookbooks/test/spec/recipes/s3_file_backup_resource_spec.rb similarity index 97% rename from spec/recipes/_s3_file_backup_resource_spec.rb rename to test/fixtures/cookbooks/test/spec/recipes/s3_file_backup_resource_spec.rb index d4545af..5294e13 100644 --- a/spec/recipes/_s3_file_backup_resource_spec.rb +++ b/test/fixtures/cookbooks/test/spec/recipes/s3_file_backup_resource_spec.rb @@ -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) } @@ -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", diff --git a/spec/recipes/_s3_mysql_backup_resource_spec.rb b/test/fixtures/cookbooks/test/spec/recipes/s3_mysql_backup_resource_spec.rb similarity index 98% rename from spec/recipes/_s3_mysql_backup_resource_spec.rb rename to test/fixtures/cookbooks/test/spec/recipes/s3_mysql_backup_resource_spec.rb index 5d92239..862355d 100644 --- a/spec/recipes/_s3_mysql_backup_resource_spec.rb +++ b/test/fixtures/cookbooks/test/spec/recipes/s3_mysql_backup_resource_spec.rb @@ -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) } @@ -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", diff --git a/spec/spec_helper.rb b/test/fixtures/cookbooks/test/spec/spec_helper.rb similarity index 100% rename from spec/spec_helper.rb rename to test/fixtures/cookbooks/test/spec/spec_helper.rb diff --git a/test/integration/default/bats/todo.bats b/test/integration/default/bats/todo.bats new file mode 100644 index 0000000..5095dd8 --- /dev/null +++ b/test/integration/default/bats/todo.bats @@ -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." +} diff --git a/tk b/tk new file mode 100755 index 0000000..4860429 --- /dev/null +++ b/tk @@ -0,0 +1,3 @@ +#!/bin/bash +. /Volumes/RNRSecure/env_scripts/s3_test.sh +kitchen $*