beaker 4.39.0 → 5.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (218) 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 +26 -13
  5. data/.rubocop.yml +6 -21
  6. data/.rubocop_todo.yml +34 -10
  7. data/CHANGELOG.md +22 -0
  8. data/Gemfile +12 -7
  9. data/Rakefile +99 -111
  10. data/acceptance/config/acceptance-options.rb +1 -1
  11. data/acceptance/config/base/acceptance-options.rb +2 -2
  12. data/acceptance/config/hypervisor/acceptance-options.rb +2 -2
  13. data/acceptance/config/subcommands/acceptance-options.rb +2 -2
  14. data/acceptance/fixtures/module/Rakefile +1 -1
  15. data/acceptance/fixtures/module/spec/acceptance/demo_spec.rb +8 -12
  16. data/acceptance/fixtures/module/spec/classes/init_spec.rb +0 -1
  17. data/acceptance/fixtures/module/spec/spec_helper_acceptance.rb +3 -5
  18. data/acceptance/lib/helpers/test_helper.rb +4 -3
  19. data/acceptance/pre_suite/subcommands/05_install_ruby.rb +2 -3
  20. data/acceptance/pre_suite/subcommands/08_install_beaker.rb +1 -2
  21. data/acceptance/tests/base/dsl/helpers/configuration_test.rb +4 -4
  22. data/acceptance/tests/base/dsl/helpers/hocon_helpers_test.rb +1 -2
  23. data/acceptance/tests/base/dsl/helpers/host_helpers/add_system32_hosts_entry_test.rb +0 -3
  24. data/acceptance/tests/base/dsl/helpers/host_helpers/archive_file_from_test.rb +3 -1
  25. data/acceptance/tests/base/dsl/helpers/host_helpers/backup_the_file_test.rb +9 -9
  26. data/acceptance/tests/base/dsl/helpers/host_helpers/check_for_package_test.rb +0 -4
  27. data/acceptance/tests/base/dsl/helpers/host_helpers/create_remote_file_test.rb +19 -26
  28. data/acceptance/tests/base/dsl/helpers/host_helpers/curl_on_test.rb +2 -2
  29. data/acceptance/tests/base/dsl/helpers/host_helpers/curl_with_retries_test.rb +0 -1
  30. data/acceptance/tests/base/dsl/helpers/host_helpers/on_test.rb +18 -13
  31. data/acceptance/tests/base/dsl/helpers/host_helpers/retry_on_test.rb +3 -3
  32. data/acceptance/tests/base/dsl/helpers/host_helpers/rsync_to_test.rb +22 -27
  33. data/acceptance/tests/base/dsl/helpers/host_helpers/run_cron_on_test.rb +12 -16
  34. data/acceptance/tests/base/dsl/helpers/host_helpers/run_script_on_test.rb +3 -4
  35. data/acceptance/tests/base/dsl/helpers/host_helpers/run_script_test.rb +3 -3
  36. data/acceptance/tests/base/dsl/helpers/host_helpers/scp_from_test.rb +4 -4
  37. data/acceptance/tests/base/dsl/helpers/host_helpers/scp_to_test.rb +3 -4
  38. data/acceptance/tests/base/dsl/helpers/host_helpers/shell_test.rb +5 -5
  39. data/acceptance/tests/base/dsl/helpers/host_helpers/upgrade_package_test.rb +3 -5
  40. data/acceptance/tests/base/dsl/platform_tag_confiner_test.rb +14 -17
  41. data/acceptance/tests/base/dsl/structure_test.rb +5 -11
  42. data/acceptance/tests/base/host/file_test.rb +2 -2
  43. data/acceptance/tests/base/host/group_test.rb +0 -1
  44. data/acceptance/tests/base/host/host_test.rb +66 -63
  45. data/acceptance/tests/base/host/packages.rb +1 -2
  46. data/acceptance/tests/base/host/packages_unix.rb +0 -55
  47. data/acceptance/tests/base/host/user_test.rb +0 -1
  48. data/acceptance/tests/base/host_prebuilt_steps/ssh_environment_test.rb +1 -2
  49. data/acceptance/tests/base/test_suite/export.rb +6 -9
  50. data/acceptance/tests/install/from_file.rb +2 -4
  51. data/acceptance/tests/load_path_bootstrap.rb +1 -1
  52. data/acceptance/tests/subcommands/destroy.rb +19 -21
  53. data/acceptance/tests/subcommands/exec.rb +0 -1
  54. data/acceptance/tests/subcommands/init.rb +2 -3
  55. data/acceptance/tests/subcommands/provision.rb +0 -1
  56. data/beaker.gemspec +4 -8
  57. data/docs/concepts/argument_processing_and_precedence.md +1 -10
  58. data/docs/how_to/debug_beaker_tests.md +12 -12
  59. data/docs/how_to/hosts/eos.md +2 -12
  60. data/docs/how_to/install_puppet.md +0 -18
  61. data/docs/how_to/the_beaker_dsl.md +0 -2
  62. data/lib/beaker/cli.rb +59 -68
  63. data/lib/beaker/command.rb +20 -28
  64. data/lib/beaker/command_factory.rb +3 -2
  65. data/lib/beaker/dsl/assertions.rb +6 -18
  66. data/lib/beaker/dsl/helpers/hocon_helpers.rb +3 -7
  67. data/lib/beaker/dsl/helpers/host_helpers.rb +62 -123
  68. data/lib/beaker/dsl/helpers/test_helpers.rb +3 -5
  69. data/lib/beaker/dsl/helpers/web_helpers.rb +19 -27
  70. data/lib/beaker/dsl/helpers.rb +2 -4
  71. data/lib/beaker/dsl/outcomes.rb +13 -15
  72. data/lib/beaker/dsl/patterns.rb +1 -3
  73. data/lib/beaker/dsl/roles.rb +17 -20
  74. data/lib/beaker/dsl/structure.rb +53 -65
  75. data/lib/beaker/dsl/test_tagging.rb +7 -10
  76. data/lib/beaker/dsl/wrappers.rb +15 -16
  77. data/lib/beaker/dsl.rb +2 -3
  78. data/lib/beaker/host/aix/exec.rb +1 -1
  79. data/lib/beaker/host/aix/file.rb +0 -1
  80. data/lib/beaker/host/aix/group.rb +1 -1
  81. data/lib/beaker/host/aix/user.rb +1 -1
  82. data/lib/beaker/host/aix.rb +3 -4
  83. data/lib/beaker/host/cisco.rb +27 -39
  84. data/lib/beaker/host/eos.rb +4 -30
  85. data/lib/beaker/host/freebsd/exec.rb +1 -1
  86. data/lib/beaker/host/freebsd/pkg.rb +3 -3
  87. data/lib/beaker/host/freebsd.rb +9 -12
  88. data/lib/beaker/host/mac/exec.rb +4 -4
  89. data/lib/beaker/host/mac/group.rb +7 -7
  90. data/lib/beaker/host/mac/pkg.rb +3 -106
  91. data/lib/beaker/host/mac/user.rb +2 -2
  92. data/lib/beaker/host/mac.rb +8 -9
  93. data/lib/beaker/host/pswindows/exec.rb +66 -70
  94. data/lib/beaker/host/pswindows/file.rb +3 -5
  95. data/lib/beaker/host/pswindows/group.rb +3 -3
  96. data/lib/beaker/host/pswindows/pkg.rb +12 -12
  97. data/lib/beaker/host/pswindows/user.rb +3 -3
  98. data/lib/beaker/host/pswindows.rb +4 -3
  99. data/lib/beaker/host/unix/exec.rb +80 -82
  100. data/lib/beaker/host/unix/file.rb +38 -43
  101. data/lib/beaker/host/unix/group.rb +1 -1
  102. data/lib/beaker/host/unix/pkg.rb +154 -417
  103. data/lib/beaker/host/unix/user.rb +2 -2
  104. data/lib/beaker/host/unix.rb +8 -11
  105. data/lib/beaker/host/windows/exec.rb +17 -17
  106. data/lib/beaker/host/windows/file.rb +3 -3
  107. data/lib/beaker/host/windows/group.rb +3 -3
  108. data/lib/beaker/host/windows/pkg.rb +3 -54
  109. data/lib/beaker/host/windows/user.rb +3 -3
  110. data/lib/beaker/host/windows.rb +12 -12
  111. data/lib/beaker/host.rb +76 -133
  112. data/lib/beaker/host_prebuilt_steps.rb +93 -198
  113. data/lib/beaker/hypervisor/noop.rb +2 -4
  114. data/lib/beaker/hypervisor.rb +44 -61
  115. data/lib/beaker/local_connection.rb +2 -4
  116. data/lib/beaker/logger.rb +68 -76
  117. data/lib/beaker/logger_junit.rb +21 -25
  118. data/lib/beaker/network_manager.rb +39 -42
  119. data/lib/beaker/options/command_line_parser.rb +12 -23
  120. data/lib/beaker/options/hosts_file_parser.rb +16 -24
  121. data/lib/beaker/options/options_file_parser.rb +3 -6
  122. data/lib/beaker/options/options_hash.rb +2 -7
  123. data/lib/beaker/options/parser.rb +86 -102
  124. data/lib/beaker/options/presets.rb +114 -123
  125. data/lib/beaker/options/subcommand_options_file_parser.rb +3 -6
  126. data/lib/beaker/options/validator.rb +26 -31
  127. data/lib/beaker/perf.rb +22 -27
  128. data/lib/beaker/platform.rb +38 -46
  129. data/lib/beaker/result.rb +7 -6
  130. data/lib/beaker/shared/error_handler.rb +8 -10
  131. data/lib/beaker/shared/fog_credentials.rb +5 -9
  132. data/lib/beaker/shared/host_manager.rb +36 -41
  133. data/lib/beaker/shared/options_resolver.rb +3 -7
  134. data/lib/beaker/shared/repetition.rb +2 -4
  135. data/lib/beaker/shared/semvar.rb +37 -41
  136. data/lib/beaker/shared/timed.rb +0 -3
  137. data/lib/beaker/shared.rb +1 -1
  138. data/lib/beaker/ssh_connection.rb +38 -47
  139. data/lib/beaker/subcommand.rb +17 -24
  140. data/lib/beaker/subcommands/subcommand_util.rb +4 -4
  141. data/lib/beaker/tasks/quick_start.rb +4 -9
  142. data/lib/beaker/tasks/rake_task.rb +25 -27
  143. data/lib/beaker/tasks/test.rb +4 -4
  144. data/lib/beaker/test_case.rb +15 -27
  145. data/lib/beaker/test_suite.rb +35 -39
  146. data/lib/beaker/test_suite_result.rb +45 -47
  147. data/lib/beaker/version.rb +1 -1
  148. data/lib/beaker.rb +5 -6
  149. data/rubocop.yml +39 -0
  150. data/spec/beaker/cli_spec.rb +121 -142
  151. data/spec/beaker/command_spec.rb +55 -59
  152. data/spec/beaker/dsl/assertions_spec.rb +36 -36
  153. data/spec/beaker/dsl/helpers/host_helpers_spec.rb +110 -131
  154. data/spec/beaker/dsl/helpers/test_helpers_spec.rb +9 -10
  155. data/spec/beaker/dsl/helpers/web_helpers_spec.rb +28 -34
  156. data/spec/beaker/dsl/outcomes_spec.rb +14 -14
  157. data/spec/beaker/dsl/roles_spec.rb +125 -130
  158. data/spec/beaker/dsl/structure_spec.rb +169 -158
  159. data/spec/beaker/dsl/test_tagging_spec.rb +89 -90
  160. data/spec/beaker/dsl/wrappers_spec.rb +32 -33
  161. data/spec/beaker/host/aix_spec.rb +14 -14
  162. data/spec/beaker/host/cisco_spec.rb +84 -94
  163. data/spec/beaker/host/eos_spec.rb +15 -36
  164. data/spec/beaker/host/freebsd/exec_spec.rb +3 -6
  165. data/spec/beaker/host/freebsd/pkg_spec.rb +24 -27
  166. data/spec/beaker/host/mac/exec_spec.rb +2 -3
  167. data/spec/beaker/host/mac/group_spec.rb +47 -56
  168. data/spec/beaker/host/mac/user_spec.rb +53 -62
  169. data/spec/beaker/host/pswindows/exec_spec.rb +30 -32
  170. data/spec/beaker/host/pswindows/file_spec.rb +16 -16
  171. data/spec/beaker/host/pswindows/user_spec.rb +17 -23
  172. data/spec/beaker/host/pswindows_spec.rb +13 -13
  173. data/spec/beaker/host/unix/exec_spec.rb +75 -80
  174. data/spec/beaker/host/unix/file_spec.rb +66 -73
  175. data/spec/beaker/host/unix/pkg_spec.rb +155 -401
  176. data/spec/beaker/host/unix_spec.rb +11 -207
  177. data/spec/beaker/host/windows/exec_spec.rb +30 -32
  178. data/spec/beaker/host/windows/file_spec.rb +18 -19
  179. data/spec/beaker/host/windows/group_spec.rb +10 -12
  180. data/spec/beaker/host/windows/pkg_spec.rb +6 -9
  181. data/spec/beaker/host/windows/user_spec.rb +17 -23
  182. data/spec/beaker/host/windows_spec.rb +39 -39
  183. data/spec/beaker/host_prebuilt_steps_spec.rb +172 -326
  184. data/spec/beaker/host_spec.rb +204 -284
  185. data/spec/beaker/hypervisor/hypervisor_spec.rb +36 -61
  186. data/spec/beaker/localhost_connection_spec.rb +10 -11
  187. data/spec/beaker/logger_junit_spec.rb +19 -30
  188. data/spec/beaker/logger_spec.rb +153 -136
  189. data/spec/beaker/network_manager_spec.rb +23 -23
  190. data/spec/beaker/options/command_line_parser_spec.rb +20 -23
  191. data/spec/beaker/options/hosts_file_parser_spec.rb +30 -32
  192. data/spec/beaker/options/options_file_parser_spec.rb +4 -7
  193. data/spec/beaker/options/options_hash_spec.rb +4 -6
  194. data/spec/beaker/options/parser_spec.rb +167 -167
  195. data/spec/beaker/options/presets_spec.rb +7 -9
  196. data/spec/beaker/options/subcommand_options_parser_spec.rb +13 -14
  197. data/spec/beaker/options/validator_spec.rb +10 -11
  198. data/spec/beaker/perf_spec.rb +18 -21
  199. data/spec/beaker/platform_spec.rb +25 -36
  200. data/spec/beaker/shared/error_handler_spec.rb +7 -16
  201. data/spec/beaker/shared/fog_credentials_spec.rb +29 -29
  202. data/spec/beaker/shared/host_manager_spec.rb +50 -84
  203. data/spec/beaker/shared/options_resolver_spec.rb +9 -12
  204. data/spec/beaker/shared/repetition_spec.rb +17 -24
  205. data/spec/beaker/shared/semvar_spec.rb +21 -26
  206. data/spec/beaker/ssh_connection_spec.rb +76 -83
  207. data/spec/beaker/subcommand/subcommand_util_spec.rb +31 -33
  208. data/spec/beaker/subcommand_spec.rb +75 -77
  209. data/spec/beaker/test_case_spec.rb +25 -50
  210. data/spec/beaker/test_suite_spec.rb +147 -154
  211. data/spec/helpers.rb +48 -53
  212. data/spec/matchers.rb +8 -7
  213. data/spec/mocks.rb +1 -6
  214. data/spec/spec_helper.rb +0 -1
  215. metadata +13 -37
  216. data/acceptance/tests/base/dsl/helpers/host_helpers/deploy_package_repo_test.rb +0 -142
  217. data/acceptance/tests/base/external_resources_test.rb +0 -31
  218. data/spec/beaker/host/mac_spec.rb +0 -113
