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 to use V3 of API #35

Merged
merged 6 commits into from
Jun 16, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ env:
- HATCHET_APP_LIMIT=80
- secure: TvpZ0CrIe0FqjyTUOAtVqjHHrtF1esMroa00bYYRBas050/y7ygVpAn9utFZZChgt1PUbM48I01UaQglGxtmXVl3ahQXtPpXlzlwJOlDS09dlZFfLenkv530/pxIlpRtqk4q18gCoLBblXX7RZu3TGt0qds+o8dQrBzL6QifAHs=
sudo: false
dist: trusty
addons:
apt:
sources:
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
source "http://rubygems.org"

gemspec

gem 'm'
3 changes: 1 addition & 2 deletions hatchet.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ Gem::Specification.new do |gem|
gem.add_dependency "threaded", "~> 0"


gem.add_development_dependency "test-unit", "~> 3.0"
gem.add_development_dependency "minitest", "~> 4.0"
gem.add_development_dependency "minitest", "~> 4.2"
gem.add_development_dependency "rake", "~> 10"
gem.add_development_dependency "mocha", "~> 1"
gem.add_development_dependency "parallel_tests", "~> 0"
Expand Down
2 changes: 0 additions & 2 deletions lib/hatchet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
module Hatchet
end



require 'hatchet/version'
require 'hatchet/reaper'
require 'hatchet/test_run'
Expand Down
35 changes: 24 additions & 11 deletions lib/hatchet/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def initialize(repo_name, options = {})
@allow_failure = options[:allow_failure] || false
@labs = ([] << options[:labs]).flatten.compact
@buildpack = options[:buildpack] || options[:buildpack_url] || [HATCHET_BUILDPACK_BASE, HATCHET_BUILDPACK_BRANCH.call].join("#")
@reaper = Reaper.new(heroku)
@reaper = Reaper.new(platform_api: platform_api)
end

def allow_failure?
Expand All @@ -46,33 +46,38 @@ def config

def set_config(options = {})
options.each do |key, value|
heroku.put_config_vars(name, key => value)
# heroku.put_config_vars(name, key => value)
platform_api.config_var.update(name, key => value)
end
end

def get_config
heroku.get_config_vars(name).body
# heroku.get_config_vars(name).body
platform_api.config_var.info_for_app(name)
end

def lab_is_installed?(lab)
get_labs.any? {|hash| hash["name"] == lab }
end

def get_labs
heroku.get_features(name).body
# heroku.get_features(name).body
platform_api.app_feature.list(name)
end

def set_labs!
@labs.each {|lab| set_lab(lab) }
end

def set_lab(lab)
heroku.post_feature(lab, name)
# heroku.post_feature(lab, name)
platform_api.app_feature.update(name, lab, enabled: true)
end

