From cb8bc48c3a3749bd49a55df50e9529c1c1eb111a Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Sun, 25 Sep 2022 16:57:01 -0700
Subject: [PATCH v17 19/23] meson: Add installcheck equivalent

run all tests that support running against existing server:
$ meson test --setup running

run just the main pg_regress tests against existing server:
$ meson test --setup running main-running/regress
---
 contrib/basic_archive/meson.build             |  1 +
 contrib/pg_freespacemap/meson.build           |  3 +
 contrib/pg_stat_statements/meson.build        |  4 ++
 contrib/pg_walinspect/meson.build             |  3 +
 contrib/test_decoding/meson.build             |  5 ++
 src/interfaces/ecpg/test/meson.build          |  1 +
 src/test/isolation/meson.build                |  3 +
 src/test/modules/commit_ts/meson.build        |  3 +
 src/test/modules/snapshot_too_old/meson.build |  3 +
 src/test/modules/test_oat_hooks/meson.build   |  1 +
 src/test/modules/test_pg_dump/meson.build     |  2 +
 src/test/modules/worker_spi/meson.build       |  4 +-
 src/test/regress/meson.build                  |  3 +
 .cirrus.yml                                   | 15 +++++
 meson.build                                   | 57 +++++++++++++++++--
 15 files changed, 102 insertions(+), 6 deletions(-)

diff --git a/contrib/basic_archive/meson.build b/contrib/basic_archive/meson.build
index c30dcfa5d41..d4ecaec47b8 100644
--- a/contrib/basic_archive/meson.build
+++ b/contrib/basic_archive/meson.build
@@ -25,5 +25,6 @@ tests += {
     'regress_args': [
       '--temp-config', files('basic_archive.conf'),
     ],
+    'runningcheck': false,
   },
 }
diff --git a/contrib/pg_freespacemap/meson.build b/contrib/pg_freespacemap/meson.build
index 904b37b6e9b..f802e297b67 100644
--- a/contrib/pg_freespacemap/meson.build
+++ b/contrib/pg_freespacemap/meson.build
@@ -33,5 +33,8 @@ tests += {
     'regress_args': [
       '--temp-config', files('pg_freespacemap.conf')
     ],
+    # Disabled because these tests require "autovacuum=off", which
+    # typical runningcheck users do not have (e.g. buildfarm clients).
+    'runningcheck': false,
   },
 }
diff --git a/contrib/pg_stat_statements/meson.build b/contrib/pg_stat_statements/meson.build
index 854df138e76..bd0e42331eb 100644
--- a/contrib/pg_stat_statements/meson.build
+++ b/contrib/pg_stat_statements/meson.build
@@ -41,5 +41,9 @@ tests += {
       'pg_stat_statements',
     ],
     'regress_args': ['--temp-config', files('pg_stat_statements.conf')],
+    # Disabled because these tests require
+    # "shared_preload_libraries=pg_stat_statements", which typical
+    # runningcheck users do not have (e.g. buildfarm clients).
+    'runningcheck': false,
   },
 }
diff --git a/contrib/pg_walinspect/meson.build b/contrib/pg_walinspect/meson.build
index 4314a3182a2..351f184de83 100644
--- a/contrib/pg_walinspect/meson.build
+++ b/contrib/pg_walinspect/meson.build
@@ -28,6 +28,9 @@ tests += {
     'sql': [
       'pg_walinspect',
     ],
+    # Disabled because these tests require "wal_level=replica", which
+    # some runningcheck users do not have (e.g. buildfarm clients).
     'regress_args': ['--temp-config', files('walinspect.conf')],
+    'runningcheck': false,
   },
 }
