PostgreSQL Source Code  git master
pg_regress.c File Reference
#include "postgres_fe.h"
#include <ctype.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#include "common/logging.h"
#include "common/restricted_token.h"
#include "common/username.h"
#include "getopt_long.h"
#include "libpq/pqcomm.h"
#include "pg_config_paths.h"
#include "pg_regress.h"
#include "portability/instr_time.h"
Include dependency graph for pg_regress.c:

Go to the source code of this file.

Data Structures

struct  _resultmap
 

Macros

#define MAX_PARALLEL_TESTS   100
 
#define ULONGPID(x)   (unsigned long) (x)
 

Typedefs

typedef struct _resultmap _resultmap
 

Functions

static bool directory_exists (const char *dir)
 
static void make_directory (const char *dir)
 
static void header (const char *fmt,...) pg_attribute_printf(1
 
static void static void status (const char *fmt,...) pg_attribute_printf(1
 
static void static void static void psql_command (const char *database, const char *query,...) pg_attribute_printf(2
 
static void static void static void void add_stringlist_item (_stringlist **listhead, const char *str)
 
static void free_stringlist (_stringlist **listhead)
 
static void split_to_stringlist (const char *s, const char *delim, _stringlist **listhead)
 
static void status_end (void)
 
static void stop_postmaster (void)
 
static bool string_matches_pattern (const char *str, const char *pattern)
 
void replace_string (char *string, const char *replace, const char *replacement)
 
static void convert_sourcefiles_in (const char *source_subdir, const char *dest_dir, const char *dest_subdir, const char *suffix)
 
static void convert_sourcefiles (void)
 
static void load_resultmap (void)
 
static const char * get_expectfile (const char *testname, const char *file)
 
static void doputenv (const char *var, const char *val)
 
static void initialize_environment (void)
 
PID_TYPE spawn_process (const char *cmdline)
 
static long file_size (const char *file)
 
static int file_line_count (const char *file)
 
bool file_exists (const char *file)
 
static char * get_alternative_expectfile (const char *expectfile, int i)
 
static int run_diff (const char *cmd, const char *filename)
 
static bool results_differ (const char *testname, const char *resultsfile, const char *default_expectfile)
 
static void wait_for_tests (PID_TYPE *pids, int *statuses, instr_time *stoptimes, char **names, int num_tests)
 
static void log_child_failure (int exitstatus)
 
static void run_schedule (const char *schedule, test_function tfunc)
 
static void run_single_test (const char *test, test_function tfunc)
 
static void open_result_files (void)
 
static void drop_database_if_exists (const char *dbname)
 
static void create_database (const char *dbname)
 
static void drop_role_if_exists (const char *rolename)
 
static void create_role (const char *rolename, const _stringlist *granted_dbs)
 
static void help (void)
 
int regression_main (int argc, char *argv[], init_function ifunc, test_function tfunc)
 

Variables

char * host_platform = HOST_TUPLE
 
static char * shellprog = SHELLPROG
 
const char * basic_diff_opts = ""
 
const char * pretty_diff_opts = "-U3"
 
_stringlistdblist = NULL
 
bool debug = false
 
char * inputdir = "."
 
char * outputdir = "."
 
char * bindir = PGBINDIR
 
char * launcher = NULL
 
static _stringlistloadextension = NULL
 
static int max_connections = 0
 
static int max_concurrent_tests = 0
 
static char * encoding = NULL
 
static _stringlistschedulelist = NULL
 
static _stringlistextra_tests = NULL
 
static char * temp_instance = NULL
 
static _stringlisttemp_configs = NULL
 
static bool nolocale = false
 
static bool use_existing = false
 
static char * hostname = NULL
 
static int port = -1
 
static bool port_specified_by_user = false
 
static char * dlpath = PKGLIBDIR
 
static char * user = NULL
 
static _stringlistextraroles = NULL
 
static char * config_auth_datadir = NULL
 
static const char * progname
 
static char * logfilename
 
static FILE * logfile
 
static char * difffilename
 
static const char * sockdir
 
static _resultmapresultmap = NULL
 
static PID_TYPE postmaster_pid = INVALID_PID
 
static bool postmaster_running = false
 
static int success_count = 0
 
static int fail_count = 0
 
static int fail_ignore_count = 0
 

Macro Definition Documentation

◆ MAX_PARALLEL_TESTS

#define MAX_PARALLEL_TESTS   100

Referenced by run_schedule().

◆ ULONGPID

#define ULONGPID (   x)    (unsigned long) (x)

Referenced by regression_main().

Typedef Documentation

◆ _resultmap

typedef struct _resultmap _resultmap

Function Documentation

◆ add_stringlist_item()

static void static void static void void add_stringlist_item ( _stringlist **  listhead,
const char *  str 
)

Definition at line 155 of file pg_regress.c.

References _stringlist::next, pg_malloc(), pg_strdup(), and _stringlist::str.

Referenced by ecpg_start_test(), isolation_init(), isolation_start_test(), psql_init(), psql_start_test(), regression_main(), run_schedule(), and split_to_stringlist().

156 {
157  _stringlist *newentry = pg_malloc(sizeof(_stringlist));
158  _stringlist *oldentry;
159 
160  newentry->str = pg_strdup(str);
161  newentry->next = NULL;
162  if (*listhead == NULL)
163  *listhead = newentry;
164  else
165  {
166  for (oldentry = *listhead; oldentry->next; oldentry = oldentry->next)
167  /* skip */ ;
168  oldentry->next = newentry;
169  }
170 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
char * str
Definition: pg_regress.h:24
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
struct _stringlist * next
Definition: pg_regress.h:25

◆ convert_sourcefiles()

static void convert_sourcefiles ( void  )
static

Definition at line 583 of file pg_regress.c.

References convert_sourcefiles_in(), and outputdir.

Referenced by initialize_environment().

584 {
585  convert_sourcefiles_in("input", outputdir, "sql", "sql");
586  convert_sourcefiles_in("output", outputdir, "expected", "out");
587 }
static void convert_sourcefiles_in(const char *source_subdir, const char *dest_dir, const char *dest_subdir, const char *suffix)
Definition: pg_regress.c:464
char * outputdir
Definition: pg_regress.c:76

◆ convert_sourcefiles_in()

static void convert_sourcefiles_in ( const char *  source_subdir,
const char *  dest_dir,
const char *  dest_subdir,
const char *  suffix 
)
static

Definition at line 464 of file pg_regress.c.

References _, directory_exists(), dlpath, fprintf, infile(), inputdir, make_directory(), MAXPGPATH, name, outfile, outputdir, pgfnames(), pgfnames_cleanup(), progname, replace_string(), rmtree(), snprintf, and strerror.

Referenced by convert_sourcefiles().

465 {
466  char testtablespace[MAXPGPATH];
467  char indir[MAXPGPATH];
468  char outdir_sub[MAXPGPATH];
469  char **name;
470  char **names;
471  int count = 0;
472 
473  snprintf(indir, MAXPGPATH, "%s/%s", inputdir, source_subdir);
474 
475  /* Check that indir actually exists and is a directory */
476  if (!directory_exists(indir))
477  {
478  /*
479  * No warning, to avoid noise in tests that do not have these
480  * directories; for example, ecpg, contrib and src/pl.
481  */
482  return;
483  }
484 
485  names = pgfnames(indir);
486  if (!names)
487  /* Error logged in pgfnames */
488  exit(2);
489 
490  /* Create the "dest" subdirectory if not present */
491  snprintf(outdir_sub, MAXPGPATH, "%s/%s", dest_dir, dest_subdir);
492  if (!directory_exists(outdir_sub))
493  make_directory(outdir_sub);
494 
495  snprintf(testtablespace, MAXPGPATH, "%s/testtablespace", outputdir);
496 
497 #ifdef WIN32
498 
499  /*
500  * On Windows only, clean out the test tablespace dir, or create it if it
501  * doesn't exist so as it is possible to run the regression tests as a
502  * Windows administrative user account with the restricted token obtained
503  * when starting pg_regress. On other platforms we expect the Makefile to
504  * take care of that.
505  */
506  if (directory_exists(testtablespace))
507  if (!rmtree(testtablespace, true))
508  {
509  fprintf(stderr, _("\n%s: could not remove test tablespace \"%s\"\n"),
510  progname, testtablespace);
511  exit(2);
512  }
513  make_directory(testtablespace);
514 #endif
515 
516  /* finally loop on each file and do the replacement */
517  for (name = names; *name; name++)
518  {
519  char srcfile[MAXPGPATH];
520  char destfile[MAXPGPATH];
521  char prefix[MAXPGPATH];
522  FILE *infile,
523  *outfile;
524  char line[1024];
525 
526  /* reject filenames not finishing in ".source" */
527  if (strlen(*name) < 8)
528  continue;
529  if (strcmp(*name + strlen(*name) - 7, ".source") != 0)
530  continue;
531 
532  count++;
533 
534  /* build the full actual paths to open */
535  snprintf(prefix, strlen(*name) - 6, "%s", *name);
536  snprintf(srcfile, MAXPGPATH, "%s/%s", indir, *name);
537  snprintf(destfile, MAXPGPATH, "%s/%s/%s.%s", dest_dir, dest_subdir,
538  prefix, suffix);
539 
540  infile = fopen(srcfile, "r");
541  if (!infile)
542  {
543  fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
544  progname, srcfile, strerror(errno));
545  exit(2);
546  }
547  outfile = fopen(destfile, "w");
548  if (!outfile)
549  {
550  fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
551  progname, destfile, strerror(errno));
552  exit(2);
553  }
554  while (fgets(line, sizeof(line), infile))
555  {
556  replace_string(line, "@abs_srcdir@", inputdir);
557  replace_string(line, "@abs_builddir@", outputdir);
558  replace_string(line, "@testtablespace@", testtablespace);
559  replace_string(line, "@libdir@", dlpath);
560  replace_string(line, "@DLSUFFIX@", DLSUFFIX);
561  fputs(line, outfile);
562  }
563  fclose(infile);
564  fclose(outfile);
565  }
566 
567  /*
568  * If we didn't process any files, complain because it probably means
569  * somebody neglected to pass the needed --inputdir argument.
570  */
571  if (count <= 0)
572  {
573  fprintf(stderr, _("%s: no *.source files found in \"%s\"\n"),
574  progname, indir);
575  exit(2);
576  }
577 
578  pgfnames_cleanup(names);
579 }
static bool directory_exists(const char *dir)
Definition: pg_regress.c:1273
char ** pgfnames(const char *path)
Definition: pgfnames.c:37
static const char * progname
Definition: pg_regress.c:98
static void make_directory(const char *dir)
Definition: pg_regress.c:1286
#define fprintf
Definition: port.h:197
#define MAXPGPATH
void replace_string(char *string, const char *replace, const char *replacement)
Definition: pg_regress.c:442
static char * dlpath
Definition: pg_regress.c:92
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:42
char * outputdir
Definition: pg_regress.c:76
char * inputdir
Definition: pg_regress.c:75
static void infile(const char *filename)
Definition: zic.c:1245
void pgfnames_cleanup(char **filenames)
Definition: pgfnames.c:86
#define strerror
Definition: port.h:206
const char * name
Definition: encode.c:561
static char * outfile
#define snprintf
Definition: port.h:193
#define _(x)
Definition: elog.c:88

◆ create_database()

static void create_database ( const char *  dbname)
static

Definition at line 1976 of file pg_regress.c.

References _, encoding, header(), _stringlist::next, nolocale, psql_command(), and _stringlist::str.

Referenced by regression_main().

1977 {
1978  _stringlist *sl;
1979 
1980  /*
1981  * We use template0 so that any installation-local cruft in template1 will
1982  * not mess up the tests.
1983  */
1984  header(_("creating database \"%s\""), dbname);
1985  if (encoding)
1986  psql_command("postgres", "CREATE DATABASE \"%s\" TEMPLATE=template0 ENCODING='%s'%s", dbname, encoding,
1987  (nolocale) ? " LC_COLLATE='C' LC_CTYPE='C'" : "");
1988  else
1989  psql_command("postgres", "CREATE DATABASE \"%s\" TEMPLATE=template0%s", dbname,
1990  (nolocale) ? " LC_COLLATE='C' LC_CTYPE='C'" : "");
1992  "ALTER DATABASE \"%s\" SET lc_messages TO 'C';"
1993  "ALTER DATABASE \"%s\" SET lc_monetary TO 'C';"
1994  "ALTER DATABASE \"%s\" SET lc_numeric TO 'C';"
1995  "ALTER DATABASE \"%s\" SET lc_time TO 'C';"
1996  "ALTER DATABASE \"%s\" SET bytea_output TO 'hex';"
1997  "ALTER DATABASE \"%s\" SET timezone_abbreviations TO 'Default';",
1999 
2000  /*
2001  * Install any requested extensions. We use CREATE IF NOT EXISTS so that
2002  * this will work whether or not the extension is preinstalled.
2003  */
2004  for (sl = loadextension; sl != NULL; sl = sl->next)
2005  {
2006  header(_("installing %s"), sl->str);
2007  psql_command(dbname, "CREATE EXTENSION IF NOT EXISTS \"%s\"", sl->str);
2008  }
2009 }
char * str
Definition: pg_regress.h:24
static char * encoding
Definition: pg_regress.c:82
static _stringlist * loadextension
Definition: pg_regress.c:79
static void static void static void psql_command(const char *database, const char *query,...) pg_attribute_printf(2
Definition: pg_regress.c:1106
char * dbname
Definition: streamutil.c:50
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:208
struct _stringlist * next
Definition: pg_regress.h:25
static bool nolocale
Definition: pg_regress.c:87
#define _(x)
Definition: elog.c:88

◆ create_role()

static void create_role ( const char *  rolename,
const _stringlist granted_dbs 
)
static

Definition at line 2019 of file pg_regress.c.

References _, header(), _stringlist::next, psql_command(), and _stringlist::str.

Referenced by regression_main().

2020 {
2021  header(_("creating role \"%s\""), rolename);
2022  psql_command("postgres", "CREATE ROLE \"%s\" WITH LOGIN", rolename);
2023  for (; granted_dbs != NULL; granted_dbs = granted_dbs->next)
2024  {
2025  psql_command("postgres", "GRANT ALL ON DATABASE \"%s\" TO \"%s\"",
2026  granted_dbs->str, rolename);
2027  }
2028 }
char * str
Definition: pg_regress.h:24
static void static void static void psql_command(const char *database, const char *query,...) pg_attribute_printf(2
Definition: pg_regress.c:1106
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:208
struct _stringlist * next
Definition: pg_regress.h:25
#define _(x)
Definition: elog.c:88

◆ directory_exists()

static bool directory_exists ( const char *  dir)
static

Definition at line 1273 of file pg_regress.c.

References S_ISDIR, and stat.

Referenced by convert_sourcefiles_in(), open_result_files(), and regression_main().

1274 {
1275  struct stat st;
1276 
1277  if (stat(dir, &st) != 0)
1278  return false;
1279  if (S_ISDIR(st.st_mode))
1280  return true;
1281  return false;
1282 }
#define stat(a, b)
Definition: win32_port.h:255
#define S_ISDIR(m)
Definition: win32_port.h:296

◆ doputenv()

static void doputenv ( const char *  var,
const char *  val 
)
static

Definition at line 714 of file pg_regress.c.

References psprintf(), and putenv.

Referenced by initialize_environment(), and regression_main().

715 {
716  char *s;
717 
718  s = psprintf("%s=%s", var, val);
719  putenv(s);
720 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define putenv(x)
Definition: win32_port.h:474
long val
Definition: informix.c:664

◆ drop_database_if_exists()

static void drop_database_if_exists ( const char *  dbname)
static

Definition at line 1969 of file pg_regress.c.

References _, header(), and psql_command().

Referenced by regression_main().

1970 {
1971  header(_("dropping database \"%s\""), dbname);
1972  psql_command("postgres", "DROP DATABASE IF EXISTS \"%s\"", dbname);
1973 }
static void static void static void psql_command(const char *database, const char *query,...) pg_attribute_printf(2
Definition: pg_regress.c:1106
char * dbname
Definition: streamutil.c:50
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:208
#define _(x)
Definition: elog.c:88

◆ drop_role_if_exists()

static void drop_role_if_exists ( const char *  rolename)
static

Definition at line 2012 of file pg_regress.c.

References _, header(), and psql_command().

Referenced by regression_main().

2013 {
2014  header(_("dropping role \"%s\""), rolename);
2015  psql_command("postgres", "DROP ROLE IF EXISTS \"%s\"", rolename);
2016 }
static void static void static void psql_command(const char *database, const char *query,...) pg_attribute_printf(2
Definition: pg_regress.c:1106
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:208
#define _(x)
Definition: elog.c:88

◆ file_exists()

bool file_exists ( const char *  file)

Definition at line 1262 of file pg_regress.c.

Referenced by results_differ().

1263 {
1264  FILE *f = fopen(file, "r");
1265 
1266  if (!f)
1267  return false;
1268  fclose(f);
1269  return true;
1270 }

◆ file_line_count()

static int file_line_count ( const char *  file)
static

Definition at line 1240 of file pg_regress.c.

References _, fprintf, progname, and strerror.

Referenced by results_differ().

1241 {
1242  int c;
1243  int l = 0;
1244  FILE *f = fopen(file, "r");
1245 
1246  if (!f)
1247  {
1248  fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
1249  progname, file, strerror(errno));
1250  return -1;
1251  }
1252  while ((c = fgetc(f)) != EOF)
1253  {
1254  if (c == '\n')
1255  l++;
1256  }
1257  fclose(f);
1258  return l;
1259 }
static const char * progname
Definition: pg_regress.c:98
#define fprintf
Definition: port.h:197
char * c
#define strerror
Definition: port.h:206
#define _(x)
Definition: elog.c:88

◆ file_size()

static long file_size ( const char *  file)
static

Definition at line 1219 of file pg_regress.c.

References _, fprintf, progname, and strerror.

Referenced by regression_main(), and run_diff().

1220 {
1221  long r;
1222  FILE *f = fopen(file, "r");
1223 
1224  if (!f)
1225  {
1226  fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
1227  progname, file, strerror(errno));
1228  return -1;
1229  }
1230  fseek(f, 0, SEEK_END);
1231  r = ftell(f);
1232  fclose(f);
1233  return r;
1234 }
static const char * progname
Definition: pg_regress.c:98
#define fprintf
Definition: port.h:197
#define strerror
Definition: port.h:206
#define _(x)
Definition: elog.c:88

◆ free_stringlist()

static void free_stringlist ( _stringlist **  listhead)
static

Definition at line 176 of file pg_regress.c.

References free.

Referenced by regression_main(), and run_schedule().

177 {
178  if (listhead == NULL || *listhead == NULL)
179  return;
180  if ((*listhead)->next != NULL)
181  free_stringlist(&((*listhead)->next));
182  free((*listhead)->str);
183  free(*listhead);
184  *listhead = NULL;
185 }
static void free_stringlist(_stringlist **listhead)
Definition: pg_regress.c:176
#define free(a)
Definition: header.h:65

◆ get_alternative_expectfile()

static char* get_alternative_expectfile ( const char *  expectfile,
int  i 
)
static

Definition at line 1300 of file pg_regress.c.

References free, malloc, and snprintf.

Referenced by results_differ().

1301 {
1302  char *last_dot;
1303  int ssize = strlen(expectfile) + 2 + 1;
1304  char *tmp;
1305  char *s;
1306 
1307  if (!(tmp = (char *) malloc(ssize)))
1308  return NULL;
1309 
1310  if (!(s = (char *) malloc(ssize)))
1311  {
1312  free(tmp);
1313  return NULL;
1314  }
1315 
1316  strcpy(tmp, expectfile);
1317  last_dot = strrchr(tmp, '.');
1318  if (!last_dot)
1319  {
1320  free(tmp);
1321  free(s);
1322  return NULL;
1323  }
1324  *last_dot = '\0';
1325  snprintf(s, ssize, "%s_%d.%s", tmp, i, last_dot + 1);
1326  free(tmp);
1327  return s;
1328 }
#define malloc(a)
Definition: header.h:50
#define free(a)
Definition: header.h:65
int i
#define snprintf
Definition: port.h:193

◆ get_expectfile()

static const char* get_expectfile ( const char *  testname,
const char *  file 
)
static

Definition at line 685 of file pg_regress.c.

References _resultmap::next, _resultmap::resultfile, _resultmap::test, and _resultmap::type.

Referenced by results_differ().

686 {
687  char *file_type;
688  _resultmap *rm;
689 
690  /*
691  * Determine the file type from the file name. This is just what is
692  * following the last dot in the file name.
693  */
694  if (!file || !(file_type = strrchr(file, '.')))
695  return NULL;
696 
697  file_type++;
698 
699  for (rm = resultmap; rm != NULL; rm = rm->next)
700  {
701  if (strcmp(testname, rm->test) == 0 && strcmp(file_type, rm->type) == 0)
702  {
703  return rm->resultfile;
704  }
705  }
706 
707  return NULL;
708 }
struct _resultmap * next
Definition: pg_regress.c:47
static _resultmap * resultmap
Definition: pg_regress.c:109
char * test
Definition: pg_regress.c:44
char * type
Definition: pg_regress.c:45
char * resultfile
Definition: pg_regress.c:46

◆ header()

static void header ( const char *  fmt,
  ... 
)
static

Definition at line 208 of file pg_regress.c.

References fprintf, generate_unaccent_rules::stdout, and vsnprintf.

Referenced by _bt_unlink_halfdead_page(), _tarPositionTo(), convertJsonbArray(), convertJsonbObject(), create_database(), create_role(), datumRestore(), datumSerialize(), DecodeMultiInsert(), DecodeXLogTuple(), drop_database_if_exists(), drop_role_if_exists(), ExecHashJoinGetSavedTuple(), pgss_shmem_startup(), printTableAddHeader(), ReceiveTarCopyChunk(), ReceiveTarFile(), refresh_utf8format(), regression_main(), scan_file(), tar_close(), and XLogReaderValidatePageHeader().

209 {
210  char tmp[64];
211  va_list ap;
212 
213  va_start(ap, fmt);
214  vsnprintf(tmp, sizeof(tmp), fmt, ap);
215  va_end(ap);
216 
217  fprintf(stdout, "============== %-38s ==============\n", tmp);
218  fflush(stdout);
219 }
#define fprintf
Definition: port.h:197
#define vsnprintf
Definition: port.h:192

◆ help()

static void help ( void  )
static

Definition at line 2031 of file pg_regress.c.

References _, printf, and progname.

Referenced by regression_main().

2032 {
2033  printf(_("PostgreSQL regression test driver\n"));
2034  printf(_("\n"));
2035  printf(_("Usage:\n %s [OPTION]... [EXTRA-TEST]...\n"), progname);
2036  printf(_("\n"));
2037  printf(_("Options:\n"));
2038  printf(_(" --bindir=BINPATH use BINPATH for programs that are run;\n"));
2039  printf(_(" if empty, use PATH from the environment\n"));
2040  printf(_(" --config-auth=DATADIR update authentication settings for DATADIR\n"));
2041  printf(_(" --create-role=ROLE create the specified role before testing\n"));
2042  printf(_(" --dbname=DB use database DB (default \"regression\")\n"));
2043  printf(_(" --debug turn on debug mode in programs that are run\n"));
2044  printf(_(" --dlpath=DIR look for dynamic libraries in DIR\n"));
2045  printf(_(" --encoding=ENCODING use ENCODING as the encoding\n"));
2046  printf(_(" -h, --help show this help, then exit\n"));
2047  printf(_(" --inputdir=DIR take input files from DIR (default \".\")\n"));
2048  printf(_(" --launcher=CMD use CMD as launcher of psql\n"));
2049  printf(_(" --load-extension=EXT load the named extension before running the\n"));
2050  printf(_(" tests; can appear multiple times\n"));
2051  printf(_(" --max-connections=N maximum number of concurrent connections\n"));
2052  printf(_(" (default is 0, meaning unlimited)\n"));
2053  printf(_(" --max-concurrent-tests=N maximum number of concurrent tests in schedule\n"));
2054  printf(_(" (default is 0, meaning unlimited)\n"));
2055  printf(_(" --outputdir=DIR place output files in DIR (default \".\")\n"));
2056  printf(_(" --schedule=FILE use test ordering schedule from FILE\n"));
2057  printf(_(" (can be used multiple times to concatenate)\n"));
2058  printf(_(" --temp-instance=DIR create a temporary instance in DIR\n"));
2059  printf(_(" --use-existing use an existing installation\n"));
2060  printf(_(" -V, --version output version information, then exit\n"));
2061  printf(_("\n"));
2062  printf(_("Options for \"temp-instance\" mode:\n"));
2063  printf(_(" --no-locale use C locale\n"));
2064  printf(_(" --port=PORT start postmaster on PORT\n"));
2065  printf(_(" --temp-config=FILE append contents of FILE to temporary config\n"));
2066  printf(_("\n"));
2067  printf(_("Options for using an existing installation:\n"));
2068  printf(_(" --host=HOST use postmaster running on HOST\n"));
2069  printf(_(" --port=PORT use postmaster running at PORT\n"));
2070  printf(_(" --user=USER connect as USER\n"));
2071  printf(_("\n"));
2072  printf(_("The exit status is 0 if all tests passed, 1 if some tests failed, and 2\n"));
2073  printf(_("if the tests could not be run for some reason.\n"));
2074  printf(_("\n"));
2075  printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
2076  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
2077 }
static const char * progname
Definition: pg_regress.c:98
#define printf(...)
Definition: port.h:199
#define _(x)
Definition: elog.c:88

◆ initialize_environment()

static void initialize_environment ( void  )
static

Definition at line 726 of file pg_regress.c.

References _, addrinfo::ai_addr, addrinfo::ai_addrlen, addrinfo::ai_canonname, addrinfo::ai_family, addrinfo::ai_flags, addrinfo::ai_next, AI_NUMERICHOST, addrinfo::ai_protocol, addrinfo::ai_socktype, Assert, convert_sourcefiles(), doputenv(), encoding, fprintf, free, get_user_name(), getaddrinfo, hostname, load_resultmap(), MAXPGPATH, _stringlist::next, nolocale, pg_malloc(), pghost, pgport, port, printf, progname, psprintf(), putenv, realloc, snprintf, sockdir, sprintf, _stringlist::str, strerror, temp_instance, unsetenv, and user.

Referenced by regression_main().

727 {
728  /*
729  * Set default application_name. (The test_function may choose to
730  * override this, but if it doesn't, we have something useful in place.)
731  */
732  putenv("PGAPPNAME=pg_regress");
733 
734  if (nolocale)
735  {
736  /*
737  * Clear out any non-C locale settings
738  */
739  unsetenv("LC_COLLATE");
740  unsetenv("LC_CTYPE");
741  unsetenv("LC_MONETARY");
742  unsetenv("LC_NUMERIC");
743  unsetenv("LC_TIME");
744  unsetenv("LANG");
745 
746  /*
747  * Most platforms have adopted the POSIX locale as their
748  * implementation-defined default locale. Exceptions include native
749  * Windows, macOS with --enable-nls, and Cygwin with --enable-nls.
750  * (Use of --enable-nls matters because libintl replaces setlocale().)
751  * Also, PostgreSQL does not support macOS with locale environment
752  * variables unset; see PostmasterMain().
753  */
754 #if defined(WIN32) || defined(__CYGWIN__) || defined(__darwin__)
755  putenv("LANG=C");
756 #endif
757  }
758 
759  /*
760  * Set translation-related settings to English; otherwise psql will
761  * produce translated messages and produce diffs. (XXX If we ever support
762  * translation of pg_regress, this needs to be moved elsewhere, where psql
763  * is actually called.)
764  */
765  unsetenv("LANGUAGE");
766  unsetenv("LC_ALL");
767  putenv("LC_MESSAGES=C");
768 
769  /*
770  * Set encoding as requested
771  */
772  if (encoding)
773  doputenv("PGCLIENTENCODING", encoding);
774  else
775  unsetenv("PGCLIENTENCODING");
776 
777  /*
778  * Set timezone and datestyle for datetime-related tests
779  */
780  putenv("PGTZ=PST8PDT");
781  putenv("PGDATESTYLE=Postgres, MDY");
782 
783  /*
784  * Likewise set intervalstyle to ensure consistent results. This is a bit
785  * more painful because we must use PGOPTIONS, and we want to preserve the
786  * user's ability to set other variables through that.
787  */
788  {
789  const char *my_pgoptions = "-c intervalstyle=postgres_verbose";
790  const char *old_pgoptions = getenv("PGOPTIONS");
791  char *new_pgoptions;
792 
793  if (!old_pgoptions)
794  old_pgoptions = "";
795  new_pgoptions = psprintf("PGOPTIONS=%s %s",
796  old_pgoptions, my_pgoptions);
797  putenv(new_pgoptions);
798  }
799 
800  if (temp_instance)
801  {
802  /*
803  * Clear out any environment vars that might cause psql to connect to
804  * the wrong postmaster, or otherwise behave in nondefault ways. (Note
805  * we also use psql's -X switch consistently, so that ~/.psqlrc files
806  * won't mess things up.) Also, set PGPORT to the temp port, and set
807  * PGHOST depending on whether we are using TCP or Unix sockets.
808  */
809  unsetenv("PGDATABASE");
810  unsetenv("PGUSER");
811  unsetenv("PGSERVICE");
812  unsetenv("PGSSLMODE");
813  unsetenv("PGREQUIRESSL");
814  unsetenv("PGCONNECT_TIMEOUT");
815  unsetenv("PGDATA");
816 #ifdef HAVE_UNIX_SOCKETS
817  if (hostname != NULL)
818  doputenv("PGHOST", hostname);
819  else
820  {
821  sockdir = getenv("PG_REGRESS_SOCK_DIR");
822  if (!sockdir)
823  sockdir = make_temp_sockdir();
824  doputenv("PGHOST", sockdir);
825  }
826 #else
827  Assert(hostname != NULL);
828  doputenv("PGHOST", hostname);
829 #endif
830  unsetenv("PGHOSTADDR");
831  if (port != -1)
832  {
833  char s[16];
834 
835  sprintf(s, "%d", port);
836  doputenv("PGPORT", s);
837  }
838  }
839  else
840  {
841  const char *pghost;
842  const char *pgport;
843 
844  /*
845  * When testing an existing install, we honor existing environment
846  * variables, except if they're overridden by command line options.
847  */
848  if (hostname != NULL)
849  {
850  doputenv("PGHOST", hostname);
851  unsetenv("PGHOSTADDR");
852  }
853  if (port != -1)
854  {
855  char s[16];
856 
857  sprintf(s, "%d", port);
858  doputenv("PGPORT", s);
859  }
860  if (user != NULL)
861  doputenv("PGUSER", user);
862 
863  /*
864  * However, we *don't* honor PGDATABASE, since we certainly don't wish
865  * to connect to whatever database the user might like as default.
866  * (Most tests override PGDATABASE anyway, but there are some ECPG
867  * test cases that don't.)
868  */
869  unsetenv("PGDATABASE");
870 
871  /*
872  * Report what we're connecting to
873  */
874  pghost = getenv("PGHOST");
875  pgport = getenv("PGPORT");
876 #ifndef HAVE_UNIX_SOCKETS
877  if (!pghost)
878  pghost = "localhost";
879 #endif
880 
881  if (pghost && pgport)
882  printf(_("(using postmaster on %s, port %s)\n"), pghost, pgport);
883  if (pghost && !pgport)
884  printf(_("(using postmaster on %s, default port)\n"), pghost);
885  if (!pghost && pgport)
886  printf(_("(using postmaster on Unix socket, port %s)\n"), pgport);
887  if (!pghost && !pgport)
888  printf(_("(using postmaster on Unix socket, default port)\n"));
889  }
890 
892  load_resultmap();
893 }
static const char * sockdir
Definition: pg_regress.c:102
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static char * encoding
Definition: pg_regress.c:82
#define printf(...)
Definition: port.h:199
static char * temp_instance
Definition: pg_regress.c:85
static void convert_sourcefiles(void)
Definition: pg_regress.c:583
#define sprintf
Definition: port.h:195
static int port
Definition: pg_regress.c:90
char * pghost
Definition: pgbench.c:242
#define Assert(condition)
Definition: c.h:745
static void load_resultmap(void)
Definition: pg_regress.c:603
#define putenv(x)
Definition: win32_port.h:474
static void doputenv(const char *var, const char *val)
Definition: pg_regress.c:714
static char * user
Definition: pg_regress.c:93
static bool nolocale
Definition: pg_regress.c:87
static char * hostname
Definition: pg_regress.c:89
#define _(x)
Definition: elog.c:88
char * pgport
Definition: pgbench.c:243
#define unsetenv(x)
Definition: win32_port.h:475

◆ load_resultmap()

static void load_resultmap ( void  )
static

Definition at line 603 of file pg_regress.c.

References _, buf, fprintf, host_platform, i, inputdir, MAXPGPATH, _resultmap::next, pg_malloc(), pg_strdup(), progname, _resultmap::resultfile, resultmap, snprintf, strerror, string_matches_pattern(), _resultmap::test, and _resultmap::type.

Referenced by initialize_environment().

604 {
605  char buf[MAXPGPATH];
606  FILE *f;
607 
608  /* scan the file ... */
609  snprintf(buf, sizeof(buf), "%s/resultmap", inputdir);
610  f = fopen(buf, "r");
611  if (!f)
612  {
613  /* OK if it doesn't exist, else complain */
614  if (errno == ENOENT)
615  return;
616  fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
617  progname, buf, strerror(errno));
618  exit(2);
619  }
620 
621  while (fgets(buf, sizeof(buf), f))
622  {
623  char *platform;
624  char *file_type;
625  char *expected;
626  int i;
627 
628  /* strip trailing whitespace, especially the newline */
629  i = strlen(buf);
630  while (i > 0 && isspace((unsigned char) buf[i - 1]))
631  buf[--i] = '\0';
632 
633  /* parse out the line fields */
634  file_type = strchr(buf, ':');
635  if (!file_type)
636  {
637  fprintf(stderr, _("incorrectly formatted resultmap entry: %s\n"),
638  buf);
639  exit(2);
640  }
641  *file_type++ = '\0';
642 
643  platform = strchr(file_type, ':');
644  if (!platform)
645  {
646  fprintf(stderr, _("incorrectly formatted resultmap entry: %s\n"),
647  buf);
648  exit(2);
649  }
650  *platform++ = '\0';
651  expected = strchr(platform, '=');
652  if (!expected)
653  {
654  fprintf(stderr, _("incorrectly formatted resultmap entry: %s\n"),
655  buf);
656  exit(2);
657  }
658  *expected++ = '\0';
659 
660  /*
661  * if it's for current platform, save it in resultmap list. Note: by
662  * adding at the front of the list, we ensure that in ambiguous cases,
663  * the last match in the resultmap file is used. This mimics the
664  * behavior of the old shell script.
665  */
666  if (string_matches_pattern(host_platform, platform))
667  {
668  _resultmap *entry = pg_malloc(sizeof(_resultmap));
669 
670  entry->test = pg_strdup(buf);
671  entry->type = pg_strdup(file_type);
672  entry->resultfile = pg_strdup(expected);
673  entry->next = resultmap;
674  resultmap = entry;
675  }
676  }
677  fclose(f);
678 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
char * host_platform
Definition: pg_regress.c:53
static const char * progname
Definition: pg_regress.c:98
struct _resultmap * next
Definition: pg_regress.c:47
static bool string_matches_pattern(const char *str, const char *pattern)
Definition: pg_regress.c:377
#define fprintf
Definition: port.h:197
static _resultmap * resultmap
Definition: pg_regress.c:109
#define MAXPGPATH
static char * buf
Definition: pg_test_fsync.c:67
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
char * test
Definition: pg_regress.c:44
char * type
Definition: pg_regress.c:45
char * inputdir
Definition: pg_regress.c:75
#define strerror
Definition: port.h:206
char * resultfile
Definition: pg_regress.c:46
int i
#define snprintf
Definition: port.h:193
#define _(x)
Definition: elog.c:88

◆ log_child_failure()

static void log_child_failure ( int  exitstatus)
static

Definition at line 1589 of file pg_regress.c.

References _, pg_strsignal(), status(), WEXITSTATUS, WIFEXITED, WIFSIGNALED, and WTERMSIG.

Referenced by run_schedule(), and run_single_test().

1590 {
1591  if (WIFEXITED(exitstatus))
1592  status(_(" (test process exited with exit code %d)"),
1593  WEXITSTATUS(exitstatus));
1594  else if (WIFSIGNALED(exitstatus))
1595  {
1596 #if defined(WIN32)
1597  status(_(" (test process was terminated by exception 0x%X)"),
1598  WTERMSIG(exitstatus));
1599 #else
1600  status(_(" (test process was terminated by signal %d: %s)"),
1601  WTERMSIG(exitstatus), pg_strsignal(WTERMSIG(exitstatus)));
1602 #endif
1603  }
1604  else
1605  status(_(" (test process exited with unrecognized status %d)"),
1606  exitstatus);
1607 }
#define WTERMSIG(w)
Definition: win32_port.h:140
#define WIFEXITED(w)
Definition: win32_port.h:137
const char * pg_strsignal(int signum)
Definition: pgstrsignal.c:42
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
#define WIFSIGNALED(w)
Definition: win32_port.h:138
#define _(x)
Definition: elog.c:88
#define WEXITSTATUS(w)
Definition: win32_port.h:139

◆ make_directory()

static void make_directory ( const char *  dir)
static

Definition at line 1286 of file pg_regress.c.

References _, fprintf, mkdir, progname, S_IRWXG, S_IRWXO, S_IRWXU, and strerror.

Referenced by convert_sourcefiles_in(), open_result_files(), and regression_main().

1287 {
1288  if (mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO) < 0)
1289  {
1290  fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
1291  progname, dir, strerror(errno));
1292  exit(2);
1293  }
1294 }
static const char * progname
Definition: pg_regress.c:98
#define fprintf
Definition: port.h:197
#define S_IRWXG
Definition: win32_port.h:281
#define strerror
Definition: port.h:206
#define S_IRWXU
Definition: win32_port.h:269
#define mkdir(a, b)
Definition: win32_port.h:57
#define _(x)
Definition: elog.c:88
#define S_IRWXO
Definition: win32_port.h:293

◆ open_result_files()

static void open_result_files ( void  )
static

Definition at line 1929 of file pg_regress.c.

References _, difffilename, directory_exists(), fprintf, logfile, logfilename, make_directory(), MAXPGPATH, outputdir, pg_strdup(), progname, snprintf, and strerror.

Referenced by regression_main().

1930 {
1931  char file[MAXPGPATH];
1932  FILE *difffile;
1933 
1934  /* create outputdir directory if not present */
1937 
1938  /* create the log file (copy of running status output) */
1939  snprintf(file, sizeof(file), "%s/regression.out", outputdir);
1940  logfilename = pg_strdup(file);
1941  logfile = fopen(logfilename, "w");
1942  if (!logfile)
1943  {
1944  fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
1945  progname, logfilename, strerror(errno));
1946  exit(2);
1947  }
1948 
1949  /* create the diffs file as empty */
1950  snprintf(file, sizeof(file), "%s/regression.diffs", outputdir);
1951  difffilename = pg_strdup(file);
1952  difffile = fopen(difffilename, "w");
1953  if (!difffile)
1954  {
1955  fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
1956  progname, difffilename, strerror(errno));
1957  exit(2);
1958  }
1959  /* we don't keep the diffs file open continuously */
1960  fclose(difffile);
1961 
1962  /* also create the results directory if not present */
1963  snprintf(file, sizeof(file), "%s/results", outputdir);
1964  if (!directory_exists(file))
1965  make_directory(file);
1966 }
static bool directory_exists(const char *dir)
Definition: pg_regress.c:1273
static const char * progname
Definition: pg_regress.c:98
static void make_directory(const char *dir)
Definition: pg_regress.c:1286
static FILE * logfile
Definition: pg_regress.c:100
#define fprintf
Definition: port.h:197
static char * difffilename
Definition: pg_regress.c:101
#define MAXPGPATH
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
char * outputdir
Definition: pg_regress.c:76
static char * logfilename
Definition: pg_regress.c:99
#define strerror
Definition: port.h:206
#define snprintf
Definition: port.h:193
#define _(x)
Definition: elog.c:88

◆ psql_command()

static void psql_command ( const char *  database,
const char *  query,
  ... 
)
static

Definition at line 1106 of file pg_regress.c.

References _, generate_unaccent_rules::args, bindir, fprintf, MAXPGPATH, PID_TYPE, snprintf, and vsnprintf.

Referenced by create_database(), create_role(), drop_database_if_exists(), and drop_role_if_exists().

1107 {
1108  char query_formatted[1024];
1109  char query_escaped[2048];
1110  char psql_cmd[MAXPGPATH + 2048];
1111  va_list args;
1112  char *s;
1113  char *d;
1114 
1115  /* Generate the query with insertion of sprintf arguments */
1116  va_start(args, query);
1117  vsnprintf(query_formatted, sizeof(query_formatted), query, args);
1118  va_end(args);
1119 
1120  /* Now escape any shell double-quote metacharacters */
1121  d = query_escaped;
1122  for (s = query_formatted; *s; s++)
1123  {
1124  if (strchr("\\\"$`", *s))
1125  *d++ = '\\';
1126  *d++ = *s;
1127  }
1128  *d = '\0';
1129 
1130  /* And now we can build and execute the shell command */
1131  snprintf(psql_cmd, sizeof(psql_cmd),
1132  "\"%s%spsql\" -X -c \"%s\" \"%s\"",
1133  bindir ? bindir : "",
1134  bindir ? "/" : "",
1135  query_escaped,
1136  database);
1137 
1138  if (system(psql_cmd) != 0)
1139  {
1140  /* psql probably already reported the error */
1141  fprintf(stderr, _("command failed: %s\n"), psql_cmd);
1142  exit(2);
1143  }
1144 }
#define fprintf
Definition: port.h:197
#define vsnprintf
Definition: port.h:192
#define MAXPGPATH
char * bindir
Definition: pg_regress.c:77
#define snprintf
Definition: port.h:193
#define _(x)
Definition: elog.c:88

◆ regression_main()

int regression_main ( int  argc,
char *  argv[],
init_function  ifunc,
test_function  tfunc 
)

Definition at line 2080 of file pg_regress.c.

References _, add_stringlist_item(), bindir, buf, config_auth_datadir, create_database(), create_role(), debug, DEVNULL, difffilename, directory_exists(), dlpath, doputenv(), drop_database_if_exists(), drop_role_if_exists(), encoding, fail_count, fail_ignore_count, file_size(), fprintf, free_stringlist(), get_progname(), get_restricted_token(), getopt_long(), header(), help(), hostname, i, initialize_environment(), inputdir, INVALID_PID, kill, launcher, logfile, logfilename, make_absolute_path(), make_directory(), max_concurrent_tests, max_connections, MAXPGPATH, _stringlist::next, no_argument, nolocale, open_result_files(), optarg, optind, outputdir, pg_logging_init(), pg_strdup(), PG_TEXTDOMAIN, pg_usleep(), port, port_specified_by_user, postmaster_pid, postmaster_running, pretty_diff_opts, printf, progname, required_argument, rmtree(), run_schedule(), run_single_test(), set_pglocale_pgservice(), SIGKILL, snprintf, sockdir, spawn_process(), split_to_stringlist(), sprintf, stop_postmaster(), _stringlist::str, strerror, success_count, temp_instance, true, ULONGPID, use_existing, user, and wait_seconds.

Referenced by main().

2081 {
2082  static struct option long_options[] = {
2083  {"help", no_argument, NULL, 'h'},
2084  {"version", no_argument, NULL, 'V'},
2085  {"dbname", required_argument, NULL, 1},
2086  {"debug", no_argument, NULL, 2},
2087  {"inputdir", required_argument, NULL, 3},
2088  {"max-connections", required_argument, NULL, 5},
2089  {"encoding", required_argument, NULL, 6},
2090  {"outputdir", required_argument, NULL, 7},
2091  {"schedule", required_argument, NULL, 8},
2092  {"temp-instance", required_argument, NULL, 9},
2093  {"no-locale", no_argument, NULL, 10},
2094  {"host", required_argument, NULL, 13},
2095  {"port", required_argument, NULL, 14},
2096  {"user", required_argument, NULL, 15},
2097  {"bindir", required_argument, NULL, 16},
2098  {"dlpath", required_argument, NULL, 17},
2099  {"create-role", required_argument, NULL, 18},
2100  {"temp-config", required_argument, NULL, 19},
2101  {"use-existing", no_argument, NULL, 20},
2102  {"launcher", required_argument, NULL, 21},
2103  {"load-extension", required_argument, NULL, 22},
2104  {"config-auth", required_argument, NULL, 24},
2105  {"max-concurrent-tests", required_argument, NULL, 25},
2106  {NULL, 0, NULL, 0}
2107  };
2108 
2109  bool use_unix_sockets;
2110  _stringlist *sl;
2111  int c;
2112  int i;
2113  int option_index;
2114  char buf[MAXPGPATH * 4];
2115  char buf2[MAXPGPATH * 4];
2116 
2117  pg_logging_init(argv[0]);
2118  progname = get_progname(argv[0]);
2119  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_regress"));
2120 
2122 
2123  atexit(stop_postmaster);
2124 
2125 #if !defined(HAVE_UNIX_SOCKETS)
2126  use_unix_sockets = false;
2127 #elif defined(WIN32)
2128 
2129  /*
2130  * We don't use Unix-domain sockets on Windows by default, even if the
2131  * build supports them. (See comment at remove_temp() for a reason.)
2132  * Override at your own risk.
2133  */
2134  use_unix_sockets = getenv("PG_TEST_USE_UNIX_SOCKETS") ? true : false;
2135 #else
2136  use_unix_sockets = true;
2137 #endif
2138 
2139  if (!use_unix_sockets)
2140  hostname = "localhost";
2141 
2142  /*
2143  * We call the initialization function here because that way we can set
2144  * default parameters and let them be overwritten by the commandline.
2145  */
2146  ifunc(argc, argv);
2147 
2148  if (getenv("PG_REGRESS_DIFF_OPTS"))
2149  pretty_diff_opts = getenv("PG_REGRESS_DIFF_OPTS");
2150 
2151  while ((c = getopt_long(argc, argv, "hV", long_options, &option_index)) != -1)
2152  {
2153  switch (c)
2154  {
2155  case 'h':
2156  help();
2157  exit(0);
2158  case 'V':
2159  puts("pg_regress (PostgreSQL) " PG_VERSION);
2160  exit(0);
2161  case 1:
2162 
2163  /*
2164  * If a default database was specified, we need to remove it
2165  * before we add the specified one.
2166  */
2169  break;
2170  case 2:
2171  debug = true;
2172  break;
2173  case 3:
2175  break;
2176  case 5:
2177  max_connections = atoi(optarg);
2178  break;
2179  case 6:
2181  break;
2182  case 7:
2184  break;
2185  case 8:
2187  break;
2188  case 9:
2190  break;
2191  case 10:
2192  nolocale = true;
2193  break;
2194  case 13:
2196  break;
2197  case 14:
2198  port = atoi(optarg);
2199  port_specified_by_user = true;
2200  break;
2201  case 15:
2202  user = pg_strdup(optarg);
2203  break;
2204  case 16:
2205  /* "--bindir=" means to use PATH */
2206  if (strlen(optarg))
2207  bindir = pg_strdup(optarg);
2208  else
2209  bindir = NULL;
2210  break;
2211  case 17:
2212  dlpath = pg_strdup(optarg);
2213  break;
2214  case 18:
2216  break;
2217  case 19:
2219  break;
2220  case 20:
2221  use_existing = true;
2222  break;
2223  case 21:
2225  break;
2226  case 22:
2228  break;
2229  case 24:
2231  break;
2232  case 25:
2233  max_concurrent_tests = atoi(optarg);
2234  break;
2235  default:
2236  /* getopt_long already emitted a complaint */
2237  fprintf(stderr, _("\nTry \"%s -h\" for more information.\n"),
2238  progname);
2239  exit(2);
2240  }
2241  }
2242 
2243  /*
2244  * if we still have arguments, they are extra tests to run
2245  */
2246  while (argc - optind >= 1)
2247  {
2249  optind++;
2250  }
2251 
2252  if (config_auth_datadir)
2253  {
2254 #ifdef ENABLE_SSPI
2255  if (!use_unix_sockets)
2256  config_sspi_auth(config_auth_datadir, user);
2257 #endif
2258  exit(0);
2259  }
2260 
2262 
2263  /*
2264  * To reduce chances of interference with parallel installations, use
2265  * a port number starting in the private range (49152-65535)
2266  * calculated from the version number. This aids !HAVE_UNIX_SOCKETS
2267  * systems; elsewhere, the use of a private socket directory already
2268  * prevents interference.
2269  */
2270  port = 0xC000 | (PG_VERSION_NUM & 0x3FFF);
2271 
2275 
2276  /*
2277  * Initialization
2278  */
2280 
2282 
2283 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
2284  unlimit_core_size();
2285 #endif
2286 
2287  if (temp_instance)
2288  {
2289  FILE *pg_conf;
2290  const char *env_wait;
2291  int wait_seconds;
2292 
2293  /*
2294  * Prepare the temp instance
2295  */
2296 
2298  {
2299  header(_("removing existing temp instance"));
2300  if (!rmtree(temp_instance, true))
2301  {
2302  fprintf(stderr, _("\n%s: could not remove temp instance \"%s\"\n"),
2304  exit(2);
2305  }
2306  }
2307 
2308  header(_("creating temporary instance"));
2309 
2310  /* make the temp instance top directory */
2312 
2313  /* and a directory for log files */
2314  snprintf(buf, sizeof(buf), "%s/log", outputdir);
2315  if (!directory_exists(buf))
2316  make_directory(buf);
2317 
2318  /* initdb */
2319  header(_("initializing database system"));
2320  snprintf(buf, sizeof(buf),
2321  "\"%s%sinitdb\" -D \"%s/data\" --no-clean --no-sync%s%s > \"%s/log/initdb.log\" 2>&1",
2322  bindir ? bindir : "",
2323  bindir ? "/" : "",
2324  temp_instance,
2325  debug ? " --debug" : "",
2326  nolocale ? " --no-locale" : "",
2327  outputdir);
2328  if (system(buf))
2329  {
2330  fprintf(stderr, _("\n%s: initdb failed\nExamine %s/log/initdb.log for the reason.\nCommand was: %s\n"), progname, outputdir, buf);
2331  exit(2);
2332  }
2333 
2334  /*
2335  * Adjust the default postgresql.conf for regression testing. The user
2336  * can specify a file to be appended; in any case we expand logging
2337  * and set max_prepared_transactions to enable testing of prepared
2338  * xacts. (Note: to reduce the probability of unexpected shmmax
2339  * failures, don't set max_prepared_transactions any higher than
2340  * actually needed by the prepared_xacts regression test.)
2341  */
2342  snprintf(buf, sizeof(buf), "%s/data/postgresql.conf", temp_instance);
2343  pg_conf = fopen(buf, "a");
2344  if (pg_conf == NULL)
2345  {
2346  fprintf(stderr, _("\n%s: could not open \"%s\" for adding extra config: %s\n"), progname, buf, strerror(errno));
2347  exit(2);
2348  }
2349  fputs("\n# Configuration added by pg_regress\n\n", pg_conf);
2350  fputs("log_autovacuum_min_duration = 0\n", pg_conf);
2351  fputs("log_checkpoints = on\n", pg_conf);
2352  fputs("log_line_prefix = '%m %b[%p] %q%a '\n", pg_conf);
2353  fputs("log_lock_waits = on\n", pg_conf);
2354  fputs("log_temp_files = 128kB\n", pg_conf);
2355  fputs("max_prepared_transactions = 2\n", pg_conf);
2356 
2357  for (sl = temp_configs; sl != NULL; sl = sl->next)
2358  {
2359  char *temp_config = sl->str;
2360  FILE *extra_conf;
2361  char line_buf[1024];
2362 
2363  extra_conf = fopen(temp_config, "r");
2364  if (extra_conf == NULL)
2365  {
2366  fprintf(stderr, _("\n%s: could not open \"%s\" to read extra config: %s\n"), progname, temp_config, strerror(errno));
2367  exit(2);
2368  }
2369  while (fgets(line_buf, sizeof(line_buf), extra_conf) != NULL)
2370  fputs(line_buf, pg_conf);
2371  fclose(extra_conf);
2372  }
2373 
2374  fclose(pg_conf);
2375 
2376 #ifdef ENABLE_SSPI
2377  if (!use_unix_sockets)
2378  {
2379  /*
2380  * Since we successfully used the same buffer for the much-longer
2381  * "initdb" command, this can't truncate.
2382  */
2383  snprintf(buf, sizeof(buf), "%s/data", temp_instance);
2384  config_sspi_auth(buf, NULL);
2385  }
2386 #elif !defined(HAVE_UNIX_SOCKETS)
2387 #error Platform has no means to secure the test installation.
2388 #endif
2389 
2390  /*
2391  * Check if there is a postmaster running already.
2392  */
2393  snprintf(buf2, sizeof(buf2),
2394  "\"%s%spsql\" -X postgres <%s 2>%s",
2395  bindir ? bindir : "",
2396  bindir ? "/" : "",
2397  DEVNULL, DEVNULL);
2398 
2399  for (i = 0; i < 16; i++)
2400  {
2401  if (system(buf2) == 0)
2402  {
2403  char s[16];
2404 
2405  if (port_specified_by_user || i == 15)
2406  {
2407  fprintf(stderr, _("port %d apparently in use\n"), port);
2409  fprintf(stderr, _("%s: could not determine an available port\n"), progname);
2410  fprintf(stderr, _("Specify an unused port using the --port option or shut down any conflicting PostgreSQL servers.\n"));
2411  exit(2);
2412  }
2413 
2414  fprintf(stderr, _("port %d apparently in use, trying %d\n"), port, port + 1);
2415  port++;
2416  sprintf(s, "%d", port);
2417  doputenv("PGPORT", s);
2418  }
2419  else
2420  break;
2421  }
2422 
2423  /*
2424  * Start the temp postmaster
2425  */
2426  header(_("starting postmaster"));
2427  snprintf(buf, sizeof(buf),
2428  "\"%s%spostgres\" -D \"%s/data\" -F%s "
2429  "-c \"listen_addresses=%s\" -k \"%s\" "
2430  "> \"%s/log/postmaster.log\" 2>&1",
2431  bindir ? bindir : "",
2432  bindir ? "/" : "",
2433  temp_instance, debug ? " -d 5" : "",
2434  hostname ? hostname : "", sockdir ? sockdir : "",
2435  outputdir);
2437  if (postmaster_pid == INVALID_PID)
2438  {
2439  fprintf(stderr, _("\n%s: could not spawn postmaster: %s\n"),
2440  progname, strerror(errno));
2441  exit(2);
2442  }
2443 
2444  /*
2445  * Wait till postmaster is able to accept connections; normally this
2446  * is only a second or so, but Cygwin is reportedly *much* slower, and
2447  * test builds using Valgrind or similar tools might be too. Hence,
2448  * allow the default timeout of 60 seconds to be overridden from the
2449  * PGCTLTIMEOUT environment variable.
2450  */
2451  env_wait = getenv("PGCTLTIMEOUT");
2452  if (env_wait != NULL)
2453  {
2454  wait_seconds = atoi(env_wait);
2455  if (wait_seconds <= 0)
2456  wait_seconds = 60;
2457  }
2458  else
2459  wait_seconds = 60;
2460 
2461  for (i = 0; i < wait_seconds; i++)
2462  {
2463  /* Done if psql succeeds */
2464  if (system(buf2) == 0)
2465  break;
2466 
2467  /*
2468  * Fail immediately if postmaster has exited
2469  */
2470 #ifndef WIN32
2471  if (waitpid(postmaster_pid, NULL, WNOHANG) == postmaster_pid)
2472 #else
2473  if (WaitForSingleObject(postmaster_pid, 0) == WAIT_OBJECT_0)
2474 #endif
2475  {
2476  fprintf(stderr, _("\n%s: postmaster failed\nExamine %s/log/postmaster.log for the reason\n"), progname, outputdir);
2477  exit(2);
2478  }
2479 
2480  pg_usleep(1000000L);
2481  }
2482  if (i >= wait_seconds)
2483  {
2484  fprintf(stderr, _("\n%s: postmaster did not respond within %d seconds\nExamine %s/log/postmaster.log for the reason\n"),
2485  progname, wait_seconds, outputdir);
2486 
2487  /*
2488  * If we get here, the postmaster is probably wedged somewhere in
2489  * startup. Try to kill it ungracefully rather than leaving a
2490  * stuck postmaster that might interfere with subsequent test
2491  * attempts.
2492  */
2493 #ifndef WIN32
2494  if (kill(postmaster_pid, SIGKILL) != 0 &&
2495  errno != ESRCH)
2496  fprintf(stderr, _("\n%s: could not kill failed postmaster: %s\n"),
2497  progname, strerror(errno));
2498 #else
2499  if (TerminateProcess(postmaster_pid, 255) == 0)
2500  fprintf(stderr, _("\n%s: could not kill failed postmaster: error code %lu\n"),
2501  progname, GetLastError());
2502 #endif
2503 
2504  exit(2);
2505  }
2506 
2507  postmaster_running = true;
2508 
2509 #ifdef _WIN64
2510 /* need a series of two casts to convert HANDLE without compiler warning */
2511 #define ULONGPID(x) (unsigned long) (unsigned long long) (x)
2512 #else
2513 #define ULONGPID(x) (unsigned long) (x)
2514 #endif
2515  printf(_("running on port %d with PID %lu\n"),
2517  }
2518  else
2519  {
2520  /*
2521  * Using an existing installation, so may need to get rid of
2522  * pre-existing database(s) and role(s)
2523  */
2524  if (!use_existing)
2525  {
2526  for (sl = dblist; sl; sl = sl->next)
2528  for (sl = extraroles; sl; sl = sl->next)
2529  drop_role_if_exists(sl->str);
2530  }
2531  }
2532 
2533  /*
2534  * Create the test database(s) and role(s)
2535  */
2536  if (!use_existing)
2537  {
2538  for (sl = dblist; sl; sl = sl->next)
2539  create_database(sl->str);
2540  for (sl = extraroles; sl; sl = sl->next)
2541  create_role(sl->str, dblist);
2542  }
2543 
2544  /*
2545  * Ready to run the tests
2546  */
2547  header(_("running regression test queries"));
2548 
2549  for (sl = schedulelist; sl != NULL; sl = sl->next)
2550  {
2551  run_schedule(sl->str, tfunc);
2552  }
2553 
2554  for (sl = extra_tests; sl != NULL; sl = sl->next)
2555  {
2556  run_single_test(sl->str, tfunc);
2557  }
2558 
2559  /*
2560  * Shut down temp installation's postmaster
2561  */
2562  if (temp_instance)
2563  {
2564  header(_("shutting down postmaster"));
2565  stop_postmaster();
2566  }
2567 
2568  /*
2569  * If there were no errors, remove the temp instance immediately to
2570  * conserve disk space. (If there were errors, we leave the instance in
2571  * place for possible manual investigation.)
2572  */
2573  if (temp_instance && fail_count == 0 && fail_ignore_count == 0)
2574  {
2575  header(_("removing temporary instance"));
2576  if (!rmtree(temp_instance, true))
2577  fprintf(stderr, _("\n%s: could not remove temp instance \"%s\"\n"),
2579  }
2580 
2581  fclose(logfile);
2582 
2583  /*
2584  * Emit nice-looking summary message
2585  */
2586  if (fail_count == 0 && fail_ignore_count == 0)
2587  snprintf(buf, sizeof(buf),
2588  _(" All %d tests passed. "),
2589  success_count);
2590  else if (fail_count == 0) /* fail_count=0, fail_ignore_count>0 */
2591  snprintf(buf, sizeof(buf),
2592  _(" %d of %d tests passed, %d failed test(s) ignored. "),
2593  success_count,
2596  else if (fail_ignore_count == 0) /* fail_count>0 && fail_ignore_count=0 */
2597  snprintf(buf, sizeof(buf),
2598  _(" %d of %d tests failed. "),
2599  fail_count,
2601  else
2602  /* fail_count>0 && fail_ignore_count>0 */
2603  snprintf(buf, sizeof(buf),
2604  _(" %d of %d tests failed, %d of these failures ignored. "),
2608 
2609  putchar('\n');
2610  for (i = strlen(buf); i > 0; i--)
2611  putchar('=');
2612  printf("\n%s\n", buf);
2613  for (i = strlen(buf); i > 0; i--)
2614  putchar('=');
2615  putchar('\n');
2616  putchar('\n');
2617 
2618  if (file_size(difffilename) > 0)
2619  {
2620  printf(_("The differences that caused some tests to fail can be viewed in the\n"
2621  "file \"%s\". A copy of the test summary that you see\n"
2622  "above is saved in the file \"%s\".\n\n"),
2624  }
2625  else
2626  {
2627  unlink(difffilename);
2628  unlink(logfilename);
2629  }
2630 
2631  if (fail_count != 0)
2632  exit(1);
2633 
2634  return 0;
2635 }
char * make_absolute_path(const char *path)
Definition: path.c:608
static bool directory_exists(const char *dir)
Definition: pg_regress.c:1273
static void drop_role_if_exists(const char *rolename)
Definition: pg_regress.c:2012
static void drop_database_if_exists(const char *dbname)
Definition: pg_regress.c:1969
static int max_concurrent_tests
Definition: pg_regress.c:81
static const char * sockdir
Definition: pg_regress.c:102
static int wait_seconds
Definition: pg_ctl.c:80
static _stringlist * extra_tests
Definition: pg_regress.c:84
static void help(void)
Definition: pg_regress.c:2031
char * str
Definition: pg_regress.h:24
static void initialize_environment(void)
Definition: pg_regress.c:726
static void run_single_test(const char *test, test_function tfunc)
Definition: pg_regress.c:1864
bool debug
Definition: pg_regress.c:74
const char * get_progname(const char *argv0)
Definition: path.c:453
static void static void static void void add_stringlist_item(_stringlist **listhead, const char *str)
Definition: pg_regress.c:155
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
void get_restricted_token(void)
void pg_logging_init(const char *argv0)
Definition: logging.c:81
static void run_schedule(const char *schedule, test_function tfunc)
Definition: pg_regress.c:1613
static char * encoding
Definition: pg_regress.c:82
static const char * progname
Definition: pg_regress.c:98
static void make_directory(const char *dir)
Definition: pg_regress.c:1286
static bool use_existing
Definition: pg_regress.c:88
static bool postmaster_running
Definition: pg_regress.c:112
#define kill(pid, sig)
Definition: win32_port.h:426
#define printf(...)
Definition: port.h:199
static FILE * logfile
Definition: pg_regress.c:100
static void stop_postmaster(void)
Definition: pg_regress.c:258
#define fprintf
Definition: port.h:197
static char * temp_instance
Definition: pg_regress.c:85
char * launcher
Definition: pg_regress.c:78
static void split_to_stringlist(const char *s, const char *delim, _stringlist **listhead)
Definition: pg_regress.c:191
static void free_stringlist(_stringlist **listhead)
Definition: pg_regress.c:176
PID_TYPE spawn_process(const char *cmdline)
Definition: pg_regress.c:1152
#define SIGKILL
Definition: win32_port.h:157
#define sprintf
Definition: port.h:195
#define true
Definition: c.h:328
static char * difffilename
Definition: pg_regress.c:101
void pg_usleep(long microsec)
Definition: signal.c:53
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:50
static _stringlist * loadextension
Definition: pg_regress.c:79
#define MAXPGPATH
_stringlist * dblist
Definition: pg_regress.c:73
static _stringlist * extraroles
Definition: pg_regress.c:94
static _stringlist * temp_configs
Definition: pg_regress.c:86
char * c
const char * pretty_diff_opts
Definition: pg_regress.c:66
static char * buf
Definition: pg_test_fsync.c:67
#define INVALID_PID
Definition: pg_regress.h:15
static void create_role(const char *rolename, const _stringlist *granted_dbs)
Definition: pg_regress.c:2019
static char * dlpath
Definition: pg_regress.c:92
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static int success_count
Definition: pg_regress.c:114
#define DEVNULL
Definition: port.h:124
static int max_connections
Definition: pg_regress.c:80
static int port
Definition: pg_regress.c:90
static PID_TYPE postmaster_pid
Definition: pg_regress.c:111
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:42
#define no_argument
Definition: getopt_long.h:24
static char * config_auth_datadir
Definition: pg_regress.c:95
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1155
static int fail_ignore_count
Definition: pg_regress.c:116
static int fail_count
Definition: pg_regress.c:115
char * outputdir
Definition: pg_regress.c:76
static char * logfilename
Definition: pg_regress.c:99
char * inputdir
Definition: pg_regress.c:75
static long file_size(const char *file)
Definition: pg_regress.c:1219
static bool port_specified_by_user
Definition: pg_regress.c:91
char * bindir
Definition: pg_regress.c:77
#define strerror
Definition: port.h:206
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:208
static void doputenv(const char *var, const char *val)
Definition: pg_regress.c:714
static char * user
Definition: pg_regress.c:93
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:434
char * optarg
Definition: getopt.c:52
static _stringlist * schedulelist
Definition: pg_regress.c:83
struct _stringlist * next
Definition: pg_regress.h:25
int i
static bool nolocale
Definition: pg_regress.c:87
static void open_result_files(void)
Definition: pg_regress.c:1929
static char * hostname
Definition: pg_regress.c:89
#define snprintf
Definition: port.h:193
#define _(x)
Definition: elog.c:88
#define ULONGPID(x)
static void create_database(const char *dbname)
Definition: pg_regress.c:1976

◆ replace_string()

void replace_string ( char *  string,
const char *  replace,
const char *  replacement 
)

Definition at line 442 of file pg_regress.c.

References free, pg_strdup(), and strlcpy().

Referenced by convert_sourcefiles_in(), ecpg_filter(), and ecpg_start_test().

443 {
444  char *ptr;
445 
446  while ((ptr = strstr(string, replace)) != NULL)
447  {
448  char *dup = pg_strdup(string);
449 
450  strlcpy(string, dup, ptr - string + 1);
451  strcat(string, replacement);
452  strcat(string, dup + (ptr - string) + strlen(replace));
453  free(dup);
454  }
455 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define free(a)
Definition: header.h:65
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45

◆ results_differ()

static bool results_differ ( const char *  testname,
const char *  resultsfile,
const char *  default_expectfile 
)
static

Definition at line 1367 of file pg_regress.c.

References _, basic_diff_opts, difffilename, file_exists(), file_line_count(), fprintf, free, get_alternative_expectfile(), get_expectfile(), i, MAXPGPATH, pretty_diff_opts, run_diff(), snprintf, strerror, and strlcpy().

Referenced by run_schedule(), and run_single_test().

1368 {
1369  char expectfile[MAXPGPATH];
1370  char diff[MAXPGPATH];
1371  char cmd[MAXPGPATH * 3];
1372  char best_expect_file[MAXPGPATH];
1373  FILE *difffile;
1374  int best_line_count;
1375  int i;
1376  int l;
1377  const char *platform_expectfile;
1378 
1379  /*
1380  * We can pass either the resultsfile or the expectfile, they should have
1381  * the same type (filename.type) anyway.
1382  */
1383  platform_expectfile = get_expectfile(testname, resultsfile);
1384 
1385  strlcpy(expectfile, default_expectfile, sizeof(expectfile));
1386  if (platform_expectfile)
1387  {
1388  /*
1389  * Replace everything after the last slash in expectfile with what the
1390  * platform_expectfile contains.
1391  */
1392  char *p = strrchr(expectfile, '/');
1393 
1394  if (p)
1395  strcpy(++p, platform_expectfile);
1396  }
1397 
1398  /* Name to use for temporary diff file */
1399  snprintf(diff, sizeof(diff), "%s.diff", resultsfile);
1400 
1401  /* OK, run the diff */
1402  snprintf(cmd, sizeof(cmd),
1403  "diff %s \"%s\" \"%s\" > \"%s\"",
1404  basic_diff_opts, expectfile, resultsfile, diff);
1405 
1406  /* Is the diff file empty? */
1407  if (run_diff(cmd, diff) == 0)
1408  {
1409  unlink(diff);
1410  return false;
1411  }
1412 
1413  /* There may be secondary comparison files that match better */
1414  best_line_count = file_line_count(diff);
1415  strcpy(best_expect_file, expectfile);
1416 
1417  for (i = 0; i <= 9; i++)
1418  {
1419  char *alt_expectfile;
1420 
1421  alt_expectfile = get_alternative_expectfile(expectfile, i);
1422  if (!alt_expectfile)
1423  {
1424  fprintf(stderr, _("Unable to check secondary comparison files: %s\n"),
1425  strerror(errno));
1426  exit(2);
1427  }
1428 
1429  if (!file_exists(alt_expectfile))
1430  {
1431  free(alt_expectfile);
1432  continue;
1433  }
1434 
1435  snprintf(cmd, sizeof(cmd),
1436  "diff %s \"%s\" \"%s\" > \"%s\"",
1437  basic_diff_opts, alt_expectfile, resultsfile, diff);
1438 
1439  if (run_diff(cmd, diff) == 0)
1440  {
1441  unlink(diff);
1442  free(alt_expectfile);
1443  return false;
1444  }
1445 
1446  l = file_line_count(diff);
1447  if (l < best_line_count)
1448  {
1449  /* This diff was a better match than the last one */
1450  best_line_count = l;
1451  strlcpy(best_expect_file, alt_expectfile, sizeof(best_expect_file));
1452  }
1453  free(alt_expectfile);
1454  }
1455 
1456  /*
1457  * fall back on the canonical results file if we haven't tried it yet and
1458  * haven't found a complete match yet.
1459  */
1460 
1461  if (platform_expectfile)
1462  {
1463  snprintf(cmd, sizeof(cmd),
1464  "diff %s \"%s\" \"%s\" > \"%s\"",
1465  basic_diff_opts, default_expectfile, resultsfile, diff);
1466 
1467  if (run_diff(cmd, diff) == 0)
1468  {
1469  /* No diff = no changes = good */
1470  unlink(diff);
1471  return false;
1472  }
1473 
1474  l = file_line_count(diff);
1475  if (l < best_line_count)
1476  {
1477  /* This diff was a better match than the last one */
1478  best_line_count = l;
1479  strlcpy(best_expect_file, default_expectfile, sizeof(best_expect_file));
1480  }
1481  }
1482 
1483  /*
1484  * Use the best comparison file to generate the "pretty" diff, which we
1485  * append to the diffs summary file.
1486  */
1487 
1488  /* Write diff header */
1489  difffile = fopen(difffilename, "a");
1490  if (difffile)
1491  {
1492  fprintf(difffile,
1493  "diff %s %s %s\n",
1494  pretty_diff_opts, best_expect_file, resultsfile);
1495  fclose(difffile);
1496  }
1497 
1498  /* Run diff */
1499  snprintf(cmd, sizeof(cmd),
1500  "diff %s \"%s\" \"%s\" >> \"%s\"",
1501  pretty_diff_opts, best_expect_file, resultsfile, difffilename);
1502  run_diff(cmd, difffilename);
1503 
1504  unlink(diff);
1505  return true;
1506 }
static char * get_alternative_expectfile(const char *expectfile, int i)
Definition: pg_regress.c:1300
#define fprintf
Definition: port.h:197
static char * difffilename
Definition: pg_regress.c:101
#define MAXPGPATH
const char * pretty_diff_opts
Definition: pg_regress.c:66
static int run_diff(const char *cmd, const char *filename)
Definition: pg_regress.c:1334
#define free(a)
Definition: header.h:65
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
static const char * get_expectfile(const char *testname, const char *file)
Definition: pg_regress.c:685
static int file_line_count(const char *file)
Definition: pg_regress.c:1240
#define strerror
Definition: port.h:206
bool file_exists(const char *file)
Definition: pg_regress.c:1262
int i
const char * basic_diff_opts
Definition: pg_regress.c:65
#define snprintf
Definition: port.h:193
#define _(x)
Definition: elog.c:88

◆ run_diff()

static int run_diff ( const char *  cmd,
const char *  filename 
)
static

Definition at line 1334 of file pg_regress.c.

References _, file_size(), fprintf, WEXITSTATUS, and WIFEXITED.

Referenced by results_differ().

1335 {
1336  int r;
1337 
1338  r = system(cmd);
1339  if (!WIFEXITED(r) || WEXITSTATUS(r) > 1)
1340  {
1341  fprintf(stderr, _("diff command failed with status %d: %s\n"), r, cmd);
1342  exit(2);
1343  }
1344 #ifdef WIN32
1345 
1346  /*
1347  * On WIN32, if the 'diff' command cannot be found, system() returns 1,
1348  * but produces nothing to stdout, so we check for that here.
1349  */
1350  if (WEXITSTATUS(r) == 1 && file_size(filename) <= 0)
1351  {
1352  fprintf(stderr, _("diff command not found: %s\n"), cmd);
1353  exit(2);
1354  }
1355 #endif
1356 
1357  return WEXITSTATUS(r);
1358 }
#define fprintf
Definition: port.h:197
#define WIFEXITED(w)
Definition: win32_port.h:137
static long file_size(const char *file)
Definition: pg_regress.c:1219
static char * filename
Definition: pg_dumpall.c:90
#define _(x)
Definition: elog.c:88
#define WEXITSTATUS(w)
Definition: win32_port.h:139

◆ run_schedule()

static void run_schedule ( const char *  schedule,
test_function  tfunc 
)
static

Definition at line 1613 of file pg_regress.c.

References _, add_stringlist_item(), fail_count, fail_ignore_count, fprintf, free_stringlist(), i, INSTR_TIME_GET_MILLISEC, INSTR_TIME_SET_CURRENT, INSTR_TIME_SUBTRACT, log_child_failure(), max_concurrent_tests, max_connections, MAX_PARALLEL_TESTS, _stringlist::next, pg_free(), pg_strdup(), PID_TYPE, printf, progname, results_differ(), status(), status_end(), _stringlist::str, strerror, success_count, _resultmap::test, and wait_for_tests().

Referenced by regression_main().

1614 {
1615 #define MAX_PARALLEL_TESTS 100
1616  char *tests[MAX_PARALLEL_TESTS];
1617  _stringlist *resultfiles[MAX_PARALLEL_TESTS];
1618  _stringlist *expectfiles[MAX_PARALLEL_TESTS];
1621  instr_time starttimes[MAX_PARALLEL_TESTS];
1622  instr_time stoptimes[MAX_PARALLEL_TESTS];
1623  int statuses[MAX_PARALLEL_TESTS];
1624  _stringlist *ignorelist = NULL;
1625  char scbuf[1024];
1626  FILE *scf;
1627  int line_num = 0;
1628 
1629  memset(tests, 0, sizeof(tests));
1630  memset(resultfiles, 0, sizeof(resultfiles));
1631  memset(expectfiles, 0, sizeof(expectfiles));
1632  memset(tags, 0, sizeof(tags));
1633 
1634  scf = fopen(schedule, "r");
1635  if (!scf)
1636  {
1637  fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
1638  progname, schedule, strerror(errno));
1639  exit(2);
1640  }
1641 
1642  while (fgets(scbuf, sizeof(scbuf), scf))
1643  {
1644  char *test = NULL;
1645  char *c;
1646  int num_tests;
1647  bool inword;
1648  int i;
1649 
1650  line_num++;
1651 
1652  /* strip trailing whitespace, especially the newline */
1653  i = strlen(scbuf);
1654  while (i > 0 && isspace((unsigned char) scbuf[i - 1]))
1655  scbuf[--i] = '\0';
1656 
1657  if (scbuf[0] == '\0' || scbuf[0] == '#')
1658  continue;
1659  if (strncmp(scbuf, "test: ", 6) == 0)
1660  test = scbuf + 6;
1661  else if (strncmp(scbuf, "ignore: ", 8) == 0)
1662  {
1663  c = scbuf + 8;
1664  while (*c && isspace((unsigned char) *c))
1665  c++;
1666  add_stringlist_item(&ignorelist, c);
1667 
1668  /*
1669  * Note: ignore: lines do not run the test, they just say that
1670  * failure of this test when run later on is to be ignored. A bit
1671  * odd but that's how the shell-script version did it.
1672  */
1673  continue;
1674  }
1675  else
1676  {
1677  fprintf(stderr, _("syntax error in schedule file \"%s\" line %d: %s\n"),
1678  schedule, line_num, scbuf);
1679  exit(2);
1680  }
1681 
1682  num_tests = 0;
1683  inword = false;
1684  for (c = test;; c++)
1685  {
1686  if (*c == '\0' || isspace((unsigned char) *c))
1687  {
1688  if (inword)
1689  {
1690  /* Reached end of a test name */
1691  char sav;
1692 
1693  if (num_tests >= MAX_PARALLEL_TESTS)
1694  {
1695  fprintf(stderr, _("too many parallel tests (more than %d) in schedule file \"%s\" line %d: %s\n"),
1696  MAX_PARALLEL_TESTS, schedule, line_num, scbuf);
1697  exit(2);
1698  }
1699  sav = *c;
1700  *c = '\0';
1701  tests[num_tests] = pg_strdup(test);
1702  num_tests++;
1703  *c = sav;
1704  inword = false;
1705  }
1706  if (*c == '\0')
1707  break; /* loop exit is here */
1708  }
1709  else if (!inword)
1710  {
1711  /* Start of a test name */
1712  test = c;
1713  inword = true;
1714  }
1715  }
1716 
1717  if (num_tests == 0)
1718  {
1719  fprintf(stderr, _("syntax error in schedule file \"%s\" line %d: %s\n"),
1720  schedule, line_num, scbuf);
1721  exit(2);
1722  }
1723 
1724  if (num_tests == 1)
1725  {
1726  status(_("test %-28s ... "), tests[0]);
1727  pids[0] = (tfunc) (tests[0], &resultfiles[0], &expectfiles[0], &tags[0]);
1728  INSTR_TIME_SET_CURRENT(starttimes[0]);
1729  wait_for_tests(pids, statuses, stoptimes, NULL, 1);
1730  /* status line is finished below */
1731  }
1732  else if (max_concurrent_tests > 0 && max_concurrent_tests < num_tests)
1733  {
1734  fprintf(stderr, _("too many parallel tests (more than %d) in schedule file \"%s\" line %d: %s\n"),
1735  max_concurrent_tests, schedule, line_num, scbuf);
1736  exit(2);
1737  }
1738  else if (max_connections > 0 && max_connections < num_tests)
1739  {
1740  int oldest = 0;
1741 
1742  status(_("parallel group (%d tests, in groups of %d): "),
1743  num_tests, max_connections);
1744  for (i = 0; i < num_tests; i++)
1745  {
1746  if (i - oldest >= max_connections)
1747  {
1748  wait_for_tests(pids + oldest, statuses + oldest,
1749  stoptimes + oldest,
1750  tests + oldest, i - oldest);
1751  oldest = i;
1752  }
1753  pids[i] = (tfunc) (tests[i], &resultfiles[i], &expectfiles[i], &tags[i]);
1754  INSTR_TIME_SET_CURRENT(starttimes[i]);
1755  }
1756  wait_for_tests(pids + oldest, statuses + oldest,
1757  stoptimes + oldest,
1758  tests + oldest, i - oldest);
1759  status_end();
1760  }
1761  else
1762  {
1763  status(_("parallel group (%d tests): "), num_tests);
1764  for (i = 0; i < num_tests; i++)
1765  {
1766  pids[i] = (tfunc) (tests[i], &resultfiles[i], &expectfiles[i], &tags[i]);
1767  INSTR_TIME_SET_CURRENT(starttimes[i]);
1768  }
1769  wait_for_tests(pids, statuses, stoptimes, tests, num_tests);
1770  status_end();
1771  }
1772 
1773  /* Check results for all tests */
1774  for (i = 0; i < num_tests; i++)
1775  {
1776  _stringlist *rl,
1777  *el,
1778  *tl;
1779  bool differ = false;
1780 
1781  if (num_tests > 1)
1782  status(_(" %-28s ... "), tests[i]);
1783 
1784  /*
1785  * Advance over all three lists simultaneously.
1786  *
1787  * Compare resultfiles[j] with expectfiles[j] always. Tags are
1788  * optional but if there are tags, the tag list has the same
1789  * length as the other two lists.
1790  */
1791  for (rl = resultfiles[i], el = expectfiles[i], tl = tags[i];
1792  rl != NULL; /* rl and el have the same length */
1793  rl = rl->next, el = el->next,
1794  tl = tl ? tl->next : NULL)
1795  {
1796  bool newdiff;
1797 
1798  newdiff = results_differ(tests[i], rl->str, el->str);
1799  if (newdiff && tl)
1800  {
1801  printf("%s ", tl->str);
1802  }
1803  differ |= newdiff;
1804  }
1805 
1806  if (differ)
1807  {
1808  bool ignore = false;
1809  _stringlist *sl;
1810 
1811  for (sl = ignorelist; sl != NULL; sl = sl->next)
1812  {
1813  if (strcmp(tests[i], sl->str) == 0)
1814  {
1815  ignore = true;
1816  break;
1817  }
1818  }
1819  if (ignore)
1820  {
1821  status(_("failed (ignored)"));
1823  }
1824  else
1825  {
1826  status(_("FAILED"));
1827  fail_count++;
1828  }
1829  }
1830  else
1831  {
1832  status(_("ok ")); /* align with FAILED */
1833  success_count++;
1834  }
1835 
1836  if (statuses[i] != 0)
1837  log_child_failure(statuses[i]);
1838 
1839  INSTR_TIME_SUBTRACT(stoptimes[i], starttimes[i]);
1840  status(_(" %8.0f ms"), INSTR_TIME_GET_MILLISEC(stoptimes[i]));
1841 
1842  status_end();
1843  }
1844 
1845  for (i = 0; i < num_tests; i++)
1846  {
1847  pg_free(tests[i]);
1848  tests[i] = NULL;
1849  free_stringlist(&resultfiles[i]);
1850  free_stringlist(&expectfiles[i]);
1851  free_stringlist(&tags[i]);
1852  }
1853  }
1854 
1855  free_stringlist(&ignorelist);
1856 
1857  fclose(scf);
1858 }
static int max_concurrent_tests
Definition: pg_regress.c:81
static void test(void)
static void log_child_failure(int exitstatus)
Definition: pg_regress.c:1589
char * str
Definition: pg_regress.h:24
static void static void static void void add_stringlist_item(_stringlist **listhead, const char *str)
Definition: pg_regress.c:155
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:202
struct timeval instr_time
Definition: instr_time.h:150
static const char * progname
Definition: pg_regress.c:98
static void status_end(void)
Definition: pg_regress.c:246
#define PID_TYPE
Definition: pg_regress.h:14
#define printf(...)
Definition: port.h:199
#define fprintf
Definition: port.h:197
static bool results_differ(const char *testname, const char *resultsfile, const char *default_expectfile)
Definition: pg_regress.c:1367
static void free_stringlist(_stringlist **listhead)
Definition: pg_regress.c:176
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:170
char * c
#define MAX_PARALLEL_TESTS
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static int success_count
Definition: pg_regress.c:114
static int max_connections
Definition: pg_regress.c:80
static int fail_ignore_count
Definition: pg_regress.c:116
static int fail_count
Definition: pg_regress.c:115
#define strerror
Definition: port.h:206
void pg_free(void *ptr)
Definition: fe_memutils.c:105
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:156
struct _stringlist * next
Definition: pg_regress.h:25
int i
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
#define _(x)
Definition: elog.c:88
static void wait_for_tests(PID_TYPE *pids, int *statuses, instr_time *stoptimes, char **names, int num_tests)
Definition: pg_regress.c:1517

◆ run_single_test()

static void run_single_test ( const char *  test,
test_function  tfunc 
)
static

Definition at line 1864 of file pg_regress.c.

References _, fail_count, INSTR_TIME_GET_MILLISEC, INSTR_TIME_SET_CURRENT, INSTR_TIME_SUBTRACT, log_child_failure(), _stringlist::next, PID_TYPE, printf, results_differ(), status(), status_end(), _stringlist::str, success_count, and wait_for_tests().

Referenced by regression_main().

1865 {
1866  PID_TYPE pid;
1867  instr_time starttime;
1868  instr_time stoptime;
1869  int exit_status;
1870  _stringlist *resultfiles = NULL;
1871  _stringlist *expectfiles = NULL;
1872  _stringlist *tags = NULL;
1873  _stringlist *rl,
1874  *el,
1875  *tl;
1876  bool differ = false;
1877 
1878  status(_("test %-28s ... "), test);
1879  pid = (tfunc) (test, &resultfiles, &expectfiles, &tags);
1880  INSTR_TIME_SET_CURRENT(starttime);
1881  wait_for_tests(&pid, &exit_status, &stoptime, NULL, 1);
1882 
1883  /*
1884  * Advance over all three lists simultaneously.
1885  *
1886  * Compare resultfiles[j] with expectfiles[j] always. Tags are optional
1887  * but if there are tags, the tag list has the same length as the other
1888  * two lists.
1889  */
1890  for (rl = resultfiles, el = expectfiles, tl = tags;
1891  rl != NULL; /* rl and el have the same length */
1892  rl = rl->next, el = el->next,
1893  tl = tl ? tl->next : NULL)
1894  {
1895  bool newdiff;
1896 
1897  newdiff = results_differ(test, rl->str, el->str);
1898  if (newdiff && tl)
1899  {
1900  printf("%s ", tl->str);
1901  }
1902  differ |= newdiff;
1903  }
1904 
1905  if (differ)
1906  {
1907  status(_("FAILED"));
1908  fail_count++;
1909  }
1910  else
1911  {
1912  status(_("ok ")); /* align with FAILED */
1913  success_count++;
1914  }
1915 
1916  if (exit_status != 0)
1917  log_child_failure(exit_status);
1918 
1919  INSTR_TIME_SUBTRACT(stoptime, starttime);
1920  status(_(" %8.0f ms"), INSTR_TIME_GET_MILLISEC(stoptime));
1921 
1922  status_end();
1923 }
static void test(void)
static void log_child_failure(int exitstatus)
Definition: pg_regress.c:1589
char * str
Definition: pg_regress.h:24
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:202
struct timeval instr_time
Definition: instr_time.h:150
static void status_end(void)
Definition: pg_regress.c:246
#define PID_TYPE
Definition: pg_regress.h:14
#define printf(...)
Definition: port.h:199
static bool results_differ(const char *testname, const char *resultsfile, const char *default_expectfile)
Definition: pg_regress.c:1367
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:170
static int success_count
Definition: pg_regress.c:114
static int fail_count
Definition: pg_regress.c:115
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:156
struct _stringlist * next
Definition: pg_regress.h:25
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
#define _(x)
Definition: elog.c:88
static void wait_for_tests(PID_TYPE *pids, int *statuses, instr_time *stoptimes, char **names, int num_tests)
Definition: pg_regress.c:1517

◆ spawn_process()

PID_TYPE spawn_process ( const char *  cmdline)

Definition at line 1152 of file pg_regress.c.

References _, fprintf, logfile, progname, psprintf(), shellprog, generate_unaccent_rules::stdout, and strerror.

Referenced by ecpg_start_test(), isolation_start_test(), psql_start_test(), and regression_main().

1153 {
1154 #ifndef WIN32
1155  pid_t pid;
1156 
1157  /*
1158  * Must flush I/O buffers before fork. Ideally we'd use fflush(NULL) here
1159  * ... does anyone still care about systems where that doesn't work?
1160  */
1161  fflush(stdout);
1162  fflush(stderr);
1163  if (logfile)
1164  fflush(logfile);
1165 
1166  pid = fork();
1167  if (pid == -1)
1168  {
1169  fprintf(stderr, _("%s: could not fork: %s\n"),
1170  progname, strerror(errno));
1171  exit(2);
1172  }
1173  if (pid == 0)
1174  {
1175  /*
1176  * In child
1177  *
1178  * Instead of using system(), exec the shell directly, and tell it to
1179  * "exec" the command too. This saves two useless processes per
1180  * parallel test case.
1181  */
1182  char *cmdline2;
1183 
1184  cmdline2 = psprintf("exec %s", cmdline);
1185  execl(shellprog, shellprog, "-c", cmdline2, (char *) NULL);
1186  fprintf(stderr, _("%s: could not exec \"%s\": %s\n"),
1187  progname, shellprog, strerror(errno));
1188  _exit(1); /* not exit() here... */
1189  }
1190  /* in parent */
1191  return pid;
1192 #else
1193  PROCESS_INFORMATION pi;
1194  char *cmdline2;
1195  HANDLE restrictedToken;
1196  const char *comspec;
1197 
1198  /* Find CMD.EXE location using COMSPEC, if it's set */
1199  comspec = getenv("COMSPEC");
1200  if (comspec == NULL)
1201  comspec = "CMD";
1202 
1203  memset(&pi, 0, sizeof(pi));
1204  cmdline2 = psprintf("\"%s\" /c \"%s\"", comspec, cmdline);
1205 
1206  if ((restrictedToken =
1207  CreateRestrictedProcess(cmdline2, &pi)) == 0)
1208  exit(2);
1209 
1210  CloseHandle(pi.hThread);
1211  return pi.hProcess;
1212 #endif
1213 }
static char * shellprog
Definition: pg_regress.c:56
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static const char * progname
Definition: pg_regress.c:98
static FILE * logfile
Definition: pg_regress.c:100
#define fprintf
Definition: port.h:197
#define strerror
Definition: port.h:206
#define _(x)
Definition: elog.c:88

◆ split_to_stringlist()

static void split_to_stringlist ( const char *  s,
const char *  delim,
_stringlist **  listhead 
)
static

Definition at line 191 of file pg_regress.c.

References add_stringlist_item(), free, and pg_strdup().

Referenced by regression_main().

192 {
193  char *sc = pg_strdup(s);
194  char *token = strtok(sc, delim);
195 
196  while (token)
197  {
198  add_stringlist_item(listhead, token);
199  token = strtok(NULL, delim);
200  }
201  free(sc);
202 }
static void static void static void void add_stringlist_item(_stringlist **listhead, const char *str)
Definition: pg_regress.c:155
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define free(a)
Definition: header.h:65

◆ status()

static void status ( const char *  fmt,
  ... 
)
static

Definition at line 225 of file pg_regress.c.

References logfile, generate_unaccent_rules::stdout, and vfprintf.

Referenced by _bt_first(), _bt_parallel_seize(), _bt_readnextpage(), _bt_steppage(), apw_start_leader_worker(), AtAbort_Portals(), AtCleanup_Portals(), AtEOSubXact_RelationCache(), AtEOXact_RelationCache(), AtPrepare_Locks(), AtSubAbort_Portals(), AtSubCleanup_Portals(), AtSubCommit_Portals(), BackendInitialize(), BaseBackup(), be_lo_lseek(), be_lo_lseek64(), bf_check_supported_key_len(), check_worker_status(), CheckSCRAMAuth(), ClientAuthentication(), compute_new_xmax_infomask(), dblink_get_connections(), DestroyPartitionDirectory(), do_kill(), do_lo_export(), do_lo_unlink(), DoesMultiXactIdConflict(), ecpg_process_output(), ecpg_store_result(), exec_command(), exec_command_crosstabview(), exec_command_d(), exec_command_edit(), exec_command_ef_ev(), exec_command_g(), exec_command_gdesc(), exec_command_gexec(), exec_command_gset(), exec_command_lo(), exec_command_quit(), exec_command_sf_sv(), exec_command_write(), executeQueryOrDie(), forget_invalid_pages(), forget_invalid_pages_db(), FreezeMultiXactId(), get_collation_actual_version(), GetLockmodeName(), GetMultiXactIdHintBits(), HandleSlashCmds(), heap_lock_tuple(), heap_lock_updated_tuple_rec(), HoldPinnedPortals(), InvalidateAttoptCacheCallback(), InvalidateOprCacheCallBack(), InvalidateOprProofCacheCallBack(), InvalidateShippableCacheCallback(), InvalidateTableSpaceCacheCallback(), InvalidateTSCacheCallBack(), libpqrcv_connect(), ListenToWorkers(), lo_read(), lo_write(), LockReassignCurrentOwner(), LockReleaseAll(), LockReleaseCurrentOwner(), LockReleaseSession(), log_child_failure(), logicalrep_partmap_invalidate_cb(), logicalrep_relmap_invalidate_cb(), MultiXactIdCreateFromMembers(), MultiXactIdExpand(), MultiXactIdGetUpdateXid(), mxid_to_string(), parallel_restore(), parseServiceInfo(), parseWorkerResponse(), pg_collation_actual_version(), pg_newlocale_from_collation(), pg_prepared_xact(), pg_start_backup(), pg_stop_backup(), pg_stop_backup_v2(), pg_xact_status(), pgrowlocks(), PortalErrorCleanup(), PortalHashTableDeleteAll(), PostmasterMain(), PostPrepare_Locks(), pqFunctionCall2(), pqFunctionCall3(), pqGetline3(), PQmakeEmptyPGresult(), PQresetPoll(), PQresStatus(), PQsetClientEncoding(), PreCommit_Portals(), RecordNewMultiXact(), rel_sync_cache_publication_cb(), RelationCacheInitializePhase3(), RelationCacheInvalidate(), RelfilenodeMapInvalidateCallback(), RememberToFreeTupleDescAtEOX(), restore_toc_entry(), run_reindex_command(), run_schedule(), run_single_test(), run_vacuum_command(), sepgsql_client_auth(), SerializeEnumBlacklist(), shm_mq_counterparty_gone(), shm_mq_wait_internal(), smgrcloseall(), StandbyReleaseAllLocks(), StandbyReleaseOldLocks(), str_numth(), test_config_settings(), ThereAreNoReadyPortals(), TransactionGroupUpdateXidStatus(), TransactionIdGetStatus(), TypeCacheOpcCallback(), TypeCacheRelCallback(), TypeCacheTypCallback(), uuid_generate_internal(), varstr_abbrev_convert(), varstr_cmp(), varstrfastcmp_locale(), wait_for_postmaster(), wait_for_tests(), WaitForBackgroundWorkerShutdown(), WaitForBackgroundWorkerStartup(), WaitForCommands(), WaitForParallelWorkersToAttach(), WaitForParallelWorkersToExit(), WaitForReplicationWorkerAttach(), WaitForTerminatingWorkers(), worker_spi_launch(), write_relcache_init_file(), XLogCheckInvalidPages(), and yearistype().

226 {
227  va_list ap;
228 
229  va_start(ap, fmt);
230  vfprintf(stdout, fmt, ap);
231  fflush(stdout);
232  va_end(ap);
233 
234  if (logfile)
235  {
236  va_start(ap, fmt);
237  vfprintf(logfile, fmt, ap);
238  va_end(ap);
239  }
240 }
static FILE * logfile
Definition: pg_regress.c:100
#define vfprintf
Definition: port.h:196

◆ status_end()

static void status_end ( void  )
static

Definition at line 246 of file pg_regress.c.

References fprintf, logfile, and generate_unaccent_rules::stdout.

Referenced by run_schedule(), and run_single_test().

247 {
248  fprintf(stdout, "\n");
249  fflush(stdout);
250  if (logfile)
251  fprintf(logfile, "\n");
252 }
static FILE * logfile
Definition: pg_regress.c:100
#define fprintf
Definition: port.h:197

◆ stop_postmaster()

static void stop_postmaster ( void  )
static

Definition at line 258 of file pg_regress.c.

References _, Assert, bindir, buf, fprintf, MAXPGPATH, mkdtemp(), port, postmaster_running, pqsignal(), progname, psprintf(), SIG_DFL, SIGHUP, SIGPIPE, snprintf, generate_unaccent_rules::stdout, strerror, temp_instance, and UNIXSOCK_PATH.

Referenced by regression_main().

259 {
260  if (postmaster_running)
261  {
262  /* We use pg_ctl to issue the kill and wait for stop */
263  char buf[MAXPGPATH * 2];
264  int r;
265 
266  /* On Windows, system() seems not to force fflush, so... */
267  fflush(stdout);
268  fflush(stderr);
269 
270  snprintf(buf, sizeof(buf),
271  "\"%s%spg_ctl\" stop -D \"%s/data\" -s",
272  bindir ? bindir : "",
273  bindir ? "/" : "",
274  temp_instance);
275  r = system(buf);
276  if (r != 0)
277  {
278  fprintf(stderr, _("\n%s: could not stop postmaster: exit code was %d\n"),
279  progname, r);
280  _exit(2); /* not exit(), that could be recursive */
281  }
282 
283  postmaster_running = false;
284  }
285 }
static const char * progname
Definition: pg_regress.c:98
static bool postmaster_running
Definition: pg_regress.c:112
#define fprintf
Definition: port.h:197
static char * temp_instance
Definition: pg_regress.c:85
#define MAXPGPATH
static char * buf
Definition: pg_test_fsync.c:67
char * bindir
Definition: pg_regress.c:77
#define snprintf
Definition: port.h:193
#define _(x)
Definition: elog.c:88

◆ string_matches_pattern()

static bool string_matches_pattern ( const char *  str,
const char *  pattern 
)
static

Definition at line 377 of file pg_regress.c.

Referenced by load_resultmap().

378 {
379  while (*str && *pattern)
380  {
381  if (*pattern == '.' && pattern[1] == '*')
382  {
383  pattern += 2;
384  /* Trailing .* matches everything. */
385  if (*pattern == '\0')
386  return true;
387 
388  /*
389  * Otherwise, scan for a text position at which we can match the
390  * rest of the pattern.
391  */
392  while (*str)
393  {
394  /*
395  * Optimization to prevent most recursion: don't recurse
396  * unless first pattern char might match this text char.
397  */
398  if (*str == *pattern || *pattern == '.')
399  {
400  if (string_matches_pattern(str, pattern))
401  return true;
402  }
403 
404  str++;
405  }
406 
407  /*
408  * End of text with no match.
409  */
410  return false;
411  }
412  else if (*pattern != '.' && *str != *pattern)
413  {
414  /*
415  * Not the single-character wildcard and no explicit match? Then
416  * time to quit...
417  */
418  return false;
419  }
420 
421  str++;
422  pattern++;
423  }
424 
425  if (*pattern == '\0')
426  return true; /* end of pattern, so declare match */
427 
428  /* End of input string. Do we have matching pattern remaining? */
429  while (*pattern == '.' && pattern[1] == '*')
430  pattern += 2;
431  if (*pattern == '\0')
432  return true; /* end of pattern, so declare match */
433 
434  return false;
435 }
static bool string_matches_pattern(const char *str, const char *pattern)
Definition: pg_regress.c:377

◆ wait_for_tests()

static void wait_for_tests ( PID_TYPE pids,
int *  statuses,
instr_time stoptimes,
char **  names,
int  num_tests 
)
static

Definition at line 1517 of file pg_regress.c.

References _, fprintf, free, i, INSTR_TIME_SET_CURRENT, INVALID_PID, pg_malloc(), PID_TYPE, status(), and strerror.

Referenced by run_schedule(), and run_single_test().

1519 {
1520  int tests_left;
1521  int i;
1522 
1523 #ifdef WIN32
1524  PID_TYPE *active_pids = pg_malloc(num_tests * sizeof(PID_TYPE));
1525 
1526  memcpy(active_pids, pids, num_tests * sizeof(PID_TYPE));
1527 #endif
1528 
1529  tests_left = num_tests;
1530  while (tests_left > 0)
1531  {
1532  PID_TYPE p;
1533 
1534 #ifndef WIN32
1535  int exit_status;
1536 
1537  p = wait(&exit_status);
1538 
1539  if (p == INVALID_PID)
1540  {
1541  fprintf(stderr, _("failed to wait for subprocesses: %s\n"),
1542  strerror(errno));
1543  exit(2);
1544  }
1545 #else
1546  DWORD exit_status;
1547  int r;
1548 
1549  r = WaitForMultipleObjects(tests_left, active_pids, FALSE, INFINITE);
1550  if (r < WAIT_OBJECT_0 || r >= WAIT_OBJECT_0 + tests_left)
1551  {
1552  fprintf(stderr, _("failed to wait for subprocesses: error code %lu\n"),
1553  GetLastError());
1554  exit(2);
1555  }
1556  p = active_pids[r - WAIT_OBJECT_0];
1557  /* compact the active_pids array */
1558  active_pids[r - WAIT_OBJECT_0] = active_pids[tests_left - 1];
1559 #endif /* WIN32 */
1560 
1561  for (i = 0; i < num_tests; i++)
1562  {
1563  if (p == pids[i])
1564  {
1565 #ifdef WIN32
1566  GetExitCodeProcess(pids[i], &exit_status);
1567  CloseHandle(pids[i]);
1568 #endif
1569  pids[i] = INVALID_PID;
1570  statuses[i] = (int) exit_status;
1571  INSTR_TIME_SET_CURRENT(stoptimes[i]);
1572  if (names)
1573  status(" %s", names[i]);
1574  tests_left--;
1575  break;
1576  }
1577  }
1578  }
1579 
1580 #ifdef WIN32
1581  free(active_pids);
1582 #endif
1583 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
#define PID_TYPE
Definition: pg_regress.h:14
#define fprintf
Definition: port.h:197
#define INVALID_PID
Definition: pg_regress.h:15
#define free(a)
Definition: header.h:65
#define strerror
Definition: port.h:206
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:156
int i
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
#define _(x)
Definition: elog.c:88

Variable Documentation

◆ basic_diff_opts

const char* basic_diff_opts = ""

Definition at line 65 of file pg_regress.c.

Referenced by results_differ().

◆ bindir

char* bindir = PGBINDIR

Definition at line 77 of file pg_regress.c.

Referenced by psql_command(), psql_start_test(), regression_main(), and stop_postmaster().

◆ config_auth_datadir

char* config_auth_datadir = NULL
static

Definition at line 95 of file pg_regress.c.

Referenced by regression_main().

◆ dblist

◆ debug

bool debug = false

Definition at line 74 of file pg_regress.c.

Referenced by regression_main().

◆ difffilename

char* difffilename
static

Definition at line 101 of file pg_regress.c.

Referenced by open_result_files(), regression_main(), and results_differ().

◆ dlpath

char* dlpath = PKGLIBDIR
static

Definition at line 92 of file pg_regress.c.

Referenced by convert_sourcefiles_in(), and regression_main().

◆ encoding

char* encoding = NULL
static

Definition at line 82 of file pg_regress.c.

Referenced by create_database(), initialize_environment(), and regression_main().

◆ extra_tests

_stringlist* extra_tests = NULL
static

Definition at line 84 of file pg_regress.c.

◆ extraroles

_stringlist* extraroles = NULL
static

Definition at line 94 of file pg_regress.c.

◆ fail_count

int fail_count = 0
static

Definition at line 115 of file pg_regress.c.

Referenced by regression_main(), run_schedule(), and run_single_test().

◆ fail_ignore_count

int fail_ignore_count = 0
static

Definition at line 116 of file pg_regress.c.

Referenced by regression_main(), and run_schedule().

◆ host_platform

char* host_platform = HOST_TUPLE

Definition at line 53 of file pg_regress.c.

Referenced by load_resultmap().

◆ hostname

char* hostname = NULL
static

Definition at line 89 of file pg_regress.c.

Referenced by initialize_environment(), pqPacketSend(), and regression_main().

◆ inputdir

◆ launcher

char* launcher = NULL

Definition at line 78 of file pg_regress.c.

Referenced by isolation_start_test(), psql_start_test(), and regression_main().

◆ loadextension

_stringlist* loadextension = NULL
static

Definition at line 79 of file pg_regress.c.

◆ logfile

FILE* logfile
static

◆ logfilename

char* logfilename
static

Definition at line 99 of file pg_regress.c.

Referenced by open_result_files(), and regression_main().

◆ max_concurrent_tests

int max_concurrent_tests = 0
static

Definition at line 81 of file pg_regress.c.

Referenced by regression_main(), and run_schedule().

◆ max_connections

int max_connections = 0
static

Definition at line 80 of file pg_regress.c.

Referenced by regression_main(), and run_schedule().

◆ nolocale

bool nolocale = false
static

Definition at line 87 of file pg_regress.c.

Referenced by create_database(), initialize_environment(), and regression_main().

◆ outputdir

◆ port

◆ port_specified_by_user

bool port_specified_by_user = false
static

Definition at line 91 of file pg_regress.c.

Referenced by regression_main().

◆ postmaster_pid

PID_TYPE postmaster_pid = INVALID_PID
static

Definition at line 111 of file pg_regress.c.

Referenced by regression_main().

◆ postmaster_running

bool postmaster_running = false
static

Definition at line 112 of file pg_regress.c.

Referenced by regression_main(), and stop_postmaster().

◆ pretty_diff_opts

const char* pretty_diff_opts = "-U3"

Definition at line 66 of file pg_regress.c.

Referenced by regression_main(), and results_differ().

◆ progname

◆ resultmap

_resultmap* resultmap = NULL
static

Definition at line 109 of file pg_regress.c.

Referenced by load_resultmap().

◆ schedulelist

_stringlist* schedulelist = NULL
static

Definition at line 83 of file pg_regress.c.

◆ shellprog

char* shellprog = SHELLPROG
static

Definition at line 56 of file pg_regress.c.

Referenced by spawn_process().

◆ sockdir

const char* sockdir
static

Definition at line 102 of file pg_regress.c.

Referenced by initialize_environment(), and regression_main().

◆ success_count

int success_count = 0
static

Definition at line 114 of file pg_regress.c.

Referenced by regression_main(), run_schedule(), and run_single_test().

◆ temp_configs

_stringlist* temp_configs = NULL
static

Definition at line 86 of file pg_regress.c.

◆ temp_instance

char* temp_instance = NULL
static

Definition at line 85 of file pg_regress.c.

Referenced by initialize_environment(), regression_main(), and stop_postmaster().

◆ use_existing

bool use_existing = false
static

Definition at line 88 of file pg_regress.c.

Referenced by regression_main().

◆ user