# Purpose:
# This rakefile executes the performance scenarios. This is the main entry point
# for executing those tests.
# It is assumed that the environment variable "ENVIRONMENT" has been set. See the
# task "environment_setup" below.

require 'rubygems'
require 'cucumber'
require 'cucumber/rake/task'

require File.dirname(__FILE__) + '/../../../../infrastructure/vagrant/aws/VagrantfileUtil.rb'
require File.dirname(__FILE__) + '/../../../../infrastructure/vagrant/Servers.rb'

# Check environment sanity and fail fast if not correct
fail 'WORKSPACE environment variable not set. Did you "source infrastructure/set.env.sh"?' unless ENV.keys.include?('WORKSPACE')
fail 'GEM_HOME environment variable not set. Did you "source infrastructure/set.env.sh"?' unless ENV.keys.include?('GEM_HOME')

if ENV['ENVIRONMENT'] == nil then
    raise "Environment variable 'ENVIRONMENT' must be set to one of { aws-perf, aws-managed-perf, local, local-managed, or ede }."
end

ENV['NOT_VAGRANT'] = "TRUE"   # needed to prevent VagrantfileUtil.rb from including the Berkshelfpatch.

task default: [:endToEndTests]

task :endToEndTests => [:environment_setup] do |t|
    puts "Running all tests"

    Cucumber::Rake::Task.new(:endToEndTests) do |t|
        puts "Executing features"
        t.cucumber_opts = 'features'
    end
end

task :selectedTests => [:environment_setup] do |t|
    puts "Running selected tests"

    Cucumber::Rake::Task.new(:selectedTests) do |x|
        puts "Executing features"
        x.cucumber_opts = 'features --format pretty -t @perf.sync'
    end
end

# Set env variables that are applicable in Virtualbox and AWS but not to EDE.
def setInternetEnvs
    ENV['GEM_SERVER_URL'] = "https://gem.vistacore.us"
    ENV['VAGRANTFILE_DIR'] = "~/Projects/vistacore/ehmp/infrastructure/vagrant/managed/managed-vista-exchange"
    ENV['PROJECT_REPO_URL'] = "https://code.vistacore.us/scm/app/ehmp.git"
    ENV['PROJECT_REPO_FQDN'] = "code.vistacore.us"
    raise %/
    You need to set your git credentials like this:
    export GIT_USERID = 'git_username'
    export GIT_PASSWORD = 'git_password'
    / if ENV['GIT_USERID'].nil? || ENV['GIT_PASSWORD'].nil?
end

task :environment_setup do
    case ENV['ENVIRONMENT']
    when 'aws-perf'  # aws, not managed
        puts "Using AWS environment"
        setInternetEnvs
        LocalProjectRoot = ENV["WORKSPACE"]
        TargetEnvPath = LocalProjectRoot + "/infrastructure/vagrant/aws/vista-exchange"
        ENV['PROVIDER'] = "awsconf"
        ENV['PROVIDER_BOX_NAME'] = "aws-dummy"
        ENV['PROVIDER_BOX_URL'] = "https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box"
        puts TargetEnvPath
        #require File.expand_path(TargetEnvPath + "/../VagrantfileUtil.rb")
        ENV['EHMP_IP'] = VagrantfileUtil::AWS.get_private_ip("vista-exchange", "ehmp")
        ENV['VE_API_IP'] = VagrantfileUtil::AWS.get_private_ip("vista-exchange", "ve-api")
        ENV['JDS_IP'] = VagrantfileUtil::AWS.get_private_ip("vista-exchange", "jds")
    when 'local'      # virtualbox, not managed
        puts "Using Virtualbox environment"
        setInternetEnvs
        ENV['WORKSPACE'] = '/Users/bergc/Projects/vistacore/ehmp'
        ENV['PROVIDER'] = "vbconf"
        ENV['SSH_USER_ID'] = "vagrant"
        ENV['PROVIDER_BOX_NAME'] = "rhel-6.5"
        ENV['PROVIDER_BOX_URL'] = "https://store.vistacore.us/nexus/content/repositories/filerepo/third-party/program/vagrant/basebox/rhel/6-5-trial/rhel-6-5-trial.box"
        LocalProjectRoot = ENV["WORKSPACE"]
        TargetEnvPath = LocalProjectRoot + "/infrastructure/vagrant/aws/vista-exchange"
        #require File.expand_path(TargetEnvPath + '/../../Servers.rb')
        ENV["EHMP_IP"] = Servers::EHMP.localIP
        ENV["VE_API_IP"] = Servers::VE_API.localIP
        ENV["JDS_IP"] = Servers::JDS.localIP
    else
        raise "Unrecognized environment #{ENV['ENVIRONMENT']}"
    end
end

