tweaks to template roots, add testing

This commit is contained in:
David Dollar
2011-06-27 13:17:26 -04:00
parent e8d2552caa
commit c8d0dba1cb
3 changed files with 22 additions and 8 deletions

View File

@@ -4,7 +4,6 @@ require "foreman/utils"
class Foreman::Export::Base
attr_reader :engine
attr_accessor :template
def initialize(engine)
@engine = engine
@@ -24,11 +23,11 @@ private ######################################################################
puts "[foreman export] %s" % message
end
def export_template(path, file)
if template and File.exist?(file_path = File.join(template, file))
def export_template(exporter, file, template_root)
if template_root && File.exist?(file_path = File.join(template_root, file))
File.read(file_path)
else
File.read(File.expand_path("../../../../data/export/#{path}/#{file}", __FILE__))
File.read(File.expand_path("../../../../data/export/#{exporter}/#{file}", __FILE__))
end
end

View File

@@ -11,7 +11,7 @@ class Foreman::Export::Upstart < Foreman::Export::Base
app = options[:app] || File.basename(engine.directory)
user = options[:user] || app
log_root = options[:log] || "/var/log/#{app}"
self.template = options[:template]
template_root = options[:template]
Dir["#{location}/#{app}*.conf"].each do |file|
say "cleaning up: #{file}"
@@ -20,14 +20,14 @@ class Foreman::Export::Upstart < Foreman::Export::Base
concurrency = Foreman::Utils.parse_concurrency(options[:concurrency])
master_template = export_template("upstart", "master.conf.erb")
master_template = export_template("upstart", "master.conf.erb", template_root)
master_config = ERB.new(master_template).result(binding)
write_file "#{location}/#{app}.conf", master_config
process_template = export_template("upstart", "process.conf.erb")
process_template = export_template("upstart", "process.conf.erb", template_root)
engine.processes.values.each do |process|
process_master_template = export_template("upstart", "process_master.conf.erb")
process_master_template = export_template("upstart", "process_master.conf.erb", template_root)
process_master_config = ERB.new(process_master_template).result(binding)
write_file "#{location}/#{app}-#{process.name}.conf", process_master_config

View File

@@ -21,4 +21,19 @@ describe Foreman::Export::Upstart do
File.read("/tmp/init/app-bravo.conf").should == example_export_file("upstart/app-bravo.conf")
File.read("/tmp/init/app-bravo-1.conf").should == example_export_file("upstart/app-bravo-1.conf")
end
context "with alternate templates" do
let(:template_root) { "/tmp/alternate" }
before do
FileUtils.mkdir_p template_root
File.open("#{template_root}/master.conf.erb", "w") { |f| f.puts "alternate_template" }
end
it "can export with alternate template files" do
upstart.export("/tmp/init", :template => template_root)
File.read("/tmp/init/app.conf").should == "alternate_template\n"
end
end
end