beaker 4.38.1 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (225) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +9 -0
  3. data/.github/workflows/release.yml +2 -2
  4. data/.github/workflows/test.yml +36 -11
  5. data/.rubocop.yml +75 -0
  6. data/.rubocop_todo.yml +239 -0
  7. data/CHANGELOG.md +88 -33
  8. data/Gemfile +16 -4
  9. data/HISTORY.md +103 -0
  10. data/Rakefile +101 -112
  11. data/acceptance/config/acceptance-options.rb +1 -1
  12. data/acceptance/config/base/acceptance-options.rb +2 -2
  13. data/acceptance/config/hypervisor/acceptance-options.rb +2 -2
  14. data/acceptance/config/subcommands/acceptance-options.rb +2 -2
  15. data/acceptance/fixtures/module/Gemfile +1 -1
  16. data/acceptance/fixtures/module/Rakefile +3 -3
  17. data/acceptance/fixtures/module/spec/acceptance/demo_spec.rb +16 -23
  18. data/acceptance/fixtures/module/spec/classes/init_spec.rb +1 -2
  19. data/acceptance/fixtures/module/spec/spec_helper_acceptance.rb +3 -5
  20. data/acceptance/lib/helpers/test_helper.rb +7 -6
  21. data/acceptance/pre_suite/subcommands/05_install_ruby.rb +2 -3
  22. data/acceptance/pre_suite/subcommands/08_install_beaker.rb +1 -2
  23. data/acceptance/tests/base/dsl/helpers/configuration_test.rb +4 -4
  24. data/acceptance/tests/base/dsl/helpers/hocon_helpers_test.rb +7 -8
  25. data/acceptance/tests/base/dsl/helpers/host_helpers/add_system32_hosts_entry_test.rb +0 -3
  26. data/acceptance/tests/base/dsl/helpers/host_helpers/archive_file_from_test.rb +9 -7
  27. data/acceptance/tests/base/dsl/helpers/host_helpers/backup_the_file_test.rb +15 -21
  28. data/acceptance/tests/base/dsl/helpers/host_helpers/check_for_package_test.rb +0 -4
  29. data/acceptance/tests/base/dsl/helpers/host_helpers/create_remote_file_test.rb +23 -30
  30. data/acceptance/tests/base/dsl/helpers/host_helpers/curl_on_test.rb +3 -3
  31. data/acceptance/tests/base/dsl/helpers/host_helpers/curl_with_retries_test.rb +0 -1
  32. data/acceptance/tests/base/dsl/helpers/host_helpers/on_test.rb +21 -16
  33. data/acceptance/tests/base/dsl/helpers/host_helpers/retry_on_test.rb +7 -7
  34. data/acceptance/tests/base/dsl/helpers/host_helpers/rsync_to_test.rb +30 -38
  35. data/acceptance/tests/base/dsl/helpers/host_helpers/run_cron_on_test.rb +14 -18
  36. data/acceptance/tests/base/dsl/helpers/host_helpers/run_script_on_test.rb +8 -14
  37. data/acceptance/tests/base/dsl/helpers/host_helpers/run_script_test.rb +7 -11
  38. data/acceptance/tests/base/dsl/helpers/host_helpers/scp_from_test.rb +5 -5
  39. data/acceptance/tests/base/dsl/helpers/host_helpers/scp_to_test.rb +4 -5
  40. data/acceptance/tests/base/dsl/helpers/host_helpers/shell_test.rb +8 -8
  41. data/acceptance/tests/base/dsl/helpers/host_helpers/upgrade_package_test.rb +4 -6
  42. data/acceptance/tests/base/dsl/platform_tag_confiner_test.rb +14 -17
  43. data/acceptance/tests/base/dsl/structure_test.rb +9 -21
  44. data/acceptance/tests/base/host/file_test.rb +8 -8
  45. data/acceptance/tests/base/host/group_test.rb +2 -3
  46. data/acceptance/tests/base/host/host_test.rb +69 -66
  47. data/acceptance/tests/base/host/packages.rb +27 -30
  48. data/acceptance/tests/base/host/packages_unix.rb +0 -55
  49. data/acceptance/tests/base/host/user_test.rb +2 -3
  50. data/acceptance/tests/base/host_prebuilt_steps/ssh_environment_test.rb +1 -2
  51. data/acceptance/tests/base/test_suite/export.rb +6 -9
  52. data/acceptance/tests/install/from_file.rb +3 -5
  53. data/acceptance/tests/load_path_bootstrap.rb +1 -1
  54. data/acceptance/tests/subcommands/destroy.rb +19 -21
  55. data/acceptance/tests/subcommands/exec.rb +0 -1
  56. data/acceptance/tests/subcommands/init.rb +2 -3
  57. data/acceptance/tests/subcommands/provision.rb +0 -1
  58. data/beaker.gemspec +10 -14
  59. data/docs/concepts/argument_processing_and_precedence.md +1 -10
  60. data/docs/concepts/style_guide.md +1 -1
  61. data/docs/how_to/debug_beaker_tests.md +13 -13
  62. data/docs/how_to/hosts/eos.md +2 -12
  63. data/docs/how_to/install_puppet.md +0 -18
  64. data/docs/how_to/test_arbitrary_beaker_versions.md +2 -2
  65. data/docs/how_to/the_beaker_dsl.md +0 -2
  66. data/lib/beaker/cli.rb +63 -74
  67. data/lib/beaker/command.rb +22 -30
  68. data/lib/beaker/command_factory.rb +4 -3
  69. data/lib/beaker/dsl/assertions.rb +7 -19
  70. data/lib/beaker/dsl/helpers/hocon_helpers.rb +5 -9
  71. data/lib/beaker/dsl/helpers/host_helpers.rb +72 -133
  72. data/lib/beaker/dsl/helpers/test_helpers.rb +3 -5
  73. data/lib/beaker/dsl/helpers/web_helpers.rb +20 -28
  74. data/lib/beaker/dsl/helpers.rb +2 -4
  75. data/lib/beaker/dsl/outcomes.rb +17 -19
  76. data/lib/beaker/dsl/patterns.rb +1 -3
  77. data/lib/beaker/dsl/roles.rb +18 -21
  78. data/lib/beaker/dsl/structure.rb +55 -67
  79. data/lib/beaker/dsl/test_tagging.rb +7 -10
  80. data/lib/beaker/dsl/wrappers.rb +15 -16
  81. data/lib/beaker/dsl.rb +2 -3
  82. data/lib/beaker/host/aix/exec.rb +1 -1
  83. data/lib/beaker/host/aix/file.rb +0 -1
  84. data/lib/beaker/host/aix/group.rb +3 -3
  85. data/lib/beaker/host/aix/user.rb +3 -3
  86. data/lib/beaker/host/aix.rb +3 -4
  87. data/lib/beaker/host/cisco.rb +36 -48
  88. data/lib/beaker/host/eos.rb +4 -30
  89. data/lib/beaker/host/freebsd/exec.rb +1 -1
  90. data/lib/beaker/host/freebsd/pkg.rb +3 -3
  91. data/lib/beaker/host/freebsd.rb +9 -12
  92. data/lib/beaker/host/mac/exec.rb +5 -5
  93. data/lib/beaker/host/mac/group.rb +13 -13
  94. data/lib/beaker/host/mac/pkg.rb +6 -109
  95. data/lib/beaker/host/mac/user.rb +7 -7
  96. data/lib/beaker/host/mac.rb +8 -9
  97. data/lib/beaker/host/pswindows/exec.rb +70 -74
  98. data/lib/beaker/host/pswindows/file.rb +4 -6
  99. data/lib/beaker/host/pswindows/group.rb +5 -5
  100. data/lib/beaker/host/pswindows/pkg.rb +17 -17
  101. data/lib/beaker/host/pswindows/user.rb +4 -4
  102. data/lib/beaker/host/pswindows.rb +4 -3
  103. data/lib/beaker/host/unix/exec.rb +86 -88
  104. data/lib/beaker/host/unix/file.rb +41 -47
  105. data/lib/beaker/host/unix/group.rb +3 -3
  106. data/lib/beaker/host/unix/pkg.rb +158 -421
  107. data/lib/beaker/host/unix/user.rb +4 -4
  108. data/lib/beaker/host/unix.rb +18 -20
  109. data/lib/beaker/host/windows/exec.rb +20 -20
  110. data/lib/beaker/host/windows/file.rb +5 -5
  111. data/lib/beaker/host/windows/group.rb +5 -5
  112. data/lib/beaker/host/windows/pkg.rb +6 -57
  113. data/lib/beaker/host/windows/user.rb +4 -4
  114. data/lib/beaker/host/windows.rb +13 -13
  115. data/lib/beaker/host.rb +82 -139
  116. data/lib/beaker/host_prebuilt_steps.rb +121 -233
  117. data/lib/beaker/hypervisor/noop.rb +2 -4
  118. data/lib/beaker/hypervisor.rb +46 -63
  119. data/lib/beaker/local_connection.rb +4 -6
  120. data/lib/beaker/logger.rb +71 -85
  121. data/lib/beaker/logger_junit.rb +22 -26
  122. data/lib/beaker/network_manager.rb +40 -43
  123. data/lib/beaker/options/command_line_parser.rb +12 -23
  124. data/lib/beaker/options/hosts_file_parser.rb +34 -25
  125. data/lib/beaker/options/options_file_parser.rb +3 -6
  126. data/lib/beaker/options/options_hash.rb +3 -10
  127. data/lib/beaker/options/parser.rb +89 -105
  128. data/lib/beaker/options/presets.rb +114 -123
  129. data/lib/beaker/options/subcommand_options_file_parser.rb +3 -6
  130. data/lib/beaker/options/validator.rb +26 -31
  131. data/lib/beaker/perf.rb +29 -34
  132. data/lib/beaker/platform.rb +38 -46
  133. data/lib/beaker/result.rb +7 -6
  134. data/lib/beaker/shared/error_handler.rb +8 -10
  135. data/lib/beaker/shared/fog_credentials.rb +5 -9
  136. data/lib/beaker/shared/host_manager.rb +40 -46
  137. data/lib/beaker/shared/options_resolver.rb +3 -7
  138. data/lib/beaker/shared/repetition.rb +6 -8
  139. data/lib/beaker/shared/semvar.rb +39 -43
  140. data/lib/beaker/shared/timed.rb +2 -5
  141. data/lib/beaker/shared.rb +1 -1
  142. data/lib/beaker/ssh_connection.rb +46 -55
  143. data/lib/beaker/subcommand.rb +23 -30
  144. data/lib/beaker/subcommands/subcommand_util.rb +4 -4
  145. data/lib/beaker/tasks/quick_start.rb +5 -10
  146. data/lib/beaker/tasks/rake_task.rb +26 -28
  147. data/lib/beaker/tasks/test.rb +4 -4
  148. data/lib/beaker/test_case.rb +15 -27
  149. data/lib/beaker/test_suite.rb +35 -39
  150. data/lib/beaker/test_suite_result.rb +52 -57
  151. data/lib/beaker/version.rb +1 -1
  152. data/lib/beaker.rb +5 -6
  153. data/spec/beaker/cli_spec.rb +135 -153
  154. data/spec/beaker/command_spec.rb +64 -58
  155. data/spec/beaker/dsl/assertions_spec.rb +38 -47
  156. data/spec/beaker/dsl/helpers/host_helpers_spec.rb +114 -135
  157. data/spec/beaker/dsl/helpers/test_helpers_spec.rb +9 -10
  158. data/spec/beaker/dsl/helpers/web_helpers_spec.rb +30 -36
  159. data/spec/beaker/dsl/outcomes_spec.rb +15 -14
  160. data/spec/beaker/dsl/roles_spec.rb +170 -132
  161. data/spec/beaker/dsl/structure_spec.rb +181 -163
  162. data/spec/beaker/dsl/test_tagging_spec.rb +94 -95
  163. data/spec/beaker/dsl/wrappers_spec.rb +39 -40
  164. data/spec/beaker/host/aix_spec.rb +14 -14
  165. data/spec/beaker/host/cisco_spec.rb +92 -102
  166. data/spec/beaker/host/eos_spec.rb +15 -36
  167. data/spec/beaker/host/freebsd/exec_spec.rb +5 -8
  168. data/spec/beaker/host/freebsd/pkg_spec.rb +29 -29
  169. data/spec/beaker/host/mac/exec_spec.rb +4 -5
  170. data/spec/beaker/host/mac/group_spec.rb +47 -56
  171. data/spec/beaker/host/mac/user_spec.rb +53 -62
  172. data/spec/beaker/host/pswindows/exec_spec.rb +36 -35
  173. data/spec/beaker/host/pswindows/file_spec.rb +21 -18
  174. data/spec/beaker/host/pswindows/user_spec.rb +17 -23
  175. data/spec/beaker/host/pswindows_spec.rb +14 -14
  176. data/spec/beaker/host/unix/exec_spec.rb +87 -92
  177. data/spec/beaker/host/unix/file_spec.rb +76 -82
  178. data/spec/beaker/host/unix/pkg_spec.rb +165 -407
  179. data/spec/beaker/host/unix_spec.rb +11 -207
  180. data/spec/beaker/host/windows/exec_spec.rb +32 -34
  181. data/spec/beaker/host/windows/file_spec.rb +22 -23
  182. data/spec/beaker/host/windows/group_spec.rb +17 -19
  183. data/spec/beaker/host/windows/pkg_spec.rb +10 -13
  184. data/spec/beaker/host/windows/user_spec.rb +17 -23
  185. data/spec/beaker/host/windows_spec.rb +39 -39
  186. data/spec/beaker/host_prebuilt_steps_spec.rb +193 -341
  187. data/spec/beaker/host_spec.rb +241 -312
  188. data/spec/beaker/hypervisor/hypervisor_spec.rb +38 -63
  189. data/spec/beaker/localhost_connection_spec.rb +14 -13
  190. data/spec/beaker/logger_junit_spec.rb +22 -34
  191. data/spec/beaker/logger_spec.rb +174 -155
  192. data/spec/beaker/network_manager_spec.rb +27 -27
  193. data/spec/beaker/options/command_line_parser_spec.rb +20 -23
  194. data/spec/beaker/options/data/hosts_preserved.yml +395 -0
  195. data/spec/beaker/options/hosts_file_parser_spec.rb +36 -31
  196. data/spec/beaker/options/options_file_parser_spec.rb +4 -7
  197. data/spec/beaker/options/options_hash_spec.rb +7 -9
  198. data/spec/beaker/options/parser_spec.rb +187 -187
  199. data/spec/beaker/options/presets_spec.rb +8 -10
  200. data/spec/beaker/options/subcommand_options_parser_spec.rb +15 -15
  201. data/spec/beaker/options/validator_spec.rb +27 -28
  202. data/spec/beaker/perf_spec.rb +32 -34
  203. data/spec/beaker/platform_spec.rb +27 -37
  204. data/spec/beaker/shared/error_handler_spec.rb +8 -17
  205. data/spec/beaker/shared/fog_credentials_spec.rb +30 -30
  206. data/spec/beaker/shared/host_manager_spec.rb +55 -89
  207. data/spec/beaker/shared/options_resolver_spec.rb +9 -12
  208. data/spec/beaker/shared/repetition_spec.rb +24 -31
  209. data/spec/beaker/shared/semvar_spec.rb +21 -26
  210. data/spec/beaker/ssh_connection_spec.rb +85 -90
  211. data/spec/beaker/subcommand/subcommand_util_spec.rb +40 -37
  212. data/spec/beaker/subcommand_spec.rb +89 -89
  213. data/spec/beaker/test_case_spec.rb +33 -62
  214. data/spec/beaker/test_suite_spec.rb +153 -160
  215. data/spec/helpers.rb +48 -53
  216. data/spec/matchers.rb +9 -8
  217. data/spec/mocks.rb +6 -11
  218. data/spec/spec_helper.rb +0 -4
  219. metadata +60 -85
  220. data/acceptance/tests/base/dsl/helpers/host_helpers/deploy_package_repo_test.rb +0 -142
  221. data/acceptance/tests/base/external_resources_test.rb +0 -31
  222. data/spec/beaker/host/mac_spec.rb +0 -113
  223. data/spec/mock_fission.rb +0 -60
  224. data/spec/mock_vsphere.rb +0 -314
  225. data/spec/mock_vsphere_helper.rb +0 -183
