~ubuntu-branches/ubuntu/quantal/ruby-vmc/quantal-201206142105

« back to all changes in this revision

Viewing changes to spec/unit/client_spec.rb

  • Committer: Michael Terry
  • Date: 2012-06-14 17:21:18 UTC
  • mfrom: (5.1.1 ruby-vmc)
  • Revision ID: michael.terry@canonical.com-20120614172118-8m3f2k7ugyki6sir
New upstream release (LP: #998111).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require 'spec_helper'
2
 
 
3
 
describe 'VMC::Client' do
4
 
  include WebMock::API
5
 
 
6
 
  before(:all) do
7
 
    @target = VMC::DEFAULT_TARGET
8
 
    @local_target = VMC::DEFAULT_LOCAL_TARGET
9
 
    @user = 'derek@gmail.com'
10
 
    @password = 'foo'
11
 
    @auth_token = spec_asset('sample_token.txt')
12
 
  end
13
 
 
14
 
  it 'should report its version' do
15
 
    VMC::Client.version.should =~ /\d.\d.\d/
16
 
  end
17
 
 
18
 
  it 'should default to local target' do
19
 
    client = VMC::Client.new
20
 
    client.target.should == VMC::DEFAULT_TARGET
21
 
  end
22
 
 
23
 
  it 'should normalize target with no scheme' do
24
 
    client = VMC::Client.new('api.cloudfoundry.com')
25
 
    client.target.should == @target
26
 
  end
27
 
 
28
 
  it 'should properly initialize with auth_token' do
29
 
    client = VMC::Client.new(@target, @auth_token)
30
 
    client.target.should     == @target
31
 
    client.auth_token.should == @auth_token
32
 
  end
33
 
 
34
 
  it 'should allow login correctly and return an auth_token' do
35
 
    login_path = "#{@local_target}/users/#{@user}/tokens"
36
 
    stub_request(:post, login_path).to_return(File.new(spec_asset('login_success.txt')))
37
 
    client = VMC::Client.new(@local_target)
38
 
    auth_token = client.login(@user, @password)
39
 
    client.target.should == @local_target
40
 
    client.user.should == @user
41
 
    client.auth_token.should be
42
 
    auth_token.should be
43
 
    auth_token.should == client.auth_token
44
 
  end
45
 
 
46
 
  it 'should raise exception if login fails' do
47
 
    login_path = "#{@local_target}/users/#{@user}/tokens"
48
 
    stub_request(:post, login_path).to_return(File.new(spec_asset('login_fail.txt')))
49
 
    client = VMC::Client.new(@local_target)
50
 
    expect { client.login(@user, @password) }.to raise_error(VMC::Client::TargetError)
51
 
  end
52
 
 
53
 
  it 'should allow admin users to proxy for others' do
54
 
    proxy = 'vadim@gmail.com'
55
 
    client = VMC::Client.new(@target)
56
 
    client.proxy_for(proxy)
57
 
    client.proxy.should == proxy
58
 
  end
59
 
 
60
 
  it 'should properly get info for valid target cloud' do
61
 
    info_path = "#{@local_target}#{VMC::INFO_PATH}"
62
 
    stub_request(:get, info_path).to_return(File.new(spec_asset('info_return.txt')))
63
 
    client = VMC::Client.new(@local_target)
64
 
    info = client.info
65
 
    a_request(:get, info_path).should have_been_made.once
66
 
    info.should have_key :support
67
 
    info.should have_key :description
68
 
    info.should have_key :name
69
 
    info.should have_key :version
70
 
    info.should have_key :build
71
 
  end
72
 
 
73
 
  it 'should raise and exception for a bad target' do
74
 
    info_path = "#{@local_target}#{VMC::INFO_PATH}"
75
 
    stub_request(:get, info_path).to_return(File.new(spec_asset('info_return_bad.txt')))
76
 
    client = VMC::Client.new(@local_target)
77
 
    expect {info = client.info}.to raise_error(VMC::Client::BadResponse)
78
 
    a_request(:get, info_path).should have_been_made.once
79
 
  end
80
 
 
81
 
  it 'should have target_valid? return true for a good target' do
82
 
    info_path = "#{@local_target}#{VMC::INFO_PATH}"
83
 
    stub_request(:get, info_path).to_return(File.new(spec_asset('info_return.txt')))
84
 
    client = VMC::Client.new(@local_target)
85
 
    client.target_valid?.should be_true
86
 
  end
87
 
 
88
 
  it 'should have target_valid? return false for a bad target' do
89
 
    info_path = "#{@local_target}#{VMC::INFO_PATH}"
90
 
    stub_request(:get, info_path).to_return(File.new(spec_asset('info_return_bad.txt')))
91
 
    client = VMC::Client.new(@local_target)
92
 
    client.target_valid?.should be_false
93
 
  end
94
 
 
95
 
  it 'should respond ok if properly logged in' do
96
 
    login_path = "#{@local_target}/users/#{@user}/tokens"
97
 
    stub_request(:post, login_path).to_return(File.new(spec_asset('login_success.txt')))
98
 
    info_path = "#{@local_target}#{VMC::INFO_PATH}"
99
 
    stub_request(:get, info_path).to_return(File.new(spec_asset('info_authenticated.txt')))
100
 
    client = VMC::Client.new(@local_target)
101
 
    client.login(@user, @password)
102
 
    client.logged_in?.should be_true
103
 
  end
104
 
 
105
 
  it 'should fail when trying to change password unless logged in' do
106
 
    login_path = "#{@local_target}/users/#{@user}/tokens"
107
 
    stub_request(:post, login_path).to_return(File.new(spec_asset('login_success.txt')))
108
 
    user_info_path = "#{@local_target}/users/#{@user}"
109
 
    stub_request(:get, user_info_path).to_return(File.new(spec_asset('user_info.txt')))
110
 
    stub_request(:put, user_info_path)
111
 
    client = VMC::Client.new(@local_target)
112
 
    client.login(@user, @password)
113
 
    client.change_password('bar')
114
 
  end
115
 
 
116
 
  it 'should get a proper list of apps' do
117
 
    info_path = "#{@local_target}#{VMC::INFO_PATH}"
118
 
    stub_request(:get, info_path).to_return(File.new(spec_asset('info_authenticated.txt')))
119
 
    apps_path = "#{@local_target}#{VMC::APPS_PATH}"
120
 
    stub_request(:get, apps_path).to_return(File.new(spec_asset('app_listings.txt')))
121
 
    client = VMC::Client.new(@local_target, @auth_token)
122
 
    apps = client.apps
123
 
    apps.should have(1).items
124
 
    app = apps.first
125
 
    app.should have_key :state
126
 
    app.should have_key :uris
127
 
    app.should have_key :name
128
 
    app.should have_key :services
129
 
    app.should have_key :instances
130
 
  end
131
 
 
132
 
  it 'should get a proper list of services' do
133
 
    info_path = "#{@local_target}#{VMC::INFO_PATH}"
134
 
    stub_request(:get, info_path).to_return(File.new(spec_asset('info_authenticated.txt')))
135
 
    services_path = "#{@local_target}#{VMC::GLOBAL_SERVICES_PATH}"
136
 
    stub_request(:get, services_path).to_return(File.new(spec_asset('global_service_listings.txt')))
137
 
    client = VMC::Client.new(@local_target, @auth_token)
138
 
    services = client.services_info
139
 
    services.should have(2).items
140
 
    # FIXME, add in more details.
141
 
  end
142
 
 
143
 
  it 'should get a proper list of provisioned services' do
144
 
    info_path = "#{@local_target}#{VMC::INFO_PATH}"
145
 
    stub_request(:get, info_path).to_return(File.new(spec_asset('info_authenticated.txt')))
146
 
    services_path = "#{@local_target}#{VMC::SERVICES_PATH}"
147
 
    stub_request(:get, services_path).to_return(File.new(spec_asset('service_listings.txt')))
148
 
    client = VMC::Client.new(@local_target, @auth_token)
149
 
    app_services = client.services
150
 
    app_services.should have(1).items
151
 
    redis = app_services.first
152
 
    redis.should have_key :type
153
 
    redis.should have_key :vendor
154
 
  end
155
 
 
156
 
  it 'should raise when trying to create an app with no manifest' do
157
 
    info_path = "#{@local_target}#{VMC::INFO_PATH}"
158
 
    stub_request(:get, info_path).to_return(File.new(spec_asset('info_authenticated.txt')))
159
 
    app_path = "#{@local_target}#{VMC::APPS_PATH}"
160
 
    stub_request(:post, app_path).to_return(File.new(spec_asset('bad_create_app.txt')))
161
 
    client = VMC::Client.new(@local_target, @auth_token)
162
 
    expect { client.create_app('foo') }.to  raise_error(VMC::Client::NotFound)
163
 
  end
164
 
 
165
 
  it 'should create an app with a simple manifest' do
166
 
    info_path = "#{@local_target}#{VMC::INFO_PATH}"
167
 
    stub_request(:get, info_path).to_return(File.new(spec_asset('info_authenticated.txt')))
168
 
    app_path = "#{@local_target}#{VMC::APPS_PATH}"
169
 
    stub_request(:post, app_path).to_return(File.new(spec_asset('good_create_app.txt')))
170
 
    client = VMC::Client.new(@local_target, @auth_token)
171
 
    manifest = {
172
 
      :name => 'foo',
173
 
      :uris => ['foo.vcap.me'],
174
 
      :instances => 1,
175
 
      :staging => { :model => 'nodejs/1.0' },
176
 
      :resources => { :memory => 64 }
177
 
    }
178
 
    client.create_app('foo', manifest)
179
 
  end
180
 
 
181
 
  it 'should allow us to delete an app we created' do
182
 
    info_path = "#{@local_target}#{VMC::INFO_PATH}"
183
 
    stub_request(:get, info_path).to_return(File.new(spec_asset('info_authenticated.txt')))
184
 
    app_path = "#{@local_target}#{VMC::APPS_PATH}/foo"
185
 
    stub_request(:delete, app_path).to_return(File.new(spec_asset('delete_app.txt')))
186
 
    client = VMC::Client.new(@local_target, @auth_token)
187
 
    client.delete_app('foo')
188
 
  end
189
 
 
190
 
  it 'should provision a service' do
191
 
    info_path = "#{@local_target}#{VMC::INFO_PATH}"
192
 
    stub_request(:get, info_path).to_return(File.new(spec_asset('info_authenticated.txt')))
193
 
    global_services_path = "#{@local_target}#{VMC::GLOBAL_SERVICES_PATH}"
194
 
    stub_request(:get, global_services_path).to_return(File.new(spec_asset('global_service_listings.txt')))
195
 
    services_path = "#{@local_target}#{VMC::SERVICES_PATH}"
196
 
    stub_request(:post, services_path).to_return(File.new(spec_asset('good_create_service.txt')))
197
 
    client = VMC::Client.new(@local_target, @auth_token)
198
 
    client.create_service('redis', 'foo')
199
 
  end
200
 
 
201
 
  it 'should complain if we try to provision a service that already exists with same name' do
202
 
    info_path = "#{@local_target}#{VMC::INFO_PATH}"
203
 
    stub_request(:get, info_path).to_return(File.new(spec_asset('info_authenticated.txt')))
204
 
    global_services_path = "#{@local_target}#{VMC::GLOBAL_SERVICES_PATH}"
205
 
    stub_request(:get, global_services_path).to_return(File.new(spec_asset('global_service_listings.txt')))
206
 
    services_path = "#{@local_target}#{VMC::SERVICES_PATH}"
207
 
    stub_request(:post, services_path).to_return(File.new(spec_asset('service_already_exists.txt')))
208
 
    client = VMC::Client.new(@local_target, @auth_token)
209
 
    expect { client.create_service('redis', 'foo') }.to raise_error(VMC::Client::NotFound)
210
 
  end
211
 
 
212
 
  it 'should complain if we try to provision a service that does not exist' do
213
 
    info_path = "#{@local_target}#{VMC::INFO_PATH}"
214
 
    stub_request(:get, info_path).to_return(File.new(spec_asset('info_authenticated.txt')))
215
 
    global_services_path = "#{@local_target}#{VMC::GLOBAL_SERVICES_PATH}"
216
 
    stub_request(:get, global_services_path).to_return(File.new(spec_asset('global_service_listings.txt')))
217
 
    services_path = "#{@local_target}#{VMC::SERVICES_PATH}"
218
 
    stub_request(:post, services_path).to_return(File.new(spec_asset('service_not_found.txt')))
219
 
    client = VMC::Client.new(@local_target, @auth_token)
220
 
    expect { client.create_service('redis', 'foo') }.to raise_error(VMC::Client::NotFound)
221
 
  end
222
 
 
223
 
  it 'should allow us to delete a provisioned service' do
224
 
    info_path = "#{@local_target}#{VMC::INFO_PATH}"
225
 
    stub_request(:get, info_path).to_return(File.new(spec_asset('info_authenticated.txt')))
226
 
    services_path = "#{@local_target}#{VMC::SERVICES_PATH}"
227
 
    stub_request(:get, services_path).to_return(File.new(spec_asset('service_listings.txt')))
228
 
    services_path = "#{@local_target}#{VMC::SERVICES_PATH}/redis-7ed7da9"
229
 
    stub_request(:delete, services_path)
230
 
    client = VMC::Client.new(@local_target, @auth_token)
231
 
    client.delete_service('redis-7ed7da9')
232
 
  end
233
 
 
234
 
  it 'should bind a service to an app' do
235
 
    info_path = "#{@local_target}#{VMC::INFO_PATH}"
236
 
    stub_request(:get, info_path).to_return(File.new(spec_asset('info_authenticated.txt')))
237
 
    app_path = "#{@local_target}#{VMC::APPS_PATH}/foo"
238
 
    stub_request(:get, app_path).to_return(File.new(spec_asset('app_info.txt')))
239
 
    stub_request(:put, app_path)
240
 
    client = VMC::Client.new(@local_target, @auth_token)
241
 
    client.bind_service('my-redis', 'foo')
242
 
    a_request(:get, app_path).should have_been_made.once
243
 
    a_request(:put, app_path).should have_been_made.once
244
 
  end
245
 
 
246
 
  it 'should unbind an existing service from an app' do
247
 
    info_path = "#{@local_target}#{VMC::INFO_PATH}"
248
 
    stub_request(:get, info_path).to_return(File.new(spec_asset('info_authenticated.txt')))
249
 
    app_path = "#{@local_target}#{VMC::APPS_PATH}/foo"
250
 
    stub_request(:get, app_path).to_return(File.new(spec_asset('app_info.txt')))
251
 
    stub_request(:put, app_path)
252
 
    client = VMC::Client.new(@local_target, @auth_token)
253
 
    client.unbind_service('my-redis', 'foo')
254
 
    a_request(:get, app_path).should have_been_made.once
255
 
    a_request(:put, app_path).should have_been_made.once
256
 
  end
257
 
 
258
 
  it 'should set a proxy if one is set' do
259
 
    info_path = "#{@local_target}#{VMC::INFO_PATH}"
260
 
    stub_request(:get, info_path).to_return(File.new(spec_asset('info_return.txt')))
261
 
    proxy = 'http://proxy.vmware.com:3128'
262
 
    ENV['http_proxy'] = proxy
263
 
    client = VMC::Client.new(@local_target)
264
 
    info = client.info
265
 
    RestClient.proxy.should == proxy
266
 
    ENV['http_proxy'] = nil
267
 
  end
268
 
 
269
 
  it 'should set a secure proxy over a normal proxy if one is set' do
270
 
    info_path = "#{@local_target}#{VMC::INFO_PATH}"
271
 
    stub_request(:get, info_path).to_return(File.new(spec_asset('info_return.txt')))
272
 
    proxy = 'http://proxy.vmware.com:3128'
273
 
    secure_proxy = 'https://proxy.vmware.com:3128'
274
 
    ENV['http_proxy'] = proxy
275
 
    ENV['https_proxy'] = secure_proxy
276
 
    client = VMC::Client.new(@local_target)
277
 
    info = client.info
278
 
    RestClient.proxy.should == secure_proxy
279
 
    ENV['http_proxy'] = ENV['https_proxy'] = nil
280
 
  end
281
 
 
282
 
  # WebMock.allow_net_connect!
283
 
 
284
 
end