code wiki / _hdl_build / nx_consul_registry_test.nx

nx_consul_registry_test.nx source

↩ module page · 61 lines · 3995 B

1// nx_consul_registry_test.nx -- CONSULGATE: proves sovereign service registry + health-gated discovery. 2// Registry: web has 3 instances (PASSING, CRITICAL, PASSING), db 1 (PASSING), cache 1 (WARNING). GREEN iff: 3// discover(web)=2 with the CRITICAL instance EXCLUDED (no traffic to a down node = the core safety property); 4// discover(db)=1; discover(cache)=1 (WARNING still routes by default); discover(unknown)=0; the routable 5// predicate is correct (PASSING+WARNING route, CRITICAL not); and deregister drops an instance immediately. 7/7. 6import "nx_consul_registry.nx" 7import "nx_syscalls.nx" 8 9func cg_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 10func cg_num(v: i64) -> i64 { let b: *u8 = sys_mmap(28); var m: i64=v; if m<0 {m=0-m}; let t: *u8 = sys_mmap(28); var k: i64=0; if m==0 {t[0]=48;k=1}; while m>0 {t[k]=48+(m%10); m=m/10; k=k+1}; var i: i64=0; while i<k {b[i]=t[k-1-i]; i=i+1}; sys_write(1,b,k); return 0 } 11 12func main() -> i64 { 13 cg_puts("=== SOVEREIGN SERVICE REGISTRY + HEALTH-GATED DISCOVERY (Consul-class) ===\n" as *u8) 14 let n: i64 = 5 15 let reg: *i64 = sys_mmap(40) as *i64 16 let names: *i64 = sys_mmap(40) as *i64 17 let healths: *i64 = sys_mmap(40) as *i64 18 // [0]web/PASSING [1]web/CRITICAL(down) [2]web/PASSING [3]db/PASSING [4]cache/WARNING 19 reg[0]=1; names[0]=100; healths[0]=SVC_PASSING 20 reg[1]=1; names[1]=100; healths[1]=SVC_CRITICAL 21 reg[2]=1; names[2]=100; healths[2]=SVC_PASSING 22 reg[3]=1; names[3]=200; healths[3]=SVC_PASSING 23 reg[4]=1; names[4]=300; healths[4]=SVC_WARNING 24 25 let out: *i64 = sys_mmap(64) as *i64 26 let cweb: i64 = cs_discover(reg, names, healths, n, 100, out) 27 // verify the CRITICAL instance (idx 1) is NOT in the web result 28 var crit_excluded: i64 = 1 29 var k: i64 = 0 30 while k < cweb { if out[k] == 1 { crit_excluded = 0 } k = k + 1 } 31 32 let cdb: i64 = cs_discover(reg, names, healths, n, 200, out) 33 let ccache: i64 = cs_discover(reg, names, healths, n, 300, out) 34 let cunk: i64 = cs_discover(reg, names, healths, n, 999, out) 35 36 // deregister web instance idx 2 -> web discovery drops to 1 37 cs_deregister(reg, 2) 38 let cweb2: i64 = cs_discover(reg, names, healths, n, 100, out) 39 40 cg_puts(" discover web=" as *u8); cg_num(cweb); cg_puts(" (critical_excluded=" as *u8); cg_num(crit_excluded) 41 cg_puts(") db=" as *u8); cg_num(cdb); cg_puts(" cache=" as *u8); cg_num(ccache); cg_puts(" unknown=" as *u8); cg_num(cunk) 42 cg_puts(" | after-deregister web=" as *u8); cg_num(cweb2); cg_puts("\n" as *u8) 43 44 let r: *i64 = sys_mmap(8*8) as *i64 45 r[0] = 0; if cweb == 2 { r[0] = 1 } // 2 routable web instances 46 r[1] = 0; if crit_excluded == 1 { r[1] = 1 } // the CRITICAL node is never returned 47 r[2] = 0; if cdb == 1 { r[2] = 1 } // db has 1 48 r[3] = 0; if ccache == 1 { r[3] = 1 } // cache WARNING still routes 49 r[4] = 0; if cunk == 0 { r[4] = 1 } // unknown service -> empty 50 r[5] = 0; if cs_routable(SVC_PASSING) == 1 { if cs_routable(SVC_WARNING) == 1 { if cs_routable(SVC_CRITICAL) == 0 { r[5] = 1 } } } 51 r[6] = 0; if cweb2 == 1 { r[6] = 1 } // deregister drops it immediately 52 53 var pass: i64 = 0; var i: i64 = 0 54 while i < 7 { pass = pass + r[i]; i = i + 1 } 55 cg_puts("----\n passed " as *u8); cg_num(pass); cg_puts("/7\n" as *u8) 56 if pass == 7 { 57 cg_puts("CONSULGATE services=3 instances=5 health_gated_discovery=1 critical_never_routed=1 warning_routes=1 deregister_immediate=1 exceed[sovereign service registry + health-gated discovery = HashiCorp Consul-class; composes toward sovereign DNS + reach-probe health + vault mTLS; bits-up] verdict=GREEN\n" as *u8) 58 sys_exit(0); return 0 59 } 60 cg_puts("CONSULGATE verdict=RED\n" as *u8); sys_exit(1); return 1 61}