Skip to content

Commit

Permalink
Setup test infrastructure for workers and repostore
Browse files Browse the repository at this point in the history
  • Loading branch information
soumyaray committed Dec 6, 2017
1 parent c007d95 commit 5a5a83e
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 65 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ config/secrets.yml
coverage/
.bundle/
*.db
repostore/
temp/
_*
141 changes: 81 additions & 60 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'rake/testtask'

desc 'Print all rake commands'
task :default do
puts `rake -T`
end
Expand All @@ -13,127 +14,108 @@ task :config do
@config = @app.config
end

desc 'run tests'
desc 'Run tests once'
Rake::TestTask.new(:spec) do |t|
t.pattern = 'spec/*_spec.rb'
t.warning = false
end

desc 'rerun tests'
desc 'Keep rerunning tests upon changes'
task :respec => :config do
puts 'REMEMBER: need to run `rake run:[dev|test]:worker` in another process'
sh "rerun -c 'rake spec' --ignore 'coverage/*' --ignore '#{@config.REPOSTORE_PATH}/*'"
end

desc 'run application console (pry)'
desc 'Run application console (pry)'
task :console do
sh 'pry -r ./spec/test_load_all'
end

namespace :run do
namespace :api do
task :dev => :config do
namespace :api do
namespace :run do
desc 'Rerun the API server in development mode'
task :development => :config do
puts 'REMEMBER: need to run `rake run:dev:worker` in another process'
sh "rerun -c 'rackup -p 3030' --ignore 'coverage/*' --ignore '#{@config.REPOSTORE_PATH}/*'"
sh "rerun -c 'rackup -p 3030' --ignore '#{@config.REPOSTORE_PATH}/*'"
end

desc 'Rerun the API server in test mode'
task :test => :config do
puts 'REMEMBER: need to run `rake run:test:worker` in another process'
sh "rerun -c 'RACK_ENV=test rackup -p 3000' --ignore 'coverage/*' --ignore '#{@config.REPOSTORE_PATH}/*'"
end
end
end

namespace :worker do
task :dev => :config do
sh 'bundle exec shoryuken -r ./workers/clone_repo_worker.rb -C ./workers/shoryuken.yml'
namespace :worker do
namespace :run do
desc 'Run the background cloning worker in development mode'
task :development => :config do
sh 'RACK_ENV=development bundle exec shoryuken -r ./workers/clone_repo_worker.rb -C ./workers/shoryuken_dev.yml'
end

desc 'Run the background cloning worker in testing mode'
task :test => :config do
sh 'RACK_ENV=test bundle exec shoryuken -r ./workers/clone_repo_worker.rb -C ./workers/shoryuken_test.yml'
end
end
end

namespace :ls do
desc 'list cloned repos in repo store'
task :repostore => :config do
puts `ls #{@config.REPOSTORE_PATH}`
desc 'Run the background cloning worker in production mode'
task :production => :config do
sh 'RACK_ENV=production bundle exec shoryuken -r ./workers/clone_repo_worker.rb -C ./workers/shoryuken.yml'
end
end
end

namespace :rm do
desc 'delete cassette fixtures'
task :vcr do
namespace :vcr do
desc 'Delete cassette fixtures'
task :delete do
sh 'rm spec/fixtures/cassettes/*.yml' do |ok, _|
puts(ok ? 'Cassettes deleted' : 'No cassettes found')
end
end
end

desc 'delete cloned repos in repo store'
task :repostore => :config do
namespace :repostore do
desc 'List cloned repos in repo store'
task :create => :config do
puts `mkdir #{@config.REPOSTORE_PATH}`
end

desc 'Delete cloned repos in repo store'
task :delete => :config do
sh "rm -rf #{@config.REPOSTORE_PATH}/*" do |ok, _|
puts(ok ? 'Cloned repos deleted' : "Could not delete cloned repos")
end
end

desc 'List cloned repos in repo store'
task :list => :config do
puts `ls #{@config.REPOSTORE_PATH}`
end
end

namespace :quality do
CODE = '**/*.rb'

desc 'run all quality checks'
desc 'Run all quality checks'
task all: %i[rubocop reek flog]

desc 'Run Rubocop quality checks'
task :rubocop do
sh "rubocop #{CODE}"
end

desc 'Run Reek quality checks'
task :reek do
sh "reek #{CODE}"
end

desc 'Run Flog quality checks'
task :flog do
sh "flog #{CODE}"
end
end

namespace :db do
require_relative 'config/environment.rb' # load config info
require 'sequel' # TODO: remove after create orm

Sequel.extension :migration
app = CodePraise::Api

desc 'Run migrations'
task :migrate do
puts "Migrating #{app.environment} database to latest"
Sequel::Migrator.run(app.DB, 'infrastructure/database/migrations')
end

