(rubocop) Fix Style/StringLiterals

This commit is contained in:
Tim Sharpe 2019-02-03 10:42:28 +11:00
parent cf2295ccfd
commit 1272343cdd
12 changed files with 181 additions and 181 deletions

View file

@ -43,7 +43,7 @@ class Vmfloaty
force = options.force force = options.force
if args.empty? if args.empty?
STDERR.puts "No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs." STDERR.puts 'No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs.'
exit 1 exit 1
end end
@ -53,12 +53,12 @@ class Vmfloaty
large_pool_requests = os_types.select{|_,v| v > max_pool_request} large_pool_requests = os_types.select{|_,v| v > max_pool_request}
if ! large_pool_requests.empty? and ! force if ! large_pool_requests.empty? and ! force
STDERR.puts "Requesting vms over #{max_pool_request} requires a --force flag." STDERR.puts "Requesting vms over #{max_pool_request} requires a --force flag."
STDERR.puts "Try again with `floaty get --force`" STDERR.puts 'Try again with `floaty get --force`'
exit 1 exit 1
end end
if os_types.empty? if os_types.empty?
STDERR.puts "No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs." STDERR.puts 'No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs.'
exit 1 exit 1
end end
@ -144,10 +144,10 @@ class Vmfloaty
modify_all = options.all modify_all = options.all
if hostname.nil? and !modify_all if hostname.nil? and !modify_all
STDERR.puts "ERROR: Provide a hostname or specify --all." STDERR.puts 'ERROR: Provide a hostname or specify --all.'
exit 1 exit 1
end end
running_vms = modify_all ? service.list_active(verbose) : hostname.split(",") running_vms = modify_all ? service.list_active(verbose) : hostname.split(',')
tags = options.tags ? JSON.parse(options.tags) : nil tags = options.tags ? JSON.parse(options.tags) : nil
modify_hash = { modify_hash = {
@ -171,11 +171,11 @@ class Vmfloaty
end end
if ok if ok
if modify_all if modify_all
puts "Successfully modified all VMs." puts 'Successfully modified all VMs.'
else else
puts "Successfully modified VM #{hostname}." puts "Successfully modified VM #{hostname}."
end end
puts "Use `floaty list --active` to see the results." puts 'Use `floaty list --active` to see the results.'
end end
end end
end end
@ -205,7 +205,7 @@ class Vmfloaty
if delete_all if delete_all
running_vms = service.list_active(verbose) running_vms = service.list_active(verbose)
if running_vms.empty? if running_vms.empty?
STDERR.puts "You have no running VMs." STDERR.puts 'You have no running VMs.'
else else
Utils.pretty_print_hosts(verbose, service, running_vms) Utils.pretty_print_hosts(verbose, service, running_vms)
# Confirm deletion # Confirm deletion
@ -236,7 +236,7 @@ class Vmfloaty
end end
end end
else else
STDERR.puts "You did not provide any hosts to delete" STDERR.puts 'You did not provide any hosts to delete'
exit 1 exit 1
end end
@ -417,7 +417,7 @@ class Vmfloaty
use_token = !options.notoken use_token = !options.notoken
if args.empty? if args.empty?
STDERR.puts "No operating systems provided to obtain. See `floaty ssh --help` for more information on how to get VMs." STDERR.puts 'No operating systems provided to obtain. See `floaty ssh --help` for more information on how to get VMs.'
exit 1 exit 1
end end

View file

@ -7,11 +7,11 @@ class Auth
def self.get_token(verbose, url, user, password) def self.get_token(verbose, url, user, password)
conn = Http.get_conn_with_auth(verbose, url, user, password) conn = Http.get_conn_with_auth(verbose, url, user, password)
resp = conn.post "token" resp = conn.post 'token'
res_body = JSON.parse(resp.body) res_body = JSON.parse(resp.body)
if res_body["ok"] if res_body['ok']
return res_body["token"] return res_body['token']
else else
raise TokenError, "HTTP #{resp.status}: There was a problem requesting a token:\n#{res_body}" raise TokenError, "HTTP #{resp.status}: There was a problem requesting a token:\n#{res_body}"
end end
@ -26,7 +26,7 @@ class Auth
response = conn.delete "token/#{token}" response = conn.delete "token/#{token}"
res_body = JSON.parse(response.body) res_body = JSON.parse(response.body)
if res_body["ok"] if res_body['ok']
return res_body return res_body
else else
raise TokenError, "HTTP #{response.status}: There was a problem deleting a token:\n#{res_body}" raise TokenError, "HTTP #{response.status}: There was a problem deleting a token:\n#{res_body}"
@ -43,7 +43,7 @@ class Auth
response = conn.get "token/#{token}" response = conn.get "token/#{token}"
res_body = JSON.parse(response.body) res_body = JSON.parse(response.body)
if res_body["ok"] if res_body['ok']
return res_body return res_body
else else
raise TokenError, "HTTP #{response.status}: There was a problem getting the status of a token:\n#{res_body}" raise TokenError, "HTTP #{response.status}: There was a problem getting the status of a token:\n#{res_body}"

View file

@ -1,23 +1,23 @@
class AuthError < StandardError class AuthError < StandardError
def initialize(msg="Could not authenticate to pooler") def initialize(msg='Could not authenticate to pooler')
super super
end end
end end
class TokenError < StandardError class TokenError < StandardError
def initialize(msg="Could not do operation with token provided") def initialize(msg='Could not do operation with token provided')
super super
end end
end end
class MissingParamError < StandardError class MissingParamError < StandardError
def initialize(msg="Argument provided to function is missing") def initialize(msg='Argument provided to function is missing')
super super
end end
end end
class ModifyError < StandardError class ModifyError < StandardError
def initialize(msg="Could not modify VM") def initialize(msg='Could not modify VM')
super super
end end
end end

View file

@ -18,7 +18,7 @@ class Http
def self.get_conn(verbose, url) def self.get_conn(verbose, url)
if url.nil? if url.nil?
raise "Did not provide a url to connect to" raise 'Did not provide a url to connect to'
end end
unless is_url(url) unless is_url(url)
@ -36,11 +36,11 @@ class Http
def self.get_conn_with_auth(verbose, url, user, password) def self.get_conn_with_auth(verbose, url, user, password)
if url.nil? if url.nil?
raise "Did not provide a url to connect to" raise 'Did not provide a url to connect to'
end end
if user.nil? if user.nil?
raise "You did not provide a user to authenticate with" raise 'You did not provide a user to authenticate with'
end end
unless is_url(url) unless is_url(url)

View file

@ -37,24 +37,24 @@ class Pooler
conn.headers['X-AUTH-TOKEN'] = token conn.headers['X-AUTH-TOKEN'] = token
end end
os_string = "" os_string = ''
os_type.each do |os,num| os_type.each do |os,num|
num.times do |i| num.times do |i|
os_string << os+"+" os_string << os+'+'
end end
end end
os_string = os_string.chomp("+") os_string = os_string.chomp('+')
if os_string.size == 0 if os_string.size == 0
raise MissingParamError, "No operating systems provided to obtain." raise MissingParamError, 'No operating systems provided to obtain.'
end end
response = conn.post "vm/#{os_string}" response = conn.post "vm/#{os_string}"
res_body = JSON.parse(response.body) res_body = JSON.parse(response.body)
if res_body["ok"] if res_body['ok']
res_body res_body
elsif response.status == 401 elsif response.status == 401
raise AuthError, "HTTP #{response.status}: The token provided could not authenticate to the pooler.\n#{res_body}" raise AuthError, "HTTP #{response.status}: The token provided could not authenticate to the pooler.\n#{res_body}"
@ -65,7 +65,7 @@ class Pooler
def self.modify(verbose, url, hostname, token, modify_hash) def self.modify(verbose, url, hostname, token, modify_hash)
if token.nil? if token.nil?
raise TokenError, "Token provided was nil. Request cannot be made to modify vm" raise TokenError, 'Token provided was nil. Request cannot be made to modify vm'
end end
modify_hash.keys.each do |key| modify_hash.keys.each do |key|
@ -93,7 +93,7 @@ class Pooler
def self.disk(verbose, url, hostname, token, disk) def self.disk(verbose, url, hostname, token, disk)
if token.nil? if token.nil?
raise TokenError, "Token provided was nil. Request cannot be made to modify vm" raise TokenError, 'Token provided was nil. Request cannot be made to modify vm'
end end
conn = Http.get_conn(verbose, url) conn = Http.get_conn(verbose, url)
@ -107,7 +107,7 @@ class Pooler
def self.delete(verbose, url, hosts, token) def self.delete(verbose, url, hosts, token)
if token.nil? if token.nil?
raise TokenError, "Token provided was nil. Request cannot be made to delete vm" raise TokenError, 'Token provided was nil. Request cannot be made to delete vm'
end end
conn = Http.get_conn(verbose, url) conn = Http.get_conn(verbose, url)
@ -154,7 +154,7 @@ class Pooler
def self.snapshot(verbose, url, hostname, token) def self.snapshot(verbose, url, hostname, token)
if token.nil? if token.nil?
raise TokenError, "Token provided was nil. Request cannot be made to snapshot vm" raise TokenError, 'Token provided was nil. Request cannot be made to snapshot vm'
end end
conn = Http.get_conn(verbose, url) conn = Http.get_conn(verbose, url)
@ -167,7 +167,7 @@ class Pooler
def self.revert(verbose, url, hostname, token, snapshot_sha) def self.revert(verbose, url, hostname, token, snapshot_sha)
if token.nil? if token.nil?
raise TokenError, "Token provided was nil. Request cannot be made to revert vm" raise TokenError, 'Token provided was nil. Request cannot be made to revert vm'
end end
conn = Http.get_conn(verbose, url) conn = Http.get_conn(verbose, url)

View file

@ -31,7 +31,7 @@ class Service
def user def user
unless @config['user'] unless @config['user']
puts "Enter your pooler service username:" puts 'Enter your pooler service username:'
@config['user'] = STDIN.gets.chomp @config['user'] = STDIN.gets.chomp
end end
@config['user'] @config['user']
@ -39,7 +39,7 @@ class Service
def token def token
unless @config['token'] unless @config['token']
puts "No token found. Retrieving a token..." puts 'No token found. Retrieving a token...'
@config['token'] = get_new_token(nil) @config['token'] = get_new_token(nil)
end end
@config['token'] @config['token']
@ -47,13 +47,13 @@ class Service
def get_new_token(verbose) def get_new_token(verbose)
username = user username = user
pass = Commander::UI::password "Enter your pooler service password:", '*' pass = Commander::UI::password 'Enter your pooler service password:', '*'
Auth.get_token(verbose, url, username, pass) Auth.get_token(verbose, url, username, pass)
end end
def delete_token(verbose, token_value = @config['token']) def delete_token(verbose, token_value = @config['token'])
username = user username = user
pass = Commander::UI::password "Enter your pooler service password:", '*' pass = Commander::UI::password 'Enter your pooler service password:', '*'
Auth.delete_token(verbose, url, username, pass, token_value) Auth.delete_token(verbose, url, username, pass, token_value)
end end
@ -91,9 +91,9 @@ class Service
def pretty_print_running(verbose, hostnames = []) def pretty_print_running(verbose, hostnames = [])
if hostnames.empty? if hostnames.empty?
puts "You have no running VMs." puts 'You have no running VMs.'
else else
puts "Running VMs:" puts 'Running VMs:'
@service_object.pretty_print_hosts(verbose, hostnames, url) @service_object.pretty_print_hosts(verbose, hostnames, url)
end end
end end

View file

@ -14,19 +14,19 @@ class Ssh
end end
def self.ssh(verbose, host_os, token, url) def self.ssh(verbose, host_os, token, url)
ssh_path = which("ssh") ssh_path = which('ssh')
if !ssh_path if !ssh_path
raise "Could not determine path to ssh" raise 'Could not determine path to ssh'
end end
os_types = {} os_types = {}
os_types[host_os] = 1 os_types[host_os] = 1
response = Pooler.retrieve(verbose, os_types, token, url) response = Pooler.retrieve(verbose, os_types, token, url)
if response["ok"] == true if response['ok'] == true
if host_os =~ /win/ if host_os =~ /win/
user = "Administrator" user = 'Administrator'
else else
user = "root" user = 'root'
end end
hostname = "#{response[host_os]["hostname"]}.#{response["domain"]}" hostname = "#{response[host_os]["hostname"]}.#{response["domain"]}"

View file

@ -65,7 +65,7 @@ class Utils
# ...] # ...]
os_types = {} os_types = {}
os_args.each do |arg| os_args.each do |arg|
os_arr = arg.split("=") os_arr = arg.split('=')
if os_arr.size == 1 if os_arr.size == 1
# assume they didn't specify an = sign if split returns 1 size # assume they didn't specify an = sign if split returns 1 size
os_types[os_arr[0]] = 1 os_types[os_arr[0]] = 1

View file

@ -3,67 +3,67 @@ require_relative '../../lib/vmfloaty/auth'
describe Pooler do describe Pooler do
before :each do before :each do
@vmpooler_url = "https://vmpooler.example.com" @vmpooler_url = 'https://vmpooler.example.com'
end end
describe "#get_token" do describe '#get_token' do
before :each do before :each do
@get_token_response = "{\"ok\": true,\"token\":\"utpg2i2xswor6h8ttjhu3d47z53yy47y\"}" @get_token_response = '{"ok": true,"token":"utpg2i2xswor6h8ttjhu3d47z53yy47y"}'
@token = "utpg2i2xswor6h8ttjhu3d47z53yy47y" @token = 'utpg2i2xswor6h8ttjhu3d47z53yy47y'
end end
it "returns a token from vmpooler" do it 'returns a token from vmpooler' do
stub_request(:post, "https://first.last:password@vmpooler.example.com/token"). stub_request(:post, 'https://first.last:password@vmpooler.example.com/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'}). 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 => {}) to_return(:status => 200, :body => @get_token_response, :headers => {})
token = Auth.get_token(false, @vmpooler_url, "first.last", "password") token = Auth.get_token(false, @vmpooler_url, 'first.last', 'password')
expect(token).to eq @token expect(token).to eq @token
end end
it "raises a token error if something goes wrong" do it 'raises a token error if something goes wrong' do
stub_request(:post, "https://first.last:password@vmpooler.example.com/token"). stub_request(:post, 'https://first.last:password@vmpooler.example.com/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'}). 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 => {}) to_return(:status => 500, :body => '{"ok":false}', :headers => {})
expect{ Auth.get_token(false, @vmpooler_url, "first.last", "password") }.to raise_error(TokenError) expect{ Auth.get_token(false, @vmpooler_url, 'first.last', 'password') }.to raise_error(TokenError)
end end
end end
describe "#delete_token" do describe '#delete_token' do
before :each do before :each do
@delete_token_response = "{\"ok\":true}" @delete_token_response = '{"ok":true}'
@token = "utpg2i2xswor6h8ttjhu3d47z53yy47y" @token = 'utpg2i2xswor6h8ttjhu3d47z53yy47y'
end end
it "deletes the specified token" do it 'deletes the specified token' do
stub_request(:delete, "https://first.last:password@vmpooler.example.com/token/utpg2i2xswor6h8ttjhu3d47z53yy47y"). stub_request(:delete, 'https://first.last:password@vmpooler.example.com/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', 'User-Agent'=>'Faraday v0.9.2'}).
to_return(:status => 200, :body => @delete_token_response, :headers => {}) to_return(:status => 200, :body => @delete_token_response, :headers => {})
expect(Auth.delete_token(false, @vmpooler_url, "first.last", "password", @token)).to eq JSON.parse(@delete_token_response) expect(Auth.delete_token(false, @vmpooler_url, 'first.last', 'password', @token)).to eq JSON.parse(@delete_token_response)
end end
it "raises a token error if something goes wrong" do it 'raises a token error if something goes wrong' do
stub_request(:delete, "https://first.last:password@vmpooler.example.com/token/utpg2i2xswor6h8ttjhu3d47z53yy47y"). stub_request(:delete, 'https://first.last:password@vmpooler.example.com/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', 'User-Agent'=>'Faraday v0.9.2'}).
to_return(:status => 500, :body => "{\"ok\":false}", :headers => {}) to_return(:status => 500, :body => '{"ok":false}', :headers => {})
expect{ Auth.delete_token(false, @vmpooler_url, "first.last", "password", @token) }.to raise_error(TokenError) expect{ Auth.delete_token(false, @vmpooler_url, 'first.last', 'password', @token) }.to raise_error(TokenError)
end end
it "raises a token error if no token provided" do it 'raises a token error if no token provided' do
expect{ Auth.delete_token(false, @vmpooler_url, "first.last", "password", nil) }.to raise_error(TokenError) expect{ Auth.delete_token(false, @vmpooler_url, 'first.last', 'password', nil) }.to raise_error(TokenError)
end end
end end
describe "#token_status" do describe '#token_status' do
before :each do before :each do
@token_status_response = "{\"ok\":true,\"utpg2i2xswor6h8ttjhu3d47z53yy47y\":{\"created\":\"2015-04-28 19:17:47 -0700\"}}" @token_status_response = '{"ok":true,"utpg2i2xswor6h8ttjhu3d47z53yy47y":{"created":"2015-04-28 19:17:47 -0700"}}'
@token = "utpg2i2xswor6h8ttjhu3d47z53yy47y" @token = 'utpg2i2xswor6h8ttjhu3d47z53yy47y'
end end
it "checks the status of a token" do it 'checks the status of a token' do
stub_request(:get, "#{@vmpooler_url}/token/utpg2i2xswor6h8ttjhu3d47z53yy47y"). stub_request(:get, "#{@vmpooler_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', 'User-Agent'=>'Faraday v0.9.2'}).
to_return(:status => 200, :body => @token_status_response, :headers => {}) to_return(:status => 200, :body => @token_status_response, :headers => {})
@ -71,15 +71,15 @@ describe Pooler do
expect(Auth.token_status(false, @vmpooler_url, @token)).to eq JSON.parse(@token_status_response) expect(Auth.token_status(false, @vmpooler_url, @token)).to eq JSON.parse(@token_status_response)
end end
it "raises a token error if something goes wrong" do it 'raises a token error if something goes wrong' do
stub_request(:get, "#{@vmpooler_url}/token/utpg2i2xswor6h8ttjhu3d47z53yy47y"). stub_request(:get, "#{@vmpooler_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', 'User-Agent'=>'Faraday v0.9.2'}).
to_return(:status => 500, :body => "{\"ok\":false}", :headers => {}) to_return(:status => 500, :body => '{"ok":false}', :headers => {})
expect{ Auth.token_status(false, @vmpooler_url, @token) }.to raise_error(TokenError) expect{ Auth.token_status(false, @vmpooler_url, @token) }.to raise_error(TokenError)
end end
it "raises a token error if no token provided" do it 'raises a token error if no token provided' do
expect{ Auth.token_status(false, @vmpooler_url, nil) }.to raise_error(TokenError) expect{ Auth.token_status(false, @vmpooler_url, nil) }.to raise_error(TokenError)
end end
end end

View file

@ -172,10 +172,10 @@ BODY
end end
it 'raises an error if the user tries to modify an unsupported attribute' do it 'raises an error if the user tries to modify an unsupported attribute' do
stub_request(:put, "https://nspooler.example.com/host/myfakehost"). stub_request(:put, 'https://nspooler.example.com/host/myfakehost').
with(body: {"{}"=>true}, with(body: {'{}'=>true},
headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Faraday v0.9.2', 'X-Auth-Token'=>'token-value'}). headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Faraday v0.9.2', 'X-Auth-Token'=>'token-value'}).
to_return(status: 200, body: "", headers: {}) to_return(status: 200, body: '', headers: {})
details = { lifetime: 12 } details = { lifetime: 12 }
expect { NonstandardPooler.modify(false, @nspooler_url, 'myfakehost', 'token-value', details) } expect { NonstandardPooler.modify(false, @nspooler_url, 'myfakehost', 'token-value', details) }
.to raise_error(ModifyError) .to raise_error(ModifyError)
@ -188,7 +188,7 @@ BODY
headers: @post_request_headers) headers: @post_request_headers)
.to_return(status: 200, body: '{"ok": true}', headers: {}) .to_return(status: 200, body: '{"ok": true}', headers: {})
modify_hash = { reason: "testing" } modify_hash = { reason: 'testing' }
modify_req = NonstandardPooler.modify(false, @nspooler_url, 'myfakehost', 'token-value', modify_hash) modify_req = NonstandardPooler.modify(false, @nspooler_url, 'myfakehost', 'token-value', modify_hash)
expect(modify_req['ok']).to be true expect(modify_req['ok']).to be true
end end

