require 'rototiller'
require 'fileutils'

namespace :ci do
  namespace :test do

    desc 'Tests at the component level for the pe-r10k project'
    task :component => [:check_pe_r10k_env_vars] do
      Rake::Task[:beaker].invoke
    end
  end
end

desc 'Run tests against a packaged PE build'
task :acceptance do
  @acceptance_pre_suite = 'pre-suite'
  Rake::Task[:beaker].invoke
end

desc 'The acceptance tests for r10k, run in the beaker framework'
rototiller_task :beaker => [:beaker_hostgenerator] do |t|

  t.add_env do |env|
    env.name = 'PE_FAMILY'
    env.message = 'The puppet enterprise major branch to install from'
  end

  t.add_env do |env|
    env.name = 'pe_dist_dir'
    env.message = 'The location to download PE from, for example "https://artifactory.delivery.puppetlabs.net/artifactory/generic_enterprise__local/20XX.X/ci-ready"'
    ENV['pe_dist_dir'] ||= "https://artifactory.delivery.puppetlabs.net/artifactory/generic_enterprise__local/#{ENV['PE_FAMILY']}/ci-ready"
  end

  t.add_env do |env|
    env.name = 'GIT_PROVIDER'
    env.message = 'The git provider that r10k should use on a SUT'
  end

  t.add_command do |cmd|
    cmd.name = 'beaker --debug'

    common_setup = <<-EOS
pre-suite/00_pe_install.rb,
component/pre-suite/05_install_dev_r10k.rb,
pre-suite/10_git_config.rb,
pre-suite/20_pe_r10k.rb,
EOS
    common_setup.gsub!("\n", '')

    cmd.add_option do |opt|
      opt.name = '--hosts'
      opt.add_argument do |arg|
        arg.name = 'configs/generated'
        arg.add_env({name: 'BEAKER_HOST'})
      end
    end

    cmd.add_option do |opt|
      opt.name = '--keyfile'
      opt.add_argument do |arg|
        arg.name = "#{ENV['HOME']}/.ssh/id_rsa-acceptance"
        arg.add_env({name: 'BEAKER_KEYFILE'})
      end
    end

    cmd.add_option do |opt|
      opt.name = '--pre-suite'
      opt.add_argument do |arg|
        arg.name = @acceptance_pre_suite || common_setup
        arg.add_env({name: 'BEAKER_PRE_SUITE'})
      end
    end

    cmd.add_option do |opt|
      opt.name = '--tests'
      opt.add_argument do |arg|
        arg.name = 'tests'
        arg.add_env({name: 'BEAKER_TESTS'})
      end
    end

    cmd.add_option do |opt|
      opt.name = '--preserve-hosts'
      opt.add_argument do |arg|
        arg.name = 'onfail'
        arg.add_env({name: 'BEAKER_PRESERVE_HOSTS'})
      end
    end

    cmd.add_option do |opt|
      opt.name = '--load-path'
      opt.add_argument({name: 'lib'})
    end
  end
end

desc 'Generate a host configuration used by Beaker'
rototiller_task :beaker_hostgenerator do |t|
  if ENV['BEAKER_HOST'].nil?
    FileUtils.mkdir_p 'configs'
    t.add_command do |c|
      c.name = 'beaker-hostgenerator'
      c.argument = '> configs/generated'
      c.add_option(:name => '', :default => 'centos7-64mdca-64.fa', :override_env => 'TEST_TARGET')
      c.add_option(:name => '--global-config', :default => '{forge_host=forgeapi.puppet.com}', :override_env => 'BHG_GLOBAL_CONFIG')
    end

  end
end

rototiller_task :check_pe_r10k_env_vars do |t|
  t.add_env(:name => 'SHA', :message => 'The sha for pe-r10k')
end

