Merge branch 'master' of github.com:briancain/vmfloaty

This commit is contained in:
Brian Cain 2015-10-14 09:56:54 -07:00
commit 2153507098
8 changed files with 207 additions and 17 deletions

5
.travis.yml Normal file
View file

@ -0,0 +1,5 @@
sudo: false
language: ruby
rvm:
- 2.0.0-p247
script: rspec spec

View file

@ -5,5 +5,6 @@ gem 'faraday'
gem 'rspec' gem 'rspec'
gem 'webmock' gem 'webmock'
gem 'rake'
gemspec gemspec

View file

@ -2,6 +2,7 @@
require 'rubygems' require 'rubygems'
require 'commander' require 'commander'
require 'pp'
require 'vmfloaty/auth' require 'vmfloaty/auth'
require 'vmfloaty/pooler' require 'vmfloaty/pooler'
require 'vmfloaty/version' require 'vmfloaty/version'
@ -58,7 +59,7 @@ class Vmfloaty
unless os_types.nil? unless os_types.nil?
response = Pooler.retrieve(verbose, os_types, token, url) response = Pooler.retrieve(verbose, os_types, token, url)
puts response Format.get_hosts(response)
else else
puts 'You did not provide an OS to get' puts 'You did not provide an OS to get'
end end
@ -94,8 +95,8 @@ class Vmfloaty
url = options.url ||= config['url'] url = options.url ||= config['url']
hostname = args[0] hostname = args[0]
query = Pooler.query(verbose, url, hostname) query_req = Pooler.query(verbose, url, hostname)
puts query pp query_req
end end
end end
@ -117,8 +118,10 @@ class Vmfloaty
tags = JSON.parse(options.tags) if options.tags tags = JSON.parse(options.tags) if options.tags
token = options.token || config['token'] token = options.token || config['token']
res_body = Pooler.modify(verbose, url, hostname, token, lifetime, tags) modify_req = Pooler.modify(verbose, url, hostname, token, lifetime, tags)
puts res_body if modify_req["ok"]
puts "Successfully modified vm #{hostname}."
end
end end
end end
@ -154,8 +157,8 @@ class Vmfloaty
hostname = args[0] hostname = args[0]
token = options.token ||= config['token'] token = options.token ||= config['token']
res_body = Pooler.snapshot(verbose, url, hostname, token) snapshot_req = Pooler.snapshot(verbose, url, hostname, token)
puts res_body pp snapshot_req
end end
end end
@ -175,8 +178,8 @@ class Vmfloaty
token = options.token || config['token'] token = options.token || config['token']
snapshot_sha = options.snapshot snapshot_sha = options.snapshot
res_body = Pooler.revert(verbose, url, hostname, token, snapshot_sha) revert_req = Pooler.revert(verbose, url, hostname, token, snapshot_sha)
puts res_body pp revert_req
end end
end end
@ -192,7 +195,7 @@ class Vmfloaty
url = options.url ||= config['url'] url = options.url ||= config['url']
status = Pooler.status(verbose, url) status = Pooler.status(verbose, url)
puts status pp status
end end
end end
@ -208,7 +211,7 @@ class Vmfloaty
url = options.url ||= config['url'] url = options.url ||= config['url']
summary = Pooler.summary(verbose, url) summary = Pooler.summary(verbose, url)
puts summary pp summary
end end
end end

View file

@ -3,5 +3,13 @@ class Format
# TODO: Takes the json response body from an HTTP GET # TODO: Takes the json response body from an HTTP GET
# request and "pretty prints" it # request and "pretty prints" it
def self.get_hosts(hostname_hash) def self.get_hosts(hostname_hash)
host_hash = {}
hostname_hash.delete("ok")
hostname_hash.each do |type, hosts|
host_hash[type] = hosts["hostname"]
end
puts host_hash.to_json
end end
end end

View file

@ -41,7 +41,13 @@ class Pooler
response = conn.post "/vm/#{os_string}" response = conn.post "/vm/#{os_string}"
res_body = JSON.parse(response.body) res_body = JSON.parse(response.body)
res_body if res_body["ok"]
res_body
else
STDERR.puts "There was a problem with your request"
STDERR.puts res_body
exit 1
end
end end
def self.modify(verbose, url, hostname, token, lifetime, tags) def self.modify(verbose, url, hostname, token, lifetime, tags)
@ -127,7 +133,7 @@ class Pooler
conn = Http.get_conn(verbose, url) conn = Http.get_conn(verbose, url)
conn.headers['X-AUTH-TOKEN'] = token conn.headers['X-AUTH-TOKEN'] = token
response = conn.post "/vm/#{hostname}/snapshot/#{snapshot}" response = conn.post "/vm/#{hostname}/snapshot/#{snapshot_sha}"
res_body = JSON.parse(response.body) res_body = JSON.parse(response.body)
res_body res_body
end end

View file

@ -1,6 +1,6 @@
class Version class Version
@version = '0.2.7' @version = '0.2.10'
def self.get def self.get
@version @version

View file

