rubix 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,63 +1,69 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "CRUD for hosts" do
3
+ describe "Hosts" do
4
4
 
5
5
  before do
6
- @hg1 = Rubix::HostGroup.new(:name => 'rubix_spec_host_group_1')
7
- ensure_save(@hg1)
8
-
9
- @hg2 = Rubix::HostGroup.new(:name => 'rubix_spec_host_group_2')
10
- ensure_save(@hg2)
11
-
12
- @t1 = Rubix::Template.new(:name => 'rubix_spec_template_1', :host_groups => [@hg1])
13
- ensure_save(@t1)
14
-
15
- @t2 = Rubix::Template.new(:name => 'rubix_spec_template_2', :host_groups => [@hg2])
16
- ensure_save(@t2)
6
+ integration_test
7
+ @host_group_1 = ensure_save(Rubix::HostGroup.new(:name => 'rubix_spec_host_group_1'))
17
8
 
18
- @um1 = Rubix::UserMacro.new(:name => 'rubix_spec_macro_1', :value => 'rubix_spec_value_1')
19
-
20
9
  end
21
10
 
22
11
  after do
23
- ensure_destroy(@um1, @t1, @t2, @hg1, @hg2)
12
+ truncate_all_tables
24
13
  end
25
-
26
- it "should be able to create, update, and destroy a host" do
27
- integration_test
28
-
29
- Rubix::Host.find(:name => 'rubix_spec_host_1').should be_nil
30
-
31
- h1 = Rubix::Host.new(:name => 'rubix_spec_host_1', :host_groups => [@hg1], :templates => [@t1], :user_macros => [@um1])
32
- h1.save.should be_true
33
- id = h1.id
34
- id.should_not be_nil
35
-
36
- ensure_destroy(h1) do
37
- h2 = Rubix::Host.find(:name => 'rubix_spec_host_1')
38
- h2.should_not be_nil
39
- h2.template_ids.should include(@t1.id)
40
- h2.host_group_ids.should include(@hg1.id)
41
- h2.user_macros.size.should == 1
42
- h2.user_macros.first.name.should == 'RUBIX_SPEC_MACRO_1'
43
- h2.user_macros.first.value.should == 'rubix_spec_value_1'
14
+
15
+ describe "when not existing" do
16
+
17
+ it "returns nil on find" do
18
+ Rubix::Host.find(:name => 'rubix_spec_host_1').should be_nil
19
+ end
20
+
21
+ it "can be created" do
22
+ host = Rubix::Host.new(:name => 'rubix_spec_host_1', :host_groups => [@host_group_1])
23
+ host.save.should be_true
24
+ end
25
+ end
26
+
27
+ describe "when existing" do
28
+ before do
29
+ @host_group_2 = ensure_save(Rubix::HostGroup.new(:name => 'rubix_spec_host_group_2'))
30
+ @host = ensure_save(Rubix::Host.new(:name => 'rubix_spec_host_1', :host_groups => [@host_group_1]))
31
+ @template_1 = ensure_save(Rubix::Template.new(:name => 'rubix_spec_template_1', :host_groups => [@host_group_2]))
32
+ @template_2 = ensure_save(Rubix::Template.new(:name => 'rubix_spec_template_2', :host_groups => [@host_group_2]))
33
+ end
34
+
35
+ it "can have its name changed" do
36
+ @host.name = 'rubix_spec_host_2'
37
+ @host.save
38
+
39
+ Rubix::Host.find(:name => 'rubix_spec_host_1').should be_nil
40
+ Rubix::Host.find(:name => 'rubix_spec_host_2').should_not be_nil
41
+ end
42
+
43
+ it "can change its host groups" do
44
+ @host.host_groups = [@host_group_1, @host_group_2]
45
+ @host.save
44
46
 
45
- h1.name = 'rubix_spec_host_2'
46
- h1.host_groups = [@hg2]
47
- h1.templates = [@t2]
48
- h1.save.should be_true
47
+ new_host = Rubix::Host.find(:name => 'rubix_spec_host_1')
48
+ new_host.should_not be_nil
49
+ new_host.host_groups.size.should == 2
50
+ new_host.host_groups.map(&:name).should include('rubix_spec_host_group_1', 'rubix_spec_host_group_2')
51
+ end
52
+
53
+ it "can change its templates" do
54
+ @host.templates = [@template_1, @template_2]
55
+ @host.save
49
56
 
