Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge remote-tracking branch 'origin/master' into avoid_creating_conf…

…_file_during_spec_tests

Conflicts:
	spec/rhc/common_spec.rb
  • Loading branch information...
commit a630b1a8bcd0f46d38f86304f0720060118fa53a 2 parents c3bc992 + 600e73a
Clayton Coleman smarterclayton authored
1  .gitignore
View
@@ -3,3 +3,4 @@ pkg/
Gemfile.lock
coverage
*~
+.rvmrc
19 bin/rhc-chk
View
@@ -5,6 +5,7 @@ require 'rhc/coverage_helper'
require 'rhc-common'
require 'net/http'
require 'net/https'
+require 'net/ssh'
require 'rbconfig'
require 'yaml'
require 'tempfile'
@@ -357,18 +358,26 @@ class Test3_SSH < Test::Unit::TestCase
end
def agent_key_fingerprints
- @@agent_keys.nil? ? nil : @@agent_keys.map{|x| x.to_s.split("\n")[1..-2].join('') }
+ @@agent_keys.nil? ? nil : @@agent_keys.map{|x| x.fingerprint }
end
def libra_public_key
- @@local_ssh_pubkey ||= (
- fp = File.open(@libra_kpfile)
- fp.gets.split(' ')[1]
+ @@local_ssh_pubkey ||= libra_public_key_content.split(' ')[1]
+ end
+
+ def libra_public_key_content
+ @@local_ssh_pubkey_content ||= (
+ fp = File.open(@libra_kpfile)
+ fp.gets
)
ensure
fp.close if fp
end
+ def libra_public_key_fingerprint
+ Net::SSH::KeyFactory.load_data_public_key(libra_public_key_content).fingerprint
+ end
+
def test_01_local_files
[
[@libra_kfile, /[4-7]00/], # Needs to at least be readable by user and nobody else
@@ -381,7 +390,7 @@ class Test3_SSH < Test::Unit::TestCase
def test_02_ssh_agent
require_agent_keys
- assert agent_key_names.include?(File.expand_path(@libra_kfile)) ,error_for(:pubkey_not_loaded, ": #{@libra_kpfile}")
+ assert agent_key_fingerprints.include?(libra_public_key_fingerprint) ,error_for(:pubkey_not_loaded, ": #{@libra_kpfile}")
end
def test_03_remote_ssh_keys
17 client.spec
View
@@ -3,7 +3,7 @@
Summary: OpenShift client management tools
Name: rhc
-Version: 0.99.7
+Version: 0.99.9
Release: 1%{?dist}
Group: Network/Daemons
License: ASL 2.0
@@ -115,6 +115,21 @@ rm -rf $RPM_BUILD_ROOT
%attr(0644,-,-) /etc/bash_completion.d/rhc
%changelog
+* Thu Oct 18 2012 Adam Miller <admiller@redhat.com> 0.99.9-1
+- Merge pull request #186 from
+ smarterclayton/bug_821107_allow_unknown_ssh_keys_to_be_uploaded
+ (openshift+bot@redhat.com)
+- Bug 821107 - Allow an unrecognizable SSH key to be uploaded
+ (ccoleman@redhat.com)
+
+* Thu Oct 18 2012 Adam Miller <admiller@redhat.com> 0.99.8-1
+- These superfluous 'Accept' headers were throwing off specs on Linux.
+ (asari.ruby@gmail.com)
+- Tweak debug messages based on feedback. (asari.ruby@gmail.com)
+- Addresses US2853. Allows REST Client to negotiate with OpenShift server which
+ API version to use in order to communicate. (asari.ruby@gmail.com)
+- Looks like #179 needs specs to cover the added lines. (asari.ruby@gmail.com)
+
* Tue Oct 16 2012 Adam Miller <admiller@redhat.com> 0.99.7-1
- Merge pull request #179 from J5/bugfix (openshift+bot@redhat.com)
- Merge pull request #180 from J5/bugfix2 (dmcphers@redhat.com)
6 features/support/env.rb
View
@@ -63,6 +63,12 @@ def set_endpoint
raise "Username not found in environment (RHC_USERNAME)" unless $username
raise "Password not found in environment (RHC_PASSWORD)" unless $password
+$user_register_script_format = "/usr/bin/ss-register-user -l admin -p admin --username %s --userpass %s"
+if ENV['REGISTER_USER']
+ command = $user_register_script_format % [$username,$password]
+ %x[#{command}]
+end
+
def _log(msg)
puts msg unless ENV['QUIET']
end
8 lib/rhc/commands.rb
View
@@ -53,8 +53,8 @@ def self.deprecated?
command_name = Commander::Runner.instance.command_name_from_args
command = Commander::Runner.instance.active_command
- new_cmd = deprecated[command_name]
- if deprecated[command_name]
+ new_cmd = deprecated[command_name.to_sym]
+ if new_cmd
new_cmd = "rhc #{command.name}" if new_cmd == true
RHC::Helpers.deprecated_command new_cmd
end
@@ -88,7 +88,7 @@ def self.to_commander(instance=Commander::Runner.instance)
o[:arg] = Commander::Runner.switch_to_sym(o[:switches].last)
end
- deprecated[name] = opts[:deprecated] unless opts[:deprecated].nil?
+ deprecated[name.to_sym] = opts[:deprecated] unless opts[:deprecated].nil?
args_metadata = opts[:args] || []
args_metadata.each do |arg_meta|
@@ -107,7 +107,7 @@ def self.to_commander(instance=Commander::Runner.instance)
# prepend the current resource
alias_components = name.split(" ")
alias_components[-1] = a[:action]
- alias_cmd = alias_components.join(' ')
+ alias_cmd = alias_components.join(' ').to_sym
end
deprecated[alias_cmd] = true if a[:deprecated]
2  lib/rhc/commands/snapshot.rb
View
@@ -103,7 +103,7 @@ def restore(app)
end
rescue Timeout::Error, Errno::EADDRNOTAVAIL, Errno::EADDRINUSE, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Net::SSH::AuthenticationFailed => e
debug e.backtrace
- raise RHC::SnapshotRestoreException.new "Error in trying to restore snapshot. You can try to save manually by running:\n#{ssh_cmd}"
+ raise RHC::SnapshotRestoreException.new "Error in trying to restore snapshot. You can try to restore manually by running:\n#{ssh_cmd}"
end
else
10 lib/rhc/commands/sshkey.rb
View
@@ -35,9 +35,10 @@ def show(name)
end
summary 'Add SSH key to the user account'
- syntax '<name> <SSH Key file>'
+ syntax '<name> <path to SSH key file>'
argument :name, 'Name for this key', []
argument :key, 'SSH public key filepath', []
+ option ['--confirm'], 'Bypass key validation'
def add(name, key)
type, content, comment = ssh_key_triple_for(key)
@@ -45,7 +46,12 @@ def add(name, key)
begin
Net::SSH::KeyFactory.load_data_public_key "#{type} #{content}"
rescue NotImplementedError, OpenSSL::PKey::PKeyError, Net::SSH::Exception => e
- raise ::RHC::KeyDataInvalidException.new("File '#{key}' contains invalid data")
+ debug e.inspect
+ if options.confirm
+ warn 'The key you are uploading is not recognized. You may not be able to authenticate to your application through Git or SSH.'
+ else
+ raise ::RHC::KeyDataInvalidException.new("File '#{key}' does not appear to be a recognizable key file (#{e}). You may specify the '--confirm' flag to add the key anyway.")
+ end
end
rest_client.add_key(name, content, type)
62 lib/rhc/core_ext.rb
View
@@ -1,6 +1,7 @@
# From Rails core_ext/object.rb
require 'rhc/json'
require 'open-uri'
+require 'highline'
class Object
def present?
@@ -24,6 +25,15 @@ def chunk(chunk_size=1024)
end
end
+class String
+ # Wrap string by the given length, and join it with the given character.
+ # The method doesn't distinguish between words, it will only work based on
+ # the length.
+ def wrap(wrap_length=80, char="\n")
+ scan(/.{#{wrap_length}}|.+/).join(char)
+ end
+end
+
#
# Allow http => https redirection, see
# http://bugs.ruby-lang.org/issues/859 to 1.8.7 for rough
@@ -38,3 +48,55 @@ def self.redirectable?(uri1, uri2) # :nodoc:
(/\A(?:http|ftp)\z/i =~ uri1.scheme && /\A(?:https?|ftp)\z/i =~ uri2.scheme)
end
end
+
+# Some versions of highline get in an infinite loop when trying to wrap.
+# Fixes BZ 866530.
+class HighLine
+ def wrap(text)
+ wrapped_text = []
+ text.each_line do |line|
+ word = []
+ i = chars_in_line = 0
+ chars = line.split(//)
+ while i < chars.length do
+ c = chars[i]
+ color_code = nil
+ # escape character probably means color code, let's check
+ if c == "\e"
+ color_code = line[i..i+6].match(/\e\[\d{1,2}m/)
+ # it's a color code
+ if color_code
+ i += color_code[0].length
+ # first the existing word buffer then the color code
+ wrapped_text << word.join.wrap(@wrap_at) << color_code[0]
+ word.clear
+ end
+ end
+ # not a color code sequence
+ if !color_code
+ chars_in_line += 1
+ # time to wrap the line?
+ if chars_in_line == @wrap_at
+ wrapped_text.pop if wrapped_text.last =~ / /
+ wrapped_text << "\n"
+ chars_in_line = 0
+ end
+ # space, so move the word to wrapped buffer and start a new word
+ if c =~ / /
+ wrapped_text << word.join.wrap(@wrap_at) << ' '
+ word.clear
+ chars_in_line += 1
+ # any other character
+ else
+ word << c
+ end
+ i += 1
+ end
+ end
+ # moves the rest of the word buffer
+ wrapped_text << word.join.wrap(@wrap_at)
+ end
+ return wrapped_text.join
+ end
+
+end
75 lib/rhc/rest/client.rb
View
@@ -1,12 +1,22 @@
require 'base64'
require 'rhc/json'
require 'rhc/rest/base'
+require 'uri'
module RHC
module Rest
class Client < Base
- def initialize(end_point, username, password, use_debug=false)
+ attr_reader :server_api_versions, :client_api_versions
+ # Keep the list of supported API versions here
+ # The list may not necessarily be sorted; we will select the last
+ # matching one supported by the server.
+ # See #api_version_negotiated
+ CLIENT_API_VERSIONS = [1.0, 1.1, 1.2]
+
+ def initialize(end_point, username, password, use_debug=false, preferred_api_versions = CLIENT_API_VERSIONS)
@debug = use_debug
+ @end_point = end_point
+ @server_api_versions = []
debug "Connecting to #{end_point}"
credentials = nil
@@ -20,11 +30,37 @@ def initialize(end_point, username, password, use_debug=false)
# :nocov:
@@headers["Authorization"] = "Basic #{credentials}"
@@headers["User-Agent"] = RHC::Helpers.user_agent rescue nil
- #first get the API
RestClient.proxy = ENV['http_proxy']
- request = new_request(:url => end_point, :method => :get, :headers => @@headers)
-
- super({:links => request(request)}, use_debug)
+
+ # if API version negotiation is unsuccessful, execute this
+ default_request = new_request(:url => @end_point, :method => :get, :headers => @@headers)
+
+ # we'll be popping from preferred_api_versions in the while loop below
+ # so we need to dup the versions we prefer
+ @client_api_versions = preferred_api_versions.dup
+
+ begin
+ while !api_version_negotiated && !preferred_api_versions.empty?
+ api_version = preferred_api_versions.pop
+ debug "Checking API version #{api_version}"
+
+ @@headers["Accept"] = "application/json; version=#{api_version}"
+ request = new_request(:url => @end_point, :method => :get, :headers => @@headers)
+ begin
+ json_response = ::RHC::Json.decode(request.execute)
+ @server_api_versions = json_response['supported_api_versions']
+ links = json_response['data']
+ rescue RestClient::NotAcceptable
+ # try the next version
+ debug "Server does not support API version #{api_version}"
+ end
+ end
+ debug "Using API version #{api_version_negotiated}" if api_version_negotiated
+ warn_about_api_versions
+ rescue Exception => e
+ raise ResourceAccessException.new("Failed to access resource: #{e.message}")
+ end
+ super({:links => links || request(default_request)}, use_debug)
end
def add_domain(id)
@@ -118,6 +154,35 @@ def logout
debug "Logout/Close client"
end
alias :close :logout
+
+
+ ### API version related methods
+ def api_version_match?
+ ! api_version_negotiated.nil?
+ end
+
+ # return the API version that the server and this client agreed on
+ def api_version_negotiated
+ return nil unless @server_api_versions
+ client_api_versions.reverse. # choose the last API version listed
+ detect { |v| @server_api_versions.include? v }
+ end
+
+ def api_version_current?
+ current_client_api_version == api_version_negotiated
+ end
+
+ def current_client_api_version
+ client_api_versions.last
+ end
+
+ def warn_about_api_versions
+ if !api_version_match?
+ # API versions did not match
+ warn "WARNING: API version mismatch. This client supports #{client_api_versions.join(', ')}
+but server at #{URI.parse(@end_point).host} supports #{@server_api_versions.join(', ')}."
+ end
+ end
end
end
end
2  lib/rhc/version.rb
View
@@ -2,7 +2,7 @@ module RHC
module VERSION #:nocov:
MAJOR = 0
MINOR = 99
- MICRO = 7
+ MICRO = 9
#PRE = ''
STRING = [MAJOR,MINOR,MICRO].compact.join('.')
end
8 lib/rhc/wizard.rb
View
@@ -34,6 +34,7 @@ def initialize(config, opts=nil)
@libra_server = @libra_server ? @libra_server : "openshift.redhat.com"
end
@config.config_user opts.rhlogin if opts && opts.rhlogin
+ @debug = config['debug']
end
# Public: Runs the setup wizard to make sure ~/.openshift and ~/.ssh are correct
@@ -47,6 +48,7 @@ def initialize(config, opts=nil)
def run
stages.each do |stage|
# FIXME: cleanup if we fail
+ debug "Running #{stage}"
if (self.send stage).nil?
return nil
end
@@ -94,7 +96,7 @@ def login_stage
# instantiate a REST client that stages can use
end_point = "https://#{@libra_server}/broker/rest/api"
- @rest_client = RHC::Rest::Client.new(end_point, @username, @password)
+ @rest_client = RHC::Rest::Client.new(end_point, @username, @password, @debug)
# confirm that the REST client can connect
return false unless @rest_client.user
@@ -430,6 +432,10 @@ def has_git?
rescue
false
end
+
+ def debug?
+ @debug
+ end
end
class RerunWizard < Wizard
2  rel-eng/packages/rhc
View
@@ -1 +1 @@
-0.99.7-1 /
+0.99.9-1 /
8 spec/rhc/commands/alias_spec.rb
View
@@ -11,7 +11,7 @@
RHC::Config.set_defaults
@rc = MockRestClient.new
@rc.add_domain("mock_domain_0").add_application("mock_app_0", "ruby-1.8.7")
- stub_api_request(:any, client_links['LIST_DOMAINS']['relative']).with(:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip, deflate'}).
+ stub_api_request(:any, client_links['LIST_DOMAINS']['relative']).with(:headers => {'Accept-Encoding'=>'gzip, deflate'}).
to_return({ :body => {
:type => 'domains',
:data =>
@@ -24,7 +24,7 @@
}.to_json,
:status => 200
})
- stub_api_request(:any, domain_0_links['LIST_APPLICATIONS']['relative']).with(:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip, deflate'}).
+ stub_api_request(:any, domain_0_links['LIST_APPLICATIONS']['relative']).with(:headers => {'Accept-Encoding'=>'gzip, deflate'}).
to_return({ :body => {
:type => 'applications',
:data =>
@@ -39,7 +39,7 @@
}.to_json,
:status => 200
})
- stub_api_request(:any, app_0_links['ADD_ALIAS']['relative']).with(:body => {:event => 'add-alias', :alias => 'www.foo.bar'}, :headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>'33', 'Content-Type'=>'application/x-www-form-urlencoded'}).
+ stub_api_request(:any, app_0_links['ADD_ALIAS']['relative']).with(:body => {:event => 'add-alias', :alias => 'www.foo.bar'}, :headers => {'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>'33', 'Content-Type'=>'application/x-www-form-urlencoded'}).
to_return({ :body => {
:type => 'application',
:data =>
@@ -55,7 +55,7 @@
}.to_json,
:status => 200
})
-stub_api_request(:any, app_0_links['REMOVE_ALIAS']['relative']).with(:body => {:event => 'remove-alias', :alias => 'www.foo.bar'}, :headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>'36', 'Content-Type'=>'application/x-www-form-urlencoded'}).
+stub_api_request(:any, app_0_links['REMOVE_ALIAS']['relative']).with(:body => {:event => 'remove-alias', :alias => 'www.foo.bar'}, :headers => {'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>'36', 'Content-Type'=>'application/x-www-form-urlencoded'}).
to_return({ :body => {
:type => 'application',
:data =>
30 spec/rhc/commands/sshkey_spec.rb
View
@@ -58,14 +58,14 @@
end
end
end
-
+
context "when adding an invalid key" do
let(:arguments) { %w[sshkey add --noprompt --config test.conf -l test@test.foo -p password foobar id_rsa.pub] }
-
+
before :each do
@rc = MockRestClient.new
end
-
+
it "fails to add the key" do
FakeFS do
keys = @rc.sshkeys
@@ -74,11 +74,33 @@
f << 'ssh-rsa AADAQABAAABAQCnCOqK7/mmvZ9AtCAerxjAasJ1rSpfuWT4vNm1+O/Fh0Di3chTWjY9a0M2hEnqkqnVG589L9CqCUeT0kdc3Vgw3JEcacSUr1z7tLr9kO+p/D5lSdQYzDGGRFOZ0H6lc/y8iNxWV1VO/sJvKx6cr5zvKIn8Q6GvhVNOxlai0IOb9FJxLGK95GLpZ+elzh8Tc9giy7KfwheAwhV2JoF9uRltE5JP/CNs7w/E29i1Z+jlueuu8RVotLmhSVNJm91Ey7OCtoI1iBE0Wv/SucFe32Qi08RWTM/MaGGz93KQNOVRGjNkosJjPmP1qU6WGBfliDkJAZXB0b6sEcnx1fbVikwZ'
end
expect { run }.should exit_with_code(128)
+ expect { run_output.should match("Name: mockkey") }
@rc.sshkeys.length.should == num_keys
end
end
end
+ context "when adding an invalid key with --confirm" do
+ let(:arguments) { %w[sshkey add --noprompt --confirm --config test.conf -l test@test.foo -p password foobar id_rsa.pub] }
+
+ before :each do
+ @rc = MockRestClient.new
+ end
+
+ it "warns and then adds the key" do
+ FakeFS do
+ keys = @rc.sshkeys
+ num_keys = keys.length
+ File.open('id_rsa.pub', 'w') do |f|
+ f << 'ssh-rsa AADAQABAAABAQCnCOqK7/mmvZ9AtCAerxjAasJ1rSpfuWT4vNm1+O/Fh0Di3chTWjY9a0M2hEnqkqnVG589L9CqCUeT0kdc3Vgw3JEcacSUr1z7tLr9kO+p/D5lSdQYzDGGRFOZ0H6lc/y8iNxWV1VO/sJvKx6cr5zvKIn8Q6GvhVNOxlai0IOb9FJxLGK95GLpZ+elzh8Tc9giy7KfwheAwhV2JoF9uRltE5JP/CNs7w/E29i1Z+jlueuu8RVotLmhSVNJm91Ey7OCtoI1iBE0Wv/SucFe32Qi08RWTM/MaGGz93KQNOVRGjNkosJjPmP1qU6WGBfliDkJAZXB0b6sEcnx1fbVikwZ'
+ end
+ expect { run }.should exit_with_code(0)
+ expect { run_output.should match("key you are uploading is not recognized") }
+ @rc.sshkeys.length.should == num_keys + 1
+ end
+ end
+ end
+
context "when adding a nonexistent key" do
let(:arguments) { %w[sshkey add --noprompt --config test.conf -l test@test.foo -p password foobar id_rsa.pub] }
@@ -138,4 +160,4 @@
end
end
end
-end
+end
6 spec/rhc/commands/threaddump_spec.rb
View
@@ -11,7 +11,7 @@
RHC::Config.set_defaults
@rc = MockRestClient.new
@rc.add_domain("mock_domain_0").add_application("mock_app_0", "ruby-1.8.7")
- stub_api_request(:any, client_links['LIST_DOMAINS']['relative']).with(:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip, deflate'}).
+ stub_api_request(:any, client_links['LIST_DOMAINS']['relative']).with(:headers => {'Accept-Encoding'=>'gzip, deflate'}).
to_return({ :body => {
:type => 'domains',
:data =>
@@ -24,7 +24,7 @@
}.to_json,
:status => 200
})
- stub_api_request(:any, domain_0_links['LIST_APPLICATIONS']['relative']).with(:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip, deflate'}).
+ stub_api_request(:any, domain_0_links['LIST_APPLICATIONS']['relative']).with(:headers => {'Accept-Encoding'=>'gzip, deflate'}).
to_return({ :body => {
:type => 'applications',
:data =>
@@ -39,7 +39,7 @@
}.to_json,
:status => 200
})
- stub_api_request(:any, app_0_links['THREAD_DUMP']['relative']).with(:body => {:event => 'thread-dump'}, :headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>'17', 'Content-Type'=>'application/x-www-form-urlencoded'}).
+ stub_api_request(:any, app_0_links['THREAD_DUMP']['relative']).with(:body => {:event => 'thread-dump'}, :headers => {'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>'17', 'Content-Type'=>'application/x-www-form-urlencoded'}).
to_return({ :body => {
:type => 'application',
:data =>
40 spec/rhc/helpers_spec.rb
View
@@ -4,6 +4,7 @@
require 'rhc/core_ext'
require 'highline/import'
require 'rhc/config'
+require 'rhc/helpers'
require 'date'
describe RHC::Helpers do
@@ -248,3 +249,42 @@ def reset
specify('https to http') { OpenURI.redirectable?(URI.parse('https://foo.com'), URI.parse('http://foo.com')).should be_false }
end
end
+
+describe HighLine do
+ it "should wrap the terminal" do
+ $terminal.wrap_at = 10
+ say "Lorem ipsum dolor sit amet"
+ output = $terminal.read
+ output.should match "Lorem\nipsum\ndolor sit\namet"
+ end
+ it "should wrap the terminal" do
+ $terminal.wrap_at = 16
+ say "Lorem ipsum dolor sit amet"
+ output = $terminal.read
+ output.should match "Lorem ipsum\ndolor sit amet"
+ end
+ it "should not wrap the terminal" do
+ $terminal.wrap_at = 50
+ say "Lorem ipsum dolor sit amet"
+ output = $terminal.read
+ output.should match "Lorem ipsum dolor sit amet"
+ end
+ it "should wrap the terminal when using color codes" do
+ $terminal.wrap_at = 10
+ say $terminal.color("Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet", :red)
+ output = $terminal.read
+ output.should match "Lorem\nipsum\ndolor sit\namet Lorem\nipsum\ndolor sit\namet"
+ end
+ it "should wrap the terminal with other escape characters" do
+ $terminal.wrap_at = 10
+ say "Lorem ipsum dolor sit am\eet"
+ output = $terminal.read
+ output.should match "Lorem\nipsum\ndolor sit\nam\eet"
+ end
+ it "should wrap the terminal when words are smaller than wrap length" do
+ $terminal.wrap_at = 3
+ say "Antidisestablishmentarianism"
+ output = $terminal.read
+ output.should match "Ant\nidi\nses\ntab\nlis\nhme\nnta\nria\nnis\nm"
+ end
+end
98 spec/rhc/rest_client_spec.rb
View
@@ -35,7 +35,7 @@ module Rest
context "#new" do
before do
stub_api_request(:get, '').
- to_return({ :body => { :data => client_links }.to_json,
+ to_return({ :body => { :data => client_links, :supported_api_versions => [1.0, 1.1] }.to_json,
:status => 200
})
stub_api_request(:get, 'api_error').
@@ -64,11 +64,65 @@ module Rest
should raise_error("Failed to access resource: Other Error")
end
end
+
+ describe "#new" do
+ context "when server supports API versions [1.0, 1.1]" do
+ before :each do
+ stub_api_request(:get, '').
+ to_return({ :body => { :data => client_links, :supported_api_versions => [1.0, 1.1] }.to_json,
+ :status => 200
+ })
+ stub_api_request(:get, 'api_error').
+ to_raise(RestClient::ExceptionWithResponse.new('API Error'))
+ stub_api_request(:get, 'other_error').
+ to_raise(Exception.new('Other Error'))
+ end
+
+ context "when client is instantiated with [1.0, 1.1] as the preferred API versions" do
+ before :each do
+ @client = RHC::Rest::Client.new(mock_href, mock_user, mock_pass, false, [1.0, 1.1])
+ end
+ it "settles on 1.1 as the API version" do
+ @client.api_version_negotiated.should == 1.1
+ end
+ end
+
+ context "when client is instantiated with [1.1, 1.0] as the preferred API versions" do
+ before :each do
+ @client = RHC::Rest::Client.new(mock_href, mock_user, mock_pass, false, [1.1, 1.0])
+ end
+ it "settles on 1.0 as the API version" do
+ @client.api_version_negotiated.should == 1.0
+ end
+ end
+
+ context "when client is instantiated with [1.2, 1.3] as the preferred API versions" do
+ before :each do
+ @client = RHC::Rest::Client.new(mock_href, mock_user, mock_pass, false, [1.2, 1.3])
+ end
+ it "fails to negotiate an agreeable API version" do
+ @client.api_version_negotiated.should be_nil
+ end
+ end
+
+ context "when client is instantiated with [1.1, 1.0, 1.3] as the preferred API versions" do
+ before :each do
+ @client = RHC::Rest::Client.new(mock_href, mock_user, mock_pass, false, [1.1, 1.0, 1.3])
+ end
+ it "settles on 1.0 as the API version" do
+ @client.api_version_negotiated.should == 1.0
+ end
+ end
+ end
+ end
context "with an instantiated client " do
before(:each) do
stub_api_request(:get, '').
- to_return({ :body => { :data => client_links }.to_json,
+ to_return({ :body => {
+ :data => client_links,
+ :supported_api_versions => [1.0, 1.1]
+ }.to_json,
:status => 200
})
@client = RHC::Rest::Client.new(mock_href, mock_user, mock_pass)
@@ -79,6 +133,7 @@ module Rest
stub_api_request(:any, client_links['ADD_DOMAIN']['relative']).
to_return({ :body => {
:type => 'domain',
+ :supported_api_versions => [1.0, 1.1],
:data => {
:id => 'mock_domain',
:links => mock_response_links(mock_domain_links('mock_domain')),
@@ -381,7 +436,7 @@ module Rest
shared_examples_for "a logout method" do
before(:each) do
stub_api_request(:get, '').
- to_return({ :body => { :data => client_links }.to_json,
+ to_return({ :body => { :data => client_links, :supported_api_versions => [1.0, 1.1] }.to_json,
:status => 200
})
@client = MockClient.new(mock_href, mock_user, mock_pass)
@@ -462,6 +517,43 @@ module Rest
it_should_behave_like "a logout method"
end
end
+
+ context "when server supports API versions 1.0 and 1.1" do
+ before :each do
+ stub_api_request(:get, '').
+ to_return({ :body => {
+ :data => client_links,
+ :supported_api_versions => [1.0, 1.1]
+ }.to_json,
+ :status => 200
+ })
+ end
+
+ context "when client supports API version 1.1" do
+ before :each do
+ @client = RHC::Rest::Client.new(mock_href, mock_user, mock_pass, false, [1.1])
+ end
+
+ describe "#api_version_negotiated" do
+ it "returns 1.1" do
+ @client.api_version_negotiated.to_s.should == '1.1'
+ end
+ end
+ end
+
+ context "when client supports only API version 1.2" do
+ before :each do
+ @client = RHC::Rest::Client.new(mock_href, mock_user, mock_pass, false, [1.2])
+ end
+
+ describe "#api_version_negotiated" do
+ it 'returns nil' do
+ @client.api_version_negotiated.should be_nil
+ end
+ end
+ end
+ end
+
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.