Class: Console::Conversation

Inherits:
Object
  • Object
show all
Includes:
Expectancies
Defined in:
lib/console/conversation.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Expectancies

#connected, #logged_in, #logout, #post_uname, #waiting_shell

Constructor Details

#initialize(domain) ⇒ Conversation

Returns a new instance of Conversation.



6
7
8
9
# File 'lib/console/conversation.rb', line 6

def initialize(domain)
		shell = PTY.spawn "virsh console #{domain}"	
		@o,@i,@pid = shell[0],shell[1],shell[2]
end

Class Method Details

.talk_with(domain, &block) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/console/conversation.rb', line 11

def self.talk_with(domain,&block)
		conv = Conversation.new(domain)
		begin
   conv.instance_eval(&block)
		ensure 
   conv.end_talk
		end
end

Instance Method Details

#end_talkObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/console/conversation.rb', line 40

def end_talk
		enter '[^'
		@o.close
		@i.close
		begin
   Process.wait @pid
		rescue PTY::ChildExited
		rescue Errno::ECHILD 
   raise Exception.new 'console process not started, make sure domain is up and running'
		end
end

#enter(input) ⇒ Object



20
21
22
23
# File 'lib/console/conversation.rb', line 20

def enter(input)
		@i.puts(input)
		@i.flush
end

#exepecting(expected) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/console/conversation.rb', line 25

def exepecting(expected)
		output =''
		begin
   FirePoll.poll(nil,5) do
  output << read
  output.eql?(expected)
   end 	
		rescue 
   puts 'diff exists between expected input and actual output:'
   puts Differ.diff_by_char expected, output
		end
		puts output
		output
end

#readObject



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/console/conversation.rb', line 52

def read 
		buffer = ""
		begin
   loop { buffer << @o.read_nonblock(10)}
		rescue Errno::EAGAIN
   # nothing to read now
		rescue EOFError
   # read was done
		end
		buffer
end