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
@@ -6,40 +6,36 @@ module Beaker
6
6
  PLATFORMS = /^(alpine|huaweios|cisco_nexus|cisco_ios_xr|(free|open)bsd|osx|centos|fedora|debian|oracle|redhat|redhatfips|scientific|opensuse|sles|ubuntu|windows|solaris|aix|archlinux|el|eos|cumulus|f5|netscaler)\-.+\-.+$/
7
7
  # Platform version numbers vs. codenames conversion hash
8
8
  PLATFORM_VERSION_CODES =
9
- { :debian => { "bullseye" => "11",
10
- "buster" => "10",
11
- "stretch" => "9",
12
- "jessie" => "8",
13
- "wheezy" => "7",
14
- "squeeze" => "6",
15
- },
16
- :ubuntu => { "jammy" => "2204",
17
- "focal" => "2004",
18
- "eoan" => "1910",
19
- "disco" => "1904",
20
- "cosmic" => "1810",
21
- "bionic" => "1804",
22
- "artful" => "1710",
23
- "zesty" => "1704",
9
+ { :debian => { "bullseye" => "11",
10
+ "buster" => "10",
11
+ "stretch" => "9",
12
+ "jessie" => "8",
13
+ "wheezy" => "7",
14
+ "squeeze" => "6", },
15
+ :ubuntu => { "jammy" => "2204",
16
+ "focal" => "2004",
17
+ "eoan" => "1910",
18
+ "disco" => "1904",
19
+ "cosmic" => "1810",
20
+ "bionic" => "1804",
21
+ "artful" => "1710",
22
+ "zesty" => "1704",
24
23
  "yakkety" => "1610",
25
- "xenial" => "1604",
26
- "wily" => "1510",
27
- "vivid" => "1504",
28
- "utopic" => "1410",
29
- "trusty" => "1404",
30
- "saucy" => "1310",
31
- "raring" => "1304",
24
+ "xenial" => "1604",
25
+ "wily" => "1510",
26
+ "vivid" => "1504",
27
+ "utopic" => "1410",
28
+ "trusty" => "1404",
29
+ "saucy" => "1310",
30
+ "raring" => "1304",
32
31
  "quantal" => "1210",
33
32
  "precise" => "1204",
34
- "lucid" => "1004",
35
- },
36
- :osx => { "highsierra" => "1013",
37
- "sierra" => "1012",
38
- "elcapitan" => "1011",
39
- "yosemite" => "1010",
40
- "mavericks" => "109",
41
- }
42
- }
33
+ "lucid" => "1004", },
34
+ :osx => { "highsierra" => "1013",
35
+ "sierra" => "1012",
36
+ "elcapitan" => "1011",
37
+ "yosemite" => "1010",
38
+ "mavericks" => "109", }, }
43
39
 
44
40
  # A string with the name of the platform.
45
41
  attr_reader :variant
@@ -82,9 +78,7 @@ module Beaker
82
78
  # * netscaler
83
79
  # * archlinux
84
80
  def initialize(name)
85
- if name !~ PLATFORMS
86
- raise ArgumentError, "Unsupported platform name #{name}"
87
- end
81
+ raise ArgumentError, "Unsupported platform name #{name}" if !PLATFORMS.match?(name)
88
82
 
89
83
  super
90
84
 
@@ -94,15 +88,15 @@ module Beaker
94
88
  @version = version
95
89
  @codename = nil
96
90
 
97
- if codename_version_hash
98
- if codename_version_hash[version]
99
- @codename = version
100
- @version = codename_version_hash[version]
101
- else
102
- version = version.delete('.')
103
- version_codename_hash = codename_version_hash.invert
104
- @codename = version_codename_hash[version]
105
- end
91
+ return unless codename_version_hash
92
+
93
+ if codename_version_hash[version]
94
+ @codename = version
95
+ @version = codename_version_hash[version]
96
+ else
97
+ version = version.delete('.')
98
+ version_codename_hash = codename_version_hash.invert
99
+ @codename = version_codename_hash[version]
106
100
  end
107
101
  end
108
102
 
@@ -118,9 +112,7 @@ module Beaker
118
112
  # @return [String] the platform string with the platform version represented as a codename
119
113
  def with_version_codename
120
114
  version_array = [@variant, @version, @arch]
