Class: AwsSpecGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_spec_generator.rb

Overview

Parent class for individual awspec tests generators

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AwsSpecGenerator

Returns a new instance of AwsSpecGenerator.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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

Instance Method Details

#clear_dir(dir) ⇒ Object

Clear out the last run



28
29
30
31
32
33
34
# 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) ⇒ Object

Generate tests for all accounts



37
38
39
40
41
42
43
# File 'lib/aws_spec_generator.rb', line 37

def generate_all_tests()
  generate_ec2_tests()
  generate_sg_tests()
  generate_s3_tests()
  generate_nacl_tests()
  generate_elb_tests()
end

#generate_ec2_tests(account) ⇒ Object

Generate the EC2 tests



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/aws_spec_generator.rb', line 46

def generate_ec2_tests()
  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', #{}: 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) ⇒ Object

Generate the ELB Tests



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/aws_spec_generator.rb', line 64

def generate_elb_tests()
  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', #{}: 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) ⇒ Object

Generate NACL tests



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/aws_spec_generator.rb', line 128

def generate_nacl_tests()
  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', #{}: 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) ⇒ Object

Generate S3 Bucket tests



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/aws_spec_generator.rb', line 102

def generate_s3_tests()
  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', #{}: 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) ⇒ Object

Generate the SG tests



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/aws_spec_generator.rb', line 83

def generate_sg_tests()
  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', #{}: 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_listObject

Get the list of s3 bucket names



160
161
162
163
164
165
166
167
168
169
170
171
# 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_idsObject

retrieve the VPC names for this account



149
150
151
152
153
154
155
156
157
# 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