igp 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/igp/shell.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'uri'
2
- class Igp::Shell
3
2
 
3
+ # class that groks the igp command line options and invokes the ping task
4
+ class Igp::Shell
4
5
  # holds the parsed options
5
6
  attr_reader :options
6
7
  # holds the URI object representing the ping target
@@ -12,12 +13,13 @@ class Igp::Shell
12
13
  #
13
14
  # +args+ is the remaining command line arguments
14
15
  #
15
- def initialize(options,args)
16
+ def initialize(options, args)
16
17
  defaults = {
17
- :interval => 1
18
+ interval: 1
18
19
  }
19
- @options = defaults.merge( (options||{}).each{|k|k} )
20
+ @options = defaults.merge((options || {}).each { |k, v| { k => v } })
20
21
  return unless args.first
22
+
21
23
  resolve_addressing args.first
22
24
  normalise_options
23
25
  end
@@ -30,7 +32,7 @@ class Igp::Shell
30
32
  unless uri.scheme
31
33
  @options[:type] = :icmp
32
34
  @options[:host] = uri.to_s
33
- @options[:url] = "#{@options[:type].to_s}://#{@options[:host]}"
35
+ @options[:url] = "#{@options[:type]}://#{@options[:host]}"
34
36
  return
35
37
  end
36
38
  @options[:url] = uri.to_s
@@ -50,7 +52,8 @@ class Igp::Shell
50
52
  # runs the ping task
51
53
  def run
52
54
  case options[:type]
53
- when :icmp,:http,:https,:tcp,:udp,:ldap,:ldaps
55
+ # TODO: LDAP was retired from net-ping. to add back in one way or another
56
+ when :icmp, :http, :https, :tcp, :udp # :ldap, :ldaps
54
57
  Igp::Base.new(options).run
55
58
  else
56
59
  usage
@@ -58,49 +61,50 @@ class Igp::Shell
58
61
  end
59
62
 
60
63
  # defines the valid command line options
61
- OPTIONS = %w(help verbose interval=i limit=i)
64
+ OPTIONS = %w[help verbose interval=i limit=i].freeze
62
65
 
63
66
  # prints usage/help information
64
67
  def usage
65
68
  self.class.usage
66
69
  end
70
+
67
71
  # prints usage/help information
68
72
  def self.usage
69
- $stderr.puts <<-EOS
73
+ $stderr.puts <<~EOS
70
74
 
71
- It goes PING! v#{Igp::VERSION}
72
- ===================================
75
+ It goes PING! v#{Igp::VERSION}
76
+ ===================================
73
77
 
74
- Usage:
75
- igp [options] uri
76
-
77
- Options:
78
- -h | --help :shows command help
79
- -i= | --interval=seconds (default: 1 second)
80
- -l= | --limit=number of tests (default: infinite)
78
+ Usage:
79
+ igp [options] uri
81
80
 
82
- The uri specifies the protocol, hostname and port.
83
- The ICMP protocol is assumed if not specified.
84
- The default well-known port is assumed if not specified.
81
+ Options:
82
+ -h | --help :shows command help
83
+ -i= | --interval=seconds (default: 1 second)
84
+ -l= | --limit=number of tests (default: infinite)
85
85
 
86
- Examples:
86
+ The uri specifies the protocol, hostname and port.
87
+ The ICMP protocol is assumed if not specified.
88
+ The default well-known port is assumed if not specified.
87
89
 
88
- ICMP ping:
89
- igp localhost
90
- igp icmp://localhost
90
+ Examples:
91
91
 
92
- UDP/TCP ping:
93
- igp udp://localhost:123
94
- igp tcp://localhost:843
92
+ ICMP ping:
93
+ igp localhost
94
+ igp icmp://localhost
95
95
 
96
- HTTP/S ping:
97
- igp http://localhost:8080
98
- igp https://localhost:4443
96
+ UDP/TCP ping:
97
+ igp udp://localhost:123
98
+ igp tcp://localhost:843
99
99
 
