def test_overlay_5(self):
        '''
        Overlay same-tagged non-leaf element with new attr where not allowed.

        Same-tagged non-leaf elements are not allowed.
        '''
        mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA)
        mim.load(self.MAIN_XML_FILE, not self.OVERLAY)
        mim.load(self.OVERLAY_XML_FILE, self.OVERLAY)

        # Verify auto_reboot attribute was added to existing element.
        (value, path) = mim.get("/auto_install/ai_instance@name")
        self.assertEquals(value, "firstname",
                          "Error finding original attribute of existing node")
        self.assertEquals(
            path, "/auto_install[1]/ai_instance[1]",
            "Error: incorrect pathname returned when getting" +
            "\"name\" attr")

        (value, path) = mim.get("/auto_install/ai_instance@auto_reboot")
        self.assertEquals(value, "true",
                          "Error adding new attribute to existing node")
        self.assertEquals(
            path, "/auto_install[1]/ai_instance[1]",
            "Error: incorrect pathname returned when getting " +
            "\"auto_reboot\" attr")
 def do_test(self):
     '''
     Load main, then overlay and test.
     '''
     mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA)
     mim.load(self.MAIN_XML_FILE, not self.OVERLAY)
     mim.load(self.OVERLAY_XML_FILE, self.OVERLAY)
     (value, path) = mim.get("/auto_install/ai_instance@name")
     self.assertEquals(path, "/auto_install[1]/ai_instance[1]",
                       "Error: incorrect pathname returned when getting " +
                       "\"name\" attribute")
     self.assertEquals(value, "secondname",
                       "Error changing existing attribute " +
                       "of non-leaf node.")
 def do_test(self):
     '''
     Load main, then overlay and test.
     '''
     mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA)
     mim.load(self.MAIN_XML_FILE, not self.OVERLAY)
     mim.load(self.OVERLAY_XML_FILE, self.OVERLAY)
     (value, path) = mim.get("/auto_install/ai_instance@name")
     self.assertEquals(
         path, "/auto_install[1]/ai_instance[1]",
         "Error: incorrect pathname returned when getting " +
         "\"name\" attribute")
     self.assertEquals(
         value, "secondname",
         "Error changing existing attribute " + "of non-leaf node.")
    def test_overlay_5(self):
        '''
        Overlay same-tagged non-leaf element with new attr where not allowed.

        Same-tagged non-leaf elements are not allowed.
        '''
        mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA)
        mim.load(self.MAIN_XML_FILE, not self.OVERLAY)
        mim.load(self.OVERLAY_XML_FILE, self.OVERLAY)

        # Verify auto_reboot attribute was added to existing element.
        (value, path) = mim.get("/auto_install/ai_instance@name")
        self.assertEquals(value, "firstname",
                          "Error finding original attribute of existing node")
        self.assertEquals(path, "/auto_install[1]/ai_instance[1]",
                          "Error: incorrect pathname returned when getting" +
                          "\"name\" attr")

        (value, path) = mim.get("/auto_install/ai_instance@auto_reboot")
        self.assertEquals(value, "true",
                          "Error adding new attribute to existing node")
        self.assertEquals(path, "/auto_install[1]/ai_instance[1]",
                          "Error: incorrect pathname returned when getting " +
                          "\"auto_reboot\" attr")
    def test_overlay_8(self):
        '''
        Add a new non-leaf element where same-tagged elements are allowed.
        '''
        mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA)
        mim.load(self.MAIN_XML_FILE, not self.OVERLAY)
        mim.load(self.OVERLAY_XML_FILE, self.OVERLAY)

        (value, path) = mim.get("/auto_install/ai_instance/"
                                "software/software_data@action")
        self.assertEquals(value, "install",
                          "Error adding new same-tagged element")

        # Target[2] means a second element was (properly) added.
        self.assertEquals(path, "/auto_install[1]/ai_instance[1]/software[2]" +
                          "/software_data[1]",
                          "Error: incorrect pathname returned when getting " +
                          "\"action\" attr.")
    def test_overlay_8(self):
        '''
        Add a new non-leaf element where same-tagged elements are allowed.
        '''
        mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA)
        mim.load(self.MAIN_XML_FILE, not self.OVERLAY)
        mim.load(self.OVERLAY_XML_FILE, self.OVERLAY)

        (value, path) = mim.get("/auto_install/ai_instance/"
                                "software/software_data@action")
        self.assertEquals(value, "install",
                          "Error adding new same-tagged element")

        # Target[2] means a second element was (properly) added.
        self.assertEquals(
            path, "/auto_install[1]/ai_instance[1]/software[2]" +
            "/software_data[1]",
            "Error: incorrect pathname returned when getting " +
            "\"action\" attr.")
