def test_clear_section_when_new_configuration_attached(self): cfg = astk.Cfg() cfg.add("mem", astk.Attr("memory", 128)) parent = AS.Elt(self.std.node.add_node("p"), AS.Elt) cparams = aster_s.AstkParams(cfg) params = cparams.build(parent) mem = params.get_elt("mem") self.assert_(mem.node.is_alive()) cfg = astk.Cfg() cfg.add("dbg", astk.Param("debug")) params.attach_cfg(cfg) self.assert_(not mem.node.is_alive())
def test_allow_interactiv_follow_up(self): wdir = self.tmp_dir.add("inter_follow_up") comm = __file__ case = AS.FromComm("c") cfg = AS.Cfg() cfg.add("n", AS.Param("nomjob", "ifup")) cfg.add("m", AS.Param("mode", "batch")) case.use(cfg) case.use_working_dir(wdir) case.use(AS.InteractivFollowUp()) case.use(AS.CommFile(comm)) fname = case.build_export_file().fname res = [ "P nomjob ifup", "P mode batch", "P follow_output yes", "F comm %s D 1" % comm, ] fid = open(fname) expf = fid.read() for line in res: self.assert_(line in expf, "not found : %s" % line) fid.close()
def test_display_astk_parameters_for_solver(self): cfg = astk.Cfg() cfg.add("dbg", astk.Param("debug", "nodebug")) cfg.add("mem", astk.Attr("memory", 128)) cparams = aster_s.AstkParams(cfg) cparams.name = "p" params = cparams.build(self.std) pnode = params.node self.assertEqual(params.read_name(), "p") params.attach_cfg(cfg) dbg = ST.Line(pnode.find_node("dbg")) self.assertEqual(dbg.read_name(), "dbg") self.assertEqual(dbg.read_value(), "nodebug") self.assertEqual(params["dbg"], "nodebug") mem = ST.Line(pnode.find_node("mem")) self.assertEqual(mem.read_value(), 128) self.assertEqual(mem.read_visible_value(), '128') params["mem"] = 256 self.assertEqual(mem.read_value(), 256) self.assertEqual(mem.read_visible_value(), '256') params["dbg"] = "debug" astk_cfg = params.get_cfg() self.assertEqual(astk_cfg["mem"].value, 256) self.assertEqual(astk_cfg["dbg"].value, "debug")
def test_copy_study_case(self): std = self.std cfg = astk.Cfg() case = std.add_case("c") case.use(aster_s.WorkingDir("/tmp/a")) case.use(aster_s.AstkParams(cfg)) case.use(aster_s.CommFile("f")) case.use(aster_s.RemoveRmed()) case._build_results() case.results.use(aster_s.MessFile("f.mess")) copy = case.copy() # New node used self.assertEqual([case.node.entry, copy.node.entry], [node.entry for node in std.node.get_children()]) # Data section self.assert_(copy.data.node.parent is copy.node) comms = copy.get_all(aster_s.CommFile) self.assertEqual(len(comms), 1) self.assertEqual(comms[0].read_fname(), "f") # Astk parameters self.assert_(copy.params.node.parent is copy.node) pcfg = copy.params.get_cfg() self.assert_(pcfg is not cfg) self.assert_(pcfg.equal(cfg)) # Results self.assert_(copy.results.node.parent is copy.node) mess = copy.get_all(aster_s.MessFile) self.assertEqual(len(mess), 1) self.assertEqual(mess[0].read_fname(), "f.mess") # Others self.assert_(copy.get(aster_s.RemoveRmed).read_value())
def test_check_two_configuration_equality(self): cfgs = [AS.Cfg() for idx in range(2)] for cfg in cfgs: cfg.add("v", AS.Param("v", "v1")) cfg.add("p", AS.Attr("p", "p1")) self.assert_(cfgs[0] is not cfgs[1]) self.assert_(cfgs[0].equal(cfgs[1])) cfgs[0]["v"].value = "v2" self.assert_(not cfgs[0].equal(cfgs[1]))
def test_remove_a_parameter_from_many(self): cfg = AS.Cfg() for key in ["a", "b", "c"]: cfg.add(key, AS.Line(key)) lines = cfg.lines() cfg.remove("c") self.assertEqual(cfg.lines(), [lines[0], lines[1]])
def test_allow_to_modify_only_added_parameters(self): cfg = AS.Cfg() cfg.add("aster-version", AS.Line("version", "10.0")) self.assertEqual(cfg["aster-version"].value, "10.0") self.assertRaises(KeyError, cfg.__getitem__, "proc-nb") cfg.add("proc-nb", AS.Line("ncpus")) cfg["proc-nb"].value = 5 self.assertEqual(cfg["proc-nb"].value, 5)
def test_copy_configuration(self): cfg = AS.Cfg() cfg.add("v", AS.Param("v", "v1")) cfg.add("c", AS.Attr("m", "f")) copy = cfg.copy() self.assert_(copy is not cfg) self.assert_(copy.equal(cfg)) self.assert_(cfg["v"] is not copy["v"]) self.assert_(cfg["c"] is not copy["c"])
def test_remove_a_parameter(self): cfg = AS.Cfg() cfg.add("p", AS.Line("proc")) self.assert_(cfg["p"].value is None) cfg.remove("p") try: val = cfg["p"] self.fail() except KeyError, exc: self.assertEqual(exc.args[0], AS.Cfg._mess % "p")
def test_use_comm_file_rep_if_no_rep_given(self): rep = self.tmp_dir.add("no_rep_given") cfg = AS.Cfg() cfg.add("name", AS.Param("nomjob", "test")) cfg.add("mode", AS.Param("mode", "batch")) comm = osp.join(rep, "f.comm") open(comm, "w").close() case = AS.build_case("f") case.use(cfg) case.use(AS.CommFile(comm)) fname = case.build_export_file().fname self.assert_(osp.isfile(fname)) self.assertEqual(osp.dirname(fname), rep) self.assert_(case.export_bld.working_dir is None)
def test_add_study_case_from_comm_file(self): std = self.std comm_file = __file__ med_file = UT.__file__ case = std.add_case("Aster case") self.assertEqual(case.read_name(), "Aster case") # Configuration cfg = case.get(aster_s.AstkParams) dcfg = astk.build_default_cfg() dcfg["name"].value = case.read_name() self.assert_(cfg.get_cfg().equal(dcfg)) astk_cfg = astk.Cfg() cfg = aster_s.AstkParams(astk_cfg) case.use(cfg) self.assert_(case.get(aster_s.AstkParams).get_cfg().equal(astk_cfg)) # Comm and med files Comm = aster_s.CommFile Med = aster_s.MedFile case.use(Comm(comm_file)) case.use(Med(med_file)) med = case.get(Med) self.assertEqual(med.read_fname(), med_file) comm = case.get(Comm) self.assertEqual(comm.read_fname(), comm_file) # Working directory Elt = aster_s.WorkingDir self.assert_(case.get(Elt) is None) case.use(Elt("/tmp/a")) self.assertEqual(case.get(Elt).read_value(), "/tmp/a") # Has rmed result Elt = aster_s.RemoveRmed # XXX should be removed self.assert_(case.get(Elt) is None) case.use(Elt()) self.assert_(case.get(Elt).read_value()) # Has base result Elt = aster_s.HasBaseResult self.assert_(case.get(Elt) is None) case.use(Elt()) self.assert_(case.get(Elt).read_value())
def test_build_astk_case(self): acfg = astk.Cfg() acfg.add("name", astk.Param("n")) comm = __file__ mmed = UT.__file__ case = self.std.add_case("a") case.use(aster_s.AstkParams(acfg)) case.use(aster_s.CommFile(comm)) acs = case.build_astk_case() bld = acs.export_bld self.assert_(bld.working_dir is None) cfg = bld.get(astk.Cfg) self.assert_(cfg.equal(acfg)) self.assertEqual(bld.name, "a") self.assertEqual(bld.get(astk.CommFile).fname, comm) self.assert_(not bld.get(astk.MedFile)) case.use(aster_s.WorkingDir("/tmp/a")) acs = case.build_astk_case() self.assertEqual(acs.export_bld.working_dir, "/tmp/a") case.use(aster_s.MedFile(mmed)) case.write_name("c") acs = case.build_astk_case() bld = acs.export_bld self.assertEqual(bld.name, "c") self.assertEqual(bld.get(astk.MedFile).fname, mmed) case.use(aster_s.HasBaseResult()) acs = case.build_astk_case() bld = acs.export_bld self.assert_(bld.get(astk.Base)) comm_file = case.get(aster_s.CommFile) med_file = case.get(aster_s.MedFile) case = self.std.add_case("d") case.use(aster_s.CommEntry(comm_file.node.entry)) case.use(aster_s.MedEntry(med_file.node.entry)) acs = case.build_astk_case() bld = acs.export_bld self.assertEqual(bld.get(astk.CommFile).fname, comm) self.assertEqual(bld.get(astk.MedFile).fname, mmed)
def test_build_export_file(self): rep = self.tmp_dir.add("build") comm = get_data("forma01a.comm") med = get_data("forma01a.comm") cfg = AS.Cfg() cfg.add("v", AS.Param("version", "STA10.0")) cfg.add("name", AS.Param("nomjob")) cfg.add("m", AS.Param("mode", "bash")) cfg.add("n", AS.Attr("nb-cpu", 2)) bld = AS.ExportFileBuilder("forma01-case") bld.working_dir = rep bld.add(cfg) bld.add(AS.RMessFile()) bld.add(AS.CommFile(comm)) bld.add(AS.MedFile(med)) bld.add(AS.RResuFile()) bld.add(AS.RRMedFile()) bld.add(AS.Base()) fname = bld.write().fname fpat = osp.join(rep, "forma01-case") self.assertEqual(fname, fpat + ".export") res = [ "P version STA10.0", "P nomjob forma01-case", "P mode bash", "A nb-cpu 2", "F comm %s D 1" % comm, "F mmed %s D 20" % med, "F mess %s R 6" % (fpat + ".mess"), "F resu %s R 8" % (fpat + ".resu"), "F rmed %s R 80" % (fpat + ".rmed"), "R base %s RC 0" % (fpat + "base"), ] fid = open(fname) expf = fid.read() for line in res: self.assert_(line in expf, "not found : %s" % line) fid.close()
def test_write_export_file_for_stanley(self): wdir = self.tmp_dir.add("write_stanley_export") basename = "/tmp/aster/base_dir" res = [ "P nomjob stanley", "P mode bash", "P special stanley%%NEXT%%R base %s DC 0" % basename, ] cfg = AS.Cfg() cfg.add("name", AS.Param("nomjob", "stanley")) cfg.add("m", AS.Param("mode", "bash")) case = AS.build_case("run_stanley", AS.RunStanley) case.use_working_dir(wdir) case.use(cfg) case.use(AS.StanleyBase(basename)) exf = case.build_export_file() fid = open(exf.fname) lines = fid.read().splitlines() for line in res: self.assert_(line in lines, line) fid.close()
def test_allow_to_add_only_one_time(self): cfg = AS.Cfg() cfg.add("a", AS.Line("a")) self.assertRaises(KeyError, cfg.add, "a", AS.Line("a"))