<commit_before>require 'spec_helper'
require 'net/ssh/gateway'
require 'rest-client'
require 'yaml'
require 'json'
require 'securerandom'
require 'mysql'
require 'digest'

describe 'HA Artifactory' do
  before(:all) do
    @gateway = Net::SSH::Gateway.new(bosh_target, bosh_director_ssh_username,
      :password => bosh_director_ssh_password)
    @load_balancer_ip = get_load_balancer_ip_from bosh_manifest
    @standalone_node_ip = get_first_node_ip_from bosh_manifest
    bundle_exec_bosh "target #{bosh_target}"
    bundle_exec_bosh "deployment #{bosh_manifest}"
    bundle_exec_bosh "login #{bosh_username} #{bosh_password}"
    #TODO: is hardcoding the license here really the right answer?
    ENV["ARTIFACTORY1_LICENSE"] = ENV["TEST_LICENSE_1"]
    ENV["ARTIFACTORY_LICENSE"] = ENV["TEST_LICENSE"]
    bosh_deploy_and_wait_for_artifactory
  end

  describe 'Initial Checks' do
    it 'should verify that deployed artifactory is running, version is correct and HA addon is available' do  
      response = RestClient.get "http://admin:password@" + @load_balancer_ip + ":8081/artifactory/api/system/version"
      expect(JSON.parse(response)['version']).to eq(expected_artifactory_version)
      expect(JSON.parse(response)['addons'].include? 'ha').to eq(true)
    end

    it 'should verify that artifactory is accessible via the route' do
      response = RestClient.get artifactory_route_version_url
      expect(JSON.parse(response)['version']).to eq(expected_artifactory_version)
    end

    describe 'artifactory logs dir' do
      it 'should have artifactory log' do
        log_path = artifactory_package_logs+'artifactory.log'
        result = exec_on_node(@standalone_node_ip, "ls #{log_path}")
        expect(result).to eq("#{log_path}\n")
      end

      it 'should have access log' do
        log_path = artifactory_package_logs+'access.log'
        result = exec_on_node(@standalone_node_ip, "ls #{log_path}")
        expect(result).to eq("#{log_path}\n")
      end

      it 'should have request log' do
        log_path = artifactory_package_logs+'request.log'
        result = exec_on_node(@standalone_node_ip, "ls #{log_path}")
        expect(result).to eq("#{log_path}\n")
      end

      it 'should have import.export log' do
        log_path = artifactory_package_logs+'import.export.log'
        result = exec_on_node(@standalone_node_ip, "ls #{log_path}")
        expect(result).to eq("#{log_path}\n")
      end
    end
  end

  

  describe 'licensing' do
    it 'should have a license present' do
        response = RestClient.get artifactory_license_url artifactory_port      
	expect(JSON.parse(response)['type']).to eq('High Availability')
        parsed_response = JSON.parse(response)['validThrough']
        $original_expiry_date = parsed_response
        puts "Original Expiry date: #{$original_expiry_date}"
    end

    context 'when license is changed' do
   
      it 'updates the license' do
        ENV["ARTIFACTORY_LICENSE"] = ENV["TEST_LICENSE_2"]
        ENV["ARTIFACTORY1_LICENSE"] = ENV["TEST_LICENSE_3"]
        puts "Deploying Lic 2:  If this test fails, check expiration date of license 2"
        bosh_deploy_and_wait_for_artifactory
        response = RestClient.get artifactory_license_url artifactory_port
        parsed_response = JSON.parse(response)['validThrough']
        puts "Expiry Date: #{parsed_response}"
        expect(parsed_response).to_not eq($original_expiry_date)
      end

      it 'resets to the original license' do
        puts 'Resetting to original license from deployment'
        ENV["ARTIFACTORY1_LICENSE"] = ENV["TEST_LICENSE_1"]
        ENV["ARTIFACTORY_LICENSE"] = ENV["TEST_LICENSE"]
        bosh_deploy_and_wait_for_artifactory
        response = RestClient.get artifactory_license_url artifactory_port
        parsed_response = JSON.parse(response)['validThrough']
        puts "Expiry Date: #{parsed_response}"
        expect(parsed_response).to eq($original_expiry_date)
      end
    end
  end

  describe 'final routing check' do
    context 'everything should be done now' do
      it 'verify that route is still available after the tests have ran' do
        response = RestClient.get artifactory_route_version_url
        expect(JSON.parse(response)['version']).to eq(expected_artifactory_version)
      end
    end
  end
