This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jeff Dickey
committed
Mar 21, 2015
1 parent
9f8dc3d
commit 13d59bc
Showing
4 changed files
with
335 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module Heroku | ||
class API | ||
def get_releases_v3(app, range=nil) | ||
headers = { 'Accept' => 'application/vnd.heroku+json; version=3' } | ||
headers.merge!('Range' => range) if range | ||
request( | ||
:expects => [ 200, 206 ], | ||
:headers => headers, | ||
:method => :get, | ||
:path => "/apps/#{app}/releases" | ||
) | ||
end | ||
|
||
def post_release_v3(app, slug_id, opts={}) | ||
headers = { | ||
'Accept' => 'application/vnd.heroku+json; version=3', | ||
'Content-Type' => 'application/json' | ||
} | ||
headers.merge!('Heroku-Deploy-Type' => opts[:deploy_type]) if opts[:deploy_type] | ||
headers.merge!('Heroku-Deploy-Source' => opts[:deploy_source]) if opts[:deploy_source] | ||
|
||
body = { 'slug' => slug_id } | ||
body.merge!('description' => opts[:description]) if opts[:description] | ||
|
||
request( | ||
:expects => 201, | ||
:headers => headers, | ||
:method => :post, | ||
:path => "/apps/#{app}/releases", | ||
:body => Heroku::Helpers.json_encode(body) | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
require "heroku/api/releases_v3" | ||
require "spec_helper" | ||
require "heroku/command/fork" | ||
|
||
module Heroku::Command | ||
|
||
describe Fork do | ||
|
||
before(:each) do | ||
stub_core | ||
api.post_app("name" => "example", "stack" => "cedar") | ||
end | ||
|
||
after(:each) do | ||
api.delete_app("example") | ||
begin | ||
api.delete_app("example-fork") | ||
rescue Heroku::API::Errors::NotFound | ||
end | ||
end | ||
|
||
context "successfully" do | ||
|
||
before(:each) do | ||
Excon.stub({ :method => :get, | ||
:path => "/apps/example/releases" }, | ||
{ :body => [{"slug" => {"id" => "SLUG_ID"}}], | ||
:status => 206}) | ||
|
||
Excon.stub({ :method => :post, | ||
:path => "/apps/example-fork/releases"}, | ||
{ :status => 201}) | ||
end | ||
|
||
after(:each) do | ||
Excon.stubs.shift | ||
Excon.stubs.shift | ||
end | ||
|
||
it "forks an app" do | ||
stderr, stdout = execute("fork example-fork") | ||
expect(stderr).to eq("") | ||
expect(stdout).to eq <<-STDOUT | ||
Creating fork example-fork... done | ||
Copying slug... done | ||
Copying config vars... done | ||
Fork complete, view it at http://example-fork.herokuapp.com/ | ||
STDOUT | ||
end | ||
|
||
it "copies slug" do | ||
from_info = api.get_app("example").body | ||
expect_any_instance_of(Heroku::API).to receive(:get_releases_v3).with("example", "version ..; order=desc,max=1;").and_call_original | ||
expect_any_instance_of(Heroku::API).to receive(:post_release_v3).with("example-fork", | ||
"SLUG_ID", | ||
:description => "Forked from example", | ||
:deploy_type => "fork", | ||
:deploy_source => from_info["id"]).and_call_original | ||
execute("fork example-fork") | ||
end | ||
|
||
it "copies config vars" do | ||
config_vars = { | ||
"SECRET" => "imasecret", | ||
"FOO" => "bar", | ||
"LANG_ENV" => "production" | ||
} | ||
api.put_config_vars("example", config_vars) | ||
execute("fork example-fork") | ||
expect(api.get_config_vars("example-fork").body).to eq(config_vars) | ||
end | ||
|
||
it "re-provisions add-ons" do | ||
api.post_addon("example", "heroku-postgresql:hobby-dev") | ||
execute("fork example-fork") | ||
expect(api.get_addons("example-fork").body[0]["name"]).to eq("heroku-postgresql:hobby-dev") | ||
end | ||
end | ||
|
||
describe "error handling" do | ||
it "fails if no source release exists" do | ||
begin | ||
Excon.stub({ :method => :get, | ||
:path => "/apps/example/releases" }, | ||
{ :body => [], | ||
:status => 206}) | ||
execute("fork example-fork") | ||
raise | ||
rescue Heroku::Command::CommandFailed => e | ||
expect(e.message).to eq("No releases on example") | ||
ensure | ||
Excon.stubs.shift | ||
end | ||
end | ||
|
||
it "fails if source slug does not exist" do | ||
begin | ||
Excon.stub({ :method => :get, | ||
:path => "/apps/example/releases" }, | ||
{ :body => [{"slug" => nil}], | ||
:status => 206}) | ||
execute("fork example-fork") | ||
raise | ||
rescue Heroku::Command::CommandFailed => e | ||
expect(e.message).to eq("No slug on example") | ||
ensure | ||
Excon.stubs.shift | ||
end | ||
end | ||
|
||
it "doesn't attempt to fork to the same app" do | ||
expect do | ||
execute("fork example") | ||
end.to raise_error(Heroku::Command::CommandFailed, /same app/) | ||
end | ||
|
||
it "confirms before deleting the app" do | ||
Excon.stub({:path => "/apps/example/releases"}, {:status => 500}) | ||
begin | ||
execute("fork example-fork") | ||
rescue Heroku::API::Errors::ErrorWithResponse | ||
ensure | ||
Excon.stubs.shift | ||
end | ||
expect(api.get_apps.body.map { |app| app["name"] }).to eq( | ||
%w( example example-fork ) | ||
) | ||
end | ||
|
||
it "deletes fork app on error, before re-raising" do | ||
stub(Heroku::Command).confirm_command.returns(true) | ||
expect(api.get_apps.body.map { |app| app["name"] }).to eq(%w( example )) | ||
end | ||
end | ||
end | ||
end |