Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions libnczarr/zmetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ typedef struct NCZ_Metadata
int (*validate_consolidated)(const NCjson *jobj);
} NCZ_Metadata;

extern const NCZ_Metadata *NCZ_metadata_handler2;
extern const NCZ_Metadata *NCZ_csl_metadata_handler2;
EXTERNL const NCZ_Metadata *NCZ_metadata_handler2;
EXTERNL const NCZ_Metadata *NCZ_csl_metadata_handler2;

/// @brief Sets the metadata handler for the given zarr file based on
/// environment variables, file creation mode, and dataset contents.
/// @param zfile - The zarr file info structure
/// @return NC_NOERR on success, NC_EZARRMETA on failure
extern int NCZMD_set_metadata_handler(struct NCZ_FILE_INFO *zfile);
EXTERNL int NCZMD_set_metadata_handler(struct NCZ_FILE_INFO *zfile);

/// @brief Determines the Zarr format version set on the metadata handler or by
/// probing the dataset for the existence of zarr metadata objects (.z*).
Expand Down
4 changes: 2 additions & 2 deletions libnczarr/zmetadata2.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ int validate_consolidated_json_noop_v2(const NCjson *json);
/// @return `NC_NOERR` if valid, `NC_EZARRMETA` otherwise
int validate_consolidated_json_v2(const NCjson *json);

