Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tests for rakeless DB admin #1

Merged
merged 8 commits into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
24 changes: 18 additions & 6 deletions provision_scripts/appliance_sync.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
17 changes: 14 additions & 3 deletions test/nfs_backup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
17 changes: 14 additions & 3 deletions test/smb_backup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
209 changes: 209 additions & 0 deletions test/support/appliance_console_cli.rb
Original file line number Diff line number Diff line change
@@ -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
Loading