end

def bosh_deploy_and_wait_for_artifactory
  bundle_exec_bosh "deploy"
  wait_for_artifactory_available
end

def bundle_exec_bosh command
  output = `bundle exec bosh -n #{command}`
  expect($?.to_i).to eq(0), output
end

def get_first_node_ip_from bosh_manifest
 YAML.load_file(bosh_manifest)['jobs'].
 find{|job_hash| job_hash['name'] == 'ha-artifactory'}['networks'].first['static_ips'].first
end

def get_load_balancer_ip_from bosh_manifest
 YAML.load_file(bosh_manifest)['jobs'].
 find{|job_hash| job_hash['name'] == 'nginx'}['properties']['artifactory_host']
end

def exec_on_gateway
  @gateway.open(@standalone_node_ip, artifactory_port) do |port|
    yield port
  end
end

def exec_on_node(node, cmd, options = {})
  user           = options.fetch(:user, 'vcap')
  password       = options.fetch(:password, '<PASSWORD>')
  run_as_root    = options.fetch(:root, false)
  discard_stderr = options.fetch(:discard_stderr, false)

  cmd = "echo -e \"#{password}\\n\" | sudo -S #{cmd}" if run_as_root
  cmd << ' 2>/dev/null' if discard_stderr

  @gateway.ssh(
    node,
    user,
    password: password,
    paranoid: false
  ) do |ssh|
    ssh.exec!(cmd)
  end
end

def wait_for_artifactory_available
  exec_on_gateway do | port |
    begin
      RestClient.get artifactory_status_api port
    rescue RestClient::ServiceUnavailable
      puts "still waiting for artifactory to become available, sleeping for 5secs"
      sleep(5)
      retry
    rescue RestClient::Forbidden
      puts "license maybe expired"
    end
    puts "artifactory available"
  end
end

def bosh_target
  ENV['BOSH_TARGET'] || '192.168.50.4'
end<commit_msg>fix the way to ping<commit_after>require 'spec_helper'
require 'net/ssh/gateway'
require 'rest-client'
require 'yaml'
require 'json'
require 'securerandom'
require 'mysql'
require 'digest'

