lbspec 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/lbspec/transfer.rb +35 -38
- data/lib/lbspec/util.rb +21 -2
- data/lib/lbspec/version.rb +1 -1
- data/spec/lbspec_transfer_spec.rb +14 -5
- metadata +4 -4
data/lib/lbspec/transfer.rb
CHANGED
@@ -23,6 +23,8 @@ RSpec::Matchers.define :transfer do |nodes|
|
|
23
23
|
@vhost_port = 80
|
24
24
|
@node_port = 0
|
25
25
|
@request_node = nil
|
26
|
+
@include_str = nil
|
27
|
+
@output_request = ''
|
26
28
|
@options = {}
|
27
29
|
|
28
30
|
@capture_command = lambda do |port, prove|
|
@@ -39,41 +41,14 @@ RSpec::Matchers.define :transfer do |nodes|
|
|
39
41
|
.exec_command("echo #{prove} | nc #{addr} #{port}", @request_node)
|
40
42
|
end
|
41
43
|
@http_request_command = lambda do |addr, port, path, prove|
|
42
|
-
env, opt = '', ''
|
43
|
-
opt << (@options[:timeout] ? " -m #{@options[:timeout]}" : '')
|
44
|
-
opt << (@options[:proxy] ? %Q( -x "#{@options[:proxy]}") : '')
|
45
|
-
if @options[:noproxy]
|
46
|
-
env << %Q( no_proxy="#{@options[:noproxy]}")
|
47
|
-
env << %Q( NO_PROXY="#{@options[:noproxy]}")
|
48
|
-
end
|
49
|
-
if @options[:header]
|
50
|
-
header = @options[:header]
|
51
|
-
header = [header] unless header.respond_to? :each
|
52
|
-
header.each { |h| opt << %Q( -H '#{h}') }
|
53
|
-
end
|
54
44
|
uri = 'http://' + "#{addr}:#{port}#{path}?#{prove}"
|
55
|
-
Lbspec::Util
|
56
|
-
|
57
|
-
@request_node)
|
45
|
+
command = Lbspec::Util.build_curl_command(uri, @options)
|
46
|
+
Lbspec::Util.exec_command(command, @request_node)
|
58
47
|
end
|
59
48
|
@https_request_command = lambda do |addr, port, path, prove|
|
60
|
-
env, opt = '', ''
|
61
|
-
opt << (@options[:timeout] ? " -m #{@options[:timeout]}" : '')
|
62
|
-
opt << (@options[:ignore_valid_ssl] ? ' -k' : '')
|
63
|
-
opt << (@options[:proxy] ? %Q( -x "#{@options[:proxy]}") : '')
|
64
|
-
if @options[:noproxy]
|
65
|
-
env << %Q( no_proxy="#{@options[:noproxy]}")
|
66
|
-
env << %Q( NO_PROXY="#{@options[:noproxy]}")
|
67
|
-
end
|
68
|
-
if @options[:header]
|
69
|
-
header = @options[:header]
|
70
|
-
header = [header] unless header.respond_to? :each
|
71
|
-
header.each { |h| opt << %Q( -H '#{h}') }
|
72
|
-
end
|
73
49
|
uri = 'https://' + "#{addr}:#{port}#{path}?#{prove}"
|
74
|
-
Lbspec::Util
|
75
|
-
|
76
|
-
@request_node)
|
50
|
+
command = Lbspec::Util.build_curl_command(uri, @options)
|
51
|
+
Lbspec::Util.exec_command(command, @request_node)
|
77
52
|
end
|
78
53
|
|
79
54
|
@result = false
|
@@ -126,6 +101,11 @@ RSpec::Matchers.define :transfer do |nodes|
|
|
126
101
|
@chain_str << " from #{from}"
|
127
102
|
end
|
128
103
|
|
104
|
+
chain :include do |str|
|
105
|
+
@include_str = str
|
106
|
+
@chain_str << " including #{str}"
|
107
|
+
end
|
108
|
+
|
129
109
|
chain :options do |options|
|
130
110
|
@options = options
|
131
111
|
end
|
@@ -176,14 +156,25 @@ RSpec::Matchers.define :transfer do |nodes|
|
|
176
156
|
def exec_capture(channel)
|
177
157
|
command = capture_command(@node_port, @prove)
|
178
158
|
channel.exec command do |ch, stream, data|
|
179
|
-
|
159
|
+
whole_data = ''
|
180
160
|
ch.on_data do |c, d|
|
181
|
-
|
182
|
-
|
161
|
+
whole_data << d
|
162
|
+
patterns = [@prove]
|
163
|
+
patterns << @include_str if @include_str
|
164
|
+
@result = match_all?(whole_data, patterns)
|
183
165
|
end
|
184
166
|
end
|
185
167
|
end
|
186
168
|
|
169
|
+
def match_all?(string, patterns)
|
170
|
+
num_patterns, num_match = 0, 0
|
171
|
+
patterns.each do |pat|
|
172
|
+
num_patterns += 1
|
173
|
+
num_match += 1 if /#{pat}/ =~ string
|
174
|
+
end
|
175
|
+
num_match == num_patterns
|
176
|
+
end
|
177
|
+
|
187
178
|
def capture_command(port, prove)
|
188
179
|
@capture_command[port, prove]
|
189
180
|
end
|
@@ -202,9 +193,11 @@ RSpec::Matchers.define :transfer do |nodes|
|
|
202
193
|
vhost_addr, vhost_port = addr_port[:addr], addr_port[:port]
|
203
194
|
@vhost_port = vhost_port if vhost_port > 0
|
204
195
|
if @application
|
205
|
-
|
196
|
+
@output_request =
|
197
|
+
send_request_application(vhost_addr, @vhost_port, @prove)
|
206
198
|
else
|
207
|
-
|
199
|
+
@output_request =
|
200
|
+
send_request_transport(vhost_addr, @vhost_port, @prove)
|
208
201
|
end
|
209
202
|
end
|
210
203
|
|
@@ -234,13 +227,17 @@ RSpec::Matchers.define :transfer do |nodes|
|
|
234
227
|
result = "expected #{vhost} to transfer requests to"
|
235
228
|
result << nodes.to_s
|
236
229
|
result << @chain_str
|
237
|
-
result <<
|
230
|
+
result << ", but did not.\n"
|
231
|
+
result << "requested:\n"
|
232
|
+
result << @output_request
|
238
233
|
end
|
239
234
|
|
240
235
|
failure_message_for_should_not do |vhost|
|
241
236
|
result = "expected #{vhost} not to transfer requests to"
|
242
237
|
result << nodes.to_s
|
243
238
|
result << @chain_str
|
244
|
-
result <<
|
239
|
+
result << ", but did.\n"
|
240
|
+
result << "requested:\n"
|
241
|
+
result << @output_request
|
245
242
|
end
|
246
243
|
end
|
data/lib/lbspec/util.rb
CHANGED
@@ -14,13 +14,32 @@ module Lbspec
|
|
14
14
|
port = splits.last.to_i if /\d+/ =~ splits.last
|
15
15
|
{ addr: addr, port: port }
|
16
16
|
end
|
17
|
+
def self.build_curl_command(uri, options)
|
18
|
+
env, opt = '', ''
|
19
|
+
opt << (options[:timeout] ? " -m #{options[:timeout]}" : '')
|
20
|
+
opt << (options[:ignore_valid_ssl] ? ' -k' : '')
|
21
|
+
opt << (options[:proxy] ? %Q( -x "#{options[:proxy]}") : '')
|
22
|
+
if options[:noproxy]
|
23
|
+
env << %Q( no_proxy="#{options[:noproxy]}")
|
24
|
+
env << %Q( NO_PROXY="#{options[:noproxy]}")
|
25
|
+
end
|
26
|
+
opt << header_option(options[:header])
|
27
|
+
%Q(#{env} curl -o /dev/null -s #{opt} '#{uri}')
|
28
|
+
end
|
29
|
+
def self.header_option(header)
|
30
|
+
opt = ''
|
31
|
+
header = [header] unless header.respond_to? :each
|
32
|
+
header.each { |h| opt << %Q( -H '#{h}') }
|
33
|
+
opt
|
34
|
+
end
|
17
35
|
def self.exec_command(command, node = nil)
|
36
|
+
output = command
|
18
37
|
if node
|
19
38
|
Net::SSH.start(node, nil, config: true) do |ssh|
|
20
|
-
ssh.exec!(command)
|
39
|
+
output << ssh.exec!(command).to_s
|
21
40
|
end
|
22
41
|
else
|
23
|
-
`#{command}
|
42
|
+
output << `#{command}`.to_s
|
24
43
|
end
|
25
44
|
end
|
26
45
|
end
|
data/lib/lbspec/version.rb
CHANGED
@@ -6,11 +6,14 @@ describe Lbspec do
|
|
6
6
|
describe '#transfer' do
|
7
7
|
before(:each) do
|
8
8
|
key = Lbspec::Util.create_prove
|
9
|
+
include_str = 'X-Test: 1'
|
9
10
|
Lbspec::Util.stub(:create_prove).and_return(key)
|
10
11
|
channel_connected = double('channel_connected')
|
11
12
|
channel_connected.stub(:request_pty).and_yield(channel_connected, true)
|
12
13
|
channel_connected.stub(:exec).and_yield(channel_connected, nil, nil)
|
13
|
-
channel_connected.stub(:on_data)
|
14
|
+
channel_connected.stub(:on_data)
|
15
|
+
.and_yield(nil, key + include_str)
|
16
|
+
.and_yield(nil, key + include_str)
|
14
17
|
ssh_connected = double('ssh_connected')
|
15
18
|
ssh_connected.stub(:open_channel).and_yield(channel_connected)
|
16
19
|
ssh_connected.stub(:closed?).and_return(false)
|
@@ -60,6 +63,12 @@ describe Lbspec do
|
|
60
63
|
'vhost_a:443'.should transfer('node_a')
|
61
64
|
.https.path('/test').from('node_a')
|
62
65
|
end
|
66
|
+
it 'should test transfer vhost:80 and a node with http includes ' do
|
67
|
+
'vhost_a:80'.should transfer('node_a').http
|
68
|
+
.include('X-Test: 1')
|
69
|
+
'vhost_a:80'.should transfer('node_a').http
|
70
|
+
.include(/Test:/)
|
71
|
+
end
|
63
72
|
|
64
73
|
describe 'request_command' do
|
65
74
|
it 'should create single header options for http' do
|
@@ -68,7 +77,7 @@ describe Lbspec do
|
|
68
77
|
Lbspec::Util.should_not_receive(:exec_command)
|
69
78
|
.with(/ -H.* -H/, nil)
|
70
79
|
'vhost_a:443'.should transfer('node_a').http
|
71
|
-
.options(
|
80
|
+
.options(header: 'X-Test1:1')
|
72
81
|
end
|
73
82
|
it 'should create single header options for https' do
|
74
83
|
Lbspec::Util.should_receive(:exec_command)
|
@@ -76,19 +85,19 @@ describe Lbspec do
|
|
76
85
|
Lbspec::Util.should_not_receive(:exec_command)
|
77
86
|
.with(/ -H.* -H/, nil)
|
78
87
|
'vhost_a:443'.should transfer('node_a').https
|
79
|
-
.options(
|
88
|
+
.options(header: 'X-Test1:1')
|
80
89
|
end
|
81
90
|
it 'should create multi header options for http' do
|
82
91
|
Lbspec::Util.should_receive(:exec_command)
|
83
92
|
.with(/ -H.* -H/, nil)
|
84
93
|
'vhost_a:443'.should transfer('node_a').http
|
85
|
-
.options(
|
94
|
+
.options(header: %w(X-Test1:1 X-Test2:2))
|
86
95
|
end
|
87
96
|
it 'should create multi header options for https' do
|
88
97
|
Lbspec::Util.should_receive(:exec_command)
|
89
98
|
.with(/ -H.* -H/, nil)
|
90
99
|
'vhost_a:443'.should transfer('node_a').https
|
91
|
-
.options(
|
100
|
+
.options(header: %w(X-Test1:1 X-Test2:2))
|
92
101
|
end
|
93
102
|
end
|
94
103
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lbspec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-03-
|
12
|
+
date: 2014-03-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -184,7 +184,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
184
184
|
version: '0'
|
185
185
|
segments:
|
186
186
|
- 0
|
187
|
-
hash: -
|
187
|
+
hash: -4298291710759407848
|
188
188
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
189
189
|
none: false
|
190
190
|
requirements:
|
@@ -193,7 +193,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
193
193
|
version: '0'
|
194
194
|
segments:
|
195
195
|
- 0
|
196
|
-
hash: -
|
196
|
+
hash: -4298291710759407848
|
197
197
|
requirements: []
|
198
198
|
rubyforge_project:
|
199
199
|
rubygems_version: 1.8.21
|