Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
add unit test cases to cover functions used by manage_object_ownership
Browse files Browse the repository at this point in the history
Change-Id: I7d5c2abfd2d9101b4e725b7db15e813363479a8c
  • Loading branch information
figo committed Feb 9, 2012
1 parent ebbd869 commit e32b6c1
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
5 changes: 2 additions & 3 deletions postgresql/lib/postgresql_service/node.rb
Expand Up @@ -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
Expand All @@ -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)
Expand Down
64 changes: 64 additions & 0 deletions postgresql/spec/postgresql_node_spec.rb
Expand Up @@ -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)")
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e32b6c1

Please sign in to comment.