forked from cloudfoundry/cloud_controller_ng
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add process readiness changed controller and event (cloudfoundry#3554)
Signed-off-by: Geoff Franks <[email protected]> Co-authored-by: Geoff Franks <[email protected]>
- Loading branch information
1 parent
dd95c36
commit 06ca46a
Showing
7 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
app/controllers/internal/app_readiness_changed_controller.rb
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,38 @@ | ||
require 'sinatra' | ||
require 'controllers/base/base_controller' | ||
|
||
module VCAP::CloudController | ||
class AppReadinessChangedController < RestController::BaseController | ||
# Endpoint does its own (non-standard) auth | ||
allow_unauthenticated_access | ||
|
||
post '/internal/v4/apps/:process_guid/readiness_changed', :readiness_changed | ||
def readiness_changed(process_guid) | ||
payload = readiness_request | ||
|
||
app_guid = Diego::ProcessGuid.cc_process_guid(process_guid) | ||
|
||
process = ProcessModel.find(guid: app_guid) | ||
raise CloudController::Errors::NotFound.new_from_details('ProcessNotFound', app_guid) unless process | ||
|
||
payload['version'] = Diego::ProcessGuid.cc_process_version(process_guid) | ||
|
||
Repositories::ProcessEventRepository.record_readiness_changed(process, payload) | ||
end | ||
|
||
private | ||
|
||
def readiness_request | ||
readiness = {} | ||
begin | ||
payload = body.read | ||
readiness = MultiJson.load(payload) | ||
rescue MultiJson::ParseError => e | ||
logger.error('diego.app_readiness_changed.parse-error', payload: payload, error: e.to_s) | ||
raise CloudController::Errors::ApiError.new_from_details('MessageParseError', payload) | ||
end | ||
|
||
readiness | ||
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
80 changes: 80 additions & 0 deletions
80
spec/unit/controllers/internal/app_readiness_changed_controller_spec.rb
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,80 @@ | ||
require 'spec_helper' | ||
|
||
## NOTICE: Prefer request specs over controller specs as per ADR #0003 ## | ||
|
||
module VCAP::CloudController | ||
RSpec.describe AppReadinessChangedController do | ||
describe 'POST /internal/v4/apps/:process_guid/readiness_changed' do | ||
let(:diego_process) { ProcessModelFactory.make(state: 'STARTED', diego: true) } | ||
let(:process_guid) { Diego::ProcessGuid.from(diego_process.guid, 'some-version-guid') } | ||
let(:url) { "/internal/v4/apps/#{process_guid}/readiness_changed" } | ||
let(:ready) { true } | ||
|
||
let(:readiness_changed_request) do | ||
{ | ||
'instance' => Sham.guid, | ||
'index' => 3, | ||
'ready' => ready | ||
} | ||
end | ||
|
||
describe 'validation' do | ||
context 'when sending invalid json' do | ||
it 'fails with a 400' do | ||
post url, 'this is not json' | ||
|
||
expect(last_response.status).to eq(400) | ||
expect(last_response.body).to match(/MessageParseError/) | ||
end | ||
end | ||
end | ||
|
||
context 'when the app is ready' do | ||
it 'audits the app readiness changed event' do | ||
post url, MultiJson.dump(readiness_changed_request) | ||
expect(last_response.status).to eq(200) | ||
|
||
app_event = Event.find(actee: diego_process.guid, actor_type: 'process') | ||
|
||
expect(app_event).to be | ||
expect(app_event.space).to eq(diego_process.space) | ||
expect(app_event.type).to eq('audit.app.process.ready') | ||
expect(app_event.actor_type).to eq('process') | ||
expect(app_event.actor).to eq(diego_process.guid) | ||
expect(app_event.metadata['instance']).to eq(readiness_changed_request['instance']) | ||
expect(app_event.metadata['index']).to eq(readiness_changed_request['index']) | ||
end | ||
end | ||
|
||
context 'when the app is not ready' do | ||
let(:ready) { false } | ||
|
||
it 'audits the app readiness changed event' do | ||
post url, MultiJson.dump(readiness_changed_request) | ||
expect(last_response.status).to eq(200) | ||
|
||
app_event = Event.find(actee: diego_process.guid, actor_type: 'process') | ||
|
||
expect(app_event).to be | ||
expect(app_event.space).to eq(diego_process.space) | ||
expect(app_event.type).to eq('audit.app.process.not-ready') | ||
expect(app_event.actor_type).to eq('process') | ||
expect(app_event.actor).to eq(diego_process.guid) | ||
expect(app_event.metadata['instance']).to eq(readiness_changed_request['instance']) | ||
expect(app_event.metadata['index']).to eq(readiness_changed_request['index']) | ||
end | ||
end | ||
|
||
context 'when the app no longer exists' do | ||
before { diego_process.delete } | ||
|
||
it 'fails with a 404' do | ||
post url, MultiJson.dump(readiness_changed_request) | ||
|
||
expect(last_response.status).to eq(404) | ||
expect(last_response.body).to match(/ProcessNotFound/) | ||
end | ||
end | ||
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