class TestOverlay9(TestMIMOverlayCommon):
    def setUp(self):
        '''
        Create needed files.
        '''
        TestMIMOverlayCommon.setUp(self)

        self.create_starting_file()

        with open(self.OVERLAY_XML_FILE, "w") as ovrl_xml:
            ovrl_xml.write('<auto_install>\n')
            ovrl_xml.write('  <ai_instance>\n')
            ovrl_xml.write('    <software/>\n')
            ovrl_xml.write('  </ai_instance>\n')
            ovrl_xml.write('</auto_install>\n')

    def tearDown(self):
        '''
        Destroy files created for these tests.
        '''
        TestMIMOverlayCommon.tearDown(self)
        self.destroy_starting_file()

        if os.path.exists(self.OVERLAY_XML_FILE):
            os.unlink(self.OVERLAY_XML_FILE)

    def test_overlay_9(self):
        '''
        Add new leaf elem where same-tagged elements are allowed and none exist
        '''
        self.mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA)
        self.mim.load(self.MAIN_XML_FILE, not self.OVERLAY)
        self.mim.load(self.OVERLAY_XML_FILE, self.OVERLAY)

        # Software[1] means a first software element was (properly) added.
        (value, path) = self.mim.get("/auto_install/ai_instance/software")
        self.assertEquals(
            path, "/auto_install[1]/ai_instance[1]/software[1]",
            "Error adding leaf element where like-tagged " +
            "elements are allowed.")
class TestOverlay9(TestMIMOverlayCommon):

    def setUp(self):
        '''
        Create needed files.
        '''
        TestMIMOverlayCommon.setUp(self)

        self.create_starting_file()

        with open(self.OVERLAY_XML_FILE, "w") as ovrl_xml:
            ovrl_xml.write('<auto_install>\n')
            ovrl_xml.write('  <ai_instance>\n')
            ovrl_xml.write('    <software/>\n')
            ovrl_xml.write('  </ai_instance>\n')
            ovrl_xml.write('</auto_install>\n')

    def tearDown(self):
        '''
        Destroy files created for these tests.
        '''
        TestMIMOverlayCommon.tearDown(self)
        self.destroy_starting_file()

        if os.path.exists(self.OVERLAY_XML_FILE):
            os.unlink(self.OVERLAY_XML_FILE)

    def test_overlay_9(self):
        '''
        Add new leaf elem where same-tagged elements are allowed and none exist
        '''
        self.mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA)
        self.mim.load(self.MAIN_XML_FILE, not self.OVERLAY)
        self.mim.load(self.OVERLAY_XML_FILE, self.OVERLAY)

        # Software[1] means a first software element was (properly) added.
        (value, path) = self.mim.get("/auto_install/ai_instance/software")
        self.assertEquals(path, "/auto_install[1]/ai_instance[1]/software[1]",
                          "Error adding leaf element where like-tagged " +
                          "elements are allowed.")
    def test_overlay_4(self):
        '''
        Leaf overlayed where same-tagged elements not allowed

        ... replaces existing element and any subtree from it.
        '''

        mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA)
        mim.load(self.MAIN_XML_FILE, not self.OVERLAY)
        mim.load(self.OVERLAY_XML_FILE, self.OVERLAY)

        # Verify the old element with name attribute is gone.
        self.assertRaises(milib.MimMatchError, mim.get,
            "/auto_install/ai_instance@name")

        # Verify the new element with auto_reboot attribute is present.
        (value, path) = mim.get("/auto_install/ai_instance@auto_reboot")
        self.assertEquals(value, "true",
            "Error adding new element with new attribute")
        self.assertEquals(path, "/auto_install[1]/ai_instance[1]",
                          "Error: incorrect pathname returned when getting " +
                          "\"auto_reboot\" attribute")
    def test_overlay_4(self):
        '''
        Leaf overlayed where same-tagged elements not allowed

        ... replaces existing element and any subtree from it.
        '''

        mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA)
        mim.load(self.MAIN_XML_FILE, not self.OVERLAY)
        mim.load(self.OVERLAY_XML_FILE, self.OVERLAY)

        # Verify the old element with name attribute is gone.
        self.assertRaises(milib.MimMatchError, mim.get,
                          "/auto_install/ai_instance@name")

        # Verify the new element with auto_reboot attribute is present.
        (value, path) = mim.get("/auto_install/ai_instance@auto_reboot")
        self.assertEquals(value, "true",
                          "Error adding new element with new attribute")
        self.assertEquals(
            path, "/auto_install[1]/ai_instance[1]",
            "Error: incorrect pathname returned when getting " +
            "\"auto_reboot\" attribute")
