deltacloud-core 0.1.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. data/DISCLAIMER +8 -0
  2. data/{COPYING → LICENSE} +0 -0
  3. data/NOTICE +13 -0
  4. data/Rakefile +50 -51
  5. data/bin/deltacloudd +8 -1
  6. data/config.ru +0 -2
  7. data/config/drivers.yaml +48 -0
  8. data/deltacloud-core.gemspec +75 -0
  9. data/deltacloud.rb +3 -2
  10. data/lib/deltacloud/backend_capability.rb +15 -3
  11. data/lib/deltacloud/base_driver.rb +0 -2
  12. data/lib/deltacloud/base_driver/base_driver.rb +85 -89
  13. data/lib/deltacloud/base_driver/features.rb +61 -7
  14. data/lib/deltacloud/base_driver/mock_driver.rb +42 -43
  15. data/lib/deltacloud/core_ext.rb +18 -0
  16. data/lib/deltacloud/core_ext/integer.rb +31 -0
  17. data/lib/deltacloud/core_ext/string.rb +50 -0
  18. data/lib/deltacloud/drivers/azure/azure_driver.rb +71 -22
  19. data/lib/deltacloud/drivers/ec2/ec2_driver.rb +641 -584
  20. data/lib/deltacloud/drivers/ec2/ec2_mock_driver.rb +0 -2
  21. data/lib/deltacloud/drivers/eucalyptus/eucalyptus_driver.rb +167 -0
  22. data/lib/deltacloud/drivers/gogrid/gogrid_client.rb +39 -1
  23. data/lib/deltacloud/drivers/gogrid/gogrid_driver.rb +41 -25
  24. data/lib/deltacloud/drivers/mock/data/buckets/blobs/blob1.yml +6 -3
  25. data/lib/deltacloud/drivers/mock/data/buckets/blobs/blob2.yml +6 -3
  26. data/lib/deltacloud/drivers/mock/data/buckets/blobs/blob3.yml +4 -2
  27. data/lib/deltacloud/drivers/mock/data/buckets/blobs/blob4.yml +5 -2
  28. data/lib/deltacloud/drivers/mock/data/buckets/blobs/blob5.yml +4 -2
  29. data/lib/deltacloud/drivers/mock/data/instances/inst0.yml +1 -0
  30. data/lib/deltacloud/drivers/mock/data/instances/inst1.yml +1 -0
  31. data/lib/deltacloud/drivers/mock/data/instances/inst2.yml +1 -0
  32. data/lib/deltacloud/drivers/mock/data/storage_volumes/vol1.yml +1 -0
  33. data/lib/deltacloud/drivers/mock/data/storage_volumes/vol2.yml +1 -0
  34. data/lib/deltacloud/drivers/mock/data/storage_volumes/vol3.yml +1 -0
  35. data/lib/deltacloud/drivers/mock/mock_driver.rb +138 -30
  36. data/lib/deltacloud/drivers/opennebula/cloud_client.rb +13 -15
  37. data/lib/deltacloud/drivers/opennebula/occi_client.rb +13 -15
  38. data/lib/deltacloud/drivers/opennebula/opennebula_driver.rb +13 -15
  39. data/lib/deltacloud/drivers/rackspace/rackspace_driver.rb +224 -113
  40. data/lib/deltacloud/drivers/rhevm/rhevm_client.rb +332 -0
  41. data/lib/deltacloud/drivers/rhevm/rhevm_driver.rb +221 -170
  42. data/lib/deltacloud/drivers/rimuhosting/rimuhosting_driver.rb +0 -1
  43. data/lib/deltacloud/drivers/sbc/sbc_client.rb +247 -0
  44. data/lib/deltacloud/drivers/sbc/sbc_driver.rb +297 -0
  45. data/lib/deltacloud/drivers/terremark/terremark_driver.rb +0 -2
  46. data/lib/deltacloud/hardware_profile.rb +1 -3
  47. data/lib/deltacloud/helpers.rb +0 -2
  48. data/lib/deltacloud/helpers/application_helper.rb +86 -12
  49. data/lib/deltacloud/helpers/blob_stream.rb +19 -2
  50. data/lib/deltacloud/helpers/conversion_helper.rb +0 -2
  51. data/lib/deltacloud/helpers/hardware_profiles_helper.rb +0 -2
  52. data/lib/deltacloud/method_serializer.rb +0 -2
  53. data/lib/deltacloud/models/base_model.rb +0 -2
  54. data/lib/deltacloud/models/blob.rb +1 -2
  55. data/lib/deltacloud/models/bucket.rb +0 -2
  56. data/lib/deltacloud/models/image.rb +0 -2
  57. data/lib/deltacloud/models/instance.rb +19 -2
  58. data/lib/deltacloud/models/instance_profile.rb +4 -2
  59. data/lib/deltacloud/models/key.rb +0 -2
  60. data/lib/deltacloud/models/load_balancer.rb +0 -2
  61. data/lib/deltacloud/models/realm.rb +0 -2
  62. data/lib/deltacloud/models/storage_snapshot.rb +0 -2
  63. data/lib/deltacloud/models/storage_volume.rb +4 -2
  64. data/lib/deltacloud/runner.rb +132 -0
  65. data/lib/deltacloud/state_machine.rb +0 -2
  66. data/lib/deltacloud/validation.rb +9 -7
  67. data/lib/drivers.rb +36 -48
  68. data/lib/sinatra/accept_media_types.rb +26 -0
  69. data/lib/sinatra/lazy_auth.rb +16 -0
  70. data/lib/sinatra/rabbit.rb +112 -54
  71. data/lib/sinatra/rack_driver_select.rb +50 -16
  72. data/lib/sinatra/rack_etag.rb +79 -0
  73. data/lib/sinatra/rack_matrix_params.rb +84 -0
  74. data/lib/sinatra/rack_runtime.rb +47 -0
  75. data/lib/sinatra/static_assets.rb +16 -0
  76. data/lib/sinatra/url_for.rb +31 -4
  77. data/public/favicon.ico +0 -0
  78. data/public/images/bread-bg.png +0 -0
  79. data/public/images/error.png +0 -0
  80. data/public/images/pending.png +0 -0
  81. data/public/images/running.png +0 -0
  82. data/public/images/stopped.png +0 -0
  83. data/public/javascripts/application.js +35 -0
  84. data/public/stylesheets/compiled/application.css +59 -5
  85. data/public/stylesheets/compiled/screen.css +1 -1
  86. data/server.rb +293 -29
  87. data/support/fedora/deltacloud-core +78 -0
  88. data/support/fedora/deltacloud-core.spec +143 -0
  89. data/support/fedora/deltacloudd +78 -18
  90. data/support/fedora/rubygem-deltacloud-core.spec +76 -40
  91. data/tests/common.rb +172 -0
  92. data/tests/drivers/mock/api_test.rb +133 -0
  93. data/tests/drivers/mock/hardware_profiles_test.rb +134 -0
  94. data/tests/drivers/mock/images_test.rb +126 -0
  95. data/tests/drivers/mock/instance_states_test.rb +71 -0
  96. data/tests/drivers/mock/instances_test.rb +236 -0
  97. data/tests/drivers/mock/realms_test.rb +93 -0
  98. data/tests/drivers/mock/setup.rb +3 -0
  99. data/tests/drivers/mock/url_for_test.rb +67 -0
  100. data/tests/drivers/rackspace/api_test.rb +41 -0
  101. data/tests/drivers/rackspace/hardware_profiles_test.rb +53 -0
  102. data/tests/drivers/rackspace/images_test.rb +40 -0
  103. data/tests/drivers/rackspace/instances_test.rb +161 -0
  104. data/tests/drivers/rackspace/realms_test.rb +36 -0
  105. data/tests/drivers/rackspace/setup.rb +14 -0
  106. data/tests/drivers/rhevm/api_test.rb +39 -0
  107. data/tests/drivers/rhevm/hardware_profiles_test.rb +53 -0
  108. data/tests/drivers/rhevm/images_test.rb +42 -0
  109. data/tests/drivers/rhevm/instances_test.rb +179 -0
  110. data/tests/drivers/rhevm/realms_test.rb +35 -0
  111. data/tests/drivers/rhevm/setup.rb +14 -0
  112. data/tests/rabbit_test.rb +52 -0
  113. data/views/api/show.html.haml +2 -5
  114. data/views/blobs/new.html.haml +17 -1
  115. data/views/blobs/show.html.haml +6 -0
  116. data/views/blobs/show.xml.haml +5 -1
  117. data/views/buckets/index.html.haml +1 -12
  118. data/views/buckets/index.xml.haml +3 -5
  119. data/views/docs/operation.html.haml +23 -11
  120. data/views/drivers/index.html.haml +15 -0
  121. data/views/drivers/index.xml.haml +7 -0
  122. data/views/drivers/show.html.haml +20 -0
  123. data/views/drivers/show.xml.haml +7 -0
  124. data/views/error.html.haml +31 -0
  125. data/views/errors/auth_exception.xml.haml +2 -1
  126. data/views/errors/backend_capability_failure.xml.haml +2 -1
  127. data/views/errors/backend_error.html.haml +3 -0
  128. data/views/errors/backend_error.xml.haml +2 -2
  129. data/views/errors/validation_failure.xml.haml +3 -2
  130. data/views/images/index.html.haml +1 -6
  131. data/views/images/index.xml.haml +2 -0
  132. data/views/images/new.html.haml +14 -0
  133. data/views/images/show.xml.haml +2 -0
  134. data/views/instances/index.html.haml +8 -6
  135. data/views/instances/index.xml.haml +4 -0
  136. data/views/instances/new.html.haml +40 -11
  137. data/views/instances/run.html.haml +9 -0
  138. data/views/instances/run.xml.haml +7 -0
  139. data/views/instances/run_command.html.haml +16 -0
  140. data/views/instances/show.html.haml +14 -0
  141. data/views/instances/show.xml.haml +12 -4
  142. data/views/layout.html.haml +7 -2
  143. data/views/load_balancers/index.html.haml +1 -1
  144. data/views/load_balancers/new.html.haml +2 -2
  145. data/views/load_balancers/show.html.haml +1 -1
  146. data/views/storage_snapshots/index.html.haml +3 -0
  147. data/views/storage_snapshots/new.html.haml +9 -0
  148. data/views/storage_volumes/attach.html.haml +20 -0
  149. data/views/storage_volumes/index.html.haml +16 -1
  150. data/views/storage_volumes/index.xml.haml +1 -10
  151. data/views/storage_volumes/new.html.haml +17 -0
  152. data/views/storage_volumes/show.html.haml +8 -0
  153. data/views/storage_volumes/show.xml.haml +25 -3
  154. metadata +197 -127
  155. data/lib/deltacloud/drivers/rackspace/rackspace_client.rb +0 -130
  156. data/parse.rb +0 -7
  157. data/test.rb +0 -3
  158. data/views/api/drivers.xml.haml +0 -6