@@ -1,36 +1,36 @@
1
- [ 'host_prebuilt_steps' ].each do |lib|
1
+ ['host_prebuilt_steps'].each do |lib|
2
2
  require "beaker/#{lib}"
3
3
  end
4
4
 
5
5
  module Beaker
6
- #The Beaker class that interacts to all the supported hypervisors
6
+ # The Beaker class that interacts to all the supported hypervisors
7
7
  class Hypervisor
8
8
  include HostPrebuiltSteps
9
9
 
10
- #Generates an array with all letters a thru z and numbers 0 thru 9
10
+ # Generates an array with all letters a thru z and numbers 0 thru 9
11
11
  CHARMAP = ('a'..'z').to_a + ('0'..'9').to_a
12
12
 
13
- #Hypervisor creator method. Creates the appropriate hypervisor class object based upon
14
- #the provided hypervisor type selected, then provisions hosts with hypervisor.
15
- #@param [String] type The type of hypervisor to create - one of aix, solaris, vsphere, fusion,
13
+ # Hypervisor creator method. Creates the appropriate hypervisor class object based upon
14
+ # the provided hypervisor type selected, then provisions hosts with hypervisor.
15
+ # @param [String] type The type of hypervisor to create - one of aix, solaris, vsphere, fusion,
16
16
  # blimpy, vcloud or vagrant