Example #11
0
def _do_aimanifest(argv):
    '''
    Main.  See usage for argv details.
    '''

    usage_str = usage(argv)

    if len(argv) <= 1:
        AIM_LOGGER.error(_("Error: Missing subcommand"))
        print >> sys.stderr, _("Usage:\n") + usage_str
        return errno.EINVAL

    parser = AimOptionParser(usage=usage_str)
    parser.add_option("-i",
                      "--incremental",
                      dest="is_incremental",
                      default=False,
                      action="store_true",
                      help=_("Do not clear data before adding new data"))
    parser.add_option("-r",
                      "--return-path",
                      dest="show_path",
                      default=False,
                      action="store_true",
                      help=_("Return unique path to affected node"))

    (options, args) = parser.parse_args(argv[1:])
    len_args = len(args)
    command = args[0]
    path = args[1] if (len_args > 1) else None
    value = args[2] if (len_args > 2) else None

    cmds_r_option = ["add", "set", "get"]
    cmds_w_value_arg = ["add", "set"]
    cmds_wo_value_arg = ["get", "load"]
    cmds_w_no_args = ["validate"]

    if ((command in cmds_w_value_arg and (len_args < 3))
            or (command in cmds_wo_value_arg and (len_args < 2))):
        parser.error_w_errno(errno.EINVAL, _("missing argument"))
    if ((command in cmds_w_value_arg and (len_args > 3))
            or (command in cmds_wo_value_arg and (len_args > 2))
            or (command in cmds_w_no_args and (len_args > 1))):
        parser.error_w_errno(errno.EINVAL, _("extra arguments given"))
    if (command != "load") and options.is_incremental:
        parser.error_w_errno(errno.EINVAL,
                             _("-i is not applicable for command given"))
    if command not in cmds_r_option and options.show_path:
        parser.error_w_errno(errno.EINVAL,
                             _("-r is not applicable for command given"))

    # Pass AIM_MANIFEST as the output file.
    try:
        mim = ManifestInput(os.environ.get("AIM_MANIFEST"),
                            os.environ.get("AIM_DTD"))
    except (milib.MimError, IOError) as err:
        return (_handle_error(err))

    if (command == "set") or (command == "add"):
        AIM_LOGGER.info(
            _("command:%(mcommand)s, path:%(mpath)s, "
              "value:%(mvalue)s") % {
                  "mcommand": command,
                  "mpath": path,
                  "mvalue": value
              })
        try:
            if command == "set":
                path = mim.set(path, value)
            else:
                path = mim.add(path, value)
            mim.commit(NO_VALIDATE)
        except (milib.MimError, IOError) as err:
            return (_handle_error(err))

        _log_final_status(mim, path)
        if options.show_path:
            # Localization not needed here.
            print path

    elif command == "get":
        AIM_LOGGER.info(
            _("command:%(mcommand)s, path:%(mpath)s") % {
                "mcommand": command,
                "mpath": path
            })
        try:
            (value, path) = mim.get(path)
        except (milib.MimError, IOError) as err:
            return (_handle_error(err))

        if value is None or not len(value):
            value = "\"\""

        AIM_LOGGER.info(
            _("successful: returns value:%(mvalue)s, "
              "path:%(mpath)s") % {
                  "mvalue": value,
                  "mpath": path
              })

        if not options.show_path:
            path = ""

        # Localization not needed here.
        print "%s %s" % (value, path)

    elif (command == "load"):
        # path arg holds the filename
        AIM_LOGGER.info(
            _("command:%(mcommand)s, "
              "incremental:%(mincr)s, file:%(mfile)s") % {
                  "mcommand": command,
                  "mincr": str(options.is_incremental),
                  "mfile": path
              })
        try:
            mim.load(path, options.is_incremental)
            mim.commit(NO_VALIDATE)
        except (milib.MimError, IOError) as err:
            return (_handle_error(err))

        _log_final_status(mim, path)

    elif (command == "validate"):
        AIM_LOGGER.info(_("Command:%s") % command)
        try:
            mim.validate()
            AIM_LOGGER.info(_("Validation successful"))
        except (milib.MimError, IOError) as err:
            return (_handle_error(err))

    else:
        AIM_LOGGER.error(_("Invalid subcommand \"%s\"") % command)
        print >> sys.stderr, _("Usage:\n") + usage_str
        return errno.EINVAL

    return 0  # No errors
