<commit_before>require 'fileutils'
require 'open-uri'
require 'json'
require File.expand_path(File.dirname(__FILE__) + '/misc/test_support')

include FileUtils

def create_app_dir(source, startup_file, dir_name)
  app_root = "/home/app/#{dir_name}"
  rm_rf(app_root)
  mkdir(app_root)
  cp("/system/internal/test/misc/#{source}", "#{app_root}/#{startup_file}")
  mkdir("#{app_root}/public")
  mkdir("#{app_root}/tmp")
  chown_R("app", "app", app_root)
  app_root
end

def create_app_dirs
  app_dirs = []
  app_dirs << create_app_dir("ruby_test_app.rb", "config.ru", "ruby_test_app")
  app_dirs << create_app_dir("python_test_app.py", "passenger_wsgi.py", "python_test_app")
  app_dirs << create_app_dir("nodejs_test_app.js", "app.js", "nodejs_test_app")
  app_dirs
end

def list_passenger_instances
  output = `passenger-config list-instances --json`
  if $?.exitstatus != 0
    abort "The command 'passenger-config list-instances --json' failed"
  end
  JSON.parse(output)
end

def passenger_instance_fully_initialized?(instance)
  !instance['core_pid'].nil?
end

shared_examples_for "Hello world Ruby application" do
  it "works" do
    URI.open("http://passenger.test/", "rb") do |io|
      expect(io.read).to eql("Hello Ruby\n")
    end
  end
end

shared_examples_for "Hello world Python application" do
  it "works" do
    URI.open("http://1.passenger.test/", "rb") do |io|
      expect(io.read).to eql("Hello Python\n")
    end
  end
end

shared_examples_for "Hello world Node.js application" do
  it "works" do
    URI.open("http://2.passenger.test/", "rb") do |io|
      expect(io.read).to eql("Hello Node.js\n")
    end
  end
end

describe "The system's Apache with Passenger enabled" do
  before :all do
    @app_dirs = create_app_dirs
    cp("/system/internal/test/apache/vhost.conf", "/etc/apache2/sites-enabled/001-testapp.conf")
    chmod(0644, "/etc/apache2/sites-enabled/001-testapp.conf")
    sh("service apache2 start")
    eventually do
      ping_tcp_socket("127.0.0.1", 80)
    end

    # Apache restarts all modules during startup, so
    # we can briefly end up with two Passenger instances.
    # Wait until the first instance is gone.

    # Wait until at least one Passenger instance exists.
    eventually do
      list_passenger_instances.size >= 1
    end
    # Allow some time for Apache to restart Passenger during startup.
    sleep 1
    # Wait until exactly one *fully initialized* Passenger instance
    # exists.
    eventually do
      instances = list_passenger_instances<commit_msg>back-compat URI.open changes<commit_after>require 'fileutils'
require 'open-uri'
require 'json'
require File.expand_path(File.dirname(__FILE__) + '/misc/test_support')

include FileUtils

def create_app_dir(source, startup_file, dir_name)
  app_root = "/home/app/#{dir_name}"
  rm_rf(app_root)
  mkdir(app_root)
  cp("/system/internal/test/misc/#{source}", "#{app_root}/#{startup_file}")
  mkdir("#{app_root}/public")
  mkdir("#{app_root}/tmp")
  chown_R("app", "app", app_root)
  app_root
end

def create_app_dirs
  app_dirs = []
  app_dirs << create_app_dir("ruby_test_app.rb", "config.ru", "ruby_test_app")
  app_dirs << create_app_dir("python_test_app.py", "passenger_wsgi.py", "python_test_app")
  app_dirs << create_app_dir("nodejs_test_app.js", "app.js", "nodejs_test_app")
  app_dirs
end

def list_passenger_instances
  output = `passenger-config list-instances --json`
  if $?.exitstatus != 0
    abort "The command 'passenger-config list-instances --json' failed"
  end
  JSON.parse(output)
end

def passenger_instance_fully_initialized?(instance)
  !instance['core_pid'].nil?
end

shared_examples_for "Hello world Ruby application" do
  it "works" do
    if if RUBY_VERSION >= '2.5'
      URI.open("http://passenger.test/", "rb") do |io|
        expect(io.read).to eql("Hello Ruby\n")
      end
    else
      open("http://passenger.test/", "rb") do |io|
        expect(io.read).to eql("Hello Ruby\n")
      end
    end
  end
end

shared_examples_for "Hello world Python application" do
  it "works" do
    if if RUBY_VERSION >= '2.5'
      URI.open("http://1.passenger.test/", "rb") do |io|
        expect(io.read).to eql("Hello Python\n")
      end
    else
      open("http://1.passenger.test/", "rb") do |io|
        expect(io.read).to eql("Hello Python\n")
      end
    end
  end
end

shared_examples_for "Hello world Node.js application" do
  it "works" do
    if if RUBY_VERSION >= '2.5'
      URI.open("http://2.passenger.test/", "rb") do |io|
        expect(io.read).to eql("Hello Node.js\n")
      end
    else
      open("http://2.passenger.test/", "rb") do |io|
        expect(io.read).to eql("Hello Node.js\n")
      end
    end
  end
end

describe "The system's Apache with Passenger enabled" do
  before :all do
    @app_dirs = create_app_dirs
    cp("/system/internal/test/apache/vhost.conf", "/etc/apache2/sites-enabled/001-testapp.conf")
    chmod(0644, "/etc/apache2/sites-enabled/001-testapp.conf")
    sh("service apache2 start")
    eventually do
      ping_tcp_socket("127.0.0.1", 80)
    end

    # Apache restarts all modules during startup, so
    # we can briefly end up with two Passenger instances.
    # Wait until the first instance is gone.

    # Wait until at least one Passenger instance exists.
    eventually do
      list_passenger_instances.size >= 1
    end
    # Allow some time for Apache to restart Passenger during startup.
    sleep 1
    # Wait until exactly one *fully initialized* Passenger instance
    # exists.
    eventually do
      instances = list_passenger_instances
{'fraction_non_alphanumeric': 0.13087308730873087, 'fraction_numerical': 0.010801080108010801, 'mean_word_length': 3.5728395061728393, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 8, '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': '6526295', 'n_tokens_mistral': 1977, 'n_tokens_neox': 1927, 'n_words': 465}