Rebasing fixed tests

This commit is contained in:
Mikker Gimenez-Peterson 2019-10-31 11:41:51 -07:00
parent de7d0fdeab
commit a77ea84092
11 changed files with 217 additions and 111 deletions

View file

@ -66,7 +66,7 @@ floaty get centos-7-x86_64=2 debian-7-x86_64 windows-10=3 --token mytokenstring
### vmfloaty dotfile
If you do not wish to continuely specify various config options with the cli, you can have a dotfile in your home directory for some defaults. For example:
If you do not wish to continually specify various config options with the cli, you can have a dotfile in your home directory for some defaults. For example:
#### Basic configuration
@ -132,9 +132,10 @@ services:
token: 'nspooler-tokenstring'
type: 'nonstandard' # <-- 'type' is necessary for any non-vmpooler service
abs:
url: 'https://abs.example.net/api/v2'
url: 'https://abs.example.net/'
token: 'abs-tokenstring'
type: 'abs' # <-- 'type' is necessary for any non-vmpooler service
```
With this configuration, you could list available OS types from nspooler like this:

View file

@ -84,6 +84,7 @@ class Vmfloaty
c.option '--url STRING', String, 'URL of pooler service'
c.action do |args, options|
verbose = options.verbose || config['verbose']
service = Service.new(options, config)
filter = args[0]

View file

@ -6,134 +6,197 @@ require 'faraday'
require 'json'
class ABS
# List active VMs in ABS
#
#
# {
# "state":"filled",
# "last_processed":"2019-10-31 20:59:33 +0000",
# "allocated_resources": [
# {
# "hostname":"h3oyntawjm7xdch.delivery.puppetlabs.net",
# "type":"centos-7.2-tmpfs-x86_64",
# "engine":"vmpooler"}
# ],
# "audit_log":{
# "2019-10-30 20:33:12 +0000":"Allocated h3oyntawjm7xdch.delivery.puppetlabs.net for job 1572467589"
# },
# "request":{
# "resources":{
# "centos-7.2-tmpfs-x86_64":1
# },
# "job": {
# "id":1572467589,
# "tags": {
# "user":"mikker",
# "url_string":"floaty://mikker/1572467589"
# },
# "user":"mikker",
# "time-received":1572467589
# }
# }
# }
#
def self.list_active(verbose, url, _token, user)
conn = Http.get_conn(verbose, url)
res = conn.get 'status/queue'
requests = JSON.parse(res.body)
requests.each do |req|
reqHash = JSON.parse(req)
next unless user == reqHash['request']['job']['user']
puts '------------------------------------'
puts "State: #{reqHash['state']}"
puts "Job ID: #{reqHash['request']['job']['id']}"
reqHash['request']['resources'].each do |vm_template, i|
puts "--VMRequest: #{vm_template}: #{i}"
end
if reqHash['state'] == 'allocated' || reqHash['state'] == 'filled'
reqHash['allocated_resources'].each do |vm_name, i|
puts "----VM: #{vm_name}: #{i}"
end
end
puts "User: #{reqHash['request']['job']['user']}"
puts ''
end
sleep(100)
end
# List available VMs in ABS
def self.list(verbose, url, os_filter = nil)
conn = Http.get_conn(verbose, url)
os_list = []
response = conn.get 'status/platforms/vmpooler'
response_body = JSON.parse(response.body)
os_list << "*** VMPOOLER Pools ***"
os_list = os_list + JSON.parse(response_body["vmpooler_platforms"])
res = conn.get 'status/platforms/vmpooler'
response = conn.get 'status/platforms/nspooler'
response_body = JSON.parse(response.body)
os_list << ""
os_list << "*** NSPOOLER Pools ***"
os_list = os_list + JSON.parse(response_body["nspooler_platforms"])
res_body = JSON.parse(res.body)
os_list << '*** VMPOOLER Pools ***'
os_list += JSON.parse(res_body['vmpooler_platforms'])
response = conn.get 'status/platforms/aws'
response_body = JSON.parse(response.body)
os_list << ""
os_list << "*** AWS Pools ***"
os_list = os_list + JSON.parse(response_body["aws_platforms"])
res = conn.get 'status/platforms/nspooler'
res_body = JSON.parse(res.body)
os_list << ''
os_list << '*** NSPOOLER Pools ***'
os_list += JSON.parse(res_body['nspooler_platforms'])
res = conn.get 'status/platforms/aws'
res_body = JSON.parse(res.body)
os_list << ''
os_list << '*** AWS Pools ***'
os_list += JSON.parse(res_body['aws_platforms'])
os_list.delete 'ok'
puts os_list
os_filter ? os_list.select { |i| i[/#{os_filter}/] } : os_list
end
# List active VMs from ABS
def self.list_active(verbose, url, token)
status = Auth.token_status(verbose, url, token)
status['reserved_hosts'] || []
end
# Retrieve an OS from ABS.
def self.retrieve(verbose, os_types, token, url, user)
#
# Contents of post must be:j
#
# {
# "resources": {
# "centos-7-i386": 1,
# "ubuntu-1404-x86_64": 2
# },
# "job": {
# "id": "12345",
# "tags": {
# "user": "jenkins",
# "jenkins_build_url": "https://jenkins/job/platform_puppet_intn-van-sys_master"
# }
# }
# }
def self.retrieve(verbose, os_type, token, url)
conn = Http.get_conn(verbose, url)
conn.headers['X-AUTH-TOKEN'] = token if token
os_string = os_type.map { |os, num| Array(os) * num }.flatten.join('+')
raise MissingParamError, 'No operating systems provided to obtain.' if os_string.empty?
saved_job_id = Time.now.to_i
response = conn.post "host/#{os_string}"
reqObj = {
:resources => os_types,
:job => {
:id => saved_job_id,
:tags => {
:user => user,
:url_string => "floaty://#{user}/#{saved_job_id}",
},
},
}
res_body = JSON.parse(response.body)
# os_string = os_type.map { |os, num| Array(os) * num }.flatten.join('+')
# raise MissingParamError, 'No operating systems provided to obtain.' if os_string.empty?
puts "Requesting VMs with job_id: #{saved_job_id}. Will retry for up to an hour."
res = conn.post 'api/v2/request', reqObj.to_json
if res_body['ok']
res_body
elsif response.status == 401
raise AuthError, "HTTP #{response.status}: The token provided could not authenticate to the pooler.\n#{res_body}"
else
raise "HTTP #{response.status}: Failed to obtain VMs from the pooler at #{url}/host/#{os_string}. #{res_body}"
i = 0
retries = 360
raise AuthError, "HTTP #{res.status}: The token provided could not authenticate to the pooler.\n#{res_body}" if res.status == 401
(1..retries).each do |i|
queue_place, res_body = check_queue(conn, saved_job_id, reqObj)
return translated(res_body) if res_body
puts "Waiting 10 seconds to check if ABS request has been filled. Queue Position: #{queue_place}... (x#{i})"
sleep(10)
end
nil
end
def self.modify(verbose, url, hostname, token, modify_hash)
raise TokenError, 'Token provided was nil; Request cannot be made to modify VM' if token.nil?
#
# We should fix the ABS API to be more like the vmpooler or nspooler api, but for now
#
def self.translated(res_body)
vmpooler_formatted_body = {}
modify_hash.each do |key, _value|
raise ModifyError, "Configured service type does not support modification of #{key}" unless %i[reason reserved_for_reason].include? key
res_body.each do |host|
if vmpooler_formatted_body[host['type']] && vmpooler_formatted_body[host['type']]['hostname'].class == Array
vmpooler_formatted_body[host['type']]['hostname'] << host['hostname']
else
vmpooler_formatted_body[host['type']] = { 'hostname' => [host['hostname']] }
end
end
vmpooler_formatted_body['ok'] = true
if modify_hash[:reason]
# "reason" is easier to type than "reserved_for_reason", but nspooler needs the latter
modify_hash[:reserved_for_reason] = modify_hash.delete :reason
end
conn = Http.get_conn(verbose, url)
conn.headers['X-AUTH-TOKEN'] = token
response = conn.put do |req|
req.url "host/#{hostname}"
req.body = modify_hash.to_json
end
response.body.empty? ? {} : JSON.parse(response.body)
vmpooler_formatted_body
end
def self.disk(_verbose, _url, _hostname, _token, _disk)
raise ModifyError, 'Configured service type does not support modification of disk space'
end
def self.check_queue(conn, job_id, reqObj)
queue_info_res = conn.get "/status/queue/info/#{job_id}"
queue_info = JSON.parse(queue_info_res.body)
def self.snapshot(_verbose, _url, _hostname, _token)
raise ModifyError, 'Configured service type does not support snapshots'
end
res = conn.post 'api/v2/request', reqObj.to_json
def self.revert(_verbose, _url, _hostname, _token, _snapshot_sha)
raise ModifyError, 'Configured service type does not support snapshots'
end
def self.delete(verbose, url, hosts, token)
raise TokenError, 'Token provided was nil; Request cannot be made to delete VM' if token.nil?
conn = Http.get_conn(verbose, url)
conn.headers['X-AUTH-TOKEN'] = token if token
response_body = {}
hosts = hosts.split(',') unless hosts.is_a? Array
hosts.each do |host|
response = conn.delete "host/#{host}"
res_body = JSON.parse(response.body)
response_body[host] = res_body
unless res.body.empty?
res_body = JSON.parse(res.body)
return queue_info['queue_place'], res_body
end
response_body
[queue_info['queue_place'], nil]
end
def self.status(verbose, url)
conn = Http.get_conn(verbose, url)
response = conn.get '/status'
JSON.parse(response.body)
res = conn.get '/status'
JSON.parse(res.body)
end
def self.summary(verbose, url)
conn = Http.get_conn(verbose, url)
response = conn.get '/summary'
JSON.parse(response.body)
res = conn.get '/summary'
JSON.parse(res.body)
end
def self.query(verbose, url, hostname)
conn = Http.get_conn(verbose, url)
response = conn.get "host/#{hostname}"
JSON.parse(response.body)
res = conn.get "host/#{hostname}"
JSON.parse(res.body)
end
end

View file

@ -17,12 +17,12 @@ class NonstandardPooler
os_filter ? os_list.select { |i| i[/#{os_filter}/] } : os_list
end
def self.list_active(verbose, url, token)
def self.list_active(verbose, url, token, _user)
status = Auth.token_status(verbose, url, token)
status['reserved_hosts'] || []
end
def self.retrieve(verbose, os_type, token, url)
def self.retrieve(verbose, os_type, token, url, _user)
conn = Http.get_conn(verbose, url)
conn.headers['X-AUTH-TOKEN'] = token if token

View file

@ -21,7 +21,7 @@ class Pooler
hosts
end
def self.list_active(verbose, url, token)
def self.list_active(verbose, url, token, _user)
status = Auth.token_status(verbose, url, token)
vms = []
vms = status[token]['vms']['running'] if status[token] && status[token]['vms']

View file

@ -36,7 +36,7 @@ class Service
def user
unless @config['user']
puts 'Enter your pooler service username:'
puts "Enter your #{@config['url']} service username:"
@config['user'] = STDIN.gets.chomp
end
@config['user']
@ -52,13 +52,13 @@ class Service
def get_new_token(verbose)
username = user
pass = Commander::UI.password "Enter your #{@config["url"]} service password:", '*'
pass = Commander::UI.password "Enter your #{@config['url']} service password:", '*'
Auth.get_token(verbose, url, username, pass)
end
def delete_token(verbose, token_value = @config['token'])
username = user
pass = Commander::UI.password 'Enter your pooler service password:', '*'
pass = Commander::UI.password "Enter your #{@config['url']} service password:", '*'
Auth.delete_token(verbose, url, username, pass, token_value)
end
@ -72,13 +72,13 @@ class Service
end
def list_active(verbose)
@service_object.list_active verbose, url, token
@service_object.list_active verbose, url, token, user
end
def retrieve(verbose, os_types, use_token = true)
puts 'Requesting a vm without a token...' unless use_token
token_value = use_token ? token : nil
@service_object.retrieve verbose, os_types, token_value, url
@service_object.retrieve verbose, os_types, token_value, url, user
end
def ssh(verbose, host_os, use_token = true)

View file

@ -31,6 +31,13 @@ class Utils
# }
# }
# abs pooler response body example when `floaty get` arguments are :
# {
# "hostname"=>"thin-soutane.delivery.puppetlabs.net",
# "type"=>"centos-7.2-tmpfs-x86_64",
# "engine"=>"vmpooler"
# }
raise ArgumentError, "Bad GET response passed to format_hosts: #{response_body.to_json}" unless response_body.delete('ok')
# vmpooler reports the domain separately from the hostname

View file

@ -5,7 +5,7 @@ require_relative '../../../lib/vmfloaty/auth'
describe Pooler do
before :each do
@abs_url = 'https://abs.example.com'
@abs_url = 'https://abs.example.com/api/v2'
end
describe '#get_token' do
@ -14,9 +14,8 @@ describe Pooler do
@token = 'utpg2i2xswor6h8ttjhu3d47z53yy47y'
end
it 'returns a token from vmpooler' do
it 'returns a token from abs' do
stub_request(:post, 'https://first.last:password@abs.example.com/api/v2/token')
.with(:headers => { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Length' => '0', 'User-Agent' => 'Faraday v0.9.2' })
.to_return(:status => 200, :body => @get_token_response, :headers => {})
token = Auth.get_token(false, @abs_url, 'first.last', 'password')
@ -25,7 +24,6 @@ describe Pooler do
it 'raises a token error if something goes wrong' do
stub_request(:post, 'https://first.last:password@abs.example.com/api/v2/token')
.with(:headers => { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Length' => '0', 'User-Agent' => 'Faraday v0.9.2' })
.to_return(:status => 500, :body => '{"ok":false}', :headers => {})
expect { Auth.get_token(false, @abs_url, 'first.last', 'password') }.to raise_error(TokenError)
@ -40,7 +38,6 @@ describe Pooler do
it 'deletes the specified token' do
stub_request(:delete, 'https://first.last:password@abs.example.com/api/v2/token/utpg2i2xswor6h8ttjhu3d47z53yy47y')
.with(:headers => { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Faraday v0.9.2' })
.to_return(:status => 200, :body => @delete_token_response, :headers => {})
expect(Auth.delete_token(false, @abs_url, 'first.last', 'password', @token)).to eq JSON.parse(@delete_token_response)
@ -48,7 +45,6 @@ describe Pooler do
it 'raises a token error if something goes wrong' do
stub_request(:delete, 'https://first.last:password@abs.example.com/api/v2/token/utpg2i2xswor6h8ttjhu3d47z53yy47y')
.with(:headers => { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Faraday v0.9.2' })
.to_return(:status => 500, :body => '{"ok":false}', :headers => {})
expect { Auth.delete_token(false, @abs_url, 'first.last', 'password', @token) }.to raise_error(TokenError)
@ -67,7 +63,7 @@ describe Pooler do
it 'checks the status of a token' do
stub_request(:get, "#{@abs_url}/token/utpg2i2xswor6h8ttjhu3d47z53yy47y")
.with(:headers => { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Faraday v0.9.2' })
.with(:headers => { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3' })
.to_return(:status => 200, :body => @token_status_response, :headers => {})
expect(Auth.token_status(false, @abs_url, @token)).to eq JSON.parse(@token_status_response)
@ -75,7 +71,7 @@ describe Pooler do
it 'raises a token error if something goes wrong' do
stub_request(:get, "#{@abs_url}/token/utpg2i2xswor6h8ttjhu3d47z53yy47y")
.with(:headers => { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Faraday v0.9.2' })
.with(:headers => { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3' })
.to_return(:status => 500, :body => '{"ok":false}', :headers => {})
expect { Auth.token_status(false, @abs_url, @token) }.to raise_error(TokenError)

38
spec/vmfloaty/abs_spec.rb Normal file
View file

@ -0,0 +1,38 @@
# frozen_string_literal: true
require 'spec_helper'
require 'vmfloaty/utils'
require 'vmfloaty/errors'
require 'vmfloaty/abs'
describe ABS do
before :each do
end
describe '#format' do
it 'returns an hash formatted like a vmpooler return' do
abs_formatted_response = [
{ 'hostname' => 'aaaaaaaaaaaaaaa.delivery.puppetlabs.net', 'type' => 'centos-7.2-x86_64', 'engine' => 'vmpooler' },
{ 'hostname' => 'aaaaaaaaaaaaaab.delivery.puppetlabs.net', 'type' => 'centos-7.2-x86_64', 'engine' => 'vmpooler' },
{ 'hostname' => 'aaaaaaaaaaaaaac.delivery.puppetlabs.net', 'type' => 'ubuntu-7.2-x86_64', 'engine' => 'vmpooler' },
]
vmpooler_formatted_response = ABS.translated(abs_formatted_response)
vmpooler_formatted_compare = {
'centos-7.2-x86_64' => {},
'ubuntu-7.2-x86_64' => {},
}
vmpooler_formatted_compare['centos-7.2-x86_64']['hostname'] = ['aaaaaaaaaaaaaaa.delivery.puppetlabs.net', 'aaaaaaaaaaaaaab.delivery.puppetlabs.net']
vmpooler_formatted_compare['ubuntu-7.2-x86_64']['hostname'] = ['aaaaaaaaaaaaaac.delivery.puppetlabs.net']
vmpooler_formatted_compare['ok'] = true
expect(vmpooler_formatted_response).to eq(vmpooler_formatted_compare)
vmpooler_formatted_response.delete('ok')
vmpooler_formatted_compare.delete('ok')
expect(vmpooler_formatted_response).to eq(vmpooler_formatted_compare)
end
end
end

View file

@ -66,7 +66,7 @@ describe NonstandardPooler do
@token_status_body_active = <<~BODY
{
"ok": true,
"user": "first.last",
"user": 'first.last',
"created": "2017-09-18 01:25:41 +0000",
"last_accessed": "2017-09-21 19:46:25 +0000",
"reserved_hosts": ["sol10-9", "sol10-11"]
@ -75,7 +75,7 @@ describe NonstandardPooler do
@token_status_body_empty = <<~BODY
{
"ok": true,
"user": "first.last",
"user": 'first.last',
"created": "2017-09-18 01:25:41 +0000",
"last_accessed": "2017-09-21 19:46:25 +0000",
"reserved_hosts": []
@ -125,7 +125,7 @@ describe NonstandardPooler do
.to_return(:status => 401, :body => '{"ok":false,"reason": "token: token-value does not exist"}', :headers => {})
vm_hash = { 'solaris-11-sparc' => 1 }
expect { NonstandardPooler.retrieve(false, vm_hash, 'token-value', @nspooler_url) }.to raise_error(AuthError)
expect { NonstandardPooler.retrieve(false, vm_hash, 'token-value', @nspooler_url, 'first.last') }.to raise_error(AuthError)
end
it 'retrieves a single vm with a token' do
@ -134,7 +134,7 @@ describe NonstandardPooler do
.to_return(:status => 200, :body => @retrieve_response_body_single, :headers => {})
vm_hash = { 'solaris-11-sparc' => 1 }
vm_req = NonstandardPooler.retrieve(false, vm_hash, 'token-value', @nspooler_url)
vm_req = NonstandardPooler.retrieve(false, vm_hash, 'token-value', @nspooler_url, 'first.last')
expect(vm_req).to be_an_instance_of Hash
expect(vm_req['ok']).to equal true
expect(vm_req['solaris-11-sparc']['hostname']).to eq 'sol11-4.delivery.puppetlabs.net'
@ -146,7 +146,7 @@ describe NonstandardPooler do
.to_return(:status => 200, :body => @retrieve_response_body_many, :headers => {})
vm_hash = { 'aix-7.1-power' => 1, 'solaris-10-sparc' => 2 }
vm_req = NonstandardPooler.retrieve(false, vm_hash, 'token-value', @nspooler_url)
vm_req = NonstandardPooler.retrieve(false, vm_hash, 'token-value', @nspooler_url, 'first.last')
expect(vm_req).to be_an_instance_of Hash
expect(vm_req['ok']).to equal true
expect(vm_req['solaris-10-sparc']['hostname']).to be_an_instance_of Array
@ -246,7 +246,7 @@ describe NonstandardPooler do
"sol10-11": {
"fqdn": "sol10-11.delivery.puppetlabs.net",
"os_triple": "solaris-10-sparc",
"reserved_by_user": "first.last",
"reserved_by_user": 'first.last',
"reserved_for_reason": "testing",
"hours_left_on_reservation": 29.12
}

View file

@ -16,9 +16,9 @@ describe Service do
it 'prompts the user for their password and retrieves a token' do
config = { 'user' => 'first.last', 'url' => 'http://default.url' }
service = Service.new(MockOptions.new, config)
allow(STDOUT).to receive(:puts).with('Enter your pooler service password:')
allow(STDOUT).to receive(:puts).with('Enter your http://default.url service password:')
allow(Commander::UI).to(receive(:password)
.with('Enter your pooler service password:', '*')
.with('Enter your http://default.url service password:', '*')
.and_return('hunter2'))
allow(Auth).to(receive(:get_token)
.with(nil, config['url'], config['user'], 'hunter2')
@ -29,11 +29,11 @@ describe Service do
it 'prompts the user for their username and password if the username is unknown' do
config = { 'url' => 'http://default.url' }
service = Service.new(MockOptions.new({}), config)
allow(STDOUT).to receive(:puts).with 'Enter your pooler service username:'
allow(STDOUT).to receive(:puts).with 'Enter your http://default.url service username:'
allow(STDOUT).to receive(:puts).with "\n"
allow(STDIN).to receive(:gets).and_return('first.last')
allow(Commander::UI).to(receive(:password)
.with('Enter your pooler service password:', '*')
.with('Enter your http://default.url service password:', '*')
.and_return('hunter2'))
allow(Auth).to(receive(:get_token)
.with(nil, config['url'], 'first.last', 'hunter2')
@ -46,7 +46,7 @@ describe Service do
it 'deletes a token' do
service = Service.new(MockOptions.new, 'user' => 'first.last', 'url' => 'http://default.url')
allow(Commander::UI).to(receive(:password)
.with('Enter your pooler service password:', '*')
.with('Enter your http://default.url service password:', '*')
.and_return('hunter2'))
allow(Auth).to(receive(:delete_token)
.with(nil, 'http://default.url', 'first.last', 'hunter2', 'token-value')