Class: RakeCloudspin::Tasks::StackTestTask

Inherits:
BaseTask show all
Defined in:
lib/rake_cloudspin/tasks/stack_test_task.rb

Instance Method Summary collapse

Methods inherited from BaseTask

#role_user_variable_name, #spin_user_variables, #stack_config

Methods inherited from RakeCloudspin::TaskLib

#check_required, check_required_for, #initialize, parameter, parameter_definitions, #process_arguments, #process_block, #setup_defaults, setup_defaults_for

Constructor Details

This class inherits a constructor from RakeCloudspin::TaskLib

Instance Method Details

#assume_role_profile(args) ⇒ Object



89
90
91
# File 'lib/rake_cloudspin/tasks/stack_test_task.rb', line 89

def assume_role_profile(args)
  "assume-spin_account_manager-#{stack_config(args).component}"
end

#aws_configuration_contents(args) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/rake_cloudspin/tasks/stack_test_task.rb', line 81

def aws_configuration_contents(args)
  <<~END_AWS_CONFIG
    [profile #{assume_role_profile(args)}]
    role_arn = #{stack_config(args).vars['assume_role_arn']}
    source_profile = #{stack_config(args).vars['aws_profile']}
  END_AWS_CONFIG
end

#aws_configuration_file_pathObject



77
78
79
# File 'lib/rake_cloudspin/tasks/stack_test_task.rb', line 77

def aws_configuration_file_path
  "#{aws_configuration_folder}/config"
end

#aws_configuration_folderObject



73
74
75
# File 'lib/rake_cloudspin/tasks/stack_test_task.rb', line 73

def aws_configuration_folder
  "work/#{stack_type}/#{stack_name}/aws"
end

#build_user_configuration_hash(api_user_hash = {}) ⇒ Object



53
54
55
# File 'lib/rake_cloudspin/tasks/stack_test_task.rb', line 53

def build_user_configuration_hash(api_user_hash = {})
  { 'configured_api_users' => api_user_hash }
end

#configured_api_users(args) ⇒ Object



49
50
51
# File 'lib/rake_cloudspin/tasks/stack_test_task.rb', line 49

def configured_api_users(args)
  build_user_configuration_hash(stack_config(args).api_users)
end

#create_inspec_attributesObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/rake_cloudspin/tasks/stack_test_task.rb', line 30

def create_inspec_attributes
  lambda do |args|
    mkpath "work/tests/inspec"
    attributes_file_path = "work/tests/inspec/attributes-#{stack_type}-#{stack_name}.yml"
    puts "INFO: Writing inspec attributes to file: #{attributes_file_path}"
    File.open(attributes_file_path, 'w') {|f| 
      f.write(test_attributes(args).to_yaml)
    }
  end
end

#defineObject



9
10
11
12
# File 'lib/rake_cloudspin/tasks/stack_test_task.rb', line 9

def define
  define_inspec_task
  define_aws_configuration_task
end

#define_aws_configuration_taskObject



57
58
59
60
61
# File 'lib/rake_cloudspin/tasks/stack_test_task.rb', line 57

def define_aws_configuration_task
  task :aws_configuration do |t, args|
    make_aws_configuration_file.call(args)
  end
end

#define_inspec_taskObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rake_cloudspin/tasks/stack_test_task.rb', line 14

def define_inspec_task
  desc 'Run inspec tests'
  task :test do |t, args|
    if has_inspec_tests?
      create_inspec_attributes.call(args)
      run_inspec_profile.call(args)
    else
      puts "NO TESTS FOR STACK ('#{stack_name}')"
    end
  end
end

#has_inspec_tests?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/rake_cloudspin/tasks/stack_test_task.rb', line 26

def has_inspec_tests?
  Dir.exist? ("#{stack_type}/#{stack_name}/tests/inspec")
end

#inspec_cmd(inspec_profile:, inspec_profile_name:, aws_profile:, aws_region:) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/rake_cloudspin/tasks/stack_test_task.rb', line 122

def inspec_cmd(inspec_profile:, inspec_profile_name:, aws_profile:, aws_region:)
    "inspec exec " \
    "#{stack_type}/#{stack_name}/tests/inspec/#{inspec_profile} " \
    "-t aws://#{aws_region}/#{aws_profile} " \
    "--reporter json-rspec:work/tests/inspec/results-#{stack_type}-#{stack_name}-#{inspec_profile_name}.json " \
    "cli " \
    "--attrs work/tests/inspec/attributes-#{stack_type}-#{stack_name}.yml"
end

#make_aws_configuration_fileObject



63
64
65
66
67
68
69
70
71
# File 'lib/rake_cloudspin/tasks/stack_test_task.rb', line 63

def make_aws_configuration_file
  lambda do |args|
    mkpath aws_configuration_folder
    puts "INFO: Writing AWS configuration file: #{aws_configuration_file_path}"
    File.open(aws_configuration_file_path, 'w') {|f| 
      f.write(aws_configuration_contents(args))
    }
  end
end

#make_inspec_profile_name(inspec_profile) ⇒ Object



118
119
120
# File 'lib/rake_cloudspin/tasks/stack_test_task.rb', line 118

def make_inspec_profile_name(inspec_profile)
  inspec_profile != '.' ? inspec_profile : '__root__'
end

#run_inspec_profileObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rake_cloudspin/tasks/stack_test_task.rb', line 93

def run_inspec_profile
  lambda do |args|
    inspec_profiles = Dir.entries("#{stack_type}/#{stack_name}/tests/inspec").select { |inspec_profile|
      inspec_profile != '..' &&
        File.exists?("#{stack_type}/#{stack_name}/tests/inspec/#{inspec_profile}/inspec.yml")
    }.each { |inspec_profile|
      inspec_profile_name = make_inspec_profile_name(inspec_profile)
      puts "INSPEC (inspec_profile '#{inspec_profile_name}'): #{inspec_cmd(
        inspec_profile: inspec_profile,
        inspec_profile_name: inspec_profile_name,
        aws_profile: assume_role_profile(args),
        aws_region: stack_config(args).region
      )}"
      system(
        inspec_cmd(
            inspec_profile: inspec_profile,
            inspec_profile_name: inspec_profile_name,
            aws_profile: assume_role_profile(args),
            aws_region: stack_config(args).region
        )
      )
    }
  end
end

#test_attributes(args) ⇒ Object



41
42
43
# File 'lib/rake_cloudspin/tasks/stack_test_task.rb', line 41

def test_attributes(args)
  stack_config(args).vars.merge(test_vars(args)).merge(configured_api_users(args))
end

#test_vars(args) ⇒ Object



45
46
47
# File 'lib/rake_cloudspin/tasks/stack_test_task.rb', line 45

def test_vars(args)
  stack_config(args).test_vars || {}
end