/
http_spec.rb
136 lines (116 loc) · 3.2 KB
/
http_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Copyright (c) 2009-2012 VMware, Inc.
require File.dirname(__FILE__) + '/../spec_helper'
require 'posix/spawn'
require 'yajl'
describe "http messages" do
def http(method, args=nil)
uri = URI.parse(@http_uri)
req = Net::HTTP::Post.new(uri.request_uri)
req.basic_auth(@user, @pass)
hash = {:reply_to => 'rspec', :method => method}
hash['arguments'] = args if args
req.body = Yajl::Encoder.encode(hash)
Net::HTTP.start(uri.host, uri.port) do |http|
response = http.request(req)
msg = Yajl::Parser.parse(response.body)
if msg.has_key?('value')
yield msg['value']
else
yield msg
end
end
end
def http_up?
uri = URI.parse(@http_uri)
req = Net::HTTP::Post.new(uri.request_uri)
req.basic_auth(@user, @pass)
Net::HTTP.start(uri.host, uri.port) do |http|
response = http.request(req)
return response.is_a?(Net::HTTPSuccess)
end
rescue Errno::ECONNREFUSED
false
end
before(:all) do
@user = "http"
@pass = @user.reverse
@port = get_free_port
@http_uri = "http://#{@user}:#{@pass}@localhost:#{@port}/agent"
@agent_id = "rspec_agent"
puts "starting http agent"
agent = File.expand_path("../../../bin/agent", __FILE__)
@basedir = File.expand_path("../../../tmp", __FILE__)
FileUtils.mkdir_p(@basedir) unless Dir.exist?(@basedir)
command = "ruby #{agent} -n #{@http_uri} -a #{@agent_id} -h 1 -b #{@basedir} -l ERROR"
@agent_pid = POSIX::Spawn::spawn(command)
counter = 0
while !http_up?
counter += 1
# wait max 10 seconds for the agent to start
raise "unable to connect to agent" if counter > 100
sleep 0.1
end
end
it "should respond to state message" do
http('state') do |msg|
msg.should have_key('deployment')
msg.should have_key('networks')
msg.should have_key('resource_pool')
msg.should have_key('agent_id')
msg.should have_key('vm')
msg.should have_key('job_state')
end
end
it "should respond to ping message" do
http('ping') do |msg|
msg.should == 'pong'
end
end
it "should respond noop message" do
http('noop') do |msg|
msg.should == 'nope'
end
end
it "should respond to start message" do
http('start') do |msg|
msg.should == 'started'
end
end
it "should respond to drain message" do
http('drain', ['shutdown', 'bar']) do |msg|
msg.should == 0
end
end
it "should respond to stop message" do
http('stop') do |msg|
msg.should == 'stopped'
end
end
it "should respond to apply message" do
task = nil
http('apply', []) do |msg|
task = msg
task['state'].should == "running"
task['agent_task_id'].should_not be_nil
end
while task['state'] == "running"
sleep 0.5
http('get_task', [ task['agent_task_id'] ]) do |msg|
task = msg
unless task['state']
msg.should have_key('exception')
break
end
end
end
end
after(:all) do
if @agent_pid
puts "stopping agent"
Process.kill(:TERM, @agent_pid)
else
raise "unable to stop agent, you need to clean up by hand"
end
FileUtils.rm_rf(@basedir)
end
end