Module: Nonnative
- Defined in:
- lib/nonnative.rb,
lib/nonnative/pool.rb,
lib/nonnative/port.rb,
lib/nonnative/error.rb,
lib/nonnative/ports.rb,
lib/nonnative/proxy.rb,
lib/nonnative/token.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/jwt_token.rb,
lib/nonnative/ssh_token.rb,
lib/nonnative/tcp_probe.rb,
lib/nonnative/grpc_probe.rb,
lib/nonnative/http_probe.rb,
lib/nonnative/stop_error.rb,
lib/nonnative/ed25519_key.rb,
lib/nonnative/grpc_health.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/http_service.rb,
lib/nonnative/paseto_token.rb,
lib/nonnative/configuration.rb,
lib/nonnative/go_executable.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_file.rb,
lib/nonnative/configuration_proxy.rb,
lib/nonnative/socket_pair_factory.rb,
lib/nonnative/timeout_socket_pair.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/configuration_readiness.rb,
lib/nonnative/invalid_data_socket_pair.rb,
lib/nonnative/configuration_service_readiness.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: an HTTP service that implements the proxying behavior.
- HTTPProxyServer: a HTTPServer wrapper that runs the proxy app under Puma.
Notes:
- This code is adapted from https://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, ConfigurationFile, ConfigurationProcess, ConfigurationProxy, ConfigurationReadiness, ConfigurationRunner, ConfigurationServer, ConfigurationService, ConfigurationServiceReadiness, DelaySocketPair, Ed25519Key, Error, FaultInjectionProxy, GRPCHealth, GRPCProbe, GRPCServer, GoExecutable, HTTPClient, HTTPProbe, HTTPProxy, HTTPProxyServer, HTTPServer, HTTPService, Header, InvalidDataSocketPair, JwtToken, NoProxy, NotFoundError, Observability, PasetoToken, Pool, Port, Ports, Process, Proxy, ProxyFactory, Runner, Server, Service, SocketPair, SocketPairFactory, SshToken, StartError, StopError, TCPProbe, Timeout, TimeoutSocketPair, Token
Constant Summary collapse
- VERSION =
The current gem version.
'3.18.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).
-
.configure {|config| ... } ⇒ void
Yields the configuration to a block for programmatic setup.
-
.go_argv(tools, output, exec, cmd, *params) ⇒ Array<String>
Builds a Go test executable argv array with optional profiling/trace/coverage flags.
-
.go_command(tools, output, exec, cmd, *params) ⇒ String
Builds a Go test executable command string with optional profiling/trace/coverage flags.
-
.grpc_health(host:, port:, service:, timeout: 1) ⇒ Nonnative::GRPCHealth
Returns a client helper for the standard gRPC health checking protocol.
-
.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.
-
.token(kind:, issuer:, key:, private_key:, expiration:) ⇒ Nonnative::Token
Builds a token generator for authenticating against a service under test.
Class Attribute Details
.pool ⇒ Nonnative::Pool?
Returns or overrides the current runner pool (created on start).
142 143 144 |
# File 'lib/nonnative.rb', line 142 def pool @pool end |
Class Method Details
.clear ⇒ void
This method returns an undefined value.
Clears memoized configuration, logger, observability client, and pool.
Call this before reconfiguring Nonnative or starting a new lifecycle in the same Ruby process.
start/stop are intended to manage one lifecycle for the current pool.
343 344 345 346 347 348 |
# File 'lib/nonnative.rb', line 343 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.
310 311 312 |
# File 'lib/nonnative.rb', line 310 def clear_configuration @configuration = nil end |
.clear_logger ⇒ void
This method returns an undefined value.
Closes and clears the memoized logger instance.
317 318 319 320 321 |
# File 'lib/nonnative.rb', line 317 def clear_logger @logger&.close ensure @logger = nil end |
.clear_observability ⇒ void
This method returns an undefined value.
Clears the memoized observability client.
326 327 328 |
# File 'lib/nonnative.rb', line 326 def clear_observability @observability = nil end |
.clear_pool ⇒ void
This method returns an undefined value.
Clears the memoized pool instance.
333 334 335 |
# File 'lib/nonnative.rb', line 333 def clear_pool @pool = nil end |
.configuration ⇒ Nonnative::Configuration
Returns the current configuration (memoized).
147 148 149 |
# File 'lib/nonnative.rb', line 147 def configuration @configuration ||= Nonnative::Configuration.new end |
.configure {|config| ... } ⇒ void
This method returns an undefined value.
Yields the configuration to a block for programmatic setup.
161 162 163 |
# File 'lib/nonnative.rb', line 161 def configure yield configuration end |
.go_argv(tools, output, exec, cmd, *params) ⇒ Array<String>
Builds a Go test executable argv array with optional profiling/trace/coverage flags.
Use this when passing argv entries directly to spawn.
207 208 209 |
# File 'lib/nonnative.rb', line 207 def go_argv(tools, output, exec, cmd, *params) Nonnative::GoExecutable.new(tools, exec, output).argv(cmd, *params) end |
.go_command(tools, output, exec, cmd, *params) ⇒ String
Builds a Go test executable command string with optional profiling/trace/coverage flags.
Use this when passing a command string directly to spawn.
193 194 195 |
# File 'lib/nonnative.rb', line 193 def go_command(tools, output, exec, cmd, *params) Nonnative::GoExecutable.new(tools, exec, output).command(cmd, *params) end |
.grpc_health(host:, port:, service:, timeout: 1) ⇒ Nonnative::GRPCHealth
Returns a client helper for the standard gRPC health checking protocol.
240 |
# File 'lib/nonnative.rb', line 240 def grpc_health(host:, port:, service:, timeout: 1) = Nonnative::GRPCHealth.new(host: host, port: port, service: service, timeout: timeout) |
.log_lines(path, predicate) ⇒ Array<String>
Reads a file and returns only lines matching the given predicate.
179 180 181 |
# File 'lib/nonnative.rb', line 179 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.
170 171 172 |
# File 'lib/nonnative.rb', line 170 def logger @logger ||= Logger.new(configuration.log) end |
.observability ⇒ Nonnative::Observability
Returns an HTTP client for common health/readiness endpoints.
231 |
# File 'lib/nonnative.rb', line 231 def observability = (@observability ||= Nonnative::Observability.new(configuration.url)) |
.proxies ⇒ Hash{String=>Class}
Returns the configured proxy kinds mapped to proxy classes.
Consumers can extend this map to add custom proxy implementations.
247 248 249 |
# File 'lib/nonnative.rb', line 247 def proxies @proxies ||= { 'fault_injection' => Nonnative::FaultInjectionProxy } end |
.proxy(kind) ⇒ Class
259 260 261 |
# File 'lib/nonnative.rb', line 259 def proxy(kind) kind.nil? || kind == 'none' ? NoProxy : proxies.fetch(kind) { raise ArgumentError, "Unsupported proxy kind '#{kind}'" } end |
.reset ⇒ void
This method returns an undefined value.
Resets proxies for all currently started runners.
354 355 356 |
# File 'lib/nonnative.rb', line 354 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/ports.
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/nonnative.rb', line 269 def start @pool ||= Nonnative::Pool.new(configuration) errors = [] errors.concat(@pool.start do |name, values, result, ports| id, started = values errors << "Started #{name} with id #{id}, though did not respond in time for #{ports.description}" 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.
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/nonnative.rb', line 291 def stop errors = [] return if @pool.nil? errors.concat(@pool.stop do |name, values, result, ports| id, stopped = Array(values).then { |v| [v.first, v.fetch(1, true)] } errors << "Stopped #{name} with id #{id}, though did not respond in time for #{ports.description}" unless result errors << "Stopped #{name} with id #{id}, though the process did not exit in time" unless stopped end) nil rescue StandardError => e errors << unexpected_lifecycle_error(:stop, e) ensure raise Nonnative::StopError, errors.join("\n") unless errors.empty? end |
.token(kind:, issuer:, key:, private_key:, expiration:) ⇒ Nonnative::Token
Builds a token generator for authenticating against a service under test.
The signing parameters are passed in directly; this is not coupled to any service's configuration format. The generated token string is ready for Nonnative::Header.auth_bearer.
226 |
# File 'lib/nonnative.rb', line 226 def token(kind:, issuer:, key:, private_key:, expiration:) = Nonnative::Token.new(kind:, issuer:, key:, private_key:, expiration:) |