121
- if @codename
122
- version_array = [@variant, @codename, @arch]
123
- end
115
+ version_array = [@variant, @codename, @arch] if @codename
124
116
  return version_array.join('-')
125
117
  end
126
118
 
data/lib/beaker/result.rb CHANGED
@@ -2,6 +2,7 @@ module Beaker
2
2
  class Result
3
3
  attr_accessor :host, :cmd, :exit_code, :stdout, :stderr, :output,
4
4
  :raw_stdout, :raw_stderr, :raw_output
5
+
5
6
  def initialize(host, cmd)
6
7
  @host = host
7
8
  @cmd = cmd
@@ -19,11 +20,11 @@ module Beaker
19
20
  # This is also the lowest overhead place to normalize line endings, IIRC
20
21
  def finalize!
21
22
  @raw_stdout = @stdout
22
- @stdout = normalize_line_endings( convert( @stdout ) )
23
+ @stdout = normalize_line_endings(convert(@stdout))
23
24
  @raw_stderr = @stderr
24
- @stderr = normalize_line_endings( convert( @stderr ) )
25
+ @stderr = normalize_line_endings(convert(@stderr))
25
26
  @raw_output = @output
26
- @output = normalize_line_endings( convert( @output ) )
27
+ @output = normalize_line_endings(convert(@output))
27
28
  end
28
29
 
29
30
  def normalize_line_endings string
@@ -33,15 +34,15 @@ module Beaker
33
34
  def convert string
34
35
  # Remove invalid and undefined UTF-8 character encodings
35
36
  string.to_s.force_encoding('UTF-8')
36
- string.to_s.chars.select{|i| i.valid_encoding?}.join
37
+ string.to_s.chars.select { |i| i.valid_encoding? }.join
37
38
  end
38
39
 
39
40
  def log(logger)
40
41
  logger.debug "Exited: #{exit_code}" unless exit_code == 0 or !exit_code
41
42
  end
42
43
 
43
- def formatted_output(limit=10)
44
- @output.split("\n").last(limit).collect {|x| "\t" + x}.join("\n")
44
+ def formatted_output(limit = 10)
45
+ @output.split("\n").last(limit).collect { |x| "\t" + x }.join("\n")
45
46
  end
46
47
 
47
48
  def exit_code_in?(range)
@@ -1,17 +1,15 @@
1
1
  module Beaker
2
2
  module Shared
3
3
  module ErrorHandler
4
-
5
- def report_and_raise(logger, e, msg)
6
- logger.error "Failed: errored in #{msg}"
7
- logger.error(e.inspect)
8
- bt = e.backtrace
9
- logger.pretty_backtrace(bt).each_line do |line|
10
- logger.error(line)
4
+ def report_and_raise(logger, e, msg)
5
+ logger.error "Failed: errored in #{msg}"
6
+ logger.error(e.inspect)
7
+ bt = e.backtrace
8
+ logger.pretty_backtrace(bt).each_line do |line|
9
+ logger.error(line)
10
+ end
11
+ raise e
11
12
  end
12
- raise e
13
- end
14
-
15
13
  end
16
14
  end
17
15
  end
@@ -41,19 +41,15 @@ module Beaker
41
41
  rescue Psych::SyntaxError, Errno::ENOENT => e
42
42
  raise fog_credential_error fog_file_path, from_env, "(#{e.class}) #{e.message}"
43
43
  end
44
- if fog == false # YAML.load => false for empty file
45
- raise fog_credential_error fog_file_path, from_env, "is empty."
46
- end
44
+ raise fog_credential_error fog_file_path, from_env, "is empty." if fog == false # YAML.load => false for empty file
45
+
47
46
  # transparently support symbols or strings for keys
48
47
  fog = StringifyHash.new.merge!(fog)
49
48
  # respect credential from env
50
49
  # @note ENV must be a string, e.g. "default" not ":default"
51
- if ENV["FOG_CREDENTIAL"]
52
- credential_group = ENV["FOG_CREDENTIAL"].to_sym
53
- end
54
- if not fog[credential_group]
55
- raise fog_credential_error fog_file_path, from_env, "could not load the specified credential group '#{credential_group}'."
56
- end
50
+ credential_group = ENV["FOG_CREDENTIAL"].to_sym if ENV["FOG_CREDENTIAL"]
51
+ raise fog_credential_error fog_file_path, from_env, "could not load the specified credential group '#{credential_group}'." if not fog[credential_group]
52
+
57
53
  fog[credential_group]
