rackspace-cloud 0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,64 @@
1
+ require File.join(File.dirname(__FILE__), "..", "lib", "rackspace-cloud")
2
+ require "rubygems"
3
+ require "active_support/test_case"
4
+ gem "mocha"
5
+ require "mocha"
6
+ gem "thoughtbot-shoulda"
7
+ require "shoulda"
8
+
9
+ ActiveSupport::Inflector.inflections do |inflect|
10
+ inflect.irregular 'base', 'bases'
11
+ end
12
+
13
+ class ActiveSupport::TestCase
14
+ # This sets up the expectation for the API version query
15
+ def expects_versions_response
16
+ RestClient.expects(:get).with("#{Rackspace::Connection::VERSION_URL}/.json", {:accept => "application/json", :content_type => "application/json"}).returns({"versions" => [{"id" => "v1.1", "status" => "BETA"}, {"id" => "v1.0", "status" => "CURRENT"}]}.to_json)
17
+ end
18
+
19
+ # This sets up the expectations for authenticating against the API
20
+ def expects_authentication(auth_token = "123456789")
21
+ response = RestClient::Response.new "", nil
22
+ response.expects(:headers).returns({:x_auth_token => auth_token, :x_storage_url => "http://test/storage", :x_server_management_url => "http://test/servers", :x_cdn_management_url => "http://test/content"})
23
+ RestClient::Request.expects(:execute).with(:method => :get, :url => "#{Rackspace::Connection::AUTH_URL}/v1.0", :headers => {"X-Auth-User" => "test_user", "X-Auth-Key" => "test_key"}, :raw_response => true).returns(response)
24
+ end
25
+
26
+ # This returns default headers used for expectations
27
+ def default_headers
28
+ {"X-Auth-Token" => "123456789", :accept => "application/json", :content_type => "application/json"}
29
+ end
30
+
31
+ # Helper method for building GET expectations
32
+ def expects_get(url, headers = {})
33
+ RestClient.expects(:get).with(url, default_headers.merge(headers))
34
+ end
35
+
36
+ # Helper method for building POST expectations
37
+ def expects_post(url, payload, headers = {})
38
+ RestClient.expects(:post).with(url, payload.to_json, default_headers.merge(headers))
39
+ end
40
+
41
+ # Helper method for building PUT expectations
42
+ def expects_put(url, payload, headers = {})
43
+ RestClient.expects(:put).with(url, payload.to_json, default_headers.merge(headers))
44
+ end
45
+
46
+ # Helper method for building DELETE expectations
47
+ def expects_delete(url, headers = {})
48
+ RestClient.expects(:delete).with(url, default_headers.merge(headers))
49
+ end
50
+
51
+ # This sets up the necessary values to mock the authentication response as if we had authenticated
52
+ def mock_auth_response(auth_token = "123456789")
53
+ Rackspace::Connection.send(:instance_variable_set, "@auth_response", {:auth_token => auth_token, :storage_url => "http://test/storage", :server_management_url => "http://test/servers", :cdn_management_url => "http://test/content"})
54
+ mock_api_init
55
+ end
56
+
57
+ # This sets up the necessary values to mock the configuration as if we had initialized
58
+ def mock_api_init
59
+ Rackspace::Connection.send(:instance_variable_set, "@user", "test_user")
60
+ Rackspace::Connection.send(:instance_variable_set, "@key", "test_key")
61
+ Rackspace::Connection.send(:instance_variable_set, "@version", "v1.0")
62
+ Rackspace::Connection.send(:instance_variable_set, "@initialized", true)
63
+ end
64
+ end
@@ -0,0 +1,177 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ class Rackspace::CloudServers::ImageTest < ActiveSupport::TestCase
4
+ context "working with Rackspace::CloudServers::Image" do
5
+ should "inherit from Rackspace::CloudServers::Base" do
6
+ assert Rackspace::CloudServers::Image.ancestors.include?(Rackspace::CloudServers::Base)
7
+ end
8
+ end
9
+
10
+ context "an instance of Rackspace::CloudServers::Image" do
11
+ setup do
12
+ @image = Rackspace::CloudServers::Image.new
13
+ end
14
+
15
+ [:id, :name, :updated, :created, :status, :serverId, :progress].each do |attrib|
16
+ should "respond to #{attrib}" do
17
+ assert @image.respond_to?(attrib)
18
+ end
19
+ end
20
+ end
21
+
22
+ context "querying images" do
23
+ should "build the right resource URL for the index" do
24
+ mock_auth_response
25
+ assert_equal "http://test/servers/images", Rackspace::CloudServers::Image.resource_url
26
+ end
27
+
28
+ should "build the right resource URL for the retrieval" do
29
+ mock_auth_response
30
+ assert_equal "http://test/servers/images/1", Rackspace::CloudServers::Image.resource_url(1)
31
+ end
32
+
33
+ should "make the right request for find(:all)" do
34
+ mock_auth_response
35
+ expects_get("http://test/servers/images/detail.json")
36
+ Rackspace::CloudServers::Image.find(:all)
37
+ end
38
+
39
+ should "make the right request for find and default to find(:all)" do
40
+ mock_auth_response
41
+ expects_get("http://test/servers/images/detail.json")
42
+ Rackspace::CloudServers::Image.find
43
+ end
44
+
45
+ should "make the right request for find(1)" do
46
+ mock_auth_response
47
+ expects_get("http://test/servers/images/1.json")
48
+ Rackspace::CloudServers::Image.find(1)
49
+ end
50
+
51
+ should "return the right data for find(:all)" do
52
+ mock_auth_response
53
+ expects_get("http://test/servers/images/detail.json").returns(find_all_response)
54
+ result = Rackspace::CloudServers::Image.find(:all)
55
+ assert_equal 2, result.length
56
+ assert_equal Rackspace::CloudServers::Image, result.first.class
57
+ assert_equal 2, result.first.id
58
+ assert_equal Rackspace::CloudServers::Image, result.last.class
59
+ assert_equal 743, result.last.id
60
+ end
61
+
62
+ should "return the right data for find(2)" do
63
+ mock_auth_response
64
+ expects_get("http://test/servers/images/2.json").returns(find_2_response)
65
+ result = Rackspace::CloudServers::Image.find(2)
66
+ assert_equal Rackspace::CloudServers::Image, result.class
67
+ assert_equal 2, result.id
68
+ end
69
+ end
70
+
71
+ context "creating an image" do
72
+ should "make the right POST request and return the values expected" do
73
+ mock_auth_response
74
+ expects_post("http://test/servers/images.json", {"image" => {"name" => "new-image-test", "serverId" => 1}}).returns(create_response)
75
+ image = Rackspace::CloudServers::Image.create(:name => "new-image-test", :serverId => 1)
76
+ assert_equal 22, image.id
77
+ assert_equal "new-image-test", image.name
78
+ assert_equal 1, image.serverId
79
+ assert_equal "SAVING", image.status
80
+ assert_equal 0, image.progress
81
+ end
82
+ end
83
+
84
+ context "updating an image" do
85
+ should "not be allowed" do
86
+ assert_equal false, Rackspace::CloudServers::Image.new(:id => 1).update
87
+ end
88
+ end
89
+
90
+ context "deleting a image" do
91
+ should "make the DELETE request for the image" do
92
+ mock_auth_response
93
+ expects_delete("http://test/servers/images/1.json")
94
+ assert_equal true, Rackspace::CloudServers::Image.new(:id => 1).destroy
95
+ end
96
+ end
97
+
98
+ context "reloading an image instance" do
99
+ should "not be allowed when it's a new record" do
100
+ assert_equal false, Rackspace::CloudServers::Image.new.reload
101
+ end
102
+
103
+ should "make the GET request for the image" do
104
+ mock_auth_response
105
+ expects_get("http://test/servers/images/2.json").returns(find_2_response_reloaded)
106
+ result = Rackspace::CloudServers::Image.new(:id => 2)
107
+ updated = result.reload
108
+ assert_equal 100, updated.progress
109
+ assert_equal 100, result.progress
110
+ end
111
+ end
112
+
113
+ def find_all_response
114
+ {
115
+ "images" => [
116
+ {
117
+ "id" => 2,
118
+ "name" => "CentOS 5.2",
119
+ "updated" => "2010-10-10T12:00:00Z",
120
+ "created" => "2010-08-10T12:00:00Z",
121
+ "status" => "ACTIVE"
122
+ },
123
+ {
124
+ "id" => 743,
125
+ "name" => "My Server Backup",
126
+ "serverId" => 12,
127
+ "updated" => "2010-10-10T12:00:00Z",
128
+ "created" => "2010-08-10T12:00:00Z",
129
+ "status" => "SAVING",
130
+ "progress" => 80
131
+ }
132
+ ]
133
+ }.to_json
134
+ end
135
+
136
+ def find_2_response
137
+ {
138
+ "image" => {
139
+ "id" => 2,
140
+ "name" => "CentOS 5.2",
141
+ "serverId" => 12,
142
+ "updated" => "2010-10-10T12:00:00Z",
143
+ "created" => "2010-08-10T12:00:00Z",
144
+ "status" => "ACTIVE",
145
+ "progress" => 80
146
+ }
147
+ }.to_json
148
+ end
149
+
150
+ def find_2_response_reloaded
151
+ {
152
+ "image" => {
153
+ "id" => 2,
154
+ "name" => "CentOS 5.2",
155
+ "serverId" => 12,
156
+ "updated" => "2010-10-10T12:00:00Z",
157
+ "created" => "2010-08-10T12:00:00Z",
158
+ "status" => "ACTIVE",
159
+ "progress" => 100
160
+ }
161
+ }.to_json
162
+ end
163
+
164
+ def create_response
165
+ {
166
+ "image" =>
167
+ {
168
+ "id" => 22,
169
+ "serverId" => 1,
170
+ "name" => "new-image-test",
171
+ "created" => "2010-10-10T12:00:00Z",
172
+ "status" => "SAVING",
173
+ "progress" => 0
174
+ }
175
+ }.to_json
176
+ end
177
+ end
@@ -0,0 +1,259 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ class Rackspace::CloudServers::ServerTest < ActiveSupport::TestCase
4
+ context "working with Rackspace::CloudServers::Server" do
5
+ should "inherit from Rackspace::CloudServers::Base" do
6
+ assert Rackspace::CloudServers::Server.ancestors.include?(Rackspace::CloudServers::Base)
7
+ end
8
+ end
9
+
10
+ context "an instance of Rackspace::CloudServers::Server" do
11
+ setup do
12
+ @server = Rackspace::CloudServers::Server.new
13
+ end
14
+
15
+ [:id, :name, :imageId, :flavorId, :hostId, :status, :progress, :addresses, :metadata].each do |attrib|
16
+ should "respond to #{attrib}" do
17
+ assert @server.respond_to?(attrib)
18
+ end
19
+ end
20
+ end
21
+
22
+ context "querying servers" do
23
+ should "build the right resource URL for the index" do
24
+ mock_auth_response
25
+ assert_equal "http://test/servers/servers", Rackspace::CloudServers::Server.resource_url
26
+ end
27
+
28
+ should "build the right resource URL for the retrieval" do
29
+ mock_auth_response
30
+ assert_equal "http://test/servers/servers/1", Rackspace::CloudServers::Server.resource_url(1)
31
+ end
32
+
33
+ should "make the right request for find(:all)" do
34
+ mock_auth_response
35
+ expects_get("http://test/servers/servers/detail.json")
36
+ Rackspace::CloudServers::Server.find(:all)
37
+ end
38
+
39
+ should "make the right request for find and default to find(:all)" do
40
+ mock_auth_response
41
+ expects_get("http://test/servers/servers/detail.json")
42
+ Rackspace::CloudServers::Server.find
43
+ end
44
+
45
+ should "make the right request for find(1)" do
46
+ mock_auth_response
47
+ expects_get("http://test/servers/servers/1.json")
48
+ Rackspace::CloudServers::Server.find(1)
49
+ end
50
+
51
+ should "return the right data for find(:all)" do
52
+ mock_auth_response
53
+ expects_get("http://test/servers/servers/detail.json").returns(find_all_response)
54
+ result = Rackspace::CloudServers::Server.find(:all)
55
+ assert_equal 2, result.length
56
+ assert_equal Rackspace::CloudServers::Server, result.first.class
57
+ assert_equal 1234, result.first.id
58
+ assert_equal Rackspace::CloudServers::Server, result.last.class
59
+ assert_equal 5678, result.last.id
60
+ end
61
+
62
+ should "return the right figure for count" do
63
+ mock_auth_response
64
+ expects_get("http://test/servers/servers/detail.json").returns(find_all_response)
65
+ assert_equal 2, Rackspace::CloudServers::Server.count
66
+ end
67
+
68
+ should "return the right data for find(1)" do
69
+ mock_auth_response
70
+ expects_get("http://test/servers/servers/1234.json").returns(find_1234_response)
71
+ result = Rackspace::CloudServers::Server.find(1234)
72
+ assert_equal Rackspace::CloudServers::Server, result.class
73
+ assert_equal 1234, result.id
74
+ end
75
+ end
76
+
77
+ context "creating a server" do
78
+ should "make the right POST request and return the expected values" do
79
+ mock_auth_response
80
+ expects_post("http://test/servers/servers.json", {"server" => {"name" => "new-server-test", "imageId" => 2, "flavorId" => 1}}).returns(create_response)
81
+ server = Rackspace::CloudServers::Server.create(:name => "new-server-test", :imageId => 2, :flavorId => 1)
82
+ assert_equal 1235, server.id
83
+ assert_equal "new-server-test", server.name
84
+ assert_equal 2, server.imageId
85
+ assert_equal 1, server.flavorId
86
+ assert_equal "e4d909c290d0fb1ca068ffaddf22cbd0", server.hostId
87
+ assert_equal 0, server.progress
88
+ assert_equal "BUILD", server.status
89
+ assert_equal "GFf1j9aP", server.adminPass
90
+ assert server.metadata.keys.include?("internal-server-name")
91
+ assert_equal "test-1", server.metadata["internal-server-name"]
92
+ assert server.addresses.keys.include?("public")
93
+ assert server.addresses.keys.include?("private")
94
+ assert_equal 1, server.addresses["public"].length
95
+ assert_equal "67.23.10.138", server.addresses["public"].first
96
+ assert_equal 1, server.addresses["private"].length
97
+ assert_equal "10.176.42.19", server.addresses["private"].first
98
+ end
99
+ end
100
+
101
+ context "updating a server" do
102
+ should "make the PUT request for the server" do
103
+ mock_auth_response
104
+ expects_put("http://test/servers/servers/1.json", {"server" => {"name" => "test-name", :adminPass => "test-pass"}})
105
+ assert_equal true, Rackspace::CloudServers::Server.new(:id => 1, :name => "test-name", :adminPass => "test-pass").save
106
+ end
107
+ end
108
+
109
+ context "deleting a server" do
110
+ should "make the DELETE request for the server" do
111
+ mock_auth_response
112
+ expects_delete("http://test/servers/servers/1.json")
113
+ assert_equal true, Rackspace::CloudServers::Server.new(:id => 1).destroy
114
+ end
115
+ end
116
+
117
+ context "reloading a server instance" do
118
+ should "not be allowed when it's a new record" do
119
+ assert_equal false, Rackspace::CloudServers::Server.new.reload
120
+ end
121
+
122
+ should "make the GET request for the server" do
123
+ mock_auth_response
124
+ expects_get("http://test/servers/servers/1234.json").returns(find_1234_response_reloaded)
125
+ result = Rackspace::CloudServers::Server.new(:id => 1234)
126
+ updated = result.reload
127
+ assert_equal 100, updated.progress
128
+ assert_equal 100, result.progress
129
+ end
130
+ end
131
+
132
+ def find_all_response
133
+ {
134
+ "servers" => [
135
+ {
136
+ "id" => 1234,
137
+ "name" => "sample-server",
138
+ "imageId" => 2,
139
+ "flavorId" => 1,
140
+ "hostId" => "e4d909c290d0fb1ca068ffaddf22cbd0",
141
+ "status" => "BUILD",
142
+ "progress" => 60,
143
+ "addresses" => {
144
+ "public" => [
145
+ "67.23.10.132",
146
+ "67.23.10.131"
147
+ ],
148
+ "private" => [
149
+ "10.176.42.16"
150
+ ]
151
+ },
152
+ "metadata" => {
153
+ "Server Label" => "Web Head 1",
154
+ "Image Version" => "2.1"
155
+ }
156
+ },
157
+ {
158
+ "id" => 5678,
159
+ "name" => "sample-server2",
160
+ "imageId" => 2,
161
+ "flavorId" => 1,
162
+ "hostId" => "9e107d9d372bb6826bd81d3542a419d6",
163
+ "status" => "ACTIVE",
164
+ "addresses" => {
165
+ "public" => [
166
+ "67.23.10.133"
167
+ ],
168
+ "private" => [
169
+ "10.176.42.17"
170
+ ]
171
+ },
172
+ "metadata" => {
173
+ "Server Label" => "DB 1"
174
+ }
175
+ }
176
+ ]
177
+ }.to_json
178
+ end
179
+
180
+ def find_1234_response
181
+ {
182
+ "server" => {
183
+ "id" => 1234,
184
+ "name" => "sample-server",
185
+ "imageId" => 2,
186
+ "flavorId" => 1,
187
+ "hostId" => "e4d909c290d0fb1ca068ffaddf22cbd0",
188
+ "status" => "BUILD",
189
+ "progress" => 60,
190
+ "addresses" => {
191
+ "public" => [
192
+ "67.23.10.132",
193
+ "67.23.10.131"
194
+ ],
195
+ "private" => [
196
+ "10.176.42.16"
197
+ ]
198
+ },
199
+ "metadata" => {
200
+ "Server Label" => "Web Head 1",
201
+ "Image Version" => "2.1"
202
+ }
203
+ }
204
+ }.to_json
205
+ end
206
+
207
+ def find_1234_response_reloaded
208
+ {
209
+ "server" => {
210
+ "id" => 1234,
211
+ "name" => "sample-server",
212
+ "imageId" => 2,
213
+ "flavorId" => 1,
214
+ "hostId" => "e4d909c290d0fb1ca068ffaddf22cbd0",
215
+ "status" => "BUILD",
216
+ "progress" => 100,
217
+ "addresses" => {
218
+ "public" => [
219
+ "67.23.10.132",
220
+ "67.23.10.131"
221
+ ],
222
+ "private" => [
223
+ "10.176.42.16"
224
+ ]
225
+ },
226
+ "metadata" => {
227
+ "Server Label" => "Web Head 1",
228
+ "Image Version" => "2.1"
229
+ }
230
+ }
231
+ }.to_json
232
+ end
233
+
234
+ def create_response
235
+ {
236
+ "server" => {
237
+ "id" => 1235,
238
+ "name" => "new-server-test",
239
+ "imageId" => 2,
240
+ "flavorId" => 1,
241
+ "hostId" => "e4d909c290d0fb1ca068ffaddf22cbd0",
242
+ "progress" => 0,
243
+ "status" => "BUILD",
244
+ "adminPass" => "GFf1j9aP",
245
+ "metadata" => {
246
+ "internal-server-name" => "test-1"
247
+ },
248
+ "addresses" => {
249
+ "public" => [
250
+ "67.23.10.138"
251
+ ],
252
+ "private" => [
253
+ "10.176.42.19"
254
+ ]
255
+ }
256
+ }
257
+ }.to_json
258
+ end
259
+ end