# File test/test_helper.rb, line 109 def assert_eventually_true(timeout=30, &block) Timeout::timeout(timeout) do while !block.call() sleep(0.2) end end end
# File test/test_helper.rb, line 117 def assert_logged_in(service) assert_nothing_raised do # A request to data/indexes requires you to be logged in. service.request(:method=>:GET, :resource=>["data", "indexes"]) end end
# File test/test_helper.rb, line 125 def assert_not_logged_in(service) if service.server_accepting_connections? begin service.request(:method=>:GET, :resource=>["data", "indexes"]) rescue SplunkHTTPError => err assert_equal(401, err.code, "Expected HTTP status code 401, found: #{err.code}") else fail("Context is logged in.") end end end
Restart Splunk and wait for it to come back up, but only if it needs it.
Throws an error if this is called on a Splunk instance that does not need to be restarted.
# File test/test_helper.rb, line 248 def checked_restart(service) if !service.server_requires_restart? raise StandardError("Tried to restart a Splunk instance that" + " does not need it.") else service.restart(DEFAULT_RESTART_TIMEOUT) end end
Clear any restart messages on service.
If there was no restart message, raises an error. (We want all the restarts and restart messages carefully controlled in the test suite.)
# File test/test_helper.rb, line 143 def clear_restart_message(service) if !service.server_requires_restart? raise StandardError.new("Tried to clear restart message " + "when there was none.") end begin service.request(:method => :DELETE, :resource => ["messages", "restart_required"]) rescue SplunkHTTPError => err if err.code != 404 raise err end end end
# File test/test_helper.rb, line 158 def has_test_data?(service) collection_name = 'sdk-app-collection' return service.apps.has_key?(collection_name) end
# File test/test_helper.rb, line 163 def install_app_from_collection(name) collection_name = 'sdk-app-collection' if !@service.apps.has_key?(collection_name) raise StandardError("#{collection_name} not installed in Splunk.") end app_path = path_in_app(collection_name, ["build", name+".tar"]) args = {"update" => 1, "name" => app_path} begin @service.request(:method => :POST, :resource => ["apps", "appinstall"], :body => args) @installed_apps << name rescue SplunkHTTPError => err if err.code == 40 raise StandardError("App #{name} not found in app collection") else raise err end end end
Return a path to path_components in app_name.
`path_in_app` is used to refer to files in applications installed with `install_app_from_collection`. For example, the app `file_to_upload` in
#path_in_app('file_to_upload', ['log.txt'])
#path_in_app('has_setup_xml', ['default', 'setup.xml'])
#path_in_app` figures out the correct separator to use (based on whether splunkd is running on Windows or Unix) and joins the elements in path_components into a path relative to the application specified by app_name.
path_components should be a list of strings giving the components. This function will try to figure out the correct separator (/ or ) for the platform that splunkd is running on and construct the path as needed.
:return: A string giving the path.
# File test/test_helper.rb, line 209 def path_in_app(app_name, path_components) splunk_home = @service.settings["SPLUNK_HOME"] if splunk_home.include?("\\") # This clause must come first, since Windows machines may # have mixed \ and / in their paths. separator = "\\" elsif splunk_home.include?("/") separator = "/" else raise StandardError("No separators in $SPLUNK_HOME. Can't determine " + "what file separator to use.") end app_path = ([splunk_home, "etc", "apps", app_name] + path_components). join(separator) return app_path end
Create a new restart message on service.
Optionally you can specify a value for the restart message, or it will default to “Ruby SDK test suite asked for a restart.”
# File test/test_helper.rb, line 233 def set_restart_message(service, message="Ruby SDK test suite asked for a restart.") service.request(:method => :POST, :namespace => Splunk::namespace(:sharing => "default"), :resource => ["messages"], :body => {"name" => "restart_required", "value" => "Message set by restart method" + " of the Splunk Ruby SDK"}) end
# File test/test_helper.rb, line 65 def setup super @splunkrc = read_splunkrc() @service = Splunk::Service.new(@splunkrc).login() @installed_apps = [] if @service.server_requires_restart? puts "Previous test left server in a state requiring a restart." checked_restart(@service) end end
# File test/test_helper.rb, line 77 def teardown if @service.server_requires_restart?() fail("Test left server in a state requiring restart.") end # Are we on Windows or Unix? We need this below. splunk_home = @service.settings["SPLUNK_HOME"] is_windows = splunk_home.include?("\\") == true if @service.splunk_version[0..1] != [4,2] @installed_apps.each() do |app_name| # There is a bug in Python on Windows which results in the # sleep_command not deleting properly because there are still # hung jobs that haven't terminated when we reach this point. # We skip deleting the sleep_command app on Windows. if app_name != 'sleep_command' or !is_windows @service.apps.delete(app_name) assert_eventually_true() do !@service.apps.has_key?(app_name) end if @service.server_requires_restart? clear_restart_message(@service) end end end end @installed_apps.clear() super end
Restarts a Splunk instance whether it needs it or not.
# File test/test_helper.rb, line 259 def unchecked_restart(service) service.restart(DEFAULT_RESTART_TIMEOUT) end