static const NCZ_Metadata NCZ_md2_table = {
const NCZ_Metadata NCZ_md2_table = {
ZARRFORMAT2,
NCZ_METADATA_VERSION,
ZARR_NOT_CONSOLIDATED,
Expand All @@ -112,7 +112,7 @@ static const NCZ_Metadata NCZ_md2_table = {

const NCZ_Metadata *NCZ_metadata_handler2 = &NCZ_md2_table;

static const NCZ_Metadata NCZ_csl_md2_table = {
const NCZ_Metadata NCZ_csl_md2_table = {
ZARRFORMAT2,
NCZ_METADATA_VERSION,
ZARR_CONSOLIDATED,
Expand Down
3 changes: 2 additions & 1 deletion nczarr_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ IF(NETCDF_ENABLE_TESTS)
build_bin_test_with_util_lib(ut_mapapi ut_util)
build_bin_test_with_util_lib(ut_json ut_util)

add_bin_test_with_util_lib(nczarr_test ut_zinfer ut_util ../libnczarr/zinfer.c ../libnczarr/zmetadata.c ../libnczarr/zmetadata2.c)
add_bin_test_with_util_lib(nczarr_test ut_consolidated ut_zinfer ut_util ../libnczarr/zinfer.c ../libnczarr/zmetadata.c ../libnczarr/zmetadata2.c)


build_bin_test_with_util_lib(test_fillonlyz test_utils)
build_bin_test_with_util_lib(test_quantize test_utils)
Expand Down
4 changes: 3 additions & 1 deletion nczarr_test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ commonsrc = ut_util.c ut_test.c ut_includes.h ut_test.h ut_util.h test_nczarr_ut
#ut_chunking_SOURCES = ut_chunking.c ${commonsrc}
#ut_walk_SOURCES = ut_walk.c ${commonsrc}

check_PROGRAMS += ut_map ut_mapapi ut_map ut_json
check_PROGRAMS += ut_map ut_mapapi ut_map ut_json ut_consolidated
ut_map_SOURCES = ut_map.c ${commonsrc}
ut_mapapi_SOURCES = ut_mapapi.c ${commonsrc}
ut_json_SOURCES = ut_json.c ${commonsrc}
ut_consolidated_SOURCES = ut_consolidated.c ${commonsrc}
TESTS += ut_consolidated

# per-file unittest
check_PROGRAMS += ut_zinfer
Expand Down
207 changes: 207 additions & 0 deletions nczarr_test/ut_consolidated.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
#include "ut_includes.h"

EXTERNL const NCZ_Metadata* NCZ_metadata_handler2;
EXTERNL const NCZ_Metadata* NCZ_csl_metadata_handler2;

void showHandlers(const NCZ_Metadata *h1, const NCZ_Metadata *h2) {
#define BOOLSTR(x) (x) ? "true" : "false"
fprintf(stderr,
"Handler:\n"
"\tFormat: %d vs %d (%s)\n"
"\tVersion: %d vs %d (%s)\n"
"\tFlags: %llu vs %llu (%s)\n"
"\tJson: %p vs %p (%s)\n"
"\tlist_nodes: %p vs %p (%s)\n"
"\tlist_groups: %p vs %p (%s)\n"
"\tlist_variables: %p vs %p (%s)\n"
"\tupdate_json_content: %p vs %p (%s)\n"
"\tfetch_json_content: %p vs %p (%s)\n"
"\tvalidate_consolidated: %p vs %p (%s)\n",
h1->zarr_format, h2->zarr_format,
BOOLSTR(h1->zarr_format == h2->zarr_format), h1->dispatch_version,
h2->dispatch_version,
BOOLSTR(h1->dispatch_version == h2->dispatch_version), h1->flags,
h2->flags, BOOLSTR(h1->flags == h2->flags), h1->jcsl, h2->jcsl,
BOOLSTR(h1->jcsl == h2->jcsl), h1->list_nodes, h2->list_nodes,
BOOLSTR(h1->list_nodes == h2->list_nodes), h1->list_groups,
h2->list_groups, BOOLSTR(h1->list_groups == h2->list_groups),
h1->list_variables, h2->list_variables,
BOOLSTR(h1->list_variables == h2->list_variables),
h1->fetch_json_content, h2->fetch_json_content,
BOOLSTR(h1->fetch_json_content == h2->fetch_json_content),
h1->update_json_content, h2->update_json_content,
BOOLSTR(h1->update_json_content == h2->update_json_content),
h1->validate_consolidated, h2->validate_consolidated,
BOOLSTR(h1->validate_consolidated == h2->validate_consolidated));
#undef BOOLSTR
}

int cmpHandlers(const NCZ_Metadata *h1, const NCZ_Metadata *h2) {
return h1->zarr_format == h2->zarr_format &&
h1->dispatch_version == h2->dispatch_version &&
h1->flags == h2->flags && // h*->jcsl aren't compared!
h1->list_nodes == h2->list_nodes &&
h1->list_groups == h2->list_groups &&
h1->list_variables == h2->list_variables &&
h1->fetch_json_content == h2->fetch_json_content &&
h1->update_json_content == h2->update_json_content &&
h1->validate_consolidated == h2->validate_consolidated;
}

#define MOCK_ZMETADATA \
"{\"metadata\":{\".zgroup\":{\"zarr_format\":2}},\"zarr_consolidated_" \
"format\":1}"

int mockedunimplemented() {
return 1;
}

int mockedlen(NCZMAP *map, const char *key, size64_t *sizep) {
if (*sizep) {
if (strcmp(key, "/.zmetadata") == 0) {
*sizep = sizeof(MOCK_ZMETADATA);
} else {
*sizep = 0;
}
}
return 0;
}
int mockedread(NCZMAP *map, const char *key, size64_t start, size64_t count,
void *content) {
if (content) {
if (strcmp(key, "/.zmetadata") == 0) {
memcpy(content, MOCK_ZMETADATA + start, count - start);
}
}
return 0;
}

NCZMAP *mockmap() {
NCZMAP *m = calloc(1, sizeof(NCZMAP));
memset(m, 0, sizeof(NCZMAP));
m->format = NCZM_UNDEF, m->url = strdup("mockedmap");
m->mode = NC_NOWRITE;

m->api = calloc(1, sizeof(NCZMAP_API));
memset(m->api, 0, sizeof(NCZMAP_API));
m->api->version = 0;
m->api->close = (int (*)(NCZMAP*, int)) mockedunimplemented;
m->api->exists = (int (*)(NCZMAP*, const char *)) mockedunimplemented;
m->api->len = mockedlen;
m->api->read = mockedread;
m->api->write = (int (*)(NCZMAP*, const char*, size64_t, const void *))mockedunimplemented;
m->api->search = (int (*)(NCZMAP*, const char*, struct NClist*))mockedunimplemented;

return m;
}

void freemockmap(NCZMAP *map) {
free(map->api);
free(map);
}

// Tests

int test_default_handler_selection() {
fprintf(stderr, "Running %-45s ... ", __func__);
NCZ_FILE_INFO_T zinfo = {0};
memset(&zinfo, 0, sizeof(NCZ_FILE_INFO_T));
NCZMD_set_metadata_handler(&zinfo);
int result = !cmpHandlers(&zinfo.metadata, NCZ_metadata_handler2);
fprintf(stderr, "(%s)\n", result ? "FAILED" : "OK");
if (result)
showHandlers(&zinfo.metadata, NCZ_metadata_handler2);
return result;
}

int test_consolidated_handler_selection_env() {
fprintf(stderr, "Running %-45s ... ", __func__);
NCZ_FILE_INFO_T zinfo = {0};
memset(&zinfo, 0, sizeof(NCZ_FILE_INFO_T));
char *consolidated_env = getenv(NCZARR_CONSOLIDATED_ENV);
putenv(NCZARR_CONSOLIDATED_ENV "=True");
zinfo.map = mockmap();
NCZMD_set_metadata_handler(&zinfo);
freemockmap(zinfo.map);
if (consolidated_env == NULL) {
#if _WIN32
_putenv_s(NCZARR_CONSOLIDATED_ENV, "");
#else
unsetenv(NCZARR_CONSOLIDATED_ENV);
#endif
} else {
char buf[256] = {0};
memcpy(&buf, NCZARR_CONSOLIDATED_ENV "=", sizeof(NCZARR_CONSOLIDATED_ENV));
memcpy(&buf + sizeof(NCZARR_CONSOLIDATED_ENV "="), consolidated_env,
strlen(consolidated_env));
putenv(buf);
}
int result = !cmpHandlers(&zinfo.metadata, NCZ_csl_metadata_handler2);
fprintf(stderr, "(%s)\n", result ? "FAILED" : "OK");
if (result)
showHandlers(&zinfo.metadata, NCZ_csl_metadata_handler2);
return result;
}

int test_consolidated_handler_selection_controls() {
fprintf(stderr, "Running %-45s ... ", __func__);
NCZ_FILE_INFO_T zinfo = {0};
memset(&zinfo, 0, sizeof(NCZ_FILE_INFO_T));
zinfo.map = mockmap();
zinfo.controls.flags = FLAG_CONSOLIDATED;
const char * e = getenv(NCZARR_CONSOLIDATED_ENV);
e = e == NULL ? "NULL" : e;
printf("CONTROLS: 0x%llx & 0x%x => (%lld) << (%s) \n", zinfo.controls.flags, FLAG_CONSOLIDATED, (&zinfo)->controls.flags & FLAG_CONSOLIDATED,e );
NCZMD_set_metadata_handler(&zinfo);
freemockmap(zinfo.map);
int result = !cmpHandlers(&zinfo.metadata, NCZ_csl_metadata_handler2);
fprintf(stderr, "(%s)\n", result ? "FAILED" : "OK");
if (result)
fprintf(stderr, "Result vs Expected\n");
showHandlers(&zinfo.metadata, NCZ_csl_metadata_handler2);
return result;
}

int test_consolidated_handler_selection_fallback() {
fprintf(stderr, "Running %-45s ... ", __func__);
NCZ_FILE_INFO_T zinfo = {0};
memset(&zinfo, 0, sizeof(NCZ_FILE_INFO_T));
zinfo.map = mockmap();
zinfo.map->api->read = (int (*)(NCZMAP*, const char*, size64_t, size64_t, void *))mockedunimplemented;
zinfo.controls.flags = FLAG_CONSOLIDATED;
NCZMD_set_metadata_handler(&zinfo);
freemockmap(zinfo.map);
int result = !cmpHandlers(&zinfo.metadata, NCZ_metadata_handler2);
fprintf(stderr, "(%s)\n", result ? "FAILED" : "OK");
if (result)
showHandlers(&zinfo.metadata, NCZ_metadata_handler2);
return result;
}

int main() {
int stat = 0;

if ((stat = test_default_handler_selection())) {
fprintf(stderr, "Unexpected default selection of metadata handler\n");
return 1;
}

if ((stat = test_consolidated_handler_selection_env())) {
fprintf(stderr, "Unexpected selection of metadata handler, should have "
"been consolidated, via env\n");
return 2;
}

if ((stat = test_consolidated_handler_selection_controls())) {
fprintf(stderr, "Unexpected selection of metadata handler, should have "
"been consolidated, via controls\n");
return 3;
}

if ((stat = test_consolidated_handler_selection_fallback())) {
fprintf(stderr, "Unexpected selection of metadata handler, should have "
"been default, not consolidated!\n");
return 4;
}

}
Loading