<commit_before>require 'spec_helper_acceptance'
require 'securerandom'

describe "ec2_instance" do

  before(:all) do
    @config = {
      :name => "#{PuppetManifest.env_id}-#{SecureRandom.uuid}",
      :instance_type => 't1.micro',
      :region => 'sa-east-1',
      :image_id => 'ami-41e85d5c',
      :ensure => 'present',
    }
    @ec2 = Ec2Helper.new(@config[:region])
    @template = 'instance.pp.tmpl'
    PuppetManifest.new(@template, @config).apply
  end

  after(:all) do
    wait_until_status(@config[:name], 'running')

    new_config = @config.update({:ensure => 'absent'})
    PuppetManifest.new(@template, new_config).apply

    wait_until_status(@config[:name], 'shutting-down')
  end

  def find_instance(name)
    instances = @ec2.get_instances(name)
    expect(instances.count).to eq(1)
    instances.first
  end

  def wait_until_status(name, status, max_wait = 15)
    slept = 0

    loop do
      current_status = find_instance(name).state.name
      break if current_status == status

      sleep(1)
      slept += 1

      if slept > max_wait
        msg = "Exceeded timeout of #{max_wait} waiting for #{name} to be #{status}"
        expect(current_status).to eq(status), msg
      end
    end
  end

  describe 'should create a new instance' do

    before(:all) do
      @instance = find_instance(@config[:name])
    end

    it "with the specified name" do
      expect(@instance.tags.detect { |tag| tag.key == 'Name' }.value).to eq(@config[:name])
    end

    it "with the specified type" do
      expect(@instance.instance_type).to eq(@config[:instance_type])
    end<commit_msg>Refactor instance acceptance test organization<commit_after>require 'spec_helper_acceptance'
require 'securerandom'

describe "ec2_instance" do

  before(:all) do
    @default_region = 'sa-east-1'
    @ec2 = Ec2Helper.new(@default_region)
    @template = 'instance.pp.tmpl'
  end

  def find_instance(name)
    instances = @ec2.get_instances(name)
    expect(instances.count).to eq(1)
    instances.first
  end

  def wait_until_status(name, status, max_wait = 15)
    slept = 0

    loop do
      current_status = find_instance(name).state.name
      break if current_status == status

      sleep(1)
      slept += 1

      if slept > max_wait
        msg = "Exceeded timeout of #{max_wait} waiting for #{name} to be #{status}"
        expect(current_status).to eq(status), msg
      end
    end
  end

  describe 'should create a new instance' do

    before(:all) do
      @config = {
        :name => "#{PuppetManifest.env_id}-#{SecureRandom.uuid}",
        :instance_type => 't1.micro',
        :region => @default_region,
        :image_id => 'ami-41e85d5c',
        :ensure => 'present',
      }

      PuppetManifest.new(@template, @config).apply
      @instance = find_instance(@config[:name])
    end

    after(:all) do
      wait_until_status(@config[:name], 'running')

      new_config = @config.update({:ensure => 'absent'})
      PuppetManifest.new(@template, new_config).apply

      wait_until_status(@config[:name], 'shutting-down')
    end

    it "with the specified name" do
      expect(@instance.tags.detect { |tag| tag.key == 'Name' }.value).to eq(@config[:name])
    end

    it "with the specified type" do
      expect(@instance.instance_type).to eq(@config[:instance_type])
    end
{'fraction_non_alphanumeric': 0.13301233824857056, 'fraction_numerical': 0.01023171832681312, 'mean_word_length': 3.078527607361963, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '1342804', 'n_tokens_mistral': 1111, 'n_tokens_neox': 1081, 'n_words': 243}