17
- #@param [Array<Host>] hosts_to_provision The hosts to be provisioned with the selected hypervisor
18
- #@param [Hash] options options Options to alter execution
19
- #@option options [String] :host_name_prefix (nil) Prefix host name if set
17
+ # @param [Array<Host>] hosts_to_provision The hosts to be provisioned with the selected hypervisor
18
+ # @param [Hash] options options Options to alter execution
19
+ # @option options [String] :host_name_prefix (nil) Prefix host name if set
20
20
  def self.create(type, hosts_to_provision, options)
21
21
  @logger = options[:logger]
22
22
  @logger.notify("Beaker::Hypervisor, found some #{type} boxes to create")
23
23
 
24
24
  hyper_class = case type
25
- when /^noop$/
26
- Beaker::Noop
27
- when /^(default)|(none)$/
28
- Beaker::Hypervisor
29
- else
30
- # Custom hypervisor
31
- require "beaker/hypervisor/#{type}"
32
- Beaker.const_get(type.split('_').collect(&:capitalize).join)
33
- end
25
+ when /^noop$/
26
+ Beaker::Noop
27
+ when /^(default)|(none)$/
28
+ Beaker::Hypervisor
29
+ else
30
+ # Custom hypervisor
31
+ require "beaker/hypervisor/#{type}"
32
+ Beaker.const_get(type.split('_').collect(&:capitalize).join)
33
+ end
34
34
 