@@ -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,17 +44,17 @@ 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
59
  def connection_preference(_host)
60
60
  DEFAULT_CONNECTION_PREFERENCE
@@ -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)
89
+ block_on @hosts, { :run_in_parallel => run_in_parallel } do |host|
90
+ timesync(host, @options) if host[:timesync]
107
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]
108
95
  rescue SignalException => e
109
- if e.signo == 15 #SIGTERM
110
- report_and_raise(@logger, e, "configure")
111
- end
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 = {}
@@ -46,7 +45,7 @@ module Beaker
46
45
  end
47
46
 
48
47
  begin
49
- clean_env = ENV.reject{ |k| /^BUNDLE|^RUBY|^GEM/.match?(k) }
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,27 +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 /info/i, :info
69
- @log_level = :info
70
- when /notify/i, :notify
71
- @log_level = :notify
72
- when /warn/i, :warn
73
- @log_level = :warn
74
- else
75
- @log_level = :verbose
76
- 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
77
76
 
78
77
  @last_result = nil
79
78
  @line_prefix = ''
80
79
 
81
80
  @destinations = []
82
81
 
83
- @log_colors = {
82
+ @log_colors = {
84
83
  :error => RED,
85
84
  :warn => BRIGHT_RED,
86
85
  :success => MAGENTA,
@@ -89,7 +88,7 @@ module Beaker
89
88
  :debug => WHITE,
90
89
  :trace => BRIGHT_YELLOW,
91
90
  :perf => BRIGHT_MAGENTA,
92
- :host => YELLOW
91
+ :host => YELLOW,
93
92
  }
94
93
 
95
94
  @log_colors.merge!(options[:log_colors]) if options[:log_colors]
@@ -112,16 +111,16 @@ module Beaker
112
111
  dests = args
113
112
  dests << STDOUT unless options[:quiet]
114
113
  dests.uniq!
115
- dests.each {|dest| add_destination(dest)}
114
+ dests.each { |dest| add_destination(dest) }
116
115
  end
117
116
 
118
117
  # Turn on/off STDOUT logging
119
118
  # @param [Boolean] off If true, disable STDOUT logging, if false enable STDOUT logging
120
119
  def quiet(off = true)
121
120
  if off
122
- remove_destination(STDOUT) #turn off the noise!
121
+ remove_destination(STDOUT) # turn off the noise!
123
122
  else
124
- 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
125
124
  add_destination(STDOUT)
126
125
  end
127
126
  end
@@ -146,7 +145,7 @@ module Beaker
146
145
  when IO, StringIO
147
146
  @destinations.delete(dest)
148
147
  when String
149
- @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 }
150
149
  else