50
- h2 = Rubix::Host.find(:name => 'rubix_spec_host_2')
51
- h2.should_not be_nil
52
- h2.template_ids.should include(@t2.id)
53
- h2.host_group_ids.should include(@hg2.id)
54
- h2.user_macros.size.should == 1
55
- h2.user_macros.first.name.should == 'RUBIX_SPEC_MACRO_1'
56
- h2.user_macros.first.value.should == 'rubix_spec_value_1'
57
-
58
- h1.destroy
59
- Rubix::Host.find(:id => id).should be_nil
57
+ new_host = Rubix::Host.find(:name => 'rubix_spec_host_1')
58
+ new_host.should_not be_nil
59
+ new_host.templates.size.should == 2
60
+ new_host.templates.map(&:name).should include('rubix_spec_template_1', 'rubix_spec_template_2')
60
61
  end
61
-
62
+
63
+ it "can be destroyed" do
64
+ @host.destroy
65
+ Rubix::Host.find(:name => 'rubix_spec_host_1').should be_nil
66
+ end
67
+
62
68
  end
63
69
  end
@@ -1,65 +1,61 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "CRUD for items" do
3
+ describe "Items" do
4
4
 
5
5
  before do
6
- @hg1 = Rubix::HostGroup.new(:name => 'rubix_spec_host_group_1')
7
- ensure_save(@hg1)
8
-
9
- @h1 = Rubix::Host.new(:name => 'rubix_spec_host_1', :host_groups => [@hg1])
10
- ensure_save(@h1)
11
-
12
- @h2 = Rubix::Host.new(:name => 'rubix_spec_host_2', :host_groups => [@hg1])
13
- ensure_save(@h2)
14
-
15
- @a1 = Rubix::Application.new(:name => 'rubix_spec_app_1', :host_id => @h1.id)
16
- ensure_save(@a1)
17
-
18
- @a2 = Rubix::Application.new(:name => 'rubix_spec_app_1', :host_id => @h2.id)
19
- ensure_save(@a2)
6
+ integration_test
7
+ @host_group = ensure_save(Rubix::HostGroup.new(:name => 'rubix_spec_host_group_1'))
8
+ @host_1 = ensure_save(Rubix::Host.new(:name => 'rubix_spec_host_1', :host_groups => [@host_group]))
9
+ @host_2 = ensure_save(Rubix::Host.new(:name => 'rubix_spec_host_2', :host_groups => [@host_group]))
10
+ @app_1 = ensure_save(Rubix::Application.new(:name => 'rubix_spec_app_1', :host_id => @host_1.id))
11
+ @app_2 = ensure_save(Rubix::Application.new(:name => 'rubix_spec_app_1', :host_id => @host_2.id))
20
12
  end
21
13
 
22
14
  after do
23
- ensure_destroy(@a1, @a2, @h1, @h2, @hg1)
15
+ truncate_all_tables
24
16
  end
25
-
26
- it "should be able to create, update, and destroy an item" do
27
- integration_test
28
-
29
- Rubix::Item.find(:key => 'rubix.spec1', :host_id => @h1.id).should be_nil
30
-
31
- item1 = Rubix::Item.new(:key => 'rubix.spec1', :description => 'rubix item description 1', :host_id => @h1.id, :value_type => :character, :applications => [@a1])
32
- item1.save.should be_true
33
- id = item1.id
34
- id.should_not be_nil
17
+
18
+ describe "when not existing" do
19
+
20
+ it "returns nil on find" do
21
+ Rubix::Item.find(:key => 'rubix.spec1', :host_id => @host_1.id).should be_nil
22
+ end
23
+
24
+ it "can be created" do
25
+ item = Rubix::Item.new(:key => 'rubix.spec1', :description => 'rubix item description 1', :host_id => @host_1.id, :value_type => :character, :applications => [@app_1])
26
+ item.save.should be_true
27
+ item.host.name.should == @host_1.name
28
+ item.applications.map(&:name).should include(@app_1.name)
29
+ end
35
30
 
