diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..e10c011 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +sudo: false +language: ruby +rvm: + - 2.0.0-p247 +script: rspec spec diff --git a/Gemfile b/Gemfile index 9f00a6a..ad4b78f 100644 --- a/Gemfile +++ b/Gemfile @@ -5,5 +5,6 @@ gem 'faraday' gem 'rspec' gem 'webmock' +gem 'rake' gemspec diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index 3e19a52..b3e8487 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -2,6 +2,7 @@ require 'rubygems' require 'commander' +require 'pp' require 'vmfloaty/auth' require 'vmfloaty/pooler' require 'vmfloaty/version' @@ -58,7 +59,7 @@ class Vmfloaty unless os_types.nil? response = Pooler.retrieve(verbose, os_types, token, url) - puts response + Format.get_hosts(response) else puts 'You did not provide an OS to get' end @@ -94,8 +95,8 @@ class Vmfloaty url = options.url ||= config['url'] hostname = args[0] - query = Pooler.query(verbose, url, hostname) - puts query + query_req = Pooler.query(verbose, url, hostname) + pp query_req end end @@ -117,8 +118,10 @@ class Vmfloaty tags = JSON.parse(options.tags) if options.tags token = options.token || config['token'] - res_body = Pooler.modify(verbose, url, hostname, token, lifetime, tags) - puts res_body + modify_req = Pooler.modify(verbose, url, hostname, token, lifetime, tags) + if modify_req["ok"] + puts "Successfully modified vm #{hostname}." + end end end @@ -154,8 +157,8 @@ class Vmfloaty hostname = args[0] token = options.token ||= config['token'] - res_body = Pooler.snapshot(verbose, url, hostname, token) - puts res_body + snapshot_req = Pooler.snapshot(verbose, url, hostname, token) + pp snapshot_req end end @@ -175,8 +178,8 @@ class Vmfloaty token = options.token || config['token'] snapshot_sha = options.snapshot - res_body = Pooler.revert(verbose, url, hostname, token, snapshot_sha) - puts res_body + revert_req = Pooler.revert(verbose, url, hostname, token, snapshot_sha) + pp revert_req end end @@ -192,7 +195,7 @@ class Vmfloaty url = options.url ||= config['url'] status = Pooler.status(verbose, url) - puts status + pp status end end @@ -208,7 +211,7 @@ class Vmfloaty url = options.url ||= config['url'] summary = Pooler.summary(verbose, url) - puts summary + pp summary end end diff --git a/lib/vmfloaty/format.rb b/lib/vmfloaty/format.rb index f9726f6..42ec5c2 100644 --- a/lib/vmfloaty/format.rb +++ b/lib/vmfloaty/format.rb @@ -3,5 +3,13 @@ class Format # TODO: Takes the json response body from an HTTP GET # request and "pretty prints" it 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 diff --git a/lib/vmfloaty/pooler.rb b/lib/vmfloaty/pooler.rb index eb91772..15725dd 100644 --- a/lib/vmfloaty/pooler.rb +++ b/lib/vmfloaty/pooler.rb @@ -41,7 +41,13 @@ class Pooler response = conn.post "/vm/#{os_string}" 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 def self.modify(verbose, url, hostname, token, lifetime, tags) @@ -127,7 +133,7 @@ class Pooler conn = Http.get_conn(verbose, url) 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 end diff --git a/lib/vmfloaty/version.rb b/lib/vmfloaty/version.rb index cb841ee..654f74d 100644 --- a/lib/vmfloaty/version.rb +++ b/lib/vmfloaty/version.rb @@ -1,6 +1,6 @@ class Version - @version = '0.2.7' + @version = '0.2.10' def self.get @version diff --git a/spec/vmfloaty/pooler_spec.rb b/spec/vmfloaty/pooler_spec.rb index 4f5291a..8fe7545 100644 --- a/spec/vmfloaty/pooler_spec.rb +++ b/spec/vmfloaty/pooler_spec.rb @@ -2,9 +2,176 @@ require 'spec_helper' require_relative '../../lib/vmfloaty/pooler' describe Pooler do + before :each do + @vmpooler_url = "https://vmpooler.example.com" + end + describe "#list" do - it "fails to be a passing test" do - expect(true).to be false + before :each do + @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 diff --git a/vmfloaty.gemspec b/vmfloaty.gemspec index 2a6f877..3fce3e8 100644 --- a/vmfloaty.gemspec +++ b/vmfloaty.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'vmfloaty' - s.version = '0.2.7' + s.version = '0.2.10' s.authors = ['Brian Cain'] s.email = ['brian.cain@puppetlabs.com'] s.license = 'Apache'