151
150
  raise "Unsuitable log destination #{dest.inspect}"
152
151
  end
@@ -191,14 +190,14 @@ module Beaker
191
190
  # Remove invalid UTF-8 codes from provided string(s)
192
191
  # @param [String, Array<String>] string The string(s) to remove invalid codes from
193
192
  def convert string
194
- if string.kind_of?(Array)
193
+ if string.is_a?(Array)
195
194
  string.map do |s|
196
195
  convert s
197
196
  end
198
197
  else
199
198
  # Remove invalid and undefined UTF-8 character encodings
200
199
  string = string.to_s.dup.force_encoding('UTF-8')
201
- return string.to_s.chars.select{|i| i.valid_encoding?}.join
200
+ return string.to_s.chars.select { |i| i.valid_encoding? }.join
202
201
  end
203
202
  end
204
203
 
@@ -209,7 +208,7 @@ module Beaker
209
208
  #
210
209
  # @return [String] the prefixed line
211
210
  def prefix_log_line line
212
- if line.kind_of?(Array)
211
+ if line.is_a?(Array)
213
212
  line.map do |s|
214
213
  prefix_log_line s
215
214
  end
@@ -227,7 +226,7 @@ module Beaker
227
226
  end
228
227
 
229
228
  # Indent the step level for the duration of block.
