Skip to content
This repository
Browse code

Test ENV vars in deploy hooks to ensure they're always set.

Also set them in ENV in case they aren't set by login shell.
  • Loading branch information...
commit af6c2b8288e1ac7ee35f07f42ce25902e2e90f6f 1 parent f137c1b
Martin Emde martinemde authored
10 lib/engineyard-serverside/configuration.rb
@@ -128,8 +128,16 @@ def binstubs_path
128 128 release_path + '/ey_bundler_binstubs'
129 129 end
130 130
  131 + def framework_env_names
  132 + %w[RAILS_ENV RACK_ENV NODE_ENV MERB_ENV]
  133 + end
  134 +
131 135 def framework_envs
132   - "RAILS_ENV=#{environment} RACK_ENV=#{environment} NODE_ENV=#{environment} MERB_ENV=#{environment}"
  136 + framework_env_names.map { |e| "#{e}=#{environment}" }.join(' ')
  137 + end
  138 +
  139 + def set_framework_envs
  140 + framework_env_names.each { |e| ENV[e] = environment }
133 141 end
134 142
135 143 def current_path
1  lib/engineyard-serverside/deploy_hook.rb
@@ -32,6 +32,7 @@ def syntax_error(file)
32 32 class CallbackContext
33 33 def initialize(config)
34 34 @configuration = config
  35 + @configuration.set_framework_envs
35 36 @node = node
36 37 end
37 38
37 spec/deploy_hook_spec.rb
... ... @@ -1,23 +1,18 @@
1 1 require 'spec_helper'
2 2
3 3 describe "the deploy-hook API" do
4   - before(:each) do
5   - @hook = EY::Serverside::DeployHook.new(options)
6   - @callback_context = EY::Serverside::DeployHook::CallbackContext.new(@hook.config)
  4 + def deploy_hook(options={})
  5 + EY::Serverside::DeployHook.new(options)
7 6 end
8 7
9 8 def run_hook(options={}, &blk)
10 9 raise ArgumentError unless block_given?
11   - options.each do |k, v|
12   - @callback_context.configuration[k] = v
13   - end
14   -
15 10 # The hooks on the filesystem are run by passing a string to
16 11 # context.instance_eval, not a block. However, using a block
17 12 # should allow us to get the same degree of test coverage and
18 13 # still let us have things like syntax checking work on this spec
19 14 # file.
20   - @callback_context.instance_eval(&blk)
  15 + deploy_hook(options).callback_context.instance_eval(&blk)
21 16 end
22 17
23 18 context "#run" do
@@ -46,9 +41,9 @@ def run_hook(options={}, &blk)
46 41 end
47 42
48 43 it "runs things with sudo" do
49   - @callback_context.should_receive(:system).with("sudo sh -l -c 'do it as root'").and_return(true)
50   -
51   - run_hook { sudo("do it as root") }
  44 + callback = deploy_hook.callback_context
  45 + callback.should_receive(:system).with("sudo sh -l -c 'do it as root'").and_return(true)
  46 + callback.instance_eval { sudo("do it as root") }
52 47 end
53 48 end
54 49
@@ -123,6 +118,15 @@ def run_hook(options={}, &blk)
123 118 end
124 119 end
125 120
  121 + context "environment variables" do
  122 + it "sets the framework env variables" do
  123 + run_hook('framework_env' => 'production') { ENV['RAILS_ENV'] }.should == 'production'
  124 + run_hook('framework_env' => 'production') { ENV['RACK_ENV'] }.should == 'production'
  125 + run_hook('framework_env' => 'production') { ENV['MERB_ENV'] }.should == 'production'
  126 + run_hook('framework_env' => 'production') { ENV['NODE_ENV'] }.should == 'production'
  127 + end
  128 + end
  129 +
126 130 context "has methods to run code only on certain instances" do
127 131 def scenarios
128 132 [
@@ -141,10 +145,11 @@ def scenarios
141 145
142 146 def where_code_runs_with(method, *args)
143 147 scenarios.map do |s|
144   - @callback_context.configuration[:current_roles] = s[:instance_role].split(',')
145   - @callback_context.configuration[:current_name] = s[:name]
  148 + hook_result = run_hook('current_roles' => s[:instance_role].split(','), 'current_name' => s[:name]) do
  149 + send(method, *args) { 'ran!' }
  150 + end
146 151
147   - if run_hook { send(method, *args) { 'ran!'} } == 'ran!'
  152 + if hook_result == 'ran!'
148 153 result = s[:instance_role]
149 154 result << "_#{s[:name]}" if s[:name]
150 155 result
@@ -189,14 +194,14 @@ def where_code_runs_with(method, *args)
189 194 context "#syntax_error" do
190 195 it "returns nil for hook files containing valid Ruby syntax" do
191 196 hook_path = File.expand_path('../fixtures/valid_hook.rb', __FILE__)
192   - @hook.syntax_error(hook_path).should be_nil
  197 + deploy_hook.syntax_error(hook_path).should be_nil
193 198 end
194 199
195 200 it "returns a brief problem description for hook files containing valid Ruby syntax" do
196 201 hook_path = File.expand_path('../fixtures/invalid_hook.rb', __FILE__)
197 202 desc = "spec/fixtures/invalid_hook.rb:1: syntax error, unexpected '^'"
198 203 match = /#{Regexp.escape desc}/
199   - @hook.syntax_error(hook_path).should =~ match
  204 + deploy_hook.syntax_error(hook_path).should =~ match
200 205 end
201 206 end
202 207

0 comments on commit af6c2b8

Please sign in to comment.
Something went wrong with that request. Please try again.