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 _stringlistloadlanguage = 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 156 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().

157 {
158  _stringlist *newentry = pg_malloc(sizeof(_stringlist));
159  _stringlist *oldentry;
160 
161  newentry->str = pg_strdup(str);
162  newentry->next = NULL;
163  if (*listhead == NULL)
164  *listhead = newentry;
165  else
166  {
167  for (oldentry = *listhead; oldentry->next; oldentry = oldentry->next)
168  /* skip */ ;
169  oldentry->next = newentry;
170  }
171 }
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(), S_ISDIR, snprintf, stat, and strerror.

Referenced by convert_sourcefiles().

465 {
466  char testtablespace[MAXPGPATH];
467  char indir[MAXPGPATH];
468  struct stat st;
469  int ret;
470  char **name;
471  char **names;
472  int count = 0;
473 
474  snprintf(indir, MAXPGPATH, "%s/%s", inputdir, source_subdir);
475 
476  /* Check that indir actually exists and is a directory */
477  ret = stat(indir, &st);
478  if (ret != 0 || !S_ISDIR(st.st_mode))
479  {
480  /*
481  * No warning, to avoid noise in tests that do not have these
482  * directories; for example, ecpg, contrib and src/pl.
483  */
484  return;
485  }
486 
487  names = pgfnames(indir);
488  if (!names)
489  /* Error logged in pgfnames */
490  exit(2);
491 
492  snprintf(testtablespace, MAXPGPATH, "%s/testtablespace", outputdir);
493 
494 #ifdef WIN32
495 
496  /*
497  * On Windows only, clean out the test tablespace dir, or create it if it
498  * doesn't exist. On other platforms we expect the Makefile to take care
499  * of that. (We don't migrate that functionality in here because it'd be
500  * harder to cope with platform-specific issues such as SELinux.)
501  *
502  * XXX it would be better if pg_regress.c had nothing at all to do with
503  * testtablespace, and this were handled by a .BAT file or similar on
504  * Windows. See pgsql-hackers discussion of 2008-01-18.
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:99
static void make_directory(const char *dir)
Definition: pg_regress.c:1286
#define fprintf
Definition: port.h:196
#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:93
#define stat(a, b)
Definition: win32_port.h:255
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:1233
void pgfnames_cleanup(char **filenames)
Definition: pgfnames.c:86
#define strerror
Definition: port.h:205
const char * name
Definition: encode.c:521
#define S_ISDIR(m)
Definition: win32_port.h:296
static char * outfile
#define snprintf
Definition: port.h:192
#define _(x)
Definition: elog.c:87

◆ 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 procedural languages. We use CREATE OR REPLACE
2002  * so that this will work whether or not the language is preinstalled.
2003  */
2004  for (sl = loadlanguage; sl != NULL; sl = sl->next)
2005  {
2006  header(_("installing %s"), sl->str);
2007  psql_command(dbname, "CREATE OR REPLACE LANGUAGE \"%s\"", sl->str);
2008  }
2009 
2010  /*
2011  * Install any requested extensions. We use CREATE IF NOT EXISTS so that
2012  * this will work whether or not the extension is preinstalled.
2013  */
2014  for (sl = loadextension; sl != NULL; sl = sl->next)
2015  {
2016  header(_("installing %s"), sl->str);
2017  psql_command(dbname, "CREATE EXTENSION IF NOT EXISTS \"%s\"", sl->str);
2018  }
2019 }
char * str
Definition: pg_regress.h:24
static char * encoding
Definition: pg_regress.c:83
static _stringlist * loadlanguage
Definition: pg_regress.c:79
static _stringlist * loadextension
Definition: pg_regress.c:80
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:209
struct _stringlist * next
Definition: pg_regress.h:25
static bool nolocale
Definition: pg_regress.c:88
#define _(x)
Definition: elog.c:87

◆ create_role()

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

Definition at line 2029 of file pg_regress.c.

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

Referenced by regression_main().

2030 {
2031  header(_("creating role \"%s\""), rolename);
2032  psql_command("postgres", "CREATE ROLE \"%s\" WITH LOGIN", rolename);
2033  for (; granted_dbs != NULL; granted_dbs = granted_dbs->next)
2034  {
2035  psql_command("postgres", "GRANT ALL ON DATABASE \"%s\" TO \"%s\"",
2036  granted_dbs->str, rolename);
2037  }
2038 }
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:209
struct _stringlist * next
Definition: pg_regress.h:25
#define _(x)
Definition: elog.c:87

◆ 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:684