230
- def with_indent()
229
+ def with_indent
231
230
  old_line_prefix = self.line_prefix.dup
232
231
  self.line_prefix << ' '
233
232
  yield
@@ -235,28 +234,13 @@ module Beaker
235
234
  self.line_prefix = old_line_prefix
236
235
  end
237
236
 
238
- # Sets the step level appropriately for logging to be indented correctly
239
- #
240
- # @return nil
241
- # @deprecated use {Logger#with_indent}
242
- def step_in
243
- self.line_prefix = self.line_prefix + ' '
244
- end
245
-
246
- # Sets the step level appropriately for logging to be indented correctly
247
- #
248
- # @return nil
249
- # @deprecated use {Logger#with_indent}
250
- def step_out
251
- self.line_prefix = self.line_prefix.chop.chop
252
- end
253
-
254
237
  # Custom reporting for messages generated by host SUTs.
255
238
  # Will not print unless we are at {LOG_LEVELS} 'verbose' or higher.
256
239
  # Strips any color codes already in the provided messages, then adds logger color codes before reporting
257
240
  # @param args[Array<String>] Strings to be reported
258
241
  def host_output *args
259
242
  return unless is_verbose?
243
+
260
244
  strings = strip_colors_from args
261
245
  string = strings.join
262
246
  optionally_color @log_colors[:host], string, false