36
- ensure_destroy(item1) do
37
- item2 = Rubix::Item.find(:key => 'rubix.spec1', :host_id => @h1.id)
38
- item2.should_not be_nil
39
- item2.host.name.should == @h1.name
40
- item2.key.should == 'rubix.spec1'
41
- item2.description.should == 'rubix item description 1'
42
- item2.value_type.should == :character
43
- item2.application_ids.should include(@a1.id)
44
-
45
- item1.key = 'rubix.spec2'
46
- item1.description = 'rubix item description 2'
47
- item1.value_type = :unsigned_int
48
- item1.host_id = @h2.id
49
- item1.applications = [@a2]
50
- item1.save.should be_true
51
-
52
- item2 = Rubix::Item.find(:key => 'rubix.spec2', :host_id => @h2.id)
53
- item2.should_not be_nil
54
- item2.host.name.should == @h2.name
55
- item2.key.should == 'rubix.spec2'
56
- item2.description.should == 'rubix item description 2'
57
- item2.value_type.should == :unsigned_int
58
- item2.application_ids.should include(@a2.id)
59
-
60
- item1.destroy
61
- Rubix::Item.find(:id => id, :host_id => @h1.id).should be_nil
62
- Rubix::Item.find(:id => id, :host_id => @h2.id).should be_nil
31
+ end
32
+
33
+ describe "when existing" do
34
+
35
+ before do
36
+ @item = ensure_save(Rubix::Item.new(:key => 'rubix.spec1', :description => 'rubix item description 1', :host_id => @host_1.id, :value_type => :character, :applications => [@app_1]))
37
+ end
38
+
39
+ it "can have its host, application, and properties updated" do
40
+ @item.key = 'rubix.spec2'
41
+ @item.description = 'rubix item description 2'
42
+ @item.value_type = :unsigned_int
43
+ @item.host_id = @host_2.id
44
+ @item.applications = [@app_2]
45
+ @item.save.should be_true
46
+
47
+ Rubix::Item.find(:key => 'rubix.spec1', :host_id => @host_1.id).should be_nil
48
+
49
+ new_item = Rubix::Item.find(:key => 'rubix.spec2', :host_id => @host_2.id)
50
+ new_item.should_not be_nil
51
+ new_item.host.name.should == @host_2.name
52
+ new_item.applications.map(&:name).should include(@app_2.name)
53
+ end
54
+
55
+ it "can be destroyed" do
56
+ @item.destroy
57
+ Rubix::Item.find(:key => 'rubix.spec1', :host_id => @host_1.id).should be_nil
63
58
  end
64
59
  end
65
60
  end
61
+
@@ -1,51 +1,51 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "CRUD for templates" do
3
+ describe "Templates" do
4
4
 
5
5
  before do
6
- @hg1 = Rubix::HostGroup.new(:name => 'rubix_spec_host_group_1')
7
- ensure_save(@hg1)
8
-
9
- @hg2 = Rubix::HostGroup.new(:name => 'rubix_spec_host_group_2')
10
- ensure_save(@hg2)
6
+ integration_test
7
+ @host_group_1 = ensure_save(Rubix::HostGroup.new(:name => 'rubix_spec_host_group_1'))
8
+ @host_group_2 = ensure_save(Rubix::HostGroup.new(:name => 'rubix_spec_host_group_2'))
11
9
  end
12
10
 
13
11
  after do
14
- ensure_destroy(@hg1, @hg2)
12
+ truncate_all_tables
15
13
  end
16
14
 
17
- it "should be able to create, update, and destroy a template" do
18
- integration_test
19
-
20
- Rubix::Template.find(:name => 'rubix_spec_template_1').should be_nil
15
+ describe "when not existing" do
16
+
17
+ it "returns nil on find" do
18
+ Rubix::Template.find(:name => 'rubix_spec_template_1').should be_nil
19
+ end
20
+
21
+ it "can be created" do
22
+ template = Rubix::Template.new(:name => 'rubix_spec_template_1', :host_groups => [@host_group_1])
23
+ template.save.should be_true
24
+ end
21
25
 
