Browse files

add unit test cases to cover functions used by manage_object_ownership

Change-Id: I7d5c2abfd2d9101b4e725b7db15e813363479a8c
  • Loading branch information...
1 parent ebbd869 commit e32b6c11d817d0e3a50cfd584d2ec43d12bda2c1 figo committed Feb 8, 2012
Showing with 66 additions and 3 deletions.
  1. +2 −3 postgresql/lib/postgresql_service/node.rb
  2. +64 −0 postgresql/spec/postgresql_node_spec.rb
View
5 postgresql/lib/postgresql_service/node.rb
@@ -140,7 +140,6 @@ def get_unruly_children(connection, parent, children)
# children which are not in fact children of the parent. (we don't
# handle children that somehow have the *wrong* parent, but that
# won't happen :-)
- ruly_children = []
query = <<-end_of_query
SELECT rolname
FROM pg_roles
@@ -154,8 +153,8 @@ def get_unruly_children(connection, parent, children)
)
);
end_of_query
- unruly_children = children - connection.query(query).map { |row| row['rolname'] }
- unruly_children
+ ruly_children = connection.query(query).map { |row| row['rolname'] }
+ children - ruly_children
end
def manage_object_ownership(name)
View
64 postgresql/spec/postgresql_node_spec.rb
@@ -327,6 +327,7 @@ class PostgresqlError
sleep 1
EM.add_timer(0.1) do
db = node.provision('free')
+ @test_dbs[db] = []
conn = connect_to_postgresql(db)
# prepare a transaction and not commit
conn.query("create table a(id int)")
@@ -619,6 +620,69 @@ class PostgresqlError
end
end
+ it "should get expected children correctly" do
+ EM.run do
+ bind = @node.bind @db['name'], @default_opts
+ children = @node.get_expected_children @db['name']
+ children.index(bind['user']).should_not == nil
+ children.index(@db['user']).should == nil
+ EM.stop
+ end
+ end
+
+ it "should get actual children correctly" do
+ EM.run do
+ # sys_user is not return from provision/bind response
+ # so only set user for parent
+ parent = VCAP::Services::Postgresql::Node::Binduser.new
+ parent.user = @db['user']
+ @db['user'] = @opts[:postgresql]['user']
+ @db['password'] = @opts[:postgresql]['pass']
+ sys_conn = connect_to_postgresql @db
+ user = @node.bind @db['name'], @default_opts
+
+ # this parent does not contain sys_user
+ children = @node.get_actual_children sys_conn, @db['name'], parent
+ sys_conn.close if sys_conn
+ children.index('').should == nil
+ children.index(parent.user).should == nil
+ children.index(@opts[:postgresql]['user']).should == nil
+ children.index(user['user']).should_not == nil
+ # should only have 2 sys_user in children
+ # one for parent and the other for new binding
+ num_sys_user = 0
+ children.each do |child|
+ num_sys_user+=1 if child.index 'su'
+ end
+ num_sys_user.should == 2
+ EM.stop
+ end
+ end
+
+ it "should get unruly children correctly" do
+ EM.run do
+ parent = VCAP::Services::Postgresql::Node::Binduser.new
+ parent.user = @db['user']
+ bind1 = @node.bind @db['name'], @default_opts
+ bind2 = VCAP::Services::Postgresql::Node::Binduser.new
+ bind2.user = "u-#{UUIDTools::UUID.random_create.to_s}".gsub(/-/, '')
+
+ @db['user'] = @opts[:postgresql]['user']
+ @db['password'] = @opts[:postgresql]['pass']
+ sys_conn = connect_to_postgresql @db
+ sys_conn.query "create role #{bind2.user}"
+
+ children = []
+ children << bind1['user']
+ children << bind2['user']
+ unruly_children = @node.get_unruly_children sys_conn, parent, children
+ sys_conn.close if sys_conn
+ unruly_children.index(bind1['user']).should == nil
+ unruly_children.index(bind2['user']).should_not == nil
+ EM.stop
+ end
+ end
+
it "should be able to migrate(manage object owner) legacy instances" do
EM.run do
# create a regular user through node

0 comments on commit e32b6c1

Please sign in to comment.