Skip to content

Commit a8e536a

Browse files
committed
Merge pull request #7 from dpetzel/json_version_and_unit_tests
relax the restriction on JSON, and provide some unit tests
2 parents cc7d7ca + fd6eef2 commit a8e536a

6 files changed

Lines changed: 171 additions & 1 deletion

File tree

.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#https://github.com/github/gitignore/blob/master/Ruby.gitignore
2+
*.gem
3+
*.rbc
4+
.bundle
5+
.config
6+
coverage
7+
InstalledFiles
8+
lib/bundler/man
9+
pkg
10+
rdoc
11+
spec/reports
12+
test/tmp
13+
test/version_tmp
14+
tmp
15+
16+
# YARD artifacts
17+
.yardoc
18+
_yardoc
19+
doc/
20+
21+
22+
# Extra's not part of the template
23+
.buildpath
24+
.project

README.textile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,15 @@ A gem is now available. 'gem install zenoss_client'
6868
</pre>
6969

7070
Have fun and let me know what needs to be fixed / added.
71+
72+
== Testing
73+
You can invoke a series of minitest based spec test using the supplied
74+
tests and rake task. Simply run the following to kick off the tests
75+
@rake test@
76+
77+
You should see output similar to the following (The number of tests will
78+
of course grow over time):
79+
@3 tests, 6 assertions, 0 failures, 0 errors, 0 skips@
80+
81+
If you get any failures, you should of course address them
82+

Rakefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ require 'rubygems'
22
require 'rake/clean'
33
require 'rake/gempackagetask'
44
require 'rake/rdoctask'
5+
require 'rake/testtask'
56

67
CLEAN.include("pkg")
78
CLEAN.include("doc")
@@ -70,3 +71,9 @@ def up_min_version
7071
f.write(ver)
7172
ver
7273
end
74+
75+
Rake::TestTask.new do |t|
76+
t.libs.push "lib"
77+
t.test_files = FileList['test/*_test.rb']
78+
t.verbose = true
79+
end

test/test_helper.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
2+
#http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-with-rspec/
3+
require_relative '../lib/zenoss'
4+
require 'test/unit'
5+
6+
7+
ZENOSS_URL = ENV['zenoss_client_url'] || "http://localhost:8080/zport/dmd"
8+
ZENOSS_USER = ENV['zenoss_client_username'] || "admin"
9+
ZENOSS_PASSWORD = ENV['zenoss_client_password'] || "zenoss"
10+
11+
TEST_DEVICE_NAME = "UnitTestDevice"

test/zenoss_client_test.rb

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
require_relative './test_helper'
2+
require 'minitest/spec'
3+
require 'logger'
4+
5+
LOG = Logger.new(STDOUT)
6+
LOG.level = Logger::INFO
7+
8+
9+
describe Zenoss do
10+
#Simulate some "before all" type of setup
11+
#https://github.com/seattlerb/minitest/issues/61#issuecomment-4581115
12+
def self.zen
13+
@zen ||= begin
14+
connection = Zenoss.connect ZENOSS_URL, ZENOSS_USER, ZENOSS_PASSWORD
15+
#We Need to Create A Device for testing
16+
#We do this here, so we can re-use the same device over and over
17+
#Without needing to create a new one per test
18+
LOG.info("Creating a Fresh Device For Testing")
19+
new_device_rsp = connection.json_request(
20+
'DeviceRouter', 'addDevice',
21+
[{:deviceName => TEST_DEVICE_NAME, :deviceClass => '/Devices/Server'}]
22+
)
23+
24+
#Now we need to wait until the device is present before we proceed.
25+
#Once we issue the create command, it takes
26+
if new_device_rsp.has_key?("success") && new_device_rsp['success'] == true
27+
#Our job was accepted
28+
retries = 20
29+
retry_delay = 15 #seconds
30+
found_device = false
31+
LOG.info("Waiting for the newly created device to be available. " +
32+
"This might take a minute or two")
33+
while found_device == false
34+
if retries > 0
35+
#This will return an Array, so we wait until the array has
36+
#something, or we give up after a while
37+
devs = connection.find_devices_by_name(TEST_DEVICE_NAME)
38+
if devs.empty?
39+
retries -= 1
40+
LOG.info("#{TEST_DEVICE_NAME} not available yet")
41+
sleep(retry_delay)
42+
else
43+
found_device = true
44+
LOG.info("#{TEST_DEVICE_NAME} is available. Proceeding with " +
45+
"testing")
46+
end
47+
else
48+
raise ZenossError, "Unable to Create A New Device For Unit Tests"
49+
end
50+
end
51+
else
52+
#We failed to create a new device....
53+
end
54+
#Return the connection object
55+
connection
56+
end
57+
end
58+
59+
before do
60+
@zen = self.class.zen
61+
@dev = @zen.find_devices_by_name(TEST_DEVICE_NAME).first
62+
end
63+
64+
65+
it "returns an Array of devices when searched by name" do
66+
x = @zen.find_devices_by_name(TEST_DEVICE_NAME)
67+
x.must_be_kind_of Array
68+
x.first.must_be_kind_of Zenoss::Model::Device
69+
end
70+
71+
it "returns device uptime when asked" do
72+
@dev.sys_uptime.wont_be_nil
73+
@dev.sys_uptime.wont_be_empty
74+
end
75+
76+
it "returns an Array of events for a device" do
77+
#There could be 0 or more, events so an empty Array is OK
78+
@dev.get_events.must_be_kind_of Array
79+
end
80+
81+
it "returns an Array of historical events for a device" do
82+
#There could be 0 or more, events so an empty Array is OK
83+
@dev.get_event_history.must_be_kind_of Array
84+
end
85+
86+
it "returns info for a device in the form of a Hash" do
87+
@dev.get_info().wont_be_nil
88+
@dev.get_info().wont_be_empty
89+
@dev.get_info().must_be_kind_of Hash
90+
end
91+
92+
it "returns an Array of events for all devices" do
93+
events = @zen.query_events
94+
events.must_be_kind_of Array
95+
events.first.must_be_kind_of Zenoss::Events::ZEvent
96+
end
97+
98+
it "fetches the report tree" do
99+
report_tree = @zen.get_report_tree
100+
report_tree.must_be_kind_of Array
101+
report_tree.first.must_be_kind_of Hash
102+
report_tree.wont_be_empty
103+
end
104+
105+
it "fetches available report types and returns a Hash" do
106+
report_types = @zen.get_report_types
107+
report_types.must_be_kind_of Hash
108+
report_types.wont_be_empty
109+
report_types.has_key?("reportTypes").must_equal true
110+
end
111+
112+
end
113+
114+

zenoss_client.gemspec

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,7 @@ Gem::Specification.new do |gem|
3030
gem.required_ruby_version = '>= 1.8.7'
3131
gem.add_runtime_dependency 'httpclient', '~> 2.2.0'
3232
gem.add_runtime_dependency 'tzinfo', '~> 0.3.20'
33-
gem.add_runtime_dependency 'json', '~> 1.5.0'
33+
gem.add_runtime_dependency 'json', '~> 1.5'
34+
35+
gem.add_development_dependency('minitest')
3436
end

0 commit comments

Comments
 (0)