<commit_before>require 'test_helper'
require 'pathname'
require 'logger'
require 'yaml'
require 'net/http'

$base_dir = Pathname.new(__FILE__).dirname

MiniTest::Parallel.processor_count = 5

# A module that provides logger and log_file attached to an integration log file
module Loggable
  # The file that logs will go do, using the class name as a differentiator
  def log_file
    return @log_file if @log_file
    @log_file = $base_dir.join('..', 'log', "#{self.class}-integration.log")
    @log_file.dirname.mkpath
    @log_file
  end

  # The logger object so you can say logger.info to log messages
  def logger
    @logger ||= Logger.new(log_file)
  end
end

class IntegrationTest < TestCase
  include Loggable

  # Returns a name for the current test's server
  # that should be fairly unique.
  def server_name
    "knife_solo-#{image_id}"
  end

  # Shortcut to access the test runner
  def runner
    MiniTest::Unit.runner
  end

  # Returns the server for this test, retrieved from the test runner
  def server
    return @server if @server
    @server = runner.get_server(self)
  end

  # The flavor to run this test on
  def flavor_id
    "m1.small"
  end

  # Sets up a kitchen directory to work in
  def setup
    @kitchen = $base_dir.join('support', 'kitchens', self.class.to_s)
    @kitchen.dirname.mkpath
    system "knife kitchen #{@kitchen} >> #{log_file}"
    @start_dir = Dir.pwd
    Dir.chdir(@kitchen)
    prepare_server
  end

  # Removes the test kitchen
  def teardown
    Dir.chdir(@start_dir)
    FileUtils.remove_entry_secure(@kitchen)
  end

  # Prepares the server unless it has already been marked as such
  def prepare_server
    return if server.tags["knife_solo_prepared"]
    assert_subcommand "prepare"
    runner.tag_as_prepared(server)
  end

  # Asserts that a prepare or cook command is successful
  def assert_subcommand(subcommand)
    verbose = ENV['VERBOSE'] && "-VV"
    key_file = MiniTest::Unit.runner.key_file
    system "knife #{subcommand} -i #{key_file} #{user}@#{server.public_ip_address} #{verbose} >> #{log_file}"
    assert $?.success?
  end

  # Tries to run cook on the box
  module EmptyCook
    def test_empty_cook
      assert_subcommand "cook"
    end
  end

  # Tries to cook with apache2 cookbook and
  # verifies the "It Works!" page is present.
  module Apache2Cook
    def write_cheffile
      File.open('Cheffile', 'w') do |f|
        f.print <<-CHEF
            site 'http://community.opscode.com/api/v1'
            cookbook 'apache2'
        CHEF
      end
    end

    def write_nodefile
      File.open("nodes/#{server.public_ip_address}.json", 'w') do |f|
        f.print <<-JSON
          { "run_list": ["recipe[apache2]"] }
        JSON
      end
    end

    def http_response
      Net::HTTP.get(URI.parse("http://"+server.public_ip_address))
    end

    def default_apache_message
      /It works!/
    end

    def test_apache2
      write_cheffile
      system "librarian-chef install >> #{log_file}"
      write_nodefile
      assert_subcommand "cook"<commit_msg>Write node file for every run since we may skip preparation on a given test but kitchen still gets regenerated<commit_after>require 'test_helper'
require 'pathname'
require 'logger'
require 'yaml'
require 'json'
require 'net/http'

$base_dir = Pathname.new(__FILE__).dirname

MiniTest::Parallel.processor_count = 5

# A module that provides logger and log_file attached to an integration log file
module Loggable
  # The file that logs will go do, using the class name as a differentiator
  def log_file
    return @log_file if @log_file
    @log_file = $base_dir.join('..', 'log', "#{self.class}-integration.log")
    @log_file.dirname.mkpath
    @log_file
  end

  # The logger object so you can say logger.info to log messages
  def logger
    @logger ||= Logger.new(log_file)
  end
end

class IntegrationTest < TestCase
  include Loggable

  # Returns a name for the current test's server
  # that should be fairly unique.
  def server_name
    "knife_solo-#{image_id}"
  end

  # Shortcut to access the test runner
  def runner
    MiniTest::Unit.runner
  end

  # Returns the server for this test, retrieved from the test runner
  def server
    return @server if @server
    @server = runner.get_server(self)
  end

  # The flavor to run this test on
  def flavor_id
    "m1.small"
  end

  # Sets up a kitchen directory to work in
  def setup
    @kitchen = $base_dir.join('support', 'kitchens', self.class.to_s)
    @kitchen.dirname.mkpath
    system "knife kitchen #{@kitchen} >> #{log_file}"
    @start_dir = Dir.pwd
    Dir.chdir(@kitchen)
    prepare_server
  end

  # Removes the test kitchen
  def teardown
    Dir.chdir(@start_dir)
    FileUtils.remove_entry_secure(@kitchen)
  end

  # Writes out the given node hash as a json file
  def write_nodefile(node)
    File.open("nodes/#{server.public_ip_address}.json", 'w') do |f|
      f.print node.to_json
    end
  end

  # Prepares the server unless it has already been marked as such
  def prepare_server
    return if server.tags["knife_solo_prepared"]
    assert_subcommand "prepare"
    runner.tag_as_prepared(server)
  end

  # Asserts that a prepare or cook command is successful
  def assert_subcommand(subcommand)
    verbose = ENV['VERBOSE'] && "-VV"
    key_file = MiniTest::Unit.runner.key_file
    system "knife #{subcommand} -i #{key_file} #{user}@#{server.public_ip_address} #{verbose} >> #{log_file}"
    assert $?.success?
  end

  # Tries to run cook on the box
  module EmptyCook
    def test_empty_cook
      write_nodefile(run_list: [])
      assert_subcommand "cook"
    end
  end

  # Tries to cook with apache2 cookbook and
  # verifies the "It Works!" page is present.
  module Apache2Cook
    def write_cheffile
      File.open('Cheffile', 'w') do |f|
        f.print <<-CHEF
            site 'http://community.opscode.com/api/v1'
            cookbook 'apache2'
        CHEF
      end
    end

    def http_response
      Net::HTTP.get(URI.parse("http://"+server.public_ip_address))
    end

    def default_apache_message
      /It works!/
    end

    def test_apache2
      write_cheffile
      system "librarian-chef install >> #{log_file}"
      write_nodefile(run_list: ["recipe[apache2]"])
      assert_subcommand "cook"
{'fraction_non_alphanumeric': 0.09285258455647734, 'fraction_numerical': 0.0025526483726866626, 'mean_word_length': 3.1162179908076166, 'pattern_counts': {'":': 1, '<': 11, '<?xml version=': 0, '>': 15, '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': '5365711', 'n_tokens_mistral': 2077, 'n_tokens_neox': 2021, 'n_words': 619}