spec/support/integration_helper.rb in rubix-0.4.3 vs spec/support/integration_helper.rb in rubix-0.5.0
- old
+ new
@@ -1,19 +1,118 @@
+require 'digest/md5'
+
module Rubix
module IntegrationHelper
+ INTEGRATION_USER = 'rubix_spec_user'
+ INTEGRATION_GROUP = 'rubix_spec_group'
+ INTEGRATION_PASSWORD = 'rubix'
+
+ # Parse the information we need to find the database and Zabbix
+ # server we're going to need for any integration tests. Also set
+ # a global variable for easy use of this information when testing.
+ def self.setup_integration_tests test_yml_path
+ unless parse_integration_settings(test_yml_path)
+ puts "Could not parse integration settings in #{test_yml_path}. Integration tests will be skipped."
+ return
+ end
+ unless connect_to_database
+ puts "Could not connect to a MySQL database using: #{$RUBIX_MYSQL_CREDENTIALS.inspect}. Integration tests will be skipped."
+ return
+ end
+ unless truncate_all_tables
+ puts "Could not truncate tables. Integration tests will be skipped."
+ return
+ end
+ unless create_integration_test_user_and_group
+ puts "Could not create integration test user #{INTEGRATION_USER} or group #{INTEGRATION_GROUP}. Integration tests will be skipped."
+ return
+ end
+ unless connect_to_api
+ puts "Could not connect to Zabbix API using: #{$RUBIX_API_CREDENTIALS.inspect}. Integration tests will be skipped."
+ return
+ end
+ $RUBIX_INTEGRATION = true
+ end
+
+ def self.parse_integration_settings test_yml_path
+ return unless File.exist?(test_yml_path)
+
+ require 'yaml'
+ test_data = YAML.load(open(test_yml_path))
+ return if test_data['disable_integration_tests']
+
+ $RUBIX_API_CREDENTIALS = test_data['api']
+ $RUBIX_MYSQL_CREDENTIALS = test_data['mysql']
+ end
+
+ def self.connect_to_database
+ begin
+ require 'mysql2'
+ $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'])
+ rescue => e
+ puts "Could not connect to MySQL database: #{e.class} -- #{e.message}"
+ puts e.backtrace
+ false
+ end
+ end
+
+ # These are the tables we'll truncate in the database.
+ 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]
+
+ def self.truncate_all_tables
+ return unless $RUBIX_MYSQL
+ begin
+ RUBIX_TABLES_TO_TRUNCATE.each { |table| $RUBIX_MYSQL.query("TRUNCATE TABLE #{table}") }
+ true
+ rescue => e
+ puts "Could not truncate tables in MySQL: #{e.class} -- #{e.message}"
+ puts e.backtrace
+ false
+ end
+ end
+
+ def self.create_integration_test_user_and_group
+ return unless $RUBIX_MYSQL
+ begin
+ $RUBIX_MYSQL.query(%Q{INSERT INTO users (`alias`, `name`, surname, passwd, type) VALUES ("#{INTEGRATION_USER}", "Rubix", "Spec User", "#{Digest::MD5.hexdigest('rubix')}", 3)})
+ $RUBIX_MYSQL.query(%Q{INSERT INTO usrgrp (`name`, api_access) VALUES ("#{INTEGRATION_GROUP}", 1)})
+ $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}'})
+ true
+ rescue => e
+ puts "Could not create integraiton user or group: #{e.class} -- #{e.message}"
+ puts e.backtrace
+ false
+ end
+ end
+
+ def self.connect_to_api
+ return unless $RUBIX_API_CREDENTIALS
+ begin
+ $RUBIX_API = Rubix::Connection.new($RUBIX_API_CREDENTIALS['url'], INTEGRATION_USER, INTEGRATION_PASSWORD)
+ $RUBIX_API.authorize!
+ Rubix.connection = $RUBIX_API
+ rescue => e
+ puts "Could not connect to Zabbix API: #{e.class} -- #{e.message}"
+ puts e.backtrace
+ false
+ end
+ end
+
def integration_test
- if $RUBIX_INTEGRATION_TEST
- Rubix.connect($RUBIX_INTEGRATION_TEST['url'], $RUBIX_INTEGRATION_TEST['username'], $RUBIX_INTEGRATION_TEST['password'])
+ if $RUBIX_INTEGRATION
+ raise Rubix::Error.new("Could not truncate tables in MySQL.") unless IntegrationHelper.truncate_all_tables
+ raise Rubix::Error.new("Could not create integration user or group.") unless IntegrationHelper.create_integration_test_user_and_group
+ raise Rubix::Error.new("Could not connect to Zabbixi API.") unless IntegrationHelper.connect_to_api
else
pending("A live Zabbix API to test against")
end
end
def ensure_save(obj)
begin
- raise Rubix::Error.new(Rubix.connection.last_response.error_message) unless obj.save
+ raise Rubix::Error.new(Rubix.connection.last_response.body) unless obj.save
obj
rescue => e
puts "#{e.class} -- #{e.message}"
puts e.backtrace
raise e
@@ -25,11 +124,11 @@
if block_given?
yield
else
errors = []
objs.each do |obj|
- errors << Rubix.connection.last_response.error_message unless obj.destroy
+ errors << Rubix.connection.last_response.body unless obj.destroy
end
raise Rubix::Error.new(errors.join("\n")) if errors.size > 0
end
rescue => e
puts "#{e.class} -- #{e.message}"
@@ -46,42 +145,15 @@
raise e
end
end
def create_history item
+ raise Rubix::Error.new("Not connected to MySQL") unless $RUBIX_MYSQL
(1..10).to_a.collect do |i|
history = { "itemid" => item.id.to_s, "clock" => (Time.now.to_i - 5*i).to_s, "value" => rand(100).to_s }
- $RUBIX_MYSQL_CLIENT.query("INSERT INTO history_uint (#{history.keys.join(', ')}) VALUES (#{history.values.join(', ')})")
+ $RUBIX_MYSQL.query("INSERT INTO history_uint (#{history.keys.join(', ')}) VALUES (#{history.values.join(', ')})")
history
end
- end
-
- def self.setup_integration_tests test_yml_path
- return unless File.exist?(test_yml_path)
-
- require 'yaml'
- test_data = YAML.load(open(test_yml_path))
- return if test_data['disable_integration_tests']
-
- api_connection = test_data['api']
- mysql_connection = test_data['mysql']
- return unless api_connection && mysql_connection
-
- Rubix.connect(api_connection['url'], api_connection['username'], api_connection['password'])
-
- require 'mysql2'
- $RUBIX_MYSQL_CLIENT = Mysql2::Client.new(:host => mysql_connection['host'], :username => mysql_connection['username'], :password => mysql_connection['password'], :database => mysql_connection['database'])
-
- truncate_all_tables
-
- $RUBIX_INTEGRATION_TEST = api_connection
- end
-
- 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]
-
- def self.truncate_all_tables
- return unless $RUBIX_INTEGRATION_TEST
- RUBIX_TABLES_TO_TRUNCATE.each { |table| $RUBIX_MYSQL_CLIENT.query("TRUNCATE TABLE #{table}") }
end
def truncate_all_tables
IntegrationHelper.truncate_all_tables
end