Skip to content

Commit

Permalink
tests UPDATE avoid calling system
Browse files Browse the repository at this point in the history
  • Loading branch information
michalvasko committed May 21, 2024
1 parent 7cf58b7 commit 3b52f0a
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 77 deletions.
34 changes: 34 additions & 0 deletions tests/np2_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "np2_test.h"

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <pwd.h>
Expand Down Expand Up @@ -332,6 +333,39 @@ np2_glob_test_setup_server(void **state, const char *test_name, const char **mod
return 0;
}

int
np2_glob_test_teardown_notif(const char *test_name)
{
char *path;
DIR *dir;
struct dirent *ent;

/* open notification dir */
if (asprintf(&path, "%s/%s/data/notif", NP_SR_REPOS_DIR, test_name) == -1) {
return 1;
}
dir = opendir(path);
free(path);
if (!dir) {
return 1;
}

/* remove all notif1 notifications */
while ((ent = readdir(dir))) {
if (!strncmp(ent->d_name, "notif1.notif", 12)) {
if (asprintf(&path, "%s/%s/data/notif/%s", NP_SR_REPOS_DIR, test_name, ent->d_name) == -1) {
closedir(dir);
return 1;
}
unlink(path);
free(path);
}
}

closedir(dir);
return 0;
}

int
np2_glob_test_teardown(void **state, const char **modules)
{
Expand Down
2 changes: 2 additions & 0 deletions tests/np2_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ int np2_glob_test_setup_env(const char *test_name);

int np2_glob_test_setup_server(void **state, const char *test_name, const char **modules);

int np2_glob_test_teardown_notif(const char *test_name);

int np2_glob_test_teardown(void **state, const char **modules);

void parse_arg(int argc, char **argv);
Expand Down
15 changes: 2 additions & 13 deletions tests/test_sub_ntf.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,9 @@ static int
teardown_common(void **state)
{
struct np2_test *st = *state;
char *cmd;
int ret;

/* Remove the notifications */
if (asprintf(&cmd, "rm -rf %s/%s/data/notif/notif1.notif*", NP_SR_REPOS_DIR, st->test_name) == -1) {
return 1;
}

ret = system(cmd);
free(cmd);

if (ret == -1) {
return 1;
} else if (!WIFEXITED(ret) || WEXITSTATUS(ret)) {
/* remove notifications */
if (np2_glob_test_teardown_notif(st->test_name)) {
return 1;
}

Expand Down
15 changes: 2 additions & 13 deletions tests/test_sub_ntf_advanced.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,9 @@ static int
teardown_common(void **state)
{
struct np2_test *st = *state;
char *cmd;
int ret;

/* Remove the notifications */
if (asprintf(&cmd, "rm -rf %s/%s/data/notif/notif1.notif*", NP_SR_REPOS_DIR, st->test_name) == -1) {
return 1;
}

ret = system(cmd);
free(cmd);

if (ret == -1) {
return 1;
} else if (!WIFEXITED(ret) || WEXITSTATUS(ret)) {
/* remove notifications */
if (np2_glob_test_teardown_notif(st->test_name)) {
return 1;
}

Expand Down
15 changes: 2 additions & 13 deletions tests/test_sub_ntf_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,9 @@ static int
teardown_common(void **state)
{
struct np2_test *st = *state;
char *cmd;
int ret;

/* Remove the notifications */
if (asprintf(&cmd, "rm -rf %s/%s/data/notif/notif1.notif*", NP_SR_REPOS_DIR, st->test_name) == -1) {
return 1;
}

ret = system(cmd);
free(cmd);

if (ret == -1) {
return 1;
} else if (!WIFEXITED(ret) || WEXITSTATUS(ret)) {
/* remove notifications */
if (np2_glob_test_teardown_notif(st->test_name)) {
return 1;
}

Expand Down
14 changes: 2 additions & 12 deletions tests/test_subscribe_param.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,9 @@ static int
clear_notif(void **state)
{
struct np2_test *st = *state;
char *cmd;
int ret;

if (asprintf(&cmd, "rm -rf %s/%s/data/notif/notif1.notif*", NP_SR_REPOS_DIR, st->test_name) == -1) {
return 1;
}

ret = system(cmd);
free(cmd);

if (ret == -1) {
return 1;
} else if (!WIFEXITED(ret) || WEXITSTATUS(ret)) {
/* remove notifications */
if (np2_glob_test_teardown_notif(st->test_name)) {
return 1;
}

Expand Down
15 changes: 2 additions & 13 deletions tests/test_yang_push.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ teardown_common(void **state)
{
struct np2_test *st = *state;
const char *data;
char *cmd;
int ret;

/* stop NETCONF session */
nc_session_free(st->nc_sess, NULL);
Expand All @@ -78,17 +76,8 @@ teardown_common(void **state)
SR_EDIT(st, data);
FREE_TEST_VARS(st);

/* Remove the notifications */
if (asprintf(&cmd, "rm -rf %s/%s/data/notif/notif1.notif*", NP_SR_REPOS_DIR, st->test_name) == -1) {
return 1;
}

ret = system(cmd);
free(cmd);

if (ret == -1) {
return 1;
} else if (!WIFEXITED(ret) || WEXITSTATUS(ret)) {
/* remove notifications */
if (np2_glob_test_teardown_notif(st->test_name)) {
return 1;
}

Expand Down
15 changes: 2 additions & 13 deletions tests/test_yang_push_advanced.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,9 @@ teardown_common(void **state)
{
struct np2_test *st = *state;
const char *data;
char *cmd;
int ret;

/* Remove the notifications */
if (asprintf(&cmd, "rm -rf %s/%s/data/notif/notif1.notif*", NP_SR_REPOS_DIR, st->test_name) == -1) {
return 1;
}

ret = system(cmd);
free(cmd);

if (ret == -1) {
return 1;
} else if (!WIFEXITED(ret) || WEXITSTATUS(ret)) {
/* remove notifications */
if (np2_glob_test_teardown_notif(st->test_name)) {
return 1;
}

Expand Down

0 comments on commit 3b52f0a

Please sign in to comment.