Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Commit

Permalink
Only create org and space in v2 build
Browse files Browse the repository at this point in the history
  • Loading branch information
mariash committed Feb 15, 2013
1 parent 5794111 commit a3833d2
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 45 deletions.
23 changes: 20 additions & 3 deletions micro/lib/micro/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def client
@client ||= CFoundry::Client.new(target)
end

def v2?
client.is_a?(CFoundry::V2::Client)
end

def target
@target ||= micro_config.api_host
end
Expand All @@ -41,10 +45,12 @@ def space
end

def find_space
return unless v2?
client.space_by_name(SPACE_NAME)
end

def create_space
return unless v2?
space = client.space
space.name = SPACE_NAME
space.organization = organization
Expand All @@ -59,10 +65,12 @@ def organization
end

def find_org
return unless v2?
client.organization_by_name(ORG_NAME)
end

def create_org
return unless v2?
org = client.organization
org.name = ORG_NAME
org.create!
Expand All @@ -72,16 +80,19 @@ def create_org
end

def domain
return unless v2?
@domain ||= space.domain_by_name(micro_config.subdomain)
end

def find_route(host_name)
return unless v2?
client.routes_by_host(host_name, :depth => 0).find do |r|
r.domain = domain
end
end

def create_route(host_name)
return unless v2?
route = client.route
route.host = host_name
route.space = space
Expand All @@ -97,7 +108,7 @@ def push_app(manifest)
unless app
app = client.app
app.name = manifest[:name]
app.space = space
app.space = space if v2?
app.total_instances = 1

app.framework = client.framework_by_name(manifest[:framework])
Expand All @@ -108,8 +119,13 @@ def push_app(manifest)
end

logger.info('Mapping url')
new_route = find_route(manifest[:name]) || create_route(manifest[:name])
app.add_route(new_route)
if v2?
new_route = find_route(manifest[:name]) || create_route(manifest[:name])
app.add_route(new_route)
else
app.urls << "#{manifest[:name]}.#{micro_config.subdomain}"
app.update!
end

logger.info('Uploading docs app')
app.upload(manifest[:path])
Expand All @@ -119,6 +135,7 @@ def push_app(manifest)
end

def create_org_and_space
return unless v2?
login
create_org
create_space
Expand Down
118 changes: 76 additions & 42 deletions micro/spec/micro/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@

let(:cfoundry_client) do
client = mock(:cfoundry_client)
client.stub(:is_a?).with(CFoundry::V2::Client) { v2? }
client.stub(:login) { 'some_token' }
client.stub(:space) { space }
client.stub(:organization) { organization }
client.stub(:space) { v2? ? space : fail("v2 only") }
client.stub(:organization) { v2? ? organization : fail("v2 only") }
client.stub(:route) { route }
client.stub(:app) { app }
client.stub(:space_by_name) { space }
client.stub(:organization_by_name) { organization }
client.stub(:space_by_name) { v2? ? space : fail("v2 only") }
client.stub(:organization_by_name) { v2? ? organization : fail("v2 only") }
client.stub(:framework_by_name) { |name| name }
client.stub(:runtime_by_name) { |name| name }
client.stub(:app_by_name) { nil }
client.stub(:routes_by_host) { [route] }
client.stub(:routes_by_host) { v2? ? [route] : fail("v2 only") }
client
end

Expand All @@ -24,13 +25,13 @@
space.stub(:name=)
space.stub(:domain_by_name)
space.stub(:organization=)
space.stub(:create!) { true }
space.stub(:create!) { fail("v2 only") unless v2? }
space
end

let(:organization) do
org = mock(:org)
org.stub(:create!) { true }
org.stub(:create!) { fail("v2 only") unless v2? }
org.stub(:name=)
org
end
Expand All @@ -40,7 +41,7 @@
route.stub(:host=)
route.stub(:space=)
route.stub(:domain=)
route.stub(:create!) { true }
route.stub(:create!) { fail("v2 only") unless v2? }
route
end