diff --git a/contrib/test_decoding/meson.build b/contrib/test_decoding/meson.build
index 6376103c689..a1aaae2dc3f 100644
--- a/contrib/test_decoding/meson.build
+++ b/contrib/test_decoding/meson.build
@@ -43,6 +43,9 @@ tests += {
     'regress_args': [
       '--temp-config', files('logical.conf'),
     ],
+    # Disabled because these tests require "wal_level=logical", which
+    # typical runningcheck users do not have (e.g. buildfarm clients).
+    'runningcheck': false,
   },
   'isolation': {
     'specs': [
@@ -60,6 +63,8 @@ tests += {
     'regress_args': [
       '--temp-config', files('logical.conf'),
     ],
+    # see above
+    'runningcheck': false,
   },
   'tap': {
     'tests': [
diff --git a/src/interfaces/ecpg/test/meson.build b/src/interfaces/ecpg/test/meson.build
index 94b26d10314..d4f1f1c0fcf 100644
--- a/src/interfaces/ecpg/test/meson.build
+++ b/src/interfaces/ecpg/test/meson.build
@@ -84,6 +84,7 @@ tests += {
     'test_kwargs': {
       'depends': ecpg_test_dependencies,
     },
+    'dbname': 'ecpg1_regression,ecpg2_regression',
     'regress_args': ecpg_regress_args,
   },
 }
diff --git a/src/test/isolation/meson.build b/src/test/isolation/meson.build
index ba27b8c1d44..8737392f23b 100644
--- a/src/test/isolation/meson.build
+++ b/src/test/isolation/meson.build
@@ -67,5 +67,8 @@ tests += {
       'priority': 40,
       'timeout': 1000,
     },
+    # checks pg_locks etc
+    'runningcheck-parallel': false,
+    'dbname': 'isolation',
   },
 }
diff --git a/src/test/modules/commit_ts/meson.build b/src/test/modules/commit_ts/meson.build
index 60cb12164d2..fa86e70e880 100644
--- a/src/test/modules/commit_ts/meson.build
+++ b/src/test/modules/commit_ts/meson.build
@@ -6,6 +6,9 @@ tests += {
     'sql': [
       'commit_timestamp',
     ],
+    # Disabled because these tests require "track_commit_timestamp = on",
+    # which typical runningcheck users do not have (e.g. buildfarm clients).
+    'runningcheck': false,
   },
   'tap': {
     'tests': [
diff --git a/src/test/modules/snapshot_too_old/meson.build b/src/test/modules/snapshot_too_old/meson.build
index efd3f1f113b..5094b52949b 100644
--- a/src/test/modules/snapshot_too_old/meson.build
+++ b/src/test/modules/snapshot_too_old/meson.build
@@ -10,5 +10,8 @@ tests += {
       'sto_using_hash_index',
     ],
     'regress_args': ['--temp-config', files('sto.conf')],
+    # Disabled because these tests require "old_snapshot_threshold" >= 0, which
+    # typical runningcheck users do not have (e.g. buildfarm clients).
+    'runningcheck': false,
   },
 }
diff --git a/src/test/modules/test_oat_hooks/meson.build b/src/test/modules/test_oat_hooks/meson.build
index 8802bbbac55..e9d09d92c30 100644
--- a/src/test/modules/test_oat_hooks/meson.build
+++ b/src/test/modules/test_oat_hooks/meson.build
@@ -25,5 +25,6 @@ tests += {
       'test_oat_hooks',
     ],
     'regress_args': ['--no-locale', '--encoding=UTF8'],
+    'runningcheck': false,
   },
 }
diff --git a/src/test/modules/test_pg_dump/meson.build b/src/test/modules/test_pg_dump/meson.build
index 41021829f3a..73f1fcf9428 100644
--- a/src/test/modules/test_pg_dump/meson.build
+++ b/src/test/modules/test_pg_dump/meson.build
@@ -13,6 +13,8 @@ tests += {
     'sql': [
       'test_pg_dump',
     ],
+    # doesn't delete its user
+    'runningcheck': false,
   },
   'tap': {
     'tests': [
diff --git a/src/test/modules/worker_spi/meson.build b/src/test/modules/worker_spi/meson.build
index a4a158c75b9..660d721eea9 100644
--- a/src/test/modules/worker_spi/meson.build
+++ b/src/test/modules/worker_spi/meson.build
@@ -30,6 +30,8 @@ tests += {
     'sql': [
       'worker_spi',
     ],
-    'regress_args': ['--temp-config', files('dynamic.conf'), '--dbname=contrib_regression'],
+    'dbname': 'contrib_regression',
+    'regress_args': ['--temp-config', files('dynamic.conf')],
+    'runningcheck': false,
   },
 }
diff --git a/src/test/regress/meson.build b/src/test/regress/meson.build
index 963c0f64ed5..a781fa2af1d 100644
--- a/src/test/regress/meson.build
+++ b/src/test/regress/meson.build
@@ -64,5 +64,8 @@ tests += {
       'priority': 50,
       'timeout': 1000,
     },
+    'dbname': 'regression',
+    # checks pg_locks etc
+    'runningcheck-parallel': false,
   },
 }
diff --git a/.cirrus.yml b/.cirrus.yml
index aea25fd7525..16a1396d869 100644
--- a/.cirrus.yml
+++ b/.cirrus.yml
@@ -121,6 +121,21 @@ task:
       meson test $MTEST_ARGS --num-processes ${TEST_JOBS}
     EOF
 
