spec/support/helpers.rb in engineyard-1.4.29 vs spec/support/helpers.rb in engineyard-1.7.0.pre2

- old
+ new

@@ -22,23 +22,29 @@ end end module IntegrationHelpers def run_ey(command_options, ey_options={}) + if respond_to?(:extra_ey_options) # needed for ssh tests ey_options.merge!(extra_ey_options) + return ey(command_to_run(command_options), ey_options) end - ey(command_to_run(command_options), ey_options) + if ey_options[:expect_failure] + fast_failing_ey(command_to_run(command_options)) + else + fast_ey(command_to_run(command_options)) + end end - def make_scenario(hash) + def make_scenario(opts) # since nil will silently turn to empty string when interpolated, # and there's a lot of string matching involved in integration # testing, it would be nice to have early notification of typos. scenario = Hash.new { |h,k| raise "Tried to get key #{k.inspect}, but it's missing!" } - scenario.merge!(hash) + scenario.merge!(opts) end end module GitRepoHelpers def define_git_repo(name, &setup) @@ -67,46 +73,53 @@ end NonzeroExitStatus = Class.new(UnexpectedExit) ZeroExitStatus = Class.new(UnexpectedExit) def ey_api - @api ||= EY::API.new('asdf') + @api ||= EY::CloudClient.new('asdf', EY::CLI::UI.new) end - def fast_ey(args) - err, out = StringIO.new, StringIO.new - capture_stderr_into(err) do - capture_stdout_into(out) do - with_env('DEBUG' => 'true') do - EY::CLI.start(args) + def ensure_eyrc + begin + unless (data = read_eyrc) and data['api_token'] + raise ".eyrc has no token, specs will stall waiting for stdin authentication input" + end + rescue Errno::ENOENT => e + raise ".eyrc must be written before calling run_ey or specs will stall waiting for stdin authentication input" + end + end + + def fast_ey(args, options = {}) + + ensure_eyrc + + begin + err, out = StringIO.new, StringIO.new + debug = options[:debug] == false ? nil : 'true' + capture_stderr_into(err) do + capture_stdout_into(out) do + with_env('DEBUG' => debug) do + EY::CLI.start(args) + end end end + ensure + @err, @out = err.string, out.string + @raw_ssh_commands, @ssh_commands = extract_ssh_commands(@out) end - ensure - @err, @out = err.string, out.string - @raw_ssh_commands, @ssh_commands = extract_ssh_commands(@out) end def fast_failing_ey(*args) begin fast_ey(*args) raise ZeroExitStatus.new(@out, @err) rescue SystemExit => exit_status # SystemExit typically indicates a bogus command, which we # here in expected-to-fail land are entirely happy with. nil - rescue EY::Error => e - more_err, more_out = StringIO.new, StringIO.new - - capture_stderr_into(more_err) do - capture_stdout_into(more_out) do - EY.ui.print_exception(e) - end - end - - @err << more_err.string - @out << more_out.string + rescue EY::Error, EY::CloudClient::Error => e + nil end end def capture_stderr_into(stream) $stderr = stream @@ -121,11 +134,16 @@ ensure $stdout = STDOUT end def ey(args = [], options = {}, &block) + if respond_to?(:extra_ey_options) # needed for ssh tests + options.merge!(extra_ey_options) + end + hide_err = options.has_key?(:hide_err) ? options[:hide_err] : options[:expect_failure] + path_prepends = options[:prepend_to_path] ey_env = { 'DEBUG' => 'true', 'EYRC' => ENV['EYRC'], @@ -152,10 +170,11 @@ eybin = File.expand_path('../bundled_ey', __FILE__) with_env(ey_env) do exit_status = Open4::open4("#{eybin} #{Escape.shell_command(args)}") do |pid, stdin, stdout, stderr| block.call(stdin) if block + stdin.close @out = stdout.read @err = stderr.read end if !exit_status.success? && !options[:expect_failure] @@ -170,11 +189,11 @@ puts @err unless @err.empty? || hide_err @out end def extract_ssh_commands(output) - raw_ssh_commands = @out.split(/\n/).find_all do |line| + raw_ssh_commands = [@out,@err].join("\n").split(/\n/).find_all do |line| line =~ /^bash -lc/ || line =~ /^ssh/ end ssh_commands = raw_ssh_commands.map do |cmd| # Strip off everything up to and including user@host, leaving @@ -196,12 +215,49 @@ end [raw_ssh_commands, ssh_commands] end - def api_scenario(scenario, remote = "user@git.host:path/to/repo.git") - response = ::RestClient.put(EY.fake_awsm + '/scenario', {"scenario" => scenario, "remote" => remote}, {}) - raise "Setting scenario failed: #{response.inspect}" unless response.code == 200 + DEPRECATED_SCENARIOS = { + "empty" => "User Name", + "one app without environment" => "App Without Env", + "one app, one environment, not linked" => "Unlinked App", + "two apps" => "Two Apps", + "one app, one environment" => "Linked App", + "two accounts, two apps, two environments, ambiguous" => "Multiple Ambiguous Accounts", + "one app, one environment, no instances" => "Linked App Not Running", + "one app, one environment, app master red" => "Linked App Red Master", + "one app, many environments" => "One App Many Envs", + "one app, many similarly-named environments" => "One App Similarly Named Envs", + "two apps, same git uri" => "Two Apps Same Git URI", + } + + def api_scenario(old_name) + clean_eyrc # switching scenarios, always clean up + name = DEPRECATED_SCENARIOS[old_name] + @scenario = EY::CloudClient::Test::Scenario[name] + @scenario_email = @scenario.email + @scenario_password = @scenario.password + @scenario_api_token = @scenario.api_token + @scenario + end + + def login_scenario(scenario_name) + scen = api_scenario(scenario_name) + write_eyrc('api_token' => scenario_api_token) + scen + end + + def scenario_email + @scenario_email + end + + def scenario_password + @scenario_password + end + + def scenario_api_token + @scenario_api_token end def read_yaml(file) YAML.load(File.read(File.expand_path(file))) end