vmpooler/spec/helpers.rb
Rick Bradley 30dc060731 [QENG-4181] Add per-pool stats to /status API
Prior to this the only per-pool statistics that could be extracted from the API
were a list of empty pools in the "status" section of the returned results of
the `/status` endpoint.

This adds a new "pools" section to the '/status' results which lists, for each
pool, the following results:

 - The number of ready vms in the pool
 - The number of running vms in the pool
 - The number of pending vms in the pool
 - The maximum size of the pool (as specified in the vmpooler configuration)

Example:

```
{
  "boot": {
  "duration": {
    "average": 163.6,
    "min": 65.49,
    "max": 830.07,
    "total": 247744.71000000002
  },
  "count": {
    "total": 1514
  }
  # ...
  "pools": {
    "pool1": {
      "ready":   5,
      "running": 2,
      "pending": 1,
      "max":     15
    },
    "pool2": {
      "ready":   0,
      "running": 10,
      "pending": 0,
      "max:      10
    }
  }
}
```

This includes spec coverage for this change (we could use more specs on `/status` in general); as well as a couple of general spec improvements.
2016-09-09 17:00:47 -05:00

85 lines
2.3 KiB
Ruby

def redis
unless @redis
@redis = Redis.new
@redis.select(15) # let's use the highest numbered database available in a default install
end
@redis
end
def expect_json(ok = true, http = 200)
expect(last_response.header['Content-Type']).to eq('application/json')
if (ok == true) then
expect(JSON.parse(last_response.body)['ok']).to eq(true)
else
expect(JSON.parse(last_response.body)['ok']).to eq(false)
end
expect(last_response.status).to eq(http)
end
def create_token(token, user, timestamp)
redis.hset("vmpooler__token__#{token}", 'user', user)
redis.hset("vmpooler__token__#{token}", 'created', timestamp)
end
def get_token_data(token)
redis.hgetall("vmpooler__token__#{token}")
end
def token_exists?(token)
result = get_token_data
result && !result.empty?
end
def create_ready_vm(template, name, token = nil)
create_vm(name, token)
redis.sadd("vmpooler__ready__#{template}", name)
redis.hset("vmpooler__vm__#{name}", "template", template)
end
def create_running_vm(template, name, token = nil)
create_vm(name, token)
redis.sadd("vmpooler__running__#{template}", name)
redis.hset("vmpooler__vm__#{name}", "template", template)
end
def create_pending_vm(template, name, token = nil)
create_vm(name, token)
redis.sadd("vmpooler__pending__#{template}", name)
redis.hset("vmpooler__vm__#{name}", "template", template)
end
def create_vm(name, token = nil)
redis.hset("vmpooler__vm__#{name}", 'checkout', Time.now)
if token
redis.hset("vmpooler__vm__#{name}", 'token:token', token)
end
end
def fetch_vm(vm)
redis.hgetall("vmpooler__vm__#{vm}")
end
def snapshot_vm(vm, snapshot = '12345678901234567890123456789012')
redis.sadd('vmpooler__tasks__snapshot', "#{vm}:#{snapshot}")
redis.hset("vmpooler__vm__#{vm}", "snapshot:#{snapshot}", "1")
end
def has_vm_snapshot?(vm)
redis.smembers('vmpooler__tasks__snapshot').any? do |snapshot|
instance, sha = snapshot.split(':')
vm == instance
end
end
def vm_reverted_to_snapshot?(vm, snapshot = nil)
redis.smembers('vmpooler__tasks__snapshot-revert').any? do |action|
instance, sha = action.split(':')
instance == vm and (snapshot ? (sha == snapshot) : true)
end
end
def pool_has_ready_vm?(pool, vm)
!!redis.sismember('vmpooler__ready__' + pool, vm)
end