100
- LDAP/S ping:
101
- igp ldap://localhost
102
- igp ldaps://localhost:6636
100
+ HTTP/S ping:
101
+ igp http://localhost:8080
102
+ igp https://localhost:4443
103
103
 
104
104
  EOS
105
+ # TODO: LDAP was retired from net-ping. to add back in one way or another
106
+ # LDAP/S ping:
107
+ # igp ldap://localhost
108
+ # igp ldaps://localhost:6636
105
109
  end
106
- end
110
+ end
data/lib/igp/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Igp
2
- VERSION = "0.0.1"
2
+ VERSION = '1.0.0'.freeze
3
3
  end
data/spec/base_spec.rb CHANGED
@@ -2,58 +2,65 @@ require 'spec_helper'
2
2
  require 'getoptions'
3
3
 
4
4
  describe Igp::Base do
5
-
6
- describe "base initialization without options" do
7
- before do
8
- @base = Igp::Base.new
9
- end
10
- it "should not set ping_handler" do
11
- @base.ping_handler.should be_nil
5
+ describe 'base initialization without options' do
6
+ it 'should not set ping_handler' do
7
+ expect(subject.ping_handler).to be_nil
12
8
  end
13
- it "should not set limit" do
14
- @base.limit.should be_nil
9
+ it 'should not set limit' do
10
+ expect(subject.limit).to be_nil
15
11
  end
16
- it "should default interval to 5 sec" do
17
- @base.interval.should eql(5)
12
+ it 'should default interval to 5 sec' do
13
+ expect(subject.interval).to eql(5)
18
14
  end
19
15
  end
20
-
21
- describe "ping_handler configuration" do
22
- it "should be Net::Ping::External for :icmp" do
23
- options = { :type => :icmp, :host => 'localhost', :port => nil}
16
+
17
+ describe 'ping_handler configuration' do
18
+ it 'should be Net::Ping::External for :icmp' do
19
+ options = { type: :icmp, host: 'localhost', port: nil }
24
20
  base = Igp::Base.new(options)
25
- base.ping_handler.class.should eql(Net::Ping::External)
21
+ expect(base.ping_handler).to be_a(Net::Ping::External)
26
22
  end
27
- it "should be Net::Ping::UDP for :tcp" do
28
- options = { :type => :udp, :host => 'localhost', :port => 22}
23
+ it 'should be Net::Ping::UDP for :tcp' do
24
+ options = { type: :udp, host: 'localhost', port: 22 }
29
25
  base = Igp::Base.new(options)
30
- base.ping_handler.should be_a(Net::Ping::UDP)
26
+ expect(base.ping_handler).to be_a(Net::Ping::UDP)
31
27
  end
32
- it "should be Net::Ping::TCP for :tcp" do
33
- options = { :type => :tcp, :host => 'localhost', :port => 22}
28
+ it 'should be Net::Ping::TCP for :tcp' do
29
+ options = { type: :tcp, host: 'localhost', port: 22 }
34
30
  base = Igp::Base.new(options)
35
- base.ping_handler.should be_a(Net::Ping::TCP)
31
+ expect(base.ping_handler).to be_a(Net::Ping::TCP)
36
32
  end
37
- it "should be Net::Ping::HTTP for :http" do
38
- options = { :type => :http, :url => 'http://localhost'}
33
+ it 'should be Net::Ping::HTTP for :http' do
34
+ options = { type: :http, url: 'http://localhost' }
39
35
  base = Igp::Base.new(options)
40
- base.ping_handler.should be_a(Net::Ping::HTTP)
36
+ expect(base.ping_handler).to be_a(Net::Ping::HTTP)
41
37
  end
42
- it "should be Net::Ping::HTTP for :https" do
43
- options = { :type => :https, :url => 'https://localhost'}
38
+ it 'should be Net::Ping::HTTP for :https' do
39
+ options = { type: :https, url: 'https://localhost' }
44
40
  base = Igp::Base.new(options)