58
54
  end
59
55
  end
@@ -1,50 +1,50 @@
1
1
  module Beaker
2
2
  module Shared
3
- #Methods for managing Hosts.
3
+ # Methods for managing Hosts.
4
4
  #- selecting hosts by role (Symbol or String)
5
5
  #- selecting hosts by name (String)
6
6
  #- adding additional method definitions for selecting by role
7
7
  #- executing blocks of code against selected sets of hosts
8
8
  module HostManager
9
-
10
- #Find hosts from a given array of hosts that all have the desired role.
11
- #@param [Array<Host>] hosts The hosts to examine
12
- #@param [String] desired_role The hosts returned will have this role in their roles list
13
- #@return [Array<Host>] The hosts that have the desired role in their roles list
9
+ # Find hosts from a given array of hosts that all have the desired role.
10
+ # @param [Array<Host>] hosts The hosts to examine
11
+ # @param [String] desired_role The hosts returned will have this role in their roles list
12
+ # @return [Array<Host>] The hosts that have the desired role in their roles list
14
13
  def hosts_with_role(hosts, desired_role = nil)
15
14
  hosts.select do |host|
16
15
  desired_role.nil? or host['roles'].include?(desired_role.to_s)
17
16
  end
18
17
  end
19
18
 
20
- #Find hosts from a given array of hosts that all have the desired name, match against host name,
21
- #vmhostname and ip (the three valid ways to identify an individual host)
22
- #@param [Array<Host>] hosts The hosts to examine
23
- #@param [String] name The hosts returned will have this name/vmhostname/ip
24
- #@return [Array<Host>] The hosts that have the desired name/vmhostname/ip
19
+ # Find hosts from a given array of hosts that all have the desired name, match against host name,
20
+ # vmhostname and ip (the three valid ways to identify an individual host)
21
+ # @param [Array<Host>] hosts The hosts to examine
22
+ # @param [String] name The hosts returned will have this name/vmhostname/ip
23
+ # @return [Array<Host>] The hosts that have the desired name/vmhostname/ip
25
24
  def hosts_with_name(hosts, name = nil)
26
25
  hosts.select do |host|
27
- name.nil? or host.name =~ /\A#{name}/ or host[:vmhostname] =~ /\A#{name}/ or host[:ip] =~ /\A#{name}/
26
+ name.nil? or host.name&.start_with?(name) or host[:vmhostname]&.start_with?(name) or host[:ip]&.start_with?(name)
28
27
  end
29
28
  end
30
29
 
31
- #Find a single host with the role provided. Raise an error if more than one host is found to have the
32
- #provided role.
33
- #@param [Array<Host>] hosts The hosts to examine
34
- #@param [String] role The host returned will have this role in its role list
35
- #@return [Host] The single host with the desired role in its roles list
36
- #@raise [ArgumentError] Raised if more than one host has the given role defined, if no host has the
30
+ # Find a single host with the role provided. Raise an error if more than one host is found to have the
31
+ # provided role.
32
+ # @param [Array<Host>] hosts The hosts to examine
33
+ # @param [String] role The host returned will have this role in its role list
34
+ # @return [Host] The single host with the desired role in its roles list
35
+ # @raise [ArgumentError] Raised if more than one host has the given role defined, if no host has the
37
36
  # role defined, or if role = nil since hosts_with_role(nil) returns all hosts.
38
37
  def only_host_with_role(hosts, role)
39
38
  raise ArgumentError, "role cannot be nil." if role.nil?
39
+
40
40
  a_host = hosts_with_role(hosts, role)
41
- case
42
- when a_host.length == 0
43
- raise ArgumentError, "There should be one host with #{role} defined!"
44
- when a_host.length > 1
45
- host_string = ( a_host.map { |host| host.name } ).join( ', ')
46
- raise ArgumentError, "There should be only one host with #{role} defined, but I found #{a_host.length} (#{host_string})"
41
+ if a_host.length == 0
42
+ raise ArgumentError, "There should be one host with #{role} defined!"
43
+ elsif a_host.length > 1
44
+ host_string = (a_host.map { |host| host.name }).join(', ')
45
+ raise ArgumentError, "There should be only one host with #{role} defined, but I found #{a_host.length} (#{host_string})"
47
46
  end
47
+
48
48
  a_host.first
