Files
flexnbd-c/tests/unit/check_status.c
Alex Young 22bea81445 Don't open the control socket until after the server socket is bound
This makes it easier for the tests (and supervisor) to guarantee to be
able to connect to the server socket.

Also this patch moves freeing the mirror supervisor into the server
thread.
2012-10-09 17:35:20 +01:00

175 lines
3.6 KiB
C

#include "status.h"
#include "serve.h"
#include "ioutil.h"
#include "util.h"
#include <check.h>
START_TEST( test_status_create )
{
struct server server;
struct status *status = NULL;
status = status_create( &server );
fail_if( NULL == status, "Status wasn't allocated" );
status_destroy( status );
}
END_TEST
START_TEST( test_gets_has_control )
{
struct server server;
struct status * status;
server.success = 1;
status = status_create( &server );
fail_unless( status->has_control == 1, "has_control wasn't copied" );
status_destroy( status );
}
END_TEST
START_TEST( test_gets_is_mirroring )
{
struct server server;
struct status * status;
server.mirror = NULL;
status = status_create( &server );
fail_if( status->is_mirroring, "is_mirroring was set" );
status_destroy( status );
server.mirror = (struct mirror *)xmalloc( sizeof( struct mirror ) );
status = status_create( &server );
fail_unless( status->is_mirroring, "is_mirroring wasn't set" );
status_destroy( status );
}
END_TEST
START_TEST( test_gets_pid )
{
struct server server;
struct status * status;
server.mirror = NULL;
status = status_create( &server );
fail_unless( getpid() == status->pid, "Pid wasn't gathered" );
status_destroy( status );
}
END_TEST
START_TEST( test_renders_has_control )
{
struct status status;
int fds[2];
pipe(fds);
char buf[1024] = {0};
status.has_control = 1;
status_write( &status, fds[1] );
fail_unless( read_until_newline( fds[0], buf, 1024 ) > 0,
"Couldn't read the result" );
char *found = strstr( buf, "has_control=true" );
fail_if( NULL == found, "has_control=true not found" );
status.has_control = 0;
status_write( &status, fds[1] );
fail_unless( read_until_newline( fds[0], buf, 1024 ) > 0,
"Couldn't read the result" );
found = strstr( buf, "has_control=false" );
fail_if( NULL == found, "has_control=false not found" );
}
END_TEST
START_TEST( test_renders_is_mirroring )
{
struct status status;
int fds[2];
pipe(fds);
char buf[1024] = {0};
status.is_mirroring = 1;
status_write( &status, fds[1] );
fail_unless( read_until_newline( fds[0], buf, 1024 ) > 0,
"Couldn't read the result" );
char *found = strstr( buf, "is_mirroring=true" );
fail_if( NULL == found, "is_mirroring=true not found" );
status.is_mirroring = 0;
status_write( &status, fds[1] );
fail_unless( read_until_newline( fds[0], buf, 1024 ) > 0,
"Couldn't read the result" );
found = strstr( buf, "is_mirroring=false" );
fail_if( NULL == found, "is_mirroring=false not found" );
}
END_TEST
START_TEST( test_renders_pid )
{
struct status status;
int fds[2];
pipe(fds);
char buf[1024] = {0};
status.pid = 42;
status_write( &status, fds[1] );
fail_unless( read_until_newline( fds[0], buf, 1024 ) > 0,
"Couldn't read the result" );
char *found = strstr( buf, "pid=42" );
fail_if( NULL == found, "pid=42 not found" );
}
END_TEST
Suite *status_suite(void)
{
Suite *s = suite_create("status");
TCase *tc_create = tcase_create("create");
TCase *tc_render = tcase_create("render");
tcase_add_test(tc_create, test_status_create);
tcase_add_test(tc_create, test_gets_has_control);
tcase_add_test(tc_create, test_gets_is_mirroring);
tcase_add_test(tc_create, test_gets_pid);
tcase_add_test(tc_render, test_renders_has_control);
tcase_add_test(tc_render, test_renders_is_mirroring);
tcase_add_test(tc_render, test_renders_pid);
suite_add_tcase(s, tc_create);
suite_add_tcase(s, tc_render);
return s;
}
int main(void)
{
int number_failed;
Suite *s = status_suite();
SRunner *sr = srunner_create(s);
srunner_run_all(sr, CK_NORMAL);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return (number_failed == 0) ? 0 : 1;
}