35
35
  hypervisor = hyper_class.new(hosts_to_provision, options)
36
36
  self.set_ssh_connection_preference(hosts_to_provision, hypervisor)
@@ -44,19 +44,19 @@ module Beaker
44
44
  @options = options
45
45
  end
46
46
 
47
- #Provisioning steps for be run for a given hypervisor. Default is nil.
47
+ # Provisioning steps for be run for a given hypervisor. Default is nil.
48
48
  def provision
49
49
  nil
50
50
  end
51
51
 
52
- #Cleanup steps to be run for a given hypervisor. Default is nil.
52
+ # Cleanup steps to be run for a given hypervisor. Default is nil.
53
53
  def cleanup
54
54
  nil
55
55
  end
56
56
 
57
- DEFAULT_CONNECTION_PREFERENCE = [:ip, :vmhostname, :hostname]
57
+ DEFAULT_CONNECTION_PREFERENCE = %i[ip vmhostname hostname]
58
58
  # SSH connection method preference. Can be overwritten by hypervisor to change the order
59
- def connection_preference(host)
59
+ def connection_preference(_host)
60
60
  DEFAULT_CONNECTION_PREFERENCE
61
61
  end
62
62
 
@@ -72,65 +72,48 @@ module Beaker
72
72
  end
73
73
  end
74
74
 
75
- #Proxy package managers on tests hosts created by this hypervisor, runs before validation and configuration.
75
+ # Proxy package managers on tests hosts created by this hypervisor, runs before validation and configuration.
76
76
  def proxy_package_manager