◆ 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:209
#define _(x)
Definition: elog.c:87

◆ drop_role_if_exists()

static void drop_role_if_exists ( const char *  rolename)
static

Definition at line 2022 of file pg_regress.c.

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

Referenced by regression_main().

2023 {
2024  header(_("dropping role \"%s\""), rolename);
2025  psql_command("postgres", "DROP ROLE IF EXISTS \"%s\"", rolename);
2026 }
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:209
#define _(x)
Definition: elog.c:87

◆ 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:99
#define fprintf
Definition: port.h:196
char * c
#define strerror
Definition: port.h:205
#define _(x)
Definition: elog.c:87

◆ 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:99
#define fprintf
Definition: port.h:196
#define strerror
Definition: port.h:205
#define _(x)
Definition: elog.c:87

◆ free_stringlist()

static void free_stringlist ( _stringlist **  listhead)
static

Definition at line 177 of file pg_regress.c.

References free.

Referenced by regression_main(), and run_schedule().

178 {
179  if (listhead == NULL || *listhead == NULL)
180  return;
181  if ((*listhead)->next != NULL)
182  free_stringlist(&((*listhead)->next));
183  free((*listhead)->str);
184  free(*listhead);
185  *listhead = NULL;
186 }
static void free_stringlist(_stringlist **listhead)
Definition: pg_regress.c:177
#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:192

◆ 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:110
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 209 of file pg_regress.c.

References fprintf, generate_unaccent_rules::stdout, and vsnprintf.

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

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

◆ help()

static void help ( void  )
static

Definition at line 2041 of file pg_regress.c.

References _, printf, and progname.

Referenced by regression_main().

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

◆ 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:103
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static char * encoding
Definition: pg_regress.c:83
#define printf(...)
Definition: port.h:198
static char * temp_instance
Definition: pg_regress.c:86
static void convert_sourcefiles(void)
Definition: pg_regress.c:583
#define sprintf
Definition: port.h:194
static int port
Definition: pg_regress.c:91
char * pghost
Definition: pgbench.c:242
#define Assert(condition)
Definition: c.h:733
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:94
static bool nolocale
Definition: pg_regress.c:88
static char * hostname
Definition: pg_regress.c:90
#define _(x)
Definition: elog.c:87
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:99
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:196
static _resultmap * resultmap
Definition: pg_regress.c:110
#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:205
char * resultfile
Definition: pg_regress.c:46
int i
#define snprintf
Definition: port.h:192
#define _(x)
Definition: elog.c:87

◆ 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:141
#define WIFEXITED(w)
Definition: win32_port.h:138
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:226
#define WIFSIGNALED(w)
Definition: win32_port.h:139
#define _(x)
Definition: elog.c:87
#define WEXITSTATUS(w)
Definition: win32_port.h:140

◆ 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:99
#define fprintf
Definition: port.h:196
#define S_IRWXG
Definition: win32_port.h:281
#define strerror
Definition: port.h:205
#define S_IRWXU
Definition: win32_port.h:269
#define mkdir(a, b)
Definition: win32_port.h:58
#define _(x)
Definition: elog.c:87
#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:99
static void make_directory(const char *dir)
Definition: pg_regress.c:1286
static FILE * logfile
Definition: pg_regress.c:101
#define fprintf
Definition: port.h:196
static char * difffilename
Definition: pg_regress.c:102
#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:100
#define strerror
Definition: port.h:205
#define snprintf
Definition: port.h:192
#define _(x)
Definition: elog.c:87

◆ 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:196
#define vsnprintf
Definition: port.h:191
#define MAXPGPATH
char * bindir
Definition: pg_regress.c:77
#define snprintf
Definition: port.h:192
#define _(x)
Definition: elog.c:87

◆ regression_main()

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

Definition at line 2091 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, ULONGPID, use_existing, user, and wait_seconds.

Referenced by main().

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

◆ 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:196
#define WIFEXITED(w)
Definition: win32_port.h:138
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:87
#define WEXITSTATUS(w)
Definition: win32_port.h:140

◆ 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:82
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:156
#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:99
static void status_end(void)
Definition: pg_regress.c:247
#define PID_TYPE
Definition: pg_regress.h:14
#define printf(...)
Definition: port.h:198
#define fprintf
Definition: port.h:196
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:177
#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:115
static int max_connections
Definition: pg_regress.c:81
static int fail_ignore_count
Definition: pg_regress.c:117
static int fail_count
Definition: pg_regress.c:116
#define strerror
Definition: port.h:205
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:226
#define _(x)
Definition: elog.c:87
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:247
#define PID_TYPE
Definition: pg_regress.h:14
#define printf(...)
Definition: port.h:198
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:115
static int fail_count
Definition: pg_regress.c:116
#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:226
#define _(x)
Definition: elog.c:87
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:99
static FILE * logfile
Definition: pg_regress.c:101
#define fprintf
Definition: port.h:196
#define strerror
Definition: port.h:205
#define _(x)
Definition: elog.c:87