@@ -268,6 +252,7 @@ module Beaker
268
252
  # @param args[Array<String>] Strings to be reported
269
253
  def color_host_output *args
270
254
  return unless is_verbose?
255
+
271
256
  string = args.join
272
257
  optionally_color NONE, string, false
273
258
  end
@@ -277,6 +262,7 @@ module Beaker
277
262
  # @param args[Array<String>] Strings to be reported
278
263
  def perf_output *args
279
264
  return unless is_debug?
265
+
280
266
  optionally_color @log_colors[:perf], *args
281
267
  end
282
268
 
@@ -285,6 +271,7 @@ module Beaker
285
271
  # @param args[Array<String>] Strings to be reported
286
272
  def trace *args
287
273
  return unless is_trace?
274
+
288
275
  optionally_color @log_colors[:trace], *args
289
276
  end
290
277
 
@@ -293,6 +280,7 @@ module Beaker
293
280
  # @param args[Array<String>] Strings to be reported
294
281
  def debug *args
295
282
  return unless is_verbose?
283
+
296
284
  optionally_color @log_colors[:debug], *args
297
285
  end
298
286
 
@@ -302,7 +290,8 @@ module Beaker
302
290
  # @param args[Array<String>] Strings to be reported
303
291
  def warn *args
304
292
  return unless is_warn?
