Class: Pfm::Command::ValidatorCommands::ServerBuild

Inherits:
Base
  • Object
show all
Defined in:
lib/iapi-idlc-sdk-pfm/command/validator_commands/server_build.rb

Instance Attribute Summary

Attributes inherited from Base

#errors, #params

Instance Method Summary collapse

Methods inherited from Base

#initialize, #params_valid?, #read_and_validate_params, #setup_artifacts_dirs, #setup_context, #use_circle_ci?

Methods inherited from Base

#build_base_dir, #build_dir, #build_exists?, #build_setup, #deploy_setup, #deploy_setupv2, #inf_base_dir, #initialize, #needs_help?, #needs_version?, #run_with_default_options, #templates_dir, #verbose?

Methods included from Helpers

debug, err, msg, system_command

Constructor Details

This class inherits a constructor from Pfm::Command::ValidatorCommands::Base

Instance Method Details

#lint_chefObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/iapi-idlc-sdk-pfm/command/validator_commands/server_build.rb', line 62

def lint_chef
  base_path = 'chef'
  cookbooks_dir = 'cookbooks'

  # Loop through the base directory looking through all of the cookbooks
  # for unit tests
  Dir.entries(base_path).each do |dir|
    next unless File.directory? "#{base_path}/#{dir}/#{cookbooks_dir}"
    next if dir.to_s == 'vendor'

    Dir.entries("#{base_path}/#{dir}/#{cookbooks_dir}").each do |cookbook|
      next unless File.directory? "#{base_path}/#{dir}/#{cookbooks_dir}/#{cookbook}"
      # skip directories eg. '.' and '..'
      next if cookbook.to_s == '.' || cookbook.to_s == '..'

      # Run the syntax and semantics checkers
      msg("\nRunning Rubocop on #{cookbook} cookbook")

      # Copy in global .foodcritic file if set
      debug("copying #{SETTINGS['RUBOCOP_RULES_FILE']} to #{Dir.pwd}")
      system("cp #{SETTINGS['RUBOCOP_RULES_FILE']} #{Dir.pwd}") if File.exist? SETTINGS['RUBOCOP_RULES_FILE']

      gem_path = `bundle show rubocop-junit-formatter`.strip!
      system("rubocop \\
        --format html -o #{@artifacts_dir}/rubocop/#{cookbook}_rubocop_output.html \\
        -r #{gem_path}/lib/rubocop/formatter/junit_formatter.rb \\
        --format RuboCop::Formatter::JUnitFormatter -o #{@reports_dir}/rubocop/#{cookbook}_junit.xml \\
        #{base_path}/#{dir}/#{cookbooks_dir}/#{cookbook}") || @failure = true

      msg("Running Foodcritic on #{cookbook} cookbook")
      ENV['FOODCRITIC_JUNIT_OUTPUT_DIR'] = "#{@reports_dir}/foodcritic"
      ENV['FOODCRITIC_JUNIT_OUTPUT_FILE'] = "#{cookbook}_foodcritic_junit.xml"

      # Copy in global .foodcritic file if set
      debug("copying #{SETTINGS['FOODCRITIC_RULES_FILE']} to #{Dir.pwd}")
      system("cp #{SETTINGS['FOODCRITIC_RULES_FILE']} #{base_path}/#{dir}/#{cookbooks_dir}/#{cookbook}") if File.exist? SETTINGS['FOODCRITIC_RULES_FILE']

      # Capure output but also fail if applicable
      system("foodcritic #{base_path}/#{dir}/#{cookbooks_dir}/#{cookbook} -C > #{cookbook}_foodcritic.out") || @failure = true
      system("foodcritic-junit < #{cookbook}_foodcritic.out")
    end
  end
end

#lint_packerObject



56
57
58
59
60
# File 'lib/iapi-idlc-sdk-pfm/command/validator_commands/server_build.rb', line 56

def lint_packer
  Packer::Binary.validate("#{@build_config.dump_build_vars} #{@config[:build_template]}")
rescue Packer::Binary::Command::CommandFailure
  @failure = true
end

#runObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/iapi-idlc-sdk-pfm/command/validator_commands/server_build.rb', line 27

def run
  read_and_validate_params
  setup_artifacts_dirs

  if params_valid?
    build_setup
    validate
    # @workspace.cleanup causing bundler issues
    0
  else
    errors.each { |error| err("Error: #{error}") }
    parse_options(params)
    msg(opt_parser)
    1
  end
rescue ValidationError => e
  err("ERROR: #{e}")
  1
end

#unit_testObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/iapi-idlc-sdk-pfm/command/validator_commands/server_build.rb', line 106

def unit_test
  # load chefspec here as it load chef and can take a long time on some systems
  # so we only want to load it when we absolutely need it
  require 'chefspec'
  base_path = 'chef'
  cookbooks_dir = 'cookbooks'

  # Loop through the base directory looking through all of the cookbooks
  # for unit tests
  Dir.entries(base_path).each do |dir|
    next unless File.directory? "#{base_path}/#{dir}/#{cookbooks_dir}"
    next if dir.to_s == 'vendor'

    Dir.entries("#{base_path}/#{dir}/#{cookbooks_dir}").each do |cookbook|
      next unless File.directory? "#{base_path}/#{dir}/#{cookbooks_dir}/#{cookbook}"
      # skip directories eg. '.' and '..'
      next if cookbook.to_s == '.' || cookbook.to_s == '..'

      # run rspec unit tests
      Dir.chdir("#{base_path}/#{dir}/#{cookbooks_dir}/#{cookbook}") do
        msg("\nRunning unit tests for #{cookbook} cookbook")
        system("rspec -r rspec_junit_formatter --format progress --format RspecJunitFormatter -o #{@reports_dir}/rspec/#{cookbook}_junit.xml") || @failure = true
      end
    end
  end
end

#validateObject

Raises:



47
48
49
50
51
52
53
54
# File 'lib/iapi-idlc-sdk-pfm/command/validator_commands/server_build.rb', line 47

def validate
  lint_packer
  lint_chef
  unit_test

  raise ValidationError, 'Failures reported during validation!' if @failure
  msg('Verified repository..')
end