◆ split_to_stringlist()

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

Definition at line 192 of file pg_regress.c.

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

Referenced by regression_main().

193 {
194  char *sc = pg_strdup(s);
195  char *token = strtok(sc, delim);
196 
197  while (token)
198  {
199  add_stringlist_item(listhead, token);
200  token = strtok(NULL, delim);
201  }
202  free(sc);
203 }
static void static void static void void add_stringlist_item(_stringlist **listhead, const char *str)
Definition: pg_regress.c:156
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 226 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_master_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(), LockAcquireExtended(), LockReassignCurrentOwner(), LockReleaseAll(), LockReleaseCurrentOwner(), LockReleaseSession(), log_child_failure(), 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(), pgrowlocks(), PortalErrorCleanup(), PortalHashTableDeleteAll(), PostmasterMain(), PostPrepare_Locks(), pqFunctionCall2(), pqFunctionCall3(), pqGetline3(), PQmakeEmptyPGresult(), PQresetPoll(), PQresStatus(), PQsetClientEncoding(), PreCommit_Portals(), RecordNewMultiXact(), rel_sync_cache_publication_cb(), RelationCacheInitializePhase3(), RelationCacheInvalidate(), RelfilenodeMapInvalidateCallback(), 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(), txid_status(), TypeCacheOpcCallback(), TypeCacheRelCallback(), 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().

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

◆ status_end()

static void status_end ( void  )
static

Definition at line 247 of file pg_regress.c.

References fprintf, logfile, and generate_unaccent_rules::stdout.

Referenced by run_schedule(), and run_single_test().

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

◆ stop_postmaster()

static void stop_postmaster ( void  )
static

Definition at line 259 of file pg_regress.c.

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

Referenced by regression_main().

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

◆ 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:196
#define INVALID_PID
Definition: pg_regress.h:15
#define free(a)
Definition: header.h:65
#define strerror
Definition: port.h:205
#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:226
#define _(x)
Definition: elog.c:87

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 96 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 102 of file pg_regress.c.

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

◆ dlpath

char* dlpath = PKGLIBDIR
static

Definition at line 93 of file pg_regress.c.

Referenced by convert_sourcefiles_in(), and regression_main().

◆ encoding

char* encoding = NULL
static

Definition at line 83 of file pg_regress.c.

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

◆ extra_tests

_stringlist* extra_tests = NULL
static

Definition at line 85 of file pg_regress.c.

◆ extraroles

_stringlist* extraroles = NULL
static

Definition at line 95 of file pg_regress.c.

◆ fail_count

int fail_count = 0
static

Definition at line 116 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 117 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 90 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 80 of file pg_regress.c.

◆ loadlanguage

_stringlist* loadlanguage = NULL
static

Definition at line 79 of file pg_regress.c.

◆ logfile

FILE* logfile
static

◆ logfilename

char* logfilename
static

Definition at line 100 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 82 of file pg_regress.c.

Referenced by regression_main(), and run_schedule().

◆ max_connections

int max_connections = 0
static

Definition at line 81 of file pg_regress.c.

Referenced by regression_main(), and run_schedule().

◆ nolocale

bool nolocale = false
static

Definition at line 88 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 92 of file pg_regress.c.

Referenced by regression_main().

◆ postmaster_pid

PID_TYPE postmaster_pid = INVALID_PID
static

Definition at line 112 of file pg_regress.c.

Referenced by regression_main().

◆ postmaster_running

bool postmaster_running = false
static

Definition at line 113 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 110 of file pg_regress.c.

Referenced by load_resultmap().

◆ schedulelist

_stringlist* schedulelist = NULL
static

Definition at line 84 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 103 of file pg_regress.c.

Referenced by initialize_environment(), and regression_main().

◆ success_count

int success_count = 0
static

Definition at line 115 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 87 of file pg_regress.c.

◆ temp_instance

char* temp_instance = NULL
static

Definition at line 86 of file pg_regress.c.

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

◆ use_existing

bool use_existing = false
static

Definition at line 89 of file pg_regress.c.

Referenced by regression_main().

◆ user