45
- base.ping_handler.should be_a(Net::Ping::HTTP)
41
+ expect(base.ping_handler).to be_a(Net::Ping::HTTP)
46
42
  end
47
- it "should be Net::Ping::LDAP for :ldap" do
48
- options = { :type => :ldap, :url => 'ldap://localhost'}
49
- base = Igp::Base.new(options)
50
- base.ping_handler.should be_a(Net::Ping::LDAP)
43
+ # TODO: LDAP was retired from net-ping. to add back in one way or another
44
+ # it 'should be Net::Ping::LDAP for :ldap' do
45
+ # options = { type: :ldap, url: 'ldap://localhost' }
46
+ # base = Igp::Base.new(options)
47
+ # expect(base.ping_handler).to be_a(Net::Ping::LDAP)
48
+ # end
49
+ # it 'should be Net::Ping::LDAP for :ldaps' do
50
+ # options = { type: :ldaps, url: 'ldaps://localhost' }
51
+ # base = Igp::Base.new(options)
52
+ # expect(base.ping_handler).to be_a(Net::Ping::LDAP)
53
+ # end
54
+ end
55
+
56
+ describe '#run' do
57
+ before do
58
+ @good = Igp::Base.new({ type: :icmp, host: 'localhost', port: nil, limit: 1 })
51
59
  end
52
- it "should be Net::Ping::LDAP for :ldaps" do
53
- options = { :type => :ldaps, :url => 'ldaps://localhost'}
54
- base = Igp::Base.new(options)
55
- base.ping_handler.should be_a(Net::Ping::LDAP)
60
+ it 'should print header and log a ping result' do
61
+ expect(@good.formatter).to receive(:header)
62
+ expect(@good.formatter).to receive(:log)
63
+ capture(:stdout, :stderr) { @good.run }
56
64
  end
57
65
  end
58
-
59
- end
66
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+ require 'getoptions'
3
+
4
+ describe Igp::Base::Format do
5
+ describe '#duration' do
6
+ it 'should return nil for nil duration' do
7
+ expect(subject.duration(nil)).to be_nil
8
+ end
9
+ it 'should return string to 6 decimal places for non-nil duration' do
10
+ result = subject.duration(1.0)
11
+ expect(result).to match(/^\d+\.\d{6}$/)
12
+ expect(result.to_f).to eql(1.0)
13
+ end
14
+ it 'should handle integer parameter' do
15
+ result = subject.duration(1)
16
+ expect(result).to match(/^\d+\.\d{6}$/)
17
+ expect(result.to_f).to eql(1.0)
18
+ end
19
+ end
20
+
21
+ describe '#header' do
22
+ it 'should output a blank line for nil parameters' do
23
+ result = capture(:stderr) { subject.header(nil) }
24
+ expect(result).to eql("\n")
25
+ end
26
+ it 'should convert an arbitrary array of strings and numbers to a space-delimited output to stderr' do
27
+ result = capture(:stderr) { subject.header('string', 1.0, 'another string', 2.0) }
28
+ expect(result).to eql("string 1.0 another string 2.0\n")
29
+ end
30
+ end
31
+
32
+ describe '#log' do
33
+ it 'should output time only for nil parameters' do
34
+ result = capture(:stdout) { subject.log(nil) }
35
+ expect(result).to match(/^.+Z,$/)
36
+ end
37
+ it 'should log successful message (boolean,float,nil) to stdout' do
38
+ result = capture(:stdout) { subject.log(true, 1.0, nil) }
39
+ expect(result).to match(/^.+Z,true,1.0,$/)
40
+ end
41
+ it 'should log unsuccessful message (boolean,nil,string) to stdout' do
42
+ result = capture(:stdout) { subject.log(false, nil, 'message') }
43
+ expect(result).to match(/^.+Z,false,,message$/)
44
+ end
45
+ end
46
+ end
data/spec/shell_spec.rb CHANGED
@@ -2,22 +2,21 @@ require 'spec_helper'
2
2
  require 'getoptions'
3
3
 
4
4
  describe Igp::Shell do