describe 'HA Artifactory' do
  before(:all) do
    @gateway = Net::SSH::Gateway.new(bosh_target, bosh_director_ssh_username,
      :password => bosh_director_ssh_password)
    @load_balancer_ip = get_load_balancer_ip_from bosh_manifest
    @standalone_node_ip = get_first_node_ip_from bosh_manifest
    bundle_exec_bosh "target #{bosh_target}"
    bundle_exec_bosh "deployment #{bosh_manifest}"
    bundle_exec_bosh "login #{bosh_username} #{bosh_password}"
    #TODO: is hardcoding the license here really the right answer?
    ENV["ARTIFACTORY1_LICENSE"] = ENV["TEST_LICENSE_1"]
    ENV["ARTIFACTORY_LICENSE"] = ENV["TEST_LICENSE"]
    bosh_deploy_and_wait_for_artifactory
  end

  describe 'Initial Checks' do
    it 'should verify that deployed artifactory is running, version is correct and HA addon is available' do  
      response = RestClient.get "http://admin:password@" + @load_balancer_ip + ":8081/artifactory/api/system/version"
      expect(JSON.parse(response)['version']).to eq(expected_artifactory_version)
      expect(JSON.parse(response)['addons'].include? 'ha').to eq(true)
    end

    it 'should verify that artifactory is accessible via the route' do
      response = RestClient.get artifactory_route_version_url
      expect(JSON.parse(response)['version']).to eq(expected_artifactory_version)
    end

    describe 'artifactory logs dir' do
      it 'should have artifactory log' do
        log_path = artifactory_package_logs+'artifactory.log'
        result = exec_on_node(@standalone_node_ip, "ls #{log_path}")
        expect(result).to eq("#{log_path}\n")
      end

      it 'should have access log' do
        log_path = artifactory_package_logs+'access.log'
        result = exec_on_node(@standalone_node_ip, "ls #{log_path}")
        expect(result).to eq("#{log_path}\n")
      end

      it 'should have request log' do
        log_path = artifactory_package_logs+'request.log'
        result = exec_on_node(@standalone_node_ip, "ls #{log_path}")
        expect(result).to eq("#{log_path}\n")
      end

      it 'should have import.export log' do
        log_path = artifactory_package_logs+'import.export.log'
        result = exec_on_node(@standalone_node_ip, "ls #{log_path}")
        expect(result).to eq("#{log_path}\n")
      end
    end
  end

  

  describe 'licensing' do
    it 'should have a license present' do
        response = RestClient.get artifactory_license_url artifactory_port      
	expect(JSON.parse(response)['type']).to eq('High Availability')
        parsed_response = JSON.parse(response)['validThrough']
        $original_expiry_date = parsed_response
        puts "Original Expiry date: #{$original_expiry_date}"
    end

    context 'when license is changed' do
   
      it 'updates the license' do
        ENV["ARTIFACTORY_LICENSE"] = ENV["TEST_LICENSE_2"]
        ENV["ARTIFACTORY1_LICENSE"] = ENV["TEST_LICENSE_3"]
        puts "Deploying Lic 2:  If this test fails, check expiration date of license 2"
        bosh_deploy_and_wait_for_artifactory
        response = RestClient.get artifactory_license_url artifactory_port
        parsed_response = JSON.parse(response)['validThrough']
        puts "Expiry Date: #{parsed_response}"
        expect(parsed_response).to_not eq($original_expiry_date)
      end

      it 'resets to the original license' do
        puts 'Resetting to original license from deployment'
        ENV["ARTIFACTORY1_LICENSE"] = ENV["TEST_LICENSE_1"]
        ENV["ARTIFACTORY_LICENSE"] = ENV["TEST_LICENSE"]
        bosh_deploy_and_wait_for_artifactory
        response = RestClient.get artifactory_license_url artifactory_port
        parsed_response = JSON.parse(response)['validThrough']
        puts "Expiry Date: #{parsed_response}"
        expect(parsed_response).to eq($original_expiry_date)
      end
    end
  end

  describe 'final routing check' do
    context 'everything should be done now' do
      it 'verify that route is still available after the tests have ran' do
        response = RestClient.get artifactory_route_version_url
        expect(JSON.parse(response)['version']).to eq(expected_artifactory_version)
      end
    end
  end
end

def bosh_deploy_and_wait_for_artifactory
  bundle_exec_bosh "deploy"
  wait_for_artifactory_available
end

def bundle_exec_bosh command
  output = `bundle exec bosh -n #{command}`
  expect($?.to_i).to eq(0), output
end

def get_first_node_ip_from bosh_manifest
 YAML.load_file(bosh_manifest)['jobs'].
 find{|job_hash| job_hash['name'] == 'ha-artifactory'}['networks'].first['static_ips'].first
end

def get_load_balancer_ip_from bosh_manifest
 YAML.load_file(bosh_manifest)['jobs'].
 find{|job_hash| job_hash['name'] == 'nginx'}['properties']['artifactory_host']
end

def exec_on_gateway
  @gateway.open(@standalone_node_ip, artifactory_port) do |port|
    yield port
  end
end

def exec_on_node(node, cmd, options = {})
  user           = options.fetch(:user, 'vcap')
  password       = options.fetch(:password, '<PASSWORD>')
  run_as_root    = options.fetch(:root, false)
  discard_stderr = options.fetch(:discard_stderr, false)

  cmd = "echo -e \"#{password}\\n\" | sudo -S #{cmd}" if run_as_root
  cmd << ' 2>/dev/null' if discard_stderr

  @gateway.ssh(
    node,
    user,
    password: password,
    paranoid: false
  ) do |ssh|
    ssh.exec!(cmd)
  end
end

def wait_for_artifactory_available
    begin
      RestClient.get artifactory_status_api artifactory_port
    rescue RestClient::ServiceUnavailable
      puts "still waiting for artifactory to become available, sleeping for 5secs"
      sleep(5)
      retry
    rescue RestClient::Forbidden
      puts "license maybe expired"
    end
    puts "artifactory available"
end

def bosh_target
  ENV['BOSH_TARGET'] || '192.168.50.4'
end
{'fraction_non_alphanumeric': 0.10168067226890756, 'fraction_numerical': 0.004369747899159664, 'mean_word_length': 3.851610273134937, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '604714', 'n_tokens_mistral': 3957, 'n_tokens_neox': 3853, 'n_words': 879}