Browse files

simplified rake_helper and removed unnecessary rake tasks

  • Loading branch information...
1 parent 5b8cfa2 commit 099dcfb1580a68857088139fbabc643af0d3938e Dmitriy Kalinin and Mark Rushakoff committed Mar 26, 2013
Showing with 131 additions and 333 deletions.
  1. +12 −62 Rakefile
  2. +1 −1 lib/harness/cfsession.rb
  3. +118 −270 lib/harness/rake_helper.rb
View
74 Rakefile
@@ -3,72 +3,22 @@ require "rspec/core/rake_task"
require "harness"
include BVT::Harness
-task :default => :full
+desc "Prepare for running parallel specs"
+task :prepare => ["assets:sync", "create_users"]
-desc "run full tests in parallel, e.g. rake full[5] (default to 10, max = 16)"
-task :full, :thread_number do |t, args|
- RakeHelper.sync_assets
- threads = 10
- threads = args[:thread_number].to_i if args[:thread_number]
- RakeHelper.prepare_all(threads)
- RakeHelper.run(threads, {'tags' => '~admin'})
+desc "Create 16 non-admin users (saved to ~/.bvt/config)"
+task :create_users do
+ RakeHelper.prepare_all(16)
end
-desc "run java tests (spring, java_web) in parallel, e.g. rake java[5] (default to 10, max = 16)"
-task :java, :thread_number, :longevity, :fail_fast do |t, args|
- RakeHelper.sync_assets
- threads = 10
- threads = args[:thread_number].to_i if args[:thread_number]
- RakeHelper.prepare_all(threads)
- RakeHelper.run(threads, {'pattern' => /_(spring|java_web)_spec\.rb/})
-end
-
-desc "tests (spring, java_web, grails, lift) in parallel e.g. rake jvm[5] (default to 10, max = 16)"
-task :jvm, :thread_number do |t, args|
- RakeHelper.sync_assets
- threads = 10
- threads = args[:thread_number].to_i if args[:thread_number]
- RakeHelper.prepare_all(threads)
- RakeHelper.run(threads, {'pattern' => /_(spring|java_web|grails|lift)_spec\.rb/})
-end
-
-desc "run ruby tests (rails3, sinatra, rack) in parallel e.g. rake ruby[5] (default to 10, max = 16)"
-task :ruby, :thread_number do |t, args|
- threads = 10
- threads = args[:thread_number].to_i if args[:thread_number]
- RakeHelper.prepare_all(threads)
- RakeHelper.run(threads, {'pattern' => /ruby_.+_spec\.rb/})
+desc "Delete yeti-like organizations"
+task :delete_orgs do
+ exec "./tools/scripts/yeti-hunter.rb"
end
-desc "run service tests (mongodb/redis/mysql/postgres/rabbitmq/neo4j/vblob) in parallel e.g. rake services[5] (default to 10, max = 16)"
-task :services, :thread_number do |t, args|
- RakeHelper.sync_assets
- threads = 10
- threads = args[:thread_number].to_i if args[:thread_number]
- RakeHelper.prepare_all(threads)
-
- if ENV["VCAP_BVT_SERVICE"]
- RakeHelper.run(threads, {'tags' => "~admin,#{ENV["VCAP_BVT_SERVICE"].downcase}"})
- else
- RakeHelper.run(threads, {'tags' => '~admin,mongodb,rabbitmq,mysql,redis,postgresql,neo4j,vblob'})
+namespace :assets do
+ desc "Sync yeti assets binaries"
+ task :sync do
+ RakeHelper.sync_assets
end
end
-
-desc <<-CLEAN
-clean up test environment(only run this task after interruption).
- 1, Remove all apps and services under test user
- 2, Remove all apps and services under parallel users
-CLEAN
-task :clean do
- RakeHelper.cleanup!
-end
-
-desc "sync yeti assets binaries"
-task :sync_assets do
- RakeHelper.sync_assets
-end
-
-desc "delete yeti-like organizations"
-task :delete_orgs do
- sh "./tools/scripts/yeti-hunter.rb"
-end
View
2 lib/harness/cfsession.rb
@@ -30,7 +30,7 @@ def initialize(options = {})
end
end
- LoggerHelper::set_logger(@TARGET)
+ LoggerHelper.set_logger(@TARGET)
@log = get_logger
@namespace = get_namespace
View
388 lib/harness/rake_helper.rb
@@ -14,91 +14,67 @@ module RakeHelper
VCAP_BVT_DEFAULT_USER = "test@vcap.me"
VCAP_BVT_DEFAULT_ADMIN = "admin@vcap.me"
- def prepare_all(threads=nil)
+ def prepare_all(threads)
+ if threads < 1 || threads > VCAP_BVT_PARALLEL_MAX_USERS
+ abort("Threads number must be within 1..#{VCAP_BVT_PARALLEL_MAX_USERS}")
+ end
+
Dir.mkdir(VCAP_BVT_HOME) unless Dir.exists?(VCAP_BVT_HOME)
@print_config = []
+
get_target
check_network_connection
- if threads == nil
- get_admin_user
- get_admin_user_passwd
- elsif threads < 1 || threads > VCAP_BVT_PARALLEL_MAX_USERS
- puts red("threads number must be within 1~#{VCAP_BVT_PARALLEL_MAX_USERS}")
- exit(1)
- else
- parallel_users = get_parallel_users(true)
- if threads > 1
- if parallel_users.size == 0
- parallel_users = create_parallel_users(VCAP_BVT_PARALLEL_MAX_USERS)
- elsif parallel_users.size < threads
- puts yellow("no enough parallel users, yeti will use all of the #{parallel_users.size} users")
- end
+ parallel_users = get_parallel_users(true)
+ if threads > 1
+ if parallel_users.size == 0
+ parallel_users = create_parallel_users(VCAP_BVT_PARALLEL_MAX_USERS)
+ elsif parallel_users.size < threads
+ puts yellow("Not enough parallel users, yeti will use all of the #{parallel_users.size} users")
end
- check_env_user = get_check_env_user(parallel_users)
- check_environment(check_env_user)
end
+ user = get_check_env_user(parallel_users)
+ generate_profile(user)
+
save_config
print_test_config
end
- def check_environment(user)
- client = BVT::Harness::CFSession.new(:email => user['email'],
- :passwd => user['passwd'],
- :target => @config['target'])
+ def generate_profile(user)
+ client = BVT::Harness::CFSession.new(
+ :email => user['email'],
+ :passwd => user['passwd'],
+ :target => @config['target']
+ )
+
profile = {}
profile[:services] = client.system_services
profile[:script_hash] = get_script_git_hash
+
target_without_http = @config['target'].split('//')[-1]
- $vcap_bvt_profile_file ||= File.join(BVT::Harness::VCAP_BVT_HOME,
- "profile.#{target_without_http}.yml")
+ $vcap_bvt_profile_file ||=
+ File.join(BVT::Harness::VCAP_BVT_HOME,
+ "profile.#{target_without_http}.yml")
File.open($vcap_bvt_profile_file, "w") { |f| f.write YAML.dump(profile) }
- end
+ end
def check_network_connection
url = "#{@config['target']}/info"
+
begin
r = RestClient.get url
rescue
raise RuntimeError,
- red("Cannot connect to target environment, #{url}\n" +
- "Please check your network connection to target environment.")
+ red("Cannot connect to target environment, #{url}\n" +
+ "Please check your network connection to target environment.")
end
+
unless r.code == HTTP_RESPONSE_CODE::OK
raise RuntimeError,
- red("URL: #{url} response code is: " +
- "#{r.code}\nPlease check your target environment first.")
- end
- end
-
- def check_user_availability(user)
- begin
- client = BVT::Harness::CFSession.new(:email => user['email'],
- :passwd => user['passwd'],
- :target => @config['target'])
- rescue => e
- puts e.message
- return false
- end
- return true
- end
-
- def cleanup!
- get_target
- get_user
- get_user_passwd
- save_config
- check_network_connection
- cleanup_user_data(@config['user']['email'], @config['user']['passwd'])
- if @config['parallel']
- @config['parallel'].each do |puser|
- cleanup_user_data(puser['email'], puser['passwd'])
- end
+ red("URL: #{url} response code is: " +
+ "#{r.code}\nPlease check your target environment first.")
end
- # clear parallel env
- ENV.delete('YETI_PARALLEL_USER')
- ENV.delete('YETI_PARALLEL_USER_PASSWD')
end
def sync_assets
@@ -167,11 +143,13 @@ def print_test_config
def get_config
if File.exists?(VCAP_BVT_CONFIG_FILE)
@multi_target_config = YAML.load_file(VCAP_BVT_CONFIG_FILE)
- raise "Invalid config file format, #{VCAP_BVT_CONFIG_FILE}" unless @multi_target_config.is_a?(Hash)
+ raise "Invalid config file format, #{VCAP_BVT_CONFIG_FILE}" \
+ unless @multi_target_config.is_a?(Hash)
else
- @multi_target_config = {} unless @multi_target_config
+ @multi_target_config ||= {}
end
- @config = {} unless @config
+
+ @config ||= {}
# since multi-target information is stored in one config file,
# so usually get_config method just initiate @config, and @multi_target_config
@@ -190,9 +168,7 @@ def get_config
@config
end
- def save_config(hash = nil)
- @config = hash || @config
-
+ def save_config
## remove password
@config['user'].delete('passwd') if @config['user']
@config['admin'].delete('passwd') if @config['admin']
@@ -202,155 +178,113 @@ def save_config(hash = nil)
@multi_target_config[@config['target']] = @config
- File.open(VCAP_BVT_CONFIG_FILE, "w") { |f| f.write YAML.dump(@multi_target_config) }
+ File.open(VCAP_BVT_CONFIG_FILE, "w") do |f|
+ f.write YAML.dump(@multi_target_config)
+ end
+
+ puts "Wrote config to #{VCAP_BVT_CONFIG_FILE}"
end
def get_target
- if ENV['VCAP_BVT_TARGET']
- target = format_target(ENV['VCAP_BVT_TARGET'])
- @print_config << "target read from ENV: \t\t#{yellow(target)}" if @print_config
- else
- input = ask_and_validate("VCAP Target",
- '\A.*',
- VCAP_BVT_DEFAULT_TARGET)
- target = format_target(input)
- end
- @multi_target_config = {} unless @multi_target_config
- @multi_target_config[target] = {} unless @multi_target_config.key?(target)
+ target = require_env!("VCAP_BVT_TARGET")
+ target = format_target(target)
+
+ @multi_target_config ||= {}
+ @multi_target_config[target] ||= {}
+
@config = @multi_target_config[target]
ENV['VCAP_BVT_TARGET'] = target
+
get_config
@config['target'] = target
- @config['target']
end
def get_admin_user
get_config unless @config
- @config['admin'] = {} if @config['admin'].nil?
- if ENV['VCAP_BVT_ADMIN_USER']
- @config['admin']['email'] = ENV['VCAP_BVT_ADMIN_USER']
- @print_config << "admin user read from ENV: \t#{yellow(@config['admin']['email'])}" if @print_config
- elsif @config['admin']['email'].nil?
- @config['admin']['email'] = ask_and_validate('Admin User',
- '\A.*\@',
- VCAP_BVT_DEFAULT_ADMIN
- )
- else
- @print_config << "admin user read from #{VCAP_BVT_CONFIG_FILE}: " +
- "\t#{yellow(@config['admin']['email'])}" if @print_config
- end
- @config['admin']['email']
+ @config['admin'] ||= {}
+ @config['admin']['email'] = require_env!("VCAP_BVT_ADMIN_USER")
end
def get_admin_user_passwd
get_config unless @config
- if ENV['VCAP_BVT_ADMIN_USER_PASSWD']
- @config['admin']['passwd'] = ENV['VCAP_BVT_ADMIN_USER_PASSWD']
- elsif @config['admin']['passwd'].nil?
- @config['admin']['passwd'] = ask_and_validate("Admin User Passwd " +
- "(#{yellow(@config['admin']['email'])})",
- '.*',
- '*',
- '*'
- )
- end
- ENV['VCAP_BVT_ADMIN_USER_PASSWD'] = @config['admin']['passwd'].to_s
- @config['admin']['passwd'] = @config['admin']['passwd'].to_s
- @config['admin']['passwd']
+ @config['admin'] ||= {}
+ @config['admin']['passwd'] = require_env!("VCAP_BVT_ADMIN_USER_PASSWD")
end
def get_user
get_config unless @config
- @config['user'] = {} if @config['user'].nil?
- if ENV['VCAP_BVT_USER']
- @config['user']['email'] = ENV['VCAP_BVT_USER']
- @print_config << "normal user read from ENV: \t#{yellow(@config['user']['email'])}" if @print_config
- elsif @config['user']['email'].nil?
- @config['user']['email'] = ask_and_validate('Non-admin User',
- '\A.*\@',
- VCAP_BVT_DEFAULT_USER
- )
- else
- @print_config << "normal user read from #{VCAP_BVT_CONFIG_FILE}: " +
- "\t#{yellow(@config['user']['email'])}" if @print_config
- end
- @config['user']['email']
+ @config['user'] ||= {}
+ @config['user']['email'] = require_env!("VCAP_BVT_USER")
end
def get_user_passwd
get_config unless @config
- if ENV['VCAP_BVT_USER_PASSWD']
- @config['user']['passwd'] = ENV['VCAP_BVT_USER_PASSWD']
- elsif @config['user'].nil? || @config['user']['passwd'].nil?
- @config['user']['passwd'] = ask_and_validate("User Passwd " +
- "(#{yellow(@config['user']['email'])})",
- '.*',
- '*',
- '*')
- end
- ENV['VCAP_BVT_USER_PASSWD'] = @config['user']['passwd'].to_s
- @config['user']['passwd'] = @config['user']['passwd'].to_s
- @config['user']['passwd']
+ @config['user'] ||= {}
+ @config['user']['passwd'] = require_env!("VCAP_BVT_USER_PASSWD")
end
def create_parallel_users(user_number)
- puts "need admin account to create parallel users"
+ puts "Using admin account to create parallel users:"
get_admin_user
get_admin_user_passwd
@config['parallel'] = []
- session = nil
- begin
- session = BVT::Harness::CFSession.new(:admin => true,
- :email => @config['admin']['email'],
- :passwd => @config['admin']['passwd'],
- :target => @config['target'])
- rescue Exception => e
- raise RuntimeError, "#{e.to_s}\nPlease input valid admin credential " +
- "for parallel running"
- end
+ session = BVT::Harness::CFSession.new(
+ :admin => true,
+ :email => @config['admin']['email'],
+ :passwd => @config['admin']['passwd'],
+ :target => @config['target']
+ )
passwd = 'aZ_x13YcIa4nhl' #parallel user secret
+
(1..user_number).to_a.each do |index|
config = {}
+
if session.v2?
- @uaa_cc_secret ||= get_uaa_cc_secret
uaa_url = @config['target'].gsub(/\/\/\w+/, '//uaa')
- email = session.namespace + "#{index}-test_user@vmware.com"
- org_name = session.namespace + "yeti_test_org-#{index}"
+ email = "#{session.namespace}#{index}-test_user@vmware.com"
+ org_name = "#{session.namespace}yeti_test_org-#{index}"
space_name = "yeti_test_space"
- CCNGUserHelper.create_user(uaa_url, @uaa_cc_secret, @config['target'], @config['admin']['email'],
- @config['admin']['passwd'], email, passwd, org_name, space_name)
- config['email'] = email
+
+ CCNGUserHelper.create_user(
+ uaa_url, get_uaa_cc_secret,
+ @config['target'],
+ @config['admin']['email'],
+ @config['admin']['passwd'],
+ email, passwd,
+ org_name, space_name
+ )
+ config['email'] = email
else
email = "#{index}-test_user@vmware.com"
user = session.user(email)
user.create(passwd)
config['email'] = user.email
end
+
config['passwd'] = passwd
- puts "create user: #{yellow(config['email'])}"
+ puts " - #{config['email']}"
$stdout.flush
+
@config['parallel'] << config
end
+
@config['admin'].delete('passwd')
@config['parallel']
end
- def get_parallel_users(check_user=false)
- parallel_users = []
- parallel_users = @config['parallel'] if @config && @config['parallel']
- if parallel_users.size > 0
- parallel_users.each do |puser|
- puser['passwd'] = puser['passwd'].to_s
- end
- if check_user
- if check_user_availability(parallel_users[0]) == false
- puts red("can't login target env using parallel user: #{parallel_users[0]}")
- exit(1)
- end
+ def get_parallel_users(check_first_user=false)
+ @config ||= {}
+ return [] unless parallel_users = @config['parallel']
+
+ if check_first_user
+ first_user = parallel_users[0]
+ unless check_user_availability(first_user)
+ abort("Cannot login using first parallel user: #{first_user}")
end
end
+
parallel_users
end
@@ -381,118 +315,13 @@ def format_target(str)
end
def get_uaa_cc_secret
- @uaa_cc_secret = nil
- if ENV['VCAP_BVT_UAA_CC_SECRET']
- @uaa_cc_secret = ENV['VCAP_BVT_UAA_CC_SECRET']
- elsif ENV['VCAP_BVT_DEPLOY_MANIFEST']
- begin
- deploy_manifest = YAML.load_file(ENV['VCAP_BVT_DEPLOY_MANIFEST'])
- @uaa_cc_secret = deploy_manifest['properties']['uaa']['cc']['client_secret']
- rescue
- puts red("can't find uaa_cc_secret in your manifest")
- exit(-1)
- end
- else
- @uaa_cc_secret = ask_and_validate("uaa_cc_client_secret", '\S*')
- end
- @uaa_cc_secret
+ require_env!("VCAP_BVT_UAA_CC_SECRET")
end
private
- def ask_and_validate(question, pattern, default = nil, echo = nil)
- res = ask(question, :default => default, :echo => echo)
- while res !~ /#{pattern}/
- puts "Incorrect input"
- res = ask(question, :default => default, :echo => echo)
- end
- res
- end
-
def get_script_git_hash
- `git log --pretty=oneline`.split("\n").first
- end
-
- def cleanup_user_data(email, passwd)
- session = BVT::Harness::CFSession.new(:email => email,
- :passwd => passwd,
- :target => @config['target'])
- puts yellow("Ready to clean up for test user: #{session.email}")
-
- services = session.client.service_instances
- puts yellow("Begin to clean up services")
- cleanup_data(session, services)
-
- apps = session.client.apps
- puts yellow("Begin to clean up apps")
- cleanup_data(session, apps)
-
- if session.v2?
- routes = session.client.routes
- puts yellow("Begin to clean up routes")
- cleanup_data(session, routes)
-
- domains = session.client.domains
- puts yellow("Begin to clean up domains")
- cleanup_data(session, domains)
- end
-
- puts yellow("Clean up work for test user: #{session.email} has been done.\n")
- end
-
- def is_parallel_user?(email)
- return true if email =~ /^t\w{6,7}-\d{1,2}-/ #parallel user, delete without asking
- false
- end
-
- def cleanup_data(session, objs)
- if objs.empty?
- puts "No instance has been created by test user: #{session.email}"
- elsif is_parallel_user?(session.email)
- objs.each{ |obj|
- puts "deleting #{obj.class.to_s}: #{obj.name}..."
- obj.delete!
- }
- else
- puts "List all instances belong to test user: #{session.email}"
- # filter system domain, since system domain cannot be removed via normal user
- if objs.first.class.to_s =~ /Domain/
- system_domain = session.TARGET.split(".", 2).last
- objs.reject! {|obj| obj.name =~ /#{system_domain}/}
- end
-
- objs.each { |obj| puts obj.name }
- if ask("Do you want to remove all above instances?", :default => true)
- objs.each { |obj| obj.delete! }
- puts yellow("all instances belong to #{session.email} have been removed")
- else
- puts yellow("keep above ones\n")
- end
- end
- end
-
- def cleanup_test_accounts
- test_user_template = 'my_fake@email.address'
-
- session = BVT::Harness::CFSession.new(:admin => true,
- :email => @config['admin']['email'],
- :passwd => @config['admin']['passwd'],
- :target => @config['target'])
- puts yellow("Ready to remove all test users created in admin_user_spec.rb")
- users = session.users.select { |user| user.email =~ /^t.*-#{test_user_template}$/ }
-
- if users.empty?
- puts "No test user need to be deleted."
- else
- puts "List all test users"
- users.each { |user| puts user.email }
- if ask("Do you want to remove all above users?", :default => true)
- users.each { |user| user.delete }
- else
- puts yellow("Keep those test users\n")
- end
- end
- puts yellow("Clean up test accounts has been done.\n")
+ `git log -1 --pretty=oneline`.split("\n").first
end
def get_assets_info
@@ -501,8 +330,8 @@ def get_assets_info
r = RestClient.get url
rescue
raise RuntimeError,
- red("Cannot connect to yeti assets storage server, #{url}\n" +
- "Please check your network connection.")
+ "Cannot connect to yeti assets storage server, #{url}\n" +
+ "Please check your network connection."
end
if r.code == HTTP_RESPONSE_CODE::OK
@@ -532,8 +361,7 @@ def download_binary(filepath)
end
rescue
raise RuntimeError,
- red("Download faild, might be caused by unstable network.\n" +
- "Please try again.")
+ "Download failed, might be caused by unstable network."
end
if r && r.code == HTTP_RESPONSE_CODE::OK
@@ -556,6 +384,26 @@ def parallel_run_number
ENV["TEST_ENV_NUMBER"].to_i
end
+ def require_env!(var_name)
+ if value = ENV[var_name]
+ value
+ else
+ abort("Please specify #{var_name}")
+ end
+ end
+
+ def check_user_availability(user)
+ BVT::Harness::CFSession.new(
+ :email => user['email'],
+ :passwd => user['passwd'],
+ :target => @config['target']
+ )
+ true
+ rescue => e
+ puts e.message
+ false
+ end
+
extend self
end
end

0 comments on commit 099dcfb

Please sign in to comment.