attach test: always flush stdout

Otherwise, after a fork, both pids may be flushing.  When
redirecting output to a file, this causes duplicate (or really
far worse) output.

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
This commit is contained in:
Serge Hallyn 2014-01-27 05:05:15 -06:00 committed by Stéphane Graber
parent b56661fead
commit c59ce7de5e

View File

@ -29,8 +29,11 @@
#include <unistd.h> #include <unistd.h>
#define TSTNAME "lxc-attach-test" #define TSTNAME "lxc-attach-test"
#define TSTOUT(fmt, ...) do { \
fprintf(stdout, fmt, ##__VA_ARGS__); fflush(NULL); \
} while (0)
#define TSTERR(fmt, ...) do { \ #define TSTERR(fmt, ...) do { \
fprintf(stderr, "%s:%d " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__); \ fprintf(stderr, "%s:%d " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__); fflush(NULL); \
} while (0) } while (0)
static const char *lsm_config_key = NULL; static const char *lsm_config_key = NULL;
@ -64,7 +67,7 @@ static void test_attach_lsm_set_config(struct lxc_container *ct)
static int test_attach_lsm_func_func(void* payload) static int test_attach_lsm_func_func(void* payload)
{ {
printf("%s", lsm_process_label_get(getpid())); TSTOUT("%s", lsm_process_label_get(getpid()));
return 0; return 0;
} }
@ -76,7 +79,7 @@ static int test_attach_lsm_func(struct lxc_container *ct)
char result[1024]; char result[1024];
lxc_attach_options_t attach_options = LXC_ATTACH_OPTIONS_DEFAULT; lxc_attach_options_t attach_options = LXC_ATTACH_OPTIONS_DEFAULT;
printf("Testing attach lsm label with func...\n"); TSTOUT("Testing attach lsm label with func...\n");
ret = pipe(pipefd); ret = pipe(pipefd);
if (ret < 0) { if (ret < 0) {
@ -125,7 +128,7 @@ static int test_attach_lsm_cmd(struct lxc_container *ct)
lxc_attach_command_t command = {"cat", argv}; lxc_attach_command_t command = {"cat", argv};
lxc_attach_options_t attach_options = LXC_ATTACH_OPTIONS_DEFAULT; lxc_attach_options_t attach_options = LXC_ATTACH_OPTIONS_DEFAULT;
printf("Testing attach lsm label with cmd...\n"); TSTOUT("Testing attach lsm label with cmd...\n");
ret = pipe(pipefd); ret = pipe(pipefd);
if (ret < 0) { if (ret < 0) {
@ -175,7 +178,7 @@ static int test_attach_lsm_cmd(struct lxc_container *ct) { return 0; }
static int test_attach_func_func(void* payload) static int test_attach_func_func(void* payload)
{ {
printf("%d", getpid()); TSTOUT("%d", getpid());
return 0; return 0;
} }
@ -187,7 +190,7 @@ static int test_attach_func(struct lxc_container *ct)
char result[1024]; char result[1024];
lxc_attach_options_t attach_options = LXC_ATTACH_OPTIONS_DEFAULT; lxc_attach_options_t attach_options = LXC_ATTACH_OPTIONS_DEFAULT;
printf("Testing attach with func...\n"); TSTOUT("Testing attach with func...\n");
/* XXX: We can't just use &nspid and have test_attach_func_func fill /* XXX: We can't just use &nspid and have test_attach_func_func fill
* it in because the function doesn't run in our process context but * it in because the function doesn't run in our process context but
@ -219,7 +222,7 @@ static int test_attach_func(struct lxc_container *ct)
* if (pid == nspid) TSTERR(...) * if (pid == nspid) TSTERR(...)
*/ */
nspid = atoi(result); nspid = atoi(result);
printf("Pid:%d in NS:%d\n", pid, nspid); TSTOUT("Pid:%d in NS:%d\n", pid, nspid);
ret = 0; ret = 0;
err2: err2:
@ -238,7 +241,7 @@ static int test_attach_cmd(struct lxc_container *ct)
lxc_attach_command_t command = {"cmp", argv}; lxc_attach_command_t command = {"cmp", argv};
lxc_attach_options_t attach_options = LXC_ATTACH_OPTIONS_DEFAULT; lxc_attach_options_t attach_options = LXC_ATTACH_OPTIONS_DEFAULT;
printf("Testing attach with success command...\n"); TSTOUT("Testing attach with success command...\n");
ret = ct->attach(ct, lxc_attach_run_command, &command, &attach_options, &pid); ret = ct->attach(ct, lxc_attach_run_command, &command, &attach_options, &pid);
if (ret < 0) { if (ret < 0) {
TSTERR("attach failed"); TSTERR("attach failed");
@ -251,7 +254,7 @@ static int test_attach_cmd(struct lxc_container *ct)
return ret; return ret;
} }
printf("Testing attach with failure command...\n"); TSTOUT("Testing attach with failure command...\n");
argv[2] = "/etc/fstab"; argv[2] = "/etc/fstab";
ret = ct->attach(ct, lxc_attach_run_command, &command, &attach_options, &pid); ret = ct->attach(ct, lxc_attach_run_command, &command, &attach_options, &pid);
if (ret < 0) { if (ret < 0) {
@ -337,7 +340,7 @@ static int test_attach(const char *lxcpath, const char *name, const char *templa
int ret = -1; int ret = -1;
struct lxc_container *ct; struct lxc_container *ct;
printf("Testing attach with on lxcpath:%s\n", lxcpath ? lxcpath : "<default>"); TSTOUT("Testing attach with on lxcpath:%s\n", lxcpath ? lxcpath : "<default>");
ct = test_ct_create(lxcpath, NULL, name, template); ct = test_ct_create(lxcpath, NULL, name, template);
if (!ct) if (!ct)
goto err1; goto err1;
@ -384,11 +387,11 @@ int main(int argc, char *argv[])
if (ret < 0) if (ret < 0)
return EXIT_FAILURE; return EXIT_FAILURE;
printf("\n"); TSTOUT("\n");
ret = test_attach(LXCPATH "/alternate-path-test", TSTNAME, "busybox"); ret = test_attach(LXCPATH "/alternate-path-test", TSTNAME, "busybox");
if (ret < 0) if (ret < 0)
return EXIT_FAILURE; return EXIT_FAILURE;
printf("All tests passed\n"); TSTOUT("All tests passed\n");
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }