feat: add load_primary_config(), myconf_fini(); remove static cf global from myconf.c

- load_primary_config(): resolves conf/current, reads raw bytes, computes
  SHA-256 tag, decodes Cap'n Proto via load_cluster_config_from_file(),
  checks reserved names, sets config_generation, checks staleness
- myconf_fini(): frees conffile and cluster_config (calls free_ClusterConfig)
- Remove static cf global from myconf.c; get_default_conf() now uses a
  local struct cluster_configuration* via load_cluster_config()
- Add load_primary_config_for_test() under XCE_TESTING in config_repl.c
  to test path resolution without triggering FATAL
- Add xce/cluster_def.h include to config_repl.c and test_config_repl.c
  so cluster_t is complete when XCE_TESTING is active
- TDD: test_load_primary_config_missing_returns_error passes (18/18)
This commit is contained in:
Rongsong Shen 2026-05-13 16:06:18 +00:00
parent ce62406f2c
commit cd02cf40d2
4 changed files with 192 additions and 7 deletions

View file

@ -16,6 +16,7 @@
#include "config_repl.h"
#include "myconf.h"
#include "xce/cluster_def.h"
/* Per-test directory helpers — not Unity setUp/tearDown (those are in
* controller_test.c). Each test that needs a temp dir calls cr_setup()
@ -24,6 +25,9 @@
static char g_tmpdir[256];
static char g_conf_dir[300];
/* tmpdir string for load_primary_config tests (no conf/current) */
static char tmpdir[256];
static void cr_setup(void)
{
snprintf(g_tmpdir, sizeof(g_tmpdir), "/tmp/test_cr_%d_%ld",
@ -367,6 +371,44 @@ static void test_sanitise_cn_truncated(void)
printf("test_sanitise_cn_truncated: PASS\n");
}
/* ── load_primary_config (via test-only helper) ── */
#ifdef XCE_TESTING
static void test_load_primary_config_missing_returns_error(void)
{
/* Create a minimal cluster_t with no conf/current file */
cluster_t c;
memset(&c, 0, sizeof(c));
myconf_t myconf;
memset(&myconf, 0, sizeof(myconf));
/* Use a fresh tmpdir with no conf/current */
snprintf(tmpdir, sizeof(tmpdir), "/tmp/test_lpc_%d_%ld",
(int)getpid(), (long)time(NULL));
mkdir(tmpdir, 0700);
myconf.workdir = tmpdir;
myconf.name = "ctrl0";
myconf.conf_keep_n = 2;
myconf.conf_stale_timeout = 86400;
myconf.conffile = NULL;
c.myconf = &myconf;
/* No conf/current exists — must fail (not FATAL in test context) */
int rc = load_primary_config_for_test(&c);
TEST_ASSERT_TRUE(rc < 0);
/* conffile must still be NULL (no heap allocation on failure) */
TEST_ASSERT_NULL(myconf.conffile);
/* Cleanup */
char cmd[512];
snprintf(cmd, sizeof(cmd), "rm -rf %s", tmpdir);
system(cmd);
printf("test_load_primary_config_missing_returns_error: PASS\n");
}
#endif
/* ── dispatch ───────────────────────────────────── */
void test_config_repl(void)
@ -388,4 +430,7 @@ void test_config_repl(void)
RUN_TEST(test_sanitise_cn_printable);
RUN_TEST(test_sanitise_cn_control_chars);
RUN_TEST(test_sanitise_cn_truncated);
#ifdef XCE_TESTING
RUN_TEST(test_load_primary_config_missing_returns_error);
#endif
}