require_relative 'test_helper' require 'splunk-sdk-ruby' include Splunk QUERY = "search index=_internal | head 3" JOB_ARGS = {:earliest_time => "-1m", :latest_time => "now", :status_buckets => 10} class JobsTestCase < TestCaseWithSplunkConnection def test_create_with_garbage_fails assert_raises(SplunkHTTPError) do @service.jobs.create("aweaj;awfaw faf'adf") end end def test_create_and_idempotent_cancel jobs = @service.jobs job = jobs.create(QUERY) assert_true(jobs.has_key?(job.sid)) job.cancel() assert_eventually_true() { !jobs.has_key?(job.sid) } job.cancel() # Cancel twice should be a nop end ## # There is a convenience method on service to create an asynchronous # search job. Test it the same way. # def test_service_create_and_idempotent_cancel jobs = @service.jobs job = @service.create_search(QUERY) assert_true(jobs.has_key?(job.sid)) job.cancel() assert_eventually_true() { !jobs.has_key?(job.sid) } job.cancel() # Cancel twice should be a nop end def test_create_with_exec_mode_fails assert_raises(ArgumentError) do @service.jobs.create(QUERY, :exec_mode => "oneshot") end end def test_oneshot jobs = @service.jobs stream = jobs.create_oneshot(QUERY) results = ResultsReader.new(stream) assert_false(results.is_preview?) events = results.to_a() assert_equal(3, events.length()) end ## # Test that Service#create_oneshot properly creates a oneshot search. # def test_oneshot_on_service jobs = @service.jobs stream = @service.create_oneshot(QUERY) results = ResultsReader.new(stream) assert_false(results.is_preview?) events = results.to_a() assert_equal(3, events.length()) end def test_oneshot_with_garbage_fails assert_raises(SplunkHTTPError) do @service.jobs.create_oneshot("abcwrawerafawf 'adfad'faw") end end def test_export_with_garbage_fails assert_raises(SplunkHTTPError) do @service.jobs.create_export("abavadfa;ejwfawfasdfadf wfw").to_a() end end def test_export stream = @service.jobs.create_export(QUERY) assert_true(stream.is_a?(ExportStream)) results = ResultsReader.new(stream).to_a() assert_equal(3, results.length()) end ## # Test that the convenience method Service#create_export behaves the same # way as Jobs#create_export. # def test_export_on_service stream = @service.create_export(QUERY) results = ResultsReader.new(stream).to_a() assert_equal(3, results.length()) end ## # Test that ResultsReader parses reporting export searches correctly # (by only returning the final, nonpreview results). # def test_export_on_reporting_search stream = @service.create_export("search index=_internal earliest=-2d | stats count(_raw) by method") results = ResultsReader.new(stream).to_a() assert_true(3 >= results.length()) end ## # Test that oneshot jobs have no elements in the XML they return # by default. # def test_oneshot_has_no_segmentation_by_default omit_if(@service.splunk_version[0] == 4) stream = @service.create_oneshot("search index=_internal GET | head 3") assert_false(stream.include?(" elements returned in the XML from a oneshot job when we pass # the option segmentation=raw? # def test_oneshot_has_forced_segmentation omit_if(@service.splunk_version[0] == 4) stream = @service.create_oneshot("search index=_internal GET | head 3", :segmentation => "raw") assert_true(stream.include?(" elements in the XML they return by # default. # def test_export_has_no_segmentation_by_default omit_if(@service.splunk_version[0] == 4) stream = @service.create_export("search index=_internal GET | head 3") assert_false(stream.include?(" elements in the XML they return when a # value is passed to the segmentation argument to make it so. # def test_export_has_forced_segmentation omit_if(@service.splunk_version[0] == 4) stream = @service.create_export("search index=_internal GET | head 3", :segmentation => "raw") assert_true(stream.include?(" "raw").include?(" "raw").include?(" 0) results = Splunk::ResultsReader.new(stream) count = 0 results.each do |event| count += 1 end assert_equal(150, count) stream = job.preview(:count => 0) results = Splunk::ResultsReader.new(stream) count = 0 results.each do |event| count += 1 end assert_equal(150, count) end end class LongJobTestCase < JobsTestCase def setup super install_app_from_collection("sleep_command") @job = @service.jobs.create("search index=_internal | sleep 20") while !@job.is_ready?() sleep(0.1) end end def teardown if @job @job.cancel() assert_eventually_true(50) do !@service.jobs.has_key?(@job.sid) end end super end def test_setttl old_ttl = Integer(@job["ttl"]) new_ttl = old_ttl + 1000 @job.set_ttl(new_ttl) assert_eventually_true() do @job.refresh() ttl = Integer(@job["ttl"]) ttl <= new_ttl && ttl > old_ttl end end def test_touch # Any request resets the TTL in Splunk 6.0. This was an error that # has been filed and will be reverted (TODO: Insert Jira number once dnoble logs it). if @service.splunk_version[0,2] == [6,0] return end i = 2 while i < 20 sleep(i) old_ttl = Integer(@job.refresh()["ttl"]) @job.touch() new_ttl = Integer(@job.refresh()["ttl"]) if new_ttl > old_ttl break else i += 1 end end assert_true(new_ttl > old_ttl) end end class RealTimeJobTestCase < JobsTestCase def setup super query = "search index=_internal" @job = @service.jobs.create(query, :earliest_time => "rt-1d", :latest_time => "rt", :priority => 5) while !@job.is_ready? sleep(0.2) end end def teardown if @job @job.cancel() assert_eventually_true() do !@service.jobs.has_key?(@job.sid) end end super end def test_set_priority assert_equal("5", @job["priority"]) sleep(1) new_priority = 3 @job.set_priority(new_priority) assert_eventually_true(50) do @job.refresh() fail("Job finished before priority was set.") if @job.is_done?() @job["priority"] == "3" end end def test_get_preview assert_equal("1", @job["isPreviewEnabled"]) assert_eventually_true do response = @job.preview() results = Splunk::ResultsReader.new(response) results.is_preview? end end def test_pause_unpause_finalize assert_equal("0", @job["isPaused"]) @job.pause() assert_eventually_true() { @job.refresh()["isPaused"] == "1" } @job.unpause() assert_eventually_true() { @job.refresh()["isPaused"] == "0" } assert_equal("0", @job["isFinalized"]) @job.finalize() assert_eventually_true() { @job.refresh()["isFinalized"] == "1" } end def test_searchlog log_stream = @job.searchlog assert_false(log_stream.empty?) end end