Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Jul 27, 2015
0 parents commit e58cefa
Show file tree
Hide file tree
Showing 30 changed files with 1,114 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
root = ture

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8

[*.rb]
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
insert_final_newline = false
trim_traling_whitespace = true
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
/spec/secrets.yml
50 changes: 50 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Lint/Eval:
Enabled: false

Lint/HandleExceptions:
Enabled: false

Lint/UselessAssignment:
Enabled: false

Metrics/AbcSize:
Max: 50

Metrics/ClassLength:
Max: 120

Metrics/CyclomaticComplexity:
Max: 15

Metrics/LineLength:
Max: 120

Metrics/MethodLength:
Max: 30

Metrics/PerceivedComplexity:
Max: 15

Style/BarePercentLiterals:
Enabled: false

Style/ClassAndModuleChildren:
Enabled: false

Style/Documentation:
Enabled: false

Style/PercentLiteralDelimiters:
Enabled: false

Style/PredicateName:
Enabled: false

Style/RedundantSelf:
Enabled: false

Style/SymbolProc:
Enabled: false

Style/BracesAroundHashParameters:
Enabled: false
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: ruby
rvm:
- 2.1.6
- 2.2.1

script:
- bundle exec rake spec
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in awspec.gemspec
gemspec
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Ken'ichiro Oyama

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Awspec

RSpec tests for your AWS resources.

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'awspec'
```

And then execute:

$ bundle

Or install it yourself as:

$ gem install awspec

## Usage

### Generate awspec template

$ awspec init

## Support AWS Resources

- [X] EC2
- [X] RDS
- [X] Security Group
- [ ] VPC
- [ ] S3
- [ ] Route53
- ...

## Contributing

1. Fork it ( https://github.com/[my-github-username]/awspec/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request

## Reference

- Original idea (architecture): [Serverspec](https://github.com/serverspec/serverspec)
- [Serverspec book](http://www.oreilly.co.jp/books/9784873117096/)

16 changes: 16 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'bundler/gem_tasks'
begin
require 'rspec/core/rake_task'
rescue LoadError
end

if defined?(RSpec)
task spec: 'spec:all'
namespace :spec do
task all: ['spec:type']

RSpec::Core::RakeTask.new(:type) do |t|
t.pattern = 'spec/type/*_spec.rb'
end
end
end
29 changes: 29 additions & 0 deletions awspec.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'awspec/version'

Gem::Specification.new do |spec|
spec.name = 'awspec'
spec.version = Awspec::VERSION
spec.authors = ['k1LoW']
spec.email = ['k1lowxb@gmail.com']

spec.summary = 'RSpec tests for your AWS resources.'
spec.description = 'RSpec tests for your AWS resources.'
spec.homepage = "TODO: Put your gem's website or public repo URL here."
spec.license = 'MIT'

spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_runtime_dependency 'rspec', '~> 3.0'
spec.add_runtime_dependency 'rspec-its'
spec.add_runtime_dependency 'aws-sdk', '~> 2'
spec.add_runtime_dependency 'thor'
spec.add_development_dependency 'bundler', '~> 1.9'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rubocop'
end
5 changes: 5 additions & 0 deletions bin/awspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env ruby

require 'awspec'

Awspec::CLI.start
11 changes: 11 additions & 0 deletions lib/awspec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'rubygems'
require 'rspec'
require 'rspec/its'
require 'awspec/version'
require 'awspec/cli'
require 'awspec/matcher'
require 'awspec/helper'
require 'awspec/ext/string'

module Awspec
end
11 changes: 11 additions & 0 deletions lib/awspec/cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'thor'
require 'awspec/setup'

module Awspec
class CLI < Thor
desc 'awspec init', 'Generate awspec spec_helper.rb'
def init
Awspec::Setup.run
end
end
end
14 changes: 14 additions & 0 deletions lib/awspec/ext/string.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class String
def to_snake_case
self.gsub(/::/, '/')
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
.tr('-', '_')
.downcase
end

def to_camel_case
return self if self !~ /_/ && self =~ /[A-Z]+.*/
split('_').map { |e| e.capitalize }.join
end
end
6 changes: 6 additions & 0 deletions lib/awspec/helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'awspec/helper/type'
extend Awspec::Helper::Type
class RSpec::Core::ExampleGroup
extend Awspec::Helper::Type
include Awspec::Helper::Type
end
18 changes: 18 additions & 0 deletions lib/awspec/helper/type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Awspec
module Helper
module Type
types = %w(
base ec2 rds security_group
)

types.each { |type| require "awspec/type/#{type}" }

types.each do |type|
define_method type do |*args|
name = args.first
eval "Awspec::Type::#{type.to_camel_case}.new(name)"
end
end
end
end
end
9 changes: 9 additions & 0 deletions lib/awspec/matcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# EC2
require 'awspec/matcher/belong_to_vpc'
require 'awspec/matcher/belong_to_subnet'

# RDS
require 'awspec/matcher/belong_to_db_subnet_group'

# SecurityGroup
require 'awspec/matcher/be_opened'
17 changes: 17 additions & 0 deletions lib/awspec/matcher/be_opened.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
RSpec::Matchers.define :be_opened do |port|
match do |sg|
sg.opened?(port, @protocol, @cidr)
end

chain :protocol do |protocol|
@protocol = protocol
end

chain :for do |cidr|
@cidr = cidr
end

chain :target do |cidr|
@cidr = cidr
end
end
5 changes: 5 additions & 0 deletions lib/awspec/matcher/belong_to_db_subnet_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RSpec::Matchers.define :belong_to_db_subnet_group do |db_subnet_group_name|
match do |resource|
return true if resource.instance[:db_subnet_group][:db_subnet_group_name] == db_subnet_group_name
end
end
33 changes: 33 additions & 0 deletions lib/awspec/matcher/belong_to_subnet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
RSpec::Matchers.define :belong_to_subnet do |subnet_id|
match do |resource|
# EC2
if resource.instance_of?(Awspec::Type::Ec2)
return true if resource.subnet_id == subnet_id
ret = resource.client.describe_subnets({
filters: [{ name: 'tag:Name', values: [subnet_id] }]
})
return false unless ret
return ret[:subnets][0][:subnet_id] == resource.subnet_id
end

# RDS
if resource.instance_of?(Awspec::Type::Rds)
subnets = resource.instance[:db_subnet_group][:subnets]
ret = subnets.find do |subnet|
subnet[:subnet_identifier] == subnet_id
end

return ret[:subnet_availability_zone][:name] == resource.availability_zone if ret

res = resource.ec2_client.describe_subnets({
filters: [{ name: 'tag:Name', values: [subnet_id] }]
})
return false unless res
ret = subnets.find do |subnet|
subnet[:subnet_identifier] == res[:subnets][0][:subnet_id]
end

return ret[:subnet_availability_zone][:name] == resource.availability_zone if ret
end
end
end
10 changes: 10 additions & 0 deletions lib/awspec/matcher/belong_to_vpc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
RSpec::Matchers.define :belong_to_vpc do |vpc_id|
match do |resource|
return true if resource.vpc_id == vpc_id
ret = resource.ec2_client.describe_vpcs({
filters: [{ name: 'tag:Name', values: [vpc_id] }]
})
return false unless ret
ret[:vpcs][0][:vpc_id] == resource.vpc_id
end
end
Loading

0 comments on commit e58cefa

Please sign in to comment.