5
-
6
- describe "base configuration" do
5
+ describe 'base configuration' do
7
6
  context 'without destination specified' do
8
7
  before do
9
8
  @options = GetOptions.new(Igp::Shell::OPTIONS, [])
10
9
  end
11
- it "should initialize without error" do
12
- expect { Igp::Shell.new(@options,[]) }.to_not raise_error
10
+ it 'should initialize without error' do
11
+ expect { Igp::Shell.new(@options, []) }.to_not raise_error
13
12
  end
14
- it "should provide help/usage" do
15
- Igp::Shell.respond_to?(:usage).should be_true
16
- Igp::Shell.new(@options,[]).respond_to?(:usage).should be_true
13
+ it 'should provide help/usage' do
14
+ expect(Igp::Shell).to respond_to(:usage)
15
+ expect(Igp::Shell.new(@options, [])).to respond_to(:usage)
17
16
  end
18
- it "should print usage when run" do
19
- shell = Igp::Shell.new(@options,[])
20
- shell.should_receive(:usage)
17
+ it 'should print usage when run' do
18
+ shell = Igp::Shell.new(@options, [])
19
+ expect(shell).to receive(:usage)
21
20
  shell.run
22
21
  end
23
22
  end
@@ -25,137 +24,137 @@ describe Igp::Shell do
25
24
  before do
26
25
  @options_server_value = 'example.com'
27
26
  @options = GetOptions.new(Igp::Shell::OPTIONS, [])
28
- @shell = Igp::Shell.new(@options,[@options_server_value])
27
+ @shell = Igp::Shell.new(@options, [@options_server_value])
29
28
  end
30
- it "should default to 1 second infinte intervals" do
31
- @shell.options[:interval].should eql(1)
32
- @shell.options[:limit].should be_nil
29
+ it 'should default to 1 second infinte intervals' do
30
+ expect(@shell.options[:interval]).to eql(1)
31
+ expect(@shell.options[:limit]).to be_nil
33
32
  end
34
- it "should accept server and default to icmp ping" do
35
- @shell.options[:host].should eql(@options_server_value)
36
- @shell.options[:type].should eql(:icmp)
33
+ it 'should accept server and default to icmp ping' do
34
+ expect(@shell.options[:host]).to eql(@options_server_value)
35
+ expect(@shell.options[:type]).to eql(:icmp)
37
36
  end
38
37
  end
39
38
  end
40
-
41
- describe "tcp configuration" do
39
+
40
+ describe 'tcp configuration' do
42
41
  before(:each) do
43
42
  @type = :tcp
44
43
  @host = 'localhost'
45
44
  @port = 843
46
- @options_server_value = "#{@type.to_s}://#{@host}:#{@port}"
45
+ @options_server_value = "#{@type}://#{@host}:#{@port}"
47
46
  @shell = Igp::Shell.new(GetOptions.new(Igp::Shell::OPTIONS, []), [@options_server_value])
48
47
  end
49
- it "should support url accessor" do
50
- @shell.options[:url].should eql(@options_server_value)
48
+ it 'should support url accessor' do
49
+ expect(@shell.options[:url]).to eql(@options_server_value)
51
50
  end
52
- it "should default to tcp ping with url when given server with tcp protocol setting" do
53
- @shell.options[:type].should eql(@type)
51
+ it 'should default to tcp ping with url when given server with tcp protocol setting' do
52
+ expect(@shell.options[:type]).to eql(@type)
54
53
  end
55
- it "should resolve host/port" do
56
- @shell.options[:host].should eql(@host)
57
- @shell.options[:port].should eql(@port)
54
+ it 'should resolve host/port' do
55
+ expect(@shell.options[:host]).to eql(@host)
56
+ expect(@shell.options[:port]).to eql(@port)
58
57
  end
59
58
  end
60
59
 
61
- describe "udp configuration" do
60
+ describe 'udp configuration' do
62
61
  before(:each) do
63
62
  @type = :udp
64
63
  @host = 'localhost'
