Module: Nonnative
- Defined in:
- lib/nonnative.rb,
lib/nonnative/pool.rb,
lib/nonnative/port.rb,
lib/nonnative/error.rb,
lib/nonnative/proxy.rb,
lib/nonnative/header.rb,
lib/nonnative/runner.rb,
lib/nonnative/server.rb,
lib/nonnative/process.rb,
lib/nonnative/service.rb,
lib/nonnative/timeout.rb,
lib/nonnative/version.rb,
lib/nonnative/cucumber.rb,
lib/nonnative/no_proxy.rb,
lib/nonnative/go_command.rb,
lib/nonnative/stop_error.rb,
lib/nonnative/grpc_server.rb,
lib/nonnative/http_client.rb,
lib/nonnative/http_server.rb,
lib/nonnative/socket_pair.rb,
lib/nonnative/start_error.rb,
lib/nonnative/configuration.rb,
lib/nonnative/observability.rb,
lib/nonnative/proxy_factory.rb,
lib/nonnative/not_found_error.rb,
lib/nonnative/delay_socket_pair.rb,
lib/nonnative/http_proxy_server.rb,
lib/nonnative/configuration_proxy.rb,
lib/nonnative/socket_pair_factory.rb,
lib/nonnative/configuration_runner.rb,
lib/nonnative/configuration_server.rb,
lib/nonnative/close_all_socket_pair.rb,
lib/nonnative/configuration_process.rb,
lib/nonnative/configuration_service.rb,
lib/nonnative/fault_injection_proxy.rb,
lib/nonnative/invalid_data_socket_pair.rb
Overview
Sinatra-based HTTP forward proxy server used as an in-process Nonnative server.
The proxy receives inbound HTTP requests and forwards them to an upstream host over HTTPS, returning the upstream response status and body.
This file defines two classes:
-
HTTPProxy: a Sinatra application that implements the proxying behavior.
-
HTTPProxyServer: a HTTPServer wrapper that runs the proxy app under Puma.
Notes:
-
This code is adapted from gist.github.com/RaVbaker/d9ead3c92b915f997dab25c7f0c0ab65
-
Only a subset of request headers are forwarded; certain headers are removed to avoid conflicts.
Defined Under Namespace
Modules: Cucumber Classes: CloseAllSocketPair, Configuration, ConfigurationProcess, ConfigurationProxy, ConfigurationRunner, ConfigurationServer, ConfigurationService, DelaySocketPair, Error, FaultInjectionProxy, GRPCServer, GoCommand, HTTPClient, HTTPProxy, HTTPProxyServer, HTTPServer, Header, InvalidDataSocketPair, NoProxy, NotFoundError, Observability, Pool, Port, Process, Proxy, ProxyFactory, Runner, Server, Service, SocketPair, SocketPairFactory, StartError, StopError, Timeout
Constant Summary collapse
- VERSION =
The current gem version.
'2.13.0'
Class Attribute Summary collapse
-
.pool ⇒ Nonnative::Pool?
Returns or overrides the current runner pool (created on Nonnative.start).
Class Method Summary collapse
-
.clear ⇒ void
Clears memoized configuration, logger, observability client, and pool.
-
.clear_configuration ⇒ void
Clears the memoized configuration instance.
-
.clear_logger ⇒ void
Closes and clears the memoized logger instance.
-
.clear_observability ⇒ void
Clears the memoized observability client.
-
.clear_pool ⇒ void
Clears the memoized pool instance.
-
.configuration ⇒ Nonnative::Configuration
Returns the current configuration (memoized).
-
.configurations(*files) ⇒ Config::Options
Loads one or more configuration files using the ‘config` gem.
-
.configure {|config| ... } ⇒ void
Yields the configuration to a block for programmatic setup.
-
.go_executable(tools, output, exec, cmd, *params) ⇒ String
Builds a Go test executable command line with optional profiling/trace/coverage flags.
-
.log_lines(path, predicate) ⇒ Array<String>
Reads a file and returns only lines matching the given predicate.
-
.logger ⇒ Logger
Returns the gem logger (memoized).
-
.observability ⇒ Nonnative::Observability
Returns an HTTP client for common health/readiness endpoints.
-
.proxies ⇒ Hash{String=>Class}
Returns the configured proxy kinds mapped to proxy classes.
-
.proxy(kind) ⇒ Class
Resolves a proxy implementation for a configured kind.
-
.reset ⇒ void
Resets proxies for all currently started runners.
-
.start ⇒ void
Starts all configured services, servers, and processes, and waits for readiness.
-
.stop ⇒ void
Stops all configured processes and servers, then services, and waits for shutdown.
Class Attribute Details
.pool ⇒ Nonnative::Pool?
Returns or overrides the current runner pool (created on start).
114 115 116 |
# File 'lib/nonnative.rb', line 114 def pool @pool end |
Class Method Details
.clear ⇒ void
This method returns an undefined value.
Clears memoized configuration, logger, observability client, and pool.
278 279 280 281 282 283 |
# File 'lib/nonnative.rb', line 278 def clear clear_logger clear_observability clear_configuration clear_pool end |
.clear_configuration ⇒ void
This method returns an undefined value.
Clears the memoized configuration instance.
248 249 250 |
# File 'lib/nonnative.rb', line 248 def clear_configuration @configuration = nil end |
.clear_logger ⇒ void
This method returns an undefined value.
Closes and clears the memoized logger instance.
255 256 257 258 259 |
# File 'lib/nonnative.rb', line 255 def clear_logger @logger&.close ensure @logger = nil end |
.clear_observability ⇒ void
This method returns an undefined value.
Clears the memoized observability client.
264 265 266 |
# File 'lib/nonnative.rb', line 264 def clear_observability @observability = nil end |
.clear_pool ⇒ void
This method returns an undefined value.
Clears the memoized pool instance.
271 272 273 |
# File 'lib/nonnative.rb', line 271 def clear_pool @pool = nil end |
.configuration ⇒ Nonnative::Configuration
Returns the current configuration (memoized).
129 130 131 |
# File 'lib/nonnative.rb', line 129 def configuration @configuration ||= Nonnative::Configuration.new end |
.configurations(*files) ⇒ Config::Options
Loads one or more configuration files using the ‘config` gem.
This is primarily used by Nonnative::Configuration#load_file, but is public for advanced cases.
122 123 124 |
# File 'lib/nonnative.rb', line 122 def configurations(*files) Config.load_files(files) end |
.configure {|config| ... } ⇒ void
This method returns an undefined value.
Yields the configuration to a block for programmatic setup.
143 144 145 |
# File 'lib/nonnative.rb', line 143 def configure yield configuration end |
.go_executable(tools, output, exec, cmd, *params) ⇒ String
Builds a Go test executable command line with optional profiling/trace/coverage flags.
This is used when process configuration specifies a ‘go` section.
175 176 177 |
# File 'lib/nonnative.rb', line 175 def go_executable(tools, output, exec, cmd, *params) Nonnative::GoCommand.new(tools, exec, output).executable(cmd, params) end |
.log_lines(path, predicate) ⇒ Array<String>
Reads a file and returns only lines matching the given predicate.
161 162 163 |
# File 'lib/nonnative.rb', line 161 def log_lines(path, predicate) File.readlines(path).select { |l| predicate.call(l) } end |
.logger ⇒ Logger
Returns the gem logger (memoized).
The logger writes to the path configured at Nonnative::Configuration#log.
152 153 154 |
# File 'lib/nonnative.rb', line 152 def logger @logger ||= Logger.new(configuration.log) end |
.observability ⇒ Nonnative::Observability
Returns an HTTP client for common health/readiness endpoints.
182 183 184 |
# File 'lib/nonnative.rb', line 182 def observability @observability ||= Nonnative::Observability.new(configuration.url) end |
.proxies ⇒ Hash{String=>Class}
Returns the configured proxy kinds mapped to proxy classes.
Consumers can extend this map to add custom proxy implementations.
191 192 193 |
# File 'lib/nonnative.rb', line 191 def proxies @proxies ||= { 'fault_injection' => Nonnative::FaultInjectionProxy }.freeze end |
.proxy(kind) ⇒ Class
Resolves a proxy implementation for a configured kind.
199 200 201 |
# File 'lib/nonnative.rb', line 199 def proxy(kind) Nonnative.proxies[kind] || Nonnative::NoProxy end |
.reset ⇒ void
This method returns an undefined value.
Resets proxies for all currently started runners.
289 290 291 |
# File 'lib/nonnative.rb', line 289 def reset Nonnative.pool.reset end |
.start ⇒ void
This method returns an undefined value.
Starts all configured services, servers, and processes, and waits for readiness.
Readiness is determined by attempting to connect to each runner’s configured host/port.
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/nonnative.rb', line 209 def start @pool ||= Nonnative::Pool.new(configuration) errors = [] errors.concat(@pool.start do |name, values, result| id, started = values errors << "Started #{name} with id #{id}, though did not respond in time" if !started || !result end) nil rescue StandardError => e errors << unexpected_lifecycle_error(:start, e) ensure if errors.any? errors.concat(rollback_start) raise Nonnative::StartError, errors.join("\n") end end |
.stop ⇒ void
This method returns an undefined value.
Stops all configured processes and servers, then services, and waits for shutdown.
231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/nonnative.rb', line 231 def stop errors = [] return if @pool.nil? errors.concat(@pool.stop do |name, id, result| errors << "Stopped #{name} with id #{id}, though did not respond in time" unless result end) nil rescue StandardError => e errors << unexpected_lifecycle_error(:stop, e) ensure raise Nonnative::StopError, errors.join("\n") unless errors.empty? end |