beaker 4.41.2 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +23 -10
  6. data/.rubocop_todo.yml +34 -10
  7. data/CHANGELOG.md +16 -52
  8. data/Gemfile +12 -7
  9. data/HISTORY.md +605 -0
  10. data/Rakefile +99 -111
  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/Rakefile +1 -1
  16. data/acceptance/fixtures/module/spec/acceptance/demo_spec.rb +8 -12
  17. data/acceptance/fixtures/module/spec/classes/init_spec.rb +0 -1
  18. data/acceptance/fixtures/module/spec/spec_helper_acceptance.rb +3 -5
  19. data/acceptance/lib/helpers/test_helper.rb +6 -5
  20. data/acceptance/pre_suite/subcommands/05_install_ruby.rb +2 -3
  21. data/acceptance/pre_suite/subcommands/08_install_beaker.rb +1 -2
  22. data/acceptance/tests/base/dsl/helpers/configuration_test.rb +4 -4
  23. data/acceptance/tests/base/dsl/helpers/hocon_helpers_test.rb +1 -2
  24. data/acceptance/tests/base/dsl/helpers/host_helpers/add_system32_hosts_entry_test.rb +0 -3
  25. data/acceptance/tests/base/dsl/helpers/host_helpers/archive_file_from_test.rb +3 -1
  26. data/acceptance/tests/base/dsl/helpers/host_helpers/backup_the_file_test.rb +9 -9
  27. data/acceptance/tests/base/dsl/helpers/host_helpers/check_for_package_test.rb +0 -4
  28. data/acceptance/tests/base/dsl/helpers/host_helpers/create_remote_file_test.rb +19 -26
  29. data/acceptance/tests/base/dsl/helpers/host_helpers/curl_on_test.rb +2 -2
  30. data/acceptance/tests/base/dsl/helpers/host_helpers/curl_with_retries_test.rb +0 -1
  31. data/acceptance/tests/base/dsl/helpers/host_helpers/on_test.rb +18 -13
  32. data/acceptance/tests/base/dsl/helpers/host_helpers/retry_on_test.rb +3 -3
  33. data/acceptance/tests/base/dsl/helpers/host_helpers/rsync_to_test.rb +22 -27
  34. data/acceptance/tests/base/dsl/helpers/host_helpers/run_cron_on_test.rb +12 -16
  35. data/acceptance/tests/base/dsl/helpers/host_helpers/run_script_on_test.rb +3 -4
  36. data/acceptance/tests/base/dsl/helpers/host_helpers/run_script_test.rb +3 -3
  37. data/acceptance/tests/base/dsl/helpers/host_helpers/scp_from_test.rb +4 -4
  38. data/acceptance/tests/base/dsl/helpers/host_helpers/scp_to_test.rb +3 -4
  39. data/acceptance/tests/base/dsl/helpers/host_helpers/shell_test.rb +5 -5
  40. data/acceptance/tests/base/dsl/helpers/host_helpers/upgrade_package_test.rb +3 -5
  41. data/acceptance/tests/base/dsl/platform_tag_confiner_test.rb +14 -17
  42. data/acceptance/tests/base/dsl/structure_test.rb +5 -11
  43. data/acceptance/tests/base/host/file_test.rb +2 -2
  44. data/acceptance/tests/base/host/group_test.rb +0 -1
  45. data/acceptance/tests/base/host/host_test.rb +66 -63
  46. data/acceptance/tests/base/host/packages.rb +1 -2
  47. data/acceptance/tests/base/host/packages_unix.rb +0 -55
  48. data/acceptance/tests/base/host/user_test.rb +0 -1
  49. data/acceptance/tests/base/host_prebuilt_steps/ssh_environment_test.rb +1 -2
  50. data/acceptance/tests/base/test_suite/export.rb +6 -9
  51. data/acceptance/tests/install/from_file.rb +2 -4
  52. data/acceptance/tests/load_path_bootstrap.rb +1 -1
  53. data/acceptance/tests/subcommands/destroy.rb +19 -21
  54. data/acceptance/tests/subcommands/exec.rb +0 -1
  55. data/acceptance/tests/subcommands/init.rb +2 -3
  56. data/acceptance/tests/subcommands/provision.rb +0 -1
  57. data/beaker.gemspec +4 -7
  58. data/docs/concepts/argument_processing_and_precedence.md +1 -10
  59. data/docs/how_to/debug_beaker_tests.md +12 -12
  60. data/docs/how_to/hosts/eos.md +2 -12
  61. data/docs/how_to/install_puppet.md +0 -18
  62. data/docs/how_to/the_beaker_dsl.md +0 -2
  63. data/lib/beaker/cli.rb +59 -68
  64. data/lib/beaker/command.rb +20 -28
  65. data/lib/beaker/command_factory.rb +3 -2
  66. data/lib/beaker/dsl/assertions.rb +6 -18
  67. data/lib/beaker/dsl/helpers/hocon_helpers.rb +3 -7
  68. data/lib/beaker/dsl/helpers/host_helpers.rb +62 -123
  69. data/lib/beaker/dsl/helpers/test_helpers.rb +3 -5
  70. data/lib/beaker/dsl/helpers/web_helpers.rb +19 -39
  71. data/lib/beaker/dsl/helpers.rb +2 -4
  72. data/lib/beaker/dsl/outcomes.rb +13 -15
  73. data/lib/beaker/dsl/patterns.rb +1 -3
  74. data/lib/beaker/dsl/roles.rb +17 -20
  75. data/lib/beaker/dsl/structure.rb +53 -65
  76. data/lib/beaker/dsl/test_tagging.rb +7 -10
  77. data/lib/beaker/dsl/wrappers.rb +15 -16
  78. data/lib/beaker/dsl.rb +2 -3
  79. data/lib/beaker/host/aix/exec.rb +1 -1
  80. data/lib/beaker/host/aix/file.rb +0 -1
  81. data/lib/beaker/host/aix/group.rb +1 -1
  82. data/lib/beaker/host/aix/user.rb +1 -1
  83. data/lib/beaker/host/aix.rb +3 -4
  84. data/lib/beaker/host/cisco.rb +27 -39
  85. data/lib/beaker/host/eos.rb +4 -30
  86. data/lib/beaker/host/freebsd/exec.rb +1 -1
  87. data/lib/beaker/host/freebsd/pkg.rb +3 -3
  88. data/lib/beaker/host/freebsd.rb +9 -12
  89. data/lib/beaker/host/mac/exec.rb +4 -4
  90. data/lib/beaker/host/mac/group.rb +7 -7
  91. data/lib/beaker/host/mac/pkg.rb +3 -106
  92. data/lib/beaker/host/mac/user.rb +2 -2
  93. data/lib/beaker/host/mac.rb +8 -9
  94. data/lib/beaker/host/pswindows/exec.rb +66 -70
  95. data/lib/beaker/host/pswindows/file.rb +3 -5
  96. data/lib/beaker/host/pswindows/group.rb +3 -3
  97. data/lib/beaker/host/pswindows/pkg.rb +12 -12
  98. data/lib/beaker/host/pswindows/user.rb +3 -3
  99. data/lib/beaker/host/pswindows.rb +4 -3
  100. data/lib/beaker/host/unix/exec.rb +81 -83
  101. data/lib/beaker/host/unix/file.rb +40 -45
  102. data/lib/beaker/host/unix/group.rb +1 -1
  103. data/lib/beaker/host/unix/pkg.rb +138 -401
  104. data/lib/beaker/host/unix/user.rb +2 -2
  105. data/lib/beaker/host/unix.rb +8 -11
  106. data/lib/beaker/host/windows/exec.rb +17 -17
  107. data/lib/beaker/host/windows/file.rb +3 -3
  108. data/lib/beaker/host/windows/group.rb +3 -3
  109. data/lib/beaker/host/windows/pkg.rb +3 -54
  110. data/lib/beaker/host/windows/user.rb +3 -3
  111. data/lib/beaker/host/windows.rb +12 -12
  112. data/lib/beaker/host.rb +76 -133
  113. data/lib/beaker/host_prebuilt_steps.rb +88 -196
  114. data/lib/beaker/hypervisor/noop.rb +2 -4
  115. data/lib/beaker/hypervisor.rb +44 -61
  116. data/lib/beaker/local_connection.rb +2 -4
  117. data/lib/beaker/logger.rb +68 -76
  118. data/lib/beaker/logger_junit.rb +21 -25
  119. data/lib/beaker/network_manager.rb +39 -42
  120. data/lib/beaker/options/command_line_parser.rb +12 -23
  121. data/lib/beaker/options/hosts_file_parser.rb +16 -25
  122. data/lib/beaker/options/options_file_parser.rb +3 -6
  123. data/lib/beaker/options/options_hash.rb +2 -7
  124. data/lib/beaker/options/parser.rb +86 -102
  125. data/lib/beaker/options/presets.rb +114 -123
  126. data/lib/beaker/options/subcommand_options_file_parser.rb +3 -6
  127. data/lib/beaker/options/validator.rb +26 -31
  128. data/lib/beaker/perf.rb +25 -30
  129. data/lib/beaker/platform.rb +26 -37
  130. data/lib/beaker/result.rb +7 -6
  131. data/lib/beaker/shared/error_handler.rb +8 -10
  132. data/lib/beaker/shared/fog_credentials.rb +5 -9
  133. data/lib/beaker/shared/host_manager.rb +36 -41
  134. data/lib/beaker/shared/options_resolver.rb +3 -7
  135. data/lib/beaker/shared/repetition.rb +2 -4
  136. data/lib/beaker/shared/semvar.rb +37 -41
  137. data/lib/beaker/shared/timed.rb +0 -3
  138. data/lib/beaker/shared.rb +1 -1
  139. data/lib/beaker/ssh_connection.rb +38 -47
  140. data/lib/beaker/subcommand.rb +17 -24
  141. data/lib/beaker/subcommands/subcommand_util.rb +4 -4
  142. data/lib/beaker/tasks/quick_start.rb +4 -9
  143. data/lib/beaker/tasks/rake_task.rb +25 -27
  144. data/lib/beaker/tasks/test.rb +4 -4
  145. data/lib/beaker/test_case.rb +16 -28
  146. data/lib/beaker/test_suite.rb +35 -39
  147. data/lib/beaker/test_suite_result.rb +45 -47
  148. data/lib/beaker/version.rb +1 -1
  149. data/lib/beaker.rb +6 -7
  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 +22 -31
  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 +172 -161
  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 +48 -57
  168. data/spec/beaker/host/mac/user_spec.rb +54 -63
  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 -71
  175. data/spec/beaker/host/unix/pkg_spec.rb +150 -411
  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 +168 -349
  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 -50
  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 +39 -45
  212. data/spec/matchers.rb +8 -7
  213. data/spec/mocks.rb +1 -6
  214. data/spec/spec_helper.rb +0 -1
  215. metadata +8 -24
  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
@@ -3,13 +3,10 @@ module Beaker
3
3
  # all String methods while adding several platform-specific use cases.
4
4
  class Platform < String
5
5
  # Supported platforms
6
- PLATFORMS = /^(alpine|amazon|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)\-.+\-.+$/
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 => { "forky" => "14",
10
- "trixie" => "13",
11
- "bookworm" => "12",
12
- "bullseye" => "11",
9
+ { :debian => { "bullseye" => "11",
13
10
  "buster" => "10",
14
11
  "stretch" => "9",
15
12
  "jessie" => "8",
@@ -24,24 +21,21 @@ module Beaker
24
21
  "artful" => "1710",
25
22
  "zesty" => "1704",
26
23
  "yakkety" => "1610",
27
- "xenial" => "1604",
28
- "wily" => "1510",
29
- "vivid" => "1504",
30
- "utopic" => "1410",
31
- "trusty" => "1404",
32
- "saucy" => "1310",
33
- "raring" => "1304",
24
+ "xenial" => "1604",
25
+ "wily" => "1510",
26
+ "vivid" => "1504",
27
+ "utopic" => "1410",
28
+ "trusty" => "1404",
29
+ "saucy" => "1310",
30
+ "raring" => "1304",
34
31
  "quantal" => "1210",
35
32
  "precise" => "1204",
36
- "lucid" => "1004",
37
- },
38
- :osx => { "highsierra" => "1013",
39
- "sierra" => "1012",
40
- "elcapitan" => "1011",
41
- "yosemite" => "1010",
42
- "mavericks" => "109",
43
- }
44
- }
33
+ "lucid" => "1004", },
34
+ :osx => { "highsierra" => "1013",
35
+ "sierra" => "1012",
36
+ "elcapitan" => "1011",
37
+ "yosemite" => "1010",
38
+ "mavericks" => "109", }, }
45
39
 
46
40
  # A string with the name of the platform.
47
41
  attr_reader :variant
@@ -59,7 +53,6 @@ module Beaker
59
53
  # Creates the Platform object. Checks to ensure that the platform String
60
54
  # provided meets the platform formatting rules. Platforms name must be of
61
55
  # the format /^OSFAMILY-VERSION-ARCH.*$/ where OSFAMILY is one of:
62
- # * amazon
63
56
  # * huaweios
64
57
  # * cisco_nexus
65
58
  # * cisco_ios_xr
@@ -85,9 +78,7 @@ module Beaker
85
78
  # * netscaler
86
79
  # * archlinux
87
80
  def initialize(name)
88
- if !PLATFORMS.match?(name)
89
- raise ArgumentError, "Unsupported platform name #{name}"
90
- end
81
+ raise ArgumentError, "Unsupported platform name #{name}" if !PLATFORMS.match?(name)
91
82
 
92
83
  super
93
84
 
@@ -97,15 +88,15 @@ module Beaker
97
88
  @version = version
98
89
  @codename = nil
99
90
 
100
- if codename_version_hash
101
- if codename_version_hash[version]
102
- @codename = version
103
- @version = codename_version_hash[version]
104
- else
105
- version = version.delete('.')
106
- version_codename_hash = codename_version_hash.invert
107
- @codename = version_codename_hash[version]
108
- 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]
109
100
  end
