diff --git a/Rakefile b/Rakefile index 1ffc6af..918f0aa 100644 --- a/Rakefile +++ b/Rakefile @@ -95,7 +95,9 @@ end desc "Run the tests" task :test => ["test:test_args"] do - test_cmd = "sudo -i rake --trace --rakefile /vagrant/test/Rakefile " + test_cmd = "sudo -i rake" + test_cmd << " --trace" if @test_args.include?("TEST_DEBUG=1") + test_cmd << " --rakefile /vagrant/test/Rakefile " test_cmd << @test_args.join(" ") sh VAGRANT_SSH_CMD % [ "appliance", test_cmd ] end @@ -106,21 +108,32 @@ namespace :test do @test_args = ["clean", "test"] end + desc "Add debugging output" + task :debug => :test_args do + @test_args << "TEST_DEBUG=1" + end + desc "Run only local backup tests" - task :local => :test_args do + task :local, [:testopts] => :test_args do |_, args| + args.with_defaults :testopts => nil @test_args << "TEST=/vagrant/test/local_backup_test.rb" + @test_args << "TESTOPTS='#{args.testopts}'" if args.testopts Rake::Task["test"].invoke end desc "Run only NFS backup tests" - task :nfs => :test_args do + task :nfs, [:testopts] => :test_args do |_, args| + args.with_defaults :testopts => nil @test_args << "TEST=/vagrant/test/nfs_backup_test.rb" + @test_args << "TESTOPTS='#{args.testopts}'" if args.testopts Rake::Task["test"].invoke end desc "Run only SMB backup tests" - task :smb => :test_args do + task :smb, [:testopts] => :test_args do |_, args| + args.with_defaults :testopts => nil @test_args << "TEST=/vagrant/test/smb_backup_test.rb" + @test_args << "TESTOPTS='#{args.testopts}'" if args.testopts Rake::Task["test"].invoke end end diff --git a/provision_scripts/appliance_sync.sh b/provision_scripts/appliance_sync.sh index a9eb0f5..50c13d0 100644 --- a/provision_scripts/appliance_sync.sh +++ b/provision_scripts/appliance_sync.sh @@ -12,24 +12,36 @@ cp /vagrant/manageiq-appliance_console/bin/appliance_console \ $APPLIANCE_CONSOLE_DIR/bin/appliance_console cp /vagrant/manageiq-appliance_console/lib/manageiq-appliance_console.rb \ $APPLIANCE_CONSOLE_DIR/lib/manageiq-appliance_console.rb +cp /vagrant/manageiq-appliance_console/lib/manageiq/appliance_console/certificate_authority.rb \ + $APPLIANCE_CONSOLE_DIR//lib/manageiq/appliance_console/certificate_authority.rb +cp /vagrant/manageiq-appliance_console/lib/manageiq/appliance_console/cli.rb \ + $APPLIANCE_CONSOLE_DIR//lib/manageiq/appliance_console/cli.rb cp /vagrant/manageiq-appliance_console/lib/manageiq/appliance_console/database_admin.rb \ $APPLIANCE_CONSOLE_DIR/lib/manageiq/appliance_console/database_admin.rb cp /vagrant/manageiq-appliance_console/lib/manageiq/appliance_console/database_configuration.rb \ $APPLIANCE_CONSOLE_DIR/lib/manageiq/appliance_console/database_configuration.rb +cp /vagrant/manageiq-appliance_console/lib/manageiq/appliance_console/database_replication.rb \ + $APPLIANCE_CONSOLE_DIR/lib/manageiq/appliance_console/database_replication.rb +cp /vagrant/manageiq-appliance_console/lib/manageiq/appliance_console/database_replication_standby.rb \ + $APPLIANCE_CONSOLE_DIR/lib/manageiq/appliance_console/database_replication_standby.rb +cp /vagrant/manageiq-appliance_console/locales/appliance/en.yml \ + $APPLIANCE_CONSOLE_DIR/locales/appliance/en.yml +cp /vagrant/manageiq-appliance_console/lib/manageiq/appliance_console/i18n.rb \ + $APPLIANCE_CONSOLE_DIR/lib/manageiq/appliance_console/i18n.rb +cp /vagrant/manageiq-appliance_console/lib/manageiq/appliance_console/internal_database_configuration.rb \ + $APPLIANCE_CONSOLE_DIR/lib/manageiq/appliance_console/internal_database_configuration.rb cp /vagrant/manageiq-appliance_console/lib/manageiq/appliance_console/message_configuration.rb \ $APPLIANCE_CONSOLE_DIR//lib/manageiq/appliance_console/message_configuration.rb cp /vagrant/manageiq-appliance_console/lib/manageiq/appliance_console/message_configuration_client.rb \ $APPLIANCE_CONSOLE_DIR//lib/manageiq/appliance_console/message_configuration_client.rb cp /vagrant/manageiq-appliance_console/lib/manageiq/appliance_console/message_configuration_server.rb \ $APPLIANCE_CONSOLE_DIR//lib/manageiq/appliance_console/message_configuration_server.rb -cp /vagrant/manageiq-appliance_console/lib/manageiq/appliance_console/i18n.rb \ - $APPLIANCE_CONSOLE_DIR/lib/manageiq/appliance_console/i18n.rb -cp /vagrant/manageiq-appliance_console/lib/manageiq/appliance_console/prompts.rb \ - $APPLIANCE_CONSOLE_DIR/lib/manageiq/appliance_console/prompts.rb cp /vagrant/manageiq-appliance_console/lib/manageiq/appliance_console/postgres_admin.rb \ $APPLIANCE_CONSOLE_DIR/lib/manageiq/appliance_console/postgres_admin.rb -cp /vagrant/manageiq-appliance_console/locales/appliance/en.yml \ - $APPLIANCE_CONSOLE_DIR/locales/appliance/en.yml +cp /vagrant/manageiq-appliance_console/lib/manageiq/appliance_console/prompts.rb \ + $APPLIANCE_CONSOLE_DIR/lib/manageiq/appliance_console/prompts.rb +cp /vagrant/manageiq-appliance_console/lib/manageiq/appliance_console/utilities.rb \ + $APPLIANCE_CONSOLE_DIR/lib/manageiq/appliance_console/utilities.rb ###### manageiq-gems-pending changes cp /vagrant/manageiq-gems-pending/lib/gems/pending/util/mount/miq_generic_mount_session.rb \ diff --git a/test/nfs_backup_test.rb b/test/nfs_backup_test.rb index 472fcee..1930a1d 100644 --- a/test/nfs_backup_test.rb +++ b/test/nfs_backup_test.rb @@ -7,7 +7,11 @@ class NFSBackupTest < BaseBackupTest def test_nfs_database_backup console_nfs_backup_file = "console_full_nfs_backup.tar.gz" - ApplianceConsoleRunner.backup console_nfs_backup_file, :nfs + + run_in_mount :nfs do |mount_point| + mount_file = File.join(mount_point, "db_backup", console_nfs_backup_file) + ApplianceConsoleRunner.backup mount_file, :nfs + end run_in_mount :nfs do |mount_point| mount_file = File.join(mount_point, "db_backup", console_nfs_backup_file) @@ -20,7 +24,10 @@ def test_nfs_database_backup def test_nfs_database_dump console_nfs_dump_file = "console_full_nfs_dump.tar.gz" - ApplianceConsoleRunner.dump console_nfs_dump_file, :nfs + run_in_mount :nfs do |mount_point| + mount_file = File.join(mount_point, "db_dump", console_nfs_dump_file) + ApplianceConsoleRunner.dump mount_file, :nfs + end run_in_mount :nfs do |mount_point| mount_file = File.join(mount_point, "db_dump", console_nfs_dump_file) @@ -32,7 +39,11 @@ def test_nfs_database_dump def test_nfs_database_dump_excluding_custom_attributes console_nfs_dump_file_without_ca = "console_partial_nfs_dump_without_custom_attributes.tar.gz" - ApplianceConsoleRunner.dump_with_no_custom_attributes console_nfs_dump_file_without_ca, :nfs + + run_in_mount :nfs do |mount_point| + mount_file = File.join(mount_point, "db_dump", console_nfs_dump_file_without_ca) + ApplianceConsoleRunner.dump_with_no_custom_attributes mount_file, :nfs + end run_in_mount :nfs do |mount_point| mount_file = File.join(mount_point, "db_dump", console_nfs_dump_file_without_ca) diff --git a/test/smb_backup_test.rb b/test/smb_backup_test.rb index af4b7d8..23ea766 100644 --- a/test/smb_backup_test.rb +++ b/test/smb_backup_test.rb @@ -7,7 +7,11 @@ class SMBBackupTest < BaseBackupTest def test_smb_database_backup console_smb_backup_file = "console_full_smb_backup.tar.gz" - ApplianceConsoleRunner.backup console_smb_backup_file, :smb + + run_in_mount :smb do |mount_point| + mount_file = File.join(mount_point, "db_backup", console_smb_backup_file) + ApplianceConsoleRunner.backup mount_file, :smb + end run_in_mount :smb do |mount_point| mount_file = File.join(mount_point, "db_backup", console_smb_backup_file) @@ -20,7 +24,10 @@ def test_smb_database_backup def test_smb_database_dump console_smb_dump_file = "console_full_smb_dump.tar.gz" - ApplianceConsoleRunner.dump console_smb_dump_file, :smb + run_in_mount :smb do |mount_point| + mount_file = File.join(mount_point, "db_dump", console_smb_dump_file) + ApplianceConsoleRunner.dump mount_file, :smb + end run_in_mount :smb do |mount_point| mount_file = File.join(mount_point, "db_dump", console_smb_dump_file) @@ -32,7 +39,11 @@ def test_smb_database_dump def test_smb_database_dump_excluding_custom_attributes console_smb_dump_file_without_ca = "console_partial_smb_dump_without_custom_attributes.tar.gz" - ApplianceConsoleRunner.dump_with_no_custom_attributes console_smb_dump_file_without_ca, :smb + + run_in_mount :smb do |mount_point| + mount_file = File.join(mount_point, "db_dump", console_smb_dump_file_without_ca) + ApplianceConsoleRunner.dump_with_no_custom_attributes mount_file, :smb + end run_in_mount :smb do |mount_point| mount_file = File.join(mount_point, "db_dump", console_smb_dump_file_without_ca) diff --git a/test/support/appliance_console_cli.rb b/test/support/appliance_console_cli.rb new file mode 100644 index 0000000..e200eb5 --- /dev/null +++ b/test/support/appliance_console_cli.rb @@ -0,0 +1,209 @@ +require 'etc' +require 'uri' +require 'fileutils' +require 'singleton' + +require_relative './vmdb_helper.rb' +require_relative './db_filename_helper.rb' + +class ApplianceConsoleCli + ACTION_MAP = { + :backup => [:backup, {}], + :backup_fktmp => [:backup, { :fake_tmp => true }], + :dump => [:dump, {}], + :dump_fktmp => [:dump, { :fake_tmp => true }], + :split_backup => [:backup, { :split => true }], + :split_dump => [:dump, { :split => true }], + :restore => [:restore, { :file_in_uri => true }] + } + + include Singleton + include DbFilenameHelper + + ## + # Run a test case for the appliance_console_cli + # + # ==== Parameters + # + # [*type* (Symbol)] Command run (:backup, :dump, :restore, etc.) + # [*location* (Symbol)] Location of backup (:local, :nfs, :smb, etc.) + # [*file* (String)] File path location of backup + # + def self.test type, location, file + action, opts = ACTION_MAP[type] + verbose = opts[:verbose] + debug = opts[:debug] + test_case = RakeTestCase.new file, action, location, opts + + if verbose || debug + puts + puts + puts type.inspect + puts location.inspect + puts file.inspect + puts opts.inspect + puts test_case.send(:build_command) + puts + test_case.run_test if verbose + else + test_case.run_test + end + end + + + ## + # Run a command for using the appliance_console_cli without any test + # expectations involved. + # + # Generally this will be called by DbValidator, and not called directly + # + # ==== Parameters + # + # [*type* (Symbol)] Command run (:backup, :dump, :restore, etc.) + # [*location* (Symbol)] Location of backup (:local, :nfs, :smb, etc.) + # [*file* (String)] File path location of backup + # [*args* (Hash)] Extra arguments to pass to the command + # + def self.run_cmd type, location, file, cli_args + action, opts = ACTION_MAP[type] + opts[:args] = cli_args + verbose = opts[:args][:verbose] + debug = opts[:args][:debug] + rake_runner = Runner.new file, action, location, opts + + if verbose || debug + puts + puts + puts type.inspect + puts location.inspect + puts file.inspect + puts cli_args.inspect + puts rake_runner.send(:build_command) + puts + rake_runner.run_command if verbose + else + rake_runner.run_command + end + end + + class Runner + DEFAULT_AUTH = %w[vagrant vagrant].freeze + REDACTED_AUTH = %w[******** ********].freeze + + CMD_FAILED_PROMPT = <<-MSG.gsub(/^ /, "").freeze + Command Failed + -------------- + + MSG + + include VmdbHelper + include DbFilenameHelper + + attr_reader :debug, :file, :action, :location, :opts, :rubyopt, :verbose + + ## + # Runs a single instance of an appliance_console_cli command. + # + # Don't use directly! Call via ApplianceConsoleCli.run_cmd + # + def initialize file, action, location, opts = {} + @file = file + @action = action + @location = location + @opts = opts + @debug = @opts.fetch(:args, {}).delete(:debug) || ENV["TEST_DEBUG"] + @verbose = @opts.fetch(:args, {}).delete(:verbose) + @rubyopt = @opts.fetch(:args, {}).delete(:rubyopt) + + @redacted = false + end + + def run_command + out, status = [StringIO.new, nil] + + if debug + puts "running appliance_console_cli..." + puts " $ #{wrapped_cmd}" + end + + run_in_vmdb do + out_data, pipe = IO.pipe + Thread.new { IO.copy_stream out_data, out } + system(wrapped_cmd, [:out, :err] => pipe) + pipe.close + status = $? + end + status.success? || fail("#{CMD_FAILED_PROMPT}$ #{wrapped_cmd}\n\n#{out.string}") + end + + private + + def build_command + "#{build_rubyopt} appliance_console_cli #{task_name} #{task_args}" + end + + def build_rubyopt + %Q'RUBYOPT="#{rubyopt}"' if rubyopt + end + + def wrapped_cmd + "sudo /bin/sh -c 'source /etc/profile.d/evm.sh; #{build_command}'" + end + + def task_name + @task_name ||= "--#{action}" + end + + def task_args options = {} + args = "--local-file #{file} " + args += add_additional_args + args + end + + def generic_remote_args params = {} + path = params[:path] || '' + host = params[:host] || ::SHARE_IP + uri = URI::Generic.new(*URI.split("#{location}://#{host}#{path}")) + uri.path << "/#{file}" if root_file_in_uri? + uri.path << "/#{namespaced_file}" if namespaced_file_in_uri? + + args = %Q{--uri "#{uri}"} + args + end + + def add_additional_args + args_hash_to_cmdline_string @opts[:args] if @opts[:args] + end + + def args_hash_to_cmdline_string args_hash + args_hash.map do |key, value| + if value.kind_of? Array + value.map { |v| "--#{key.to_s.tr('_', '-')} #{v}" }.join(" ") + else + "--#{key.to_s.tr('_', '-')} #{value}" + end + end.unshift(nil).join(" ").to_s + end + + def namespaced_file + return @namespaced_file if defined?(@namespaced_file) + @namespaced_file = if location == :local + file + else + basename = File.basename file + _, type, __ = parse_db_filename basename + folder = type == "backup" ? "db_backup" : "db_dump" + + File.join(folder, basename) + end + end + + def root_file_in_uri? + opts[:file_in_uri] && false && [:nfs, :smb].include?(location) + end + + def namespaced_file_in_uri? + opts[:file_in_uri] && [:nfs, :smb].include?(location) + end + end +end diff --git a/test/support/appliance_console_runner.rb b/test/support/appliance_console_runner.rb index 4758c90..334d86b 100644 --- a/test/support/appliance_console_runner.rb +++ b/test/support/appliance_console_runner.rb @@ -34,15 +34,6 @@ def self.current :restore => /^(?\d*)\).*Restore Database.*$/, :quit => /^(?\d*)\).*Quit.*$/ }.freeze - IO_MENU_OPTION_REGEXPS = { - :local => /^(?\d*)\).*Local file.*$/, - :nfs => /^(?\d*)\).*Network File System.*$/, - :smb => /^(?\d*)\).*Samba.*$/, - :s3 => /^(?\d*)\).*Amazon S3.*$/, - :ftp => /^(?\d*)\).*File Transfer Protocol.*$/, - :swift => /^(?\d*)\).*OpenStack Swift.*$/, - :cancel => /^(?\d*)\).*Cancel.*$/, - }.freeze def self.backup file, mode = :local, split = nil new(:backup, file, mode, split).run_console @@ -80,7 +71,7 @@ def run_console output = Thread.new do begin while line = out.gets - debug "(output thread) #{line.inspect}" + debug "(output thread) #{line.chomp.inspect}" new_input = line_parser line @input += Array(new_input) if new_input end @@ -96,7 +87,7 @@ def run_console else sleep 1 # give the sub process a second to accept input input_line = @input.shift - debug "(input thread) #{input_line.inspect}" + debug "(input thread) #{input_line.chomp.inspect}" stdin.puts input_line end end @@ -179,6 +170,8 @@ def localize filename end def line_parser line + # debug line.split(CLEAR_CODE).last.strip.inspect + debug " state: #{state.inspect}" if line =~ PRESS_ANY_KEY_REGEXP @state = nil return "" @@ -191,11 +184,8 @@ def line_parser line @state = nil if match match && match[:OPTION_NUMBER] when :io_menu - match = line.match IO_MENU_OPTION_REGEXPS[io_key] - if match - @state = nil - inject_input match[:OPTION_NUMBER] - end + @state = nil + inject_input unless @finished else # state change check # Some of the menu prompts come right after a clear, and so `out.gets` # doesn't pick that up properly as a seperate line. Split will return @@ -207,28 +197,28 @@ def line_parser line end end - def inject_input input_number + def inject_input @finished = true - split_answers = split ? ["y", split] : ["n"] exclude_tables = if @table_exclusions.empty? ["n"] else ["y"] + @table_exclusions + [""] end - inject = [input_number] - inject << file - inject << @uri unless mode == :local - inject += SMB_CREDS if mode == :smb - inject += aws_input if mode == :s3 - inject += FTP_CREDS if mode == :ftp - inject += FTP_ANON if mode == :ftp_anonymous - inject += swift_input if mode == :swift + if [:smb, :nfs].include? mode + inject = [file] + else + inject = [file] + end + + # inject += SMB_CREDS if mode == :smb + # inject += aws_input if mode == :s3 + # inject += FTP_CREDS if mode == :ftp + # inject += FTP_ANON if mode == :ftp_anonymous + # inject += swift_input if mode == :swift inject += exclude_tables if type == :dump - inject += split_answers inject += ["n", "y"] if type == :restore && mode == :local # ^ don't delete backup (but answer) - inject end @@ -266,8 +256,8 @@ def io_key end end - def debug input + def debug input = "" @io_log.puts input - puts input if false + puts input if ENV["TEST_DEBUG"] end end diff --git a/test/support/appliance_rake_restore_patches.rb b/test/support/appliance_restore_patches.rb similarity index 60% rename from test/support/appliance_rake_restore_patches.rb rename to test/support/appliance_restore_patches.rb index 6ee52df..2aec7b5 100644 --- a/test/support/appliance_rake_restore_patches.rb +++ b/test/support/appliance_restore_patches.rb @@ -6,7 +6,7 @@ # is loaded. module LinuxAdmin def Service.new(service_name) - if PostgresAdmin.service_name == service_name + if ManageIQ::ApplianceConsole::PostgresAdmin.service_name == service_name ApplianceSecondaryDB else super @@ -14,14 +14,8 @@ def Service.new(service_name) end end -require "postgres_admin" - -# Patch PostgresAdmin to use different user/group -class PostgresAdmin - def self.user - "vagrant".freeze - end -end +console_codebase_dir = Dir["/opt/manageiq/manageiq-gemset/gems/manageiq-appliance_console-*"].first +require "#{console_codebase_dir}/lib/manageiq/appliance_console/postgres_admin.rb" ENV["APPLIANCE_PG_DATA"] = "/opt/manageiq/postgres_restore_pg/data" ENV["APPLIANCE_PG_SERVICE"] = "local_pg_instance" diff --git a/test/support/appliance_secondary_db.rb b/test/support/appliance_secondary_db.rb index d15ef5d..6bfbdf2 100644 --- a/test/support/appliance_secondary_db.rb +++ b/test/support/appliance_secondary_db.rb @@ -1,9 +1,9 @@ -require "etc" require "fileutils" class ApplianceSecondaryDB extend FileUtils + DB_USER = "postgres" PG_DIR = File.join("", "opt", "manageiq", "postgres_restore_pg").freeze RUN_DIR = File.join(PG_DIR, "run").freeze DATA_DIR = File.join(PG_DIR, "data").freeze @@ -25,30 +25,40 @@ def self.reset_db end def self.run_cmd cmd - cmd = %Q{su vagrant -lc \"#{cmd}\"} if Etc.getlogin == "vagrant" - system cmd, [:out, :err] => File::NULL + cmd = %Q{sudo -u #{DB_USER} #{cmd}} + cmd_options = ENV["TEST_DEBUG"] ? {} : {[:out, :err] => File::NULL} + + puts "$ #{cmd}" if ENV["TEST_DEBUG"] + + system cmd, cmd_options end def self.rebuild_data_dir - rm_rf Dir["#{DATA_DIR}/*"] - mkdir_p DATA_DIR - mkdir_p RUN_DIR - chown_R "vagrant", "vagrant", DATA_DIR - chown_R "vagrant", "vagrant", RUN_DIR + rm_rf DATA_DIR, verbose: !!ENV["TEST_DEBUG"] + mkdir_p DATA_DIR, verbose: !!ENV["TEST_DEBUG"] + mkdir_p RUN_DIR, verbose: !!ENV["TEST_DEBUG"] + chown_R DB_USER, DB_USER, DATA_DIR, verbose: !!ENV["TEST_DEBUG"] + puts `ls -lh #{DATA_DIR}` if ENV["TEST_DEBUG"] + chown_R DB_USER, DB_USER, RUN_DIR, verbose: !!ENV["TEST_DEBUG"] + puts `ls -lh #{DATA_DIR}` if ENV["TEST_DEBUG"] - run_cmd "pg_ctl initdb -D #{DATA_DIR} -o '-A trust'" + run_cmd "pg_ctl initdb -D #{DATA_DIR} -o '-A trust' -U #{DB_USER}" + chown_R DB_USER, DB_USER, DATA_DIR, verbose: !!ENV["TEST_DEBUG"] + puts `ls -lh #{DATA_DIR}` if ENV["TEST_DEBUG"] + chown_R DB_USER, DB_USER, RUN_DIR, verbose: !!ENV["TEST_DEBUG"] + puts `ls -lh #{DATA_DIR}` if ENV["TEST_DEBUG"] end def self.create_roles - run_cmd %Q{psql --port 5555 -h localhost -c \\"CREATE ROLE root WITH LOGIN CREATEDB SUPERUSER PASSWORD 'smartvm'\\" postgres} - run_cmd %Q{psql --port 5555 -h localhost -c \\"CREATE ROLE postgres\\" postgres} + run_cmd %Q{psql --port 5555 -h /opt/manageiq/postgres_restore_pg/run -c "CREATE ROLE root WITH LOGIN CREATEDB SUPERUSER PASSWORD 'smartvm'" postgres} + run_cmd %Q{psql --port 5555 -h /opt/manageiq/postgres_restore_pg/run -c "CREATE ROLE postgres" postgres} end def self.update_config conf_file = File.join DATA_DIR, 'postgresql.conf' conf = File.read conf_file conf.gsub! "include_dir = '/etc/manageiq/postgresql.conf.d'", "ssl = on" - conf << "\n\nunix_socket_directories = '#{RUN_DIR}, /tmp'" + conf << "\n\nunix_socket_directories = '#{RUN_DIR}'" File.write conf_file, conf end diff --git a/test/support/db_validator.rb b/test/support/db_validator.rb index 691a80c..e23b251 100644 --- a/test/support/db_validator.rb +++ b/test/support/db_validator.rb @@ -1,18 +1,18 @@ +require_relative './appliance_console_cli.rb' require_relative './appliance_secondary_db.rb' require_relative './constants.rb' require_relative './db_filename_helper.rb' require_relative './db_connection.rb' -require_relative './rake_helper.rb' require_relative './ssh.rb' DbConnection.configure class DbValidator - RAKE_RESTORE_PATCHES = "/vagrant/test/support/appliance_rake_restore_patches.rb" + RESTORE_PATCHES = "/vagrant/test/support/appliance_restore_patches.rb" include ::DbFilenameHelper - attr_reader :dbname, :restore_type, :rake_location, :split + attr_reader :dbname, :restore_type, :backup_location, :split def self.validate db new(db).matches_origial? @@ -50,6 +50,12 @@ def initialize db = :default end def matches_origial? + debug + debug + debug ".matches_origial?" + debug " #{counts.inspect}" + debug " #{self.class.defaults.inspect}" + debug counts == self.class.defaults end @@ -76,7 +82,7 @@ def has_custom_attributes? def with_connection ActiveRecord::Base.establish_connection @db.to_sym - # puts ActiveRecord::Base.connection_config.inspect + debug ActiveRecord::Base.connection_config.inspect yield ensure ActiveRecord::Base.remove_connection @@ -139,13 +145,13 @@ def load_database_backup SSH.run_commands DB_BACKUP_RESTORE_TEMPLATE % filepath else ApplianceSecondaryDB.reset_db - RakeHelper.run_rake :restore, rake_location, @db, - :dbname => dbname, - :port => "5555", - :hostname => "localhost", - :username => "root", - :password => "smartvm", - :rake_cmd_args => { :require => RAKE_RESTORE_PATCHES } + ApplianceConsoleCli.run_cmd :restore, backup_location, @db, + :dbname => dbname, + :port => "5555", + :hostname => "localhost", + :username => "root", + :password => "smartvm", + :rubyopt => "-r#{RESTORE_PATCHES}" end end @@ -159,11 +165,11 @@ def load_database_dump filepath = share_filepath_for :dump SSH.run_commands DB_BACKUP_DUMP_TEMPLATE % [dbname, dbname, filepath, dbname] else - RakeHelper.run_rake :restore, rake_location, @db, - :dbname => dbname, - :hostname => ::SHARE_IP, - :username => "root", - :password => "smartvm" + ApplianceConsoleCli.run_cmd :restore, backup_location, @db, + :dbname => dbname, + :hostname => ::SHARE_IP, + :username => "root", + :password => "smartvm" end end @@ -185,6 +191,10 @@ def dump_database_name def set_restore_vars @dbname = dump_database_name - @rake_location, @restore_type, _, @split = parse_db_filename dbname + @backup_location, @restore_type, _, @split = parse_db_filename dbname + end + + def debug input = "" + puts input if ENV["TEST_DEBUG"] end end diff --git a/test/support/mount_helper.rb b/test/support/mount_helper.rb index 055a5db..b19f33c 100644 --- a/test/support/mount_helper.rb +++ b/test/support/mount_helper.rb @@ -11,6 +11,7 @@ module MountHelper def run_in_mount mount_type if mount_cmd = MOUNT_TYPE_STRINGS[mount_type] @mount_point = Dir.mktmpdir "miq_" + puts "$ #{mount_cmd % @mount_point}" if ENV["TEST_DEBUG"] system mount_cmd % @mount_point end diff --git a/test/support/rake_helper.rb b/test/support/rake_helper.rb deleted file mode 100644 index cee88d5..0000000 --- a/test/support/rake_helper.rb +++ /dev/null @@ -1,242 +0,0 @@ -require 'etc' -require 'uri' -require 'fileutils' -require 'singleton' - -require_relative './vmdb_helper.rb' -require_relative './db_filename_helper.rb' - -class RakeHelper - ACTION_MAP = { - :backup => [:backup, {}], - :backup_fktmp => [:backup, { :fake_tmp => true }], - :dump => [:dump, {}], - :dump_fktmp => [:dump, { :fake_tmp => true }], - :split_backup => [:backup, { :split => true }], - :split_dump => [:dump, { :split => true }], - :restore => [:restore, { :file_in_uri => true }] - } - - include Singleton - include DbFilenameHelper - - def self.test type, location, file - action, opts = ACTION_MAP[type] - verbose = opts[:verbose] - debug = opts[:debug] - test_case = RakeTestCase.new file, action, location, opts - - if verbose || debug - puts - puts - puts type.inspect - puts location.inspect - puts file.inspect - puts opts.inspect - puts test_case.send(:build_command) - puts - test_case.run_test if verbose - else - test_case.run_test - end - end - - def self.run_rake type, location, file, rake_args - action, opts = ACTION_MAP[type] - opts[:args] = rake_args - verbose = opts[:args][:verbose] - debug = opts[:args][:debug] - rake_runner = RakeRunner.new file, action, location, opts - - if verbose || debug - puts - puts - puts type.inspect - puts location.inspect - puts file.inspect - puts rake_args.inspect - puts rake_runner.send(:build_command) - puts - rake_runner.run_command if verbose - else - rake_runner.run_command - end - end - - def self.validate_databases *db_backups - ::DbTestCase.validate *db_backups - end - - def self.backup_sizes - instance.backup_sizes - end - - attr_reader :backup_sizes - - def initialize - @backup_sizes = {} - end - -end - -class RakeRunner - DEFAULT_AUTH = %w[vagrant vagrant].freeze - REDACTED_AUTH = %w[******** ********].freeze - - CMD_FAILED_PROMPT = <<-MSG.gsub(/^ /, "").freeze - Command Failed - -------------- - - MSG - - include VmdbHelper - include DbFilenameHelper - - attr_reader :file, :action, :location, :opts, :rake_cmd_args, :verbose, :debug - - def initialize file, action, location, opts = {} - @file = file - @action = action - @location = location - @opts = opts - @debug = @opts.fetch(:args, {}).delete(:debug) - @verbose = @opts.fetch(:args, {}).delete(:verbose) - @rake_cmd_args = @opts.fetch(:args, {}).delete(:rake_cmd_args) || {} - - @redacted = false - end - - def run_command - out, status = [StringIO.new, nil] - run_in_vmdb do - out_data, pipe = IO.pipe - Thread.new { IO.copy_stream out_data, out } - system(wrapped_cmd, [:out, :err] => pipe) - pipe.close - status = $? - end - status.success? || fail("#{CMD_FAILED_PROMPT}$ #{wrapped_cmd}\n\n#{out.string}") - end - - private - - def build_command - "rake #{task_name} #{task_args}" - end - - def wrapped_cmd - "sudo /bin/sh -c 'source /etc/profile.d/evm.sh; bin/#{build_command}'" - end - - def task_name - @task_name ||= "evm:db:#{action}:#{location == :local ? :local : :remote}" - end - - def task_args options = {} - args = base_rake_cmd_args - - case location - when :local then args += "--local-file #{file}" - when :nfs then args += nfs_args - when :smb then args += smb_args - when :s3 then args += s3_args - when :ftp then args += ftp_args - when :swift then args += swift_args - end - - args += add_additional_args.to_s - args - end - - def nfs_args - generic_remote_args :path => '/var/nfs', :no_auth => true - end - - def smb_args - generic_remote_args :path => '/share' - end - - def s3_args - params = { - :host => S3Helper.suite_s3_bucket_name, - :auth => @redacted ? REDACTED_AUTH : S3Helper.auth_creds - } - - generic_remote_args params - end - - def ftp_args - if FTPHelper.is_anonymous? file - generic_remote_args :path => '/uploads', :no_auth => true - else - generic_remote_args - end - end - - def swift_args - params = { - :host => SwiftHelper.suite_host_name_and_port, - :path => SwiftHelper.suite_container_path_and_options, - :auth => @redacted ? REDACTED_AUTH : SwiftHelper.auth_creds - } - - generic_remote_args params - end - - def generic_remote_args params = {} - path = params[:path] || '' - host = params[:host] || ::SHARE_IP - user, pass = params[:auth] || DEFAULT_AUTH - uri = URI::Generic.new *URI.split("#{location}://#{host}#{path}") - uri.path << "/#{file}" if root_file_in_uri? - uri.path << "/#{namespaced_file}" if namespaced_file_in_uri? - - args = %Q{--uri "#{uri}"} - args << " --uri-username #{user}" unless params[:no_auth] - args << " --uri-password #{pass}" unless params[:no_auth] - args << " --remote-file-name #{file}" unless opts[:file_in_uri] - args - end - - def base_rake_cmd_args - result = args_hash_to_cmdline_string rake_cmd_args - result << " --trace" if verbose - result << " -- " - result - end - - def add_additional_args - args_hash_to_cmdline_string @opts[:args] if @opts[:args] - end - - def args_hash_to_cmdline_string args_hash - args_hash.map do |key, value| - if value.kind_of? Array - value.map { |v| "--#{key.to_s.tr('_', '-')} #{v}" }.join(" ") - else - "--#{key.to_s.tr('_', '-')} #{value}" - end - end.unshift(nil).join(" ") - end - - def namespaced_file - return @namespaced_file if defined?(@namespaced_file) - @namespaced_file = if location == :local - file - else - basename = File.basename file - _, type, __ = parse_db_filename basename - folder = type == "backup" ? "db_backup" : "db_dump" - - File.join(folder, basename) - end - end - - def root_file_in_uri? - opts[:file_in_uri] && false - end - - def namespaced_file_in_uri? - opts[:file_in_uri] && [:nfs, :smb].include?(location) - end -end