|
| 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 | + |
0 commit comments