diff --git a/README.md b/README.md index 6cf4b4a..37db6a5 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,9 @@ export BAT_INFRASTRUCTURE= # Run tests with --fail-fast and skip cleanup in case of failure (optional) export BAT_DEBUG_MODE= + +# Provide an alternate directory where Bat::CloudConfig#load_template(cpi) should find "cloud_config_${cpi}.yml.erb" files +export BAT_CC_TEMPLATES_DIR= ``` #### Environment variables for the BOSH v2 cli diff --git a/lib/bat/cloud_config.rb b/lib/bat/cloud_config.rb index b08c52e..6476b5e 100644 --- a/lib/bat/cloud_config.rb +++ b/lib/bat/cloud_config.rb @@ -8,7 +8,10 @@ module Bosh; end # Ugly hack module Bat class CloudConfig - def initialize(spec) + DEFAULT_TEMPLATES_DIR = File.expand_path("../../../templates/", __FILE__) + + def initialize(env, spec) + @env = env @spec = spec generate_cloud_config(spec) end @@ -53,7 +56,8 @@ def store_cloud_config(content) end def load_template(cpi) - template = File.expand_path("../../../templates/cloud_config_#{cpi}.yml.erb", __FILE__) + template = File.join(cc_templates_dir, "cloud_config_#{cpi}.yml.erb") + File.read(template) end @@ -64,5 +68,15 @@ def tempfile(name) def to_s "#{name}" end + + def cc_templates_dir + if @env.cc_templates_dir && + @env.cc_templates_dir != "" && + Dir.exist?(@env.cc_templates_dir) + @env.cc_templates_dir + else + DEFAULT_TEMPLATES_DIR + end + end end end diff --git a/lib/bat/env.rb b/lib/bat/env.rb index 02f4cd8..d131ff2 100644 --- a/lib/bat/env.rb +++ b/lib/bat/env.rb @@ -10,6 +10,7 @@ class Env OPTIONAL_VARS = { debug_mode: 'BAT_DEBUG_MODE', + cc_templates_dir: 'BAT_CC_TEMPLATES_DIR', }.freeze def self.from_env diff --git a/lib/bat/requirements.rb b/lib/bat/requirements.rb index 7d3a757..648cc1a 100644 --- a/lib/bat/requirements.rb +++ b/lib/bat/requirements.rb @@ -2,15 +2,15 @@ module Bat class Requirements include RSpec::Matchers - def initialize(stemcell_path, bosh_runner, spec_state, logger) - @stemcell_path = stemcell_path + def initialize(env, bosh_runner, spec_state, logger) + @env = env @bosh_runner = bosh_runner @spec_state = spec_state @logger = logger end def stemcell - @stemcell ||= Bat::Stemcell.from_path(@stemcell_path) + @stemcell ||= Bat::Stemcell.from_path(@env.stemcell_path) end def release @@ -82,7 +82,7 @@ def only_scan_and_fix_processing? end def update_cloud_config(deployment_spec) - cloud_config = Bat::CloudConfig.new(deployment_spec) + cloud_config = Bat::CloudConfig.new(@env, deployment_spec) update_cc_output = @bosh_runner.bosh_safe("update-cloud-config #{cloud_config.to_path}") puts update_cc_output diff --git a/spec/bat/requirements_spec.rb b/spec/bat/requirements_spec.rb index 362712a..1bb0fc8 100644 --- a/spec/bat/requirements_spec.rb +++ b/spec/bat/requirements_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'bat/env' require 'bat/requirements' require 'bat/bosh_runner' require 'bat/spec_state' @@ -7,10 +8,13 @@ require 'common/exec' describe Bat::Requirements do - subject(:requirements) { described_class.new("/some-stemcell/path", bosh_runner, spec_state, logger) } + subject(:requirements) { described_class.new(bat_env, bosh_runner, spec_state, logger) } + let(:bosh_runner) { Bat::BoshRunner.new("bosh", logger) } let(:logger) { Logger.new('/dev/null') } let(:spec_state) { Bat::SpecState.new(false) } + let(:stemcell_path) { "/some-stemcell/path" } + let(:bat_env) { instance_double(Bat::Env, stemcell_path: stemcell_path) } describe "#tasks_processing?" do let(:tasks_result) { Bosh::Exec::Result.new('bosh tasks', tasks_json, 0) } diff --git a/spec/system/spec_helper.rb b/spec/system/spec_helper.rb index c2a16fd..34d6fd6 100644 --- a/spec/system/spec_helper.rb +++ b/spec/system/spec_helper.rb @@ -31,7 +31,7 @@ class OnlyScanAndFixProcessing < StandardError;end env = Bat::Env.from_env spec_state = Bat::SpecState.new(env.debug_mode) bosh_runner = Bat::BoshRunner.new(ENV['BAT_BOSH_CLI'], logger) -requirements = Bat::Requirements.new(env.stemcell_path, bosh_runner, spec_state, logger) +requirements = Bat::Requirements.new(env, bosh_runner, spec_state, logger) RSpec.configure do |config| config.include(Bat::BoshHelper)