49
49
  end
50
50
 
@@ -58,17 +58,17 @@ module Beaker
58
58
  # or if role = nil since hosts_with_role(nil) returns all hosts.
59
59
  def find_at_most_one_host_with_role(hosts, role)
60
60
  raise ArgumentError, "role cannot be nil." if role.nil?
61
+
61
62
  role_hosts = hosts_with_role(hosts, role)
62
- host_with_role = nil
63
63
  case role_hosts.length
64
64
  when 0
65
+ nil
65
66
  when 1
66
- host_with_role = role_hosts[0]
67
+ role_hosts[0]
67
68
  else
68
- host_string = ( role_hosts.map { |host| host.name } ).join( ', ')
69
+ host_string = (role_hosts.map { |host| host.name }).join(', ')
69
70
  raise ArgumentError, "There should be only one host with #{role} defined, but I found #{role_hosts.length} (#{host_string})"
70
71
  end
71
- host_with_role
72
72
  end
73
73
 
74
74
  # Execute a block selecting the hosts that match with the provided criteria
@@ -89,28 +89,25 @@ module Beaker
89
89
  # and thus a result object is returned.
90
90
  def run_block_on hosts = [], filter = nil, opts = {}, &block
91
91
  result = nil
92
- block_hosts = hosts #the hosts to apply the block to after any filtering
92
+ block_hosts = hosts # the hosts to apply the block to after any filtering
93
93
  if filter
94
- if not hosts.empty?
95
- block_hosts = hosts_with_role(hosts, filter) #check by role
96
- if block_hosts.empty?
97
- block_hosts = hosts_with_name(hosts, filter) #check by name
98
- end
99
- if block_hosts.length == 1 #we only found one matching host, don't need it wrapped in an array
100
- block_hosts = block_hosts.pop
101
- end
102
- else
103
- raise ArgumentError, "Unable to sort for #{filter} type hosts when provided with [] as Hosts"
94
+ raise ArgumentError, "Unable to sort for #{filter} type hosts when provided with [] as Hosts" if hosts.empty?
95
+
96
+ block_hosts = hosts_with_role(hosts, filter) # check by role
97
+ if block_hosts.empty?
98
+ block_hosts = hosts_with_name(hosts, filter) # check by name
104
99
  end
100
+ block_hosts = block_hosts.pop if block_hosts.length == 1 # we only found one matching host, don't need it wrapped in an array
101
+
105
102
  end
106
103
  if block_hosts.is_a? Array
107
104
  if block_hosts.length > 0
108
105
  if run_in_parallel? opts
109
106
  # Pass caller[1] - the line that called block_on - for logging purposes.
110
- result = block_hosts.map.each_in_parallel(caller[1]) do |h|
107
+ result = block_hosts.map.each_in_parallel(caller(2..2).first) do |h|
111
108
  run_block_on h, &block
112
109
  end
113
- hosts.each{|host| host.close}# For some reason, I have to close the SSH connection
110
+ hosts.each { |host| host.close } # For some reason, I have to close the SSH connection
114
111
  # after spawning a process and running commands on a host,
115
112
  # or else it gets into a broken state for the next call.
116
113
  else
@@ -118,20 +115,17 @@ module Beaker
118
115
  run_block_on h, &block
119
116
  end
120
117
  end
121
- else
118
+ elsif (cur_logger = (logger || @logger))
122
119
  # there are no matching hosts to execute against
123
120
  # should warn here
124
121
  # check if logger is defined in this context
125
- if ( cur_logger = (logger || @logger ) )
126
- cur_logger.info "Attempting to execute against an empty array of hosts (#{hosts}, filtered to #{block_hosts}), no execution will occur"
127
- end
122
+ cur_logger.info "Attempting to execute against an empty array of hosts (#{hosts}, filtered to #{block_hosts}), no execution will occur"
128
123
  end
129
124
  else
130
125
  result = yield block_hosts
131
126
  end
132
127
  result
133
128
  end
134
-
135
129
  end
136
130
  end
137
131
  end
@@ -23,16 +23,12 @@ module Beaker
23
23
  #
24
24
  # run_in_parallel?({:run_in_parallel => false}, {:run_in_parallel => ['install','configure']}, 'install')
25
25
  # -> will return false