22
- t1 = Rubix::Template.new(:name => 'rubix_spec_template_1', :host_groups => [@hg1])
23
- t1.save.should be_true
24
- id = t1.id
25
- id.should_not be_nil
26
-
27
- ensure_destroy(t1) do
28
- t2 = Rubix::Template.find(:name => 'rubix_spec_template_1')
29
- t2.should_not be_nil
30
- t2.id.should == id
31
- t2.host_group_ids.should include(@hg1.id)
32
-
33
- t1.name = 'rubix_spec_template_2'
34
- t1.host_groups = [@hg2]
35
- t1.save.should be_true
36
-
37
- t2 = Rubix::Template.find(:id => id)
38
- t2.name.should == 'rubix_spec_template_2'
39
- t2.host_group_ids.should_not include(@hg1.id)
40
- t2.host_group_ids.should include(@hg2.id)
41
-
42
- t1.destroy
43
- Rubix::Template.find(:id => id).should be_nil
26
+ end
27
+
28
+ describe "when existing" do
29
+
30
+ before do
31
+ @template = ensure_save(Rubix::Template.new(:name => 'rubix_spec_template_1', :host_groups => [@host_group_1]))
32
+ end
33
+
34
+ it "can have its name changed" do
35
+ @template.name = 'rubix_spec_template_2'
36
+ @template.save
37
+ Rubix::Template.find(:name => 'rubix_spec_template_1').should be_nil
38
+ Rubix::Template.find(:name => 'rubix_spec_template_2').should_not be_nil
44
39
  end
40
+
41
+ it "can be destroyed" do
42
+ @template.destroy
43
+ Rubix::Template.find(:name => 'rubix_spec_template_1').should be_nil
44
+ end
45
+
45
46
  end
46
47
 
47
48
  it "should be able to import and export a template" do
48
- integration_test
49
49
  pending "Learning how to import/export XML via the API"
50
50
  end
51
51
 
@@ -1,43 +1,49 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "CRUD for user macros" do
3
+ describe "User Macros" do
4
4
 
5
5
  before do
6
- @hg1 = Rubix::HostGroup.new(:name => 'rubix_spec_host_group_1')
7
- ensure_save(@hg1)
8
-
9
- @h1 = Rubix::Host.new(:name => 'rubix_spec_host_1', :host_groups => [@hg1])
10
- ensure_save(@h1)
6
+ integration_test
7
+ @host_group = ensure_save(Rubix::HostGroup.new(:name => 'rubix_spec_host_group_1'))
8
+ @host = ensure_save(Rubix::Host.new(:name => 'rubix_spec_host_1', :host_groups => [@host_group]))
11
9
  end
12
10
 
13
11
  after do
14
- ensure_destroy(@h1, @hg1)
12
+ truncate_all_tables
15
13
  end
16
-
17
- it "should be able to create, update, and destroy a host" do
18
- integration_test
19
-
20
- Rubix::UserMacro.find(:name => 'rubix_spec_macro_1', :host_id => @h1.id).should be_nil
21
-
22
- um1 = Rubix::UserMacro.new(:name => 'rubix_spec_macro_1', :value => 'rubix_spec_value_1', :host_id => @h1.id)
23
- um1.save.should be_true
24
14
 
25
- id = um1.id
26
- id.should_not be_nil
15
+ describe "when not existing" do
27
16
 
28
- ensure_destroy(um1) do
29
- um2 = Rubix::UserMacro.find(:name => 'rubix_spec_macro_1', :host_id => @h1.id)
30
- um2.should_not be_nil
17
+ it "returns nil on find" do
18
+ Rubix::UserMacro.find(:name => 'rubix_spec_macro_1', :host_id => @host.id).should be_nil
19
+ end
31
20
 
32
- um1.value = 'rubix_spec_value_2'
33
- um1.save.should be_true
21
+ it "can be created" do
22
+ um = Rubix::UserMacro.new(:name => 'rubix_spec_macro_1', :value => 'rubix_spec_value_1', :host_id => @host.id)
23
+ um.save.should be_true
24
+ end
34
25
 
35
- um2 = Rubix::UserMacro.find(:name => 'rubix_spec_macro_1', :host_id => @h1.id)
36
- um2.should_not be_nil
37
- um2.value.should == 'rubix_spec_value_2'
26
+ end
38
27
 
