#include "unittest.h"
#include "pmemcommon.h"
#include "set.h"
#include <errno.h>
#include "mocks.h"
#define LOG_PREFIX "ut"
#define LOG_LEVEL_VAR "TEST_LOG_LEVEL"
#define LOG_FILE_VAR "TEST_LOG_FILE"
#define MAJOR_VERSION 1
#define MINOR_VERSION 0
#define SIG "PMEMXXX"
#define MIN_PART ((size_t)(1024 * 1024 * 2))
#define TEST_FORMAT_INCOMPAT_CHECK POOL_FEAT_NOHDRS
size_t Extend_size = MIN_PART * 2;
const char *Open_path = "";
os_off_t Fallocate_len = -1;
size_t Is_pmem_len = 0;
static void
poolset_info(const char *fname, struct pool_set *set, int o)
{
if (o)
UT_OUT("%s: opened: nreps %d poolsize %zu rdonly %d",
fname, set->nreplicas, set->poolsize,
set->rdonly);
else
UT_OUT("%s: created: nreps %d poolsize %zu zeroed %d",
fname, set->nreplicas, set->poolsize,
set->zeroed);
size_t poolsize = SIZE_MAX;
for (unsigned r = 0; r < set->nreplicas; r++) {
struct pool_replica *rep = set->replica[r];
size_t repsize = 0;
UT_OUT(" replica[%d]: nparts %d nhdrs %d repsize %zu "
"is_pmem %d",
r, rep->nparts, rep->nhdrs, rep->repsize, rep->is_pmem);
for (unsigned i = 0; i < rep->nparts; i++) {
struct pool_set_part *part = &rep->part[i];
UT_OUT(" part[%d] path %s filesize %zu size %zu",
i, part->path, part->filesize, part->size);
size_t partsize =
(part->filesize & ~(Ut_mmap_align - 1));
repsize += partsize;
if (i > 0 && (set->options & OPTION_NO_HDRS) == 0)
UT_ASSERTeq(part->size,
partsize - Ut_mmap_align);
}
repsize -= (rep->nhdrs - 1) * Ut_mmap_align;
UT_ASSERTeq(rep->repsize, repsize);
UT_ASSERT(rep->part[0].size >= repsize);
if (rep->repsize < poolsize)
poolsize = rep->repsize;
}
UT_ASSERTeq(set->poolsize, poolsize);
}
static int
mock_options(const char *arg)
{
Open_path = "";
Fallocate_len = -1;
Is_pmem_len = 0;
if (arg[0] != '-' || arg[1] != 'm')
return 0;
switch (arg[2]) {
case 'n':
break;
case 'o':
Open_path = &arg[4];
break;
case 'f':
Fallocate_len = atoll(&arg[4]);
break;
case 'p':
Is_pmem_len = atoll(&arg[4]);
break;
default:
UT_FATAL("unknown mock option: %c", arg[2]);
}
return 1;
}
int
main(int argc, char *argv[])
{
START(argc, argv, "util_poolset");
common_init(LOG_PREFIX, LOG_LEVEL_VAR, LOG_FILE_VAR,
MAJOR_VERSION, MINOR_VERSION);
if (argc < 3)
UT_FATAL("usage: %s cmd minsize [mockopts] "
"setfile ...", argv[0]);
char *fname;
struct pool_set *set;
int ret;
size_t minsize = strtoul(argv[2], &fname, 0);
for (int arg = 3; arg < argc; arg++) {
arg += mock_options(argv[arg]);
fname = argv[arg];
switch (argv[1][0]) {
case 'c':
ret = util_pool_create(&set, fname,
0, minsize, MIN_PART,
SIG, 1, 0, 0, 0, NULL, REPLICAS_ENABLED);
if (ret == -1)
UT_OUT("!%s: util_pool_create", fname);
else {
#ifndef _WIN32
util_poolset_chmod(set, S_IWUSR | S_IRUSR);
#endif
poolset_info(fname, set, 0);
util_poolset_close(set, DO_NOT_DELETE_PARTS);
}
break;
case 'o':
ret = util_pool_open(&set, fname, 0 ,
MIN_PART, SIG, 1, 0, TEST_FORMAT_INCOMPAT_CHECK,
0, NULL, NULL);
if (ret == -1)
UT_OUT("!%s: util_pool_open", fname);
else {
poolset_info(fname, set, 1);
util_poolset_close(set, DO_NOT_DELETE_PARTS);
}
break;
case 'e':
ret = util_pool_open(&set, fname, 0 ,
MIN_PART, SIG, 1, 0, TEST_FORMAT_INCOMPAT_CHECK,
0, NULL, NULL);
UT_ASSERTeq(ret, 0);
void *nptr = util_pool_extend(set, Extend_size);
if (nptr == NULL)
UT_OUT("!%s: util_pool_extend", fname);
else {
poolset_info(fname, set, 1);
}
util_poolset_close(set, DO_NOT_DELETE_PARTS);
break;
}
}
common_fini();
DONE(NULL);
}