From c8bf1917f5f40e4e28cff2149c01d9cd55966f2e Mon Sep 17 00:00:00 2001 From: Timothy Messier Date: Wed, 28 Jul 2021 11:05:11 -0400 Subject: [PATCH] db: Refactor test setup for pre-populated data Now when initializing the test database for sql tests, it will load the colors persona data into the database. This will provide a data set that tests can use without needing to call `wtt_load`. The main motivation is to allow for a separation of setup and test execution that requires crossing a transaction boundary. The pgTap tests are required to exectue as a single transaction, so that the rollback can reset the state, and each test can be isolated. However, can cause issues with some test senarios, particuarly with the use of some times releated functions like `current_timestamp` and `now` which return the time that the current transaction started. This creates an issue if a test relies on some setup that sets a time via `now`, then an update that expects a new time via `now`. See: https://www.postgresql.org/docs/13/functions-datetime.html#FUNCTIONS-DATETIME-CURRENT --- .../db/sqltest/initdb.d/01_colors_persona.sql | 172 ++++++++++++++ .../db/sqltest/initdb.d/02_colors_persona.sql | 216 ------------------ .../{01_wtt_load.sql => 02_wtt_load.sql} | 0 .../sqltest/initdb.d/03_widgets_persona.sql | 39 ++-- internal/db/sqltest/tests/setup/wtt_load.sql | 8 +- .../sqltest/tests/setup/wtt_load_colors.sql | 7 - 6 files changed, 196 insertions(+), 246 deletions(-) create mode 100644 internal/db/sqltest/initdb.d/01_colors_persona.sql delete mode 100644 internal/db/sqltest/initdb.d/02_colors_persona.sql rename internal/db/sqltest/initdb.d/{01_wtt_load.sql => 02_wtt_load.sql} (100%) delete mode 100644 internal/db/sqltest/tests/setup/wtt_load_colors.sql diff --git a/internal/db/sqltest/initdb.d/01_colors_persona.sql b/internal/db/sqltest/initdb.d/01_colors_persona.sql new file mode 100644 index 0000000000..1abed6164f --- /dev/null +++ b/internal/db/sqltest/initdb.d/01_colors_persona.sql @@ -0,0 +1,172 @@ +begin; + -- Add organizations + insert into iam_scope + (parent_id, type, public_id, name) + values + ('global', 'org', 'o_____colors', 'Colors R Us'); + + -- Add projects to the organizations + insert into iam_scope + (parent_id, type, public_id, name) + values + ('o_____colors', 'project', 'p____bcolors', 'Blue Color Mill'), + ('o_____colors', 'project', 'p____rcolors', 'Red Color Mill'); + + -- Add global users + insert into iam_user + (scope_id, public_id, name) + values + ('global', 'u_______gary', 'Gary'), + ('global', 'u_______gina', 'Gina'), + ('global', 'u______nancy', 'Nancy'); + + -- Add organization users + insert into iam_user + (scope_id, public_id, name) + values + ('o_____colors', 'u______clare', 'Clare'), + ('o_____colors', 'u______cindy', 'Cindy'), + ('o_____colors', 'u______carly', 'Carly'), + ('o_____colors', 'u______ciara', 'Ciara'); + + insert into iam_group + (scope_id, public_id, name) + values + ('global', 'g___gg-group', 'Global Group'), + ('o_____colors', 'g___oc-group', 'Colors R Us Group'), + ('p____bcolors', 'g___cb-group', 'Blue Color Group'), + ('p____rcolors', 'g___cr-group', 'Red Color Group'); + + insert into iam_group_member_user + (group_id, member_id) + values + ('g___gg-group', 'u_______gary'), + ('g___oc-group', 'u______clare'), + ('g___cb-group', 'u______cindy'), + ('g___cr-group', 'u______carly'); + + insert into iam_role + (scope_id, grant_scope_id, public_id, name) + values + ('p____bcolors', 'p____bcolors', 'r_pp_bc__mix', 'Color Mixer'), + ('p____rcolors', 'p____rcolors', 'r_pp_rc__mix', 'Color Mixer'), + ('o_____colors', 'p____bcolors', 'r_op_bc__art', 'Blue Color Artist'), + ('o_____colors', 'p____rcolors', 'r_op_rc__art', 'Red Color Artist'), + ('o_____colors', 'o_____colors', 'r_oo_____art', 'Color Artist'), + ('global', 'o_____colors', 'r_go____name', 'Color Namer'), + ('global', 'p____bcolors', 'r_gp____spec', 'Blue Color Inspector'), + ('global', 'global', 'r_gg_____buy', 'Purchaser'), + ('global', 'global', 'r_gg____shop', 'Shopper'); + + insert into iam_role_grant + (role_id, canonical_grant, raw_grant) + values + ('r_gg_____buy', 'type=*;action=purchase', 'purchase anything'), + ('r_gg____shop', 'type=*;action=view', 'view anything'), + ('r_go____name', 'type=color;action=name', 'name colors'), + ('r_gp____spec', 'type=color;action=inspect', 'inspect colors'), + ('r_oo_____art', 'type=color;action=create', 'create color'), + ('r_op_bc__art', 'type=color;action=create', 'create color'), + ('r_op_rc__art', 'type=color;action=create', 'create color'), + ('r_pp_bc__mix', 'type=color;action=mix', 'mix color'), + ('r_pp_rc__mix', 'type=color;action=mix', 'mix color'); + + insert into iam_group_role + (role_id, principal_id) + values + ('r_op_rc__art', 'g___oc-group'), -- color + ('r_pp_bc__mix', 'g___cb-group'), -- color + ('r_pp_rc__mix', 'g___cr-group'); -- color + + insert into iam_user_role + (role_id, principal_id) + values + ('r_go____name', 'u_______gary'), + ('r_gp____spec', 'u_______gina'), + ('r_gg_____buy', 'u_auth'), + ('r_gg____shop', 'u_anon'); + + insert into auth_password_conf + (password_method_id, private_id) + values + ('apm___colors', 'apmc__colors'); + + + -- Add password auth method to organizations + insert into auth_password_method + (scope_id, public_id, password_conf_id, name) + values + ('o_____colors', 'apm___colors', 'apmc__colors', 'Colors Auth Password'); + + insert into auth_password_account + (auth_method_id, public_id, login_name) + values + ('apm___colors', 'apa____clare', 'clare'), + ('apm___colors', 'apa____cindy', 'cindy'), + ('apm___colors', 'apa____carly', 'carly'), + ('apm___colors', 'apa____ciara', 'ciara'); + + update auth_account set iam_user_id = 'u______clare' where public_id = 'apa____clare'; + update auth_account set iam_user_id = 'u______cindy' where public_id = 'apa____cindy'; + update auth_account set iam_user_id = 'u______carly' where public_id = 'apa____carly'; + update auth_account set iam_user_id = 'u______ciara' where public_id = 'apa____ciara'; + + insert into static_host_catalog + (scope_id, public_id, name) + values + ('p____bcolors', 'c___cb-sthcl', 'Blue Color Static Catalog'), + ('p____rcolors', 'c___cr-sthcl', 'Red Color Static Catalog'); + + insert into static_host + (catalog_id, public_id, address) + values + ('c___cb-sthcl', 'h_____cb__01', '1.blue.color'), + ('c___cb-sthcl', 'h_____cb__02', '2.blue.color'), + ('c___cb-sthcl', 'h_____cb__03', '3.blue.color'), + ('c___cb-sthcl', 'h_____cb__04', '4.blue.color'), + ('c___cb-sthcl', 'h_____cb__05', '5.blue.color'), + ('c___cb-sthcl', 'h_____cb__06', '6.blue.color'), + ('c___cb-sthcl', 'h_____cb__07', '7.blue.color'), + ('c___cb-sthcl', 'h_____cb__08', '8.blue.color'), + ('c___cb-sthcl', 'h_____cb__09', '9.blue.color'), + + ('c___cr-sthcl', 'h_____cr__01', '1.red.color'), + ('c___cr-sthcl', 'h_____cr__02', '2.red.color'), + ('c___cr-sthcl', 'h_____cr__03', '3.red.color'), + ('c___cr-sthcl', 'h_____cr__04', '4.red.color'), + ('c___cr-sthcl', 'h_____cr__05', '5.red.color'), + ('c___cr-sthcl', 'h_____cr__06', '6.red.color'), + ('c___cr-sthcl', 'h_____cr__07', '7.red.color'), + ('c___cr-sthcl', 'h_____cr__08', '8.red.color'), + ('c___cr-sthcl', 'h_____cr__09', '9.red.color'); + + insert into static_host_set + (catalog_id, public_id, name) + values + ('c___cb-sthcl', 's___1cb-sths', 'Blue Color Static Set 1'), + ('c___cb-sthcl', 's___2cb-sths', 'Blue Color Static Set 2'), + ('c___cr-sthcl', 's___1cr-sths', 'Red Color Static Set 1'), + ('c___cr-sthcl', 's___2cr-sths', 'Red Color Static Set 2'); + + insert + into static_host_set_member + ( host_id, set_id, catalog_id) + select h.public_id, s.public_id, s.catalog_id + from static_host as h, + static_host_set as s + where h.catalog_id = s.catalog_id; + + insert into target_tcp + (scope_id, public_id, name) + values + ('p____bcolors', 't_________cb', 'Blue Color Target'), + ('p____rcolors', 't_________cr', 'Red Color Target'); + + insert into target_host_set + (target_id, host_set_id) + values + ('t_________cb', 's___1cb-sths'), + ('t_________cb', 's___2cb-sths'), + ('t_________cr', 's___1cr-sths'), + ('t_________cr', 's___2cr-sths'); +commit; diff --git a/internal/db/sqltest/initdb.d/02_colors_persona.sql b/internal/db/sqltest/initdb.d/02_colors_persona.sql deleted file mode 100644 index 16046005da..0000000000 --- a/internal/db/sqltest/initdb.d/02_colors_persona.sql +++ /dev/null @@ -1,216 +0,0 @@ -begin; - -- _wtt_load_colors_iam populates all iam_ tables for the colors persona. - -- iam does not depend on any other aggregates, but others depend on it, - -- as such is it should be first in the list. - create function _wtt_load_colors_iam() - returns void - as $$ - begin - -- Add organizations - insert into iam_scope - (parent_id, type, public_id, name) - values - ('global', 'org', 'o_____colors', 'Colors R Us'); - - -- Add projects to the organizations - insert into iam_scope - (parent_id, type, public_id, name) - values - ('o_____colors', 'project', 'p____bcolors', 'Blue Color Mill'), - ('o_____colors', 'project', 'p____rcolors', 'Red Color Mill'); - - -- Add global users - insert into iam_user - (scope_id, public_id, name) - values - ('global', 'u_______gary', 'Gary'), - ('global', 'u_______gina', 'Gina'), - ('global', 'u______nancy', 'Nancy'); - - -- Add organization users - insert into iam_user - (scope_id, public_id, name) - values - ('o_____colors', 'u______clare', 'Clare'), - ('o_____colors', 'u______cindy', 'Cindy'), - ('o_____colors', 'u______carly', 'Carly'), - ('o_____colors', 'u______ciara', 'Ciara'); - - insert into iam_group - (scope_id, public_id, name) - values - ('global', 'g___gg-group', 'Global Group'), - ('o_____colors', 'g___oc-group', 'Colors R Us Group'), - ('p____bcolors', 'g___cb-group', 'Blue Color Group'), - ('p____rcolors', 'g___cr-group', 'Red Color Group'); - - insert into iam_group_member_user - (group_id, member_id) - values - ('g___gg-group', 'u_______gary'), - ('g___oc-group', 'u______clare'), - ('g___cb-group', 'u______cindy'), - ('g___cr-group', 'u______carly'); - - insert into iam_role - (scope_id, grant_scope_id, public_id, name) - values - ('p____bcolors', 'p____bcolors', 'r_pp_bc__mix', 'Color Mixer'), - ('p____rcolors', 'p____rcolors', 'r_pp_rc__mix', 'Color Mixer'), - ('o_____colors', 'p____bcolors', 'r_op_bc__art', 'Blue Color Artist'), - ('o_____colors', 'p____rcolors', 'r_op_rc__art', 'Red Color Artist'), - ('o_____colors', 'o_____colors', 'r_oo_____art', 'Color Artist'), - ('global', 'o_____colors', 'r_go____name', 'Color Namer'), - ('global', 'p____bcolors', 'r_gp____spec', 'Blue Color Inspector'), - ('global', 'global', 'r_gg_____buy', 'Purchaser'), - ('global', 'global', 'r_gg____shop', 'Shopper'); - - insert into iam_role_grant - (role_id, canonical_grant, raw_grant) - values - ('r_gg_____buy', 'type=*;action=purchase', 'purchase anything'), - ('r_gg____shop', 'type=*;action=view', 'view anything'), - ('r_go____name', 'type=color;action=name', 'name colors'), - ('r_gp____spec', 'type=color;action=inspect', 'inspect colors'), - ('r_oo_____art', 'type=color;action=create', 'create color'), - ('r_op_bc__art', 'type=color;action=create', 'create color'), - ('r_op_rc__art', 'type=color;action=create', 'create color'), - ('r_pp_bc__mix', 'type=color;action=mix', 'mix color'), - ('r_pp_rc__mix', 'type=color;action=mix', 'mix color'); - - insert into iam_group_role - (role_id, principal_id) - values - ('r_op_rc__art', 'g___oc-group'), -- color - ('r_pp_bc__mix', 'g___cb-group'), -- color - ('r_pp_rc__mix', 'g___cr-group'); -- color - - insert into iam_user_role - (role_id, principal_id) - values - ('r_go____name', 'u_______gary'), - ('r_gp____spec', 'u_______gina'), - ('r_gg_____buy', 'u_auth'), - ('r_gg____shop', 'u_anon'); - end; - $$ language plpgsql; - - -- _wtt_load_kms populates all kms_ tables for the colors persona. - -- kms depends on iam. - create function _wtt_load_colors_kms() - returns void - as $$ - begin - end; - $$ language plpgsql; - - -- _wtt_load_colors_auth populates all auth_ tables for the colors persona. - -- auth depends on iam, and kms. - create function _wtt_load_colors_auth() - returns void - as $$ - begin - insert into auth_password_conf - (password_method_id, private_id) - values - ('apm___colors', 'apmc__colors'); - - - -- Add password auth method to organizations - insert into auth_password_method - (scope_id, public_id, password_conf_id, name) - values - ('o_____colors', 'apm___colors', 'apmc__colors', 'Colors Auth Password'); - - insert into auth_password_account - (auth_method_id, public_id, login_name) - values - ('apm___colors', 'apa____clare', 'clare'), - ('apm___colors', 'apa____cindy', 'cindy'), - ('apm___colors', 'apa____carly', 'carly'), - ('apm___colors', 'apa____ciara', 'ciara'); - - update auth_account set iam_user_id = 'u______clare' where public_id = 'apa____clare'; - update auth_account set iam_user_id = 'u______cindy' where public_id = 'apa____cindy'; - update auth_account set iam_user_id = 'u______carly' where public_id = 'apa____carly'; - update auth_account set iam_user_id = 'u______ciara' where public_id = 'apa____ciara'; - - end; - $$ language plpgsql; - - -- _wtt_load_colors_hosts populates all host_ tables for the colors persona. - -- hosts depend on iam. - create function _wtt_load_colors_hosts() - returns void - as $$ - begin - insert into static_host_catalog - (scope_id, public_id, name) - values - ('p____bcolors', 'c___cb-sthcl', 'Blue Color Static Catalog'), - ('p____rcolors', 'c___cr-sthcl', 'Red Color Static Catalog'); - - insert into static_host - (catalog_id, public_id, address) - values - ('c___cb-sthcl', 'h_____cb__01', '1.blue.color'), - ('c___cb-sthcl', 'h_____cb__02', '2.blue.color'), - ('c___cb-sthcl', 'h_____cb__03', '3.blue.color'), - ('c___cb-sthcl', 'h_____cb__04', '4.blue.color'), - ('c___cb-sthcl', 'h_____cb__05', '5.blue.color'), - ('c___cb-sthcl', 'h_____cb__06', '6.blue.color'), - ('c___cb-sthcl', 'h_____cb__07', '7.blue.color'), - ('c___cb-sthcl', 'h_____cb__08', '8.blue.color'), - ('c___cb-sthcl', 'h_____cb__09', '9.blue.color'), - - ('c___cr-sthcl', 'h_____cr__01', '1.red.color'), - ('c___cr-sthcl', 'h_____cr__02', '2.red.color'), - ('c___cr-sthcl', 'h_____cr__03', '3.red.color'), - ('c___cr-sthcl', 'h_____cr__04', '4.red.color'), - ('c___cr-sthcl', 'h_____cr__05', '5.red.color'), - ('c___cr-sthcl', 'h_____cr__06', '6.red.color'), - ('c___cr-sthcl', 'h_____cr__07', '7.red.color'), - ('c___cr-sthcl', 'h_____cr__08', '8.red.color'), - ('c___cr-sthcl', 'h_____cr__09', '9.red.color'); - - insert into static_host_set - (catalog_id, public_id, name) - values - ('c___cb-sthcl', 's___1cb-sths', 'Blue Color Static Set 1'), - ('c___cb-sthcl', 's___2cb-sths', 'Blue Color Static Set 2'), - ('c___cr-sthcl', 's___1cr-sths', 'Red Color Static Set 1'), - ('c___cr-sthcl', 's___2cr-sths', 'Red Color Static Set 2'); - - insert - into static_host_set_member - ( host_id, set_id, catalog_id) - select h.public_id, s.public_id, s.catalog_id - from static_host as h, - static_host_set as s - where h.catalog_id = s.catalog_id; - end; - $$ language plpgsql; - - -- _wtt_load_colors_targets populates all target_ tables for the colors persona. - -- targets depend on iam, auth, hosts. - create function _wtt_load_colors_targets() - returns void - as $$ - begin - insert into target_tcp - (scope_id, public_id, name) - values - ('p____bcolors', 't_________cb', 'Blue Color Target'), - ('p____rcolors', 't_________cr', 'Red Color Target'); - - insert into target_host_set - (target_id, host_set_id) - values - ('t_________cb', 's___1cb-sths'), - ('t_________cb', 's___2cb-sths'), - ('t_________cr', 's___1cr-sths'), - ('t_________cr', 's___2cr-sths'); - - end; - $$ language plpgsql; -commit; diff --git a/internal/db/sqltest/initdb.d/01_wtt_load.sql b/internal/db/sqltest/initdb.d/02_wtt_load.sql similarity index 100% rename from internal/db/sqltest/initdb.d/01_wtt_load.sql rename to internal/db/sqltest/initdb.d/02_wtt_load.sql diff --git a/internal/db/sqltest/initdb.d/03_widgets_persona.sql b/internal/db/sqltest/initdb.d/03_widgets_persona.sql index 7f2c95f88b..9630ee94e4 100644 --- a/internal/db/sqltest/initdb.d/03_widgets_persona.sql +++ b/internal/db/sqltest/initdb.d/03_widgets_persona.sql @@ -20,12 +20,12 @@ begin; ('o_____widget', 'project', 'p____swidget', 'Small Widget Factory'); -- Add global users - insert into iam_user - (scope_id, public_id, name) - values - ('global', 'u_______gary', 'Gary'), - ('global', 'u_______gina', 'Gina'), - ('global', 'u______nancy', 'Nancy'); + -- insert into iam_user + -- (scope_id, public_id, name) + -- values + -- ('global', 'u_______gary', 'Gary'), + -- ('global', 'u_______gina', 'Gina'), + -- ('global', 'u______nancy', 'Nancy'); -- Add organization users insert into iam_user @@ -39,7 +39,7 @@ begin; insert into iam_group (scope_id, public_id, name) values - ('global', 'g___gg-group', 'Global Group'), + -- ('global', 'g___gg-group', 'Global Group'), ('o_____widget', 'g___ow-group', 'Widget Inc Group'), ('p____bwidget', 'g___wb-group', 'Big Widget Group'), ('p____swidget', 'g___ws-group', 'Small Widget Group'); @@ -47,7 +47,7 @@ begin; insert into iam_group_member_user (group_id, member_id) values - ('g___gg-group', 'u_______gary'), + -- ('g___gg-group', 'u_______gary'), ('g___ow-group', 'u_____walter'), ('g___wb-group', 'u_____warren'), ('g___ws-group', 'u_____waylon'); @@ -55,18 +55,18 @@ begin; insert into iam_role (scope_id, grant_scope_id, public_id, name) values + -- ('global', 'global', 'r_gg_____buy', 'Purchaser'), + -- ('global', 'global', 'r_gg____shop', 'Shopper'), ('p____bwidget', 'p____bwidget', 'r_pp_bw__bld', 'Widget Builder'), ('p____swidget', 'p____swidget', 'r_pp_sw__bld', 'Widget Builder'), ('o_____widget', 'p____swidget', 'r_op_sw__eng', 'Small Widget Engineer'), - ('o_____widget', 'o_____widget', 'r_oo_____eng', 'Widget Engineer'), - ('global', 'global', 'r_gg_____buy', 'Purchaser'), - ('global', 'global', 'r_gg____shop', 'Shopper'); + ('o_____widget', 'o_____widget', 'r_oo_____eng', 'Widget Engineer'); insert into iam_role_grant (role_id, canonical_grant, raw_grant) values - ('r_gg_____buy', 'type=*;action=purchase', 'purchase anything'), - ('r_gg____shop', 'type=*;action=view', 'view anything'), + -- ('r_gg_____buy', 'type=*;action=purchase', 'purchase anything'), + -- ('r_gg____shop', 'type=*;action=view', 'view anything'), ('r_oo_____eng', 'type=widget;action=design', 'design widget'), ('r_op_sw__eng', 'type=widget;action=design', 'design widget'), ('r_op_sw__eng', 'type=widget;action=tune', 'tune widget'), @@ -81,11 +81,11 @@ begin; ('r_pp_bw__bld', 'g___wb-group'), -- widget ('r_pp_sw__bld', 'g___ws-group'); -- widget - insert into iam_user_role - (role_id, principal_id) - values - ('r_gg_____buy', 'u_auth'), - ('r_gg____shop', 'u_anon'); + -- insert into iam_user_role + -- (role_id, principal_id) + -- values + -- ('r_gg_____buy', 'u_auth'), + -- ('r_gg____shop', 'u_anon'); end; $$ language plpgsql; @@ -234,7 +234,8 @@ begin; select h.public_id, s.public_id, s.catalog_id from static_host as h, static_host_set as s - where h.catalog_id = s.catalog_id; + where h.catalog_id = s.catalog_id + and h.address like '%.widget'; end; $$ language plpgsql; diff --git a/internal/db/sqltest/tests/setup/wtt_load.sql b/internal/db/sqltest/tests/setup/wtt_load.sql index f5fa580edf..351ab07226 100644 --- a/internal/db/sqltest/tests/setup/wtt_load.sql +++ b/internal/db/sqltest/tests/setup/wtt_load.sql @@ -4,15 +4,15 @@ begin; -- invalid or missing args select throws_ok($$select wtt_load('unknown', 'iam')$$); - select throws_ok($$select wtt_load('colors', 'unknown')$$); - select throws_ok($$select wtt_load('colors')$$); + select throws_ok($$select wtt_load('widgets', 'unknown')$$); + select throws_ok($$select wtt_load('widgets')$$); select throws_ok($$select wtt_load()$$); -- incorrect order since auth depends on iam - select throws_ok($$select wtt_load('colors', 'auth', 'iam')$$); + select throws_ok($$select wtt_load('widgets', 'auth', 'iam')$$); -- missing a dependancy, auth depends on iam - select throws_ok($$select wtt_load('colors', 'auth')$$); + select throws_ok($$select wtt_load('widgets', 'auth')$$); select * from finish(); rollback; diff --git a/internal/db/sqltest/tests/setup/wtt_load_colors.sql b/internal/db/sqltest/tests/setup/wtt_load_colors.sql deleted file mode 100644 index 318f0adfa4..0000000000 --- a/internal/db/sqltest/tests/setup/wtt_load_colors.sql +++ /dev/null @@ -1,7 +0,0 @@ --- wtt_load_colors tests the wtt_load test helper function for the colors persona. -begin; - select plan(1); - select lives_ok($$select wtt_load('colors', 'iam', 'kms', 'auth', 'hosts', 'targets')$$); - - select * from finish(); -rollback;