+  # test runningcheck, freebsd chosen because it's currently fast enough
+  test_running_script: |
+    su postgres <<-EOF
+      set -e
+      ulimit -c unlimited
+      meson test $MTEST_ARGS --quiet --suite setup
+      export LD_LIBRARY_PATH="$(pwd)/build/tmp_install/usr/local/lib/:$LD_LIBRARY_PATH"
+      mkdir -p build/testrun
+      build/tmp_install/usr/local/bin/initdb -N build/runningcheck --no-instructions -A trust
+      echo "include '$(pwd)/src/tools/ci/pg_ci_base.conf'" >> build/runningcheck/postgresql.conf
+      build/tmp_install/usr/local/bin/pg_ctl -c -o '-c fsync=off' -D build/runningcheck -l build/testrun/runningcheck.log start
+      meson test $MTEST_ARGS --num-processes ${TEST_JOBS} --setup running
+      build/tmp_install/usr/local/bin/pg_ctl -D build/runningcheck stop
+    EOF
+
   on_failure:
     <<: *on_failure_meson
     cores_script: src/tools/ci/cores_backtrace.sh freebsd /tmp/cores
diff --git a/meson.build b/meson.build
index 5f09d2e079a..5e7e95f352c 100644
--- a/meson.build
+++ b/meson.build
@@ -2866,6 +2866,18 @@ endif
 # Test Generation
 ###############################################################
 
+# when using a meson version understanding exclude_suites, define a 'default'
+# test setup that excludes tests running against a pre-existing install and a
+# 'running' setup that conflicts with creation of the temporary installation
+# and tap tests (which don't support running against a running server).
+running_suites = []
+install_suites = []
+if meson.version().version_compare('>=0.57')
+  runningcheck = true
+else
+  runningcheck = false
+endif
+
 testwrap = files('src/tools/testwrap')
 
 foreach test_dir : tests
@@ -2894,18 +2906,27 @@ foreach test_dir : tests
 
       test_output = test_result_dir / test_dir['name'] / kind
 
+      # unless specified by the test, choose a non-conflicting database name,
+      # to avoid conflicts when running against existing server.
+      dbname = t.get('dbname',
+        'regression_@0@_@1@'.format(test_dir['name'], kind))
+
       test_command = [
         runner.full_path(),
         '--inputdir', t.get('inputdir', test_dir['sd']),
         '--expecteddir', t.get('expecteddir', test_dir['sd']),
         '--outputdir', test_output,
-        '--temp-instance', test_output / 'tmp_check',
         '--bindir', '',
         '--dlpath', test_dir['bd'],
         '--max-concurrent-tests=20',
-        '--port', testport.to_string(),
+        '--dbname', dbname,
       ] + t.get('regress_args', [])
 
+      test_command_tmp_install = test_command + [
+          '--temp-instance', test_output / 'tmp_check',
+          '--port', testport.to_string(),
+        ]
+
       if t.has_key('schedule')
         test_command += ['--schedule', t['schedule'],]
       endif
@@ -2920,7 +2941,6 @@ foreach test_dir : tests
       env.prepend('PATH', temp_install_bindir, test_dir['bd'])
 
       test_kwargs = {
-        'suite': [test_dir['name']],
         'priority': 10,
         'timeout': 1000,
         'depends': test_deps + t.get('deps', []),
@@ -2931,10 +2951,28 @@ foreach test_dir : tests
         python,
         args: testwrap_base + [
           '--testname', kind,
-          '--', test_command,
+          '--', test_command_tmp_install,
         ],
+        suite: test_dir['name'],
         kwargs: test_kwargs,
       )
+      install_suites += test_dir['name']
+
+      # some tests can't support running against running DB
+      if runningcheck and t.get('runningcheck', true)
+        suite = test_dir['name'] + '-running'
+        test(suite / kind,
+          python,
+          args: testwrap_base + [
+            '--testname', kind,
+            '--', test_command,
+          ],
+          is_parallel: t.get('runningcheck-parallel', true),
+          suite: suite,
+          kwargs: test_kwargs,
+        )
+        running_suites += suite
+      endif
 
       testport += 1
     elif kind == 'tap'
@@ -2957,9 +2995,10 @@ foreach test_dir : tests
         env.set(name, value)
       endforeach
 
+      suite = test_dir['name']
       test_kwargs = {
         'protocol': 'tap',
-        'suite': [test_dir['name']],
+        'suite': suite,
         'timeout': 1000,
         'depends': test_deps + t.get('deps', []),
         'env': env,
@@ -2985,6 +3024,7 @@ foreach test_dir : tests
           ],
         )
       endforeach
+      install_suites += suite
     else
       error('unknown kind @0@ of test in @1@'.format(kind, test_dir['sd']))
     endif
@@ -2993,6 +3033,13 @@ foreach test_dir : tests
 
 endforeach # directories with tests
 
+if runningcheck
+  add_test_setup('default',
+    is_default: true,
+    exclude_suites: running_suites)
+  add_test_setup('running',
+    exclude_suites: ['setup'] + install_suites)
+endif
 
 
 ###############################################################
-- 
2.37.3.542.gdd3f6c4cae