Expand All @@ -52,7 +53,9 @@
app.stub(:framework=)
app.stub(:runtime=)
app.stub(:command=)
app.stub(:add_route)
app.stub(:add_route) { fail("v2 only") unless v2? }
app.stub(:urls) { v2? ? fail("v2 only") : [] }
app.stub(:update!)
app.stub(:create!) { true }
app.stub(:upload) { true }
app.stub(:start!) { true }
Expand All @@ -66,6 +69,21 @@
config
end

let(:v2?) { true }

let(:app_name) { 'app_name' }
let(:framework) { 'sinatra' }
let(:runtime) { 'ruby19' }
let(:upload_path) { '/some/path' }
let(:manifest) do
{
:path => upload_path,
:name => app_name,
:framework => framework,
:runtime => runtime
}
end

before do
CFoundry::Client.stub(:new) { cfoundry_client }
VCAP::Micro::ConfigFile.stub(:new) { config }
Expand All @@ -82,46 +100,62 @@
subject.login
end

it 'creates organization' do
cfoundry_client.organization.should_receive(:create!)
subject.login
subject.create_org
end
context 'when v2' do
it 'creates organization' do
cfoundry_client.organization.should_receive(:create!)
subject.login
subject.create_org
end

it 'creates space' do
cfoundry_client.space.should_receive(:create!)
subject.login
subject.create_space
it 'creates space' do
cfoundry_client.space.should_receive(:create!)
subject.login
subject.create_space
end

it 'pushes an app according to manifest' do
cfoundry_client.route.should_receive(:host=).with(app_name)
cfoundry_client.route.should_receive(:create!)
cfoundry_client.app.should_receive(:name=).with(app_name)
cfoundry_client.app.should_receive(:framework=).with(framework)
cfoundry_client.app.should_receive(:runtime=).with(runtime)
cfoundry_client.app.should_receive(:create!)
cfoundry_client.app.should_receive(:add_route)
cfoundry_client.app.should_receive(:upload).with(upload_path)
cfoundry_client.app.should_receive(:start!)

subject.push_app(manifest)
end
end

let(:app_name) { 'app_name' }
let(:framework) { 'sinatra' }
let(:runtime) { 'ruby19' }
let(:upload_path) { '/some/path' }
context 'when v1' do
let(:v2?) { false }

shared_examples 'app push' do
it 'pushes an app' do
it 'does not create space' do
cfoundry_client.should_not_receive(:space)
subject.create_space
end

it 'does not create org' do
cfoundry_client.should_not_receive(:organization)
subject.create_org
end
end

it 'pushes an app according to manifest' do
cfoundry_client.route.should_receive(:host=).with(app_name)
cfoundry_client.route.should_receive(:create!)
cfoundry_client.app.should_receive(:name=).with(app_name)
cfoundry_client.app.should_receive(:framework=).with(framework)
cfoundry_client.app.should_receive(:runtime=).with(runtime)
cfoundry_client.app.should_receive(:create!)
cfoundry_client.app.should_receive(:add_route)
cfoundry_client.app.should_receive(:upload).with(upload_path)
cfoundry_client.app.should_receive(:start!)

subject.push_app({
:path => upload_path,
:name => app_name,
:framework => framework,
:runtime => runtime
})
it 'adds urls when creating app' do
cfoundry_client.app.should_receive(:urls)
cfoundry_client.app.should_receive(:update!)
subject.push_app(manifest)
end

it 'does not add route when creating app' do
cfoundry_client.app.should_not_receive(:add_route)
subject.push_app(manifest)
end

it 'does not add space to app when creating app' do
cfoundry_client.app.should_not_receive(:space=)
subject.push_app(manifest)
end
end

it 'pushes docs app' do
Expand Down

0 comments on commit a3833d2

Please sign in to comment.