View file

@ -3,15 +3,15 @@ require_relative '../../lib/vmfloaty/pooler'
describe Pooler do describe Pooler do
before :each do before :each do
@vmpooler_url = "https://vmpooler.example.com" @vmpooler_url = 'https://vmpooler.example.com'
end end
describe "#list" do describe '#list' do
before :each do before :each do
@list_response_body = "[\"debian-7-i386\",\"debian-7-x86_64\",\"centos-7-x86_64\"]" @list_response_body = '["debian-7-i386","debian-7-x86_64","centos-7-x86_64"]'
end end
it "returns a hash with operating systems from the pooler" do it 'returns a hash with operating systems from the pooler' do
stub_request(:get, "#{@vmpooler_url}/vm"). stub_request(:get, "#{@vmpooler_url}/vm").
to_return(:status => 200, :body => @list_response_body, :headers => {}) to_return(:status => 200, :body => @list_response_body, :headers => {})
@ -19,42 +19,42 @@ describe Pooler do
expect(list).to be_an_instance_of Array expect(list).to be_an_instance_of Array
end end
it "filters operating systems based on the filter param" do it 'filters operating systems based on the filter param' do
stub_request(:get, "#{@vmpooler_url}/vm"). stub_request(:get, "#{@vmpooler_url}/vm").
to_return(:status => 200, :body => @list_response_body, :headers => {}) to_return(:status => 200, :body => @list_response_body, :headers => {})
list = Pooler.list(false, @vmpooler_url, "deb") list = Pooler.list(false, @vmpooler_url, 'deb')
expect(list).to be_an_instance_of Array expect(list).to be_an_instance_of Array
expect(list.size).to equal 2 expect(list.size).to equal 2
end end
it "returns nothing if the filter does not match" do it 'returns nothing if the filter does not match' do
stub_request(:get, "#{@vmpooler_url}/vm"). stub_request(:get, "#{@vmpooler_url}/vm").
to_return(:status => 200, :body => @list_response_body, :headers => {}) to_return(:status => 200, :body => @list_response_body, :headers => {})
list = Pooler.list(false, @vmpooler_url, "windows") list = Pooler.list(false, @vmpooler_url, 'windows')
expect(list).to be_an_instance_of Array expect(list).to be_an_instance_of Array
expect(list.size).to equal 0 expect(list.size).to equal 0
end end
end end
describe "#retrieve" do describe '#retrieve' do
before :each do before :each do
@retrieve_response_body_single = "{\"ok\":true,\"debian-7-i386\":{\"hostname\":\"fq6qlpjlsskycq6\"}}" @retrieve_response_body_single = '{"ok":true,"debian-7-i386":{"hostname":"fq6qlpjlsskycq6"}}'
@retrieve_response_body_double = "{\"ok\":true,\"debian-7-i386\":{\"hostname\":[\"sc0o4xqtodlul5w\",\"4m4dkhqiufnjmxy\"]},\"centos-7-x86_64\":{\"hostname\":\"zb91y9qbrbf6d3q\"}}" @retrieve_response_body_double = '{"ok":true,"debian-7-i386":{"hostname":["sc0o4xqtodlul5w","4m4dkhqiufnjmxy"]},"centos-7-x86_64":{"hostname":"zb91y9qbrbf6d3q"}}'
end end
it "raises an AuthError if the token is invalid" do it 'raises an AuthError if the token is invalid' do
stub_request(:post, "#{@vmpooler_url}/vm/debian-7-i386"). stub_request(:post, "#{@vmpooler_url}/vm/debian-7-i386").
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', 'X-Auth-Token'=>'mytokenfile'}). 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', 'X-Auth-Token'=>'mytokenfile'}).
to_return(:status => 401, :body => "{\"ok\":false}", :headers => {}) to_return(:status => 401, :body => '{"ok":false}', :headers => {})
vm_hash = {} vm_hash = {}
vm_hash['debian-7-i386'] = 1 vm_hash['debian-7-i386'] = 1
expect{ Pooler.retrieve(false, vm_hash, 'mytokenfile', @vmpooler_url) }.to raise_error(AuthError) expect{ Pooler.retrieve(false, vm_hash, 'mytokenfile', @vmpooler_url) }.to raise_error(AuthError)
end end
it "retrieves a single vm with a token" do it 'retrieves a single vm with a token' do
stub_request(:post, "#{@vmpooler_url}/vm/debian-7-i386"). stub_request(:post, "#{@vmpooler_url}/vm/debian-7-i386").
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', 'X-Auth-Token'=>'mytokenfile'}). 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', 'X-Auth-Token'=>'mytokenfile'}).
to_return(:status => 200, :body => @retrieve_response_body_single, :headers => {}) to_return(:status => 200, :body => @retrieve_response_body_single, :headers => {})
@ -63,11 +63,11 @@ describe Pooler do
vm_hash['debian-7-i386'] = 1 vm_hash['debian-7-i386'] = 1
vm_req = Pooler.retrieve(false, vm_hash, 'mytokenfile', @vmpooler_url) vm_req = Pooler.retrieve(false, vm_hash, 'mytokenfile', @vmpooler_url)
expect(vm_req).to be_an_instance_of Hash expect(vm_req).to be_an_instance_of Hash
expect(vm_req["ok"]).to equal true expect(vm_req['ok']).to equal true
expect(vm_req["debian-7-i386"]["hostname"]).to eq "fq6qlpjlsskycq6" expect(vm_req['debian-7-i386']['hostname']).to eq 'fq6qlpjlsskycq6'
end end
it "retrieves a multiple vms with a token" do it 'retrieves a multiple vms with a token' do
stub_request(:post, "#{@vmpooler_url}/vm/debian-7-i386+debian-7-i386+centos-7-x86_64"). stub_request(:post, "#{@vmpooler_url}/vm/debian-7-i386+debian-7-i386+centos-7-x86_64").
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', 'X-Auth-Token'=>'mytokenfile'}). 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', 'X-Auth-Token'=>'mytokenfile'}).
to_return(:status => 200, :body => @retrieve_response_body_double, :headers => {}) to_return(:status => 200, :body => @retrieve_response_body_double, :headers => {})
@ -77,24 +77,24 @@ describe Pooler do
vm_hash['centos-7-x86_64'] = 1 vm_hash['centos-7-x86_64'] = 1
vm_req = Pooler.retrieve(false, vm_hash, 'mytokenfile', @vmpooler_url) vm_req = Pooler.retrieve(false, vm_hash, 'mytokenfile', @vmpooler_url)
expect(vm_req).to be_an_instance_of Hash expect(vm_req).to be_an_instance_of Hash
expect(vm_req["ok"]).to equal true expect(vm_req['ok']).to equal true
expect(vm_req["debian-7-i386"]["hostname"]).to be_an_instance_of Array expect(vm_req['debian-7-i386']['hostname']).to be_an_instance_of Array
expect(vm_req["debian-7-i386"]["hostname"]).to eq ["sc0o4xqtodlul5w", "4m4dkhqiufnjmxy"] expect(vm_req['debian-7-i386']['hostname']).to eq ['sc0o4xqtodlul5w', '4m4dkhqiufnjmxy']
expect(vm_req["centos-7-x86_64"]["hostname"]).to eq "zb91y9qbrbf6d3q" expect(vm_req['centos-7-x86_64']['hostname']).to eq 'zb91y9qbrbf6d3q'
end end
end end
describe "#modify" do describe '#modify' do
before :each do before :each do
@modify_response_body_success = "{\"ok\":true}" @modify_response_body_success = '{"ok":true}'
@modify_response_body_fail = "{\"ok\":false}" @modify_response_body_fail = '{"ok":false}'
end end
it "raises a TokenError if token provided is nil" do it 'raises a TokenError if token provided is nil' do
expect{ Pooler.modify(false, @vmpooler_url, 'myfakehost', nil, {}) }.to raise_error(TokenError) expect{ Pooler.modify(false, @vmpooler_url, 'myfakehost', nil, {}) }.to raise_error(TokenError)
end end
it "modifies the TTL of a vm" do it 'modifies the TTL of a vm' do
modify_hash = { :lifetime => 12 } modify_hash = { :lifetime => 12 }
stub_request(:put, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6"). stub_request(:put, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6").
with(:body => {'{"lifetime":12}'=>true}, with(:body => {'{"lifetime":12}'=>true},
@ -102,17 +102,17 @@ describe Pooler do
to_return(:status => 200, :body => @modify_response_body_success, :headers => {}) to_return(:status => 200, :body => @modify_response_body_success, :headers => {})
modify_req = Pooler.modify(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile', modify_hash) modify_req = Pooler.modify(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile', modify_hash)
expect(modify_req["ok"]).to be true expect(modify_req['ok']).to be true
end end
end end
describe "#delete" do describe '#delete' do
before :each do before :each do
@delete_response_body_success = "{\"ok\":true}" @delete_response_body_success = '{"ok":true}'
@delete_response = {"fq6qlpjlsskycq6"=>{"ok"=>true}} @delete_response = {'fq6qlpjlsskycq6'=>{'ok'=>true}}
end end
it "deletes a specified vm" do it 'deletes a specified vm' do
stub_request(:delete, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6"). stub_request(:delete, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6").
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Faraday v0.9.2', 'X-Auth-Token'=>'mytokenfile'}). with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Faraday v0.9.2', 'X-Auth-Token'=>'mytokenfile'}).
to_return(:status => 200, :body => @delete_response_body_success, :headers => {}) to_return(:status => 200, :body => @delete_response_body_success, :headers => {})
@ -120,18 +120,18 @@ describe Pooler do
expect(Pooler.delete(false, @vmpooler_url, ['fq6qlpjlsskycq6'], 'mytokenfile')).to eq @delete_response expect(Pooler.delete(false, @vmpooler_url, ['fq6qlpjlsskycq6'], 'mytokenfile')).to eq @delete_response
end end
it "raises a token error if no token provided" do it 'raises a token error if no token provided' do
expect{ Pooler.delete(false, @vmpooler_url, ['myfakehost'], nil) }.to raise_error(TokenError) expect{ Pooler.delete(false, @vmpooler_url, ['myfakehost'], nil) }.to raise_error(TokenError)
end end
end end
describe "#status" do describe '#status' do
before :each do before :each do
#smaller version #smaller version
@status_response_body = "{\"capacity\":{\"current\":716,\"total\":717,\"percent\": 99.9},\"status\":{\"ok\":true,\"message\":\"Battle station fully armed and operational.\"}}" @status_response_body = '{"capacity":{"current":716,"total":717,"percent": 99.9},"status":{"ok":true,"message":"Battle station fully armed and operational."}}'
end end
it "prints the status" do it 'prints the status' do
stub_request(:get, "#{@vmpooler_url}/status"). stub_request(:get, "#{@vmpooler_url}/status").
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', 'User-Agent'=>'Faraday v0.9.2'}).
to_return(:status => 200, :body => @status_response_body, :headers => {}) to_return(:status => 200, :body => @status_response_body, :headers => {})
@ -141,21 +141,21 @@ describe Pooler do
end end
end end
describe "#summary" do describe '#summary' do
before :each do before :each do
@status_response_body = "" @status_response_body = ''
it "prints the summary" do it 'prints the summary' do
end end
end end
end end
describe "#query" do describe '#query' do
before :each do before :each do
@query_response_body = "{\"ok\": true,\"fq6qlpjlsskycq6\":{\"template\":\"debian-7-x86_64\",\"lifetime\": 2,\"running\": 0.08,\"state\":\"running\",\"snapshots\":[\"n4eb4kdtp7rwv4x158366vd9jhac8btq\" ],\"domain\": \"delivery.puppetlabs.net\"}}" @query_response_body = '{"ok": true,"fq6qlpjlsskycq6":{"template":"debian-7-x86_64","lifetime": 2,"running": 0.08,"state":"running","snapshots":["n4eb4kdtp7rwv4x158366vd9jhac8btq" ],"domain": "delivery.puppetlabs.net"}}'
end end
it "makes a query about a vm" do it 'makes a query about a vm' do
stub_request(:get, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6"). stub_request(:get, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6").
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', 'User-Agent'=>'Faraday v0.9.2'}).
to_return(:status => 200, :body => @query_response_body, :headers => {}) to_return(:status => 200, :body => @query_response_body, :headers => {})
@ -165,59 +165,59 @@ describe Pooler do
end end
end end
describe "#snapshot" do describe '#snapshot' do
before :each do before :each do
@snapshot_response_body = "{\"ok\":true}" @snapshot_response_body = '{"ok":true}'
end end
it "makes a snapshot for a single vm" do it 'makes a snapshot for a single vm' do
stub_request(:post, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6/snapshot"). stub_request(:post, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6/snapshot").
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', 'X-Auth-Token'=>'mytokenfile'}). 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', 'X-Auth-Token'=>'mytokenfile'}).
to_return(:status => 200, :body => @snapshot_response_body, :headers => {}) to_return(:status => 200, :body => @snapshot_response_body, :headers => {})
snapshot_req = Pooler.snapshot(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile') snapshot_req = Pooler.snapshot(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile')
expect(snapshot_req["ok"]).to be true expect(snapshot_req['ok']).to be true
end end
end end
describe "#revert" do describe '#revert' do
before :each do before :each do
@revert_response_body = "{\"ok\":true}" @revert_response_body = '{"ok":true}'
end end
it "makes a request to revert a vm from a snapshot" do it 'makes a request to revert a vm from a snapshot' do
stub_request(:post, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6/snapshot/dAfewKNfaweLKNve"). stub_request(:post, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6/snapshot/dAfewKNfaweLKNve").
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', 'X-Auth-Token'=>'mytokenfile'}). 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', 'X-Auth-Token'=>'mytokenfile'}).
to_return(:status => 200, :body => @revert_response_body, :headers => {}) to_return(:status => 200, :body => @revert_response_body, :headers => {})
revert_req = Pooler.revert(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile', 'dAfewKNfaweLKNve') revert_req = Pooler.revert(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile', 'dAfewKNfaweLKNve')
expect(revert_req["ok"]).to be true expect(revert_req['ok']).to be true
end end
it "doesn't make a request to revert a vm if snapshot is not provided" do it "doesn't make a request to revert a vm if snapshot is not provided" do
expect{ Pooler.revert(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile', nil) }.to raise_error(RuntimeError, "Snapshot SHA provided was nil, could not revert fq6qlpjlsskycq6") expect{ Pooler.revert(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile', nil) }.to raise_error(RuntimeError, 'Snapshot SHA provided was nil, could not revert fq6qlpjlsskycq6')
end end
it "raises a TokenError if no token was provided" do it 'raises a TokenError if no token was provided' do
expect{ Pooler.revert(false, @vmpooler_url, 'myfakehost', nil, 'shaaaaaaa') }.to raise_error(TokenError) expect{ Pooler.revert(false, @vmpooler_url, 'myfakehost', nil, 'shaaaaaaa') }.to raise_error(TokenError)
end end
end end
describe "#disk" do describe '#disk' do
before :each do before :each do
@disk_response_body_success = "{\"ok\":true}" @disk_response_body_success = '{"ok":true}'
@disk_response_body_fail = "{\"ok\":false}" @disk_response_body_fail = '{"ok":false}'
end end
it "makes a request to extend disk space of a vm" do it 'makes a request to extend disk space of a vm' do
stub_request(:post, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6/disk/12"). stub_request(:post, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6/disk/12").
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', 'X-Auth-Token'=>'mytokenfile'}). to_return(:status => 200, :body => @disk_response_body_success, :headers => {}) 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', 'X-Auth-Token'=>'mytokenfile'}). to_return(:status => 200, :body => @disk_response_body_success, :headers => {})
disk_req = Pooler.disk(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile', 12) disk_req = Pooler.disk(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile', 12)
expect(disk_req["ok"]).to be true expect(disk_req['ok']).to be true
end end
it "raises a TokenError if no token was provided" do it 'raises a TokenError if no token was provided' do
expect{ Pooler.disk(false, @vmpooler_url, 'myfakehost', nil, 12) }.to raise_error(TokenError) expect{ Pooler.disk(false, @vmpooler_url, 'myfakehost', nil, 12) }.to raise_error(TokenError)
end end
end end