305
- strings = args.map {|msg| "Warning: #{msg}" }
293
+
294
+ strings = args.map { |msg| "Warning: #{msg}" }
306
295
  optionally_color @log_colors[:warn], strings
307
296
  end
308
297
 
@@ -311,6 +300,7 @@ module Beaker
311
300
  # @param args[Array<String>] Strings to be reported
312
301
  def info *args
313
302
  return unless is_info?
303
+
314
304
  optionally_color @log_colors[:info], *args
315
305
  end
316
306
 
@@ -326,6 +316,7 @@ module Beaker
326
316
  # @param args[Array<String>] Strings to be reported
327
317
  def notify *args
328
318
  return unless is_notify?
319
+
329
320
  optionally_color @log_colors[:notify], *args
330
321
  end
331
322
 
@@ -340,7 +331,7 @@ module Beaker
340
331
  # @param [String] lines A single or array of lines to removed color codes from
341
332
  # @return [Array<String>] An array of strings that do not have color codes
342
333
  def strip_colors_from lines
343
- Array( lines ).map do |line|
334
+ Array(lines).map do |line|
344
335
  Logger.strip_color_codes(convert(line))
345
336
  end
346
337
  end
@@ -356,7 +347,9 @@ module Beaker
356
347
  @destinations.each do |to|
357
348
  to.print color_code if @color
358
349
  to.send print_statement, msg
359
- to.print NORMAL if @color unless color_code == NONE
350
+ unless color_code == NONE
351
+ to.print NORMAL if @color
352
+ end
360
353
  to.flush