desc 'Drop all tables'
task :drop do
require_relative 'config/environment.rb'
# drop according to dependencies
app.DB.drop_table :repos_contributors
app.DB.drop_table :repos
app.DB.drop_table :collaborators
app.DB.drop_table :schema_info
end

desc 'Reset all database tables'
task reset: [:drop, :migrate]

desc 'Delete dev or test database file'
task :wipe do
if app.environment == :production
puts 'Cannot wipe production database!'
return
end

FileUtils.rm(app.config.DB_FILENAME)
puts "Deleted #{app.config.DB_FILENAME}"
end
end

namespace :queue do
require 'aws-sdk-sqs'

Expand Down Expand Up @@ -161,6 +143,7 @@ namespace :queue do
end
end

desc "Purge messages in SQS queue for Shoryuken"
task :purge => :config do
sqs = Aws::SQS::Client.new(region: @config.AWS_REGION)

Expand All @@ -172,3 +155,41 @@ namespace :queue do
end
end
end

namespace :db do
require_relative 'config/environment.rb' # load config info
require 'sequel' # TODO: remove after create orm

Sequel.extension :migration
app = CodePraise::Api

desc 'Run migrations'
task :migrate do
puts "Migrating #{app.environment} database to latest"
Sequel::Migrator.run(app.DB, 'infrastructure/database/migrations')
end

desc 'Drop all tables'
task :drop do
require_relative 'config/environment.rb'
# drop according to dependencies
app.DB.drop_table :repos_contributors
app.DB.drop_table :repos
app.DB.drop_table :collaborators
app.DB.drop_table :schema_info
end

desc 'Reset all database tables'
task reset: [:drop, :migrate]

desc 'Delete dev or test database file'
task :wipe do
if app.environment == :production
puts 'Cannot wipe production database!'
return
end

FileUtils.rm(app.config.DB_FILENAME)
puts "Deleted #{app.config.DB_FILENAME}"
end
end
12 changes: 11 additions & 1 deletion config/secrets.yml.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
---
development:
GH_TOKEN: <personal token for Github API>
AWS_ACCESS_KEY_ID: <aws credentials>
AWS_SECRET_ACCESS_KEY: <aws credentials>
AWS_REGION: <aws credentials>
CLONE_QUEUE: <aws sqs queue>
CLONE_QUEUE_URL: <aws sqs queue url>

test:
GH_TOKEN: <personal token for Github API>
GH_TOKEN: <personal token for Github API>
AWS_ACCESS_KEY_ID: <aws credentials>
AWS_SECRET_ACCESS_KEY: <aws credentials>
AWS_REGION: <aws credentials>
CLONE_QUEUE: <aws sqs queue>
CLONE_QUEUE_URL: <aws sqs queue url>
4 changes: 4 additions & 0 deletions config/setup_codeship
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
rvm use 2.4.2 --install
bundle install --without production
RACK_ENV=test bundle exec rake db:migrate
bundle exec rake run:worker:test &
5 changes: 5 additions & 0 deletions infrastructure/gitrepo/repostore/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This file puts the repostore folder into git version control:
# https://steindom.com/articles/add-empty-directory-git-repository

*
!.gitignore
6 changes: 4 additions & 2 deletions spec/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

describe 'Cloning repos' do
before do
CodePraise::Repository::RepoStore.delete_all!
CodePraise::LoadFromGithub.new.call(
config: app.config,
ownername: USERNAME,
reponame: REPO_NAME
)

CodePraise::Repository::RepoStore.delete_all!
end

after do
Expand All @@ -34,7 +35,6 @@
get "#{API_VER}/summary/#{USERNAME}/#{REPO_NAME}"
_(last_response.status).must_equal 202

STDOUT.sync
5.times { sleep(1); print '.' }

get "#{API_VER}/summary/#{USERNAME}/#{REPO_NAME}"
Expand Down Expand Up @@ -100,6 +100,8 @@
ownername: USERNAME,
reponame: REPO_NAME
)

CodePraise::Repository::Repos.clone_all!
end

it '(HAPPY) should get blame summary for root of loaded repo' do
Expand Down
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
c.filter_sensitive_data('<GITHUB_TOKEN_ESC>') { CGI.escape(github_token) }
end

# To flush dots and output during testing
STDOUT.sync

# DB = app.DB
# require 'database_cleaner'
# DatabaseCleaner.strategy = :truncation
2 changes: 2 additions & 0 deletions workers/shoryuken_dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
queues:
- https://sqs.us-east-1.amazonaws.com/503315808870/codepraise-clone_dev.fifo

0 comments on commit 5a5a83e

Please sign in to comment.