65
64
  @port = 22
66
- @options_server_value = "#{@type.to_s}://#{@host}:#{@port}"
65
+ @options_server_value = "#{@type}://#{@host}:#{@port}"
67
66
  @shell = Igp::Shell.new(GetOptions.new(Igp::Shell::OPTIONS, []), [@options_server_value])
68
67
  end
69
- it "should support url accessor" do
70
- @shell.options[:url].should eql(@options_server_value)
68
+ it 'should support url accessor' do
69
+ expect(@shell.options[:url]).to eql(@options_server_value)
71
70
  end
72
- it "should default to tcp ping with url when given server with tcp protocol setting" do
73
- @shell.options[:type].should eql(@type)
71
+ it 'should default to tcp ping with url when given server with tcp protocol setting' do
72
+ expect(@shell.options[:type]).to eql(@type)
74
73
  end
75
- it "should resolve host/port" do
76
- @shell.options[:host].should eql(@host)
77
- @shell.options[:port].should eql(@port)
74
+ it 'should resolve host/port' do
75
+ expect(@shell.options[:host]).to eql(@host)
76
+ expect(@shell.options[:port]).to eql(@port)
78
77
  end
79
78
  end
80
79
 
81
- describe "http configuration" do
80
+ describe 'http configuration' do
82
81
  before(:each) do
83
82
  @type = :http
84
83
  @host = 'localhost'
85
84
  @port = 80
86
- @options_server_value = "#{@type.to_s}://#{@host}"
85
+ @options_server_value = "#{@type}://#{@host}"
87
86
  @shell = Igp::Shell.new(GetOptions.new(Igp::Shell::OPTIONS, []), [@options_server_value])
88
87
  end
89
- it "should support url accessor" do
90
- @shell.options[:url].should eql(@options_server_value)
88
+ it 'should support url accessor' do
89
+ expect(@shell.options[:url]).to eql(@options_server_value)
91
90
  end
92
- it "should default to tcp ping with url when given server with tcp protocol setting" do
93
- @shell.options[:type].should eql(@type)
91
+ it 'should default to tcp ping with url when given server with tcp protocol setting' do
92
+ expect(@shell.options[:type]).to eql(@type)
94
93
  end
95
- it "should resolve host/port" do
96
- @shell.options[:host].should eql(@host)
97
- @shell.options[:port].should eql(@port)
94
+ it 'should resolve host/port' do
95
+ expect(@shell.options[:host]).to eql(@host)
96
+ expect(@shell.options[:port]).to eql(@port)
98
97
  end
99
98
  end
100
99
 
101
- describe "https configuration" do
100
+ describe 'https configuration' do
102
101
  before(:each) do
103
102
  @type = :https
104
103
  @host = 'localhost'
105
104
  @port = 443
106
- @options_server_value = "#{@type.to_s}://#{@host}"
105
+ @options_server_value = "#{@type}://#{@host}"
107
106
  @shell = Igp::Shell.new(GetOptions.new(Igp::Shell::OPTIONS, []), [@options_server_value])
108
107
  end
109
- it "should support url accessor" do
110
- @shell.options[:url].should eql(@options_server_value)
108
+ it 'should support url accessor' do
109
+ expect(@shell.options[:url]).to eql(@options_server_value)
111
110
  end
112
- it "should default to tcp ping with url when given server with tcp protocol setting" do
113
- @shell.options[:type].should eql(@type)
111
+ it 'should default to tcp ping with url when given server with tcp protocol setting' do
112
+ expect(@shell.options[:type]).to eql(@type)
114
113
  end
115
- it "should resolve host/port" do
116
- @shell.options[:host].should eql(@host)
117
- @shell.options[:port].should eql(@port)
114
+ it 'should resolve host/port' do
115
+ expect(@shell.options[:host]).to eql(@host)
116
+ expect(@shell.options[:port]).to eql(@port)
118
117
  end
119
118
  end
120
119
 