26
- def run_in_parallel?(local_options=nil, global_options=nil, mode=nil)
26
+ def run_in_parallel?(local_options = nil, global_options = nil, mode = nil)
27
27
  run_in_parallel = local_options[:run_in_parallel] unless local_options.nil?
28
28
 
29
- if !run_in_parallel.nil? && run_in_parallel.is_a?(Array)
30
- run_in_parallel = false
31
- end
29
+ run_in_parallel = false if !run_in_parallel.nil? && run_in_parallel.is_a?(Array)
32
30
 
33
- if run_in_parallel.nil? && global_options && global_options[:run_in_parallel].is_a?(Array)
34
- run_in_parallel = global_options[:run_in_parallel].include?(mode)
35
- end
31
+ run_in_parallel = global_options[:run_in_parallel].include?(mode) if run_in_parallel.nil? && global_options && global_options[:run_in_parallel].is_a?(Array)
36
32
 
37
33
  run_in_parallel
38
34
  end
@@ -1,28 +1,27 @@
1
1
  module Beaker
2
2
  module Shared
3
3
  module Repetition
4
-
5
4
  def repeat_for seconds, &block
6
5
  # do not peg CPU if &block takes less than 1 second
7
6
  repeat_for_and_wait seconds, 1, &block
8
7
  end
9
8
 
10
- def repeat_for_and_wait seconds, wait, &block
9
+ def repeat_for_and_wait seconds, wait
11
10
  timeout = Time.now + seconds
12
11
  done = false
13
- until done or timeout < Time.now do
14
- done = block.call
12
+ until done or timeout < Time.now
13
+ done = yield
15
14
  sleep wait unless done
16
15
  end
17
16
  return done
18
17
  end
19
18
 
20
- def repeat_fibonacci_style_for attempts, &block
19
+ def repeat_fibonacci_style_for attempts
21
20
  done = false
22
21
  attempt = 1
23
22
  last_wait, wait = 0, 1
24
- while not done and attempt <= attempts do
25
- done = block.call
23
+ while not done and attempt <= attempts
24
+ done = yield
26
25
  attempt += 1
27
26
  sleep wait unless done
28
27
  last_wait, wait = wait, last_wait + wait
@@ -32,4 +31,3 @@ module Beaker
32
31
  end
33
32
  end
34
33
  end
35
-
@@ -1,38 +1,34 @@
1
1
  module Beaker
2
2
  module Shared
3
3
  module Semvar
4
-
5
- #Is semver-ish version a less than semver-ish version b
6
- #@param [String] a A version of the from '\d.\d.\d.*'
7
- #@param [String] b A version of the form '\d.\d.\d.*'
8
- #@return [Boolean] true if a is less than b, otherwise return false
4
+ # Is semver-ish version a less than semver-ish version b
5
+ # @param [String] a A version of the from '\d.\d.\d.*'
6
+ # @param [String] b A version of the form '\d.\d.\d.*'
7
+ # @return [Boolean] true if a is less than b, otherwise return false
9
8
  #
10
- #@note This has been updated for our current versioning scheme.
11
- #@note 2019.5.0 is greater than 2019.5.0-rc0
12
- #@note 2019.5.0-rc0-1-gabc1234 is greater than 2019.5.0-rc0
13
- #@note 2019.5.0-rc1 is greater than 2019.5.0-rc0-1-gabc1234
14
- #@note 2019.5.0-1-gabc1234 is greater than 2019.5.0
9
+ # @note This has been updated for our current versioning scheme.
10
+ # @note 2019.5.0 is greater than 2019.5.0-rc0
11
+ # @note 2019.5.0-rc0-1-gabc1234 is greater than 2019.5.0-rc0
12
+ # @note 2019.5.0-rc1 is greater than 2019.5.0-rc0-1-gabc1234
13
+ # @note 2019.5.0-1-gabc1234 is greater than 2019.5.0
15
14
  def version_is_less a, b
16
15
  a_nums = a.split('-')[0].split('.')
17
16
  b_nums = b.split('-')[0].split('.')
18
17
  (0...a_nums.length).each do |i|
19
- if i < b_nums.length
20
- if a_nums[i].to_i < b_nums[i].to_i
21
- return true
22
- elsif a_nums[i].to_i > b_nums[i].to_i
23
- return false
24
- end
25
- else
18
+ return false unless i < b_nums.length
19
+ if a_nums[i].to_i < b_nums[i].to_i
20
+ return true
21
+ elsif a_nums[i].to_i > b_nums[i].to_i
26
22
  return false