View file

@ -5,7 +5,7 @@ require_relative '../../lib/vmfloaty/utils'
describe Utils do describe Utils do
describe "#standardize_hostnames" do describe '#standardize_hostnames' do
before :each do before :each do
@vmpooler_response_body ='{ @vmpooler_response_body ='{
"ok": true, "ok": true,
@ -28,24 +28,24 @@ describe Utils do
}' }'
end end
it "formats a result from vmpooler into a hash of os to hostnames" do it 'formats a result from vmpooler into a hash of os to hostnames' do
result = Utils.standardize_hostnames(JSON.parse(@vmpooler_response_body)) result = Utils.standardize_hostnames(JSON.parse(@vmpooler_response_body))
expect(result).to eq('centos-7-x86_64' => ["dlgietfmgeegry2.delivery.mycompany.net"], expect(result).to eq('centos-7-x86_64' => ['dlgietfmgeegry2.delivery.mycompany.net'],
'ubuntu-1610-x86_64' => ["gdoy8q3nckuob0i.delivery.mycompany.net", "ctnktsd0u11p9tm.delivery.mycompany.net"]) 'ubuntu-1610-x86_64' => ['gdoy8q3nckuob0i.delivery.mycompany.net', 'ctnktsd0u11p9tm.delivery.mycompany.net'])
end end
it "formats a result from the nonstandard pooler into a hash of os to hostnames" do it 'formats a result from the nonstandard pooler into a hash of os to hostnames' do
result = Utils.standardize_hostnames(JSON.parse(@nonstandard_response_body)) result = Utils.standardize_hostnames(JSON.parse(@nonstandard_response_body))
expect(result).to eq('solaris-10-sparc' => ['sol10-10.delivery.mycompany.net', 'sol10-11.delivery.mycompany.net'], expect(result).to eq('solaris-10-sparc' => ['sol10-10.delivery.mycompany.net', 'sol10-11.delivery.mycompany.net'],
'ubuntu-16.04-power8' => ['power8-ubuntu16.04-6.delivery.mycompany.net']) 'ubuntu-16.04-power8' => ['power8-ubuntu16.04-6.delivery.mycompany.net'])
end end
end end
describe "#format_host_output" do describe '#format_host_output' do
before :each do before :each do
@vmpooler_results = { @vmpooler_results = {
'centos-7-x86_64' => ["dlgietfmgeegry2.delivery.mycompany.net"], 'centos-7-x86_64' => ['dlgietfmgeegry2.delivery.mycompany.net'],
'ubuntu-1610-x86_64' => ["gdoy8q3nckuob0i.delivery.mycompany.net", "ctnktsd0u11p9tm.delivery.mycompany.net"] 'ubuntu-1610-x86_64' => ['gdoy8q3nckuob0i.delivery.mycompany.net', 'ctnktsd0u11p9tm.delivery.mycompany.net']
} }
@nonstandard_results = { @nonstandard_results = {
'solaris-10-sparc' => ['sol10-10.delivery.mycompany.net', 'sol10-11.delivery.mycompany.net'], 'solaris-10-sparc' => ['sol10-10.delivery.mycompany.net', 'sol10-11.delivery.mycompany.net'],
@ -62,43 +62,43 @@ describe Utils do
- power8-ubuntu16.04-6.delivery.mycompany.net (ubuntu-16.04-power8) - power8-ubuntu16.04-6.delivery.mycompany.net (ubuntu-16.04-power8)
OUT OUT
end end
it "formats a hostname hash from vmpooler into a list that includes the os" do it 'formats a hostname hash from vmpooler into a list that includes the os' do
expect(Utils.format_host_output(@vmpooler_results)).to eq(@vmpooler_output) expect(Utils.format_host_output(@vmpooler_results)).to eq(@vmpooler_output)
end end
it "formats a hostname hash from the nonstandard pooler into a list that includes the os" do it 'formats a hostname hash from the nonstandard pooler into a list that includes the os' do
expect(Utils.format_host_output(@nonstandard_results)).to eq(@nonstandard_output) expect(Utils.format_host_output(@nonstandard_results)).to eq(@nonstandard_output)
end end
end end
describe "#get_service_object" do describe '#get_service_object' do
it "assumes vmpooler by default" do it 'assumes vmpooler by default' do
expect(Utils.get_service_object).to be Pooler expect(Utils.get_service_object).to be Pooler
end end
it "uses nspooler when told explicitly" do it 'uses nspooler when told explicitly' do
expect(Utils.get_service_object "nspooler").to be NonstandardPooler expect(Utils.get_service_object 'nspooler').to be NonstandardPooler
end end
end end
describe "#get_service_config" do describe '#get_service_config' do
before :each do before :each do
@default_config = { @default_config = {
"url" => "http://default.url", 'url' => 'http://default.url',
"user" => "first.last.default", 'user' => 'first.last.default',
"token" => "default-token", 'token' => 'default-token',
} }
@services_config = { @services_config = {
"services" => { 'services' => {
"vm" => { 'vm' => {
"url" => "http://vmpooler.url", 'url' => 'http://vmpooler.url',
"user" => "first.last.vmpooler", 'user' => 'first.last.vmpooler',
"token" => "vmpooler-token" 'token' => 'vmpooler-token'
}, },
"ns" => { 'ns' => {
"url" => "http://nspooler.url", 'url' => 'http://nspooler.url',
"user" => "first.last.nspooler", 'user' => 'first.last.nspooler',
"token" => "nspooler-token" 'token' => 'nspooler-token'
} }
} }
} }
@ -110,44 +110,44 @@ describe Utils do
expect(Utils.get_service_config(config, options)).to include @services_config['services']['vm'] expect(Utils.get_service_config(config, options)).to include @services_config['services']['vm']
end end
it "allows selection by configured service key" do it 'allows selection by configured service key' do
config = @default_config.merge @services_config config = @default_config.merge @services_config
options = MockOptions.new({:service => "ns"}) options = MockOptions.new({:service => 'ns'})
expect(Utils.get_service_config(config, options)).to include @services_config['services']['ns'] expect(Utils.get_service_config(config, options)).to include @services_config['services']['ns']
end end
it "uses top-level service config values as defaults when configured service values are missing" do it 'uses top-level service config values as defaults when configured service values are missing' do
config = @default_config.merge @services_config config = @default_config.merge @services_config
config["services"]['vm'].delete 'url' config['services']['vm'].delete 'url'
options = MockOptions.new({:service => "vm"}) options = MockOptions.new({:service => 'vm'})
expect(Utils.get_service_config(config, options)['url']).to eq 'http://default.url' expect(Utils.get_service_config(config, options)['url']).to eq 'http://default.url'
end end
it "raises an error if passed a service name that hasn't been configured" do it "raises an error if passed a service name that hasn't been configured" do
config = @default_config.merge @services_config config = @default_config.merge @services_config
options = MockOptions.new({:service => "none"}) options = MockOptions.new({:service => 'none'})
expect { Utils.get_service_config(config, options) }.to raise_error ArgumentError expect { Utils.get_service_config(config, options) }.to raise_error ArgumentError
end end
it "prioritizes values passed as command line options over configuration options" do it 'prioritizes values passed as command line options over configuration options' do
config = @default_config config = @default_config
options = MockOptions.new({:url => "http://alternate.url", :token => "alternate-token"}) options = MockOptions.new({:url => 'http://alternate.url', :token => 'alternate-token'})
expected = config.merge({"url" => "http://alternate.url", "token" => "alternate-token"}) expected = config.merge({'url' => 'http://alternate.url', 'token' => 'alternate-token'})
expect(Utils.get_service_config(config, options)).to include expected expect(Utils.get_service_config(config, options)).to include expected
end end
end end
describe "#generate_os_hash" do describe '#generate_os_hash' do
before :each do before :each do
@host_hash = {"centos"=>1, "debian"=>5, "windows"=>1} @host_hash = {'centos'=>1, 'debian'=>5, 'windows'=>1}
end end
it "takes an array of os arguments and returns a formatted hash" do it 'takes an array of os arguments and returns a formatted hash' do
host_arg = ["centos", "debian=5", "windows=1"] host_arg = ['centos', 'debian=5', 'windows=1']
expect(Utils.generate_os_hash(host_arg)).to eq @host_hash expect(Utils.generate_os_hash(host_arg)).to eq @host_hash
end end
it "returns an empty hash if there are no arguments provided" do it 'returns an empty hash if there are no arguments provided' do
host_arg = [] host_arg = []
expect(Utils.generate_os_hash(host_arg)).to be_empty expect(Utils.generate_os_hash(host_arg)).to be_empty
end end
@ -166,7 +166,7 @@ describe Utils do
'ip' => '127.0.0.1', 'ip' => '127.0.0.1',
'domain' => 'delivery.mycompany.net' 'domain' => 'delivery.mycompany.net'
}} }}
output = "- mcpy42eqjxli9g2.delivery.mycompany.net (ubuntu-1604-x86_64, 9.66/12 hours)" output = '- mcpy42eqjxli9g2.delivery.mycompany.net (ubuntu-1604-x86_64, 9.66/12 hours)'
expect(Utils).to receive(:puts).with(output) expect(Utils).to receive(:puts).with(output)
@ -192,7 +192,7 @@ describe Utils do
'ip' => '127.0.0.1', 'ip' => '127.0.0.1',
'domain' => 'delivery.mycompany.net' 'domain' => 'delivery.mycompany.net'
}} }}
output = "- aiydvzpg23r415q.delivery.mycompany.net (redhat-7-x86_64, 7.67/48 hours, user: bob, role: agent)" output = '- aiydvzpg23r415q.delivery.mycompany.net (redhat-7-x86_64, 7.67/48 hours, user: bob, role: agent)'
expect(Utils).to receive(:puts).with(output) expect(Utils).to receive(:puts).with(output)
@ -205,7 +205,7 @@ describe Utils do
end end
it 'prints a nonstandard pooler output with host, template, and time remaining' do it 'prints a nonstandard pooler output with host, template, and time remaining' do
hostname = "sol11-9.delivery.mycompany.net" hostname = 'sol11-9.delivery.mycompany.net'
response_body = { hostname => { response_body = { hostname => {
'fqdn' => hostname, 'fqdn' => hostname,
'os_triple' => 'solaris-11-sparc', 'os_triple' => 'solaris-11-sparc',
@ -213,7 +213,7 @@ describe Utils do
'reserved_for_reason' => '', 'reserved_for_reason' => '',
'hours_left_on_reservation' => 35.89 'hours_left_on_reservation' => 35.89
}} }}
output = "- sol11-9.delivery.mycompany.net (solaris-11-sparc, 35.89h remaining)" output = '- sol11-9.delivery.mycompany.net (solaris-11-sparc, 35.89h remaining)'
expect(Utils).to receive(:puts).with(output) expect(Utils).to receive(:puts).with(output)
@ -234,7 +234,7 @@ describe Utils do
'reserved_for_reason' => 'testing', 'reserved_for_reason' => 'testing',
'hours_left_on_reservation' => 35.89 'hours_left_on_reservation' => 35.89
}} }}
output = "- sol11-9.delivery.mycompany.net (solaris-11-sparc, 35.89h remaining, reason: testing)" output = '- sol11-9.delivery.mycompany.net (solaris-11-sparc, 35.89h remaining, reason: testing)'
expect(Utils).to receive(:puts).with(output) expect(Utils).to receive(:puts).with(output)