import json, os, pathlib, sqlite3, subprocess, sys, tempfile from contextlib import closing root=pathlib.Path(__file__).resolve().parents[1] sys.path.insert(0,str(root/'nds'/'tools')) import snapshot as s with tempfile.TemporaryDirectory() as t: t=pathlib.Path(t);conf=t/'nwserv.conf';db=t/'directory.sqlite3' data={'tree':'MARS_TREE','organization':'HOME','units':['USERS','SERVERS'],'placements':[{'name':'alice','type':1,'unit':'USERS'}]} conf.write_text('2 mars # server\n1 sys /secret-host-path ikO\n1 DATA /another-private-path\n12 SUPERVISOR root test-secret-password\n') resources=s.parse_resources(conf);assert resources==('MARS',['SYS','DATA']) # Simulate the 0.9.6 layout with no mars_nds_volume table, then upgrade. s.publish(data,db,True) with closing(sqlite3.connect(db)) as c: oldid=c.execute("SELECT id FROM nds_entries WHERE dn='O=HOME'").fetchone()[0] c.execute('DROP TABLE mars_nds_volume');c.commit() cfg=s.publish(data,db,True,resources) def get(dn): with closing(sqlite3.connect(db)) as c:return c.execute('SELECT id FROM nds_entries WHERE dn=?',(dn,)).fetchone() assert get('O=HOME')[0]==oldid v='CN=MARS_SYS.OU=SERVERS.O=HOME';vid=get(v)[0];sid=get('CN=MARS.OU=SERVERS.O=HOME')[0] assert b'secret-host-path' not in db.read_bytes() and b'test-secret-password' not in db.read_bytes() subprocess.run([str(root/'build/test-nds-schema'),str(db)],check=True) cli=[str(root/'build/mars-nds-inspect'),str(db)] assert 'Host Resource Name\tSYS' in subprocess.check_output(cli+['read',v],text=True) assert subprocess.check_output(cli+['compare',v,'Host Server','MARS.SERVERS.HOME'],text=True).strip()=='TRUE' assert len(subprocess.check_output(cli+['list','OU=SERVERS.O=HOME'],text=True).splitlines())==3 assert len(subprocess.check_output(cli+['search','[Root]','Volume'],text=True).splitlines())==2 assert subprocess.run(cli+['read','MISSING'],capture_output=True).returncode!=0 s.publish(data,db,True,resources);assert get(v)[0]==vid and get('CN=MARS.OU=SERVERS.O=HOME')[0]==sid # Verify an actual NCP 104/2 response from the existing C dispatcher. env=dict(os.environ,MARS_NDS_LAB='1',MARS_NDS_LAB_CONFIG=json.dumps(cfg),MARS_NDS_LAB_DB=str(db)) subprocess.run([str(root/'build/test-nds-lab'),'resource',v,str(vid)],env=env,check=True) logged=subprocess.run([str(root/'build/test-nds-lab'),'resource',v,str(vid)],env=dict(env,MARS_NDS_LAB_TRACE='1'),check=True,capture_output=True,text=True) lines=logged.stderr.splitlines();assert len(lines)==14 and 'verb=57 packet_bytes=25 result=unsupported' in logged.stderr assert v not in logged.stderr and 'ALICE' not in logged.stderr print('PASS optional verb-only trace, no names or packet payload') before=db.read_bytes() try:s.publish(dict(data,placements=data['placements']+[{'name':'mars','type':1,'unit':'SERVERS'}]),db,True,resources) except ValueError:pass else:raise AssertionError('collision accepted') assert db.read_bytes()==before for bad in ['2 MARS\n2 DUP\n1 SYS /x','2 MARS\n1 DATA /x','2 MARS\n1 SYS /x\n1 sys /y','2 MARS\n1 SYS','2 MARS\n1 SYS /x\n1 TOOLONG99 /y']: conf.write_text(bad) try:s.parse_resources(conf) except ValueError:pass else:raise AssertionError('invalid resources accepted') s.publish(data,db,False);assert get(v) is None and get('CN=ALICE.OU=USERS.O=HOME') is None print('PASS resource import, no secrets/paths, stable IDs, NCP resolve, local CLI, collisions, opt-in and validation')