121
- describe "ldap configuration" do
122
- before(:each) do
123
- @type = :ldap
124
- @host = 'localhost'
125
- @port = 389
126
- @options_server_value = "#{@type.to_s}://#{@host}"
127
- @shell = Igp::Shell.new(GetOptions.new(Igp::Shell::OPTIONS, []), [@options_server_value])
128
- end
129
- it "should support url accessor" do
130
- @shell.options[:url].should eql(@options_server_value)
131
- end
132
- it "should default to tcp ping with url when given server with tcp protocol setting" do
133
- @shell.options[:type].should eql(@type)
134
- end
135
- it "should resolve host/port" do
136
- @shell.options[:host].should eql(@host)
137
- @shell.options[:port].should eql(@port)
138
- end
139
- end
140
-
141
- describe "ldaps configuration" do
142
- before(:each) do
143
- @type = :ldaps
144
- @host = 'localhost'
145
- @port = 636
146
- @options_server_value = "#{@type.to_s}://#{@host}"
147
- @shell = Igp::Shell.new(GetOptions.new(Igp::Shell::OPTIONS, []), [@options_server_value])
148
- end
149
- it "should support url accessor" do
150
- @shell.options[:url].should eql(@options_server_value)
151
- end
152
- it "should default to tcp ping with url when given server with tcp protocol setting" do
153
- @shell.options[:type].should eql(@type)
154
- end
155
- it "should resolve host/port" do
156
- @shell.options[:host].should eql(@host)
157
- @shell.options[:port].should eql(@port)
158
- end
159
- end
120
+ # TODO: LDAP was retired from net-ping. to add back in one way or another
121
+ # describe 'ldap configuration' do
122
+ # before(:each) do
123
+ # @type = :ldap
124
+ # @host = 'localhost'
125
+ # @port = 389
126
+ # @options_server_value = "#{@type}://#{@host}"
127
+ # @shell = Igp::Shell.new(GetOptions.new(Igp::Shell::OPTIONS, []), [@options_server_value])
128
+ # end
129
+ # it 'should support url accessor' do
130
+ # expect(@shell.options[:url]).to eql(@options_server_value)
131
+ # end
132
+ # it 'should default to tcp ping with url when given server with tcp protocol setting' do
133
+ # expect(@shell.options[:type]).to eql(@type)
134
+ # end
135
+ # it 'should resolve host/port' do
136
+ # expect(@shell.options[:host]).to eql(@host)
137
+ # expect(@shell.options[:port]).to eql(@port)
138
+ # end
139
+ # end
160
140
 
161
- end
141
+ # describe 'ldaps configuration' do
142
+ # before(:each) do
143
+ # @type = :ldaps
144
+ # @host = 'localhost'
145
+ # @port = 636
146
+ # @options_server_value = "#{@type}://#{@host}"
147
+ # @shell = Igp::Shell.new(GetOptions.new(Igp::Shell::OPTIONS, []), [@options_server_value])
148
+ # end
149
+ # it 'should support url accessor' do
150
+ # expect(@shell.options[:url]).to eql(@options_server_value)
151
+ # end
152
+ # it 'should default to tcp ping with url when given server with tcp protocol setting' do
153
+ # expect(@shell.options[:type]).to eql(@type)
154
+ # end
155
+ # it 'should resolve host/port' do
156
+ # expect(@shell.options[:host]).to eql(@host)
157
+ # expect(@shell.options[:port]).to eql(@port)
158
+ # end
159
+ # end
160
+ end
data/spec/spec_helper.rb CHANGED
@@ -1 +1,14 @@
1
1
  require 'igp'
2
+ require 'stringio'
3
+
4
+ def capture(*streams)
5
+ streams.map!(&:to_s)
6
+ begin
7
+ result = StringIO.new
8
+ streams.each { |stream| eval "$#{stream} = result # $#{stream} = result" }
9
+ yield
10
+ ensure
11
+ streams.each { |stream| eval("$#{stream} = #{stream.upcase} # $#{stream} = #{stream.upcase}") }
12
+ end
13
+ result.string
14
+ end