lbspec 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -1,3 +1,7 @@
1
1
  language: ruby
2
+
3
+ script: "rake travis"
4
+
2
5
  rvm:
3
6
  - 1.9.3
7
+ - 2.0.0
data/README.md CHANGED
@@ -2,6 +2,9 @@
2
2
 
3
3
  Lbspec is an RSpec plugin for easy Loadbalancer testing.
4
4
 
5
+ [![Build Status](https://travis-ci.org/otahi/lbspec.png?branch=master)](https://travis-ci.org/otahi/lbspec)
6
+ [![Coverage Status](https://coveralls.io/repos/otahi/lbspec/badge.png?branch=master)](https://coveralls.io/r/otahi/lbspec?branch=master)
7
+ [![Gem Version](https://badge.fury.io/rb/lbspec.png)](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
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ #!/usr/bin/env rake
2
+ # -*- coding: utf-8 -*-
3
3
 
4
- RSpec::Core::RakeTask.new(:spec)
4
+ require 'rake'
5
5
 
6
- task :default => :spec
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"
@@ -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
- connect_nodes nodes
16
+ @keyword = gen_keyword
17
+ capture_on_nodes nodes
13
18
  wait_nodes_connected nodes
14
- send_request(vhost, 80)
19
+ send_request vhost
15
20
  disconnect_nodes
16
- is_ok?
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
- t = Time.now
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
- nodes_length = nodes.length if nodes.respond_to? :each
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
- until (@nodes_connected.length == nodes_length) do
29
- sleep 0.5
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 connect_nodes(nodes)
34
- if nodes.respond_to? :each
35
- nodes.each do |node|
36
- @threads << Thread.new { connect_node(node) }
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 connect_node(node)
44
- @ssh.push = Net::SSH.start(node, nil, :config => true) do |ssh|
45
- ssh.open_channel do |ch|
46
- ch.request_pty do |ch, success|
47
- raise "Could not obtain pty " if !success
48
- @nodes_connected.push(true)
49
- ch.exec "sudo ngrep #{@keyword}" do |ch, stream, data|
50
- ch.on_data do |c, data|
51
- @result = true if /#{@keyword}/ === data
52
- end
53
- end
54
- end
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 if ! ssh.closed?
85
+ ssh.close unless ssh.closed?
65
86
  end
66
87
  end
67
88
 
68
- def send_request(vhost, port)
69
- system("echo #{@keyword} | nc #{vhost} #{port}")
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
- def is_ok?
72
- @result
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
@@ -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
@@ -1,3 +1,5 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # Lbspec is an RSpec plugin for easy Loadbalancer testing.
1
3
  module Lbspec
2
- VERSION = "0.0.1"
4
+ VERSION = '0.0.2'
3
5
  end
data/lib/lbspec.rb CHANGED
@@ -1,5 +1,8 @@
1
+ # -*- encoding: utf-8 -*-
1
2
  require 'lbspec/version'
2
3
  require 'lbspec/transfer'
4
+ require 'lbspec/util'
3
5
 
6
+ # Lbspec is an RSpec plugin for easy Loadbalancer testing.
4
7
  module Lbspec
5
8
  end
data/rubocop-todo.yml ADDED
@@ -0,0 +1,2 @@
1
+ HashSyntax:
2
+ EnforcedStyle: hash_rockets
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,11 +1,8 @@
1
+ # -*- encoding: utf-8 -*-
1
2
  require 'spec_helper'
2
3
 
3
4
  describe Lbspec do
4
5
  it 'should have a version number' do
5
6
  Lbspec::VERSION.should_not be_nil
6
7
  end
7
-
8
- it 'should do something useful' do
9
- false.should eq(true)
10
- end
11
8
  end
@@ -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(['node_a','node_b'])
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
@@ -1,2 +1,6 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'coveralls'
3
+ Coveralls.wear!
4
+
1
5
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
6
  require 'lbspec'
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.1
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-07 00:00:00.000000000 Z
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