test/support/apache2_controller.rb in passenger-4.0.60 vs test/support/apache2_controller.rb in passenger-5.0.0.beta1

- old
+ new

@@ -25,63 +25,63 @@ # # Suppose that you want to test a hypothetical "AlwaysPrintHelloWorld" # Apache configuration option. Then you can write the following test: # # apache = Apache2Controller.new -# +# # # Add a configuration option to the configuration file. # apache << "AlwaysPrintHelloWorld on" -# +# # # Write configuration file and start Apache with that configuration file. # apache.start -# +# # begin # response_body = http_get("http://localhost:#{apache.port}/some/url") # response_body.should == "hello world!" # ensure # apache.stop # end class Apache2Controller include PhusionPassenger STUB_DIR = File.expand_path(File.dirname(__FILE__) + "/../stub/apache2") - + class VHost attr_accessor :domain attr_accessor :document_root attr_accessor :additional_configs - + def initialize(domain, document_root) @domain = domain @document_root = document_root @additional_configs = [] end - + def <<(config) @additional_configs << config end end - + attr_accessor :port attr_accessor :vhosts attr_reader :server_root - + def initialize(options = nil) set(options) if options @port = 64506 @vhosts = [] @extra = [] @server_root = File.expand_path('tmp.apache2') - @passenger_root = File.expand_path(PhusionPassenger.source_root) + @passenger_root = File.expand_path(PhusionPassenger.install_spec) @mod_passenger = PhusionPassenger.apache2_module_path end - + def set(options) options.each_pair do |key, value| instance_variable_set("@#{key}", value) end end - + # Create an Apache configuration folder and start Apache on that # configuration folder. This method does not return until Apache # has done initializing. # # If Apache is already started, this this method will stop Apache first. @@ -89,22 +89,22 @@ if running? stop else File.unlink("#{@server_root}/httpd.pid") rescue nil end - + if File.exist?(@server_root) FileUtils.rm_r(@server_root) end FileUtils.mkdir_p(@server_root) write_config_file FileUtils.cp("#{STUB_DIR}/mime.types", @server_root) - + if !system(PlatformInfo.httpd, "-f", "#{@server_root}/httpd.conf", "-k", "start") raise "Could not start an Apache server." end - + begin # Wait until the PID file has been created. Timeout::timeout(20) do while !File.exist?("#{@server_root}/httpd.pid") sleep(0.1) @@ -130,18 +130,18 @@ if File.file?(filename) File.chmod(0666, filename) end end end - + def graceful_restart write_config_file if !system(PlatformInfo.httpd, "-f", "#{@server_root}/httpd.conf", "-k", "graceful") raise "Cannot restart Apache." end end - + # Stop Apache and delete its configuration folder. This method waits # until Apache is done with its shutdown procedure. # # This method does nothing if Apache is already stopped. def stop @@ -182,11 +182,11 @@ if File.exist?(@server_root) FileUtils.chmod_R(0777, @server_root) FileUtils.rm_r(@server_root) end end - + # Define a virtual host configuration block for the Apache configuration # file. If there was already a vhost definition with the same domain name, # then it will be overwritten. # # The given document root will be created if it doesn't exist. @@ -197,11 +197,11 @@ yield vhost end vhosts.reject! {|host| host.domain == domain} vhosts << vhost end - + # Checks whether this Apache instance is running. def running? if File.exist?("#{@server_root}/httpd.pid") pid = File.read("#{@server_root}/httpd.pid").strip begin @@ -214,45 +214,45 @@ end else return false end end - + # Defines a configuration snippet to be added to the Apache configuration file. def <<(line) @extra << line end private def get_binding return binding end - + def write_config_file template = ERB.new(File.read("#{STUB_DIR}/httpd.conf.erb")) File.open("#{@server_root}/httpd.conf", 'w') do |f| f.write(template.result(get_binding)) end end - + def modules_dir @@modules_dir ||= `#{PlatformInfo.apxs2} -q LIBEXECDIR`.strip end - + def builtin_modules @@builtin_modules ||= `#{PlatformInfo.httpd} -l`.split("\n").grep(/\.c$/).map do |line| line.strip end end - + def has_builtin_module?(name) return builtin_modules.include?(name) end - + def has_module?(name) return File.exist?("#{modules_dir}/#{name}") end - + def we_are_root? return Process.uid == 0 end end