Skip to content

Commit 9b6d066

Browse files
committed
style: cosmetic changes
1 parent e752a69 commit 9b6d066

1 file changed

Lines changed: 123 additions & 115 deletions

File tree

test/zenoss_client_test.rb

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

0 commit comments

Comments
 (0)