Class: VagrantTestSubject::VM

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-test-subject.rb,
lib/vagrant-test-subject/os/omnios.rb,
lib/vagrant-test-subject/os/redhat.rb

Direct Known Subclasses

OmniOS, RedHat

Defined Under Namespace

Classes: OmniOS, RedHat

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vm_name = 'default') ⇒ VM

Returns a new instance of VM.



39
40
41
42
43
44
45
# File 'lib/vagrant-test-subject.rb', line 39

def initialize(vm_name = 'default')
  @vm_name = vm_name
  @vbox_guid = VM.read_vbox_guid(vm_name)
  @vm_info = `VBoxManage showvminfo #{@vbox_guid} --machinereadable`.split("\n")
  init_port_map()
  @ssh = VagrantTestSubject::SSH.new(vm_name)
end

Instance Attribute Details

#external_ipObject (readonly)

Returns the value of attribute external_ip.



22
23
24
# File 'lib/vagrant-test-subject.rb', line 22

def external_ip
  @external_ip
end

#sshObject (readonly)

Returns the value of attribute ssh.



22
23
24
# File 'lib/vagrant-test-subject.rb', line 22

def ssh
  @ssh
end

#vbox_guidObject (readonly)

Returns the value of attribute vbox_guid.



22
23
24
# File 'lib/vagrant-test-subject.rb', line 22

def vbox_guid
  @vbox_guid
end

Class Method Details

.attach(vm_name = 'default') ⇒ Object

Factory method, reads VM type and then instantiates



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/vagrant-test-subject.rb', line 25

def self.attach(vm_name = 'default')
  os_type = VM.read_vbox_os_type(vm_name)

  # Yecch
  vbox_os_to_class = {
    'RedHat_64' => VagrantTestSubject::VM::RedHat,
    'Red Hat (64 bit)' => VagrantTestSubject::VM::RedHat,
    'OpenSolaris_64' => VagrantTestSubject::VM::OmniOS,
  }

  klass = vbox_os_to_class[os_type]
  klass.new(vm_name)
end

Instance Method Details

#has_matching_process_listening?(ip, vm_port, process_regex) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/vagrant-test-subject.rb', line 91

def has_matching_process_listening? (ip, vm_port, process_regex)
  raise "pure virtual"
end

#has_running_service?(service_name) ⇒ Boolean

#

Testing Predicates

#

Returns:

  • (Boolean)


77
78
79
# File 'lib/vagrant-test-subject.rb', line 77

def has_running_service? (service_name)
  raise "pure virtual"
end

#http_get(local_absolute_url) ⇒ Object

Returns a Net::HTTP::Response



64
65
66
67
# File 'lib/vagrant-test-subject.rb', line 64

def http_get (local_absolute_url)
  uri = URI('http://localhost:' + self.map_port(80).to_s + local_absolute_url)
  response = Net::HTTP.get_response(uri)
end

#https_get(local_absolute_url, opts = {}) ⇒ Object



68
69
70
71
72
# File 'lib/vagrant-test-subject.rb', line 68

def https_get (local_absolute_url, opts={})
  # TODO - set OpenSSL::SSL::VERIFY_PEER based on opts ?
  uri = URI('http://localhost:' + self.map_port(443).to_s + local_absolute_url)
  response = Net::HTTP.get_response(uri)
end

#list_internal_portsObject

#
VM Info Methods
#


51
52
53
# File 'lib/vagrant-test-subject.rb', line 51

def list_internal_ports 
  return @port_map.keys.sort
end

#listening_on_external_ip?(vm_port) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
# File 'lib/vagrant-test-subject.rb', line 86

def listening_on_external_ip? (vm_port)
  vm_port = vm_port.to_i
  self.listening_ports.find{|lp| [@external_ip, '0.0.0.0', '*'].include?(lp[:ip]) && lp[:port] == vm_port }
end

#listening_on_localhost?(vm_port) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
# File 'lib/vagrant-test-subject.rb', line 81

def listening_on_localhost? (vm_port)
  vm_port = vm_port.to_i
  self.listening_ports.find{|lp| ['127.0.0.1', '0.0.0.0', '*'].include?(lp[:ip]) && lp[:port] == vm_port }
end

#listening_on_portmap?(vm_port) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/vagrant-test-subject.rb', line 95

def listening_on_portmap?(vm_port)
  host_port = map_port(vm_port)
  
  # This doesn't work - always connects, even to a port that isn't listening.
  return false

  begin
    #Timeout::timeout(1) do
      begin
        puts "Trying VM host port " + host_port.to_s
        binding.pry
        s = TCPSocket.new('127.0.0.1', host_port )
        s.close
        return true
      rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
        return false
      end
    #end
  #rescue Timeout::Error
  end
  return false
end

#map_port(internal) ⇒ Object



55
56
57
# File 'lib/vagrant-test-subject.rb', line 55

def map_port(internal) 
  return @port_map[internal.to_i]
end