39
- um1.destroy
40
- Rubix::UserMacro.find(:name => 'rubix_spec_macro_1', :host_id => @h1.id).should be_nil
28
+ describe "when existing" do
29
+
30
+ before do
31
+ @macro = ensure_save(Rubix::UserMacro.new(:name => 'rubix_spec_macro_1', :value => 'rubix_spec_value_1', :host_id => @host.id))
32
+ end
33
+
34
+ it "can have its value changed" do
35
+ @macro.value = 'rubix_spec_value_2'
36
+ @macro.save
37
+
38
+ new_macro = Rubix::UserMacro.find(:name => 'rubix_spec_macro_1', :host_id => @host.id)
39
+ new_macro.should_not be_nil
40
+ new_macro.value.should == 'rubix_spec_value_2'
41
41
  end
42
+
43
+ it "can be destroyed" do
44
+ @macro.destroy
45
+ Rubix::UserMacro.find(:name => 'rubix_spec_macro_1', :host_id => @host.id).should be_nil
46
+ end
47
+
42
48
  end
43
49
  end
data/spec/spec_helper.rb CHANGED
@@ -15,17 +15,5 @@ RSpec.configure do |config|
15
15
  config.include Rubix::IntegrationHelper
16
16
  config.include Rubix::ConfigliereHelper
17
17
 
18
- test_yml_path = File.expand_path('../test.yml', __FILE__)
19
- if File.exist?(test_yml_path)
20
- require 'yaml'
21
- test_data = YAML.load(open(test_yml_path))
22
- if test_data['disable_integration_tests']
23
- $RUBIX_INTEGRATION_TEST = false
24
- else
25
- Rubix.connect(test_data['url'], test_data['username'], test_data['password'])
26
- $RUBIX_INTEGRATION_TEST = true
27
- end
28
- else
29
- $RUBIX_INTEGRATION_TEST = false
30
- end
18
+ Rubix::IntegrationHelper.setup_integration_tests(File.expand_path('../test.yml', __FILE__))
31
19
  end
@@ -1,6 +1,6 @@
1
1
  module Rubix
2
2
  module IntegrationHelper
3
-
3
+
4
4
  def integration_test
5
5
  pending("A live Zabbix API to test against") unless $RUBIX_INTEGRATION_TEST
6
6
  end
@@ -8,6 +8,7 @@ module Rubix
8
8
  def ensure_save(obj)
9
9
  begin
10
10
  raise Rubix::Error.new(Rubix.connection.last_response.error_message) unless obj.save
11
+ obj
11
12
  rescue => e
12
13
  puts "#{e.class} -- #{e.message}"
13
14
  puts e.backtrace
@@ -41,6 +42,38 @@ module Rubix
41
42
  raise e
42
43
  end
43
44
  end
45
+
46
+ def self.setup_integration_tests test_yml_path
47
+ return unless File.exist?(test_yml_path)
48
+
49
+ require 'yaml'
50
+ test_data = YAML.load(open(test_yml_path))
51
+ return if test_data['disable_integration_tests']
52
+
53
+ api_connection = test_data['api']
54
+ mysql_connection = test_data['mysql']
55
+ return unless api_connection && mysql_connection
56
+
57
+ Rubix.connect(api_connection['url'], api_connection['username'], api_connection['password'])
58
+
59
+ require 'mysql2'
60
+ $RUBIX_MYSQL_CLIENT = Mysql2::Client.new(:host => mysql_connection['host'], :username => mysql_connection['username'], :password => mysql_connection['password'], :database => mysql_connection['database'])
61
+
62
+ truncate_all_tables
63
+
64
+ $RUBIX_INTEGRATION_TEST = true
65
+ end
66
+
67
+ RUBIX_TABLES_TO_TRUNCATE = %w[applications groups hostmacro hosts hosts_groups hosts_profiles hosts_profiles_ext hosts_templates items items_applications profiles triggers]
68
+
69
+ def self.truncate_all_tables
70
+ return unless $RUBIX_INTEGRATION_TEST
71
+ RUBIX_TABLES_TO_TRUNCATE.each { |table| $RUBIX_MYSQL_CLIENT.query("TRUNCATE TABLE #{table}") }
72
+ end
73
+
74
+ def truncate_all_tables
75
+ IntegrationHelper.truncate_all_tables
76
+ end
44
77
 
45
78
  end
46
79
  end