Class: TddDeploy::Server

Inherits:
Base
  • Object
show all
Defined in:
lib/tdd_deploy/server.rb

Overview

TddDeploy::Server

implements a simple ‘rack’ server. Methods are either used internally or called from the web page during page reloads.

It only displays one page - which is defined in the gem in lib/tdd_deploy/server-templates/test_results.html.erb.

Constant Summary collapse

LIB_DIR =
File.expand_path('../..', __FILE__)
HOST_TESTS_DIR =
File.join(Dir.pwd, 'lib', 'tdd_deploy', 'host_tests')
SITE_TESTS_DIR =
File.join(Dir.pwd, 'lib', 'tdd_deploy', 'site_tests')
LOCAL_TESTS_DIR =
File.join(Dir.pwd, 'lib', 'tdd_deploy', 'local_tests')
TEMPLATE_PATH =
File.join(LIB_DIR, 'tdd_deploy', 'server-templates', 'test_results.html.erb')

Constants included from Assertions

Assertions::GROUP_ELT_TAG, Assertions::HEADER_ELT_TAG, Assertions::RESULT_ELT_TAG

Constants included from Environ

Environ::ENV_FNAME

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DeployTestMethods

#deploy_test_file_exists_on_hosts_as, #deploy_test_on_a_host_as, #deploy_test_on_hosts_as, #deploy_test_process_running_on_hosts_as

Methods included from RunMethods

#ping_host, #run_locally, #run_on_a_host_as, #run_on_all_hosts, #run_on_all_hosts_as, #run_on_hosts_as

Methods included from Assertions

#assert, #assert_equal, #assert_match, #assert_nil, #assert_not_nil, #assert_raises, #fail, #failure_count, #failure_messages, #formatted_test_results, #pass, #refute, #refute_equal, #refute_nil, #remove_failed_tests, #reset_tests, #test_count, #test_messages, #test_results, #total_failures, #total_tests

Methods included from CopyMethods

#append_dir_to_remote_hosts_as, #append_file_to_remote_file_as, #append_file_to_remote_hosts_as, #append_string_to_remote_file_as, #append_string_to_remote_file_on_hosts_as, #copy_dir_to_remote_hosts_as, #copy_file_to_remote_as, #copy_file_to_remote_hosts_as, #copy_string_to_remote_file_as, #copy_string_to_remote_file_on_hosts_as, #mkdir_on_remote_as

Methods included from Environ

#capfile, #clear_env, #env_defaults, #env_desc, #env_hash, #env_hash=, #env_types, #hosts, #hosts=, #list_to_str, #migration_hosts, #rationalize_host_list, #read_env, #reset_env, #save_env, #set_env, #str_to_list

Constructor Details

#initialize(*args) ⇒ Server

Returns a new instance of Server.

Raises:

  • (RuntimeError)


27
28
29
30
31
32
# File 'lib/tdd_deploy/server.rb', line 27

def initialize *args
  raise RuntimeError.new("No Environment File") unless File.exists? TddDeploy::Environ::ENV_FNAME
  super
  load_all_tests
  @request_count = 0
end

Instance Attribute Details

#query_hashObject

Returns the value of attribute query_hash.



25
26
27
# File 'lib/tdd_deploy/server.rb', line 25

def query_hash
  @query_hash
end

#test_classesObject

Returns the value of attribute test_classes.



25
26
27
# File 'lib/tdd_deploy/server.rb', line 25

def test_classes
  @test_classes
end

Instance Method Details

#call(env) ⇒ Object

rack interface. takes an env Hash and returns [code, headers, body]



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/tdd_deploy/server.rb', line 58