@@ -0,0 +1,71 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one or more
2
+ # contributor license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright ownership. The
4
+ # ASF licenses this file to you under the Apache License, Version 2.0 (the
5
+ # "License"); you may not use this file except in compliance with the
6
+ # License. You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations
14
+ # under the License.
15
+ #
16
+
17
+ $:.unshift File.join(File.dirname(__FILE__), '..', '..', '..')
18
+ require 'tests/common'
19
+
20
+ module DeltacloudUnitTest
21
+ class RealmsTest < Test::Unit::TestCase
22
+ include Rack::Test::Methods
23
+
24
+ def app
25
+ Sinatra::Application
26
+ end
27
+
28
+ def test_it_not_require_authentication
29
+ require_authentication?('/api/realms').should_not == true
30
+ end
31
+
32
+ def test_it_returns_instance_states
33
+ get_auth_url '/api/instance_states', {}
34
+ (last_xml_response/'states/state').length.should > 0
35
+ end
36
+
37
+ def test_each_state_has_transition
38
+ get_auth_url '/api/instance_states', {}
39
+ (last_xml_response/'states/state').each do |state|
40
+ next if state['name'].eql?('finish') # Finnish state doesn't have transitions
41
+ (state/'transition').length.should > 0
42
+ (state/'transition').each do |transition|
43
+ transition['to'].should_not == nil
44
+ end
45
+ end
46
+ end
47
+
48
+ def test_it_responses_to_json
49
+ # FIXME: This test is suffering from conflict between JSON gem and Activesupport
50
+ # gem in EC2.
51
+ #
52
+ #do_request '/api/instance_states', {}, false, { :format => :json }
53
+ #JSON::parse(last_response.body).class.should == Array
54
+ #JSON::parse(last_response.body).first['transitions'].class.should == Array
55
+ #JSON::parse(last_response.body).first['name'].should == 'start'
56
+ end
57
+
58
+ def test_it_responses_to_html
59
+ get_url '/api/instance_states', {}, { :format => :html }
60
+ last_response.status.should == 200
61
+ Nokogiri::HTML(last_response.body).search('html').first.name.should == 'html'
62
+ end
63
+
64
+ def test_it_responses_to_png
65
+ get_url '/api/instance_states', { :format => 'png' }
66
+ last_response.status.should == 200
67
+ last_response.headers['Content-Type'].should =~ /^image\/png/
68
+ end
69
+
70
+ end
71
+ end
@@ -0,0 +1,236 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one or more
2
+ # contributor license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright ownership. The
4
+ # ASF licenses this file to you under the Apache License, Version 2.0 (the
5
+ # "License"); you may not use this file except in compliance with the
6
+ # License. You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations
14
+ # under the License.
15
+ #
16
+
17
+ $:.unshift File.join(File.dirname(__FILE__), '..', '..', '..')
18
+ require 'tests/common'
19
+
20
+ module DeltacloudUnitTest
21
+ class InstancesTest < Test::Unit::TestCase
22
+ include Rack::Test::Methods
23
+
24
+ def app
25
+ Sinatra::Application
26
+ end
27
+
28
+ def test_it_require_authentication
29
+ require_authentication?('/api/instances').should == true
30
+ end
31
+
32
+ def test_it_returns_instances
33
+ get_auth_url '/api/instances', {}
34
+ (last_xml_response/'instances/instance').length.should > 0
35
+ end
36
+
37
+ def test_it_has_correct_attributes_set
38
+ get_auth_url '/api/images', {}
39
+ (last_xml_response/'images/image').each do |image|
40
+ image.attributes.keys.sort.should == [ 'href', 'id' ]
41
+ end
42
+ end
43
+
44
+ def test_it_has_unique_ids
45
+ get_auth_url '/api/instances', {}
46
+ ids = []
47
+ (last_xml_response/'instances/instance').each do |image|
48
+ ids << image['id'].to_s
49
+ end
50
+ ids.sort.should == ids.sort.uniq
51
+ end
52
+
53
+ def test_inst1_has_correct_attributes
54
+ get_auth_url '/api/instances', {}
55
+ instance = (last_xml_response/'instances/instance[@id="inst1"]')
56
+ test_instance_attributes(instance)
57
+ end
58
+
59
+ def test_it_returns_valid_realm
60
+ get_auth_url '/api/instances/inst1', {}
61
+ instance = (last_xml_response/'instance')
62
+ test_instance_attributes(instance)
63
+ end
64
+
65
+ def test_it_responses_to_json
66
+ get_auth_url '/api/instances', {}, { :format => :json }
67
+ JSON::parse(last_response.body).class.should == Hash
68
+ JSON::parse(last_response.body)['instances'].class.should == Array
69
+
70
+ get_auth_url '/api/instances/inst1', {}, { :format => :json }
71
+ last_response.status.should == 200
72
+ JSON::parse(last_response.body).class.should == Hash
73
+ JSON::parse(last_response.body)['instance'].class.should == Hash
74
+ end
75
+
76
+ def test_it_responses_to_html
77
+ get_auth_url '/api/instances', {}, { :format => :html }
78
+ last_response.status.should == 200
79
+ Nokogiri::HTML(last_response.body).search('html').first.name.should == 'html'
80
+ get_auth_url '/api/instances/inst1', {}, { :format => :html }
81
+ last_response.status.should == 200
82
+ Nokogiri::HTML(last_response.body).search('html').first.name.should == 'html'
83
+ end
84
+
85
+ def test_it_create_a_new_instance_using_image_id
86
+ params = {
87
+ :image_id => 'img1'
88
+ }
89
+ post_url '/api/instances', params
90
+ last_response.status.should == 201
91
+ last_response.headers['Location'].should_not == nil
92
+ get_auth_url last_response.headers['Location'], {}
93
+ (last_xml_response/'instance/name').should_not == nil
94
+ add_created_instance (last_xml_response/'instance').first['id']
95
+ test_instance_attributes(last_xml_response/'instance')
96
+ end
97
+
98
+ def test_it_create_a_new_instance_using_image_id_and_name
99
+ params = {
100
+ :image_id => 'img1',
101
+ :name => "unit_test_instance1"
102
+ }
103
+ post_url '/api/instances', params
104
+ last_response.status.should == 201
105
+ last_response.headers['Location'].should_not == nil
106
+ get_auth_url last_response.headers['Location'], {}
107
+ (last_xml_response/'instance/name').text.should == 'unit_test_instance1'
108
+ add_created_instance (last_xml_response/'instance').first['id']
109
+ test_instance_attributes(last_xml_response/'instance')
110
+ end
111
+
112
+ def test_it_create_a_new_instance_using_image_id_and_name_and_hwp
113
+ params = {
114
+ :image_id => 'img1',
115
+ :name => "unit_test_instance1",
116
+ :hwp_id => "m1-xlarge"
117
+ }
118
+ post_url '/api/instances', params
119
+ last_response.status.should == 201
120
+ last_response.headers['Location'].should_not == nil
121
+ get_auth_url last_response.headers['Location'], {}
122
+ (last_xml_response/'instance/name').text.should == 'unit_test_instance1'
123
+ (last_xml_response/'instance/hardware_profile').first['id'].should == 'm1-xlarge'
124
+ add_created_instance (last_xml_response/'instance').first['id']
125
+ test_instance_attributes(last_xml_response/'instance')
126
+ end
127
+
128
+ def test_it_z0_stop_and_start_instance
129
+ $created_instances.each do |instance_id|
130
+ get_auth_url "/api/instances/#{instance_id}", {}
131
+ stop_url = (last_xml_response/'actions/link[@rel="stop"]').first['href']
132
+ stop_url.should_not == nil
133
+ post_url stop_url
134
+ last_response.status.should == 200
135
+ instance = Nokogiri::XML(last_response.body)
136
+ test_instance_attributes(instance)
137
+ (instance/'state').text.should == 'STOPPED'
138
+ get_auth_url "/api/instances/#{instance_id}", {}
139
+ start_url = (last_xml_response/'actions/link[@rel="start"]').first['href']
140
+ start_url.should_not == nil
141
+ post_url start_url
142
+ last_response.status.should == 200
143
+ instance = Nokogiri::XML(last_response.body)
144
+ test_instance_attributes(instance)
145
+ (instance/'state').text.should == 'RUNNING'
146
+ end
147
+ end
148
+
149
+ def test_z0_reboot_instance
150
+ $created_instances.each do |instance_id|
151
+ get_auth_url "/api/instances/#{instance_id}", {}
152
+ reboot_url = (last_xml_response/'actions/link[@rel="reboot"]').first['href']
153
+ reboot_url.should_not == nil
154
+ post_url reboot_url
155
+ last_response.status.should == 200
156
+ instance = Nokogiri::XML(last_response.body)
157
+ test_instance_attributes(instance)
158
+ (instance/'state').text.should == 'RUNNING'
159
+ end
160
+ end
161
+
162
+ def test_z1_stop_created_instances
163
+ $created_instances.each do |instance_id|
164
+ get_auth_url "/api/instances/#{instance_id}", {}
165
+ stop_url = (last_xml_response/'actions/link[@rel="stop"]').first['href']
166
+ stop_url.should_not == nil
167
+ post_url stop_url, {}
168
+ last_response.status.should == 200
169
+ instance = Nokogiri::XML(last_response.body)
170
+ test_instance_attributes(instance)
171
+ (instance/'state').text.should == 'STOPPED'
172
+ end
173
+ end
174
+
175
+ def test_z2_destroy_created_instances
176
+ $created_instances.each do |instance_id|
177
+ get_auth_url "/api/instances/#{instance_id}", {}
178
+ destroy_url = (last_xml_response/'actions/link[@rel="destroy"]').first['href']
179
+ destroy_url.should_not == nil
180
+ delete_url destroy_url, {}
181
+ last_response.status.should == 204
182
+ end
183
+ end
184
+
185
+ def test_create_key_returns_201
186
+ post_url '/api/keys', {:name => Time.now.to_f.to_s}
187
+ last_response.status.should == 201
188
+ end
189
+
190
+ private
191
+
192
+ def test_instance_attributes(instance)
193
+ (instance/'name').should_not == nil
194
+ (instance/'owner_id').should_not == nil
195
+ ['RUNNING', 'STOPPED'].include?((instance/'state').text).should == true
196
+
197
+ (instance/'public_addreses').should_not == nil
198
+ (instance/'public_addresses/address').to_a.size.should > 0
199
+ (instance/'public_addresses/address').first.text.should_not == ""
200
+
201
+ (instance/'private_addresses').should_not == nil
202
+ (instance/'private_addresses/address').to_a.size.should > 0
203
+ (instance/'private_addresses/address').first.text.should_not == ""
204
+
205
+ (instance/'actions/link').to_a.size.should > 0
206
+ (instance/'actions/link').each do |link|
207
+ link['href'].should_not == ""
208
+ link['rel'].should_not == ""
209
+ link['method'].should_not == ""
210
+ ['get', 'post', 'delete', 'put'].include?(link['method']).should == true
211
+ end
212
+
213
+ (instance/'image').size.should > 0
214
+ (instance/'image').first['href'].should_not == ""
215
+ (instance/'image').first['id'].should_not == ""
216
+ get_auth_url (instance/'image').first['href'], {}
217
+ (last_xml_response/'image').should_not == nil
218
+ (last_xml_response/'image').first['href'] == (instance/'image').first['href']
219
+
220
+ (instance/'realm').size.should > 0
221
+ (instance/'realm').first['href'].should_not == ""
222
+ (instance/'realm').first['id'].should_not == ""
223
+ get_auth_url (instance/'realm').first['href']
224
+ (last_xml_response/'realm').should_not == nil
225
+ (last_xml_response/'realm').first['href'] == (instance/'realm').first['href']
226
+
227
+ (instance/'hardware_profile').size.should > 0
228
+ (instance/'hardware_profile').first['href'].should_not == ""
229
+ (instance/'hardware_profile').first['id'].should_not == ""
230
+ get_auth_url (instance/'hardware_profile').first['href']
231
+ (last_xml_response/'hardware_profile').should_not == nil
232
+ (last_xml_response/'hardware_profile').first['href'] == (instance/'hardware_profile').first['href']
233
+ end
234
+
235
+ end
236
+ end
@@ -0,0 +1,93 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one or more
2
+ # contributor license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright ownership. The
4
+ # ASF licenses this file to you under the Apache License, Version 2.0 (the
5
+ # "License"); you may not use this file except in compliance with the
6
+ # License. You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations
14
+ # under the License.
15
+ #
16
+
17
+ $:.unshift File.join(File.dirname(__FILE__), '..', '..', '..')
18
+ require 'tests/common'
19
+
20
+ module DeltacloudUnitTest
21
+ class RealmsTest < Test::Unit::TestCase
22
+ include Rack::Test::Methods
23
+
24
+ def app
25
+ Sinatra::Application
26
+ end
27
+
28
+ def test_it_not_require_authentication
29
+ require_authentication?('/api/realms').should_not == true
30
+ end
31
+
32
+ def test_it_returns_realms
33
+ get_auth_url '/api/realms', {}
34
+ (last_xml_response/'realms/realm').length.should > 0
35
+ end
36
+
37
+ def test_it_has_correct_attributes_set
38
+ get_auth_url '/api/realms', {}
39
+ (last_xml_response/'realms/realm').each do |realm|
40
+ realm.attributes.keys.sort.should == [ 'href', 'id' ]
41
+ end
42
+ end
43
+
44
+ def test_us_has_correct_attributes
45
+ get_auth_url '/api/realms', {}
46
+ realm = (last_xml_response/'realms/realm[@id="us"]')
47
+ test_realm_attributes(realm)
48
+ end
49
+
50
+ def test_it_returns_valid_realm
51
+ get_auth_url '/api/realms/us', {}
52
+ realm = (last_xml_response/'realm')
53
+ test_realm_attributes(realm)
54
+ end
55
+
56
+ def test_it_has_unique_ids
57
+ get_auth_url '/api/realms', {}
58
+ ids = []
59
+ (last_xml_response/'realms/realm').each do |realm|
60
+ ids << realm['id'].to_s
61
+ end
62
+ ids.sort.should == ids.sort.uniq
63
+ end
64
+
65
+ def test_it_responses_to_json
66
+ get_auth_url '/api/realms', {}, { :format => :json }
67
+ JSON::parse(last_response.body).class.should == Hash
68
+ JSON::parse(last_response.body)['realms'].class.should == Array
69
+ get_auth_url '/api/realms/us', {}, { :format => :json }
70
+ last_response.status.should == 200
71
+ JSON::parse(last_response.body).class.should == Hash
72
+ JSON::parse(last_response.body)['realm'].class.should == Hash
73
+ end
74
+
75
+ def test_it_responses_to_html
76
+ get_auth_url '/api/realms', {}, { :format => :html }
77
+ last_response.status.should == 200
78
+ Nokogiri::HTML(last_response.body).search('html').first.name.should == 'html'
79
+ get_auth_url '/api/realms/us', {}, { :format => :html }
80
+ last_response.status.should == 200
81
+ Nokogiri::HTML(last_response.body).search('html').first.name.should == 'html'
82
+ end
83
+
84
+ private
85
+
86
+ def test_realm_attributes(realm)
87
+ (realm/'name').should_not == nil
88
+ (realm/'limit').should_not == nil
89
+ ['AVAILABLE'].include?((realm/'state').text).should == true
90
+ end
91
+
92
+ end
93
+ end
@@ -0,0 +1,3 @@
1
+ ENV['API_DRIVER'] = "mock"
2
+ ENV['API_USER'] = 'mockuser'
3
+ ENV['API_PASSWORD'] = 'mockpassword'
@@ -0,0 +1,67 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one or more
2
+ # contributor license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright ownership. The
4
+ # ASF licenses this file to you under the Apache License, Version 2.0 (the
5
+ # "License"); you may not use this file except in compliance with the
6
+ # License. You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations
14
+ # under the License.
15
+ #
16
+
17
+ $:.unshift File.join(File.dirname(__FILE__), '..', '..', '..')
18
+ require 'tests/common'
19
+
20
+ module DeltacloudUnitTest
21
+ class UrlForTest < Test::Unit::TestCase
22
+ include Rack::Test::Methods
23
+
24
+ def app
25
+ Sinatra::Application
26
+ end
27
+
28
+ def test_it_works_for_root
29
+ verify_url_for("/", "/")
30
+ end
31
+
32
+ def test_it_works_for_root_absolute
33
+ verify_url_for("/", "http://example.org/", :full)
34
+ end
35
+
36
+ def test_it_works_with_spaces
37
+ verify_url_for("/url with spaces", "/url%20with%20spaces")
38
+ end
39
+
40
+ def test_it_works_when_given_absolute
41
+ verify_url_for("http://test.com", "http://test.com")
42
+ end
43
+
44
+ def test_it_works_when_not_at_root_context
45
+ verify_url_for("/", "context/", :path_only, {}, {"SCRIPT_NAME" => "context"})
46
+ end
47
+
48
+ def verify_url_for(url, expected_url, mode=:path_only, params={}, rack_env={})
49
+ # generate a unique url for each test
50
+ test_url = "/url_for_test/#{expected_url.hash}/#{Time.now.to_i}"
51
+ # Create our sinatra test endpoint
52
+ self.class.create_test_url_content(test_url, url, mode)
53
+
54
+ # verify the generated url matches what we expect
55
+ get test_url, params, rack_env
56
+ last_response.body.should == expected_url
57
+ end
58
+
59
+ def self.create_test_url_content(test_url, url_content, mode)
60
+ get test_url do
61
+ content_type "text/plain"
62
+ url_for(url_content, mode)
63
+ end
64
+ end
65
+
66
+ end
67
+ end