def test_no_identifying_tag(self): '''Ensure exception thrown if unable to identify manifest type''' myname = "lil_old_me" df.DataFiles.AI_root = MockAIRoot(tag="foobar", name=myname) myfile = "/tmp/fake_manifest" f = open(myfile, "w") f.close() dfiles = df.DataFiles(manifest_file=myfile) os.unlink(myfile) self.assertRaises(SystemExit, df.DataFiles.manifest_name.fget, dfiles)
def test_name_from_filename(self): '''Ensure manifest name from filename set properly''' myfile = "/tmp/file_name" f = open(myfile, "w") f.close() df.DataFiles.AI_root = MockAIRoot(tag="auto_install", name=None) dfiles = df.DataFiles(manifest_file=myfile, manifest_name=None) print "basename(myfile) = %s, dfiles:%s\n" % (os.path.basename(myfile), dfiles.manifest_name) os.unlink(myfile) self.assertEquals(os.path.basename(myfile), dfiles.manifest_name)
def test_name_from_old_manifest(self): '''Ensure manifest name from old style manifest set properly''' attribute_name = "oldstylename" myfile = "/tmp/fake_manifest" f = open(myfile, "w") f.close() df.DataFiles.AI_root = MockAIRoot(tag="ai_manifest", name=attribute_name) dfiles = df.DataFiles(manifest_file=myfile) os.unlink(myfile) self.assertEquals(attribute_name, dfiles.manifest_name)
def test_name_from_attribute_wins(self): '''Ensure manifest name from attribute second highest precedence''' attribute_name = "name_set_by_attribute" df.DataFiles.AI_root = MockAIRoot(tag="auto_install", name=attribute_name) cmdline_name = None myfile = "/tmp/file_name" f = open(myfile, "w") f.close() dfiles = df.DataFiles(manifest_file=myfile, manifest_name=cmdline_name) os.unlink(myfile) self.assertEquals(attribute_name, dfiles.manifest_name)
def parse_options(do_create, cmd_options=None): """ Parse and validate options Args: - do_create (True) or do_update (False) - Optional cmd_options, used for unit testing. Otherwise, cmd line options handled by OptionParser Returns: the DataFiles object populated and initialized Raises: The DataFiles initialization of manifest(s) A/I, SC, SMF looks for many error conditions and, when caught, are flagged to the user via raising SystemExit exceptions. """ if do_create: usage = '\n' + get_create_usage() else: usage = '\n' + get_update_usage() parser = OptionParser(usage=usage) if do_create: parser.add_option("-c", "--criteria", dest="criteria_c", action="append", default=list(), help=_("Criteria: " "<-c criteria=value|range> ..."), metavar="CRITERIA") parser.add_option("-C", "--criteria-file", dest="criteria_file", default=None, help=_("Path to criteria XML file.")) parser.add_option("-d", "--default", dest="set_as_default", default=False, action='store_true', help=_("Set manifest as default ")) parser.add_option("-f", "--file", dest="manifest_path", default=None, help=_("Path to manifest file ")) parser.add_option("-m", "--manifest", dest="manifest_name", default=None, help=_("Name of manifest")) parser.add_option("-n", "--service", dest="service_name", default=None, help=_("Name of install service.")) # Get the parsed options using parse_args(). We know we don't have # args, so we're just grabbing the first item of the tuple returned. options, args = parser.parse_args(cmd_options) if len(args): parser.error(_("Unexpected argument(s): %s" % args)) if not do_create: options.criteria_file = None options.criteria_c = None options.set_as_default = False # options are: # -c criteria=<value/range> ... (create only) # -C XML file with criteria specified (create only) # -d set manifest as default (create only) # -n service name # -f path to manifest file # -m manifest name # check that we got the install service's name and an AI manifest. if options.manifest_path is None or options.service_name is None: parser.error(_("Missing one or more required options.")) logging.debug("options = %s", options) criteria_dict = None if do_create: # check that we aren't mixing -c and -C # Note: -c and -C will be accepted for create, not for update. if options.criteria_c and options.criteria_file: parser.error(_("Options used are mutually exclusive.")) # if we have criteria from cmd line, convert into dictionary if options.criteria_c: try: criteria_dict = criteria_to_dict(options.criteria_c) except ValueError as err: parser.error(err) elif options.criteria_file: if not os.path.exists(options.criteria_file): parser.error( _("Unable to find criteria file: %s") % options.criteria_file) if not config.is_service(options.service_name): raise SystemExit(_("Failed to find service %s") % options.service_name) # Get the service's imagepath. If service is an alias, the # base service's imagepath is obtained. service = AIService(options.service_name) try: image_path = service.image.path except KeyError as err: raise SystemExit( _("Data for service %s is corrupt. Missing " "property: %s\n") % (options.service_name, err)) service_dir = service.config_dir dbname = service.database_path try: files = df.DataFiles(service_dir=service_dir, image_path=image_path, database_path=dbname, manifest_file=options.manifest_path, manifest_name=options.manifest_name, criteria_dict=criteria_dict, criteria_file=options.criteria_file, service_name=options.service_name, set_as_default=options.set_as_default) except (AssertionError, IOError, ValueError) as err: raise SystemExit(err) except (lxml.etree.LxmlError) as err: raise SystemExit(_("Error:\tmanifest error: %s") % err) return (files)