27
23
  end
28
24
  end
29
- #checks all dots, they are equal so examine the rest
25
+ # checks all dots, they are equal so examine the rest
30
26
  a_rest = a.split('-').drop(1)
31
27
  a_is_release = a_rest.empty?
32
- a_is_rc = !a_is_release && !!(a_rest[0] =~ /rc\d+/)
28
+ a_is_rc = !a_is_release && /rc\d+/.match?(a_rest[0])
33
29
  b_rest = b.split('-').drop(1)
34
30
  b_is_release = b_rest.empty?
35
- b_is_rc = !b_is_release && !!(b_rest[0] =~ /rc\d+/)
31
+ b_is_rc = !b_is_release && /rc\d+/.match?(b_rest[0])
36
32
 
37
33
  if a_is_release && b_is_release
38
34
  # They are equal
@@ -40,29 +36,28 @@ module Beaker
40
36
  elsif !a_is_release && !b_is_release
41
37
  a_next = a_rest.shift
42
38
  b_next = b_rest.shift
43
- if a_is_rc && b_is_rc
44
- a_rc = a_next.gsub('rc','').to_i
45
- b_rc = b_next.gsub('rc','').to_i
46
- if a_rc < b_rc
47
- return true
48
- elsif a_rc > b_rc
49
- return false
50
- else
51
- a_next = a_rest.shift
52
- b_next = b_rest.shift
53
- if a_next && b_next
54
- return a_next.to_i < b_next.to_i
55
- else
56
- # If a has nothing after -rc#, it is a tagged RC and
57
- # b must be a later build after this tag.
58
- return a_next.nil?
59
- end
60
- end
39
+ return a_is_rc unless a_is_rc && b_is_rc
40
+
41
+ a_rc = a_next.gsub('rc', '').to_i
42
+ b_rc = b_next.gsub('rc', '').to_i
43
+ if a_rc < b_rc
44
+ return true
45
+ elsif a_rc > b_rc
46
+ return false
61
47
  else
62
- # If one of them is not an rc (and also not a release),
63
- # that one is a post-release build. So if a is the RC, it is less.
64
- return a_is_rc
48
+ a_next = a_rest.shift
49
+ b_next = b_rest.shift
50
+ return a_next.to_i < b_next.to_i if a_next && b_next
51
+
52
+ # If a has nothing after -rc#, it is a tagged RC and
53
+ # b must be a later build after this tag.
54
+ return a_next.nil?
55
+
65
56
  end
57
+
58
+ # If one of them is not an rc (and also not a release),
59
+ # that one is a post-release build. So if a is the RC, it is less.
60
+
66
61
  else
67
62
  return (b_is_release && a_is_rc) || (a_is_release && !b_is_rc)
68
63
  end
@@ -78,12 +73,14 @@ module Beaker
78
73
  #
79
74
  # @return [String, nil] the max string out of the versions list or the
80
75
  # default value if the list is faulty, which can either be set or nil
81
- def max_version(versions, default=nil)
76
+ def max_version(versions, default = nil)
82
77
  return default if !versions || versions.empty?
78
+
83
79
  versions_copy = versions.dup
84
80
  highest = versions_copy.shift
85
81
  versions_copy.each do |version|
86
82
  next if !version
83
+
87
84
  highest = version if version_is_less(highest, version)
88
85
  end
89
86
  highest
@@ -91,4 +88,3 @@ module Beaker
91
88
  end
92
89
  end
93
90
  end
94
-
@@ -1,14 +1,11 @@
1
1
  module Beaker
2
2
  module Shared
3
3
  module Timed
4
-
5
- def run_and_report_duration &block
4
+ def run_and_report_duration
6
5
  start = Time.now
7
- block.call
6
+ yield
8
7
  Time.now - start
9
8
  end
10
-
11
9
  end
12
10
  end
13
11
  end
14
-
data/lib/beaker/shared.rb CHANGED
@@ -1,4 +1,4 @@
1
- [ 'repetition', 'error_handler', 'host_manager', 'timed', 'semvar', 'options_resolver', 'fog_credentials'].each do |lib|
1
+ %w[repetition error_handler host_manager timed semvar options_resolver fog_credentials].each do |lib|
2
2
  require "beaker/shared/#{lib}"
3
3
  end
4
4
  module Beaker