Skip to content

Commit

Permalink
Move various testing functionalities from init to the Pythonshell.
Browse files Browse the repository at this point in the history
Also remove exec_test and thread_test, as they are no longer necessary.
  • Loading branch information
sgielen committed Jun 17, 2017
1 parent e898a7f commit c794005
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 262 deletions.
90 changes: 88 additions & 2 deletions misc/python/cosix.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import sys
import code
import time

original_print = print
def my_print(*args, **kwargs):
Expand Down Expand Up @@ -90,13 +91,98 @@ def print(self, obj):
def this_conn():
return sys.stderr.sock

def rm_rf(name, dir_fd):
try:
os.unlink(name, dir_fd=dir_fd)
return
except IsADirectoryError:
pass
except FileNotFoundError:
return
fd = -1
try:
fd = os.open(name, os.O_RDWR, dir_fd=dir_fd)
for file in os.listdir(fd):
rm_rf(file, fd)
os.rmdir(name, dir_fd=dir_fd)
except Exception as e:
print("Failed to recursively remove " + name + ": " + str(e))
finally:
if fd >= 0: os.close(fd)

def run_unittests():
rm_rf('unittests', sys.argdata['tmpdir'])
os.mkdir('unittests', dir_fd=sys.argdata['tmpdir'])
dirfd = os.open("unittests", os.O_RDWR, dir_fd=sys.argdata['tmpdir'])

binfd = os.open("unittests", os.O_RDONLY, dir_fd=sys.argdata['bootfs'])
procfd = os.program_spawn(binfd,
{'logfile': this_conn(),
'tmpdir': FDWrapper(sys.argdata['tmpdir']),
'tmpdir': FDWrapper(dirfd),
'nthreads': 1,
})
os.pdwait(procfd, 0)
res = os.pdwait(procfd, 0)
os.close(procfd)
os.close(binfd)
return res

def run_unittests_count(count):
num_success = 0
num_failures = 0
while count == 0 or (num_success + num_failures) < count:
res = run_unittests()
if res.si_status == 0:
success="succeeded"
num_success += 1
else:
success="FAILED"
num_failures += 1
print("== Unittest iteration %d %s. Total %d successes, %d failures." %
(num_success + num_failures, success, num_success, num_failures))
time.sleep(5)

class allocation_tracking():
def __enter__(self):
self.send_alloctracker_command(b'1')
return self

def __exit__(self, *args):
self.send_alloctracker_command(b'0')

@staticmethod
def send_alloctracker_command(cmd):
fd = os.open("kernel/alloctracker", os.O_WRONLY, dir_fd=sys.argdata['procfs'])
os.write(fd, cmd)
os.close(fd)

@staticmethod
def report():
allocation_tracking.send_alloctracker_command(b'R')

def run_leak_analysis():
run_unittests()
with allocation_tracking() as a:
run_unittests()
run_unittests()
run_unittests()
allocation_tracking.report()

def run_binary(binary):
binfd = os.open(binary, os.O_RDONLY, dir_fd=sys.argdata['bootfs'])
procfd = os.program_spawn(binfd,
{'stdout': this_conn(),
'tmpdir': FDWrapper(sys.argdata['tmpdir']),
'networkd': sys.argdata['networkd'],
})
res = os.pdwait(procfd, 0)
os.close(procfd)
os.close(binfd)
return res

def run_tests():
tests = ("pipe_test", "concur_test", "time_test",
"mmap_test", "forkfork_test", "unixsock_test",
"udptest", "tcptest")
for test in tests:
run_binary(test)
run_unittests()
2 changes: 0 additions & 2 deletions userland/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ macro(add_external_binary BINARY_NAME)
endmacro()

add_external_binary(init)
add_external_binary(exec_test)
add_external_binary(thread_test)
add_external_binary(pipe_test)
add_external_binary(concur_test)
add_external_binary(time_test)
Expand Down
11 changes: 0 additions & 11 deletions userland/exec_test/CMakeLists.txt

This file was deleted.

86 changes: 0 additions & 86 deletions userland/exec_test/exec_test.cpp

This file was deleted.

105 changes: 1 addition & 104 deletions userland/init/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,6 @@ int reversefd;
int pseudofd;
int ifstore;

void allocation_tracker_cmd(char cmd) {
int alltrackfd = openat(procfs, "kernel/alloctracker", O_WRONLY);
if(alltrackfd < 0) {
dprintf(stdout, "INIT: failed to send allocation tracker cmd: %s\n", strerror(errno));
return;
}
write(alltrackfd, &cmd, 1);
close(alltrackfd);
}

