From d602057cf22607ee44fe3160a7d54e3512641972 Mon Sep 17 00:00:00 2001 From: Spencer McElmurry Date: Tue, 10 Jul 2018 11:46:57 -0700 Subject: [PATCH] Remove abs eval from GET, rework spec tests to check each field. This allows us to account for "flakiness" of the remaining return. --- lib/vmpooler/api/v1.rb | 2 +- spec/integration/api/v1/vm_spec.rb | 24 ++++++++++-------------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/lib/vmpooler/api/v1.rb b/lib/vmpooler/api/v1.rb index 54f3780..e7f6729 100644 --- a/lib/vmpooler/api/v1.rb +++ b/lib/vmpooler/api/v1.rb @@ -645,7 +645,7 @@ 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).abs + 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']) result[params[:hostname]]['end_time'] = Time.parse(rdata['checkout']) + rdata['lifetime'].to_i*60*60 result[params[:hostname]]['state'] = 'running' diff --git a/spec/integration/api/v1/vm_spec.rb b/spec/integration/api/v1/vm_spec.rb index 059e96e..8134cba 100644 --- a/spec/integration/api/v1/vm_spec.rb +++ b/spec/integration/api/v1/vm_spec.rb @@ -44,20 +44,16 @@ describe Vmpooler::API::V1 do create_running_vm 'pool1', 'abcdefghijklmnop' get "#{prefix}/vm/abcdefghijklmnop" expect_json(ok = true, http = 200) - expected = { - ok: true, - abcdefghijklmnop: { - template: "pool1", - lifetime: 0, - running: 0.0, - remaining: 0.0, - start_time: "#{current_time}", - end_time: "#{current_time}", - state: "running", - ip: "" - } - } - expect(last_response.body).to match(JSON.pretty_generate(expected)) + 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}") + expect(response_body["end_time"]).to eq("#{current_time}") + expect(response_body["state"]).to eq("running") + expect(response_body["ip"]).to eq("") end end