<commit_before>require 'yajl'
require 'bosh/dev/sandbox/main'

module IntegrationExampleGroup
  def deploy_simple(options={})
    no_track = options.fetch(:no_track, false)
    manifest_hash = options.fetch(:manifest_hash, Bosh::Spec::Deployments.simple_manifest)

    deployment_manifest = yaml_file('simple', manifest_hash)

    run_bosh("target http://localhost:#{current_sandbox.director_port}")
    run_bosh('login admin admin')

    run_bosh('create release', work_dir: TEST_RELEASE_DIR)
    run_bosh('upload release', work_dir: TEST_RELEASE_DIR)

    run_bosh("upload stemcell #{spec_asset('valid_stemcell.tgz')}")

    run_bosh("deployment #{deployment_manifest.path}")
    deploy_result = run_bosh("#{no_track ? '--no-track ' : ''}deploy")
    expect($?).to be_success
    deploy_result
  end

  def run_bosh(cmd, options = {})
    failure_expected = options.fetch(:failure_expected, false)
    work_dir = options.fetch(:work_dir, BOSH_WORK_DIR)
    Dir.chdir(work_dir) do
      command = "bosh -n -c #{BOSH_CONFIG} #{cmd}"
      output = `#{command} 2>&1`
      if $?.exitstatus != 0 && !failure_expected
        raise "ERROR: #{command} failed with #{output}"
      end
      output
    end
  end

  def current_sandbox
    @current_sandbox = Thread.current[:sandbox] || Bosh::Dev::Sandbox::Main.new
    Thread.current[:sandbox] = @current_sandbox
  end

  def regexp(string)
    Regexp.compile(Regexp.escape(string))
  end

  def format_output(out)
    out.gsub(/^\s*/, '').gsub(/\s*$/, '')
  end

  # forcefully suppress raising on error...caller beware
  def expect_output(cmd, expected_output)
    format_output(run_bosh(cmd, :failure_expected => true)).should == format_output(expected_output)
  end

  def get_vms
    output = run_bosh('vms --details')
    table = output.lines.grep(/\|/)

    table = table.map { |line| line.split('|').map(&:strip).reject(&:empty?) }
    headers = table.shift || []
    headers.map! do |header|
      header.downcase.tr('/ ', '_').to_sym
    end
    output = []
    table.each do |row|
      output << Hash[headers.zip(row)]
    end
    output
  end

  def wait_for_vm(name)
    5.times do
      vm = get_vms.detect { |v| v[:job_index] == name }
      return vm if vm
    end
    nil
  end

  def kill_job_agent(name)
    vm = get_vms.detect { |v| v[:job_index] == name }
    Process.kill('INT', vm[:cid].to_i)
    vm[:cid]
  end

  def self.included(base)
    base.before do |example|
      unless $sandbox_started
        puts 'Starting sandboxed environment for BOSH tests...'
        current_sandbox.start

        $sandbox_started = true
        at_exit do
          begin
            if $!
              status = $!.is_a?(::SystemExit) ? $!.status : 1
            else
              status = 0
            end
            puts "\n  Stopping sandboxed environment for BOSH tests..."
            current_sandbox.stop
            cleanup_bosh
          ensure
            exit status
          end
        end
      end

      unless example.example.metadata[:no_reset]
        desc = example ? example.example.metadata[:description] : ''
        current_sandbox.reset(desc)
      end
    end

    base.after do |example|
      desc = example ? example.example.metadata[:description] : ""
      current_sandbox.save_task_logs(desc)
      FileUtils.rm_rf(current_sandbox.cloud_storage_dir)
    end
  end
end
<commit_msg>Changed use of example for Rspec3 upgrade<commit_after>require 'yajl'
require 'bosh/dev/sandbox/main'

module IntegrationExampleGroup
  def deploy_simple(options={})
    no_track = options.fetch(:no_track, false)
    manifest_hash = options.fetch(:manifest_hash, Bosh::Spec::Deployments.simple_manifest)

    deployment_manifest = yaml_file('simple', manifest_hash)

    run_bosh("target http://localhost:#{current_sandbox.director_port}")
    run_bosh('login admin admin')

    run_bosh('create release', work_dir: TEST_RELEASE_DIR)
    run_bosh('upload release', work_dir: TEST_RELEASE_DIR)

    run_bosh("upload stemcell #{spec_asset('valid_stemcell.tgz')}")

    run_bosh("deployment #{deployment_manifest.path}")
    deploy_result = run_bosh("#{no_track ? '--no-track ' : ''}deploy")
    expect($?).to be_success
    deploy_result
  end

  def run_bosh(cmd, options = {})
    failure_expected = options.fetch(:failure_expected, false)
    work_dir = options.fetch(:work_dir, BOSH_WORK_DIR)
    Dir.chdir(work_dir) do
      command = "bosh -n -c #{BOSH_CONFIG} #{cmd}"
      output = `#{command} 2>&1`
      if $?.exitstatus != 0 && !failure_expected
        raise "ERROR: #{command} failed with #{output}"
      end
      output
    end
  end

  def current_sandbox
    @current_sandbox = Thread.current[:sandbox] || Bosh::Dev::Sandbox::Main.new
    Thread.current[:sandbox] = @current_sandbox
  end

  def regexp(string)
    Regexp.compile(Regexp.escape(string))
  end

  def format_output(out)
    out.gsub(/^\s*/, '').gsub(/\s*$/, '')
  end

  # forcefully suppress raising on error...caller beware
  def expect_output(cmd, expected_output)
    format_output(run_bosh(cmd, :failure_expected => true)).should == format_output(expected_output)
  end

  def get_vms
    output = run_bosh('vms --details')
    table = output.lines.grep(/\|/)

    table = table.map { |line| line.split('|').map(&:strip).reject(&:empty?) }
    headers = table.shift || []
    headers.map! do |header|
      header.downcase.tr('/ ', '_').to_sym
    end
    output = []
    table.each do |row|
      output << Hash[headers.zip(row)]
    end
    output
  end

  def wait_for_vm(name)
    5.times do
      vm = get_vms.detect { |v| v[:job_index] == name }
      return vm if vm
    end
    nil
  end

  def kill_job_agent(name)
    vm = get_vms.detect { |v| v[:job_index] == name }
    Process.kill('INT', vm[:cid].to_i)
    vm[:cid]
  end

  def self.included(base)
    base.before do |example|
      unless $sandbox_started
        puts 'Starting sandboxed environment for BOSH tests...'
        current_sandbox.start

        $sandbox_started = true
        at_exit do
          begin
            if $!
              status = $!.is_a?(::SystemExit) ? $!.status : 1
            else
              status = 0
            end
            puts "\n  Stopping sandboxed environment for BOSH tests..."
            current_sandbox.stop
            cleanup_bosh
          ensure
            exit status
          end
        end
      end

      unless example.metadata[:no_reset]
        desc = example ? example.metadata[:description] : ''
        current_sandbox.reset(desc)
      end
    end

    base.after do |example|
      desc = example ? example.metadata[:description] : ""
      current_sandbox.save_task_logs(desc)
      FileUtils.rm_rf(current_sandbox.cloud_storage_dir)
    end
  end
end

{'fraction_non_alphanumeric': 0.12633293838862558, 'fraction_numerical': 0.0019253554502369668, 'mean_word_length': 2.7558398220244715, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '6189870', 'n_tokens_mistral': 2279, 'n_tokens_neox': 2184, 'n_words': 460}