rubix 0.4.3 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/bin/zabbix_api +45 -27
- data/lib/rubix.rb +17 -0
- data/lib/rubix/associations.rb +7 -1
- data/lib/rubix/associations/belongs_to_action.rb +31 -0
- data/lib/rubix/associations/belongs_to_media_type.rb +33 -0
- data/lib/rubix/associations/belongs_to_user.rb +33 -0
- data/lib/rubix/associations/belongs_to_user_group.rb +32 -0
- data/lib/rubix/associations/has_many_conditions.rb +23 -0
- data/lib/rubix/associations/has_many_user_groups.rb +32 -0
- data/lib/rubix/associations/has_many_users.rb +32 -0
- data/lib/rubix/models.rb +17 -10
- data/lib/rubix/models/action.rb +117 -0
- data/lib/rubix/models/application.rb +1 -3
- data/lib/rubix/models/condition.rb +99 -0
- data/lib/rubix/models/host.rb +9 -11
- data/lib/rubix/models/host_group.rb +1 -4
- data/lib/rubix/models/item.rb +12 -27
- data/lib/rubix/models/media_type.rb +19 -21
- data/lib/rubix/models/medium.rb +86 -0
- data/lib/rubix/models/model.rb +53 -1
- data/lib/rubix/models/operation.rb +134 -0
- data/lib/rubix/models/script.rb +49 -0
- data/lib/rubix/models/template.rb +2 -2
- data/lib/rubix/models/trigger.rb +8 -9
- data/lib/rubix/models/user.rb +145 -0
- data/lib/rubix/models/user_group.rb +94 -0
- data/lib/rubix/models/user_macro.rb +10 -20
- data/spec/requests/action_request_spec.rb +45 -0
- data/spec/requests/connection_request_spec.rb +3 -3
- data/spec/requests/media_type_request_spec.rb +8 -8
- data/spec/requests/script_request_spec.rb +54 -0
- data/spec/requests/user_group_request_spec.rb +97 -0
- data/spec/requests/user_request_spec.rb +84 -0
- data/spec/support/integration_helper.rb +105 -33
- data/spec/test.yml +0 -2
- metadata +22 -4
@@ -5,25 +5,24 @@ module Rubix
|
|
5
5
|
#
|
6
6
|
# == Properties & Finding ==
|
7
7
|
#
|
8
|
+
|
9
|
+
attr_reader :name
|
10
|
+
def set_name n, validate=true
|
11
|
+
return if n.nil? || n.empty?
|
12
|
+
raise ValidationError.new("Cannot change the name of a UserMacro once it's created.") if validate && @name && (!new_record?)
|
13
|
+
@name = n
|
14
|
+
end
|
8
15
|
|
9
|
-
|
16
|
+
zabbix_attr :value, :required => true
|
10
17
|
|
11
18
|
def initialize properties={}
|
12
19
|
super(properties)
|
13
|
-
|
14
|
-
|
15
|
-
|
20
|
+
set_name(properties[:name] || self.class.unmacro_name(properties[:macro]), false)
|
21
|
+
|
16
22
|
self.host = properties[:host]
|
17
23
|
self.host_id = properties[:host_id]
|
18
24
|
end
|
19
25
|
|
20
|
-
attr_reader :name
|
21
|
-
def name= n
|
22
|
-
return if n.nil? || n.empty?
|
23
|
-
raise ValidationError.new("Cannot change the name of a UserMacro once it's created.") if @name && (!new_record?)
|
24
|
-
@name = n
|
25
|
-
end
|
26
|
-
|
27
26
|
def self.unmacro_name name
|
28
27
|
(name || '').gsub(/^\{\$/, '').gsub(/\}$/, '').upcase
|
29
28
|
end
|
@@ -50,15 +49,6 @@ module Rubix
|
|
50
49
|
|
51
50
|
include Associations::BelongsToHost
|
52
51
|
|
53
|
-
#
|
54
|
-
# == Validation ==
|
55
|
-
#
|
56
|
-
|
57
|
-
def validate
|
58
|
-
raise ValidationError.new("A user macro must have both a 'name' and a 'value'") if name.nil? || name.strip.empty? || value.nil? || value.strip.empty?
|
59
|
-
true
|
60
|
-
end
|
61
|
-
|
62
52
|
#
|
63
53
|
# == Requests ==
|
64
54
|
#
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Actions" do
|
4
|
+
|
5
|
+
before do
|
6
|
+
integration_test
|
7
|
+
@user_group = ensure_save(Rubix::UserGroup.new(:name => 'rubix_spec_user_group_1'))
|
8
|
+
@user = ensure_save(Rubix::User.new(:username => 'rubix_spec_user_1', :first_name => 'rubix', :last_name => 'user', :password => 'pass'))
|
9
|
+
end
|
10
|
+
|
11
|
+
after do
|
12
|
+
truncate_all_tables
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "when not existing" do
|
16
|
+
|
17
|
+
it "returns nil on find" do
|
18
|
+
Rubix::Action.find(:name => 'rubix_spec_action_1').should be_nil
|
19
|
+
end
|
20
|
+
|
21
|
+
it "can be created" do
|
22
|
+
a = Rubix::Action.new(:name => 'rubix_spec_action_1', :operations => [{:user_group => @user_group}])
|
23
|
+
a.save.should be_true
|
24
|
+
|
25
|
+
na = Rubix::Action.find(:name => 'rubix_spec_action_1')
|
26
|
+
na.should_not be_nil
|
27
|
+
na.name.should == 'rubix_spec_action_1'
|
28
|
+
na.operations.size.should == 1
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "when existing" do
|
34
|
+
before do
|
35
|
+
@action = ensure_save(Rubix::Action.new(:name => 'rubix_spec_action_1', :operations => [{:user_group => @user_group}]))
|
36
|
+
end
|
37
|
+
|
38
|
+
it "can be destroyed" do
|
39
|
+
@action.destroy
|
40
|
+
Rubix::Action.find(:name => 'rubix_spec_action_1').should be_nil
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -15,7 +15,7 @@ describe Rubix::Connection do
|
|
15
15
|
response.should_not be_nil
|
16
16
|
response.code.to_i.should == 200
|
17
17
|
response.body.should_not include('guest')
|
18
|
-
response.body.should include(
|
18
|
+
response.body.should include(Rubix::IntegrationHelper::INTEGRATION_USER)
|
19
19
|
end
|
20
20
|
|
21
21
|
it "can perform an authorized POST request to the homepage" do
|
@@ -23,7 +23,7 @@ describe Rubix::Connection do
|
|
23
23
|
response.should_not be_nil
|
24
24
|
response.code.to_i.should == 200
|
25
25
|
response.body.should_not include('guest')
|
26
|
-
response.body.should include(
|
26
|
+
response.body.should include(Rubix::IntegrationHelper::INTEGRATION_USER)
|
27
27
|
end
|
28
28
|
|
29
29
|
it "can perform an authorized multipart POST request to the homepage" do
|
@@ -31,7 +31,7 @@ describe Rubix::Connection do
|
|
31
31
|
response.should_not be_nil
|
32
32
|
response.code.to_i.should == 200
|
33
33
|
response.body.should_not include('guest')
|
34
|
-
response.body.should include(
|
34
|
+
response.body.should include(Rubix::IntegrationHelper::INTEGRATION_USER)
|
35
35
|
end
|
36
36
|
|
37
37
|
end
|
@@ -13,11 +13,11 @@ describe "MediaTypes" do
|
|
13
13
|
describe "when not existing" do
|
14
14
|
|
15
15
|
it "returns nil on find" do
|
16
|
-
Rubix::MediaType.find(:
|
16
|
+
Rubix::MediaType.find(:name => 'rubix_spec_media_type_1').should be_nil
|
17
17
|
end
|
18
18
|
|
19
19
|
it "can be created" do
|
20
|
-
mt = Rubix::MediaType.new(:
|
20
|
+
mt = Rubix::MediaType.new(:name => 'rubix_spec_media_type_1', :path => 'foo')
|
21
21
|
mt.save.should be_true
|
22
22
|
end
|
23
23
|
|
@@ -26,20 +26,20 @@ describe "MediaTypes" do
|
|
26
26
|
describe "when existing" do
|
27
27
|
|
28
28
|
before do
|
29
|
-
@mt = ensure_save(Rubix::MediaType.new(:
|
29
|
+
@mt = ensure_save(Rubix::MediaType.new(:name => 'rubix_spec_media_type_1', :path => 'foo'))
|
30
30
|
end
|
31
31
|
|
32
32
|
it "can be found" do
|
33
|
-
Rubix::MediaType.find(:
|
33
|
+
Rubix::MediaType.find(:name => 'rubix_spec_media_type_1').should_not be_nil
|
34
34
|
end
|
35
35
|
|
36
36
|
it "can have its properties changed" do
|
37
|
-
@mt.
|
37
|
+
@mt.name = 'rubix_spec_media_type_2'
|
38
38
|
@mt.type = :sms
|
39
39
|
@mt.modem = '/foo/bar'
|
40
40
|
@mt.save
|
41
|
-
Rubix::MediaType.find(:
|
42
|
-
new_mt = Rubix::MediaType.find(:
|
41
|
+
Rubix::MediaType.find(:name => 'rubix_spec_media_type_1').should be_nil
|
42
|
+
new_mt = Rubix::MediaType.find(:name => 'rubix_spec_media_type_2')
|
43
43
|
new_mt.should_not be_nil
|
44
44
|
new_mt.type.should == :sms
|
45
45
|
new_mt.modem.should == '/foo/bar'
|
@@ -47,7 +47,7 @@ describe "MediaTypes" do
|
|
47
47
|
|
48
48
|
it "can be destroyed" do
|
49
49
|
@mt.destroy
|
50
|
-
Rubix::MediaType.find(:
|
50
|
+
Rubix::MediaType.find(:name => 'rubix_spec_media_type_1').should be_nil
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Scripts" do
|
4
|
+
|
5
|
+
before do
|
6
|
+
integration_test
|
7
|
+
end
|
8
|
+
|
9
|
+
after do
|
10
|
+
truncate_all_tables
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "when not existing" do
|
14
|
+
|
15
|
+
it "returns nil on find" do
|
16
|
+
Rubix::Script.find(:name => 'rubix_spec_script_1').should be_nil
|
17
|
+
end
|
18
|
+
|
19
|
+
it "can be created" do
|
20
|
+
s = Rubix::Script.new(:name => 'rubix_spec_script_1', :command => 'foo')
|
21
|
+
s.save.should be_true
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "when existing" do
|
27
|
+
|
28
|
+
before do
|
29
|
+
@s = ensure_save(Rubix::Script.new(:name => 'rubix_spec_script_1', :command => 'foo'))
|
30
|
+
end
|
31
|
+
|
32
|
+
it "can be found" do
|
33
|
+
Rubix::Script.find(:name => 'rubix_spec_script_1').should_not be_nil
|
34
|
+
end
|
35
|
+
|
36
|
+
it "can have its properties changed" do
|
37
|
+
@s.name = 'rubix_spec_script_2'
|
38
|
+
@s.command = 'bar'
|
39
|
+
@s.access = :write
|
40
|
+
@s.save
|
41
|
+
Rubix::Script.find(:name => 'rubix_spec_script_1').should be_nil
|
42
|
+
new_s = Rubix::Script.find(:name => 'rubix_spec_script_2')
|
43
|
+
new_s.should_not be_nil
|
44
|
+
new_s.command.should == 'bar'
|
45
|
+
new_s.access.should == :write
|
46
|
+
end
|
47
|
+
|
48
|
+
it "can be destroyed" do
|
49
|
+
@s.destroy
|
50
|
+
Rubix::Script.find(:name => 'rubix_spec_script_1').should be_nil
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "UserGroups" do
|
4
|
+
|
5
|
+
before do
|
6
|
+
integration_test
|
7
|
+
end
|
8
|
+
|
9
|
+
after do
|
10
|
+
truncate_all_tables
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "when not existing" do
|
14
|
+
|
15
|
+
it "returns nil on find" do
|
16
|
+
Rubix::UserGroup.find(:name => 'rubix_spec_user_group_1').should be_nil
|
17
|
+
end
|
18
|
+
|
19
|
+
it "can be created" do
|
20
|
+
ug = Rubix::UserGroup.new(:name => 'rubix_spec_user_group_1')
|
21
|
+
ug.save.should be_true
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "when existing" do
|
27
|
+
|
28
|
+
before do
|
29
|
+
@ug = ensure_save(Rubix::UserGroup.new(:name => 'rubix_spec_user_group_1'))
|
30
|
+
end
|
31
|
+
|
32
|
+
it "can be found" do
|
33
|
+
Rubix::UserGroup.find(:name => 'rubix_spec_user_group_1').should_not be_nil
|
34
|
+
end
|
35
|
+
|
36
|
+
it "can have its name and settings changed" do
|
37
|
+
@ug.name = 'rubix_spec_user_group_2'
|
38
|
+
@ug.banned = true
|
39
|
+
@ug.api_access = true
|
40
|
+
@ug.debug_mode = true
|
41
|
+
@ug.gui_access = :disabled
|
42
|
+
@ug.save
|
43
|
+
Rubix::UserGroup.find(:name => 'rubix_spec_user_group_1').should be_nil
|
44
|
+
nug = Rubix::UserGroup.find(:name => 'rubix_spec_user_group_2')
|
45
|
+
nug.should_not be_nil
|
46
|
+
nug.name.should == 'rubix_spec_user_group_2'
|
47
|
+
nug.banned.should == true
|
48
|
+
nug.api_access.should == true
|
49
|
+
nug.gui_access.should == :disabled
|
50
|
+
nug.debug_mode.should == true
|
51
|
+
end
|
52
|
+
|
53
|
+
it "can be destroyed" do
|
54
|
+
@ug.destroy
|
55
|
+
Rubix::UserGroup.find(:name => 'rubix_spec_user_group_1').should be_nil
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "linking users to user groups" do
|
60
|
+
|
61
|
+
before do
|
62
|
+
@u1 = ensure_save(Rubix::User.new(:username => 'rubix_spec_user_1', :first_name => 'rubix1', :last_name => 'user1', :password => 'pass1'))
|
63
|
+
@u2 = ensure_save(Rubix::User.new(:username => 'rubix_spec_user_2', :first_name => 'rubix2', :last_name => 'user2', :password => 'pass2'))
|
64
|
+
@u3 = ensure_save(Rubix::User.new(:username => 'rubix_spec_user_3', :first_name => 'rubix3', :last_name => 'user3', :password => 'pass3'))
|
65
|
+
end
|
66
|
+
|
67
|
+
it "can add users on create" do
|
68
|
+
ug = Rubix::UserGroup.new(:name => 'rubix_spec_user_group_1', :users => [@u1, @u2, @u3])
|
69
|
+
ug.save
|
70
|
+
nug = Rubix::UserGroup.find(:name => 'rubix_spec_user_group_1')
|
71
|
+
nug.users.map(&:username).should include(*[@u1, @u2, @u3].map(&:username))
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "with existing users" do
|
75
|
+
|
76
|
+
before do
|
77
|
+
Rubix::UserGroup.new(:name => 'rubix_spec_user_group_1', :users => [@u1, @u2]).save
|
78
|
+
@ug = Rubix::UserGroup.find(:name => 'rubix_spec_user_group_1')
|
79
|
+
end
|
80
|
+
|
81
|
+
it "can will not mess with existing users if it doesn't need to" do
|
82
|
+
@ug.name = 'rubix_spec_user_group_2'
|
83
|
+
@ug.save
|
84
|
+
nug = Rubix::UserGroup.find(:name => 'rubix_spec_user_group_2')
|
85
|
+
nug.users.map(&:username).should include(*[@u1, @u2].map(&:username))
|
86
|
+
end
|
87
|
+
|
88
|
+
it "will replace users" do
|
89
|
+
@ug.users = [@u2, @u3]
|
90
|
+
@ug.save
|
91
|
+
nug = Rubix::UserGroup.find(:name => 'rubix_spec_user_group_1')
|
92
|
+
nug.users.map(&:username).should include(*[@u2, @u3].map(&:username))
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Users" do
|
4
|
+
|
5
|
+
before do
|
6
|
+
integration_test
|
7
|
+
end
|
8
|
+
|
9
|
+
after do
|
10
|
+
truncate_all_tables
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "when not existing" do
|
14
|
+
|
15
|
+
it "returns nil on find" do
|
16
|
+
Rubix::User.find(:username => 'rubix_spec_user_1', :first_name => 'rubix1', :last_name => 'user1').should be_nil
|
17
|
+
end
|
18
|
+
|
19
|
+
it "can be created" do
|
20
|
+
hg = Rubix::User.new(:username => 'rubix_spec_user_1', :first_name => 'rubix1', :last_name => 'user1', :password => 'pass1')
|
21
|
+
hg.save.should be_true
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "when existing" do
|
27
|
+
|
28
|
+
before do
|
29
|
+
@u = ensure_save(Rubix::User.new(:username => 'rubix_spec_user_1', :first_name => 'rubix1', :last_name => 'user1', :password => 'pass1'))
|
30
|
+
end
|
31
|
+
|
32
|
+
it "can be found" do
|
33
|
+
Rubix::User.find(:username => 'rubix_spec_user_1').should_not be_nil
|
34
|
+
end
|
35
|
+
|
36
|
+
it "can have its name and settings changed" do
|
37
|
+
@u.username = 'rubix_spec_user_2'
|
38
|
+
@u.first_name = 'rubix2'
|
39
|
+
@u.last_name = 'user2'
|
40
|
+
@u.url = 'http://foobar.com'
|
41
|
+
@u.auto_login = true
|
42
|
+
@u.type = :super_admin
|
43
|
+
@u.lang = 'fo_ba'
|
44
|
+
@u.theme = 'foo.css'
|
45
|
+
@u.refresh_period = 10
|
46
|
+
@u.rows_per_page = 10
|
47
|
+
@u.password = 'pass2'
|
48
|
+
@u.save
|
49
|
+
Rubix::User.find(:username => 'rubix_spec_user_1').should be_nil
|
50
|
+
nu = Rubix::User.find(:username => 'rubix_spec_user_2')
|
51
|
+
nu.should_not be_nil
|
52
|
+
nu.username.should == 'rubix_spec_user_2'
|
53
|
+
nu.first_name.should == 'rubix2'
|
54
|
+
nu.last_name.should == 'user2'
|
55
|
+
nu.url.should == 'http://foobar.com'
|
56
|
+
nu.auto_login.should == true
|
57
|
+
nu.type.should == :super_admin
|
58
|
+
nu.lang.should == 'fo_ba'
|
59
|
+
nu.theme.should == 'foo.css'
|
60
|
+
nu.refresh_period.should == 10
|
61
|
+
nu.rows_per_page.should == 10
|
62
|
+
end
|
63
|
+
|
64
|
+
it "can be destroyed" do
|
65
|
+
@u.destroy
|
66
|
+
Rubix::User.find(:username => 'rubix_spec_user_1').should be_nil
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe "handling media" do
|
71
|
+
|
72
|
+
before do
|
73
|
+
@media_type = ensure_save(Rubix::MediaType.new(:name => 'rubix_spec_media_type_1'))
|
74
|
+
@media_params = [{:media_type_id => @media_type.id, :address => "XXX"}]
|
75
|
+
end
|
76
|
+
|
77
|
+
it "can add media to a user when creating" do
|
78
|
+
Rubix::User.new(:username => 'rubix_spec_user_1', :first_name => 'rubix1', :last_name => 'user1', :password => 'pass1', :media => @media_params).save.should be_true
|
79
|
+
# FIXME no facility to fetch media!!
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
@@ -1,9 +1,108 @@
|
|
1
|
+
require 'digest/md5'
|
2
|
+
|
1
3
|
module Rubix
|
2
4
|
module IntegrationHelper
|
3
5
|
|
6
|
+
INTEGRATION_USER = 'rubix_spec_user'
|
7
|
+
INTEGRATION_GROUP = 'rubix_spec_group'
|
8
|
+
INTEGRATION_PASSWORD = 'rubix'
|
9
|
+
|
10
|
+
# Parse the information we need to find the database and Zabbix
|
11
|
+
# server we're going to need for any integration tests. Also set
|
12
|
+
# a global variable for easy use of this information when testing.
|
13
|
+
def self.setup_integration_tests test_yml_path
|
14
|
+
unless parse_integration_settings(test_yml_path)
|
15
|
+
puts "Could not parse integration settings in #{test_yml_path}. Integration tests will be skipped."
|
16
|
+
return
|
17
|
+
end
|
18
|
+
unless connect_to_database
|
19
|
+
puts "Could not connect to a MySQL database using: #{$RUBIX_MYSQL_CREDENTIALS.inspect}. Integration tests will be skipped."
|
20
|
+
return
|
21
|
+
end
|
22
|
+
unless truncate_all_tables
|
23
|
+
puts "Could not truncate tables. Integration tests will be skipped."
|
24
|
+
return
|
25
|
+
end
|
26
|
+
unless create_integration_test_user_and_group
|
27
|
+
puts "Could not create integration test user #{INTEGRATION_USER} or group #{INTEGRATION_GROUP}. Integration tests will be skipped."
|
28
|
+
return
|
29
|
+
end
|
30
|
+
unless connect_to_api
|
31
|
+
puts "Could not connect to Zabbix API using: #{$RUBIX_API_CREDENTIALS.inspect}. Integration tests will be skipped."
|
32
|
+
return
|
33
|
+
end
|
34
|
+
$RUBIX_INTEGRATION = true
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.parse_integration_settings test_yml_path
|
38
|
+
return unless File.exist?(test_yml_path)
|
39
|
+
|
40
|
+
require 'yaml'
|
41
|
+
test_data = YAML.load(open(test_yml_path))
|
42
|
+
return if test_data['disable_integration_tests']
|
43
|
+
|
44
|
+
$RUBIX_API_CREDENTIALS = test_data['api']
|
45
|
+
$RUBIX_MYSQL_CREDENTIALS = test_data['mysql']
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.connect_to_database
|
49
|
+
begin
|
50
|
+
require 'mysql2'
|
51
|
+
$RUBIX_MYSQL = Mysql2::Client.new(:host => $RUBIX_MYSQL_CREDENTIALS['host'], :username => $RUBIX_MYSQL_CREDENTIALS['username'], :password => $RUBIX_MYSQL_CREDENTIALS['password'], :database => $RUBIX_MYSQL_CREDENTIALS['database'])
|
52
|
+
rescue => e
|
53
|
+
puts "Could not connect to MySQL database: #{e.class} -- #{e.message}"
|
54
|
+
puts e.backtrace
|
55
|
+
false
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# These are the tables we'll truncate in the database.
|
60
|
+
RUBIX_TABLES_TO_TRUNCATE = %w[actions conditions operations opconditions opmediatypes applications groups hostmacro hosts hosts_groups hosts_profiles hosts_profiles_ext hosts_templates items items_applications profiles triggers trigger_depends history sessions media_type scripts users usrgrp users_groups]
|
61
|
+
|
62
|
+
def self.truncate_all_tables
|
63
|
+
return unless $RUBIX_MYSQL
|
64
|
+
begin
|
65
|
+
RUBIX_TABLES_TO_TRUNCATE.each { |table| $RUBIX_MYSQL.query("TRUNCATE TABLE #{table}") }
|
66
|
+
true
|
67
|
+
rescue => e
|
68
|
+
puts "Could not truncate tables in MySQL: #{e.class} -- #{e.message}"
|
69
|
+
puts e.backtrace
|
70
|
+
false
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.create_integration_test_user_and_group
|
75
|
+
return unless $RUBIX_MYSQL
|
76
|
+
begin
|
77
|
+
$RUBIX_MYSQL.query(%Q{INSERT INTO users (`alias`, `name`, surname, passwd, type) VALUES ("#{INTEGRATION_USER}", "Rubix", "Spec User", "#{Digest::MD5.hexdigest('rubix')}", 3)})
|
78
|
+
$RUBIX_MYSQL.query(%Q{INSERT INTO usrgrp (`name`, api_access) VALUES ("#{INTEGRATION_GROUP}", 1)})
|
79
|
+
$RUBIX_MYSQL.query(%Q{INSERT INTO users_groups (usrgrpid, userid) SELECT users.userid, usrgrp.usrgrpid FROM users, usrgrp WHERE users.alias = '#{INTEGRATION_USER}' AND usrgrp.name = '#{INTEGRATION_GROUP}'})
|
80
|
+
true
|
81
|
+
rescue => e
|
82
|
+
puts "Could not create integraiton user or group: #{e.class} -- #{e.message}"
|
83
|
+
puts e.backtrace
|
84
|
+
false
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.connect_to_api
|
89
|
+
return unless $RUBIX_API_CREDENTIALS
|
90
|
+
begin
|
91
|
+
$RUBIX_API = Rubix::Connection.new($RUBIX_API_CREDENTIALS['url'], INTEGRATION_USER, INTEGRATION_PASSWORD)
|
92
|
+
$RUBIX_API.authorize!
|
93
|
+
Rubix.connection = $RUBIX_API
|
94
|
+
rescue => e
|
95
|
+
puts "Could not connect to Zabbix API: #{e.class} -- #{e.message}"
|
96
|
+
puts e.backtrace
|
97
|
+
false
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
4
101
|
def integration_test
|
5
|
-
if $
|
6
|
-
Rubix.
|
102
|
+
if $RUBIX_INTEGRATION
|
103
|
+
raise Rubix::Error.new("Could not truncate tables in MySQL.") unless IntegrationHelper.truncate_all_tables
|
104
|
+
raise Rubix::Error.new("Could not create integration user or group.") unless IntegrationHelper.create_integration_test_user_and_group
|
105
|
+
raise Rubix::Error.new("Could not connect to Zabbixi API.") unless IntegrationHelper.connect_to_api
|
7
106
|
else
|
8
107
|
pending("A live Zabbix API to test against")
|
9
108
|
end
|
@@ -11,7 +110,7 @@ module Rubix
|
|
11
110
|
|
12
111
|
def ensure_save(obj)
|
13
112
|
begin
|
14
|
-
raise Rubix::Error.new(Rubix.connection.last_response.
|
113
|
+
raise Rubix::Error.new(Rubix.connection.last_response.body) unless obj.save
|
15
114
|
obj
|
16
115
|
rescue => e
|
17
116
|
puts "#{e.class} -- #{e.message}"
|
@@ -27,7 +126,7 @@ module Rubix
|
|
27
126
|
else
|
28
127
|
errors = []
|
29
128
|
objs.each do |obj|
|
30
|
-
errors << Rubix.connection.last_response.
|
129
|
+
errors << Rubix.connection.last_response.body unless obj.destroy
|
31
130
|
end
|
32
131
|
raise Rubix::Error.new(errors.join("\n")) if errors.size > 0
|
33
132
|
end
|
@@ -48,41 +147,14 @@ module Rubix
|
|
48
147
|
end
|
49
148
|
|
50
149
|
def create_history item
|
150
|
+
raise Rubix::Error.new("Not connected to MySQL") unless $RUBIX_MYSQL
|
51
151
|
(1..10).to_a.collect do |i|
|
52
152
|
history = { "itemid" => item.id.to_s, "clock" => (Time.now.to_i - 5*i).to_s, "value" => rand(100).to_s }
|
53
|
-
$
|
153
|
+
$RUBIX_MYSQL.query("INSERT INTO history_uint (#{history.keys.join(', ')}) VALUES (#{history.values.join(', ')})")
|
54
154
|
history
|
55
155
|
end
|
56
156
|
end
|
57
157
|
|
58
|
-
def self.setup_integration_tests test_yml_path
|
59
|
-
return unless File.exist?(test_yml_path)
|
60
|
-
|
61
|
-
require 'yaml'
|
62
|
-
test_data = YAML.load(open(test_yml_path))
|
63
|
-
return if test_data['disable_integration_tests']
|
64
|
-
|
65
|
-
api_connection = test_data['api']
|
66
|
-
mysql_connection = test_data['mysql']
|
67
|
-
return unless api_connection && mysql_connection
|
68
|
-
|
69
|
-
Rubix.connect(api_connection['url'], api_connection['username'], api_connection['password'])
|
70
|
-
|
71
|
-
require 'mysql2'
|
72
|
-
$RUBIX_MYSQL_CLIENT = Mysql2::Client.new(:host => mysql_connection['host'], :username => mysql_connection['username'], :password => mysql_connection['password'], :database => mysql_connection['database'])
|
73
|
-
|
74
|
-
truncate_all_tables
|
75
|
-
|
76
|
-
$RUBIX_INTEGRATION_TEST = api_connection
|
77
|
-
end
|
78
|
-
|
79
|
-
RUBIX_TABLES_TO_TRUNCATE = %w[applications groups hostmacro hosts hosts_groups hosts_profiles hosts_profiles_ext hosts_templates items items_applications profiles triggers trigger_depends history sessions media_type]
|
80
|
-
|
81
|
-
def self.truncate_all_tables
|
82
|
-
return unless $RUBIX_INTEGRATION_TEST
|
83
|
-
RUBIX_TABLES_TO_TRUNCATE.each { |table| $RUBIX_MYSQL_CLIENT.query("TRUNCATE TABLE #{table}") }
|
84
|
-
end
|
85
|
-
|
86
158
|
def truncate_all_tables
|
87
159
|
IntegrationHelper.truncate_all_tables
|
88
160
|
end
|