77
- if @options[:package_proxy]
78
- package_proxy(@hosts, @options)
79
- end
77
+ return unless @options[:package_proxy]
78
+
79
+ package_proxy(@hosts, @options)
80
80
  end
81
81
 
82
- #Default configuration steps to be run for a given hypervisor. Any additional configuration to be done
83
- #to the provided SUT for test execution to be successful.
82
+ # Default configuration steps to be run for a given hypervisor. Any additional configuration to be done
83
+ # to the provided SUT for test execution to be successful.
84
84
  def configure(opts = {})
85
85
  begin
86
86
  return unless @options[:configure]
87
+
87
88
  run_in_parallel = run_in_parallel? opts, @options, 'configure'
88
- block_on @hosts, { :run_in_parallel => run_in_parallel} do |host|
89
- if host[:timesync]
90
- timesync(host, @options)
91
- end
92
- end
93
- if @options[:root_keys]
94
- sync_root_keys(@hosts, @options)
95
- end
96
- if @options[:add_el_extras]
97
- add_el_extras(@hosts, @options)
98
- end
99
- if @options[:disable_iptables]
100
- disable_iptables @hosts, @options
101
- end
102
- if @options[:set_env]
103
- set_env(@hosts, @options)
104
- end
105
- if @options[:disable_updates]
106
- disable_updates(@hosts, @options)
107
- end
108
- rescue SignalException => ex
109
- if ex.signo == 15 #SIGTERM
110
- report_and_raise(@logger, ex, "configure")
89
+ block_on @hosts, { :run_in_parallel => run_in_parallel } do |host|
90
+ timesync(host, @options) if host[:timesync]
111
91
  end
92
+ sync_root_keys(@hosts, @options) if @options[:root_keys]
93
+ set_env(@hosts, @options) if @options[:set_env]
94
+ disable_updates(@hosts, @options) if @options[:disable_updates]
95
+ rescue SignalException => e
96
+ report_and_raise(@logger, e, "configure") if e.signo == 15 # SIGTERM
112
97
  raise
113
98
  end
114
99
  end
115
100
 
116
- #Default validation steps to be run for a given hypervisor. Ensures that SUTs meet requirements to be
117
- #beaker test nodes.
101
+ # Default validation steps to be run for a given hypervisor. Ensures that SUTs meet requirements to be
102
+ # beaker test nodes.
118
103
  def validate
119
- if @options[:validate]
120
- validate_host(@hosts, @options)
121
- end
104
+ return unless @options[:validate]
105
+
106
+ validate_host(@hosts, @options)
122
107
  end
123
108
 
124
- #Generate a random string composted of letter and numbers
125
- #prefixed with value of {Beaker::Hypervisor::create} option :host_name_prefix
109
+ # Generate a random string composted of letter and numbers
110
+ # prefixed with value of {Beaker::Hypervisor::create} option :host_name_prefix
126
111
  def generate_host_name
127
- n = CHARMAP[rand(25)] + (0...14).map{CHARMAP[rand(CHARMAP.length)]}.join
128
- if @options[:host_name_prefix]
129
- return @options[:host_name_prefix] + n
130
- end
112
+ n = CHARMAP[rand(25)] + (0...14).map { CHARMAP[rand(CHARMAP.length)] }.join
113
+ return @options[:host_name_prefix] + n if @options[:host_name_prefix]
114
+
131
115
  n
132
116
  end
133
-
134
117
  end
135
118
  end
136
119
 
@@ -2,7 +2,6 @@ require 'open3'
2
2
 
3
3
  module Beaker
4
4
  class LocalConnection
5
-
6
5
  attr_accessor :logger, :hostname, :ip
7
6
 
8
7
  def initialize options = {}
@@ -19,7 +18,7 @@ module Beaker
19
18
  connection
20
19
  end
21
20
 
22
- def connect options = {}
21
+ def connect _options = {}
23
22
  @logger.debug "Local connection, no connection to start"
24
23
  end
25
24
 
@@ -35,7 +34,7 @@ module Beaker
35
34
  ENV.replace(backup)
36
35
  end
37
36
 
38
- def execute command, options = {}, stdout_callback = nil, stderr_callback = stdout_callback
37
+ def execute command, _options = {}, stdout_callback = nil, _stderr_callback = stdout_callback
39
38
  result = Result.new(@hostname, command)
40
39
  envs = {}
41
40
  if File.readable?(@ssh_env_file)
@@ -46,7 +45,7 @@ module Beaker
46
45
  end
47
46
 
48
47
  begin
49
- clean_env = ENV.reject{ |k| k =~ /^BUNDLE|^RUBY|^GEM/ }
48
+ clean_env = ENV.reject { |k| /^BUNDLE|^RUBY|^GEM/.match?(k) }
50
49
 
51
50
  with_env(clean_env) do
52
51
  std_out, std_err, status = Open3.capture3(envs, command)
@@ -68,13 +67,12 @@ module Beaker
68
67
  end
69
68
 
70
69
  def scp_to(source, target, _options = {})
71
-
72
70
  result = Result.new(@hostname, [source, target])
73
71
  begin
74
72
  FileUtils.cp_r source, target
75
73
  rescue Errno::ENOENT => e
76
74
  @logger.warn "#{e.class} error in cp'ing. Forcing the connection to close, which should " \