void start_allocation_tracker() {
allocation_tracker_cmd('1');
}

void stop_allocation_tracker() {
allocation_tracker_cmd('0');
}

void dump_allocation_tracker() {
allocation_tracker_cmd('R');
}

long uptime() {
int uptimefd = openat(procfs, "kernel/uptime", O_RDONLY);
if(uptimefd < 0) {
Expand Down Expand Up @@ -88,23 +66,6 @@ int program_run(const char *name, int bfd, argdata_t *ad) {
return si.si_status;
}

int start_unittests() {
int bfd = openat(bootfs, "unittests", O_RDONLY);
if(bfd < 0) {
dprintf(stdout, "Won't run unittests, because I failed to open them: %s\n", strerror(errno));
return -1;
}

dprintf(stdout, "Running unit tests...\n");
argdata_t *keys[] = {argdata_create_string("logfile"), argdata_create_string("tmpdir"), argdata_create_string("nthreads")};
argdata_t *values[] = {argdata_create_fd(stdout), argdata_create_fd(pseudofd), argdata_create_int(1)};
argdata_t *ad = argdata_create_map(keys, values, sizeof(keys) / sizeof(keys[0]));

auto res = program_run("unittests", bfd, ad);
close(bfd);
return res;
}

void start_tmpfs() {
int bfd = openat(bootfs, "tmpfs", O_RDONLY);
if(bfd < 0) {
Expand Down Expand Up @@ -167,24 +128,6 @@ void start_networkd() {
}
}

int start_binary(const char *name) {
int bfd = openat(bootfs, name, O_RDONLY);
if(bfd < 0) {
dprintf(stdout, "Failed to open %s: %s\n", name, strerror(errno));
return 1;
}

dprintf(stdout, "Init going to program_spawn() %s...\n", name);

argdata_t *keys[] = {argdata_create_string("stdout"), argdata_create_string("tmpdir")};
argdata_t *values[] = {argdata_create_fd(stdout), argdata_create_fd(pseudofd)};
argdata_t *ad = argdata_create_map(keys, values, sizeof(keys) / sizeof(keys[0]));

auto r = program_run(name, bfd, ad);
close(bfd);
return r;
}

int start_networked_binary(const char *name, bool wait = true) {
int bfd = openat(bootfs, name, O_RDONLY);
if(bfd < 0) {
Expand Down Expand Up @@ -354,18 +297,8 @@ void program_main(const argdata_t *) {
FILE *out = fdopen(stdout, "w");
fswap(stderr, out);

start_binary("exec_test");
start_binary("thread_test");
start_binary("pipe_test");
start_binary("concur_test");
start_binary("time_test");
start_binary("mmap_test");
start_binary("forkfork_test");

open_pseudo();
start_tmpfs();
//start_binary("tmptest");
start_binary("unixsock_test");
start_networkd();

// sleep for a bit for networkd to come up
Expand All @@ -374,45 +307,9 @@ void program_main(const argdata_t *) {
clock_nanosleep(CLOCK_MONOTONIC, 0, &ts);
}

start_networked_binary("udptest");
start_networked_binary("tcptest");
start_networked_binary("pythonshell", false);
start_networked_binary("httpd", false);

// sleep for a bit after networking tests
{
struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
clock_nanosleep(CLOCK_MONOTONIC, 0, &ts);
}

uint32_t num_success = 0;
uint32_t num_failures = 0;
while(1) {
auto res = start_unittests();
if(res == 0) {
num_success++;
dprintf(stdout, "== Unittest iteration %d succeeded. Total %d successes, %d failures.\n",
num_success + num_failures, num_success, num_failures);
} else {
num_failures++;
dprintf(stdout, "== Unittest iteration %d FAILED. Total %d successes, %d failures.\n",
num_success + num_failures, num_success, num_failures);
}
DIR *dir = fdopendir(dup(pseudofd));
rm_rf_contents(dir);
closedir(dir);
struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
clock_nanosleep(CLOCK_MONOTONIC, 0, &ts);

size_t count = num_success + num_failures;
if(count == 2) {
start_allocation_tracker();
} else if(count == 6) {
stop_allocation_tracker();
} else if(count == 10) {
dump_allocation_tracker();
break;
}
start_networked_binary("pythonshell", true);
}

pthread_mutex_t mtx;
Expand Down
11 changes: 0 additions & 11 deletions userland/thread_test/CMakeLists.txt

This file was deleted.

Loading

0 comments on commit c794005

Please sign in to comment.