From 398f706e7e1001b1ebf6fa8e3ae5eed43d322b33 Mon Sep 17 00:00:00 2001 From: Eric Hodel Date: Thu, 6 Dec 2012 14:30:30 -0800 Subject: [PATCH 1/4] OpenStack create_server mocks now match reality Previously the mocks would return the same data as list_servers_detail. OpenStack does not return all of this data so you must reload a server after creation to get the necessary data when using fog for real. Now the mock returns the same detail as the real call with the extra detail stored in the mock data for retrieval by list_servers_detail. --- .../requests/compute/create_server.rb | 19 ++++++++++++++----- .../requests/compute/server_tests.rb | 12 ++++++++++-- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/fog/openstack/requests/compute/create_server.rb b/lib/fog/openstack/requests/compute/create_server.rb index 644bc3ca8..890a82a50 100644 --- a/lib/fog/openstack/requests/compute/create_server.rb +++ b/lib/fog/openstack/requests/compute/create_server.rb @@ -55,10 +55,12 @@ module Fog response = Excon::Response.new response.status = 202 - data = { + server_id = Fog::Mock.random_numbers(6).to_s + + mock_data = { 'addresses' => {}, 'flavor' => {"id" => flavor_ref, "links"=>[{"href"=>"http://nova1:8774/admin/flavors/1", "rel"=>"bookmark"}]}, - 'id' => Fog::Mock.random_numbers(6).to_s, + 'id' => server_id, 'image' => {"id" => image_ref, "links"=>[{"href"=>"http://nova1:8774/admin/images/#{image_ref}", "rel"=>"bookmark"}]}, 'links' => [{"href"=>"http://nova1:8774/v1.1/admin/servers/5", "rel"=>"self"}, {"href"=>"http://nova1:8774/admin/servers/5", "rel"=>"bookmark"}], 'hostId' => "123456789ABCDEF01234567890ABCDEF", @@ -70,11 +72,18 @@ module Fog 'status' => 'BUILD', 'created' => '2012-09-27T00:04:18Z', 'updated' => '2012-09-27T00:04:27Z', + 'user_id' => @openstack_username, } - self.data[:last_modified][:servers][data['id']] = Time.now - self.data[:servers][data['id']] = data - response.body = { 'server' => data.merge({'adminPass' => 'password'}) } + response_data = { + 'adminPass' => 'password', + 'id' => server_id, + 'links' => mock_data['links'], + } + + self.data[:last_modified][:servers][server_id] = Time.now + self.data[:servers][server_id] = mock_data + response.body = { 'server' => response_data } response end diff --git a/tests/openstack/requests/compute/server_tests.rb b/tests/openstack/requests/compute/server_tests.rb index a173e6aee..8d4617f3a 100644 --- a/tests/openstack/requests/compute/server_tests.rb +++ b/tests/openstack/requests/compute/server_tests.rb @@ -14,7 +14,15 @@ Shindo.tests('Fog::Compute[:openstack] | server requests', ['openstack']) do 'accessIPv6' => Fog::Nullable::String, 'links' => Array, 'created' => String, - 'updated' => String + 'updated' => String, + 'user_id' => String, + } + + @create_format = { + 'adminPass' => String, + 'id' => String, + 'links' => Array, + 'security_groups' => Fog::Nullable::Array, } @image_format = { @@ -37,7 +45,7 @@ Shindo.tests('Fog::Compute[:openstack] | server requests', ['openstack']) do @snapshot_id = nil @flavor_id = 2 - tests('#create_server("test", #{@image_id} , 19)').formats(@server_format.merge('adminPass' => String), false) do + tests('#create_server("test", #{@image_id} , 19)').formats(@create_format, false) do data = Fog::Compute[:openstack].create_server("test", @image_id, @flavor_id).body['server'] @server_id = data['id'] data From b430ec6a20eb1216130df5024e32d18c4a60c3d6 Mon Sep 17 00:00:00 2001 From: Eric Hodel Date: Thu, 6 Dec 2012 14:58:23 -0800 Subject: [PATCH 2/4] Added mock for Fog::Identity#get_user_by_name OpenStack does not support filtering by name as in the real method so the mock returns all users regardless of the name query parameter. See: http://docs.openstack.org/api/openstack-identity-service/2.0/content/GET_listUsers_v2.0_users_Admin_API_Service_Developer_Operations-d1e1356.html --- lib/fog/openstack/requests/identity/get_user_by_name.rb | 9 +++++++++ tests/openstack/requests/identity/user_tests.rb | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/fog/openstack/requests/identity/get_user_by_name.rb b/lib/fog/openstack/requests/identity/get_user_by_name.rb index 7cb4cb77f..7b7da6814 100644 --- a/lib/fog/openstack/requests/identity/get_user_by_name.rb +++ b/lib/fog/openstack/requests/identity/get_user_by_name.rb @@ -15,6 +15,15 @@ module Fog class Mock + def get_user_by_name(name) + response = Excon::Response.new + response.status = 200 + + response.body = { + 'users' => self.data[:users].values + } + response + end end diff --git a/tests/openstack/requests/identity/user_tests.rb b/tests/openstack/requests/identity/user_tests.rb index cf1659298..6954aa707 100644 --- a/tests/openstack/requests/identity/user_tests.rb +++ b/tests/openstack/requests/identity/user_tests.rb @@ -17,6 +17,14 @@ Shindo.tests('Fog::Identity[:openstack] | user requests', ['openstack']) do Fog::Identity[:openstack].list_users.body end + tests('#get_user_by_id').formats(@user_format) do + Fog::Identity[:openstack].get_user_by_id(@user['id']).body['user'] + end + + tests('#get_user_by_name').formats({'users' => [@user_format]}) do + Fog::Identity[:openstack].get_user_by_name(@user['name']).body + end + tests("#update_user(#{@user['id']}, :name => 'fogupdateduser')").succeeds do Fog::Identity[:openstack].update_user(@user['id'], :name => 'fogupdateduser', :email => 'fog@test.com') end From 6628281f5aea1007f296959db997cf45cc586d1a Mon Sep 17 00:00:00 2001 From: Eric Hodel Date: Thu, 6 Dec 2012 15:01:22 -0800 Subject: [PATCH 3/4] Removed extra whitespace from previous commit --- lib/fog/openstack/requests/identity/get_user_by_name.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/fog/openstack/requests/identity/get_user_by_name.rb b/lib/fog/openstack/requests/identity/get_user_by_name.rb index 7b7da6814..ff220c229 100644 --- a/lib/fog/openstack/requests/identity/get_user_by_name.rb +++ b/lib/fog/openstack/requests/identity/get_user_by_name.rb @@ -25,7 +25,6 @@ module Fog response end - end end end From 4a0f5f4feac51dd4640ab9418500d4ac1aaef899 Mon Sep 17 00:00:00 2001 From: Eric Hodel Date: Thu, 6 Dec 2012 15:17:03 -0800 Subject: [PATCH 4/4] Store the user_id in the server mock data OpenStack returns the user_id, not the username, in the list_servers_detail response. This commit looks up or creates a user to retrieve its id so it matches real OpenStack behavior. --- .../openstack/requests/compute/create_server.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/fog/openstack/requests/compute/create_server.rb b/lib/fog/openstack/requests/compute/create_server.rb index 890a82a50..bd98b7d91 100644 --- a/lib/fog/openstack/requests/compute/create_server.rb +++ b/lib/fog/openstack/requests/compute/create_server.rb @@ -56,6 +56,22 @@ module Fog response.status = 202 server_id = Fog::Mock.random_numbers(6).to_s + identity = Fog::Identity[:openstack] + user = identity.users.find { |u| + u.name == @openstack_username + } + + user_id = if user then + user.id + else + identity.user.create(:name => @openstack_username, + :password => 'password', + :email => + "#{@openstack_username}@example", + :tenant_id => @openstack_tenant, + :enabled => true).id + end + mock_data = { 'addresses' => {},