def test_realize_readonly() -> None: with setup_storage2('test_realize_readonly') as (T, S): rref1 = realize(instantiate(mkstage, {'a': '1'}, S=S)) try: with open(join(store_rref2path(rref1, S), 'newfile'), 'w') as f: f.write('foo') raise ShouldHaveFailed('No write-protection??') except OSError: pass try: rmtree(store_rref2path(rref1, S)) raise ShouldHaveFailed('No remove-protection??') except OSError: pass def _realize(b: Build): with open(join(build_outpath(b), 'exe'), 'w') as f: f.write('#!/bin/sh\necho "Fooo"') chmod(join(build_outpath(b), 'exe'), S_IWRITE | S_IREAD | S_IEXEC) rref2 = realize( instantiate(mkdrv, Config({}), match_some(), build_wrapper(_realize), S=S)) assert pipe_stdout([join(store_rref2path(rref2,S),'exe')])=='Fooo\n', \ "Did we lost exec permission?"
def test_repl_override(): with setup_storage('test_repl_override'): n1: DRef n2: DRef def _setting(m: Manager) -> DRef: nonlocal n1, n2 n1 = mkstage(m, {'a': '1'}, lambda i, tag: 33) n2 = mkstage(m, {'maman': n1}, lambda i, tag: 42) return n2 clo = instantiate(_setting) rh = repl_realize(clo, force_interrupt=[n1]) assert rh.dref == n1 b = repl_build(rh) with open(join(build_outpath(b), 'artifact'), 'w') as f: f.write('777') repl_continue(b.outgroups, rh=rh) rref = repl_rref(rh) assert rref is not None rrefn1 = store_deref(rref, n1)[Tag('out')] assert tryread(Path(join(store_rref2path(rrefn1), 'artifact'))) == '777'
def test_mkfile() -> None: with setup_storage('test_mkfile'): def _setting(m: Manager, nm) -> DRef: return mkfile(m, Name('foo'), bytes((nm or 'bar').encode('utf-8')), nm) rref1 = realize(instantiate(_setting, None)) with open(join(store_rref2path(rref1), 'foo'), 'r') as f: bar = f.read() assert bar == 'bar' rref2 = realize(instantiate(_setting, 'baz')) with open(join(store_rref2path(rref2), 'baz'), 'r') as f: baz = f.read() assert baz == 'baz' assert rref1 != rref2
def _match(S: SPath, dref: DRef, context: Context) -> Optional[List[RRefGroup]]: # Get the available groups grps = store_rrefs(dref, context, S) # Sort the output groups by the value of artifact values = list( sorted([(maybereadstr( join(store_rref2path(gr[tag_out()], S), 'artifact'), '0', int), gr) for gr in grps], key=lambda x: x[0])) # Return `top-n` matched groups return [tup[1] for tup in values[-nmatch:]] if len(values) > 0 else None
def test_mknode_with_artifacts(d, a) -> None: with setup_storage('test_mknode_with_artifacts'): def _setting(m: Manager) -> DRef: return mknode(m, config_dict=d, artifacts=a) cl = instantiate(_setting) assert len(cl.derivations) == 1 rref = realize(instantiate(_setting)) for nm, val in a.items(): assert isfile(join(store_rref2path(rref),nm)), \ f"RRef {rref} doesn't contain artifact {nm}"
def test_fetchlocal2(): with setup_storage('test_fetclocal') as tmp: mockdata = join(tmp, 'mockdata') with open(mockdata, 'w') as f: f.write('dogfood') wanted_sha256 = pipe_stdout([SHA256SUM, "mockdata"], cwd=tmp).split()[0] rref = realize( instantiate(fetchlocal, path=mockdata, sha256=wanted_sha256, mode='as-is')) assert isrref(rref) assert isfile(join(store_rref2path(rref), 'mockdata')) assert isfile(mklens(rref).out_path.syspath)
def test_fetchlocal(): with setup_storage('test_fetclocal') as tmp: mockdata = join(tmp, 'mockdata') with open(mockdata, 'w') as f: f.write('dogfood') system(f"tar -C '{tmp}' -zcvf {tmp}/mockdata.tar.gz mockdata") wanted_sha256 = pipe_stdout([SHA256SUM, "mockdata.tar.gz"], cwd=tmp).split()[0] rref = realize( instantiate(fetchlocal, path=mockdata + '.tar.gz', filename='validname.tar.gz', sha256=wanted_sha256)) assert isrref(rref) assert isfile(join(store_rref2path(rref), 'mockdata'))
def test_rmdref(): with setup_storage('test_rmdref') as s: clo=instantiate(mkstage, {'a':1}, lambda:42) drefpath=store_dref2path(clo.dref) rref1=realize(clo, force_rebuild=[clo.dref]) rrefpath=store_rref2path(rref1) assert isdir(rrefpath) rmref(rref1) assert not isdir(rrefpath) assert isdir(drefpath) rmref(clo.dref) assert not isdir(drefpath) try: rmref('asdasd') # type:ignore raise ShouldHaveFailed('shoud reject garbage') except AssertionError: pass
def test_path2rref() -> None: with setup_storage2('test_path2rref') as (T, S): s1 = partial(mkstage, config={ 'name': '1', 'promise': [promise, 'artifact'] }) rref1 = realize(instantiate(s1, S=S)) rref2 = path2rref(store_rref2path(rref1, S)) assert rref1 == rref2 l = mksymlink(rref1, S, 'result', S=S) assert path2rref(Path(l)) == rref1 rref3 = path2rref( Path( "/foo/00000000000000000000000000000000-bar/11111111111111111111111111111111" )) assert rref3 == 'rref:11111111111111111111111111111111-00000000000000000000000000000000-bar' for x in [path2rref(Path('')), path2rref(Path('foo'))]: assert x is None
def test_repl_globalHelper(): with setup_storage('test_repl_globalHelper'): n1: DRef n2: DRef def _setting(m: Manager) -> DRef: nonlocal n1, n2 n1 = mkstage(m, {'a': '1'}) n2 = mkstage(m, {'maman': n1}) return n2 rh = repl_realize(instantiate(_setting), force_interrupt=True) assert repl_rref(rh) is None b = repl_build() with open(join(build_outpath(b), 'artifact.txt'), 'w') as f: f.write("Fooo") repl_continueBuild(b) rref = repl_rref(rh) assert rref is not None assert isfile(join(store_rref2path(rref), 'artifact.txt'))
def test_overwrite_realizer() -> None: with setup_storage2('test_overwrite_realizer') as (T, S): n1: DRef n2: DRef n3: DRef n4: DRef def _setting(m: Manager) -> DRef: nonlocal n1, n2, n3, n4 n1 = mkstage(m, {'a': '1'}, lambda i, t: 33) n2 = mkstage(m, {'maman': n1}) n3 = mkstage(m, {'a': '1'}, lambda i, t: 42) assert n1 == n3 return n2 rref_n2 = realize(instantiate(_setting, S=S)) all_drefs = list(alldrefs(S)) assert len(all_drefs) == 2 rref_n3 = store_deref(rref_n2, store_cattrs(rref_n2, S).maman, S)[Tag('out')] assert open(join(store_rref2path(rref_n3, S), 'artifact'), 'r').read() == '42'
def test_shellref(): with setup_storage('test_shellref') as s: with TemporaryDirectory() as tmp: mockshell=join(tmp,'mockshell') with open(mockshell,'w') as f: f.write(f"#!/bin/sh\n") f.write(f"pwd\n") chmod(mockshell, stat(mockshell).st_mode | S_IEXEC) environ['SHELL']=mockshell rref=realize(instantiate(mkstage, {'a':1})) shellref(rref) shellref(rref2dref(rref)) shellref() shell(store_rref2path(rref)) repl_realize(instantiate(mkstage, {'n':1}), force_interrupt=True) b=repl_build() o=build_outpath(b) shell(b) repl_cancelBuild(b) try: shellref('foo') # type:ignore raise ShouldHaveFailed('shellref should reject garbage') except AssertionError: pass