Unverified Commit 9b937486 by Christian Brauner Committed by GitHub

Merge pull request #2445 from 2xsec/bugfix

tests: cleanup source codes.
parents 5221c891 6ea8a436
......@@ -59,6 +59,7 @@ int main(int argc, char *argv[])
lxc_error("%s\n", "Container \"reboot\" is not defined");
goto on_error_put;
}
c->clear_config(c);
if (!c->load_config(c, NULL)) {
......@@ -118,7 +119,9 @@ on_error_stop:
on_error_put:
lxc_container_put(c);
if (ret == EXIT_SUCCESS)
lxc_debug("%s\n", "All reboot tests passed");
exit(ret);
}
......@@ -41,6 +41,7 @@ int main(int argc, char *argv[])
lxc_container_put(c);
c = NULL;
}
if (c2) {
c2->destroy(c2);
lxc_container_put(c2);
......@@ -52,11 +53,14 @@ int main(int argc, char *argv[])
ret = 1;
goto out;
}
c->save_config(c, NULL);
if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) {
fprintf(stderr, "%d: failed to create a container\n", __LINE__);
goto out;
}
c->load_config(c, NULL);
if (!c->is_defined(c)) {
......@@ -92,12 +96,14 @@ int main(int argc, char *argv[])
c2->destroy(c2);
lxc_container_put(c2);
}
c2 = lxc_container_new("clonetest-o1", NULL);
if (c2) {
if (c2->is_defined(c2))
c2->destroy(c2);
lxc_container_put(c2);
}
c2 = lxc_container_new("clonetest-o2", NULL);
if (c2) {
if (c2->is_defined(c2))
......@@ -112,6 +118,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "failed loading clonetestlvm1\n");
goto out;
}
if (!c->is_defined(c)) {
fprintf(stderr, "clonetestlvm1 does not exist, skipping lvm tests\n");
ret = 0;
......@@ -138,6 +145,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "lvm clone failed\n");
goto out;
}
lxc_container_put(c2);
lxc_container_put(c);
c = c2 = NULL;
......@@ -166,13 +174,16 @@ out:
if (c3) {
lxc_container_put(c3);
}
if (c2) {
c2->destroy(c2);
lxc_container_put(c2);
}
if (c) {
c->destroy(c);
lxc_container_put(c);
}
exit(ret);
}
......@@ -54,6 +54,7 @@ int main(int argc, char *argv[])
for (key = strtok_r(keys, "\n", &saveptr); key != NULL;
key = strtok_r(NULL, "\n", &saveptr)) {
struct lxc_config_t *config;
config = lxc_get_config(key);
if (!config) {
lxc_error("configuration key \"%s\" not implemented in "
......@@ -90,5 +91,4 @@ on_error:
free(keys);
exit(ret);
}
......@@ -46,6 +46,7 @@ static void test_console_close_all(int ttyfd[MAXCONSOLES],
close(masterfd[i]);
masterfd[i] = -1;
}
if (ttyfd[i] != -1) {
close(ttyfd[i]);
ttyfd[i] = -1;
......@@ -64,11 +65,13 @@ static int test_console_running_container(struct lxc_container *c)
ttynum[i] = ttyfd[i] = masterfd[i] = -1;
ttynum[0] = 1;
ret = c->console_getfd(c, &ttynum[0], &masterfd[0]);
if (ret < 0) {
TSTERR("console allocate failed");
goto err1;
}
ttyfd[0] = ret;
if (ttynum[0] != 1) {
TSTERR("console allocate got bad ttynum %d", ttynum[0]);
......@@ -94,16 +97,20 @@ static int test_console_running_container(struct lxc_container *c)
break;
ttyfd[nrconsoles] = ret;
}
if (nrconsoles != TTYCNT) {
TSTERR("didn't allocate all consoles %d != %d", nrconsoles, TTYCNT);
goto err2;
}
test_console_close_all(ttyfd, masterfd);
}
ret = 0;
err2:
test_console_close_all(ttyfd, masterfd);
err1:
return ret;
}
......@@ -135,20 +142,24 @@ static int test_console(const char *lxcpath,
TSTERR("instantiating container %s", name);
goto out1;
}
if (c->is_defined(c)) {
c->stop(c);
c->destroy(c);
c = lxc_container_new(name, lxcpath);
}
if (!c->createl(c, template, NULL, NULL, 0, NULL)) {
TSTERR("creating container %s", name);
goto out2;
}
c->load_config(c, NULL);
c->set_config_item(c, "lxc.tty.max", TTYCNT_STR);
c->set_config_item(c, "lxc.pty.max", "1024");
c->save_config(c, NULL);
c->want_daemonize(c, true);
if (!c->startl(c, 0, NULL)) {
TSTERR("starting container %s", name);
goto out3;
......@@ -157,10 +168,13 @@ static int test_console(const char *lxcpath,
ret = test_console_running_container(c);
c->stop(c);
out3:
c->destroy(c);
out2:
lxc_container_put(c);
out1:
return ret;
}
......@@ -168,6 +182,7 @@ out1:
int main(int argc, char *argv[])
{
int ret;
ret = test_console(NULL, NULL, TSTNAME, "busybox");
if (ret < 0)
goto err1;
......@@ -175,7 +190,9 @@ int main(int argc, char *argv[])
ret = test_console("/var/lib/lxctest2", NULL, TSTNAME, "busybox");
if (ret < 0)
goto err1;
printf("All tests passed\n");
err1:
return ret;
}
......@@ -39,10 +39,12 @@ static int destroy_busybox(void)
perror("fork");
return -1;
}
if (pid == 0) {
execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME, NULL);
exit(EXIT_FAILURE);
}
again:
ret = waitpid(pid, &status, 0);
if (ret == -1) {
......@@ -51,12 +53,15 @@ again:
perror("waitpid");
return -1;
}
if (ret != pid)
goto again;
if (!WIFEXITED(status)) { // did not exit normally
fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
return -1;
}
return WEXITSTATUS(status);
}
......@@ -69,10 +74,12 @@ static int create_busybox(void)
perror("fork");
return -1;
}
if (pid == 0) {
execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME, NULL);
exit(EXIT_FAILURE);
}
again:
ret = waitpid(pid, &status, 0);
if (ret == -1) {
......@@ -81,12 +88,15 @@ again:
perror("waitpid");
return -1;
}
if (ret != pid)
goto again;
if (!WIFEXITED(status)) { // did not exit normally
fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
return -1;
}
return WEXITSTATUS(status);
}
......@@ -99,25 +109,30 @@ int main(int argc, char *argv[])
char *str;
ret = 1;
/* test refcounting */
c = lxc_container_new(MYNAME, NULL);
if (!c) {
fprintf(stderr, "%d: error creating lxc_container %s\n", __LINE__, MYNAME);
goto out;
}
if (!lxc_container_get(c)) {
fprintf(stderr, "%d: error getting refcount\n", __LINE__);
goto out;
}
/* peek in, inappropriately, make sure refcount is a we'd like */
if (c->numthreads != 2) {
fprintf(stderr, "%d: refcount is %d, not %d\n", __LINE__, c->numthreads, 2);
goto out;
}
if (strcmp(c->name, MYNAME) != 0) {
fprintf(stderr, "%d: container has wrong name (%s not %s)\n", __LINE__, c->name, MYNAME);
goto out;
}
str = c->config_file_name(c);
#define CONFIGFNAM LXCPATH "/" MYNAME "/config"
if (!str || strcmp(str, CONFIGFNAM)) {
......@@ -127,19 +142,23 @@ int main(int argc, char *argv[])
free(str);
free(c->configfile);
c->configfile = NULL;
str = c->config_file_name(c);
if (str) {
fprintf(stderr, "%d: config file name was not NULL as it should have been\n", __LINE__);
goto out;
}
if (lxc_container_put(c) != 0) {
fprintf(stderr, "%d: c was freed on non-final put\n", __LINE__);
goto out;
}
if (c->numthreads != 1) {
fprintf(stderr, "%d: refcount is %d, not %d\n", __LINE__, c->numthreads, 1);
goto out;
}
if (lxc_container_put(c) != 1) {
fprintf(stderr, "%d: c was not freed on final put\n", __LINE__);
goto out;
......@@ -202,9 +221,11 @@ int main(int argc, char *argv[])
fprintf(stderr, "%d: lxc_get_wait_states gave %d not %d\n", __LINE__, numstates, MAX_STATE);
goto out;
}
const char **sstr = malloc(numstates * sizeof(const char *));
numstates = lxc_get_wait_states(sstr);
int i;
for (i=0; i<numstates; i++) {
fprintf(stderr, "got state %d %s\n", i, sstr[i]);
}
......@@ -237,6 +258,7 @@ out:
c->stop(c);
destroy_busybox();
}
lxc_container_put(c);
exit(ret);
}
......@@ -48,8 +48,10 @@ int main(int argc, char *argv[])
fprintf(stderr, "%d: failed to set network type\n", __LINE__);
goto out;
}
c->set_config_item(c, "lxc.net.0.link", "lxcbr0");
c->set_config_item(c, "lxc.net.0.flags", "up");
if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) {
fprintf(stderr, "%d: failed to create a trusty container\n", __LINE__);
goto out;
......@@ -63,6 +65,7 @@ int main(int argc, char *argv[])
c->clear_config(c);
c->load_config(c, NULL);
c->want_daemonize(c, true);
if (!c->startl(c, 0, NULL)) {
fprintf(stderr, "%d: failed to start %s\n", __LINE__, MYNAME);
goto out;
......@@ -85,6 +88,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "all lxc_container tests passed for %s\n", c->name);
ret = 0;
out:
lxc_container_put(c);
exit(ret);
......
......@@ -86,5 +86,6 @@ on_error_put:
lxc_container_put(c);
if (ret == EXIT_SUCCESS)
lxc_debug("%s\n", "All criu feature check tests passed");
exit(ret);
}
......@@ -37,24 +37,30 @@ static int create_container(void)
perror("fork");
return -1;
}
if (pid == 0) {
execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME, NULL);
exit(EXIT_FAILURE);
}
again:
ret = waitpid(pid, &status, 0);
if (ret == -1) {
if (errno == EINTR)
goto again;
perror("waitpid");
return -1;
}
if (ret != pid)
goto again;
if (!WIFEXITED(status)) { // did not exit normally
fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
return -1;
}
return WEXITSTATUS(status);
}
......@@ -96,6 +102,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "all lxc_container tests passed for %s\n", c->name);
ret = 0;
out:
lxc_container_put(c);
exit(ret);
......
......@@ -50,6 +50,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "Adding %s to the container (%s) failed...\n", DEVICE, NAME);
goto out;
}
if (!c->remove_device_node(c, DEVICE, DEVICE)) {
fprintf(stderr, "Removing %s from the container (%s) failed...\n", DEVICE, NAME);
goto out;
......@@ -64,6 +65,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "Destroying the container (%s) failed...\n", NAME);
goto out;
}
ret = 0;
out:
......
......@@ -51,6 +51,7 @@ int main(int argc, char *argv[])
ret = 1;
goto out;
}
ret = c->get_keys(c, NULL, v3, len+1);
if (ret != len) {
fprintf(stderr, "%d: failed to get keys (%d)\n", __LINE__, ret);
......
......@@ -31,17 +31,21 @@ static void test_list_func(const char *lxcpath, const char *type,
char **names;
printf("%-10s Counting containers\n", type);
n = func(lxcpath, NULL, NULL);
printf("%-10s Counted %d containers\n", type, n);
printf("%-10s Get container struct only\n", type);
n2 = func(lxcpath, NULL, &clist);
if (n2 != n)
printf("Warning: first call returned %d, second %d\n", n, n2);
for (i = 0; i < n2; i++) {
struct lxc_container *c = clist[i];
printf("%-10s Got container struct %s\n", type, c->name);
lxc_container_put(c);
}
if (n2 > 0) {
free(clist);
clist = NULL;
......@@ -51,10 +55,12 @@ static void test_list_func(const char *lxcpath, const char *type,
n2 = func(lxcpath, &names, NULL);
if (n2 != n)
printf("Warning: first call returned %d, second %d\n", n, n2);
for (i = 0; i < n2; i++) {
printf("%-10s Got container name %s\n", type, names[i]);
free(names[i]);
}
if (n2 > 0) {
free(names);
names = NULL;
......@@ -64,14 +70,18 @@ static void test_list_func(const char *lxcpath, const char *type,
n2 = func(lxcpath, &names, &clist);
if (n2 != n)
printf("Warning: first call returned %d, second %d\n", n, n2);
for (i = 0; i < n2; i++) {
struct lxc_container *c = clist[i];
printf("%-10s Got container struct %s, name %s\n", type, c->name, names[i]);
if (strcmp(c->name, names[i]))
fprintf(stderr, "ERROR: name mismatch!\n");
free(names[i]);
lxc_container_put(c);
}
if (n2 > 0) {
free(names);
free(clist);
......
......@@ -38,39 +38,49 @@ static void test_two_locks(void)
if (pipe(p) < 0)
exit(1);
if ((pid = fork()) < 0)
exit(1);
if (pid == 0) {
if (read(p[0], &c, 1) < 0) {
perror("read");
exit(1);
}
l = lxc_newlock("/tmp", "lxctest-sem");
if (!l) {
fprintf(stderr, "%d: child: failed to create lock\n", __LINE__);
exit(1);
}
if (lxclock(l, 0) < 0) {
fprintf(stderr, "%d: child: failed to grab lock\n", __LINE__);
exit(1);
}
fprintf(stderr, "%d: child: grabbed lock\n", __LINE__);
exit(0);
}
l = lxc_newlock("/tmp", "lxctest-sem");
if (!l) {
fprintf(stderr, "%d: failed to create lock\n", __LINE__);
exit(1);
}
if (lxclock(l, 0) < 0) {
fprintf(stderr, "%d; failed to get lock\n", __LINE__);
exit(1);
}
if (write(p[1], "a", 1) < 0) {
perror("write");
exit(1);
}
sleep(3);
ret = waitpid(pid, &status, WNOHANG);
if (ret == pid) { // task exited
if (WIFEXITED(status)) {
......@@ -85,6 +95,7 @@ static void test_two_locks(void)
perror("waitpid");
exit(1);
}
kill(pid, SIGKILL);
wait(&status);
close(p[1]);
......@@ -103,6 +114,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "%d: failed to get unnamed lock\n", __LINE__);
exit(1);
}
ret = lxclock(lock, 0);
if (ret) {
fprintf(stderr, "%d: failed to take unnamed lock (%d)\n", __LINE__, ret);
......@@ -121,8 +133,10 @@ int main(int argc, char *argv[])
fprintf(stderr, "%d: failed to get lock\n", __LINE__);
exit(1);
}
struct stat sb;
char *pathname = RUNTIME_PATH "/lxc/lock/var/lib/lxc/";
ret = stat(pathname, &sb);
if (ret != 0) {
fprintf(stderr, "%d: filename %s not created\n", __LINE__,
......
......@@ -49,6 +49,7 @@ void test_lxc_deslashify(void)
t = lxc_deslashify(s);
if (!t)
exit(EXIT_FAILURE);
lxc_test_assert_abort(strcmp(t, "/A/B/C/D/E") == 0);
free(t);
......@@ -57,6 +58,7 @@ void test_lxc_deslashify(void)
t = lxc_deslashify(s);
if (!t)
exit(EXIT_FAILURE);
lxc_test_assert_abort(strcmp(t, "/A") == 0);
free(t);
......@@ -64,6 +66,7 @@ void test_lxc_deslashify(void)
t = lxc_deslashify(s);
if (!t)
exit(EXIT_FAILURE);
lxc_test_assert_abort(strcmp(t, "") == 0);
free(t);
......@@ -72,6 +75,7 @@ void test_lxc_deslashify(void)
t = lxc_deslashify(s);
if (!t)
exit(EXIT_FAILURE);
lxc_test_assert_abort(strcmp(t, "/") == 0);
free(t);
}
......@@ -188,6 +192,7 @@ void test_detect_ramfs_rootfs(void)
for (i = 0; i < sizeof(mountinfo) / sizeof(mountinfo[0]); i++) {
if (strcmp(mountinfo[i], "24 0 8:2 / / rw - rootfs rootfs rw,size=1004396k,nr_inodes=251099") == 0)
continue;
if (fprintf(fp2, "%s\n", mountinfo[i]) < 0) {
lxc_error("Could not write \"%s\" to temporary file.", mountinfo[i]);
goto non_test_error;
......@@ -217,6 +222,7 @@ non_test_error:
fclose(fp1);
else if (fd1 > 0)
close(fd1);
if (fp2)
fclose(fp2);
else if (fd2 > 0)
......@@ -229,8 +235,10 @@ non_test_error:
}
close(init_ns);
}
if (fret == EXIT_SUCCESS)
return;
exit(fret);
}
......@@ -246,11 +254,13 @@ void test_lxc_safe_uint(void)
ret = snprintf(numstr, LXC_NUMSTRLEN64, "%" PRIu64, (uint64_t)UINT_MAX);
if (ret < 0 || ret >= LXC_NUMSTRLEN64)
exit(EXIT_FAILURE);
lxc_test_assert_abort((0 == lxc_safe_uint(numstr, &n)) && n == UINT_MAX);
ret = snprintf(numstr, LXC_NUMSTRLEN64, "%" PRIu64, (uint64_t)UINT_MAX + 1);
if (ret < 0 || ret >= LXC_NUMSTRLEN64)
exit(EXIT_FAILURE);
lxc_test_assert_abort((-ERANGE == lxc_safe_uint(numstr, &n)));
lxc_test_assert_abort((0 == lxc_safe_uint("1234345", &n)) && n == 1234345);
......@@ -277,21 +287,25 @@ void test_lxc_safe_int(void)
ret = snprintf(numstr, LXC_NUMSTRLEN64, "%" PRIu64, (uint64_t)INT_MAX);
if (ret < 0 || ret >= LXC_NUMSTRLEN64)
exit(EXIT_FAILURE);
lxc_test_assert_abort((0 == lxc_safe_int(numstr, &n)) && n == INT_MAX);
ret = snprintf(numstr, LXC_NUMSTRLEN64, "%" PRIu64, (uint64_t)INT_MAX + 1);
if (ret < 0 || ret >= LXC_NUMSTRLEN64)
exit(EXIT_FAILURE);
lxc_test_assert_abort((-ERANGE == lxc_safe_int(numstr, &n)));
ret = snprintf(numstr, LXC_NUMSTRLEN64, "%" PRId64, (int64_t)INT_MIN);
if (ret < 0 || ret >= LXC_NUMSTRLEN64)
exit(EXIT_FAILURE);
lxc_test_assert_abort((0 == lxc_safe_int(numstr, &n)) && n == INT_MIN);
ret = snprintf(numstr, LXC_NUMSTRLEN64, "%" PRId64, (int64_t)INT_MIN - 1);
if (ret < 0 || ret >= LXC_NUMSTRLEN64)
exit(EXIT_FAILURE);
lxc_test_assert_abort((-ERANGE == lxc_safe_int(numstr, &n)));
lxc_test_assert_abort((0 == lxc_safe_int("1234345", &n)) && n == 1234345);
......@@ -394,6 +408,7 @@ void test_parse_byte_size_string(void)
lxc_error("%s\n", "Failed to parse \"0\"");
exit(EXIT_FAILURE);
}
if (n != 0) {
lxc_error("%s\n", "Failed to parse \"0\"");
exit(EXIT_FAILURE);
......@@ -404,6 +419,7 @@ void test_parse_byte_size_string(void)
lxc_error("%s\n", "Failed to parse \"1\"");
exit(EXIT_FAILURE);
}
if (n != 1) {
lxc_error("%s\n", "Failed to parse \"1\"");
exit(EXIT_FAILURE);
......@@ -420,6 +436,7 @@ void test_parse_byte_size_string(void)
lxc_error("%s\n", "Failed to parse \"1B\"");
exit(EXIT_FAILURE);
}
if (n != 1) {
lxc_error("%s\n", "Failed to parse \"1B\"");
exit(EXIT_FAILURE);
......@@ -430,6 +447,7 @@ void test_parse_byte_size_string(void)
lxc_error("%s\n", "Failed to parse \"1kB\"");
exit(EXIT_FAILURE);
}
if (n != 1024) {
lxc_error("%s\n", "Failed to parse \"1kB\"");
exit(EXIT_FAILURE);
......@@ -440,6 +458,7 @@ void test_parse_byte_size_string(void)
lxc_error("%s\n", "Failed to parse \"1MB\"");
exit(EXIT_FAILURE);
}
if (n != 1048576) {
lxc_error("%s\n", "Failed to parse \"1MB\"");
exit(EXIT_FAILURE);
......@@ -456,6 +475,7 @@ void test_parse_byte_size_string(void)
lxc_error("%s\n", "Failed to parse \"1 B\"");
exit(EXIT_FAILURE);
}
if (n != 1) {
lxc_error("%s\n", "Failed to parse \"1 B\"");
exit(EXIT_FAILURE);
......@@ -466,6 +486,7 @@ void test_parse_byte_size_string(void)
lxc_error("%s\n", "Failed to parse \"1 kB\"");
exit(EXIT_FAILURE);
}
if (n != 1024) {
lxc_error("%s\n", "Failed to parse \"1 kB\"");
exit(EXIT_FAILURE);
......@@ -476,6 +497,7 @@ void test_parse_byte_size_string(void)
lxc_error("%s\n", "Failed to parse \"1 MB\"");
exit(EXIT_FAILURE);
}
if (n != 1048576) {
lxc_error("%s\n", "Failed to parse \"1 MB\"");
exit(EXIT_FAILURE);
......@@ -503,8 +525,10 @@ void test_lxc_config_net_hwaddr(void)
if (lxc_config_net_hwaddr("lxc.net"))
exit(EXIT_FAILURE);
if (lxc_config_net_hwaddr("lxc.net."))
exit(EXIT_FAILURE);
if (lxc_config_net_hwaddr("lxc.net.0."))
exit(EXIT_FAILURE);
}
......
......@@ -125,6 +125,7 @@ int main(int argc, char *argv[])
flags |= CLONE_NEWIPC;
flags |= CLONE_NEWPID;
flags |= CLONE_NEWUTS;
pid = lxc_raw_clone(flags);
if (pid < 0) {
lxc_error("%s\n", "Failed to call lxc_raw_clone(CLONE_NEWUSER "
......@@ -172,7 +173,6 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
if (pid == 0) {
lxc_error("%s\n", "Child will exit(EXIT_SUCCESS)");
exit(EXIT_SUCCESS);
......@@ -190,7 +190,6 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
if (pid == 0) {
lxc_error("%s\n", "Child will exit(EXIT_FAILURE)");
exit(EXIT_FAILURE);
......@@ -208,7 +207,6 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
if (pid == 0) {
lxc_error("%s\n", "Child will exit(EXIT_SUCCESS)");
exit(EXIT_SUCCESS);
......@@ -226,7 +224,6 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
if (pid == 0) {
lxc_error("%s\n", "Child will exit(EXIT_FAILURE)");
exit(EXIT_FAILURE);
......
......@@ -44,6 +44,7 @@ int main()
TSTERR("create using default path");
goto err;
}
p1 = c->get_config_path(c);
p2 = c->config_file_name(c);
if (!p1 || !p2 || strncmp(p1, p2, strlen(p1))) {
......@@ -57,6 +58,7 @@ int main()
TSTERR("Error setting custom path");
goto err;
}
p1 = c->get_config_path(c);
p2 = c->config_file_name(c);
if (strcmp(p1, CPATH) || strcmp(p2, FPATH)) {
......
......@@ -35,12 +35,16 @@ int main(int argc, char *argv[])
if (argc < 2)
usage(argv[0]);
name = argv[1];
if (argc == 3)
lxcpath = argv[2];
c = lxc_container_new(name, lxcpath);
if (c)
may = c->may_control(c);
printf("You may%s control %s\n", may ? "" : " not", name);
exit(may ? 0 : 1);
}
......@@ -41,6 +41,7 @@ static int do_reboot(void *arg)
if (reboot(*cmd))
printf("failed to reboot(%d): %s\n", *cmd, strerror(errno));
return 0;
}
......@@ -65,6 +66,7 @@ static int test_reboot(int cmd, int sig)
if (!WIFSIGNALED(status)) {
if (sig != -1)
printf("child process exited but was not signaled\n");
return -1;
}
......@@ -89,9 +91,11 @@ static int have_reboot_patch(void)
fclose(f);
if (ret != 1)
return 0;
ret = reboot(v ? LINUX_REBOOT_CMD_CAD_ON : LINUX_REBOOT_CMD_CAD_OFF);
if (ret != -1)
return 0;
return 1;
}
......
......@@ -37,24 +37,30 @@ static int create_container(void)
perror("fork");
return -1;
}
if (pid == 0) {
execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME, NULL);
exit(EXIT_FAILURE);
}
again:
ret = waitpid(pid, &status, 0);
if (ret == -1) {
if (errno == EINTR)
goto again;
perror("waitpid");
return -1;
}
if (ret != pid)
goto again;
if (!WIFEXITED(status)) { // did not exit normally
fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
return -1;
}
return WEXITSTATUS(status);
}
......@@ -90,6 +96,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "%d: failed writing config file /tmp/lxctest1\n", __LINE__);
goto out;
}
rename(LXCPATH "/" MYNAME "/config", LXCPATH "/" MYNAME "/config.bak");
if (!c->save_config(c, NULL)) {
fprintf(stderr, "%d: failed writing config file\n", __LINE__);
......@@ -108,6 +115,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "all lxc_container tests passed for %s\n", c->name);
ret = 0;
out:
lxc_container_put(c);
exit(ret);
......
......@@ -46,10 +46,12 @@ static int destroy_container(void)
perror("fork");
return -1;
}
if (pid == 0) {
execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME, NULL);
exit(EXIT_FAILURE);
}
again:
ret = waitpid(pid, &status, 0);
if (ret == -1) {
......@@ -58,12 +60,15 @@ again:
perror("waitpid");
return -1;
}
if (ret != pid)
goto again;
if (!WIFEXITED(status)) { // did not exit normally
fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
return -1;
}
return WEXITSTATUS(status);
}
......@@ -76,24 +81,30 @@ static int create_container(void)
perror("fork");
return -1;
}
if (pid == 0) {
execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME, NULL);
exit(EXIT_FAILURE);
}
again:
ret = waitpid(pid, &status, 0);
if (ret == -1) {
if (errno == EINTR)
goto again;
perror("waitpid");
return -1;
}
if (ret != pid)
goto again;
if (!WIFEXITED(status)) { // did not exit normally
fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
return -1;
}
return WEXITSTATUS(status);
}
......@@ -124,6 +135,7 @@ int main(int argc, char *argv[])
log.prefix = "shortlived";
log.quiet = false;
log.lxcpath = NULL;
if (lxc_log_init(&log))
exit(EXIT_FAILURE);
......@@ -256,11 +268,13 @@ out:
if (fd >= 0) {
char buf[4096];
ssize_t buflen;
while ((buflen = read(fd, buf, 1024)) > 0) {
buflen = write(STDERR_FILENO, buf, buflen);
if (buflen <= 0)
break;
}
close(fd);
}
}
......
......@@ -49,8 +49,10 @@ int main(int argc, char *argv[])
fprintf(stderr, "%d: failed to set network type\n", __LINE__);
goto out;
}
c->set_config_item(c, "lxc.net.0.link", "lxcbr0");
c->set_config_item(c, "lxc.net.0.flags", "up");
if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) {
fprintf(stderr, "%d: failed to create a container\n", __LINE__);
goto out;
......@@ -64,6 +66,7 @@ int main(int argc, char *argv[])
c->clear_config(c);
c->load_config(c, NULL);
c->want_daemonize(c, true);
if (!c->startl(c, 0, NULL)) {
fprintf(stderr, "%d: failed to start %s\n", __LINE__, MYNAME);
goto out;
......@@ -74,9 +77,9 @@ int main(int argc, char *argv[])
if (!c->shutdown(c, 120)) {
fprintf(stderr, "%d: failed to shut down %s\n", __LINE__, MYNAME);
if (!c->stop(c)) {
if (!c->stop(c))
fprintf(stderr, "%d: failed to kill %s\n", __LINE__, MYNAME);
}
goto out;
}
......@@ -92,10 +95,10 @@ int main(int argc, char *argv[])
fprintf(stderr, "all lxc_container tests passed for %s\n", c->name);
ret = 0;
out:
if (c && c->is_defined(c)) {
if (c && c->is_defined(c))
c->destroy(c);
}
lxc_container_put(c);
exit(ret);
......
......@@ -32,23 +32,28 @@
static void try_to_remove(void)
{
struct lxc_container *c;
c = lxc_container_new(RESTNAME, NULL);
if (c) {
c->snapshot_destroy_all(c);
if (c->is_defined(c))
c->destroy(c);
lxc_container_put(c);
}
c = lxc_container_new(MYNAME2, NULL);
if (c) {
c->destroy_with_snapshots(c);
lxc_container_put(c);
}
c = lxc_container_new(MYNAME, NULL);
if (c) {
c->snapshot_destroy_all(c);
if (c->is_defined(c))
c->destroy(c);
lxc_container_put(c);
}
}
......@@ -66,6 +71,7 @@ int main(int argc, char *argv[])
template = argv[1];
try_to_remove();
c = lxc_container_new(MYNAME, NULL);
if (!c) {
fprintf(stderr, "%s: %d: failed to load first container\n", __FILE__, __LINE__);
......@@ -76,15 +82,19 @@ int main(int argc, char *argv[])
fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME);
(void) c->destroy_with_snapshots(c);
}
if (!c->set_config_item(c, "lxc.net.0.type", "empty")) {
fprintf(stderr, "%s: %d: failed to set network type\n", __FILE__, __LINE__);
goto err;
}
c->save_config(c, NULL);
if (!c->createl(c, template, NULL, NULL, 0, NULL)) {
fprintf(stderr, "%s: %d: failed to create %s container\n", __FILE__, __LINE__, template);
goto err;
}
c->load_config(c, NULL);
if (c->snapshot(c, NULL) != 0) {
......@@ -110,13 +120,14 @@ int main(int argc, char *argv[])
fprintf(stderr, "%s: %d: failed listing containers\n", __FILE__, __LINE__);
goto err;
}
if (strcmp(s->name, "snap0") != 0) {
fprintf(stderr, "%s: %d: snapshot had bad name\n", __FILE__, __LINE__);
goto err;
}
for (i=0; i<n; i++) {
for (i=0; i<n; i++)
s[i].free(&s[i]);
}
free(s);
if (!c->snapshot_restore(c, "snap0", RESTNAME)) {
......@@ -152,13 +163,14 @@ int main(int argc, char *argv[])
fprintf(stderr, "%s: %d: failed listing containers\n", __FILE__, __LINE__);
goto err;
}
if (strcmp(s->name, "snap0") != 0) {
fprintf(stderr, "%s: %d: snapshot had bad name\n", __FILE__, __LINE__);
goto err;
}
for (i=0; i<n; i++) {
for (i=0; i<n; i++)
s[i].free(&s[i]);
}
free(s);
if (!c2->snapshot_restore(c2, "snap0", NULL)) {
......@@ -177,6 +189,7 @@ good:
printf("All tests passed\n");
exit(0);
err:
lxc_container_put(c);
try_to_remove();
......
......@@ -39,24 +39,30 @@ static int destroy_container(void)
perror("fork");
return -1;
}
if (pid == 0) {
execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME, NULL);
exit(EXIT_FAILURE);
}
again:
ret = waitpid(pid, &status, 0);
if (ret == -1) {
if (errno == EINTR)
goto again;
perror("waitpid");
return -1;
}
if (ret != pid)
goto again;
if (!WIFEXITED(status)) { // did not exit normally
fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
return -1;
}
return WEXITSTATUS(status);
}
......@@ -69,24 +75,30 @@ static int create_container(void)
perror("fork");
return -1;
}
if (pid == 0) {
execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME, NULL);
exit(EXIT_FAILURE);
}
again:
ret = waitpid(pid, &status, 0);
if (ret == -1) {
if (errno == EINTR)
goto again;
perror("waitpid");
return -1;
}
if (ret != pid)
goto again;
if (!WIFEXITED(status)) { // did not exit normally
fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
return -1;
}
return WEXITSTATUS(status);
}
......@@ -100,6 +112,7 @@ int main(int argc, char *argv[])
int len;
ret = 1;
/* test a real container */
c = lxc_container_new(MYNAME, NULL);
if (!c) {
......@@ -167,6 +180,7 @@ int main(int argc, char *argv[])
}
sleep(3);
s = c->state(c);
if (!s || strcmp(s, "RUNNING")) {
fprintf(stderr, "%d: %s is in state %s, not in RUNNING.\n", __LINE__, c->name, s ? s : "undefined");
......@@ -192,7 +206,7 @@ int main(int argc, char *argv[])
goto out;
}
sprintf(buf, "XXX");
sprintf(buf, "XXX");
len = c->get_cgroup_item(c, "freezer.state", buf, 200);
if (len <= 0 || (strcmp(buf, "FREEZING\n") && strcmp(buf, "FROZEN\n"))) {
fprintf(stderr, "%d: not able to get freezer.state (len %d buf %s)\n", __LINE__, len, buf);
......
......@@ -149,5 +149,6 @@ on_error_put:
lxc_container_put(c);
if (ret == EXIT_SUCCESS)
lxc_debug("%s\n", "All state server tests passed");
exit(ret);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment