cem_acpt 0.2.6-universal-java-17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.rspec +3 -0
  4. data/CODEOWNERS +1 -0
  5. data/Gemfile +9 -0
  6. data/Gemfile.lock +93 -0
  7. data/README.md +150 -0
  8. data/Rakefile +6 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/cem_acpt.gemspec +39 -0
  12. data/exe/cem_acpt +84 -0
  13. data/lib/cem_acpt/bootstrap/bootstrapper.rb +206 -0
  14. data/lib/cem_acpt/bootstrap/operating_system/rhel_family.rb +129 -0
  15. data/lib/cem_acpt/bootstrap/operating_system.rb +17 -0
  16. data/lib/cem_acpt/bootstrap.rb +12 -0
  17. data/lib/cem_acpt/context.rb +153 -0
  18. data/lib/cem_acpt/core_extensions.rb +108 -0
  19. data/lib/cem_acpt/image_name_builder.rb +104 -0
  20. data/lib/cem_acpt/logging.rb +351 -0
  21. data/lib/cem_acpt/platform/base/cmd.rb +71 -0
  22. data/lib/cem_acpt/platform/base.rb +78 -0
  23. data/lib/cem_acpt/platform/gcp/cmd.rb +345 -0
  24. data/lib/cem_acpt/platform/gcp/compute.rb +332 -0
  25. data/lib/cem_acpt/platform/gcp.rb +85 -0
  26. data/lib/cem_acpt/platform/vmpooler.rb +24 -0
  27. data/lib/cem_acpt/platform.rb +103 -0
  28. data/lib/cem_acpt/puppet_helpers.rb +39 -0
  29. data/lib/cem_acpt/rspec_utils.rb +242 -0
  30. data/lib/cem_acpt/shared_objects.rb +537 -0
  31. data/lib/cem_acpt/spec_helper_acceptance.rb +184 -0
  32. data/lib/cem_acpt/test_data.rb +146 -0
  33. data/lib/cem_acpt/test_runner/run_handler.rb +187 -0
  34. data/lib/cem_acpt/test_runner/runner.rb +210 -0
  35. data/lib/cem_acpt/test_runner/runner_result.rb +103 -0
  36. data/lib/cem_acpt/test_runner.rb +10 -0
  37. data/lib/cem_acpt/utils.rb +144 -0
  38. data/lib/cem_acpt/version.rb +5 -0
  39. data/lib/cem_acpt.rb +34 -0
  40. data/sample_config.yaml +58 -0
  41. metadata +218 -0
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ # GCP platform implementation
4
+ module Platform
5
+ require_relative 'gcp/compute'
6
+ require_relative 'gcp/cmd'
7
+
8
+ # Returns information about the GCP instance
9
+ def node
10
+ @instance.info
11
+ end
12
+
13
+ # Provision a GCP instance
14
+ def provision
15
+ creation_params = config.dup
16
+ creation_params[:disk][:image_name] = image_name
17
+ creation_params[:local_port] = local_port
18
+ @instance = CemAcpt::Platform::Gcp::VM.new(
19
+ node_name,
20
+ components: creation_params,
21
+ )
22
+ @instance.configure!
23
+ logger.debug("Creating with command: #{@instance.send(:create_cmd)}")
24
+ @instance.create
25
+ end
26
+
27
+ # Destroy a GCP instance
28
+ def destroy
29
+ @instance.destroy
30
+ end
31
+
32
+ # Returns true if the GCP instance is ready for use in the test suite
33
+ def ready?
34
+ logger.debug("Checking if #{node_name} is ready...")
35
+ @instance.ready?
36
+ end
37
+
38
+ # Runs the test suite against the GCP instance. Must be given a block.
39
+ # If necessary, can pass information into the block to be used in the test suite.
40
+ def run_tests(&block)
41
+ logger.debug("Running tests for #{node_name}...")
42
+ block.call @instance.cmd.env
43
+ end
44
+
45
+ # Uploads and installs a Puppet module package on the GCP instance.
46
+ def install_puppet_module_package(module_pkg_path, remote_path = nil, puppet_path = '/opt/puppetlabs/bin/puppet')
47
+ remote_path = remote_path.nil? ? File.join('/tmp', File.basename(module_pkg_path)) : remote_path
48
+ logger.info("Uploading module package #{module_pkg_path} to #{remote_path} on #{node_name} and installing it...")
49
+ logger.debug("Using puppet path: #{puppet_path}")
50
+ @instance.install_puppet_module_package(module_pkg_path, remote_path, puppet_path)
51
+ logger.info("Module package #{module_pkg_path} installed on #{node_name}")
52
+ end
53
+
54
+ # Extends the class with class methods from the SpecMethods module
55
+ def self.included(base)
56
+ base.extend(SpecMethods)
57
+ end
58
+
59
+ # Holds class methods called from spec tests.
60
+ module SpecMethods
61
+ # Returns an instance of the GCP platform class command provider
62
+ # @return [CemAcpt::Platform::Gcp::Cmd]
63
+ def command_provider
64
+ CemAcpt::Platform::Gcp::Cmd.new(out_format: 'json')
65
+ end
66
+
67
+ # Apllies the given Puppet manifest on the given instance
68
+ # @param instance_name [String] the name of the instance to apply the manifest to
69
+ # @param manifest [String] the Puppet manifest to apply
70
+ # @param opts [Hash] options to pass to the apply command
71
+ # @return [String] the output of the apply command
72
+ def apply_manifest(instance_name, manifest, opts = {})
73
+ command_provider.apply_manifest(instance_name, manifest, opts)
74
+ end
75
+
76
+ # Runs a shell command on the given instance
77
+ # @param instance_name [String] the name of the instance to run the command on
78
+ # @param command [String] the command to run
79
+ # @param opts [Hash] options to pass to the run_shell command
80
+ # @return [String] the output of the run_shell command
81
+ def run_shell(instance_name, cmd, opts = {})
82
+ command_provider.run_shell(instance_name, cmd, opts)
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Currently a placeholder for future functionality
4
+ module Platform
5
+ def node
6
+ logger.debug 'VMPooler node'
7
+ end
8
+
9
+ def provision
10
+ logger.debug 'VMPooler provision'
11
+ end
12
+
13
+ def destroy
14
+ logger.debug 'VMPooler destroy'
15
+ end
16
+
17
+ def ready?
18
+ logger.debug 'VMPooler ready?'
19
+ end
20
+
21
+ def install_puppet_module_package
22
+ logger.debug 'VMPooler install_puppet_module_package'
23
+ end
24
+ end
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'concurrent-ruby'
4
+
5
+ # CemAcpt::Platform manages creating and configring platform specific objects
6
+ # for the acceptance test suites.
7
+ module CemAcpt::Platform
8
+ require_relative 'logging'
9
+
10
+ class Error < StandardError; end
11
+
12
+ PLATFORM_DIR = File.expand_path(File.join(__dir__, 'platform'))
13
+
14
+ class << self
15
+ include CemAcpt::Logging
16
+
17
+ # Creates a new platform specific object of the given platform for each
18
+ # item in the test data.
19
+ # @param platform [String] the name of the platform
20
+ # @param config [CemAcpt::Config] the config object
21
+ # @param test_data [Hash] the test data
22
+ # @param local_port_allocator [CemAcpt::LocalPortAllocator] the local port allocator
23
+ def use(platform, config, test_data, local_port_allocator)
24
+ raise Error, "Platform #{platform} is not supported" unless platforms.include?(platform)
25
+ raise Error, 'test_data must be an Array' unless test_data.is_a?(Array)
26
+
27
+ logger.info "Using #{platform} for #{test_data.length} tests..."
28
+ test_data.each_with_object([]) do |single_test_data, ary|
29
+ local_port = local_port_allocator.allocate
30
+ logger.debug("Allocated local port #{local_port} for test #{single_test_data[:test_name]}")
31
+ ary << new_platform_object(platform, config, single_test_data, local_port)
32
+ end
33
+ end
34
+
35
+ # Returns an un-initialized platform specific Class of the given platform.
36
+ # @param platform [String] the name of the platform
37
+ def get(platform)
38
+ raise Error, "Platform #{platform} is not supported" unless platforms.include?(platform)
39
+
40
+ platform_class(platform)
41
+ end
42
+
43
+ private
44
+
45
+ # Dynamically creates a new platform class if it doesn't exist
46
+ # and returns a new instance of it.
47
+ # @param platform [String] the name of the platform.
48
+ # @param config [CemAcpt::Config] the config object.
49
+ # @param single_test_data [Hash] the test data for a single test.
50
+ # @return [CemAcpt::Platform::Base] an initialized platform class.
51
+ def new_platform_object(platform, config, single_test_data, local_port)
52
+ raise Error, 'single_test_data must be a Hash' unless single_test_data.is_a?(Hash)
53
+
54
+ platform_class(platform).new(config, single_test_data, local_port)
55
+ end
56
+
57
+ # Creates a new platform-specific Class object for the given platform.
58
+ # Does not initialize the class.
59
+ # @param platform [String] the name of the platform.
60
+ # @return [Class] the platform-specific Object.
61
+ def platform_class(platform)
62
+ # We require the platform base class here so that we can use it as
63
+ # a parent class for the platform-specific class.
64
+ require_relative 'platform/base'
65
+ # If the class has already been defined, we can just use it.
66
+ if Object.const_defined?(platform.capitalize)
67
+ klass = Object.const_get(platform.capitalize)
68
+ else
69
+ # Otherwise, we need to create the class. We do this by setting
70
+ # a new constant with the name of the platform capitalized, and
71
+ # associate that constant with a new instance of Class that inherits
72
+ # from the platform base class. We then require the platform file,
73
+ # include and extend our class with the Platform module from the file,
74
+ # include Logging and Concurrent::Async, and finally call the
75
+ # initialize method on the class.
76
+ klass = Object.const_set(
77
+ platform.capitalize,
78
+ Class.new(CemAcpt::Platform::Base) do
79
+ require_relative "platform/#{platform}"
80
+ include Platform
81
+ end,
82
+ )
83
+ end
84
+ klass
85
+ end
86
+
87
+ # Returns an array of the names of the supported platforms.
88
+ # Supported platforms are discovered by looking for files in the
89
+ # platform directory, and platform names are the basename (no extension)
90
+ # of the files. We deliberately exclude the base class, as it is not
91
+ # a platform.
92
+ def platforms
93
+ return @platforms if defined?(@platforms)
94
+
95
+ @platforms = Dir.glob(File.join(PLATFORM_DIR, '*.rb')).map do |file|
96
+ File.basename(file, '.rb') unless file.end_with?('base.rb')
97
+ end
98
+ @platforms.compact!
99
+ logger.debug "Discovered platform(s): #{@platforms}"
100
+ @platforms
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'logging'
4
+
5
+ module CemAcpt
6
+ # Holds modules that provide helper methods for various Puppet-related
7
+ # tasks.
8
+ module PuppetHelpers
9
+ # Provides helper methods for Puppet Modules.
10
+ module Module
11
+ class << self
12
+ include CemAcpt::Logging
13
+ end
14
+
15
+ # Builds a Puppet module package.
16
+ # @param module_dir [String] Path to the module directory. If target_dir
17
+ # is specified as a relative path, it will be relative to the module dir.
18
+ # @param target_dir [String] Path to the target directory where the package
19
+ # will be built. This defaults to the relative path 'pkg/'.
20
+ # @param should_log [Boolean] Whether or not to log the build process.
21
+ # @return [String] Path to the built package.
22
+ def self.build_module_package(module_dir, target_dir = nil, should_log: false)
23
+ require 'puppet/modulebuilder'
24
+ require 'fileutils'
25
+
26
+ builder_logger = should_log ? logger : nil
27
+ builder = Puppet::Modulebuilder::Builder.new(File.expand_path(module_dir), target_dir, builder_logger)
28
+
29
+ # Validates module metadata by raising exception if invalid
30
+ _metadata = builder.metadata
31
+ logger.debug("Metadata for module #{builder.release_name} is valid")
32
+
33
+ # Builds the module package
34
+ logger.info("Building module package for #{builder.release_name}")
35
+ builder.build
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,242 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'English'
4
+ require 'open3'
5
+ require 'pty'
6
+ require 'shellwords'
7
+ require_relative 'logging'
8
+
9
+ module CemAcpt
10
+ # Module that provides methods and objects for running and managing RSpec
11
+ module RSpecUtils
12
+ class BundlerNotFoundError < StandardError; end
13
+ class RSpecNotFoundError < StandardError; end
14
+
15
+ # Holds and formats a RSpec command
16
+ class Command
17
+ include CemAcpt::LoggingAsync
18
+ attr_reader :debug, :format, :test_path, :use_bundler, :pty_pid
19
+
20
+ # @param opts [Hash] options hash for the RSpec command
21
+ # @option opts [String] :test_path The path (or glob path) to the test file(s) to run. If blank, runs all.
22
+ # @option opts [Hash] :format Format options for rspec where the key is the format (documentation, json, etc)
23
+ # and the value is the out-file path. If you do not want to save the results of a format to a file, the
24
+ # value should be `nil`.
25
+ # @option opts [Boolean] :debug True if RSpec should run in debug mode, false if not. Mutually exclusive with
26
+ # `:quiet`. Default is `false`.
27
+ # @option opts [Boolean] :quiet True if no output should be logged from RSpec command, false if output should
28
+ # be logged. Mutually exclusive with `:debug`. Default is `false`.
29
+ # @option opts [Boolean] :use_bundler Whether or not `bundle exec` should be used to run the RSpec command.
30
+ # Default is `true`.
31
+ # @option opts [Boolean] :bundle_install Whether or not to run `bundle install` before the RSpec command
32
+ # if `use_bundler` is `true`.
33
+ # @option opts [Boolean] :use_shell Whether or not to add `$SHELL` as a prefix to the command
34
+ # @option opts [Hash] :env Environment variables to prepend to the command
35
+ def initialize(opts = {})
36
+ @test_path = opts[:test_path]&.shellescape
37
+ @format = opts.fetch(:format, {})
38
+ @debug = opts.fetch(:debug, false)
39
+ @quiet = @debug ? false : opts.fetch(:quiet, false)
40
+ @use_bundler = opts.fetch(:use_bundler, false)
41
+ @bundle_install = opts.fetch(:bundle_install, false)
42
+ @env = opts.fetch(:env, {})
43
+ @pty_pid = nil
44
+ validate_and_set_bin_paths(opts)
45
+ end
46
+
47
+ # Sets debug mode to `true`
48
+ def set_debug
49
+ @debug = true
50
+ if @quiet
51
+ async_debug('Setting :quiet to false because :debug is now true.')
52
+ @quiet = false
53
+ end
54
+ end
55
+
56
+ # Sets debug mode to `false`
57
+ def unset_debug
58
+ @debug = false
59
+ end
60
+
61
+ def quiet
62
+ @quiet && !debug
63
+ end
64
+
65
+ # Adds a new format to the RSpec command
66
+ # @param fmt [String] The name of the format (i.e. "documentation", "json", etc.)
67
+ # @param out [String] If specified, saves the specified format to a file at this path
68
+ def with_format(fmt, out: nil)
69
+ @format[fmt.to_sym] = out
70
+ end
71
+
72
+ # Environment variables that will be used for the RSpec command
73
+ # @return [Hash] A Hash of environment variables with each key pair being: <var name> => <var value>
74
+ def env
75
+ @debug ? @env.merge({ 'RSPEC_DEBUG' => 'true' }) : @env
76
+ end
77
+
78
+ # Returns an array representation of the RSpec command
79
+ def to_a
80
+ cmd = cmd_base.dup
81
+ cmd << test_path if test_path
82
+ format.each do |fmt, out|
83
+ cmd += ['--format', fmt.to_s.shellescape]
84
+ cmd += ['--out', out.to_s.shellescape] if out
85
+ end
86
+ cmd.compact
87
+ end
88
+
89
+ # Returns a string representation of the RSpec command
90
+ def to_s
91
+ to_a.join(' ')
92
+ end
93
+
94
+ # Executes the RSpec command on the current machine
95
+ # @param pty [Boolean] If true, execute command in a PTY. If false, execute command directly.
96
+ # @param log_prefix [String] A prefix to add to log messages generated while the command is running.
97
+ def execute(pty: true, log_prefix: 'RSPEC')
98
+ if pty
99
+ execute_pty(log_prefix: log_prefix)
100
+ else
101
+ execute_no_pty(log_prefix: log_prefix)
102
+ end
103
+ end
104
+
105
+ # Executes the RSpec command in a psuedo-terminal (PTY). First, it spawns a process
106
+ # for $SHELL, sets environment variables `export_envs`, then calls the current RSpec
107
+ # command in the shell and exits with the last exit code `$?`. Output is read from the
108
+ # RSpec command in near real-time in a blocking manner unless the `:quiet` option has
109
+ # been specified.
110
+ # @param log_prefix [String] A prefix to add to the log messages that are output from
111
+ # the RSpec command.
112
+ # @return [Integer] The exit code of the RSpec command
113
+ def execute_pty(log_prefix: 'RSPEC')
114
+ async_debug("Executing RSpec command '#{self}' in PTY...", log_prefix)
115
+ PTY.spawn(env, ENV['SHELL']) do |r, w, pid|
116
+ @pty_pid = pid
117
+ async_debug("Spawned RSpec PTY with PID #{@pty_pid}", log_prefix)
118
+ export_envs(w)
119
+ w.puts "#{self}; exit $?"
120
+ quiet ? wait_io(r) : read_io(r, log_prefix: log_prefix)
121
+ end
122
+ $CHILD_STATUS
123
+ end
124
+
125
+ # Executes the RSpec command using Open3.popen2e(). The output stream, which is both
126
+ # stderr and stdout, is read in real-time in a non-blocking manner.
127
+ # @param log_prefix [String] A prefix to add to the log messages that are output from
128
+ # the RSpec command.
129
+ # @return [Integer] The exit code of the RSpec command
130
+ def execute_no_pty(log_prefix: 'RSPEC')
131
+ async_info("Executing RSpec command '#{self}' with Open3.popen2e()...", log_prefix)
132
+ exit_status = nil
133
+ Open3.popen2e(env, to_s) do |stdin, std_out_err, wait_thr|
134
+ stdin.close
135
+ quiet ? wait_io(std_out_err) : read_io(std_out_err, log_prefix: log_prefix)
136
+ exit_status = wait_thr.value
137
+ end
138
+ exit_status
139
+ end
140
+
141
+ # Kills the PTY process with `SIGKILL` if the process exists
142
+ def kill_pty
143
+ Process.kill('KILL', @pty_pid) unless @pty_pid.nil?
144
+ rescue Errno::ESRCH
145
+ true
146
+ end
147
+
148
+ private
149
+
150
+ # Detects if the current Ruby context is JRuby
151
+ def jruby?
152
+ File.basename(RbConfig.ruby) == 'jruby'
153
+ end
154
+
155
+ # The base RSpec command
156
+ def cmd_base
157
+ use_bundler ? cmd_base_bundler : cmd_base_rspec
158
+ end
159
+
160
+ # The base RSpec command if `:use_bundler` is `true`.
161
+ def cmd_base_bundler
162
+ base = [@bundle, 'exec', 'rspec']
163
+ base.unshift("#{@bundle} install;") if @bundle_install
164
+ base
165
+ end
166
+
167
+ # The base RSpec command if `:use_bundler` is `false`
168
+ def cmd_base_rspec
169
+ [@rspec]
170
+ end
171
+
172
+ # Puts export statements for each key-value pair in `env` to the given writer.
173
+ # Writer is the write pipe of a PTY session, or a similar IO object that can
174
+ # pass the statements to a shell.
175
+ # @param writer [IO] An IO object that supprts `puts` and can send statements to a shell
176
+ def export_envs(writer)
177
+ env.each do |key, val|
178
+ writer.puts "export #{key}=#{val}"
179
+ end
180
+ end
181
+
182
+ # Finds and sets the paths to the `bundle` and `rspec` binaries. The paths can
183
+ # be either passed in as options in the `opts` Hash or interrogated from the
184
+ # system.
185
+ # @param opts [Hash] The options hash
186
+ # @option opts [String] :bundle An absolute path on the system to the `bundle` binary.
187
+ # @option opts [String] :rspec An absolute path on the system to the `rspec` binary.
188
+ # @raise [CemAcpt::RSpecUtils::BundlerNotFoundError] if `@use_bundler` is true and
189
+ # `bundle` binary is not found.
190
+ # @raise [CemAcpt::RSpecUtils::RSpecNotFoundError] if `rspec` binary is not found.
191
+ def validate_and_set_bin_paths(opts = {})
192
+ %i[bundle rspec].each do |bin|
193
+ bin_path = opts[bin] || `command -v #{bin}`.strip
194
+ bin_not_found(bin, bin_path) unless bin_path && File.exist?(bin_path)
195
+ instance_variable_set("@#{bin}", bin_path)
196
+ end
197
+ end
198
+
199
+ # Handles binary paths which are not found
200
+ # @param bin [Symbol] The binary that was not found, either :bundle or :rspec.
201
+ # @param bin_path [String] The path to the binary that was checked.
202
+ # @raise [CemAcpt::RSpecUtils::BundlerNotFoundError] if `@use_bundler` is true and
203
+ # `bundle` binary is not found.
204
+ # @raise [CemAcpt::RSpecUtils::RSpecNotFoundError] if `rspec` binary is not found.
205
+ # @raise [RuntimeError] if `bin` is not :bundle or :rspec.
206
+ def bin_not_found(bin, bin_path)
207
+ msg_base = "#{bin} not found."
208
+ msg = bin_path.nil? ? "#{msg_base} Path is nil." : "#{msg_base} Path: #{bin_path}"
209
+ case bin
210
+ when :bundle
211
+ raise BundlerNotFoundError, msg if @use_bundler
212
+ when :rspec
213
+ raise RSpecNotFoundError, msg
214
+ else
215
+ raise "bin #{bin} not recognized!"
216
+ end
217
+ end
218
+
219
+ # Blocking wait on an IO stream. Wait stops once the IO stream has reached
220
+ # end of file.
221
+ # @param stdout [IO] An IO stream with output that can be read from.
222
+ def wait_io(stdout)
223
+ sleep(0.01) until stdout.eof?
224
+ end
225
+
226
+ # Reads and logs data from `stdout` in a near real-time, blocking manner.
227
+ # @param stdout [IO] An IO stream with output that can be read from.
228
+ # @param log_prefix [String] A string prefix that is added to log messages.
229
+ def read_io(stdout, log_prefix: 'RSPEC')
230
+ loop do
231
+ chunk = stdout.read_nonblock(4096).strip
232
+ async_info(chunk, log_prefix) unless chunk.nil? || chunk.empty?
233
+ rescue IO::WaitReadable
234
+ IO.select([stdout])
235
+ retry
236
+ rescue EOFError
237
+ break
238
+ end
239
+ end
240
+ end
241
+ end
242
+ end