Unverified Commit 6ad802b2 by 2xsec Committed by Christian Brauner

tests: cleanup containertests.c

Signed-off-by: 's avatar2xsec <dh48.jeong@samsung.com>
parent d4e6fc58
...@@ -39,10 +39,12 @@ static int destroy_busybox(void) ...@@ -39,10 +39,12 @@ static int destroy_busybox(void)
perror("fork"); perror("fork");
return -1; return -1;
} }
if (pid == 0) { if (pid == 0) {
execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME, NULL); execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME, NULL);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
again: again:
ret = waitpid(pid, &status, 0); ret = waitpid(pid, &status, 0);
if (ret == -1) { if (ret == -1) {
...@@ -51,12 +53,15 @@ again: ...@@ -51,12 +53,15 @@ again:
perror("waitpid"); perror("waitpid");
return -1; return -1;
} }
if (ret != pid) if (ret != pid)
goto again; goto again;
if (!WIFEXITED(status)) { // did not exit normally if (!WIFEXITED(status)) { // did not exit normally
fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__); fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
return -1; return -1;
} }
return WEXITSTATUS(status); return WEXITSTATUS(status);
} }
...@@ -69,10 +74,12 @@ static int create_busybox(void) ...@@ -69,10 +74,12 @@ static int create_busybox(void)
perror("fork"); perror("fork");
return -1; return -1;
} }
if (pid == 0) { if (pid == 0) {
execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME, NULL); execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME, NULL);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
again: again:
ret = waitpid(pid, &status, 0); ret = waitpid(pid, &status, 0);
if (ret == -1) { if (ret == -1) {
...@@ -81,12 +88,15 @@ again: ...@@ -81,12 +88,15 @@ again:
perror("waitpid"); perror("waitpid");
return -1; return -1;
} }
if (ret != pid) if (ret != pid)
goto again; goto again;
if (!WIFEXITED(status)) { // did not exit normally if (!WIFEXITED(status)) { // did not exit normally
fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__); fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
return -1; return -1;
} }
return WEXITSTATUS(status); return WEXITSTATUS(status);
} }
...@@ -99,25 +109,30 @@ int main(int argc, char *argv[]) ...@@ -99,25 +109,30 @@ int main(int argc, char *argv[])
char *str; char *str;
ret = 1; ret = 1;
/* test refcounting */ /* test refcounting */
c = lxc_container_new(MYNAME, NULL); c = lxc_container_new(MYNAME, NULL);
if (!c) { if (!c) {
fprintf(stderr, "%d: error creating lxc_container %s\n", __LINE__, MYNAME); fprintf(stderr, "%d: error creating lxc_container %s\n", __LINE__, MYNAME);
goto out; goto out;
} }
if (!lxc_container_get(c)) { if (!lxc_container_get(c)) {
fprintf(stderr, "%d: error getting refcount\n", __LINE__); fprintf(stderr, "%d: error getting refcount\n", __LINE__);
goto out; goto out;
} }
/* peek in, inappropriately, make sure refcount is a we'd like */ /* peek in, inappropriately, make sure refcount is a we'd like */
if (c->numthreads != 2) { if (c->numthreads != 2) {
fprintf(stderr, "%d: refcount is %d, not %d\n", __LINE__, c->numthreads, 2); fprintf(stderr, "%d: refcount is %d, not %d\n", __LINE__, c->numthreads, 2);
goto out; goto out;
} }
if (strcmp(c->name, MYNAME) != 0) { if (strcmp(c->name, MYNAME) != 0) {
fprintf(stderr, "%d: container has wrong name (%s not %s)\n", __LINE__, c->name, MYNAME); fprintf(stderr, "%d: container has wrong name (%s not %s)\n", __LINE__, c->name, MYNAME);
goto out; goto out;
} }
str = c->config_file_name(c); str = c->config_file_name(c);
#define CONFIGFNAM LXCPATH "/" MYNAME "/config" #define CONFIGFNAM LXCPATH "/" MYNAME "/config"
if (!str || strcmp(str, CONFIGFNAM)) { if (!str || strcmp(str, CONFIGFNAM)) {
...@@ -127,19 +142,23 @@ int main(int argc, char *argv[]) ...@@ -127,19 +142,23 @@ int main(int argc, char *argv[])
free(str); free(str);
free(c->configfile); free(c->configfile);
c->configfile = NULL; c->configfile = NULL;
str = c->config_file_name(c); str = c->config_file_name(c);
if (str) { if (str) {
fprintf(stderr, "%d: config file name was not NULL as it should have been\n", __LINE__); fprintf(stderr, "%d: config file name was not NULL as it should have been\n", __LINE__);
goto out; goto out;
} }
if (lxc_container_put(c) != 0) { if (lxc_container_put(c) != 0) {
fprintf(stderr, "%d: c was freed on non-final put\n", __LINE__); fprintf(stderr, "%d: c was freed on non-final put\n", __LINE__);
goto out; goto out;
} }
if (c->numthreads != 1) { if (c->numthreads != 1) {
fprintf(stderr, "%d: refcount is %d, not %d\n", __LINE__, c->numthreads, 1); fprintf(stderr, "%d: refcount is %d, not %d\n", __LINE__, c->numthreads, 1);
goto out; goto out;
} }
if (lxc_container_put(c) != 1) { if (lxc_container_put(c) != 1) {
fprintf(stderr, "%d: c was not freed on final put\n", __LINE__); fprintf(stderr, "%d: c was not freed on final put\n", __LINE__);
goto out; goto out;
...@@ -202,9 +221,11 @@ int main(int argc, char *argv[]) ...@@ -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); fprintf(stderr, "%d: lxc_get_wait_states gave %d not %d\n", __LINE__, numstates, MAX_STATE);
goto out; goto out;
} }
const char **sstr = malloc(numstates * sizeof(const char *)); const char **sstr = malloc(numstates * sizeof(const char *));
numstates = lxc_get_wait_states(sstr); numstates = lxc_get_wait_states(sstr);
int i; int i;
for (i=0; i<numstates; i++) { for (i=0; i<numstates; i++) {
fprintf(stderr, "got state %d %s\n", i, sstr[i]); fprintf(stderr, "got state %d %s\n", i, sstr[i]);
} }
...@@ -237,6 +258,7 @@ out: ...@@ -237,6 +258,7 @@ out:
c->stop(c); c->stop(c);
destroy_busybox(); destroy_busybox();
} }
lxc_container_put(c); lxc_container_put(c);
exit(ret); 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