class ConfigurationFileTestCase

Public Instance Methods

setup() click to toggle source
Calls superclass method TestCaseWithSplunkConnection#setup
# File test/test_configuration_file.rb, line 7
def setup
  super
  # We cannot delete configuration files from the REST API
  # so we create a temporary application to do our manipulation
  # in. When we delete it at the end of the test run, the
  # configuration modifications will be deleted as well.
  @container_app_name = temporary_name()
  @service.apps.create(@container_app_name)

  # Now reconnect in the test app context.
  app_service_args = @splunkrc.clone()
  app_service_args[:namespace] =
      Splunk::namespace(:sharing => "app", :app => @container_app_name)
  @app_service = Splunk::Service.new(app_service_args).login()
  @confs = @app_service.confs
end
teardown() click to toggle source
Calls superclass method TestCaseWithSplunkConnection#teardown
# File test/test_configuration_file.rb, line 24
def teardown
  if @service.server_requires_restart?
    fail("Test left Splunk in a state requiring restart.")
  end

  @service.apps.delete(@container_app_name)
  if @service.server_requires_restart?
    clear_restart_message(@service)
  end

  super
end
test_create_and_delete() click to toggle source
# File test/test_configuration_file.rb, line 37
def test_create_and_delete
  file_name = temporary_name()
  assert_false(@confs.has_key?(file_name))
  assert_false(@confs.contains?(file_name))
  assert_false(@confs.member?(file_name))
  assert_false(@confs.key?(file_name))
  assert_false(@confs.include?(file_name))

  conf = @confs.create(file_name)
  assert_equal(file_name, conf.name)
  assert_equal(0, conf.length)

  assert_true(@confs.has_key?(file_name))
  assert_true(@confs.contains?(file_name))
  assert_true(@confs.member?(file_name))
  assert_true(@confs.key?(file_name))
  assert_true(@confs.include?(file_name))

  assert_raises(Splunk::IllegalOperation) { @confs.delete(file_name) }
end
test_create_and_delete_stanzas() click to toggle source
# File test/test_configuration_file.rb, line 103
def test_create_and_delete_stanzas
  file_name = temporary_name()
  conf = @confs.create(file_name)

  assert_equal(0, conf.length())

  n = 5 + rand(5)
  stanza_names = []
  (1..n).each() do
    stanza_name = temporary_name()
    stanza_names << stanza_name
    conf.create(stanza_name)
  end

  assert_equal(n, conf.length())

  stanza_names.each() do |name|
    conf.delete(name)
    n -= 1
    assert_equal(n, conf.length())
  end
end
test_each() click to toggle source
# File test/test_configuration_file.rb, line 126
def test_each
  @confs.each() do |configuration_file|
    assert_true(configuration_file.is_a?(ConfigurationFile))
  end
end
test_each_and_values() click to toggle source
# File test/test_configuration_file.rb, line 93
def test_each_and_values
  each_names = []
  @confs.each() { |entity| each_names << entity.name }

  values_names = @confs.values().map() { |e| e.name }

  assert_false(each_names.empty?)
  assert_equal(each_names, values_names)
end
test_error_from_fetch() click to toggle source

Check that server errors during fetch are encapsulated sensibly.

# File test/test_configuration_file.rb, line 84
def test_error_from_fetch
  new_service = Splunk::Service.new(@splunkrc)
  new_confs = new_service.confs()
  new_service.logout()
  assert_raise(SplunkHTTPError) do
    new_confs[temporary_name()]
  end
end
test_fetch() click to toggle source
# File test/test_configuration_file.rb, line 58
def test_fetch
  file_name = temporary_name()
  created_conf = @confs.create(file_name)

  fetched_conf = @confs.fetch(file_name)
  assert_true(fetched_conf.is_a?(ConfigurationFile))
  assert_equal(created_conf.name, fetched_conf.name)

  bracket_fetched_conf = @confs[file_name]
  assert_true(fetched_conf.is_a?(ConfigurationFile))
  assert_equal(created_conf.name, bracket_fetched_conf.name)
end
test_fetch_nonexistent() click to toggle source

Check that fetching nonexistent configuration files returns nil.

# File test/test_configuration_file.rb, line 74
def test_fetch_nonexistent
  nonexistent_name = temporary_name()

  assert_nil(@confs[nonexistent_name])
  assert_nil(@confs.fetch(nonexistent_name))
end
test_submit_to_stanza() click to toggle source
# File test/test_configuration_file.rb, line 132
def test_submit_to_stanza
  file_name = temporary_name()
  conf = @confs.create(file_name)

  stanza_name = temporary_name()
  stanza = conf.create(stanza_name)

  assert_equal(0, stanza.length())
  stanza.submit(:boris => "natasha", :hilda => "moose on the roof")
  stanza.refresh()
  assert_equal(2, stanza.length())
  assert_equal("natasha", stanza["boris"])
  assert_equal("moose on the roof", stanza["hilda"])
end