(POOLER-81) Add time remaining information (#280)

* (POOLER-81) Add time_remaining information

Before, the only time calculation displayed for a given VM was the
lifetime parameter. Added the remaining parameter which will
display time until the VM is destroyed as a float.

Additionally, start_time and end_time were added to api to return
as UTC based times (e.g. 2018-07-10 11:01:03 -0700).

* Remove abs eval from GET, rework spec tests to check each field.
This allows us to account for "flakiness" of the remaining return.

* Change datetime to RFC3339 for start_time and end_time
This commit is contained in:
Spencer McElmurry 2018-07-12 09:40:22 -07:00 committed by mattkirby
parent 84065dd059
commit 12bba418d1
2 changed files with 21 additions and 0 deletions

View file

@ -645,6 +645,9 @@ module Vmpooler
result[params[:hostname]]['state'] = 'destroyed'
elsif rdata['checkout']
result[params[:hostname]]['running'] = ((Time.now - Time.parse(rdata['checkout'])) / 60 / 60).round(2)
result[params[:hostname]]['remaining'] = ((Time.parse(rdata['checkout']) + rdata['lifetime'].to_i*60*60 - Time.now) / 60 / 60).round(2)
result[params[:hostname]]['start_time'] = Time.parse(rdata['checkout']).to_datetime.rfc3339
result[params[:hostname]]['end_time'] = (Time.parse(rdata['checkout']) + rdata['lifetime'].to_i*60*60).to_datetime.rfc3339
result[params[:hostname]]['state'] = 'running'
elsif rdata['check']
result[params[:hostname]]['state'] = 'ready'

View file

@ -39,6 +39,24 @@ describe Vmpooler::API::V1 do
create_token('abcdefghijklmnopqrstuvwxyz012345', 'jdoe', current_time)
end
describe 'GET /vm/:hostname' do
it 'returns correct information on a running vm' do
create_running_vm 'pool1', 'abcdefghijklmnop'
get "#{prefix}/vm/abcdefghijklmnop"
expect_json(ok = true, http = 200)
response_body = (JSON.parse(last_response.body)["abcdefghijklmnop"])
expect(response_body["template"]).to eq("pool1")
expect(response_body["lifetime"]).to eq(0)
expect(response_body["running"]).to be >= 0
expect(response_body["remaining"]).to be <= 0
expect(response_body["start_time"]).to eq(current_time.to_datetime.rfc3339)
expect(response_body["end_time"]).to eq(current_time.to_datetime.rfc3339)
expect(response_body["state"]).to eq("running")
expect(response_body["ip"]).to eq("")
end
end
describe 'POST /vm' do
it 'returns a single VM' do
create_ready_vm 'pool1', 'abcdefghijklmnop'