lbspec 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.coveralls.yml +1 -0
- data/.rubocop.yml +1 -0
- data/.travis.yml +4 -0
- data/README.md +8 -0
- data/Rakefile +19 -4
- data/lbspec.gemspec +3 -0
- data/lib/lbspec/transfer.rb +66 -33
- data/lib/lbspec/util.rb +18 -0
- data/lib/lbspec/version.rb +3 -1
- data/lib/lbspec.rb +3 -0
- data/rubocop-todo.yml +2 -0
- data/sample/lb_spec.rb +6 -0
- data/spec/lbspec_spec.rb +1 -4
- data/spec/lbspec_transfer_spec.rb +24 -1
- data/spec/spec_helper.rb +4 -0
- metadata +54 -2
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
data/.rubocop.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
inherit_from: rubocop-todo.yml
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -2,6 +2,9 @@
|
|
2
2
|
|
3
3
|
Lbspec is an RSpec plugin for easy Loadbalancer testing.
|
4
4
|
|
5
|
+
[](https://travis-ci.org/otahi/lbspec)
|
6
|
+
[](https://coveralls.io/r/otahi/lbspec?branch=master)
|
7
|
+
[](http://badge.fury.io/rb/lbspec)
|
5
8
|
## Installation
|
6
9
|
|
7
10
|
Add this line to your application's Gemfile:
|
@@ -46,6 +49,11 @@ end
|
|
46
49
|
describe 'vhost_b' do
|
47
50
|
it { should transfer(['node_b','node_c']) }
|
48
51
|
end
|
52
|
+
|
53
|
+
describe 'vhost_c:80' do
|
54
|
+
it { should transfer(['node_b','node_c']).port(80) }
|
55
|
+
end
|
56
|
+
|
49
57
|
```
|
50
58
|
|
51
59
|
## Contributing
|
data/Rakefile
CHANGED
@@ -1,6 +1,21 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
# -*- coding: utf-8 -*-
|
3
3
|
|
4
|
-
|
4
|
+
require 'rake'
|
5
5
|
|
6
|
-
task :default => :
|
6
|
+
task :default => :travis
|
7
|
+
task :travis => [:spec, :rubocop, 'coveralls:push']
|
8
|
+
|
9
|
+
require 'rspec/core/rake_task'
|
10
|
+
require 'rubocop/rake_task'
|
11
|
+
require 'coveralls/rake/task'
|
12
|
+
|
13
|
+
Coveralls::RakeTask.new
|
14
|
+
|
15
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
16
|
+
t.pattern = 'spec/**/*_spec.rb'
|
17
|
+
end
|
18
|
+
|
19
|
+
Rubocop::RakeTask.new(:rubocop) do |task|
|
20
|
+
task.patterns = %w(lib/**/*.rb spec/**/*.rb)
|
21
|
+
end
|
data/lbspec.gemspec
CHANGED
@@ -21,6 +21,9 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.5"
|
22
22
|
spec.add_development_dependency "rake"
|
23
23
|
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "rubocop"
|
25
|
+
spec.add_development_dependency "coveralls"
|
26
|
+
spec.add_development_dependency "debugger"
|
24
27
|
|
25
28
|
spec.add_runtime_dependency "rspec"
|
26
29
|
spec.add_runtime_dependency "net-ssh"
|
data/lib/lbspec/transfer.rb
CHANGED
@@ -1,74 +1,107 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
1
2
|
require 'net/ssh'
|
2
3
|
require 'rspec/expectations'
|
4
|
+
require 'lbspec'
|
3
5
|
|
4
6
|
RSpec::Matchers.define :transfer do |nodes|
|
5
7
|
@ssh = []
|
6
8
|
@threads = []
|
7
9
|
@nodes_connected = []
|
10
|
+
@vhost_port = 80
|
11
|
+
@node_port = 0
|
8
12
|
@result = false
|
13
|
+
Thread.abort_on_exception = true
|
9
14
|
|
10
15
|
match do |vhost|
|
11
|
-
gen_keyword
|
12
|
-
|
16
|
+
@keyword = gen_keyword
|
17
|
+
capture_on_nodes nodes
|
13
18
|
wait_nodes_connected nodes
|
14
|
-
send_request
|
19
|
+
send_request vhost
|
15
20
|
disconnect_nodes
|
16
|
-
|
21
|
+
@result
|
22
|
+
end
|
23
|
+
|
24
|
+
chain :port do |port|
|
25
|
+
@node_port = port
|
17
26
|
end
|
18
27
|
|
19
28
|
def gen_keyword
|
20
|
-
|
21
|
-
@keyword = t.to_i.to_s + t.nsec.to_s
|
29
|
+
Lbspec::Util.gen_keyword
|
22
30
|
end
|
23
31
|
|
24
32
|
def wait_nodes_connected(nodes)
|
25
|
-
nodes_length = 1
|
26
|
-
|
33
|
+
nodes_length = (nodes.respond_to? :each) ? nodes.length : 1
|
34
|
+
sleep 0.5 until @nodes_connected.length == nodes_length
|
35
|
+
end
|
27
36
|
|
28
|
-
|
29
|
-
|
37
|
+
def capture_on_nodes(nodes)
|
38
|
+
if nodes.respond_to? :each
|
39
|
+
nodes.each { |node| capture_on_node(node) }
|
40
|
+
else
|
41
|
+
capture_on_node(nodes)
|
30
42
|
end
|
31
43
|
end
|
32
44
|
|
33
|
-
def
|
34
|
-
|
35
|
-
|
36
|
-
@
|
45
|
+
def capture_on_node(node)
|
46
|
+
@threads << Thread.new do
|
47
|
+
Net::SSH.start(node, nil, :config => true) do |ssh|
|
48
|
+
@ssh << ssh
|
49
|
+
ssh.open_channel { |channel| run_check channel }
|
37
50
|
end
|
38
|
-
else
|
39
|
-
@threads << Thread.new { connect_node(nodes) }
|
40
51
|
end
|
41
52
|
end
|
42
53
|
|
43
|
-
def
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
54
|
+
def run_check(channel)
|
55
|
+
channel.request_pty do |chan, success|
|
56
|
+
fail 'Could not obtain pty' unless success
|
57
|
+
@nodes_connected.push(true)
|
58
|
+
exec_capture(chan, capture_cmd)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def exec_capture(channel, command)
|
63
|
+
channel.exec capture_cmd do |ch, stream, data|
|
64
|
+
num_match = 0
|
65
|
+
ch.on_data do |c, d|
|
66
|
+
# because of ngrep output condition first
|
67
|
+
# to avoid incorrect match, count 2
|
68
|
+
num_match += 1 if /#{@keyword}/ =~ d
|
69
|
+
@result = true if num_match > 1
|
55
70
|
end
|
56
71
|
end
|
57
72
|
end
|
58
73
|
|
74
|
+
def capture_cmd
|
75
|
+
port_str = @node_port > 0 ? "port #{@node_port}" : ''
|
76
|
+
"sudo ngrep #{@keyword} #{port_str}"
|
77
|
+
end
|
78
|
+
|
59
79
|
def disconnect_nodes
|
80
|
+
sleep 1
|
60
81
|
@threads.each do |t|
|
61
82
|
t.kill
|
62
83
|
end
|
63
84
|
@ssh.each do |ssh|
|
64
|
-
ssh.close
|
85
|
+
ssh.close unless ssh.closed?
|
65
86
|
end
|
66
87
|
end
|
67
88
|
|
68
|
-
def send_request(vhost
|
69
|
-
|
89
|
+
def send_request(vhost)
|
90
|
+
addr_port = Lbspec::Util.split_addr_port(vhost.to_s)
|
91
|
+
vhost_addr, vhost_port = addr_port[:addr], addr_port[:port]
|
92
|
+
@vhost_port = vhost_port if vhost_port > 0
|
93
|
+
system("echo #{@keyword} | nc #{vhost_addr} #{@vhost_port}")
|
70
94
|
end
|
71
|
-
|
72
|
-
|
95
|
+
|
96
|
+
description do
|
97
|
+
"transfer requests to #{nodes}."
|
98
|
+
end
|
99
|
+
|
100
|
+
failure_message_for_should do |vhost|
|
101
|
+
"expected #{vhost} to transfer requests to #{nodes}, but did not."
|
102
|
+
end
|
103
|
+
|
104
|
+
failure_message_for_should_not do |vhost|
|
105
|
+
"expected #{vhost} not to transfer requests to #{nodes}, but it did."
|
73
106
|
end
|
74
107
|
end
|
data/lib/lbspec/util.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# Lbspec is an RSpec plugin for easy Loadbalancer testing.
|
3
|
+
module Lbspec
|
4
|
+
# Lbspec::Util provides some utilities
|
5
|
+
class Util
|
6
|
+
def self.gen_keyword
|
7
|
+
t = Time.now
|
8
|
+
t.to_i.to_s + t.nsec.to_s
|
9
|
+
end
|
10
|
+
def self.split_addr_port(addr_port_str)
|
11
|
+
port = 0
|
12
|
+
splits = addr_port_str.split(':')
|
13
|
+
addr = splits.first
|
14
|
+
port = splits.last.to_i if /\d+/ =~ splits.last
|
15
|
+
{ :addr => addr, :port => port }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/lbspec/version.rb
CHANGED
data/lib/lbspec.rb
CHANGED
data/rubocop-todo.yml
ADDED
data/sample/lb_spec.rb
CHANGED
@@ -3,4 +3,10 @@ require_relative 'spec_helper'
|
|
3
3
|
describe 'vhost_a' do
|
4
4
|
it { should transfer('node_a') }
|
5
5
|
it { should transfer(['node_c','node_b']) }
|
6
|
+
it 'should test transfer a node on port 80' do
|
7
|
+
'vhost_a'.should transfer('node_a:80')
|
8
|
+
end
|
9
|
+
it 'should test transfer a node on port 8080' do
|
10
|
+
'vhost_a'.should_not transfer('node_a:8080')
|
11
|
+
end
|
6
12
|
end
|
data/spec/lbspec_spec.rb
CHANGED
@@ -1,12 +1,35 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
1
2
|
require 'spec_helper'
|
3
|
+
require 'net/ssh'
|
2
4
|
|
3
5
|
describe Lbspec do
|
4
6
|
describe '#transfer' do
|
7
|
+
before(:each) do
|
8
|
+
key = Lbspec::Util.gen_keyword
|
9
|
+
Lbspec::Util.stub(:gen_keyword).and_return(key)
|
10
|
+
channel_connected = double('channel_connected')
|
11
|
+
channel_connected.stub(:request_pty).and_yield(channel_connected, true)
|
12
|
+
channel_connected.stub(:exec).and_yield(channel_connected, nil, nil)
|
13
|
+
channel_connected.stub(:on_data).and_yield(nil, key).and_yield(nil, key)
|
14
|
+
ssh_connected = double('ssh_connected')
|
15
|
+
ssh_connected.stub(:open_channel).and_yield(channel_connected)
|
16
|
+
ssh_connected.stub(:closed?).and_return(false)
|
17
|
+
ssh_connected.stub(:close)
|
18
|
+
Net::SSH.stub(:start).and_yield(ssh_connected).and_return(ssh_connected)
|
19
|
+
Kernel.stub(:system).and_return true
|
20
|
+
end
|
21
|
+
|
5
22
|
it 'should test transfer a node' do
|
6
23
|
'vhost_a'.should transfer('node_a')
|
7
24
|
end
|
8
25
|
it 'should test transfer nodes' do
|
9
|
-
'vhost_a'.should transfer(
|
26
|
+
'vhost_a'.should transfer(%w{node_a node_b})
|
27
|
+
end
|
28
|
+
it 'should test transfer a node on port 80' do
|
29
|
+
'vhost_a'.should transfer('node_a').port(80)
|
30
|
+
end
|
31
|
+
it 'should test transfer vhost:80 and a node on port 80' do
|
32
|
+
'vhost_a:80'.should transfer('node_a').port(80)
|
10
33
|
end
|
11
34
|
end
|
12
35
|
end
|
data/spec/spec_helper.rb
CHANGED
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.0.
|
4
|
+
version: 0.0.2
|
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-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -59,6 +59,54 @@ dependencies:
|
|
59
59
|
- - ! '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rubocop
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: coveralls
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: debugger
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
62
110
|
- !ruby/object:Gem::Dependency
|
63
111
|
name: rspec
|
64
112
|
requirement: !ruby/object:Gem::Requirement
|
@@ -98,8 +146,10 @@ executables: []
|
|
98
146
|
extensions: []
|
99
147
|
extra_rdoc_files: []
|
100
148
|
files:
|
149
|
+
- .coveralls.yml
|
101
150
|
- .gitignore
|
102
151
|
- .rspec
|
152
|
+
- .rubocop.yml
|
103
153
|
- .travis.yml
|
104
154
|
- Gemfile
|
105
155
|
- LICENSE
|
@@ -109,7 +159,9 @@ files:
|
|
109
159
|
- lbspec.gemspec
|
110
160
|
- lib/lbspec.rb
|
111
161
|
- lib/lbspec/transfer.rb
|
162
|
+
- lib/lbspec/util.rb
|
112
163
|
- lib/lbspec/version.rb
|
164
|
+
- rubocop-todo.yml
|
113
165
|
- sample/lb_spec.rb
|
114
166
|
- sample/spec_helper.rb
|
115
167
|
- spec/lbspec_spec.rb
|