spec/localhost.rb in zabbixapi-0.3.0 vs spec/localhost.rb in zabbixapi-0.4.1
- old
+ new
@@ -1,112 +1,264 @@
require 'zabbixapi'
-require 'json'
# settings
-api_url = 'http://localhost/zabbix/api_jsonrpc.php'
+api_url = 'http://localhost/api_jsonrpc.php'
api_login = 'Admin'
api_password = 'zabbix'
+zbx = ZabbixApi.connect(
+ :url => api_url,
+ :user => api_login,
+ :password => api_password,
+ :debug => false
+)
-zbx = Zabbix::ZabbixApi.new(api_url, api_login, api_password)
-#zbx.debug = true
+hostgroup = "hostgroup______1"
+template = "template______1"
+application = "application_____1"
+item = "item_____1"
+host = "hostname____1"
+trigger = "trigger____1"
+user = "user____1"
+user2 = "user____2"
+graph = "graph___a"
-describe Zabbix::ZabbixApi, "test_api" do
+describe ZabbixApi, "test_api" do
- # 01. Create group
- it "Create some group" do
- result = zbx.add_group('some_group')
- result.should be_kind_of(Integer)
+ it "SERVER: Get version api" do
+ zbx.server.version.should be_kind_of(String)
end
- # 02. Get group_id
- it "Get group_id" do
- result = zbx.get_group_id('some_group')
- result.should be_kind_of(Integer)
+ it "HOSTGROUP: Create" do
+ zbx.hostgroups.create(:name => hostgroup).should be_kind_of(Integer)
end
- # 03. Get unknown group_id
- it "Get unknown group" do
- result = zbx.get_group_id('___some_group')
- result.should be_nil
+ it "HOSTGROUP: Find" do
+ zbx.hostgroups.get_id(:name => hostgroup).should be_kind_of(Integer)
end
- # 04. Create host
- host_options = {
- "ip" => '127.0.0.1',
- "dns" => 'my.example.com',
- "host" => 'my.example.com',
- "useip" => 1,
- "groups" => [1]
- }
- it "Create host" do
- result = zbx.add_host(host_options)
- result.should be_kind_of(Integer)
+ it "HOSTGROUP: Find unknown" do
+ zbx.hostgroups.get_id(:name => "#{hostgroup}______").should be_kind_of(NilClass)
end
- # 05. Get host
- it "Get host by name" do
- result = zbx.get_host_id('my.example.com')
- result.should be_kind_of(Integer)
+ it "TEMPLATE: Create" do
+ zbx.templates.create(
+ :host => template,
+ :groups => [:groupid => zbx.hostgroups.get_id(:name => hostgroup)]
+ ).should be_kind_of(Integer)
end
- # 06. Get unknown host
- it "Get unknown host by name" do
- result = zbx.get_host_id('___my.example.com')
- result.should be_nil
+ it "TEMPLATE: Check full data" do
+ zbx.templates.get_full_data(:host => template)[0]['host'].should be_kind_of(String)
end
- # 07. Delete host
- it "Delete host" do
- result = zbx.delete_host('my.example.com')
- result.should be_kind_of(Integer)
+ it "TEMPLATE: Find" do
+ zbx.templates.get_id(:host => template).should be_kind_of(Integer)
end
- # 08. Delete unknown host
- it "Delete unknown host" do
- result = zbx.delete_host('__my.example.com')
- result.should be_nil
+ it "TEMPLATE: Find unknown" do
+ zbx.templates.get_id(:host => "#{template}_____").should be_kind_of(NilClass)
end
- # 09. Delete group
- it "Delete some group" do
- result = zbx.delete_group('some_group')
- result.should be_kind_of(Integer)
+ it "APPLICATION: Create" do
+ zbx.applications.create(
+ :name => application,
+ :hostid => zbx.templates.get_id(:host => template)
+ )
end
- # 10. Delete unknown group
- it "Delete unknown group" do
- result = zbx.delete_group('___some_group')
- result.should be_nil
+ it "APPLICATION: Full info check" do
+ zbx.applications.get_full_data(:name => application)[0]['applicationid'].should be_kind_of(String)
end
- # 11. Mediatype create
- mediatype_options = {
- 'type' => '0', #email
- 'description' => 'example_mediatype',
- 'smtp_server' => 'test.company.com',
- 'smtp_helo' => 'test.company.com',
- 'smtp_email' => 'test@test.company.com',
- }
- it "Create mediatype" do
- result = zbx.add_mediatype(mediatype_options)
- result.should be_kind_of(Integer)
+ it "APPLICATION: Find" do
+ zbx.applications.get_id(:name => application).should be_kind_of(Integer)
end
- # 12. Mediatype unknown delete
- it "Delete unknown mediatype" do
- result = zbx.delete_mediatype('__example_mediatype')
- result.should be_nil
+
+ it "APPLICATION: Find unknown" do
+ zbx.applications.get_id(:name => "#{application}___").should be_kind_of(NilClass)
end
- # 13. Mediatype delete
- it "Delete mediatype" do
- result = zbx.delete_mediatype('example_mediatype')
- result.should be_kind_of(Integer)
+ it "ITEM: Create" do
+ zbx.items.create(
+ :description => item,
+ :key_ => "proc.num[aaa]",
+ :hostid => zbx.templates.get_id(:host => template),
+ :applications => [zbx.applications.get_id(:name => application)]
+ )
end
-end
+ it "ITEM: Full info check" do
+ zbx.items.get_full_data(:description => item)[0]['itemid'].should be_kind_of(String)
+ end
-describe Zabbix::ZabbixApi, "test_examples" do
- it "Test all examples" do
- system("examples/populate_new_zabbix.sh development")
+ it "ITEM: Find" do
+ zbx.items.get_id(:description => item).should be_kind_of(Integer)
end
+
+ it "ITEM: Update" do
+ zbx.items.update(
+ :itemid => zbx.items.get_id(:description => item),
+ :status => 0
+ ).should be_kind_of(Integer)
+ end
+
+ it "ITEM: Get unknown" do
+ zbx.items.get_id(:description => "#{item}_____")
+ end
+
+ it "HOST: Create" do
+ zbx.hosts.create(
+ :host => host,
+ :ip => "10.20.48.88",
+ :groups => [:groupid => zbx.hostgroups.get_id(:name => hostgroup)]
+ ).should be_kind_of(Integer)
+ end
+
+ it "HOST: Find unknown" do
+ zbx.hosts.get_id(:host => "#{host}___").should be_kind_of(NilClass)
+ end
+
+ it "HOST: Find" do
+ zbx.hosts.get_id(:host => host).should be_kind_of(Integer)
+ end
+
+ it "HOST: Update" do
+ zbx.hosts.update(
+ :hostid => zbx.hosts.get_id(:host => host),
+ :status => 0
+ )
+ end
+
+ it "TEMPLATE: Get all templates linked with host" do
+ zbx.templates.get_ids_by_host(
+ :hostids => [zbx.hosts.get_id(:host => host)]
+ ).should be_kind_of(Array)
+ end
+
+ it "HOSTS: Linked host with templates" do
+ zbx.hosts.unlink_templates(
+ :hosts_id => [zbx.hosts.get_id(:host => host)],
+ :templates_id => [zbx.templates.get_id(:host => template)]
+ ).should be_kind_of(TrueClass)
+ end
+
+ it "TEMPLATE: Unlink host from templates" do
+
+ end
+
+ it "TEMPLATE: Get all" do
+ zbx.templates.all.should be_kind_of(Hash)
+ end
+
+ it "TRIGGER: Create" do
+ zbx.triggers.create(
+ :description => trigger,
+ :expression => "{#{template}:proc.num[aaa].last(0)}<1",
+ :comments => "Bla-bla is faulty (disaster)",
+ :priority => 5,
+ :status => 0,
+ :templateid => 0,
+ :type => 0
+ ).should be_kind_of(Integer)
+ end
+
+ it "TRIGGER: Find" do
+ zbx.triggers.get_id(:description => trigger).should be_kind_of(Integer)
+ end
+
+ it "GRAPH: Create" do
+ gitems = {
+ :itemid => zbx.items.get_id(:description => item),
+ :calc_fnc => "2",
+ :type => "0",
+ :periods_cnt => "5"
+ }
+ zbx.graphs.create(
+ :gitems => [gitems],
+ :show_triggers => "0",
+ :name => graph,
+ :width => "900",
+ :height => "200"
+ ).should be_kind_of(Integer)
+ #
+ end
+
+ it "GRAPH: Find" do
+ zbx.graphs.get_id( :name => graph ).should be_kind_of(Integer)
+ end
+
+ it "GRAPH: Update" do
+ zbx.graphs.update(
+ :graphid => zbx.graphs.get_id(
+ :name => graph,
+ :hostid => zbx.hosts.get_id(:host => host)
+ ),
+ :ymax_type => 1
+ ).should be_kind_of(Integer)
+ end
+
+ it "GRAPH: Delete" do
+ zbx.graphs.delete(zbx.graphs.get_id(:name => graph)).should be_kind_of(Integer)
+ end
+
+ it "TRIGGER: Delete" do
+ zbx.triggers.delete( zbx.triggers.get_id(:description => trigger) ).should be_kind_of(Integer)
+ end
+
+ it "HOST: Delete" do
+ zbx.hosts.delete( zbx.hosts.get_id(:host => host) ).should be_kind_of(Integer)
+ end
+
+ it "ITEM: Delete" do
+ zbx.items.delete(
+ zbx.items.get_id(:description => item)
+ ).should be_kind_of(Integer)
+ end
+
+ it "APPLICATION: Delete" do
+ zbx.applications.delete( zbx.applications.get_id(:name => application) )
+ end
+
+ it "TEMPLATE: Delete" do
+ zbx.templates.delete(zbx.templates.get_id(:host => template))
+ end
+
+ it "HOSTGROUP: Delete" do
+ zbx.hostgroups.delete(
+ zbx.hostgroups.get_id(:name => hostgroup)
+ ).should be_kind_of(Integer)
+ end
+
+ it "USER: Create" do
+ zbx.users.create(
+ :alias => "Test #{user}",
+ :name => user,
+ :surname => user,
+ :passwd => user
+ ).should be_kind_of(Integer)
+ end
+
+ it "USER: Find" do
+ zbx.users.get_full_data(:name => user)[0]['name'].should be_kind_of(String)
+ end
+
+ it "USER: Update" do
+ zbx.users.update(:userid => zbx.users.get_id(:name => user), :name => user2).should be_kind_of(Integer)
+ end
+
+ it "USER: Find unknown" do
+ zbx.users.get_id(:name => "#{user}_____")
+ end
+
+ it "USER: Delete" do
+ zbx.users.delete(zbx.users.get_id(:name => user2)).should be_kind_of(Integer)
+ end
+
+ it "QUERY" do
+ zbx.query(
+ :method => "apiinfo.version",
+ :params => {}
+ ).should be_kind_of(String)
+ end
+
end