/** * check_for_overlayfs: * * Determine if the mount point used by the tests for creating temporary * files is using overlayfs. * * Returns: TRUE if temporary work area is on overlayfs, else FALSE. **/ int check_for_overlayfs (void) { struct statfs statbuf; char path[PATH_MAX]; int found = FALSE; /* Create a file in the temporary work area */ TEST_FILENAME (path); fclose (fopen (path, "w")); /* Check it exits */ assert0 (statfs (path, &statbuf)); if (statbuf.f_type == OVERLAYFS_SUPER_MAGIC) { nih_warn ("Mountpoint for '%s' (needed by the Upstart tests) is an overlayfs " "filesystem, which does not support inotify.", path); found = TRUE; } assert0 (unlink (path)); return found; }
void test_read_pidfile (void) { FILE *f; char filename[PATH_MAX]; TEST_FUNCTION ("nih_main_read_pidfile"); TEST_FILENAME (filename); nih_main_set_pidfile (filename); /* Check that reading from a valid pid file will return the pid * stored there. */ TEST_FEATURE ("with valid pid file"); f = fopen (filename, "w"); fprintf (f, "1234\n"); fclose (f); TEST_EQ (nih_main_read_pidfile (), 1234); /* Check that reading from a pid file without a newline will still * return the pid stored there. */ TEST_FEATURE ("with no newline in pid file"); f = fopen (filename, "w"); fprintf (f, "1234"); fclose (f); TEST_EQ (nih_main_read_pidfile (), 1234); /* Check that reading from an invalid pid file returns -1. */ TEST_FEATURE ("with invalid pid file"); f = fopen (filename, "w"); fprintf (f, "foo\n1234\n"); fclose (f); TEST_EQ (nih_main_read_pidfile (), -1); /* Check that reading from a non-existant pid file returns -1. */ TEST_FEATURE ("with non-existant pid file"); nih_main_unlink_pidfile (); TEST_EQ (nih_main_read_pidfile (), -1); nih_main_set_pidfile (NULL); }
void test_libupstart (void) { nih_local NihDBusProxy *upstart = NULL; nih_local char *version = NULL; int ret; pid_t upstart_pid; pid_t dbus_pid; char xdg_runtime_dir[PATH_MAX]; nih_local char *orig_xdg_runtime_dir = NULL; nih_local char *session_file = NULL; nih_local char *path = NULL; TEST_GROUP ("libupstart"); TEST_FEATURE ("version"); TEST_FILENAME (xdg_runtime_dir); TEST_EQ (mkdir (xdg_runtime_dir, 0755), 0); /* Take care to avoid disrupting users environment by saving and * restoring this variable (assuming the tests all pass...). */ orig_xdg_runtime_dir = getenv ("XDG_RUNTIME_DIR"); if (orig_xdg_runtime_dir) orig_xdg_runtime_dir = NIH_MUST (nih_strdup (NULL, orig_xdg_runtime_dir)); assert0 (setenv ("XDG_RUNTIME_DIR", xdg_runtime_dir, 1)); /*******************************************************************/ /* Create a private Session Init instance to connect to */ TEST_DBUS (dbus_pid); START_UPSTART (upstart_pid, TRUE); upstart = upstart_open (NULL); TEST_NE_P (upstart, NULL); /* Basic test (that does not change the state of the system * running this test) to see if we can query version of running * Upstart instance. */ ret = upstart_get_version_sync (NULL, upstart, &version); TEST_EQ (ret, 0); nih_message ("Running instance version: '%s'", version); assert0 (fnmatch ("init (upstart*)", version, 0x0)); STOP_UPSTART (upstart_pid); TEST_DBUS_END (dbus_pid); /*******************************************************************/ if (orig_xdg_runtime_dir) { /* restore */ setenv ("XDG_RUNTIME_DIR", orig_xdg_runtime_dir, 1); } else { assert0 (unsetenv ("XDG_RUNTIME_DIR")); } session_file = get_session_file (xdg_runtime_dir, upstart_pid); unlink (session_file); /* Remove the directory tree the Session Init created */ path = NIH_MUST (nih_sprintf (NULL, "%s/upstart/sessions", xdg_runtime_dir)); assert0 (rmdir (path)); path = NIH_MUST (nih_sprintf (NULL, "%s/upstart", xdg_runtime_dir)); assert0 (rmdir (path)); assert0 (rmdir (xdg_runtime_dir)); }
void test_cgroup_job_start (void) { char confdir[PATH_MAX]; char logdir[PATH_MAX]; char flagfile[PATH_MAX]; nih_local char *cmd = NULL; pid_t dbus_pid = 0; pid_t upstart_pid = 0; char **output; size_t lines; size_t len; nih_local char *logfile = NULL; nih_local char *logfile_name = NULL; nih_local char *contents = NULL; if (geteuid ()) { printf ("INFO: skipping %s tests as not running as root\n", __func__); fflush (NULL); return; } TEST_GROUP ("cgroup manager handling"); TEST_FILENAME (confdir); TEST_EQ (mkdir (confdir, 0755), 0); TEST_FILENAME (logdir); TEST_EQ (mkdir (logdir, 0755), 0); TEST_FILENAME (flagfile); /* Use the "secret" interface */ TEST_EQ (setenv ("UPSTART_CONFDIR", confdir, 1), 0); TEST_EQ (setenv ("UPSTART_LOGDIR", logdir, 1), 0); TEST_DBUS (dbus_pid); /*******************************************************************/ TEST_FEATURE ("Ensure startup job does not start until cgmanager available"); contents = nih_sprintf (NULL, "start on startup\n" "\n" "cgroup memory mem-%s\n" "\n" "exec echo hello\n", __func__); TEST_NE_P (contents, NULL); CREATE_FILE (confdir, "cgroup.conf", contents); logfile_name = NIH_MUST (nih_sprintf (NULL, "%s/%s", logdir, "cgroup.log")); start_upstart_common (&upstart_pid, FALSE, FALSE, confdir, logdir, NULL); cmd = nih_sprintf (NULL, "%s status %s 2>&1", get_initctl (), "cgroup"); TEST_NE_P (cmd, NULL); RUN_COMMAND (NULL, cmd, &output, &lines); TEST_EQ (lines, 1); /* job should *NOT* start on startup */ TEST_EQ_STR (output[0], "cgroup stop/waiting"); nih_free (output); TEST_FALSE (file_exists (logfile_name)); cmd = nih_sprintf (NULL, "%s notify-cgroup-manager-address %s 2>&1", get_initctl (), CGMANAGER_DBUS_SOCK); TEST_NE_P (cmd, NULL); RUN_COMMAND (NULL, cmd, &output, &lines); TEST_EQ (lines, 0); WAIT_FOR_FILE (logfile_name); logfile = nih_file_read (NULL, logfile_name, &len); TEST_NE_P (logfile, NULL); TEST_EQ_STR (logfile, "hello\r\n"); DELETE_FILE (confdir, "cgroup.conf"); assert0 (unlink (logfile_name)); /*******************************************************************/ TEST_FEATURE ("Ensure bogus cgroups don't crash init"); contents = nih_sprintf (NULL, "cgroup name\n" "\n" "exec echo hello\n"); TEST_NE_P (contents, NULL); CREATE_FILE (confdir, "cgroup-name.conf", contents); logfile_name = NIH_MUST (nih_sprintf (NULL, "%s/%s", logdir, "cgroup-name.log")); cmd = nih_sprintf (NULL, "%s status %s 2>&1", get_initctl (), "cgroup-name"); TEST_NE_P (cmd, NULL); RUN_COMMAND (NULL, cmd, &output, &lines); TEST_EQ (lines, 1); /* job is not running yet */ TEST_EQ_STR (output[0], "cgroup-name stop/waiting"); nih_free (output); TEST_FALSE (file_exists (logfile_name)); cmd = nih_sprintf (NULL, "%s start %s 2>&1", get_initctl (), "cgroup-name"); TEST_NE_P (cmd, NULL); RUN_COMMAND (NULL, cmd, &output, &lines); TEST_EQ (lines, 1); TEST_EQ_STR (output[0], "initctl: Job failed to start"); DELETE_FILE (confdir, "cgroup-name.conf"); /*******************************************************************/ STOP_UPSTART (upstart_pid); TEST_DBUS_END (dbus_pid); TEST_EQ (rmdir (confdir), 0); TEST_EQ (rmdir (logdir), 0); /*******************************************************************/ }
void test_write_pidfile (void) { FILE *f; NihError *err; char dirname[PATH_MAX], filename[PATH_MAX], tmpname[PATH_MAX]; int ret; TEST_FUNCTION ("nih_main_write_pidfile"); TEST_FILENAME (dirname); mkdir (dirname, 0755); strcpy (filename, dirname); strcat (filename, "/test.pid"); strcpy (tmpname, dirname); strcat (tmpname, "/.test.pid.tmp"); nih_main_set_pidfile (filename); /* Check that we can write a pid to the file, and have it appaer * on disk where we expect. */ TEST_FEATURE ("with successful write"); ret = nih_main_write_pidfile (1234); TEST_EQ (ret, 0); f = fopen (filename, "r"); TEST_FILE_EQ (f, "1234\n"); fclose (f); /* Check that we can overwrite an existing pid file with a new * value. */ TEST_FEATURE ("with overwrite of existing pid"); ret = nih_main_write_pidfile (5678); TEST_EQ (ret, 0); f = fopen (filename, "r"); TEST_FILE_EQ (f, "5678\n"); fclose (f); /* Check that an error writing to the temporary file does not result * in the replacement of the existing file and does not result in * the unlinking of the temporary file. */ TEST_FEATURE ("with failure to write to temporary file"); f = fopen (tmpname, "w"); fclose (f); chmod (tmpname, 0000); ret = nih_main_write_pidfile (1234); TEST_LT (ret, 0); err = nih_error_get (); TEST_EQ (err->number, EACCES); nih_free (err); f = fopen (filename, "r"); TEST_FILE_EQ (f, "5678\n"); fclose (f); TEST_EQ (chmod (tmpname, 0644), 0); nih_main_unlink_pidfile (); unlink (tmpname); rmdir (dirname); nih_main_set_pidfile (NULL); }