From 12bba418d1afcbee7521f2314e2e74699d2d983b Mon Sep 17 00:00:00 2001 From: Spencer McElmurry Date: Thu, 12 Jul 2018 09:40:22 -0700 Subject: [PATCH] (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 --- lib/vmpooler/api/v1.rb | 3 +++ spec/integration/api/v1/vm_spec.rb | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/vmpooler/api/v1.rb b/lib/vmpooler/api/v1.rb index 29d52a6..9915a60 100644 --- a/lib/vmpooler/api/v1.rb +++ b/lib/vmpooler/api/v1.rb @@ -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' diff --git a/spec/integration/api/v1/vm_spec.rb b/spec/integration/api/v1/vm_spec.rb index 387ecd0..5399370 100644 --- a/spec/integration/api/v1/vm_spec.rb +++ b/spec/integration/api/v1/vm_spec.rb @@ -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'