splunk-sdk-ruby 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +160 -0
- data/Gemfile +8 -0
- data/LICENSE +177 -0
- data/README.md +310 -0
- data/Rakefile +40 -0
- data/examples/1_connect.rb +51 -0
- data/examples/2_manage.rb +103 -0
- data/examples/3_blocking_searches.rb +82 -0
- data/examples/4_asynchronous_searches.rb +79 -0
- data/examples/5_stream_data_to_splunk.rb +79 -0
- data/lib/splunk-sdk-ruby.rb +47 -0
- data/lib/splunk-sdk-ruby/ambiguous_entity_reference.rb +28 -0
- data/lib/splunk-sdk-ruby/atomfeed.rb +323 -0
- data/lib/splunk-sdk-ruby/collection.rb +417 -0
- data/lib/splunk-sdk-ruby/collection/apps.rb +35 -0
- data/lib/splunk-sdk-ruby/collection/case_insensitive_collection.rb +58 -0
- data/lib/splunk-sdk-ruby/collection/configuration_file.rb +50 -0
- data/lib/splunk-sdk-ruby/collection/configurations.rb +80 -0
- data/lib/splunk-sdk-ruby/collection/jobs.rb +136 -0
- data/lib/splunk-sdk-ruby/collection/messages.rb +51 -0
- data/lib/splunk-sdk-ruby/context.rb +522 -0
- data/lib/splunk-sdk-ruby/entity.rb +260 -0
- data/lib/splunk-sdk-ruby/entity/index.rb +191 -0
- data/lib/splunk-sdk-ruby/entity/job.rb +339 -0
- data/lib/splunk-sdk-ruby/entity/message.rb +36 -0
- data/lib/splunk-sdk-ruby/entity/saved_search.rb +71 -0
- data/lib/splunk-sdk-ruby/entity/stanza.rb +45 -0
- data/lib/splunk-sdk-ruby/entity_not_ready.rb +26 -0
- data/lib/splunk-sdk-ruby/illegal_operation.rb +27 -0
- data/lib/splunk-sdk-ruby/namespace.rb +239 -0
- data/lib/splunk-sdk-ruby/resultsreader.rb +716 -0
- data/lib/splunk-sdk-ruby/service.rb +339 -0
- data/lib/splunk-sdk-ruby/splunk_http_error.rb +49 -0
- data/lib/splunk-sdk-ruby/synonyms.rb +50 -0
- data/lib/splunk-sdk-ruby/version.rb +27 -0
- data/lib/splunk-sdk-ruby/xml_shim.rb +117 -0
- data/splunk-sdk-ruby.gemspec +27 -0
- data/test/atom_test_data.rb +472 -0
- data/test/data/atom/atom_feed_with_message.xml +19 -0
- data/test/data/atom/atom_with_feed.xml +99 -0
- data/test/data/atom/atom_with_several_entries.xml +101 -0
- data/test/data/atom/atom_with_simple_entries.xml +30 -0
- data/test/data/atom/atom_without_feed.xml +248 -0
- data/test/data/export/4.2.5/export_results.xml +88 -0
- data/test/data/export/4.3.5/export_results.xml +87 -0
- data/test/data/export/5.0.1/export_results.xml +78 -0
- data/test/data/export/5.0.1/nonreporting.xml +232 -0
- data/test/data/results/4.2.5/results-empty.xml +0 -0
- data/test/data/results/4.2.5/results-preview.xml +255 -0
- data/test/data/results/4.2.5/results.xml +336 -0
- data/test/data/results/4.3.5/results-empty.xml +0 -0
- data/test/data/results/4.3.5/results-preview.xml +1057 -0
- data/test/data/results/4.3.5/results.xml +626 -0
- data/test/data/results/5.0.2/results-empty.xml +1 -0
- data/test/data/results/5.0.2/results-empty_preview.xml +1 -0
- data/test/data/results/5.0.2/results-preview.xml +448 -0
- data/test/data/results/5.0.2/results.xml +501 -0
- data/test/export_test_data.json +360 -0
- data/test/resultsreader_test_data.json +1119 -0
- data/test/services.server.info.xml +43 -0
- data/test/services.xml +111 -0
- data/test/test_atomfeed.rb +71 -0
- data/test/test_collection.rb +278 -0
- data/test/test_configuration_file.rb +124 -0
- data/test/test_context.rb +119 -0
- data/test/test_entity.rb +95 -0
- data/test/test_helper.rb +250 -0
- data/test/test_http_error.rb +52 -0
- data/test/test_index.rb +91 -0
- data/test/test_jobs.rb +319 -0
- data/test/test_messages.rb +17 -0
- data/test/test_namespace.rb +188 -0
- data/test/test_restarts.rb +49 -0
- data/test/test_resultsreader.rb +106 -0
- data/test/test_roles.rb +41 -0
- data/test/test_saved_searches.rb +119 -0
- data/test/test_service.rb +65 -0
- data/test/test_users.rb +33 -0
- data/test/test_xml_shim.rb +28 -0
- data/test/testfile.txt +1 -0
- metadata +200 -0
@@ -0,0 +1,124 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
require 'splunk-sdk-ruby'
|
3
|
+
|
4
|
+
include Splunk
|
5
|
+
|
6
|
+
class ConfigurationFileTestCase < TestCaseWithSplunkConnection
|
7
|
+
def setup
|
8
|
+
super
|
9
|
+
# We cannot delete configuration files from the REST API
|
10
|
+
# so we create a temporary application to do our manipulation
|
11
|
+
# in. When we delete it at the end of the test run, the
|
12
|
+
# configuration modifications will be deleted as well.
|
13
|
+
@container_app_name = temporary_name()
|
14
|
+
@service.apps.create(@container_app_name)
|
15
|
+
|
16
|
+
# Now reconnect in the test app context.
|
17
|
+
app_service_args = @splunkrc.clone()
|
18
|
+
app_service_args[:namespace] =
|
19
|
+
Splunk::namespace(:sharing => "app", :app => @container_app_name)
|
20
|
+
@app_service = Splunk::Service.new(app_service_args).login()
|
21
|
+
@confs = @app_service.confs
|
22
|
+
end
|
23
|
+
|
24
|
+
def teardown
|
25
|
+
if @service.server_requires_restart?
|
26
|
+
fail("Test left Splunk in a state requiring restart.")
|
27
|
+
end
|
28
|
+
|
29
|
+
@service.apps.delete(@container_app_name)
|
30
|
+
if @service.server_requires_restart?
|
31
|
+
clear_restart_message(@service)
|
32
|
+
end
|
33
|
+
|
34
|
+
super
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_create_and_delete
|
38
|
+
file_name = temporary_name()
|
39
|
+
assert_false(@confs.has_key?(file_name))
|
40
|
+
assert_false(@confs.contains?(file_name))
|
41
|
+
assert_false(@confs.member?(file_name))
|
42
|
+
assert_false(@confs.key?(file_name))
|
43
|
+
assert_false(@confs.include?(file_name))
|
44
|
+
|
45
|
+
conf = @confs.create(file_name)
|
46
|
+
assert_equal(file_name, conf.name)
|
47
|
+
assert_equal(0, conf.length)
|
48
|
+
|
49
|
+
assert_true(@confs.has_key?(file_name))
|
50
|
+
assert_true(@confs.contains?(file_name))
|
51
|
+
assert_true(@confs.member?(file_name))
|
52
|
+
assert_true(@confs.key?(file_name))
|
53
|
+
assert_true(@confs.include?(file_name))
|
54
|
+
|
55
|
+
assert_raises(Splunk::IllegalOperation) { @confs.delete(file_name) }
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_fetch
|
59
|
+
file_name = temporary_name()
|
60
|
+
created_conf = @confs.create(file_name)
|
61
|
+
|
62
|
+
fetched_conf = @confs.fetch(file_name)
|
63
|
+
assert_true(fetched_conf.is_a?(ConfigurationFile))
|
64
|
+
assert_equal(created_conf.name, fetched_conf.name)
|
65
|
+
|
66
|
+
bracket_fetched_conf = @confs[file_name]
|
67
|
+
assert_true(fetched_conf.is_a?(ConfigurationFile))
|
68
|
+
assert_equal(created_conf.name, bracket_fetched_conf.name)
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_each_and_values
|
72
|
+
each_names = []
|
73
|
+
@confs.each() { |entity| each_names << entity.name }
|
74
|
+
|
75
|
+
values_names = @confs.values().map() { |e| e.name }
|
76
|
+
|
77
|
+
assert_false(each_names.empty?)
|
78
|
+
assert_equal(each_names, values_names)
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_create_and_delete_stanzas
|
82
|
+
file_name = temporary_name()
|
83
|
+
conf = @confs.create(file_name)
|
84
|
+
|
85
|
+
assert_equal(0, conf.length())
|
86
|
+
|
87
|
+
n = 5 + rand(5)
|
88
|
+
stanza_names = []
|
89
|
+
(1..n).each() do
|
90
|
+
stanza_name = temporary_name()
|
91
|
+
stanza_names << stanza_name
|
92
|
+
conf.create(stanza_name)
|
93
|
+
end
|
94
|
+
|
95
|
+
assert_equal(n, conf.length())
|
96
|
+
|
97
|
+
stanza_names.each() do |name|
|
98
|
+
conf.delete(name)
|
99
|
+
n -= 1
|
100
|
+
assert_equal(n, conf.length())
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_each
|
105
|
+
@confs.each() do |configuration_file|
|
106
|
+
assert_true(configuration_file.is_a?(ConfigurationFile))
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_submit_to_stanza
|
111
|
+
file_name = temporary_name()
|
112
|
+
conf = @confs.create(file_name)
|
113
|
+
|
114
|
+
stanza_name = temporary_name()
|
115
|
+
stanza = conf.create(stanza_name)
|
116
|
+
|
117
|
+
assert_equal(0, stanza.length())
|
118
|
+
stanza.submit(:boris => "natasha", :hilda => "moose on the roof")
|
119
|
+
stanza.refresh()
|
120
|
+
assert_equal(2, stanza.length())
|
121
|
+
assert_equal("natasha", stanza["boris"])
|
122
|
+
assert_equal("moose on the roof", stanza["hilda"])
|
123
|
+
end
|
124
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
require "splunk-sdk-ruby"
|
3
|
+
|
4
|
+
include Splunk
|
5
|
+
|
6
|
+
# Test the helper functions in test_helper.rb
|
7
|
+
class TestHelpers < TestCaseWithSplunkConnection
|
8
|
+
def test_temporary_name
|
9
|
+
assert_true(temporary_name().start_with?("delete-me"))
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_set_and_clear_restart_messages()
|
13
|
+
service = Context.new(@splunkrc).login()
|
14
|
+
assert_false(service.server_requires_restart?)
|
15
|
+
|
16
|
+
set_restart_message(service)
|
17
|
+
assert_true(service.server_requires_restart?)
|
18
|
+
|
19
|
+
clear_restart_message(service)
|
20
|
+
assert_false(service.server_requires_restart?)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class TestContext < TestCaseWithSplunkConnection
|
25
|
+
def test_login()
|
26
|
+
service = Context.new(@splunkrc)
|
27
|
+
service.login()
|
28
|
+
assert_logged_in(service)
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_login_with_encodings()
|
32
|
+
["ASCII", "UTF-8"].each() do |encoding|
|
33
|
+
values = {}
|
34
|
+
@splunkrc.each() do |key, value|
|
35
|
+
if value.is_a?(String)
|
36
|
+
values[key] = value.clone().force_encoding(encoding)
|
37
|
+
else
|
38
|
+
values[key] = value
|
39
|
+
end
|
40
|
+
end
|
41
|
+
service = Context.new(values).login()
|
42
|
+
assert_logged_in(service)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_authenticate_with_token
|
47
|
+
service = Context.new(@splunkrc).login()
|
48
|
+
token = service.token
|
49
|
+
|
50
|
+
new_arguments = @splunkrc.clone
|
51
|
+
new_arguments.delete(:username)
|
52
|
+
new_arguments.delete(:password)
|
53
|
+
new_arguments[:token] = token
|
54
|
+
|
55
|
+
new_service = Context.new(new_arguments)
|
56
|
+
assert_not_nil(new_service.token)
|
57
|
+
assert_logged_in(new_service)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_failed_login()
|
61
|
+
args = @splunkrc.clone()
|
62
|
+
args[:username] = args[:username] + "-boris"
|
63
|
+
service = Context.new(args)
|
64
|
+
|
65
|
+
assert_raises(SplunkHTTPError) {service.login()}
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_multiple_logins_are_nops()
|
69
|
+
service = Context.new(@splunkrc).login()
|
70
|
+
assert_logged_in(service)
|
71
|
+
|
72
|
+
assert_nothing_raised() {service.login()}
|
73
|
+
assert_logged_in(service)
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_logout
|
77
|
+
service = Context.new(@splunkrc).login()
|
78
|
+
assert_logged_in(service)
|
79
|
+
|
80
|
+
service.logout()
|
81
|
+
assert_not_logged_in(service)
|
82
|
+
|
83
|
+
service.login()
|
84
|
+
assert_logged_in(service)
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_connect()
|
88
|
+
service = Context.new(@splunkrc).login()
|
89
|
+
socket = service.connect()
|
90
|
+
# Send a manual HTTP request
|
91
|
+
socket.write("GET /services/data/indexes HTTP/1.1\r\n")
|
92
|
+
socket.write("Authorization: Splunk #{service.token}\r\n")
|
93
|
+
socket.write("\r\n")
|
94
|
+
response = socket.readlines()
|
95
|
+
assert_equal("HTTP/1.1 200 OK", response[0].strip)
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_server_accepting_connections?
|
99
|
+
values = @splunkrc.clone()
|
100
|
+
values[:port] = 10253
|
101
|
+
service = Context.new(values)
|
102
|
+
assert_false(service.server_accepting_connections?)
|
103
|
+
|
104
|
+
service = Context.new(@splunkrc)
|
105
|
+
assert_true(service.server_accepting_connections?)
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_info
|
109
|
+
assert_true(@service.info.has_key?("version"))
|
110
|
+
end
|
111
|
+
|
112
|
+
def test_splunk_version
|
113
|
+
version = @service.splunk_version
|
114
|
+
assert_true(version.is_a?(Array))
|
115
|
+
version.each() do |v|
|
116
|
+
assert_true(v.is_a?(Integer))
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
data/test/test_entity.rb
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
require 'splunk-sdk-ruby'
|
3
|
+
|
4
|
+
include Splunk
|
5
|
+
|
6
|
+
class EntityTestCase < TestCaseWithSplunkConnection
|
7
|
+
def setup
|
8
|
+
super
|
9
|
+
@app_args = {
|
10
|
+
"author" => "Harry Belten",
|
11
|
+
"label" => "A random app",
|
12
|
+
"description" => "Sumer is icumen in",
|
13
|
+
}
|
14
|
+
@entity = @service.apps.create(temporary_name(), @app_args)
|
15
|
+
end
|
16
|
+
|
17
|
+
def teardown
|
18
|
+
@entity.delete()
|
19
|
+
|
20
|
+
if @service.splunk_version[0..1] != [4,2]
|
21
|
+
clear_restart_message(@service)
|
22
|
+
end
|
23
|
+
|
24
|
+
super
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_fetch
|
28
|
+
@app_args.each() do |key, value|
|
29
|
+
assert_equal(value, @entity[key])
|
30
|
+
assert_equal(value, @entity.fetch(key))
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_fetch_with_default
|
35
|
+
assert_equal("boris", @entity.fetch("nonexistant key", "boris"))
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_update_and_refresh
|
39
|
+
@entity.update("label" => "This is a test")
|
40
|
+
assert_equal(@app_args["label"], @entity["label"])
|
41
|
+
@entity.refresh()
|
42
|
+
assert_equal("This is a test", @entity["label"])
|
43
|
+
|
44
|
+
@entity["label"] = "Oh the vogonity!"
|
45
|
+
assert_equal("This is a test", @entity["label"])
|
46
|
+
@entity.refresh()
|
47
|
+
assert_equal("Oh the vogonity!", @entity["label"])
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_read
|
51
|
+
state = @entity.read()
|
52
|
+
assert_false(state.empty?)
|
53
|
+
state.each() do |key, value|
|
54
|
+
assert_equal(value, @entity[key])
|
55
|
+
end
|
56
|
+
state.each() do |key, value|
|
57
|
+
state[key] = "boris"
|
58
|
+
assert_not_equal("boris", value)
|
59
|
+
assert_equal(value, @entity[key])
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_state_with_field_list
|
64
|
+
state = @entity.read("label", "description")
|
65
|
+
assert_false(state.empty?)
|
66
|
+
state.each() do |key, value|
|
67
|
+
assert_equal(value, @entity[key])
|
68
|
+
end
|
69
|
+
state.each() do |key, value|
|
70
|
+
state[key] = "boris"
|
71
|
+
assert_not_equal("boris", value)
|
72
|
+
assert_equal(value, @entity[key])
|
73
|
+
end
|
74
|
+
|
75
|
+
assert_equal(@entity.read("label", "description"),
|
76
|
+
@entity.read(["label", "description"]))
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_disable_enable
|
80
|
+
# We have to refresh first, because apps on some versions of Splunk
|
81
|
+
# do not have all their keys (including "disabled") when first created.
|
82
|
+
@entity.refresh()
|
83
|
+
|
84
|
+
assert_equal('0', @entity["disabled"])
|
85
|
+
|
86
|
+
@entity.disable()
|
87
|
+
@entity.refresh()
|
88
|
+
assert_equal('1', @entity["disabled"])
|
89
|
+
|
90
|
+
@entity.enable()
|
91
|
+
@entity.refresh()
|
92
|
+
assert_equal('0', @entity["disabled"])
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,250 @@
|
|
1
|
+
if ENV.has_key?("COVERAGE")
|
2
|
+
require "simplecov"
|
3
|
+
SimpleCov.start() do
|
4
|
+
add_filter("test")
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
# This line is required to let RubyMine run the test suite,
|
9
|
+
# since otherwise random packages load other random packages
|
10
|
+
# in random order and clobber RubyMine's configuration.
|
11
|
+
# See http://youtrack.jetbrains.com/issue/RUBY-11922
|
12
|
+
$:.unshift($:.select {|i| i.include? '/patch/'}).flatten!
|
13
|
+
|
14
|
+
require 'test-unit'
|
15
|
+
|
16
|
+
$:.unshift File.expand_path(File.join([File.dirname(__FILE__), "..", "lib"]))
|
17
|
+
|
18
|
+
def read_splunkrc
|
19
|
+
file = File.new(File.expand_path("~/.splunkrc"))
|
20
|
+
options = {
|
21
|
+
:host => 'localhost',
|
22
|
+
:port => 8089,
|
23
|
+
:username => 'admin',
|
24
|
+
:password => 'changeme',
|
25
|
+
:scheme => 'https',
|
26
|
+
:version => '5.0'
|
27
|
+
}
|
28
|
+
file.readlines.each do |raw_line|
|
29
|
+
line = raw_line.strip()
|
30
|
+
if line.start_with?("\#") or line.length == 0
|
31
|
+
next
|
32
|
+
else
|
33
|
+
raw_key, raw_value = line.split('=', limit=2)
|
34
|
+
key = raw_key.strip().intern
|
35
|
+
value = raw_value.strip()
|
36
|
+
|
37
|
+
if key == 'port'
|
38
|
+
value = Integer(value)
|
39
|
+
end
|
40
|
+
|
41
|
+
options[key] = value
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
options
|
46
|
+
end
|
47
|
+
|
48
|
+
def nokogiri_available?
|
49
|
+
begin
|
50
|
+
require 'nokogiri'
|
51
|
+
return true
|
52
|
+
rescue LoadError
|
53
|
+
return false
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
require 'securerandom'
|
58
|
+
def temporary_name
|
59
|
+
return "delete-me-" + SecureRandom.uuid()
|
60
|
+
end
|
61
|
+
|
62
|
+
DEFAULT_RESTART_TIMEOUT = 500 # seconds
|
63
|
+
|
64
|
+
class TestCaseWithSplunkConnection < Test::Unit::TestCase
|
65
|
+
def setup
|
66
|
+
super
|
67
|
+
@splunkrc = read_splunkrc()
|
68
|
+
@service = Splunk::Service.new(@splunkrc).login()
|
69
|
+
@installed_apps = []
|
70
|
+
|
71
|
+
if @service.server_requires_restart?
|
72
|
+
puts "Previous test left server in a state requiring a restart."
|
73
|
+
checked_restart(@service)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def teardown
|
78
|
+
if @service.server_requires_restart?()
|
79
|
+
fail("Test left server in a state requiring restart.")
|
80
|
+
end
|
81
|
+
|
82
|
+
if @service.splunk_version[0..1] != [4,2]
|
83
|
+
@installed_apps.each() do |app_name|
|
84
|
+
@service.apps.delete(app_name)
|
85
|
+
assert_eventually_true() do
|
86
|
+
!@service.apps.has_key?(app_name)
|
87
|
+
end
|
88
|
+
if @service.server_requires_restart?
|
89
|
+
clear_restart_message(@service)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
@installed_apps.clear()
|
95
|
+
|
96
|
+
super
|
97
|
+
end
|
98
|
+
|
99
|
+
def assert_eventually_true(timeout=30, &block)
|
100
|
+
Timeout::timeout(timeout) do
|
101
|
+
while !block.call()
|
102
|
+
sleep(0.2)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def assert_logged_in(service)
|
108
|
+
assert_nothing_raised do
|
109
|
+
# A request to data/indexes requires you to be logged in.
|
110
|
+
service.request(:method=>:GET,
|
111
|
+
:resource=>["data", "indexes"])
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def assert_not_logged_in(service)
|
116
|
+
begin
|
117
|
+
service.request(:method=>:GET,
|
118
|
+
:resource=>["data", "indexes"])
|
119
|
+
rescue SplunkHTTPError => err
|
120
|
+
assert_equal(401, err.code, "Expected HTTP status code 401, found: #{err.code}")
|
121
|
+
else
|
122
|
+
fail("Context is logged in.")
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
# Clear any restart messages on _service_.
|
127
|
+
#
|
128
|
+
# If there was no restart message, raises an error. (We want all the restarts
|
129
|
+
# and restart messages carefully controlled in the test suite.)
|
130
|
+
#
|
131
|
+
def clear_restart_message(service)
|
132
|
+
if !service.server_requires_restart?
|
133
|
+
raise StandardError.new("Tried to clear restart message " +
|
134
|
+
"when there was none.")
|
135
|
+
end
|
136
|
+
begin
|
137
|
+
service.request(:method => :DELETE,
|
138
|
+
:resource => ["messages", "restart_required"])
|
139
|
+
rescue SplunkHTTPError => err
|
140
|
+
if err.code != 404
|
141
|
+
raise err
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def has_app_collection?(service)
|
147
|
+
collection_name = 'sdk-app-collection'
|
148
|
+
return service.apps.has_key?(collection_name)
|
149
|
+
end
|
150
|
+
|
151
|
+
def install_app_from_collection(name)
|
152
|
+
collection_name = 'sdk-app-collection'
|
153
|
+
if !@service.apps.has_key?(collection_name)
|
154
|
+
raise StandardError("#{collection_name} not installed in Splunk.")
|
155
|
+
end
|
156
|
+
|
157
|
+
app_path = path_in_app(collection_name, ["build", name+".tar"])
|
158
|
+
args = {"update" => 1, "name" => app_path}
|
159
|
+
begin
|
160
|
+
@service.request(:method => :POST,
|
161
|
+
:resource => ["apps", "appinstall"],
|
162
|
+
:body => args)
|
163
|
+
@installed_apps << name
|
164
|
+
rescue SplunkHTTPError => err
|
165
|
+
if err.code == 40
|
166
|
+
raise StandardError("App #{name} not found in app collection")
|
167
|
+
else
|
168
|
+
raise err
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
# Return a path to *path_components* in *app_name*.
|
174
|
+
#
|
175
|
+
# `path_in_app` is used to refer to files in applications installed with
|
176
|
+
# `install_app_from_collection`. For example, the app `file_to_upload` in
|
177
|
+
# the collection contains `log.txt`. To get the path to it, call::
|
178
|
+
#
|
179
|
+
# path_in_app('file_to_upload', ['log.txt'])
|
180
|
+
#
|
181
|
+
# The path to `setup.xml` in `has_setup_xml` would be fetched with::
|
182
|
+
#
|
183
|
+
# path_in_app('has_setup_xml', ['default', 'setup.xml'])
|
184
|
+
#
|
185
|
+
# path_in_app` figures out the correct separator to use (based on whether
|
186
|
+
# splunkd is running on Windows or Unix) and joins the elements in
|
187
|
+
# *path_components* into a path relative to the application specified by
|
188
|
+
# *app_name*.
|
189
|
+
#
|
190
|
+
# *path_components* should be a list of strings giving the components.
|
191
|
+
# This function will try to figure out the correct separator (/ or \)
|
192
|
+
# for the platform that splunkd is running on and construct the path
|
193
|
+
# as needed.
|
194
|
+
#
|
195
|
+
# :return: A string giving the path.
|
196
|
+
#
|
197
|
+
def path_in_app(app_name, path_components)
|
198
|
+
splunk_home = @service.settings["SPLUNK_HOME"]
|
199
|
+
if splunk_home.include?("\\")
|
200
|
+
# This clause must come first, since Windows machines may
|
201
|
+
# have mixed \ and / in their paths.
|
202
|
+
separator = "\\"
|
203
|
+
elsif splunk_home.include?("/")
|
204
|
+
separator = "/"
|
205
|
+
else
|
206
|
+
raise StandardError("No separators in $SPLUNK_HOME. Can't determine " +
|
207
|
+
"what file separator to use.")
|
208
|
+
end
|
209
|
+
|
210
|
+
app_path = ([splunk_home, "etc", "apps", app_name] + path_components).
|
211
|
+
join(separator)
|
212
|
+
return app_path
|
213
|
+
end
|
214
|
+
|
215
|
+
|
216
|
+
# Create a new restart message on _service_.
|
217
|
+
#
|
218
|
+
# Optionally you can specify a value for the restart message, or it will
|
219
|
+
# default to "Ruby SDK test suite asked for a restart."
|
220
|
+
#
|
221
|
+
def set_restart_message(service,
|
222
|
+
message="Ruby SDK test suite asked for a restart.")
|
223
|
+
service.request(:method => :POST,
|
224
|
+
:namespace => Splunk::namespace(:sharing => "default"),
|
225
|
+
:resource => ["messages"],
|
226
|
+
:body => {"name" => "restart_required",
|
227
|
+
"value" => "Message set by restart method" +
|
228
|
+
" of the Splunk Ruby SDK"})
|
229
|
+
end
|
230
|
+
|
231
|
+
# Restart Splunk and wait for it to come back up, but only if it needs it.
|
232
|
+
#
|
233
|
+
# Throws an error if this is called on a Splunk instance that does not
|
234
|
+
# need to be restarted.
|
235
|
+
#
|
236
|
+
def checked_restart(service)
|
237
|
+
if !service.server_requires_restart?
|
238
|
+
raise StandardError("Tried to restart a Splunk instance that" +
|
239
|
+
" does not need it.")
|
240
|
+
else
|
241
|
+
service.restart(DEFAULT_RESTART_TIMEOUT)
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
# Restarts a Splunk instance whether it needs it or not.
|
246
|
+
#
|
247
|
+
def unchecked_restart(service)
|
248
|
+
service.restart(DEFAULT_RESTART_TIMEOUT)
|
249
|
+
end
|
250
|
+
end
|