110
101
  end
111
102
 
@@ -121,9 +112,7 @@ module Beaker
121
112
  # @return [String] the platform string with the platform version represented as a codename
122
113
  def with_version_codename
123
114
  version_array = [@variant, @version, @arch]
124
- if @codename
125
- version_array = [@variant, @codename, @arch]
126
- end
115
+ version_array = [@variant, @codename, @arch] if @codename
127
116
  return version_array.join('-')
128
117
  end
129
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
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,6 +58,7 @@ 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
63
  case role_hosts.length
63
64
  when 0
@@ -65,7 +66,7 @@ module Beaker
65
66
  when 1
66
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
72
  end
@@ -88,19 +89,16 @@ module Beaker
88
89
  # and thus a result object is returned.
89
90
  def run_block_on hosts = [], filter = nil, opts = {}, &block
90
91
  result = nil
91
- 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
92
93
  if filter
93
- if not hosts.empty?
94
- block_hosts = hosts_with_role(hosts, filter) #check by role
95
- if block_hosts.empty?
96
- block_hosts = hosts_with_name(hosts, filter) #check by name
97
- end
98
- if block_hosts.length == 1 #we only found one matching host, don't need it wrapped in an array
99
- block_hosts = block_hosts.pop
100
- end
101
- else
102
- 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
103
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
+
104
102
  end
105
103
  if block_hosts.is_a? Array
106
104
  if block_hosts.length > 0
@@ -109,7 +107,7 @@ module Beaker
109
107
  result = block_hosts.map.each_in_parallel(caller(2..2).first) do |h|
110
108
  run_block_on h, &block
111
109
  end
112
- 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
113
111
  # after spawning a process and running commands on a host,
114
112
  # or else it gets into a broken state for the next call.
115
113
  else
@@ -117,20 +115,17 @@ module Beaker
117
115
  run_block_on h, &block
118
116
  end
119
117
  end
120
- else
118
+ elsif (cur_logger = (logger || @logger))
121
119
  # there are no matching hosts to execute against
122
120
  # should warn here
123
121
  # check if logger is defined in this context
124
- if ( cur_logger = (logger || @logger ) )
125
- cur_logger.info "Attempting to execute against an empty array of hosts (#{hosts}, filtered to #{block_hosts}), no execution will occur"
126
- end
122
+ cur_logger.info "Attempting to execute against an empty array of hosts (#{hosts}, filtered to #{block_hosts}), no execution will occur"
127
123
  end
128
124
  else
129
125
  result = yield block_hosts
130
126
  end
131
127
  result
132
128
  end
133
-
134
129
  end
135
130
  end
136
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,7 +1,6 @@
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
@@ -10,7 +9,7 @@ module Beaker
10
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
12
+ until done or timeout < Time.now
14
13
  done = yield
15
14
  sleep wait unless done
16
15
  end
@@ -21,7 +20,7 @@ module Beaker
21
20
  done = false
22
21
  attempt = 1
23
22
  last_wait, wait = 0, 1
24
- while not done and attempt <= attempts do
23
+ while not done and attempt <= attempts
25
24
  done = yield
26
25
  attempt += 1
27
26
  sleep wait unless done
@@ -32,4 +31,3 @@ module Beaker
32
31
  end
33
32
  end
34
33
  end
35
-
@@ -1,32 +1,28 @@
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
28
  a_is_rc = !a_is_release && /rc\d+/.match?(a_rest[0])
@@ -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
4
  def run_and_report_duration
6
5
  start = Time.now
7
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