class AwsSpecGenerator
Parent class for individual awspec tests generators
Public Class Methods
new(options = {})
click to toggle source
# File lib/aws_spec_generator.rb, line 11 def initialize(options = {}) @vpc_list = [] @bucket_list = [] @output_directory = options[:output_directory] if @output_directory.nil? raise( 'Output dir expected by AwsSpecGenerator.new(output_directory: dir)' ) end FileUtils.mkdir_p @output_directory @output_directory += File::SEPARATOR clear_dir(@output_directory) query_vpc_ids query_bucket_list end
Public Instance Methods
clear_dir(dir)
click to toggle source
Clear out the last run
# File lib/aws_spec_generator.rb, line 28 def clear_dir(dir) Dir.glob("#{dir}*spec.rb").each do |fn| fn = File.absolute_path(fn) puts "Deleting file from previous run - #{fn}" File.delete("#{fn}") unless File.directory?("#{fn}") end end
generate_all_tests(account)
click to toggle source
Generate tests for all accounts
# File lib/aws_spec_generator.rb, line 37 def generate_all_tests(account) generate_ec2_tests(account) generate_sg_tests(account) generate_s3_tests(account) generate_nacl_tests(account) generate_elb_tests(account) end
generate_ec2_tests(account)
click to toggle source
Generate the EC2 tests
# File lib/aws_spec_generator.rb, line 46 def generate_ec2_tests(account) puts "Generating EC2 tests" @vpc_list.each do |vpc| target_file = File.absolute_path( @output_directory + "ec2_on_#{vpc}_tests_spec.rb" ) File.open(target_file, 'w') do |f| f.write("require_relative '../../spec_helper'\n\ncontext '#{vpc} tests', #{account}: true do\n\n") end stdout, stderr, status = Open3.capture3("awspec generate ec2 #{vpc} >> \"#{target_file}\"") raise 'Failed to generate ec2 tests (' + stderr + ')' unless status.success? File.open(target_file, 'a+') { |f|f.write("end\n") } end end
generate_elb_tests(account)
click to toggle source
Generate the ELB Tests
# File lib/aws_spec_generator.rb, line 64 def generate_elb_tests(account) puts "Generating ELB tests" @vpc_list.each do |vpc| target_file = File.absolute_path( @output_directory + "elbs_on_#{vpc}_tests_spec.rb" ) File.open(target_file, 'w') do |f| f.write("require_relative '../../spec_helper'\n\ncontext 'ELBs on"\ " #{vpc} tests', #{account}: true do\n\n") end stdout, stderr, status = Open3.capture3("awspec generate elb #{vpc} >> \"#{target_file}\"") raise 'Failed to generate elb tests (' + stderr + ')' unless status.success? File.open(target_file, 'a+') { |f|f.write("end\n") } end end
generate_nacl_tests(account)
click to toggle source
Generate NACL tests
# File lib/aws_spec_generator.rb, line 128 def generate_nacl_tests(account) puts "Generating NACL tests" @vpc_list.each do |vpc| target_file = File.absolute_path( @output_directory + "nacls_on_#{vpc}_tests_spec.rb" ) File.open(target_file, 'w') do |f| f.write( "require_relative '../../spec_helper'\n\ncontext 'NACL "\ "on #{vpc} tests', #{account}: true do\n\n" ) end stdout, stderr, status = Open3.capture3("awspec generate network_acl #{vpc} >> \"#{target_file}\"") raise 'Failed to generate nacl tests (' + stderr + ')' unless status.success? File.open(target_file, 'a+') { |f|f.write("end\n") } end end
generate_s3_tests(account)
click to toggle source
Generate S3 Bucket tests
# File lib/aws_spec_generator.rb, line 102 def generate_s3_tests(account) puts "Generating S3 tests" @bucket_list.each do |bucket| target_file = File.absolute_path(@output_directory + "s3_buckets_on_#{bucket['Name']}_tests_spec.rb") File.open(target_file, 'w') do |f| f.write( "require_relative '../../spec_helper'\n\ncontext 'S3 buckets on"\ " #{bucket['Name']} tests', #{account}: true do\n\n" ) end begin stderr, status = Open3.capture3( "awspec generate s3 #{bucket['Name']} >> \"#{target_file}\"" ) rescue StandardError raise 'Error: (' + status + ')Failed to generate bucket tests (' + stderr + ')' end File.open(target_file, 'a+') { |f|f.write("end\n") } end end
generate_sg_tests(account)
click to toggle source
Generate the SG tests
# File lib/aws_spec_generator.rb, line 83 def generate_sg_tests(account) puts "Generating SG tests" @vpc_list.each do |vpc| target_file = File.absolute_path( @output_directory + "security_groups_on_#{vpc}_tests_spec.rb" ) File.open(target_file, 'w') do |f| f.write("require_relative '../../spec_helper'\n\ncontext 'Security Groups on"\ " #{vpc} tests', #{account}: true do\n\n") end stdout, stderr, status = Open3.capture3("awspec generate security_group #{vpc} >> \"#{target_file}\"") raise 'Error: (' + status + ') Failed to generate security_group tests (' + stderr + ')' unless status.success? File.open(target_file, 'a+') { |f|f.write("end\n") } end end
query_bucket_list()
click to toggle source
Get the list of s3 bucket names
# File lib/aws_spec_generator.rb, line 160 def query_bucket_list begin stdout, stderr, status = Open3.capture3('aws s3api list-buckets') rescue StandardError raise('Error: ' + status + 'Failed to recover buckets list: (' + stderr + ')') end JSON.parse(stdout)['Buckets'].each do |bucket| @bucket_list.push(bucket) end @bucket_list.uniq! end
query_vpc_ids()
click to toggle source
retrieve the VPC names for this account
# File lib/aws_spec_generator.rb, line 149 def query_vpc_ids stdout, stderr, status = Open3.capture3('aws ec2 describe-vpcs') raise("Error: Failed to recover vpc list #{stderr}") unless status.success? JSON.parse(stdout)['Vpcs'].each do |vpc| @vpc_list.push(vpc['VpcId']) end @vpc_list.uniq! end