361
354
  end
362
355
  end
@@ -368,15 +361,13 @@ module Beaker
368
361
  # @param [String] backtrace (caller(1)) The backtrace to format
369
362
  # @return [String] The formatted backtrace
370
363
  def pretty_backtrace backtrace = caller(1)
371
- trace = is_debug? ? backtrace : purge_harness_files_from( backtrace )
372
- expand_symlinks( trace ).join "\n"
364
+ trace = is_debug? ? backtrace : purge_harness_files_from(backtrace)
365
+ expand_symlinks(trace).join "\n"
373
366
  end
374
367
 
375
368
  # Create a new StringIO log to track the current output
376
369
  def start_sublog
377
- if @sublog
378
- remove_destination(@sublog)
379
- end
370
+ remove_destination(@sublog) if @sublog
380
371
  @sublog = StringIO.new
381
372
  add_destination(@sublog)
382
373
  end
@@ -399,28 +390,29 @@ module Beaker
399
390
  # @note since this uses 'mkdir -p', log_prefix can be a number of nested directories
400
391
  #
401
392
  # @return [String] the path of the dated log folder generated
402
- def Logger.generate_dated_log_folder(base_dir, log_prefix, timestamp)
393
+ def self.generate_dated_log_folder(base_dir, log_prefix, timestamp)
403
394
  log_dir = File.join(base_dir, log_prefix, timestamp.strftime("%F_%H_%M_%S"))
404
395
  FileUtils.mkdir_p(log_dir) unless File.directory?(log_dir)
405
396
  log_dir
406
397
  end
407
398
 
