Class: Helpers::TestInstance

Inherits:
Object
  • Object
show all
Includes:
Connections, MetadataPassthrough
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/etcdv3-0.11.5/spec/helpers/test_instance.rb

Defined Under Namespace

Classes: InvalidVersionException, PortInUseException

Constant Summary collapse

MINIMUM_VERSION =
Gem::Version.new('3.0.0')

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MetadataPassthrough

#expect_metadata_passthrough, #expect_metadata_passthrough_namespace

Methods included from Connections

#full_local_url, #local_connection, #local_connection_with_auth, #local_connection_with_namespace, #local_connection_with_timeout, #local_namespace_stub, #local_namespace_stub_with_metadata, #local_stub, #local_stub_with_metadata, #local_url, #port

Constructor Details

#initializeTestInstance

Returns a new instance of TestInstance.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/etcdv3-0.11.5/spec/helpers/test_instance.rb', line 20

def initialize
  @pids = []
  @tmpdir = Dir.mktmpdir
  @bin = discover_binary_path
  @version = discover_binary_version

  raise InvalidVersionException if @version < MINIMUM_VERSION
  raise PortInUseException if port_open?

rescue InvalidVersionException
  puts "Invalid Etcd Version: #{@version}. Must be running 3.0+"
  exit(1)
rescue PortInUseException
  puts "Port #{port} is already in use. To choose a new port: `export ETCD_TEST_PORT=new_port`"
  exit(1)
end

Instance Attribute Details

#versionObject

Returns the value of attribute version.



18
19
20
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/etcdv3-0.11.5/spec/helpers/test_instance.rb', line 18

def version
  @version
end

Instance Method Details

#spawn_etcd_instanceObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/etcdv3-0.11.5/spec/helpers/test_instance.rb', line 44

def spawn_etcd_instance
  peer_url = "http://127.0.0.1:#{port+1}"
  client_url = "http://127.0.0.1:#{port}"
  cluster_url = "node=http://127.0.0.1:#{port+1}"
  flags =  ' --name=node'
  flags << " --initial-advertise-peer-urls=#{peer_url}"
  flags << " --listen-peer-urls=#{peer_url}"
  flags << " --listen-client-urls=#{client_url}"
  flags << " --advertise-client-urls=#{client_url}"
  flags << " --initial-cluster=#{cluster_url}"
  flags << " --data-dir=#{@tmpdir} "

  # Assumes etcd is in PATH
  command = "ETCDCTL_API=3 #{@bin} " + flags
  pid = spawn(command, out: '/dev/null', err: '/dev/null')
  Process.detach(pid)
  pid
end

#startObject



37
38
39
40
41
42
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/etcdv3-0.11.5/spec/helpers/test_instance.rb', line 37

def start
  raise "Already running etcd servers(#{@pids.inspect})" unless @pids.empty?
  puts "Starting up testing environment on port #{port}..."
  @pids << spawn_etcd_instance
  sleep(5)
end

#stopObject



63
64
65
66
67
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/etcdv3-0.11.5/spec/helpers/test_instance.rb', line 63

def stop
  @pids.each { |pid| Process.kill('TERM', pid) } rescue nil
  FileUtils.remove_entry_secure(@tmpdir, true)
  @pids.clear
end