77
- "raise an error."
75
+ "raise an error."
78
76
  end
79
77
 
80
78
  result.stdout << " CP'ed file #{source} to #{target}"
data/lib/beaker/logger.rb CHANGED
@@ -1,14 +1,13 @@
1
1
  module Beaker
2
- # The Beaker Logger class
3
- # This class handles message reporting for Beaker, it reports based upon a provided log level
4
- # to a given destination (be it a string or file)
5
- #
2
+ # The Beaker Logger class
3
+ # This class handles message reporting for Beaker, it reports based upon a provided log level
4
+ # to a given destination (be it a string or file)
5
+ #
6
6
  class Logger
7
-
8
- #The results of the most recently run command
7
+ # The results of the most recently run command
9
8
  attr_accessor :last_result
10
9
 
11
- #Determines the spacing that happens before an output line
10
+ # Determines the spacing that happens before an output line
12
11
  attr_accessor :line_prefix
13
12
 
14
13
  NORMAL = "\e[00;00m"
@@ -21,7 +20,7 @@ module Beaker
21
20
  MAGENTA = "\e[00;35m"
22
21
  CYAN = "\e[00;36m"
23
22
  WHITE = "\e[00;37m"
24
- GREY = "\e[00;00m" # Some terms can't handle grey, use normal
23
+ GREY = "\e[00;00m" # Some terms can't handle grey, use normal
25
24
  BRIGHT_RED = "\e[01;31m"
26
25
  BRIGHT_GREEN = "\e[01;32m"
27
26
  BRIGHT_YELLOW = "\e[01;33m"
@@ -32,13 +31,13 @@ module Beaker
32
31
  NONE = ""
33
32
 
34
33
  # The defined log levels. Each log level also reports messages at levels lower than itself