408
- #Remove color codes from provided string. Color codes are of the format /(\e\[\d\d;\d\dm)+/.
409
- #@param [String] text The string to remove color codes from
410
- #@return [String] The text without color codes
411
- 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)
412
403
  text.gsub(/(\e|\^\[)\[(\d*;)*\d*m/, '')
413
404
  end
414
405
 
415
406
  private
407
+
416
408
  # Expand each symlink found to its full path
417
409
  # Lines are assumed to be in the format "String : Integer"
418
410
  # @param [String] backtrace The string to search and expand symlinks in
419
411
  # @return [String] The backtrace with symlinks expanded
420
412
  def expand_symlinks backtrace
421
413
  backtrace.collect do |line|
422
- file_path, line_num = line.split( ":" )
423
- 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)
424
416
  expanded_path.to_s + ":" + line_num.to_s
425
417
  end
426
418
  end
@@ -438,7 +430,7 @@ module Beaker
438
430
  end
439
431
 
440
432
  # And remove lines that contain our program name in them
441
- mostly_purged.reject {|line| line.include? $0 }
433
+ mostly_purged.reject { |line| line.include? $0 }
442
434
  end
443
435
 
444
436
  # Utility method that takes a path as input, checks each component
@@ -447,16 +439,16 @@ module Beaker
447
439
  # @param [String] file_path The path to be examined
448
440
  # @return [String] The fully expanded file_path
449
441
  def expand_symlink file_path
450
- file_path.split( "/" ).inject do |full_path, next_dir|
442
+ file_path.split("/").inject do |full_path, next_dir|
451
443
  next_path = full_path + "/" + next_dir
452
444
  if File.symlink? next_path
453
445
  link = File.readlink next_path
454
446
  next_path =
455
- case link
456
- when /^\// then link
457
- else
458
- File.expand_path( full_path + "/" + link )
459
- end
447
+ case link
448
+ when /^\// then link
449
+ else
450
+ File.expand_path(full_path + "/" + link)
451
+ end
460
452
  end
461
453
  next_path
462
454
  end
@@ -7,7 +7,6 @@ module Beaker
7
7
  # Here's a list of example usages:
8
8
  # - {Beaker::TestSuiteResult#write_junit_xml}
9
9
  module LoggerJunit
10
-
11
10
  # writes the xml created in the block to the xml file given
12
11
  #
13
12
  # Note: Error Recovery should take place in the caller of this
@@ -68,9 +67,9 @@ module Beaker
68
67
  #
69
68
  # @return nil
70
69
  def self.copy_stylesheet_into_xml_dir(stylesheet, xml_file)
71
- if not File.file?(File.join(File.dirname(xml_file), File.basename(stylesheet)))
72
- FileUtils.copy(stylesheet, File.join(File.dirname(xml_file), File.basename(stylesheet)))
73
- end
70
+ return if File.file?(File.join(File.dirname(xml_file), File.basename(stylesheet)))
71
+
72
+ FileUtils.copy(stylesheet, File.join(File.dirname(xml_file), File.basename(stylesheet)))
74
73
  end
75
74
 
76
75
  # sets up doc & gives us the suites for the testsuite named
@@ -81,14 +80,12 @@ module Beaker
81
80
  #
82
81
  # @return [Rexml::Element] testsuites
83
82
  def self.get_testsuites_from_doc(doc, name, already_existed)
84
- #check to see if an output file already exists, if it does add or replace test suite data
83
+ # check to see if an output file already exists, if it does add or replace test suite data
85
84
  if already_existed
86
85
  suites = REXML::XPath.first(doc, "testsuites")
87
- #remove old data
86
+ # remove old data
88
87
  suites.elements.each("testsuite") do |e|
89
- if /#{name}/.match?(e.name)
90
- suites.delete_element e
91
- end
88
+ suites.delete_element e if /#{name}/.match?(e.name)
92
89
  end
93
90
  else
94
91
  suites = doc.add_element(REXML::Element.new('testsuites'))
@@ -105,13 +102,13 @@ module Beaker
105
102
  # @return [REXML::Document] Doc that you want to write in
106
103
  def self.get_doc_for_filename(filename, stylesheet, already_exists)
107
104
  if already_exists
108
- doc = REXML::Document.new File.open(filename)
105
+ doc = REXML::Document.new File.open(filename)
109
106
  else
110
- #no existing file, create a new one
111
- doc = REXML::Document.new
112
- doc << REXML::XMLDecl.new(version="1.0", encoding="UTF-8")
107
+ # no existing file, create a new one
108
+ doc = REXML::Document.new
109
+ doc << REXML::XMLDecl.new(version = "1.0", encoding = "UTF-8")
113
110
  instruction_content = "type='text/xsl' href='#{File.basename(stylesheet)}'"
114
- doc << REXML::Instruction.new(target="xml-stylesheet", content=instruction_content)
111
+ doc << REXML::Instruction.new(target = "xml-stylesheet", content = instruction_content)
115
112
  end
116
113
  return doc
117
114
  end
@@ -131,11 +128,11 @@ module Beaker
131
128
  escaped_string = ""
132
129
  string.chars.each do |i|
133
130
  char_as_codestring = i.unpack("U*").join
134
- if self.is_valid_xml(char_as_codestring.to_i)
135
- escaped_string << i
136
- else
137
- escaped_string << "\\#{char_as_codestring}"
138
- end
131
+ escaped_string << if self.is_valid_xml(char_as_codestring.to_i)
132
+ i
133
+ else
134
+ "\\#{char_as_codestring}"
135
+ end
139
136
  end
140
137
  escaped_string
141
138
  end
@@ -145,13 +142,12 @@ module Beaker
145
142
  # @param [Integer] int The number to check against
146
143
  # @return [Boolean] True, if the number corresponds to a valid xml unicode character, otherwise false
147
144
  def self.is_valid_xml(int)
148
- return ( int == 0x9 or
145
+ return (int == 0x9 or
149
146
  int == 0xA or
150
- ( int >= 0x0020 and int <= 0xD7FF ) or
151
- ( int >= 0xE000 and int <= 0xFFFD ) or
152
- ( int >= 0x100000 and int <= 0x10FFFF )
153
- )
147
+ (int >= 0x0020 and int <= 0xD7FF) or
148
+ (int >= 0xE000 and int <= 0xFFFD) or
149
+ (int >= 0x100000 and int <= 0x10FFFF)
150
+ )
154
151
  end
155
-
156
152
  end
157
153
  end