Permalink
Browse files

Check & purge orphan for postgresql

Change-Id: Ib968d6e190ecc447f90f827138883721f717da9f
  • Loading branch information...
1 parent c1cd0f7 commit 1b2aef34d5cc38b39cb48d4830c2f2d168a0cafa felixhoo committed Nov 2, 2011
@@ -18,3 +18,5 @@ mbus: nats://localhost:4222
pid: /var/vcap/sys/run/postgresql_service.pid
node_timeout: 2
# z_interval: 30
+# check_orphan_interval: 3600
+# double_check_orphan_interval: 300
@@ -97,6 +97,24 @@ def announcement
a
end
+ def all_instances_list
+ Provisionedservice.all.map{|s| s.name}
+ end
+
+ def all_bindings_list
+ res = []
+ Provisionedservice.all.each do |provisionedservice|
+ provisionedservice.bindusers.all.each do |binduser|
+ res << {
+ "name" => provisionedservice.name,
+ "username" => binduser.user,
+ "user" => binduser.user
+ }
+ end
+ end
+ res
+ end
+
def check_db_consistency()
db_list = []
@connection.query('select datname,datacl from pg_database').each{|message|
@@ -305,11 +323,11 @@ def bind(name, bind_opts, credential=nil)
def unbind(credential)
return if credential.nil?
@logger.info("Unbind service: #{credential.inspect}")
- name, user, bind_opts,passwd = %w(name user bind_opts password).map{|k| credential[k]}
+ name, user, bind_opts = %w(name user bind_opts).map{|k| credential[k]}
provisionedservice = Provisionedservice.get(name)
raise PostgresqlError.new(PostgresqlError::POSTGRESQL_CONFIG_NOT_FOUND, name) unless provisionedservice
# validate the existence of credential, in case we delete a normal account because of a malformed credential
- res = @connection.query("SELECT count(*) from pg_authid WHERE rolname='#{user}' AND rolpassword='md5'||MD5('#{passwd}#{user}')")
+ res = @connection.query("SELECT count(*) from pg_authid WHERE rolname='#{user}'")
raise PostgresqlError.new(PostgresqlError::POSTGRESQL_CRED_NOT_FOUND, credential.inspect) if res[0]['count'].to_i<=0
unbinduser = provisionedservice.bindusers.get(user)
if unbinduser != nil then
@@ -99,6 +99,45 @@ class PostgresqlError
end
end
+ it "should return correct instances & bindings list" do
+ EM.run do
+ before_ins_list = @node.all_instances_list
+ tmp_db = @node.provision(@default_plan)
+ @test_dbs[tmp_db] = []
+ after_ins_list = @node.all_instances_list
+ before_ins_list << tmp_db["name"]
+ (before_ins_list.sort == after_ins_list.sort).should be_true
+
+ before_bind_list = @node.all_bindings_list
+ tmp_bind = @node.bind(tmp_db["name"], @default_opts)
+ @test_dbs[tmp_db] << tmp_bind
+ after_bind_list = @node.all_bindings_list
+ before_bind_list << tmp_bind
+ a, b = [after_bind_list, before_bind_list].map do |list|
+ list.map { |item| item["username"] }.sort
+ end
+ (a == b).should be_true
+
+ EM.stop
+ end
+ end
+
+ it "should be able to purge the instance & binding from the all_list" do
+ EM.run do
+ tmp_db = @node.provision(@default_plan)
+ ins_list = @node.all_instances_list
+ tmp_bind = @node.bind(tmp_db["name"], @default_opts)
+ bind_list = @node.all_bindings_list
+ oi = ins_list.find { |ins| ins == tmp_db["name"] }
+ ob = bind_list.find { |bind| bind["name"] == tmp_bind["name"] and bind["username"] == tmp_bind["username"] }
+ oi.should_not be_nil
+ ob.should_not be_nil
+ expect { @node.unbind(ob) }.should_not raise_error
+ expect { @node.unprovision(oi, []) }.should_not raise_error
+ EM.stop
+ end
+ end
+
it "should calculate available storage correctly" do
EM.run do
original= @node.available_storage
@@ -192,11 +231,6 @@ class PostgresqlError
credential = @node.bind(@db["name"], @default_opts)
credential.should_not == nil
@test_dbs[@db] << credential
- invalid_credential = credential.dup
- invalid_credential["password"] = 'fake'
- expect {
- @node.unbind(invalid_credential)
- }.should raise_error(PostgresqlError, /Postgresql credential .* not found/)
# nil input
@node.unbind(nil).should == nil

0 comments on commit 1b2aef3

Please sign in to comment.