def add_database(db_name = 'heroku-postgresql:dev', match_val = "HEROKU_POSTGRESQL_[A-Z]+_URL")
def add_database(plan_name = 'heroku-postgresql:dev', match_val = "HEROKU_POSTGRESQL_[A-Z]+_URL")
Hatchet::RETRIES.times.retry do
heroku.post_addon(name, db_name)
# heroku.post_addon(name, plan_name)
platform_api.addon.create(name, plan: plan_name )
_, value = get_config.detect {|k, v| k.match(/#{match_val}/) }
set_config('DATABASE_URL' => value)
end
Expand Down Expand Up @@ -107,13 +112,17 @@ def not_debugging?
alias :no_debug? :not_debugging?

def deployed?
!heroku.get_ps(name).body.detect {|ps| ps["process"].include?("web") }.nil?
# !heroku.get_ps(name).body.detect {|ps| ps["process"].include?("web") }.nil?
platform_api.formation.list(name).detect {|ps| ps["type"] == "web"}
end

def create_app
3.times.retry do
begin
heroku.post_app({ name: name, stack: stack }.delete_if {|k,v| v.nil? })
# heroku.post_app({ name: name, stack: stack }.delete_if {|k,v| v.nil? })
hash = { name: name, stack: stack }
hash.delete_if { |k,v| v.nil? }
platform_api.app.create(hash)
rescue Heroku::API::Errors::RequestFailed => e
@reaper.cycle if e.message.match(/app limit/)
raise e
Expand All @@ -127,6 +136,8 @@ def setup!
puts "Hatchet setup: #{name.inspect} for #{repo_name.inspect}"
create_app
set_labs!
# heroku.put_config_vars(name, 'BUILDPACK_URL' => @buildpack)
platform_api.buildpack_installation.update(name, updates: [{buildpack: @buildpack}])
@app_is_setup = true
self
end
Expand Down Expand Up @@ -162,7 +173,7 @@ def deploy(&block)
in_directory do
self.setup!
self.push_with_retry!
block.call(self, heroku, output) if block_given?
block.call(self, platform_api, output) if block_given?
end
ensure
self.teardown!
Expand Down Expand Up @@ -201,6 +212,7 @@ def api_key
end

def heroku
raise "Not supported, use `platform_api` instead."
@heroku ||= Heroku::API.new(api_key: api_key)
end

Expand Down Expand Up @@ -253,7 +265,8 @@ def delete_pipeline(pipeline_id)
end

def platform_api
@platform_api ||= PlatformAPI.connect_oauth(api_key)
# We have to not use a cache due to https://github.com/heroku/platform-api/issues/73
@platform_api ||= PlatformAPI.connect_oauth(api_key, cache: Moneta.new(:Null))
end

private
Expand Down
6 changes: 0 additions & 6 deletions lib/hatchet/git_app.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
module Hatchet
# used for deploying a test app to heroku via git
class GitApp < App
def setup!
super
heroku.put_config_vars(name, 'BUILDPACK_URL' => @buildpack)
self
end

def git_repo
"https://git.heroku.com/#{name}.git"
end
Expand Down
34 changes: 19 additions & 15 deletions lib/hatchet/reaper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ class Reaper
attr_accessor :apps


def initialize(heroku, regex = DEFAULT_REGEX)
@heroku = heroku
@regex = regex
def initialize(platform_api:, regex: DEFAULT_REGEX)
@platform_api = platform_api
@regex = regex
end

# Ascending order, oldest is last
def get_apps
@apps = @heroku.get_apps.body.sort_by {|app| DateTime.parse(app["created_at"]) }.reverse
@hatchet_apps = @apps.select {|app| app["name"].match(@regex) }
@apps
apps = @platform_api.app.list.sort_by { |app| DateTime.parse(app["created_at"]) }.reverse
@app_count = apps.count
@hatchet_apps = apps.select {|app| app["name"].match(@regex) }
end

def cycle(apps = get_apps)
def cycle
get_apps
if over_limit?
if @hatchet_apps.count > 1
destroy_oldest
Expand All @@ -37,28 +38,31 @@ def cycle(apps = get_apps)
end

def destroy_oldest
oldest_name = @hatchet_apps.pop["name"]
destroy_by_name(oldest_name, "Hatchet app limit: #{HATCHET_APP_LIMT}")
rescue Heroku::API::Errors::NotFound
oldest = @hatchet_apps.pop
destroy_by_id(name: oldest["name"], id: oldest["id"], details: "Hatchet app limit: #{HATCHET_APP_LIMT}")
rescue Heroku::API::Errors::NotFound, Excon::Error::NotFound => e
puts "Error while destroying an app, maybe it's already deleted?"
puts oldest.inspect
puts e.inspect
# app already deleted, cycle will catch if there's still too many
end

def destroy_all
get_apps
@hatchet_apps.each do |app|
destroy_by_name(app["name"])
destroy_by_id(name: app["name"], id: app["id"])
end
end

def destroy_by_name(name, details="")
puts "Destroying #{name.inspect}. #{details}"
@heroku.delete_app(name)
def destroy_by_id(name:, id:, details: "")
puts "Destroying #{name.inspect}: #{id}. #{details}"
@platform_api.app.delete(id)
end

private

def over_limit?
@apps.count > HEROKU_APP_LIMIT || @hatchet_apps.count > HATCHET_APP_LIMT
@app_count > HEROKU_APP_LIMIT || @hatchet_apps.count > HATCHET_APP_LIMT
end
end
end
4 changes: 0 additions & 4 deletions test/hatchet/allow_failure_anvil_test.rb

This file was deleted.

3 changes: 1 addition & 2 deletions test/hatchet/allow_failure_git_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'test_helper'

class AllowFailureGitTest < Test::Unit::TestCase

class AllowFailureGitTest < Minitest::Test
def test_allowed_failure
Hatchet::GitApp.new("no_lockfile", allow_failure: true).deploy do |app|
refute app.deployed?
Expand Down
7 changes: 4 additions & 3 deletions test/hatchet/app_test.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
require 'test_helper'

class AppTest < Test::Unit::TestCase
class AppTest < Minitest::Test
def test_create_app_with_stack
app = Hatchet::App.new("rails3_mri_193", stack: "cedar-14")
stack = "heroku-16"
app = Hatchet::App.new("default_ruby", stack: stack)
app.create_app
assert_equal 'cedar-14', app.heroku.get_app(app.name).body["stack"]
assert_equal stack, app.platform_api.app.info(app.name)["build_stack"]["name"]
end
end
2 changes: 1 addition & 1 deletion test/hatchet/ci_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'test_helper'


class CITest < Test::Unit::TestCase
class CITest < Minitest::Test

def test_ci_create_app_with_stack
Hatchet::GitApp.new("rails5_ruby_schema_format").run_ci do |test_run|
Expand Down
2 changes: 1 addition & 1 deletion test/hatchet/config_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'test_helper'

class ConfigTest < Test::Unit::TestCase
class ConfigTest < Minitest::Test

def setup
@config = Hatchet::Config.new
Expand Down
2 changes: 1 addition & 1 deletion test/hatchet/edit_repo_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'test_helper'

class EditRepoTest < Test::Unit::TestCase
class EditRepoTest < Minitest::Test
def test_can_deploy_git_app
Hatchet::GitApp.new("default_ruby").in_directory do |app|
msg = `touch foo`
Expand Down
8 changes: 4 additions & 4 deletions test/hatchet/git_test.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
require 'test_helper'

class GitAppTest < Test::Unit::TestCase
class GitAppTest < Minitest::Test
def test_can_deploy_git_app
Hatchet::GitApp.new("rails3_mri_193", buildpack: "https://github.com/heroku/heroku-buildpack-ruby.git").deploy do |app|
Hatchet::GitApp.new("rails5_ruby_schema_format").deploy do |app|
assert true
assert_match '1.9.3', app.run("ruby -v")
assert_match '2.4.1', app.run("ruby -v")

app.run("bash") do |cmd|
# cmd.run("cd public/assets")
cmd.run("ls public/assets") {|r| assert_match "application.css", r}
cmd.run("ls public/") {|r| assert_match("assets", r) }
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/hatchet/heroku_api_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'test_helper'

class HerokuApiTest < Test::Unit::TestCase
class HerokuApiTest < Minitest::Test

def test_config_vars
runner = Hatchet::Runner.new("no_lockfile").setup!
Expand Down
2 changes: 1 addition & 1 deletion test/hatchet/labs_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'test_helper'

class LabsTest < Test::Unit::TestCase
class LabsTest < Minitest::Test
def setup
@buildpack_path = File.expand_path 'test/fixtures/buildpacks/heroku-buildpack-ruby'
end
Expand Down
5 changes: 2 additions & 3 deletions test/hatchet/multi_cmd_runner_test.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
require 'test_helper'

class MultiCmdRunnerTest < Test::Unit::TestCase
class MultiCmdRunnerTest < Minitest::Test
# slow but needed, there are ghosts in the machine
# by running common command multiple times we can find them
def test_multi_repl_commands
Hatchet::GitApp.new("default_ruby").deploy do |app|
app.add_database

assert_raise ReplRunner::UnregisteredCommand do
assert_raise(ReplRunner::UnregisteredCommand) do
app.run("ls", 2) do |ls| # will return right away, should raise error
ls.run("cat")
end
Expand Down
31 changes: 0 additions & 31 deletions test/hatchet/reaper_test.rb

This file was deleted.

14 changes: 12 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'hatchet'

require 'bundler'
Bundler.require

require 'test/unit'
# require 'test/unit'

require 'minitest/autorun'
require "mocha/setup"

def assert_tests_run
# Not needed if you're using the most recent version
if defined?(MiniTest::Unit::TestCase)
Minitest::Test = MiniTest::Unit::TestCase
end

def assert_raise(*args, &block)
assert_raises(*args, &block)
end


ENV['HATCHET_BUILDPACK_BRANCH'] = "master"