35
- LOG_LEVELS = {
36
- :trace => 6,
37
- :debug => 5,
34
+ LOG_LEVELS = {
35
+ :trace => 6,
36
+ :debug => 5,
38
37
  :verbose => 3,
39
- :info => 2,
40
- :notify => 1,
41
- :warn => 0,
38
+ :info => 2,
39
+ :notify => 1,
40
+ :warn => 0,
42
41
  }
43
42
 
44
43
  attr_accessor :color, :log_level, :destinations, :log_colors
@@ -60,29 +59,27 @@ module Beaker
60
59
  options = args.last.is_a?(Hash) ? args.pop : {}
61
60
  @color = options[:color]
62
61
  @sublog = nil
63
- case options[:log_level]
64
- when /trace/i, :trace
65
- @log_level = :trace
66
- when /debug/i, :debug
67
- @log_level = :debug
68
- when /verbose/i, :verbose
69
- @log_level = :verbose
70
- when /info/i, :info
71
- @log_level = :info
72
- when /notify/i, :notify
73
- @log_level = :notify
74
- when /warn/i, :warn
75
- @log_level = :warn
76
- else
77
- @log_level = :verbose
78
- end
62
+ @log_level = case options[:log_level]
63
+ when /trace/i, :trace
64
+ :trace
65
+ when /debug/i, :debug
66
+ :debug
67
+ when /info/i, :info
68
+ :info
69
+ when /notify/i, :notify
70
+ :notify
71
+ when /warn/i, :warn
72
+ :warn
73
+ else
74
+ :verbose
75
+ end
79
76
 
80
77
  @last_result = nil
81
78
  @line_prefix = ''
82
79
 
83
80
  @destinations = []
84
81
 
85
- @log_colors = {
82
+ @log_colors = {
86
83
  :error => RED,
87
84
  :warn => BRIGHT_RED,
88
85
  :success => MAGENTA,
@@ -91,7 +88,7 @@ module Beaker
91
88
  :debug => WHITE,
92
89
  :trace => BRIGHT_YELLOW,
93
90
  :perf => BRIGHT_MAGENTA,
94
- :host => YELLOW
91
+ :host => YELLOW,
95
92
  }
96
93
 
97
94
  @log_colors.merge!(options[:log_colors]) if options[:log_colors]
@@ -114,16 +111,16 @@ module Beaker
114
111
  dests = args
115
112
  dests << STDOUT unless options[:quiet]
116
113
  dests.uniq!
117
- dests.each {|dest| add_destination(dest)}
114
+ dests.each { |dest| add_destination(dest) }
118
115
  end
119
116
 
120
117
  # Turn on/off STDOUT logging
121
118
  # @param [Boolean] off If true, disable STDOUT logging, if false enable STDOUT logging
122
119
  def quiet(off = true)
123
120
  if off
124
- remove_destination(STDOUT) #turn off the noise!
121
+ remove_destination(STDOUT) # turn off the noise!
125
122
  else
126
- remove_destination(STDOUT) #in case we are calling this in error and we are already noisy
123
+ remove_destination(STDOUT) # in case we are calling this in error and we are already noisy
127
124
  add_destination(STDOUT)
128
125
  end
129
126
  end
@@ -132,9 +129,7 @@ module Beaker
132
129
  # @param [Array<IO, String>] dest Array of strings (each used as a file path) and IO steams that messages will be printed to
133
130
  def add_destination(dest)
134
131
  case dest
135
- when IO
136
- @destinations << dest
137
- when StringIO
132
+ when IO, StringIO
138
133
  @destinations << dest
139
134
  when String
140
135
  @destinations << File.open(dest, 'w')
@@ -147,12 +142,10 @@ module Beaker
147
142
  # @param [String, IO] dest String representing a file path or IO stream
148
143
  def remove_destination(dest)
149
144
  case dest
150
- when IO
151
- @destinations.delete(dest)
152
- when StringIO
145
+ when IO, StringIO
153
146
  @destinations.delete(dest)
154
147
  when String
155
- @destinations.delete_if {|d| d.respond_to?(:path) and d.path == dest}
148
+ @destinations.delete_if { |d| d.respond_to?(:path) and d.path == dest }
156
149
  else
157
150
  raise "Unsuitable log destination #{dest.inspect}"
158
151
  end
@@ -197,14 +190,14 @@ module Beaker
197
190
  # Remove invalid UTF-8 codes from provided string(s)
198
191
  # @param [String, Array<String>] string The string(s) to remove invalid codes from
199
192
  def convert string
200
- if string.kind_of?(Array)
193
+ if string.is_a?(Array)
201
194
  string.map do |s|
202
195
  convert s
203
196
  end
204
197
  else
205
198
  # Remove invalid and undefined UTF-8 character encodings
206
199
  string = string.to_s.dup.force_encoding('UTF-8')
207
- return string.to_s.chars.select{|i| i.valid_encoding?}.join
200
+ return string.to_s.chars.select { |i| i.valid_encoding? }.join
208
201
  end
209
202
  end
210
203
 
@@ -215,12 +208,12 @@ module Beaker
215
208
  #
216
209
  # @return [String] the prefixed line
217
210
  def prefix_log_line line
218
- if line.kind_of?(Array)
211
+ if line.is_a?(Array)
219
212
  line.map do |s|
220
213
  prefix_log_line s
221
214
  end
222
215
  else
223
- line.gsub!(/\r/, '')
216
+ line.delete!("\r")
224
217
  has_ending_newline = line.end_with?("\n")
225
218
  actual_lines = line.split("\n")
226
219
  actual_lines.map! do |actual_line|
@@ -233,7 +226,7 @@ module Beaker
233
226
  end
234
227
 
235
228
  # Indent the step level for the duration of block.
236
- def with_indent(&block)
229
+ def with_indent
237
230
  old_line_prefix = self.line_prefix.dup
238
231
  self.line_prefix << ' '
239
232
  yield
@@ -241,28 +234,13 @@ module Beaker
241
234
  self.line_prefix = old_line_prefix
242
235
  end
243
236
 
244
- # Sets the step level appropriately for logging to be indented correctly
245
- #
246
- # @return nil
247
- # @deprecated use {Logger#with_indent}
248
- def step_in
249
- self.line_prefix = self.line_prefix + ' '
250
- end
251
-
252
- # Sets the step level appropriately for logging to be indented correctly
253
- #
254
- # @return nil
255
- # @deprecated use {Logger#with_indent}
256
- def step_out
257
- self.line_prefix = self.line_prefix.chop.chop
258
- end
259
-
260
237
  # Custom reporting for messages generated by host SUTs.
261
238
  # Will not print unless we are at {LOG_LEVELS} 'verbose' or higher.
262
239
  # Strips any color codes already in the provided messages, then adds logger color codes before reporting
263
240
  # @param args[Array<String>] Strings to be reported
264
241
  def host_output *args
265
242
  return unless is_verbose?
243
+
266
244
  strings = strip_colors_from args
267
245
  string = strings.join
268
246
  optionally_color @log_colors[:host], string, false
@@ -274,6 +252,7 @@ module Beaker
274
252
  # @param args[Array<String>] Strings to be reported
275
253
  def color_host_output *args
276
254
  return unless is_verbose?
255
+
277
256
  string = args.join
278
257
  optionally_color NONE, string, false
279
258
  end
@@ -283,6 +262,7 @@ module Beaker
283
262
  # @param args[Array<String>] Strings to be reported
284
263
  def perf_output *args
285
264
  return unless is_debug?
265
+
286
266
  optionally_color @log_colors[:perf], *args
287
267
  end
288
268
 
@@ -291,6 +271,7 @@ module Beaker
291
271
  # @param args[Array<String>] Strings to be reported
292
272
  def trace *args
293
273
  return unless is_trace?
274
+
294
275
  optionally_color @log_colors[:trace], *args
295
276
  end
296
277
 
@@ -299,6 +280,7 @@ module Beaker
299
280
  # @param args[Array<String>] Strings to be reported
300
281
  def debug *args
301
282
  return unless is_verbose?
283
+
302
284
  optionally_color @log_colors[:debug], *args
303
285
  end
304
286
 
@@ -308,7 +290,8 @@ module Beaker
308
290
  # @param args[Array<String>] Strings to be reported
309
291
  def warn *args
310
292
  return unless is_warn?
311
- strings = args.map {|msg| "Warning: #{msg}" }
293
+
294
+ strings = args.map { |msg| "Warning: #{msg}" }
312
295
  optionally_color @log_colors[:warn], strings
313
296
  end
314
297
 
@@ -317,6 +300,7 @@ module Beaker
317
300
  # @param args[Array<String>] Strings to be reported
318
301
  def info *args
319
302
  return unless is_info?
303
+
320
304
  optionally_color @log_colors[:info], *args
321
305
  end
322
306
 
@@ -332,6 +316,7 @@ module Beaker
332
316
  # @param args[Array<String>] Strings to be reported
333
317
  def notify *args
334
318
  return unless is_notify?
319
+
335
320
  optionally_color @log_colors[:notify], *args
336
321
  end
337
322
 
@@ -346,7 +331,7 @@ module Beaker
346
331
  # @param [String] lines A single or array of lines to removed color codes from
347
332
  # @return [Array<String>] An array of strings that do not have color codes
348
333
  def strip_colors_from lines
349
- Array( lines ).map do |line|
334
+ Array(lines).map do |line|
350
335
  Logger.strip_color_codes(convert(line))
351
336
  end
352
337
  end
@@ -362,7 +347,9 @@ module Beaker
362
347
  @destinations.each do |to|
363
348
  to.print color_code if @color
364
349
  to.send print_statement, msg
365
- to.print NORMAL if @color unless color_code == NONE
350
+ unless color_code == NONE
351
+ to.print NORMAL if @color
352
+ end
366
353
  to.flush
367
354
  end
368
355
  end
@@ -374,15 +361,13 @@ module Beaker
374
361
  # @param [String] backtrace (caller(1)) The backtrace to format
375
362
  # @return [String] The formatted backtrace
376
363
  def pretty_backtrace backtrace = caller(1)
377
- trace = is_debug? ? backtrace : purge_harness_files_from( backtrace )
378
- expand_symlinks( trace ).join "\n"
364
+ trace = is_debug? ? backtrace : purge_harness_files_from(backtrace)
365
+ expand_symlinks(trace).join "\n"
379
366
  end
380
367
 
381
368
  # Create a new StringIO log to track the current output
382
369
  def start_sublog
383
- if @sublog
384
- remove_destination(@sublog)
385
- end
370
+ remove_destination(@sublog) if @sublog
386
371
  @sublog = StringIO.new
387
372
  add_destination(@sublog)
388
373
  end
@@ -405,28 +390,29 @@ module Beaker
405
390
  # @note since this uses 'mkdir -p', log_prefix can be a number of nested directories
406
391
  #
407
392
  # @return [String] the path of the dated log folder generated
408
- def Logger.generate_dated_log_folder(base_dir, log_prefix, timestamp)
393
+ def self.generate_dated_log_folder(base_dir, log_prefix, timestamp)
409
394
  log_dir = File.join(base_dir, log_prefix, timestamp.strftime("%F_%H_%M_%S"))
410
395
  FileUtils.mkdir_p(log_dir) unless File.directory?(log_dir)
411
396
  log_dir
412
397
  end
413
398
 
414
- #Remove color codes from provided string. Color codes are of the format /(\e\[\d\d;\d\dm)+/.
415
- #@param [String] text The string to remove color codes from
416
- #@return [String] The text without color codes
417
- def Logger.strip_color_codes(text)
399
+ # Remove color codes from provided string. Color codes are of the format /(\e\[\d\d;\d\dm)+/.
400
+ # @param [String] text The string to remove color codes from
401
+ # @return [String] The text without color codes
402
+ def self.strip_color_codes(text)
418
403
  text.gsub(/(\e|\^\[)\[(\d*;)*\d*m/, '')
419
404
  end
420
405
 
421
406
  private
407
+
422
408
  # Expand each symlink found to its full path
423
409
  # Lines are assumed to be in the format "String : Integer"
424
410
  # @param [String] backtrace The string to search and expand symlinks in
425
411
  # @return [String] The backtrace with symlinks expanded
426
412
  def expand_symlinks backtrace
427
413
  backtrace.collect do |line|
428
- file_path, line_num = line.split( ":" )
429
- expanded_path = expand_symlink File.expand_path( file_path )
414
+ file_path, line_num = line.split(":")
415
+ expanded_path = expand_symlink File.expand_path(file_path)
430
416
  expanded_path.to_s + ":" + line_num.to_s
431
417
  end
432
418
  end
@@ -444,7 +430,7 @@ module Beaker
444
430
  end
445
431
 
446
432
  # And remove lines that contain our program name in them
447
- completely_purged = mostly_purged.reject {|line| line.include? $0 }
433
+ mostly_purged.reject { |line| line.include? $0 }
448
434
  end
449
435
 
450
436
  # Utility method that takes a path as input, checks each component
@@ -453,16 +439,16 @@ module Beaker
453
439
  # @param [String] file_path The path to be examined
454
440
  # @return [String] The fully expanded file_path
455
441
  def expand_symlink file_path
456
- file_path.split( "/" ).inject do |full_path, next_dir|
442
+ file_path.split("/").inject do |full_path, next_dir|
457
443
  next_path = full_path + "/" + next_dir
458
444
  if File.symlink? next_path
459
445
  link = File.readlink next_path
460
446
  next_path =
461
- case link
462
- when /^\// then link
463
- else
464
- File.expand_path( full_path + "/" + link )
465
- end
447
+ case link
448
+ when /^\// then link
449
+ else
450
+ File.expand_path(full_path + "/" + link)
451
+ end
466
452
  end
467
453
  next_path
468
454
  end