Permalink
Browse files

Add tablespace_owner_is()`.

Ref #40.
  • Loading branch information...
theory committed Jan 25, 2013
1 parent 1ab4dfa commit 4e425daf810ed0c9543c87f3be4a48e232d9f233
Showing with 159 additions and 3 deletions.
  1. +3 −1 Changes
  2. +35 −0 doc/pgtap.mmd
  3. +34 −0 sql/pgtap--0.92.0--0.93.0.sql
  4. +34 −0 sql/pgtap.sql.in
  5. +13 −1 test/expected/ownership.out
  6. +40 −1 test/sql/ownership.sql
View
@@ -3,7 +3,9 @@ Revision history for pgTAP
0.93.0
---------------------------
-* Added `schema_owner_is()`. Not sure how I overlooked adding it in v0.92.0.
+* Added additional ownership-testing functions:
+ + `tablespace_owner_is()`
+ + `schema_owner_is()`
* Fixed misselling of "constraint" in constraint test diagnostic output.
* Fixed `fk_ok()` so that the table must be visible in the search path when
the schema name is not specified.
View
@@ -5254,6 +5254,41 @@ diagnostics will look something like:
# have: postgres
# want: root
+### `tablespace_owner_is ()` ###
+
+ SELECT tablespace_owner_is ( :tablespacename, :user, :description );
+ SELECT tablespace_owner_is ( :tablespacename, :user );
+
+**Parameters**
+
+`:tablespacename`
+: Name of a tablespace.
+
+`:user`
+: Name of a user.
+
+`:description`
+: A short description of the test.
+
+Tests the ownership of the tablespace. If the `:description` argument is
+omitted, an appropriate description will be created. Examples:
+
+ SELECT tablespace_owner_is( 'myts', 'joe', 'Joe has mytablespace' );
+ SELECT tablespace_owner_is( 'pg_default', current_user );
+
+In the event that the test fails because the tablespace in question does not
+actually exist, you will see an appropriate diagnostic such as:
+
+ # Failed test 16: "Tablespace ssd should be owned by www"
+ # Tablespace ssd does not exist
+
+If the test fails because the tablespace is not owned by the specified user,
+the diagnostics will look something like:
+
+ # Failed test 17: "Tablespace raid_hds should be owned by root"
+ # have: postgres
+ # want: root
+
### `relation_owner_is ()` ###
SELECT relation_owner_is ( :schema, :relation, :user, :description );
@@ -330,3 +330,37 @@ RETURNS TEXT AS $$
$2
);
$$ LANGUAGE sql;
+
+-- _get_tablespace_owner( tablespace )
+CREATE OR REPLACE FUNCTION _get_tablespace_owner( NAME )
+RETURNS NAME AS $$
+ SELECT pg_catalog.pg_get_userbyid(spcowner)
+ FROM pg_catalog.pg_tablespace
+ WHERE spcname = $1;
+$$ LANGUAGE SQL;
+
+-- tablespace_owner_is ( tablespace, user, description )
+CREATE OR REPLACE FUNCTION tablespace_owner_is ( NAME, NAME, TEXT )
+RETURNS TEXT AS $$
+DECLARE
+ owner NAME := _get_tablespace_owner($1);
+BEGIN
+ -- Make sure the tablespace exists.
+ IF owner IS NULL THEN
+ RETURN ok(FALSE, $3) || E'\n' || diag(
+ E' Tablespace ' || quote_ident($1) || ' does not exist'
+ );
+ END IF;
+
+ RETURN is(owner, $2, $3);
+END;
+$$ LANGUAGE plpgsql;
+
+-- tablespace_owner_is ( tablespace, user )
+CREATE OR REPLACE FUNCTION tablespace_owner_is ( NAME, NAME )
+RETURNS TEXT AS $$
+ SELECT tablespace_owner_is(
+ $1, $2,
+ 'Tablespace ' || quote_ident($1) || ' should be owned by ' || quote_ident($2)
+ );
+$$ LANGUAGE sql;
View
@@ -7942,6 +7942,40 @@ RETURNS TEXT AS $$
);
$$ LANGUAGE sql;
+-- _get_tablespace_owner( tablespace )
+CREATE OR REPLACE FUNCTION _get_tablespace_owner( NAME )
+RETURNS NAME AS $$
+ SELECT pg_catalog.pg_get_userbyid(spcowner)
+ FROM pg_catalog.pg_tablespace
+ WHERE spcname = $1;
+$$ LANGUAGE SQL;
+
+-- tablespace_owner_is ( tablespace, user, description )
+CREATE OR REPLACE FUNCTION tablespace_owner_is ( NAME, NAME, TEXT )
+RETURNS TEXT AS $$
+DECLARE
+ owner NAME := _get_tablespace_owner($1);
+BEGIN
+ -- Make sure the tablespace exists.
+ IF owner IS NULL THEN
+ RETURN ok(FALSE, $3) || E'\n' || diag(
+ E' Tablespace ' || quote_ident($1) || ' does not exist'
+ );
+ END IF;
+
+ RETURN is(owner, $2, $3);
+END;
+$$ LANGUAGE plpgsql;
+
+-- tablespace_owner_is ( tablespace, user )
+CREATE OR REPLACE FUNCTION tablespace_owner_is ( NAME, NAME )
+RETURNS TEXT AS $$
+ SELECT tablespace_owner_is(
+ $1, $2,
+ 'Tablespace ' || quote_ident($1) || ' should be owned by ' || quote_ident($2)
+ );
+$$ LANGUAGE sql;
+
CREATE OR REPLACE FUNCTION _assets_are ( text, text[], text[], TEXT )
RETURNS TEXT AS $$
SELECT _areni(
@@ -1,5 +1,5 @@
\unset ECHO
-1..240
+1..252
ok 1 - db_owner_is(db, user, desc) should pass
ok 2 - db_owner_is(db, user, desc) should have the proper description
ok 3 - db_owner_is(db, user, desc) should have the proper diagnostics
@@ -240,3 +240,15 @@ ok 237 - function_owner_is(sch, function, args[integer], non-user, desc) should
ok 238 - function_owner_is(function, args[integer], non-user, desc) should fail
ok 239 - function_owner_is(function, args[integer], non-user, desc) should have the proper description
ok 240 - function_owner_is(function, args[integer], non-user, desc) should have the proper diagnostics
+ok 241 - tablespace_owner_is(tablespace, user, desc) should pass
+ok 242 - tablespace_owner_is(tablespace, user, desc) should have the proper description
+ok 243 - tablespace_owner_is(tablespace, user, desc) should have the proper diagnostics
+ok 244 - tablespace_owner_is(tablespace, user) should pass
+ok 245 - tablespace_owner_is(tablespace, user) should have the proper description
+ok 246 - tablespace_owner_is(tablespace, user) should have the proper diagnostics
+ok 247 - tablespace_owner_is(non-tablespace, user) should fail
+ok 248 - tablespace_owner_is(non-tablespace, user) should have the proper description
+ok 249 - tablespace_owner_is(non-tablespace, user) should have the proper diagnostics
+ok 250 - tablespace_owner_is(tablespace, non-user) should fail
+ok 251 - tablespace_owner_is(tablespace, non-user) should have the proper description
+ok 252 - tablespace_owner_is(tablespace, non-user) should have the proper diagnostics
View
@@ -1,7 +1,7 @@
\unset ECHO
\i test/setup.sql
-SELECT plan(240);
+SELECT plan(252);
--SELECT * FROM no_plan();
-- This will be rolled back. :-)
@@ -29,6 +29,7 @@ CREATE FUNCTION public.somefunction(int) RETURNS VOID LANGUAGE SQL AS '';
RESET client_min_messages;
/****************************************************************************/
+-- Test db_owner_is().
SELECT * FROM check_test(
db_owner_is(current_database(), current_user, 'mumble'),
true,
@@ -63,6 +64,7 @@ SELECT * FROM check_test(
);
/****************************************************************************/
+-- Test schema_owner_is().
SELECT * FROM check_test(
schema_owner_is(current_schema(), _get_schema_owner(current_schema()), 'mumble'),
true,
@@ -786,6 +788,43 @@ SELECT * FROM check_test(
want: no one'
);
+/****************************************************************************/
+-- Test tablespace_owner_is().
+
+SELECT * FROM check_test(
+ tablespace_owner_is('pg_default', _get_tablespace_owner('pg_default'), 'mumble'),
+ true,
+ 'tablespace_owner_is(tablespace, user, desc)',
+ 'mumble',
+ ''
+);
+
+SELECT * FROM check_test(
+ tablespace_owner_is('pg_default', _get_tablespace_owner('pg_default')),
+ true,
+ 'tablespace_owner_is(tablespace, user)',
+ 'Tablespace ' || quote_ident('pg_default') || ' should be owned by ' || _get_tablespace_owner('pg_default'),
+ ''
+);
+
+SELECT * FROM check_test(
+ tablespace_owner_is('__not__' || 'pg_default', _get_tablespace_owner('pg_default'), 'mumble'),
+ false,
+ 'tablespace_owner_is(non-tablespace, user)',
+ 'mumble',
+ ' Tablespace __not__' || 'pg_default' || ' does not exist'
+);
+
+SELECT * FROM check_test(
+ tablespace_owner_is('pg_default', '__not__' || _get_tablespace_owner('pg_default'), 'mumble'),
+ false,
+ 'tablespace_owner_is(tablespace, non-user)',
+ 'mumble',
+ ' have: ' || _get_tablespace_owner('pg_default') || '
+ want: __not__' || _get_tablespace_owner('pg_default')
+);
+
+
/****************************************************************************/
-- Finish the tests and clean up.
SELECT * FROM finish();

0 comments on commit 4e425da

Please sign in to comment.