def _do_aimanifest(argv):
    '''
    Main.  See usage for argv details.
    '''

    usage_str = usage(argv)

    if len(argv) <= 1:
        AIM_LOGGER.error(_("Error: Missing subcommand"))
        print >> sys.stderr, _("Usage:\n") + usage_str
        return errno.EINVAL

    parser = AimOptionParser(usage=usage_str)
    parser.add_option("-i", "--incremental", dest="is_incremental",
                      default=False, action="store_true",
                      help=_("Do not clear data before adding new data"))
    parser.add_option("-r", "--return-path", dest="show_path", default=False,
                      action="store_true",
                      help=_("Return unique path to affected node"))

    (options, args) = parser.parse_args(argv[1:])
    len_args = len(args)
    command = args[0]
    path = args[1] if (len_args > 1) else None
    value = args[2] if (len_args > 2) else None

    cmds_r_option = ["add", "set", "get"]
    cmds_w_value_arg = ["add", "set"]
    cmds_wo_value_arg = ["get", "load"]
    cmds_w_no_args = ["validate"]

    if ((command in cmds_w_value_arg and (len_args < 3)) or
        (command in cmds_wo_value_arg and (len_args < 2))):
        parser.error_w_errno(errno.EINVAL, _("missing argument"))
    if ((command in cmds_w_value_arg and (len_args > 3)) or
        (command in cmds_wo_value_arg and (len_args > 2)) or
        (command in cmds_w_no_args and (len_args > 1))):
        parser.error_w_errno(errno.EINVAL, _("extra arguments given"))
    if (command != "load") and options.is_incremental:
        parser.error_w_errno(errno.EINVAL,
                     _("-i is not applicable for command given"))
    if command not in cmds_r_option and options.show_path:
        parser.error_w_errno(errno.EINVAL,
                     _("-r is not applicable for command given"))

    # Pass AIM_MANIFEST as the output file.
    try:
        mim = ManifestInput(os.environ.get("AIM_MANIFEST"),
                            os.environ.get("AIM_DTD"))
    except (milib.MimError, IOError) as err:
        return (_handle_error(err))

    if (command == "set") or (command == "add"):
        AIM_LOGGER.info(_("command:%(mcommand)s, path:%(mpath)s, "
                "value:%(mvalue)s") %
                {"mcommand": command, "mpath": path, "mvalue": value})
        try:
            if command == "set":
                path = mim.set(path, value)
            else:
                path = mim.add(path, value)
            mim.commit(NO_VALIDATE)
        except (milib.MimError, IOError) as err:
            return (_handle_error(err))

        _log_final_status(mim, path)
        if options.show_path:
            # Localization not needed here.
            print path

    elif command == "get":
        AIM_LOGGER.info(_("command:%(mcommand)s, path:%(mpath)s") %
                {"mcommand": command, "mpath": path})
        try:
            (value, path) = mim.get(path)
        except (milib.MimError, IOError) as err:
            return (_handle_error(err))

        if value is None or not len(value):
            value = "\"\""

        AIM_LOGGER.info(_("successful: returns value:%(mvalue)s, "
                      "path:%(mpath)s") % {"mvalue": value, "mpath": path})

        if not options.show_path:
            path = ""

        # Localization not needed here.
        print "%s %s" % (value, path)

    elif (command == "load"):
        # path arg holds the filename
        AIM_LOGGER.info(_("command:%(mcommand)s, "
                      "incremental:%(mincr)s, file:%(mfile)s") %
                    {"mcommand": command, "mincr": str(options.is_incremental),
                     "mfile": path})
        try:
            mim.load(path, options.is_incremental)
            mim.commit(NO_VALIDATE)
        except (milib.MimError, IOError) as err:
            return (_handle_error(err))

        _log_final_status(mim, path)

    elif (command == "validate"):
        AIM_LOGGER.info(_("Command:%s") % command)
        try:
            mim.validate()
            AIM_LOGGER.info(_("Validation successful"))
        except (milib.MimError, IOError) as err:
            return (_handle_error(err))

    else:
        AIM_LOGGER.error(_("Invalid subcommand \"%s\"") % command)
        print >> sys.stderr, _("Usage:\n") + usage_str
        return errno.EINVAL

    return 0  # No errors