@ -2,9 +2,176 @@ require 'spec_helper'
require_relative '../../lib/vmfloaty/pooler' require_relative '../../lib/vmfloaty/pooler'
describe Pooler do describe Pooler do
before :each do
@vmpooler_url = "https://vmpooler.example.com"
end
describe "#list" do describe "#list" do
it "fails to be a passing test" do before :each do
expect(true).to be false @list_response_body = "[\"debian-7-i386\",\"debian-7-x86_64\",\"centos-7-x86_64\"]"
end
it "returns a hash with operating systems from the pooler" do
stub_request(:get, "#{@vmpooler_url}/vm").
to_return(:status => 200, :body => @list_response_body, :headers => {})
list = Pooler.list(false, @vmpooler_url, nil)
expect(list).to be_an_instance_of Array
end
it "filters operating systems based on the filter param" do
stub_request(:get, "#{@vmpooler_url}/vm").
to_return(:status => 200, :body => @list_response_body, :headers => {})
list = Pooler.list(false, @vmpooler_url, "deb")
expect(list).to be_an_instance_of Array
expect(list.size).to equal 2
end
it "returns nothing if the filter does not match" do
stub_request(:get, "#{@vmpooler_url}/vm").
to_return(:status => 200, :body => @list_response_body, :headers => {})
list = Pooler.list(false, @vmpooler_url, "windows")
expect(list).to be_an_instance_of Array
expect(list.size).to equal 0
end
end
describe "#retrieve" do
before :each do
@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\"}}"
end
it "retrieves a single vm with a token" do
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.1', 'X-Auth-Token'=>'mytokenfile'}).
to_return(:status => 200, :body => @retrieve_response_body_single, :headers => {})
vm_hash = {}
vm_hash['debian-7-i386'] = 1
vm_req = Pooler.retrieve(false, vm_hash, 'mytokenfile', @vmpooler_url)
expect(vm_req).to be_an_instance_of Hash
expect(vm_req["ok"]).to equal true
expect(vm_req["debian-7-i386"]["hostname"]).to eq "fq6qlpjlsskycq6"
end
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").
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.1', 'X-Auth-Token'=>'mytokenfile'}).
to_return(:status => 200, :body => @retrieve_response_body_double, :headers => {})
vm_hash = {}
vm_hash['debian-7-i386'] = 2
vm_hash['centos-7-x86_64'] = 1
vm_req = Pooler.retrieve(false, vm_hash, 'mytokenfile', @vmpooler_url)
expect(vm_req).to be_an_instance_of Hash
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 eq ["sc0o4xqtodlul5w", "4m4dkhqiufnjmxy"]
expect(vm_req["centos-7-x86_64"]["hostname"]).to eq "zb91y9qbrbf6d3q"
end
end
describe "#modify" do
before :each do
@modify_response_body_success = "{\"ok\":true}"
@modify_response_body_fail = "{\"ok\":false}"
end
it "modifies the TTL of a vm" do
stub_request(:put, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6").
with(:body => {"lifetime"=>"12"},
: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.1', 'X-Auth-Token'=>'mytokenfile'}).
to_return(:status => 200, :body => @modify_response_body_success, :headers => {})
modify_req = Pooler.modify(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile', 12, nil)
expect(modify_req["ok"]).to be true
end
end
describe "#delete" do
before :each do
@delete_response_body_success = "{\"ok\":true}"
end
it "deletes a specified vm" do
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.1', 'X-Auth-Token'=>'mytokenfile'}).
to_return(:status => 200, :body => @delete_response_body_success, :headers => {})
#expect(Pooler.delete(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile')).to output(/Scheduling host fq6qlpjlsskycq6 for deletion/).to_stdout
end
end
describe "#staus" do
before :each do
#smaller version
@status_response_body = "{\"capacity\":{\"current\":716,\"total\":717,\"percent\": 99.9},\"status\":{\"ok\":true,\"message\":\"Battle station fully armed and operational.\"}}"
end
it "prints the status" do
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.1'}).
to_return(:status => 200, :body => @status_response_body, :headers => {})
status = Pooler.status(false, @vmpooler_url)
expect(status).to be_an_instance_of Hash
end
end
describe "#summary" do
before :each do
@status_response_body = ""
it "prints the summary" do
end
end
end
describe "#query" 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\"}}"
end
it "makes a query about a vm" do
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.1'}).
to_return(:status => 200, :body => @query_response_body, :headers => {})
query_req = Pooler.query(false, @vmpooler_url, 'fq6qlpjlsskycq6')
expect(query_req).to be_an_instance_of Hash
end
end
describe "#snapshot" do
before :each do
@snapshot_response_body = "{\"ok\":true}"
end
it "makes a snapshot for a single vm" do
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.1', 'X-Auth-Token'=>'mytokenfile'}).
to_return(:status => 200, :body => @snapshot_response_body, :headers => {})
snapshot_req = Pooler.snapshot(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile')
expect(snapshot_req["ok"]).to be true
end
end
describe "#revert" do
before :each do
@revert_response_body = "{\"ok\":true}"
end
it "makes a request to revert a vm from a snapshot" do
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.1', 'X-Auth-Token'=>'mytokenfile'}).
to_return(:status => 200, :body => @revert_response_body, :headers => {})
revert_req = Pooler.revert(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile', 'dAfewKNfaweLKNve')
expect(revert_req["ok"]).to be true
end end
end end
end end

View file

@ -1,6 +1,6 @@
Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = 'vmfloaty' s.name = 'vmfloaty'
s.version = '0.2.7' s.version = '0.2.10'
s.authors = ['Brian Cain'] s.authors = ['Brian Cain']
s.email = ['brian.cain@puppetlabs.com'] s.email = ['brian.cain@puppetlabs.com']
s.license = 'Apache' s.license = 'Apache'