def main(self, argv=None): self.parseArgs(argv) # 1. load policy policy = self.tryThis(Policy, "Reading policy file \"" + self.policyFile + "\"", self.policyFile) # resolve policy file references polLoadDir = self.options.loadPolicy polLoadDesc = polLoadDir if polLoadDir is None: if self.policyFile.find("/") >= 0: polLoadDir = self.policyFile.rpartition("/")[0] polLoadDesc = polLoadDir else: polLoadDir = "" polLoadDesc = "current directory; " \ "try -l DIR or --load-policy-references=DIR" if self.verbose: print("No policy load dir specified; defaulting to " + polLoadDesc) message = "Resolving references in " + self.policyFile \ + ",\n using " + polLoadDesc self.tryThis(policy.loadPolicyFiles, message, polLoadDir, True) # derive short name by trimming off path & suffix shortname = self.policyFile if shortname.endswith(".paf"): shortname = shortname.rpartition(".paf")[0] if shortname.find("/") >= 0: shortname = shortname.rpartition("/")[2] if self.verbose: print("Short name =", shortname) # 2. create a dictionary from it dictionary = Policy() dictionary.set("target", shortname) self.generateDict(policy, dictionary) # TODO: remove commas from lists if self.verbose: print("Generating Dictionary for Policy " + self.policyFile + ":") print("---------------------------Policy---------------------------") print(policy) print("-------------------------Dictionary-------------------------") realDict = Dictionary(dictionary) try: realDict.validate(policy) print("#<?cfg paf dictionary ?>") writer = PAFWriter() writer.write(dictionary) print(writer.toString()) # print(dictionary) except lsst.pex.exceptions.Exception as e: ve = e.args[0] print("Internal error: validation fails against extrapolated dictionary:") print(ve.describe(" - "))
def testChildDef(self): # simple d = Dictionary(self.getTestDictionary("childdef_simple_dictionary.paf")) p = Policy(self.getTestDictionary("childdef_simple_policy_good.paf")) d.validate(p) p = Policy(self.getTestDictionary("childdef_simple_policy_bad.paf")) ve = ValidationError("Dictionary_1.py", 1, "testChildDef") d.validate(p, ve.cpp) self.assertEqual(ve.getErrors("baz"), ValidationError.WRONG_TYPE) # multiple childDefs (DictionaryError) d = Dictionary(self.getTestDictionary("childdef_dictionary_bad_multiple.paf")) self.assertRaiseLCE(DictionaryError, "Multiple childDef", d.validate, "Dictionary specifies unknown types", p) # complex d = Dictionary(self.getTestDictionary("childdef_complex_dictionary.paf")) p = Policy(self.getTestDictionary("childdef_complex_policy_good_1.paf")) d.validate(p) p = Policy(self.getTestDictionary("childdef_complex_policy_good_2.paf")) d.validate(p) p = Policy(self.getTestDictionary("childdef_complex_policy_bad_1.paf")) ve = ValidationError("Dictionary_1.py", 1, "testChildDef") d.validate(p, ve.cpp) self.assertEqual(ve.getErrors("joe"), ValidationError.NOT_AN_ARRAY) self.assertEqual(ve.getErrors("deb"), ValidationError.NOT_AN_ARRAY) self.assertEqual(ve.getErrors("bob"), ValidationError.NOT_AN_ARRAY) self.assertEqual(ve.getErrors("bob.bar"), ValidationError.NOT_AN_ARRAY) self.assertEqual(ve.getErrors("nested.helen.qux"), ValidationError.MISSING_REQUIRED) self.assertEqual(ve.getErrors("nested.marvin.rafael"), ValidationError.TOO_MANY_VALUES) self.assertEqual(ve.getErrors("disallowed.foo"), ValidationError.TOO_MANY_VALUES) self.assertEqual(ve.getParamCount(), 7)
def testChildDef(self): # simple d = Dictionary(self.getTestDictionary("childdef_simple_dictionary.paf")) p = Policy(self.getTestDictionary("childdef_simple_policy_good.paf")) d.validate(p) p = Policy(self.getTestDictionary("childdef_simple_policy_bad.paf")) ve = ValidationError("Dictionary_1.py", 1, "testChildDef") d.validate(p, ve.cpp) self.assert_(ve.getErrors("baz") == ValidationError.WRONG_TYPE) # multiple childDefs (DictionaryError) d = Dictionary(self.getTestDictionary("childdef_dictionary_bad_multiple.paf")) self.assertRaiseLCE(DictionaryError, "Multiple childDef", d.validate, "Dictionary specifies unknown types", p) # complex d = Dictionary(self.getTestDictionary("childdef_complex_dictionary.paf")) p = Policy(self.getTestDictionary("childdef_complex_policy_good_1.paf")) d.validate(p) p = Policy(self.getTestDictionary("childdef_complex_policy_good_2.paf")) d.validate(p) p = Policy(self.getTestDictionary("childdef_complex_policy_bad_1.paf")) ve = ValidationError("Dictionary_1.py", 1, "testChildDef") d.validate(p, ve.cpp) self.assert_(ve.getErrors("joe") == ValidationError.NOT_AN_ARRAY) self.assert_(ve.getErrors("deb") == ValidationError.NOT_AN_ARRAY) self.assert_(ve.getErrors("bob") == ValidationError.NOT_AN_ARRAY) self.assert_(ve.getErrors("bob.bar") == ValidationError.NOT_AN_ARRAY) self.assert_(ve.getErrors("nested.helen.qux") == ValidationError.MISSING_REQUIRED) self.assert_(ve.getErrors("nested.marvin.rafael") == ValidationError.TOO_MANY_VALUES) self.assert_(ve.getErrors("disallowed.foo") == ValidationError.TOO_MANY_VALUES) self.assert_(ve.getParamCount() == 7)
def testSimpleValidate(self): d = Dictionary(self.getTestDictionary("simple_dictionary.paf")) p = Policy(self.getTestDictionary("simple_policy.paf")) ve = ValidationError("Dictionary_1.py", 0, "testSimpleValidate") d.validate(p, ve.cpp) self.assertEqual(ve.getErrors("name"), 0, "no errors in name") self.assertEqual(ve.getErrors("height"), 0, "no errors in height") self.assertEqual(ve.getErrors(), 0, "no errors overall")
def testSimpleValidate(self): d = Dictionary(self.getTestDictionary("simple_dictionary.paf")) p = Policy(self.getTestDictionary("simple_policy.paf")) ve = ValidationError("Dictionary_1.py", 0, "testSimpleValidate") d.validate(p, ve.cpp) self.assert_(ve.getErrors("name") == 0, "no errors in name") self.assert_(ve.getErrors("height") == 0, "no errors in height") self.assert_(ve.getErrors() == 0, "no errors overall")
def testBadDictionary(self): d = Dictionary(self.getTestDictionary("dictionary_bad_policyfile.paf")) self.assertRaiseLCE(DictionaryError, "Illegal type: \"PolicyFile\"", d.makeDef("file_type").getType, "Dictionary specified PolicyFile type") d = Dictionary(self.getTestDictionary("dictionary_bad_unknown_type.paf")) self.assertRaiseLCE(DictionaryError, "Unknown type: \"NotAType\"", d.makeDef("something").getType, "Dictionary specifies unknown types") d = Dictionary(self.getTestDictionary("dictionary_bad_type_type.paf")) self.assertRaiseLCE(DictionaryError, "Expected string", d.makeDef("something").getType, "Expected string \"type\" type") self.assertRaiseLCE(DictionaryError, "property found at bad: min_occurs", Dictionary, "Dictionary has mispelled keyword \"min_occurs\".", self.getTestDictionary("dictionary_bad_keyword.paf")) dbmd = self.getTestDictionary("dictionary_bad_multiple_definitions.paf") self.assertRaiseLCE(DictionaryError, "expected a single", Dictionary, "Dictionary has two 'definitions' sections", dbmd) p = Policy(self.getTestDictionary("values_policy_good_1.paf")) d = Dictionary(self.getTestDictionary("dictionary_bad_multiple_min.paf")) self.assertRaiseLCE(DictionaryError, "Min value for int_ra", d.validate, "Two mins specified.", p) d = Dictionary(self.getTestDictionary("dictionary_bad_multiple_max.paf")) self.assertRaiseLCE(DictionaryError, "Max value for int_ra", d.validate, "Two maxes specified.", p) d = Dictionary(self.getTestDictionary("dictionary_bad_min_wrong_type.paf")) self.assertRaiseLCE(DictionaryError, "Wrong type for int_range_count_type min", d.validate, "Wrong min type.", p) d = Dictionary(self.getTestDictionary("dictionary_bad_max_wrong_type.paf")) self.assertRaiseLCE(DictionaryError, "Wrong type for int_range_count_type max", d.validate, "Wrong max type.", p) # conflict between minOccurs and maxOccurs d = Dictionary(self.getTestDictionary("conflict_occurs_dictionary.paf")) p = Policy(self.getTestDictionary("conflict_occurs_policy_1.paf")) ve = ValidationError("Dictionary_1.py", 1, "testBadDictionary") d.validate(p, ve.cpp) self.assertEqual(ve.getErrors("1to0"), ValidationError.TOO_MANY_VALUES) self.assertEqual(ve.getErrors("2to1"), ValidationError.NOT_AN_ARRAY) self.assertEqual(ve.getParamCount(), 2) p = Policy(self.getTestDictionary("conflict_occurs_policy_2.paf")) ve = ValidationError("Dictionary_1.py", 1, "testBadDictionary") d.validate(p, ve.cpp) self.assertEqual(ve.getErrors("1to0"), ValidationError.MISSING_REQUIRED) self.assertEqual(ve.getErrors("2to1"), ValidationError.TOO_MANY_VALUES) self.assertEqual(ve.getParamCount(), 2)
def testBadDictionary(self): d = Dictionary(self.getTestDictionary("dictionary_bad_policyfile.paf")) self.assertRaiseLCE(DictionaryError, "Illegal type: \"PolicyFile\"", d.makeDef("file_type").getType, "Dictionary specified PolicyFile type") d = Dictionary(self.getTestDictionary("dictionary_bad_unknown_type.paf")) self.assertRaiseLCE(DictionaryError, "Unknown type: \"NotAType\"", d.makeDef("something").getType, "Dictionary specifies unknown types") d = Dictionary(self.getTestDictionary("dictionary_bad_type_type.paf")) self.assertRaiseLCE(DictionaryError, "Expected string", d.makeDef("something").getType, "Expected string \"type\" type") self.assertRaiseLCE(DictionaryError, "property found at bad: min_occurs", Dictionary, "Dictionary has mispelled keyword \"min_occurs\".", self.getTestDictionary("dictionary_bad_keyword.paf")) dbmd = self.getTestDictionary("dictionary_bad_multiple_definitions.paf") self.assertRaiseLCE(DictionaryError, "expected a single", Dictionary, "Dictionary has two 'definitions' sections", dbmd) p = Policy(self.getTestDictionary("values_policy_good_1.paf")) d = Dictionary(self.getTestDictionary("dictionary_bad_multiple_min.paf")) self.assertRaiseLCE(DictionaryError, "Min value for int_ra", d.validate, "Two mins specified.", p) d = Dictionary(self.getTestDictionary("dictionary_bad_multiple_max.paf")) self.assertRaiseLCE(DictionaryError, "Max value for int_ra", d.validate, "Two maxes specified.", p) d = Dictionary(self.getTestDictionary("dictionary_bad_min_wrong_type.paf")) self.assertRaiseLCE(DictionaryError, "Wrong type for int_range_count_type min", d.validate, "Wrong min type.", p) d = Dictionary(self.getTestDictionary("dictionary_bad_max_wrong_type.paf")) self.assertRaiseLCE(DictionaryError, "Wrong type for int_range_count_type max", d.validate, "Wrong max type.", p) # conflict between minOccurs and maxOccurs d = Dictionary(self.getTestDictionary("conflict_occurs_dictionary.paf")) p = Policy(self.getTestDictionary("conflict_occurs_policy_1.paf")) ve = ValidationError("Dictionary_1.py", 1, "testBadDictionary") d.validate(p, ve.cpp) self.assert_(ve.getErrors("1to0") == ValidationError.TOO_MANY_VALUES) self.assert_(ve.getErrors("2to1") == ValidationError.NOT_AN_ARRAY) self.assert_(ve.getParamCount() == 2) p = Policy(self.getTestDictionary("conflict_occurs_policy_2.paf")) ve = ValidationError("Dictionary_1.py", 1, "testBadDictionary") d.validate(p, ve.cpp) self.assert_(ve.getErrors("1to0") == ValidationError.MISSING_REQUIRED) self.assert_(ve.getErrors("2to1") == ValidationError.TOO_MANY_VALUES) self.assert_(ve.getParamCount() == 2)
def testTypeValidation(self): d = Dictionary(self.getTestDictionary("types_dictionary.paf")) self.assertEqual( d.makeDef("undef_type").getType(), Policy.UNDEF, "UNDEF definition type") self.assertEqual( d.makeDef("bool_type").getType(), Policy.BOOL, "BOOL definition type") self.assertEqual( d.makeDef("int_type").getType(), Policy.INT, "INT definition type") self.assertEqual( d.makeDef("double_type").getType(), Policy.DOUBLE, "DOUBLE definition type") self.assertEqual( d.makeDef("string_type").getType(), Policy.STRING, "STRING definition type") self.assertEqual( d.makeDef("policy_type").getType(), Policy.POLICY, "POLICY definition type") self.assertEqual( d.makeDef("file_type").getType(), Policy.POLICY, "POLICY definition type (substituted for PolicyFile)") p = Policy(self.getTestDictionary("types_policy_good.paf")) ve = ValidationError("Dictionary_1.py", 0, "testTypeValidation") d.validate(p, ve.cpp) self.assertEqual(ve.getErrors("file_type"), ValidationError.NOT_LOADED, "require loadPolicyFiles before validating") self.assertEqual(ve.getErrors("undef_file"), ValidationError.NOT_LOADED, "require loadPolicyFiles before validating") self.assertEqual(ve.getErrors(), ValidationError.NOT_LOADED, "no other errors") self.assertEqual(ve.getParamCount(), 2, "no other errors") p.loadPolicyFiles(self.getTestDictionary(), True) ve = ValidationError("Dictionary_1.py", 0, "testTypeValidation") d.validate(p, ve.cpp) self.assertEqual(ve.getErrors("undef_type"), 0, "no errors with undef") self.assertEqual(ve.getErrors("int_type"), 0, "no errors with int") self.assertEqual(ve.getErrors("double_type"), 0, "no errors with double") self.assertEqual(ve.getErrors("bool_type"), 0, "no errors with bool") self.assertEqual(ve.getErrors("string_type"), 0, "no errors with string") self.assertEqual(ve.getErrors("policy_type"), 0, "no errors with policy") self.assertEqual(ve.getErrors("file_type"), 0, "no errors with file") self.assertEqual(ve.getErrors(), 0, "no errors overall")
def testTypeValidation(self): d = Dictionary(self.getTestDictionary("types_dictionary.paf")) self.assert_(d.makeDef("undef_type") .getType() == Policy.UNDEF, "UNDEF definition type") self.assert_(d.makeDef("bool_type") .getType() == Policy.BOOL, "BOOL definition type") self.assert_(d.makeDef("int_type") .getType() == Policy.INT, "INT definition type") self.assert_(d.makeDef("double_type").getType() == Policy.DOUBLE, "DOUBLE definition type") self.assert_(d.makeDef("string_type").getType() == Policy.STRING, "STRING definition type") self.assert_(d.makeDef("policy_type").getType() == Policy.POLICY, "POLICY definition type") self.assert_(d.makeDef("file_type").getType() == Policy.POLICY, "POLICY definition type (substituted for PolicyFile)") p = Policy(self.getTestDictionary("types_policy_good.paf")) ve = ValidationError("Dictionary_1.py", 0, "testTypeValidation") d.validate(p, ve.cpp) self.assert_(ve.getErrors("file_type") == ValidationError.NOT_LOADED, "require loadPolicyFiles before validating") self.assert_(ve.getErrors("undef_file") == ValidationError.NOT_LOADED, "require loadPolicyFiles before validating") self.assert_(ve.getErrors() == ValidationError.NOT_LOADED, "no other errors") self.assert_(ve.getParamCount() == 2, "no other errors") p.loadPolicyFiles(self.getTestDictionary(), True) ve = ValidationError("Dictionary_1.py", 0, "testTypeValidation") d.validate(p, ve.cpp) self.assert_(ve.getErrors("undef_type") == 0, "no errors with undef") self.assert_(ve.getErrors("int_type") == 0, "no errors with int") self.assert_(ve.getErrors("double_type") == 0, "no errors with double") self.assert_(ve.getErrors("bool_type") == 0, "no errors with bool") self.assert_(ve.getErrors("string_type") == 0, "no errors with string") self.assert_(ve.getErrors("policy_type") == 0, "no errors with policy") self.assert_(ve.getErrors("file_type") == 0, "no errors with file") self.assert_(ve.getErrors() == 0, "no errors overall")
def testNested(self): self.assertRaiseLCE(DictionaryError, "policy_bad_subdef.dictionary is a string", Dictionary, "Malformed subdictionary", self.getTestDictionary("nested_dictionary_bad_1.paf")) p = Policy(self.getTestDictionary("nested_policy_good.paf")) self.assertRaiseLCE(DictionaryError, "Unknown Dictionary property", Dictionary, "Malformed subdictionary", self.getTestDictionary("nested_dictionary_bad_2.paf")) d = Dictionary(self.getTestDictionary("nested_dictionary_good.paf")) d.check() self.assertRaiseLCE(lsst.pex.exceptions.LogicError, "dictionaryFile needs to be loaded", d.validate, "dictionaryFile not loaded", p) self.assertFalse(d.hasSubDictionary("policy_1")) self.assertTrue(d.hasSubDictionary("policy_2")) self.assertFalse(d.hasSubDictionary("policy_load")) n = d.loadPolicyFiles(self.getTestDictionary(), True) self.assertTrue(d.hasSubDictionary("policy_load")) self.assertEqual(n, 1) # number of files loaded d.validate(p) ve = ValidationError("Dictionary_1.py", 1, "testNested") p = Policy(self.getTestDictionary("nested_policy_bad.paf")) d.validate(p, ve.cpp) self.assertEqual(ve.getErrors("policy_1"), ValidationError.WRONG_TYPE) self.assertEqual(ve.getErrors("policy_2.foo"), ValidationError.VALUE_DISALLOWED) self.assertEqual(ve.getErrors("policy_2.bar"), ValidationError.MISSING_REQUIRED) self.assertEqual(ve.getErrors("policy_3.baz.qux"), ValidationError.WRONG_TYPE) self.assertEqual(ve.getErrors("policy_3.baz.paisley"), ValidationError.MISSING_REQUIRED) self.assertEqual(ve.getErrors("policy_3.baz.paisley"), ValidationError.MISSING_REQUIRED) self.assertEqual(ve.getErrors("policy_load.height"), ValidationError.MISSING_REQUIRED) self.assertEqual(ve.getParamCount(), 6) # multiple nesting p = Policy(self.getTestDictionary("nested_policy_1.paf")) n = p.loadPolicyFiles(self.getTestDictionary()) self.assertEqual(n, 3) self.assertEqual(p.getString("1.2b.foo"), "bar") d = Dictionary(self.getTestDictionary("nested_dictionary_1.paf")) n = d.loadPolicyFiles(self.getTestDictionary()) self.assertEqual(n, 3) p = Policy(True, d) # load from defaults self.assertEqual(p.getString("1.2a.foo"), "bar") self.assertEqual(p.getString("1.2b.foo"), "bar") # error in child d = Dictionary(self.getTestDictionary("nested_dictionary_bad_child.paf")) d.loadPolicyFiles(self.getTestDictionary()) # this should really be caught during loadPolicyFiles(), above self.assertRaiseLCE(DictionaryError, "Unknown type: \"NotAType\"", d.makeDef("sub.something").getType, "Loaded sub-dictionary specified a bogus type")
def testValues(self): d = Dictionary(self.getTestDictionary("values_dictionary.paf")) d.check() p = Policy(self.getTestDictionary("values_policy_good_1.paf")) ve = ValidationError("Dictionary_1.py", 0, "testValues") d.validate(p, ve.cpp) self.assertEqual(ve.getParamCount(), 0) # policy: disallow allowed, min, max p = Policy(self.getTestDictionary("values_policy_bad_policy_set.paf")) ve = ValidationError("Dictionary_1.py", 1, "testValues") d.validate(p, ve.cpp) self.assertEqual(ve.getErrors("policy_set_type"), ValidationError.VALUE_DISALLOWED) p = Policy(self.getTestDictionary("values_policy_bad_policy_max.paf")) ve = ValidationError("Dictionary_1.py", 2, "testValues") d.validate(p, ve.cpp) self.assertEqual(ve.getErrors("policy_max_type"), ValidationError.VALUE_OUT_OF_RANGE) p = Policy(self.getTestDictionary("values_policy_bad_policy_min.paf")) ve = ValidationError("Dictionary_1.py", 3, "testValues") d.validate(p, ve.cpp) self.assertEqual(ve.getErrors("policy_min_type"), ValidationError.VALUE_OUT_OF_RANGE) # minOccurs/maxOccurs p = Policy(self.getTestDictionary("values_policy_bad_occurs.paf")) ve = ValidationError("Dictionary_1.py", 1, "testValues") d.validate(p, ve.cpp) self.assertEqual(ve.getParamCount(), 6) self.assertEqual(ve.getErrors("bool_set_count_type"), ValidationError.TOO_MANY_VALUES) self.assertEqual(ve.getErrors("int_range_count_type"), ValidationError.MISSING_REQUIRED) self.assertEqual(ve.getErrors("double_range_count_type"), ValidationError.TOO_MANY_VALUES) self.assertEqual(ve.getErrors("string_count_type"), ValidationError.ARRAY_TOO_SHORT) self.assertEqual(ve.getErrors("disallowed"), ValidationError.TOO_MANY_VALUES) self.assertEqual(ve.getErrors("policy_count_type"), ValidationError.TOO_MANY_VALUES) # min p = Policy(self.getTestDictionary("values_policy_bad_min.paf")) ve = ValidationError("Dictionary_1.py", 1, "testValues") d.validate(p, ve.cpp) self.assertEqual(ve.getParamCount(), 4) self.assertEqual(ve.getErrors(), ValidationError.VALUE_OUT_OF_RANGE) self.assertEqual(ve.getErrors("string_count_type"), ValidationError.OK) self.assertEqual(ve.getErrors("policy_count_type"), ValidationError.OK) # max p = Policy(self.getTestDictionary("values_policy_bad_max.paf")) ve = ValidationError("Dictionary_1.py", 1, "testValues") d.validate(p, ve.cpp) self.assertEqual(ve.getParamCount(), 4) self.assertEqual(ve.getErrors(), ValidationError.VALUE_OUT_OF_RANGE) self.assertEqual(ve.getErrors("string_count_type"), ValidationError.OK) self.assertEqual(ve.getErrors("policy_count_type"), ValidationError.OK) # allowed p = Policy(self.getTestDictionary("values_policy_bad_allowed.paf")) ve = ValidationError("Dictionary_1.py", 1, "testValues") d.validate(p, ve.cpp) self.assertEqual(ve.getParamCount(), 4) self.assertEqual(ve.getErrors(), ValidationError.VALUE_DISALLOWED) self.assertEqual(ve.getErrors("int_range_count_type"), ValidationError.OK) self.assertEqual(ve.getErrors("string_count_type"), ValidationError.OK) self.assertEqual(ve.getErrors("policy_count_type"), ValidationError.OK) # allowed & min/max p = Policy(self.getTestDictionary("values_policy_bad_allowedminmax.paf")) ve = ValidationError("Dictionary_1.py", 1, "testValues") d.validate(p, ve.cpp) self.assertEqual(ve.getParamCount(), 2) self.assertEqual(ve.getErrors("int_range_set_type"), int(ValidationError.VALUE_DISALLOWED) + int(ValidationError.VALUE_OUT_OF_RANGE)) self.assertEqual(ve.getErrors("double_range_count_type"), int(ValidationError.TOO_MANY_VALUES) + int(ValidationError.VALUE_OUT_OF_RANGE)) ve = ValidationError("Dictionary_1.py", 1, "testValues") d.validate(p, ve.cpp)
def testWrongType(self): d = Dictionary(self.getTestDictionary("types_dictionary.paf")) p = Policy(self.getTestDictionary("types_policy_bad_bool.paf")) ve = ValidationError("Dictionary_1.py", 0, "testWrongType") d.validate(p, ve.cpp) self.assertEqual(ve.getErrors(), ValidationError.WRONG_TYPE, "wrong type") self.assertEqual(ve.getParamCount(), 5, "number of errors") self.assertEqual(ve.getErrors("bool_type"), 0, "correct type") p = Policy(self.getTestDictionary("types_policy_bad_int.paf")) ve = ValidationError("Dictionary_1.py", 1, "testWrongType") d.validate(p, ve.cpp) self.assertEqual(ve.getErrors(), ValidationError.WRONG_TYPE, "wrong type") self.assertEqual(ve.getParamCount(), 5, "number of errors") self.assertEqual(ve.getErrors("int_type"), 0, "correct type") self.assertEqual(ve.getErrors("double_type"), ValidationError.WRONG_TYPE, "int can't pass as double") p = Policy(self.getTestDictionary("types_policy_bad_double.paf")) ve = ValidationError("Dictionary_1.py", 2, "testWrongType") d.validate(p, ve.cpp) self.assertEqual(ve.getErrors(), ValidationError.WRONG_TYPE, "wrong type") self.assertEqual(ve.getParamCount(), 5, "number of errors") self.assertEqual(ve.getErrors("double_type"), 0, "correct type") p = Policy(self.getTestDictionary("types_policy_bad_string.paf")) ve = ValidationError("Dictionary_1.py", 3, "testWrongType") d.validate(p, ve.cpp) self.assertEqual(ve.getErrors(), ValidationError.WRONG_TYPE, "wrong type") self.assertEqual(ve.getParamCount(), 5, "number of errors") self.assertEqual(ve.getErrors("string_type"), 0, "correct type") p = Policy(self.getTestDictionary("types_policy_bad_policy.paf")) ve = ValidationError("Dictionary_1.py", 4, "testWrongType") d.validate(p, ve.cpp) self.assertEqual(ve.getErrors(), ValidationError.WRONG_TYPE, "wrong type") self.assertEqual(ve.getParamCount(), 4, "number of errors") self.assertEqual(ve.getErrors("policy_type"), 0, "correct type") self.assertEqual(ve.getErrors("file_type"), 0, "correct type") p = Policy(self.getTestDictionary("types_policy_bad_file.paf")) ve = ValidationError("Dictionary_1.py", 5, "testWrongType") d.validate(p, ve.cpp) self.assertEqual(ve.getErrors(), ValidationError.NOT_LOADED, "not loaded") self.assertEqual(ve.getParamCount(), 6, "number of errors") self.assertEqual(ve.getErrors("bool_type"), ValidationError.NOT_LOADED, "not loaded") self.assertEqual(ve.getErrors("file_type"), ValidationError.NOT_LOADED, "not loaded") self.assertEqual(ve.getErrors("policy_type"), ValidationError.NOT_LOADED, "not loaded") p.loadPolicyFiles(self.getTestDictionary(), True) ve = ValidationError("Dictionary_1.py", 6, "testWrongType") d.validate(p, ve.cpp) self.assertEqual(ve.getErrors(), ValidationError.WRONG_TYPE, "wrong type") self.assertEqual(ve.getErrors("file_type"), 0, "correct type") self.assertEqual(ve.getErrors("policy_type"), 0, "correct type") self.assertEqual(ve.getParamCount(), 4, "wrong type")
def main(self, argv=None): self.parseArgs(argv) # 1. load policy policy = self.tryThis( Policy, "Reading policy file \"" + self.policyFile + "\"", self.policyFile) # resolve policy file references polLoadDir = self.options.loadPolicy polLoadDesc = polLoadDir if polLoadDir is None: if self.policyFile.find("/") >= 0: polLoadDir = self.policyFile.rpartition("/")[0] polLoadDesc = polLoadDir else: polLoadDir = "" polLoadDesc = "current directory; " \ "try -l DIR or --load-policy-references=DIR" if self.verbose: print("No policy load dir specified; defaulting to " + polLoadDesc) message = "Resolving references in " + self.policyFile \ + ",\n using " + polLoadDesc self.tryThis(policy.loadPolicyFiles, message, polLoadDir, True) # derive short name by trimming off path & suffix shortname = self.policyFile if shortname.endswith(".paf"): shortname = shortname.rpartition(".paf")[0] if shortname.find("/") >= 0: shortname = shortname.rpartition("/")[2] if self.verbose: print("Short name =", shortname) # 2. create a dictionary from it dictionary = Policy() dictionary.set("target", shortname) self.generateDict(policy, dictionary) # TODO: remove commas from lists if self.verbose: print("Generating Dictionary for Policy " + self.policyFile + ":") print( "---------------------------Policy---------------------------") print(policy) print( "-------------------------Dictionary-------------------------") realDict = Dictionary(dictionary) try: realDict.validate(policy) print("#<?cfg paf dictionary ?>") writer = PAFWriter() writer.write(dictionary) print(writer.toString()) # print(dictionary) except lsst.pex.exceptions.Exception as e: ve = e.args[0] print( "Internal error: validation fails against extrapolated dictionary:" ) print(ve.describe(" - "))
def testNested(self): self.assertRaiseLCE(DictionaryError, "policy_bad_subdef.dictionary is a string", Dictionary, "Malformed subdictionary", self.getTestDictionary("nested_dictionary_bad_1.paf")) p = Policy(self.getTestDictionary("nested_policy_good.paf")) self.assertRaiseLCE(DictionaryError, "Unknown Dictionary property", Dictionary, "Malformed subdictionary", self.getTestDictionary("nested_dictionary_bad_2.paf")) d = Dictionary(self.getTestDictionary("nested_dictionary_good.paf")) d.check() self.assertRaiseLCE(lsst.pex.exceptions.LogicError, "dictionaryFile needs to be loaded", d.validate, "dictionaryFile not loaded", p) self.assert_(not d.hasSubDictionary("policy_1")) self.assert_(d.hasSubDictionary("policy_2")) self.assert_(not d.hasSubDictionary("policy_load")) n = d.loadPolicyFiles(self.getTestDictionary(), True) self.assert_(d.hasSubDictionary("policy_load")) self.assert_(n == 1) # number of files loaded d.validate(p) ve = ValidationError("Dictionary_1.py", 1, "testNested") p = Policy(self.getTestDictionary("nested_policy_bad.paf")) d.validate(p, ve.cpp) self.assert_(ve.getErrors("policy_1") == ValidationError.WRONG_TYPE) self.assert_(ve.getErrors("policy_2.foo") == ValidationError.VALUE_DISALLOWED) self.assert_(ve.getErrors("policy_2.bar") == ValidationError.MISSING_REQUIRED) self.assert_(ve.getErrors("policy_3.baz.qux") == ValidationError.WRONG_TYPE) self.assert_(ve.getErrors("policy_3.baz.paisley") == ValidationError.MISSING_REQUIRED) self.assert_(ve.getErrors("policy_3.baz.paisley") == ValidationError.MISSING_REQUIRED) self.assert_(ve.getErrors("policy_load.height") == ValidationError.MISSING_REQUIRED) self.assert_(ve.getParamCount() == 6) # multiple nesting p = Policy(self.getTestDictionary("nested_policy_1.paf")) n = p.loadPolicyFiles(self.getTestDictionary()) self.assert_(n == 3) self.assert_(p.getString("1.2b.foo") == "bar") d = Dictionary(self.getTestDictionary("nested_dictionary_1.paf")) n = d.loadPolicyFiles(self.getTestDictionary()) self.assert_(n == 3) p = Policy(True, d) # load from defaults self.assert_(p.getString("1.2a.foo") == "bar") self.assert_(p.getString("1.2b.foo") == "bar") # error in child d = Dictionary(self.getTestDictionary("nested_dictionary_bad_child.paf")) d.loadPolicyFiles(self.getTestDictionary()) # this should really be caught during loadPolicyFiles(), above self.assertRaiseLCE(DictionaryError, "Unknown type: \"NotAType\"", d.makeDef("sub.something").getType, "Loaded sub-dictionary specified a bogus type")
def testValues(self): d = Dictionary(self.getTestDictionary("values_dictionary.paf")) d.check() p = Policy(self.getTestDictionary("values_policy_good_1.paf")) ve = ValidationError("Dictionary_1.py", 0, "testValues") d.validate(p, ve.cpp) self.assert_(ve.getParamCount() == 0) # policy: disallow allowed, min, max p = Policy(self.getTestDictionary("values_policy_bad_policy_set.paf")) ve = ValidationError("Dictionary_1.py", 1, "testValues") d.validate(p, ve.cpp) self.assert_(ve.getErrors("policy_set_type") == ValidationError.VALUE_DISALLOWED) p = Policy(self.getTestDictionary("values_policy_bad_policy_max.paf")) ve = ValidationError("Dictionary_1.py", 2, "testValues") d.validate(p, ve.cpp) self.assert_(ve.getErrors("policy_max_type") == ValidationError.VALUE_OUT_OF_RANGE) p = Policy(self.getTestDictionary("values_policy_bad_policy_min.paf")) ve = ValidationError("Dictionary_1.py", 3, "testValues") d.validate(p, ve.cpp) self.assert_(ve.getErrors("policy_min_type") == ValidationError.VALUE_OUT_OF_RANGE) # minOccurs/maxOccurs p = Policy(self.getTestDictionary("values_policy_bad_occurs.paf")) ve = ValidationError("Dictionary_1.py", 1, "testValues") d.validate(p, ve.cpp) self.assert_(ve.getParamCount() == 6) self.assert_(ve.getErrors("bool_set_count_type") == ValidationError.TOO_MANY_VALUES) self.assert_(ve.getErrors("int_range_count_type") == ValidationError.MISSING_REQUIRED) self.assert_(ve.getErrors("double_range_count_type") == ValidationError.TOO_MANY_VALUES) self.assert_(ve.getErrors("string_count_type") == ValidationError.ARRAY_TOO_SHORT) self.assert_(ve.getErrors("disallowed") == ValidationError.TOO_MANY_VALUES) self.assert_(ve.getErrors("policy_count_type") == ValidationError.TOO_MANY_VALUES) # min p = Policy(self.getTestDictionary("values_policy_bad_min.paf")) ve = ValidationError("Dictionary_1.py", 1, "testValues") d.validate(p, ve.cpp) self.assert_(ve.getParamCount() == 4) self.assert_(ve.getErrors() == ValidationError.VALUE_OUT_OF_RANGE) self.assert_(ve.getErrors("string_count_type") == ValidationError.OK) self.assert_(ve.getErrors("policy_count_type") == ValidationError.OK) # max p = Policy(self.getTestDictionary("values_policy_bad_max.paf")) ve = ValidationError("Dictionary_1.py", 1, "testValues") d.validate(p, ve.cpp) self.assert_(ve.getParamCount() == 4) self.assert_(ve.getErrors() == ValidationError.VALUE_OUT_OF_RANGE) self.assert_(ve.getErrors("string_count_type") == ValidationError.OK) self.assert_(ve.getErrors("policy_count_type") == ValidationError.OK) # allowed p = Policy(self.getTestDictionary("values_policy_bad_allowed.paf")) ve = ValidationError("Dictionary_1.py", 1, "testValues") d.validate(p, ve.cpp) self.assert_(ve.getParamCount() == 4) self.assert_(ve.getErrors() == ValidationError.VALUE_DISALLOWED) self.assert_(ve.getErrors("int_range_count_type") == ValidationError.OK) self.assert_(ve.getErrors("string_count_type") == ValidationError.OK) self.assert_(ve.getErrors("policy_count_type") == ValidationError.OK) # allowed & min/max p = Policy(self.getTestDictionary("values_policy_bad_allowedminmax.paf")) ve = ValidationError("Dictionary_1.py", 1, "testValues") d.validate(p, ve.cpp) self.assert_(ve.getParamCount() == 2) self.assert_(ve.getErrors("int_range_set_type") == ValidationError.VALUE_DISALLOWED + ValidationError.VALUE_OUT_OF_RANGE) self.assert_(ve.getErrors("double_range_count_type") == ValidationError.TOO_MANY_VALUES + ValidationError.VALUE_OUT_OF_RANGE) ve = ValidationError("Dictionary_1.py", 1, "testValues") d.validate(p, ve.cpp)
def testWrongType(self): d = Dictionary(self.getTestDictionary("types_dictionary.paf")) p = Policy(self.getTestDictionary("types_policy_bad_bool.paf")) ve = ValidationError("Dictionary_1.py", 0, "testWrongType") d.validate(p, ve.cpp) self.assert_(ve.getErrors() == ValidationError.WRONG_TYPE, "wrong type") self.assert_(ve.getParamCount() == 5, "number of errors") self.assert_(ve.getErrors("bool_type") == 0, "correct type") p = Policy(self.getTestDictionary("types_policy_bad_int.paf")) ve = ValidationError("Dictionary_1.py", 1, "testWrongType") d.validate(p, ve.cpp) self.assert_(ve.getErrors() == ValidationError.WRONG_TYPE, "wrong type") self.assert_(ve.getParamCount() == 5, "number of errors") self.assert_(ve.getErrors("int_type") == 0, "correct type") self.assert_(ve.getErrors("double_type") == ValidationError.WRONG_TYPE, "int can't pass as double") p = Policy(self.getTestDictionary("types_policy_bad_double.paf")) ve = ValidationError("Dictionary_1.py", 2, "testWrongType") d.validate(p, ve.cpp) self.assert_(ve.getErrors() == ValidationError.WRONG_TYPE, "wrong type") self.assert_(ve.getParamCount() == 5, "number of errors") self.assert_(ve.getErrors("double_type") == 0, "correct type") p = Policy(self.getTestDictionary("types_policy_bad_string.paf")) ve = ValidationError("Dictionary_1.py", 3, "testWrongType") d.validate(p, ve.cpp) self.assert_(ve.getErrors() == ValidationError.WRONG_TYPE, "wrong type") self.assert_(ve.getParamCount() == 5, "number of errors") self.assert_(ve.getErrors("string_type") == 0, "correct type") p = Policy(self.getTestDictionary("types_policy_bad_policy.paf")) ve = ValidationError("Dictionary_1.py", 4, "testWrongType") d.validate(p, ve.cpp) self.assert_(ve.getErrors() == ValidationError.WRONG_TYPE, "wrong type") self.assert_(ve.getParamCount() == 4, "number of errors") self.assert_(ve.getErrors("policy_type") == 0, "correct type") self.assert_(ve.getErrors("file_type") == 0, "correct type") p = Policy(self.getTestDictionary("types_policy_bad_file.paf")) ve = ValidationError("Dictionary_1.py", 5, "testWrongType") d.validate(p, ve.cpp) self.assert_(ve.getErrors() == ValidationError.NOT_LOADED, "not loaded") self.assert_(ve.getParamCount() == 6, "number of errors") self.assert_(ve.getErrors("bool_type") == ValidationError.NOT_LOADED, "not loaded") self.assert_(ve.getErrors("file_type") == ValidationError.NOT_LOADED, "not loaded") self.assert_(ve.getErrors("policy_type") == ValidationError.NOT_LOADED, "not loaded") p.loadPolicyFiles(self.getTestDictionary(), True) ve = ValidationError("Dictionary_1.py", 6, "testWrongType") d.validate(p, ve.cpp) self.assert_(ve.getErrors() == ValidationError.WRONG_TYPE, "wrong type") self.assert_(ve.getErrors("file_type") == 0, "correct type") self.assert_(ve.getErrors("policy_type") == 0, "correct type") self.assert_(ve.getParamCount() == 4, "wrong type")