def call(env)
  self.query_hash = parse_query_string(env['QUERY_STRING'])

  if self.query_hash['run_configurator']
    require 'tdd_deploy/configurator'
    configurator = TddDeploy::Configurator.new
    configurator.make_configuration_files
  end
  
  if self.query_hash['install_special']
    require 'tdd_deploy/installer'
    installer = TddDeploy::Installer.new
    [:app_hosts, :balance_hosts, :db_hosts, :web_hosts].each do |host_list|
      installer.empty_special_dir self.site_user, host_list
    end
    [:app_hosts, :balance_hosts, :db_hosts, :web_hosts].each do |host_list|
      installer.install_special_files_on_host_list_as self.site_user, host_list
    end
    query_hash['failed-tests'] = failed_tests.join(',')
  end
  
  if self.query_hash['install_configs']
    require 'tdd_deploy/installer'
    installer ||= TddDeploy::Installer.new
    [:app_hosts, :balance_hosts, :db_hosts, :web_hosts].each do |host_list|
      installer.install_config_files_on_host_list_as self.site_user, host_list
    end
    query_hash['failed-tests'] = failed_tests.join(',')
  end
  
  if self.query_hash['run_cap_deploy']
    require 'tdd_deploy/installer'
    installer ||= TddDeploy::Installer.new
    installer.run_cap_deploy
    query_hash['failed-tests'] = true
  end

  load_all_tests
  
  if query_hash['failed-tests'] && @request_count > 0
    remove_failed_tests
    run_selected_tests(query_hash['failed-tests'])
  else
    run_all_tests
  end
  @request_count += 1
  
  query_string = new_query_string
  body = [
    render_results,
    # "#{env.inspect}"
    ]
  return [200, {'Content-Length' => body.join('').length.to_s, 'Content-Type' => 'text/html'}, body]
end

#failed_testsObject

failed_tests returns a unique, sorted list of strings. It just seemed easier to do it in the accessors than take the chance of using push, pop, etc and mucking it up - like I did before.

Raises:

  • (RuntimeError)


37
38
39
40
41
# File 'lib/tdd_deploy/server.rb', line 37

def failed_tests
  @failed_tests ||= []
  raise RuntimeError.new("@failed_tests is not an Array: #{@failed_tests.inspect}") unless @failed_tests.is_a? Array
  @failed_tests = @failed_tests.map { |x| x.to_s }.uniq.sort
end

#failed_tests=(value) ⇒ Object

failed_tests= does the right thing for all kinds of input variations.



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/tdd_deploy/server.rb', line 44

def failed_tests=(value)
  begin
    value = value.split(/[\s,]+/) if value.is_a? String
    value = [value] unless value.is_a? Array
  
    @failed_tests = @failed_tests.to_a unless @failed_tests.is_a? Array

    @failed_tests = (@failed_tests + value.map { |x| x.to_s }).uniq
  rescue
    @failed_tests = (@failed_tests.to_a.map { |x| x.to_s } + value.to_a.map { |x| x.to_s }).uniq.sort
  end
end

#load_all_testsObject

loads all files in ‘lib/tdd_deploy/host_tests | site_tests | local_tests. both host_tests and site_tests are clobbered by the rake install task. local_tests is safe.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/tdd_deploy/server.rb', line 116

def load_all_tests
  # discard any already defined tests
  TddDeploy::TestBase.flush_children_methods
  
  # reload all tests
  [TddDeploy::Server::HOST_TESTS_DIR, TddDeploy::Server::SITE_TESTS_DIR,
      TddDeploy::Server::LOCAL_TESTS_DIR].each do |dir|
    if File.exists?(dir)
      # puts "gathering tests from #{dir}"
      Dir.new(dir).each do |fname|
        load File.join(dir, fname) if fname =~ /\.rb$/
      end
    else
      puts "skipping #{dir} - no such directory"
    end
  end

  self.test_classes = TddDeploy::TestBase.children

  @test_to_class_map = {}
  self.test_classes.each do |klass|
    klass.instance_methods(false).each do |func|
      @test_to_class_map[func.to_s] = klass
    end
  end
end

#run_all_testsObject

Re-reads the environment and then runs all known tests.



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/tdd_deploy/server.rb', line 144

def run_all_tests
  read_env
  reset_tests

  ret = true
  self.failed_tests = []
  self.test_classes.each do |klass|
    ret &= run_all_tests_in_class(klass)
  end
  ret
end

#run_selected_tests(test_list) ⇒ Object

Re-reads the environment and then runs tests from ‘test_list’



157
158
159
160
161
162
163
164
165
166
# File 'lib/tdd_deploy/server.rb', line 157

def run_selected_tests(test_list)
  read_env
  ret = true
  test_list = test_list.split(/[\s,]+/) if test_list.is_a? String
  self.failed_tests -= test_list
  test_list.each do |test|
    ret &= run_a_test test
  end
  ret
end