Execution Order

Now that we have all the logic defined, we need to specify the order of which the stage steps execute in. The exec_order list is the variable used to specify this and will be read first-to-last. The list should contain the method names we wrote in the previous sections. Any methods not in the list will not execute. If the list contains an invalid name (a method that does not exist) a test will fail letting you know.

In this example the methods will execute in the following order:

  1. delete_boot_variable

  2. configure_boot_variable

  3. set_configuration_register

  4. write_memory

  5. verify_boot_variable

  6. verify_configuration_register

Note

This documentation did not show the development of the remaining steps, though, they have been added for completeness. Please note that we have also updated the exec_order to accommodate the new steps.

  1from genie.metaparser.util.schemaengine import Optional
  2from genie.libs.clean import BaseStage
  3
  4class ChangeBootVariable(BaseStage):
  5
  6    # ============
  7    # Stage Schema
  8    # ============
  9    schema = {
 10        Optional('images'): list,
 11        Optional('timeout'): int,
 12        Optional('config_register'): str,
 13        Optional('current_running_image'): bool,
 14    }
 15
 16    # =================
 17    # Argument Defaults
 18    # =================
 19    TIMEOUT = 300
 20    CONFIG_REGISTER = '0x2102'
 21    CURRENT_RUNNING_IMAGE = False
 22
 23    # ==============================
 24    # Execution order of Stage steps
 25    # ==============================
 26    exec_order = [
 27        'delete_boot_variable',
 28        'configure_boot_variable',
 29        'set_configuration_register',
 30        'write_memory',
 31        'verify_boot_variable',
 32        'verify_configuration_register'
 33    ]
 34
 35    def delete_boot_variable(self, steps, device):
 36
 37        with steps.start("Delete any configure boot variables") as step:
 38
 39            try:
 40                device.configure("no boot system")
 41            except Exception as e:
 42                step.failed("Failed to delete configured boot variables",
 43                            from_exception=e)
 44
 45            step.passed("Successfully deleted configured boot variables")
 46
 47    def configure_boot_variable(self, steps, device, images, timeout=TIMEOUT,
 48                                current_running_image=CURRENT_RUNNING_IMAGE):
 49
 50        with steps.start("Set boot variable to images provided for {}".format(
 51                device.name)) as step:
 52
 53            if current_running_image:
 54                log.info("Retrieving and using the running image due to "
 55                         "'current_running_image: True'")
 56
 57                try:
 58                    output = device.parse('show version')
 59                    images = [output['version']['system_image']]
 60                except Exception as e:
 61                    step.failed("Failed to retrieve the running image. Cannot "
 62                                "set boot variables",
 63                                from_exception=e)
 64
 65            try:
 66                device.api.execute_set_boot_variable(
 67                    boot_images=images, timeout=timeout)
 68            except Exception as e:
 69                step.failed("Failed to set boot variables to images provided",
 70                            from_exception=e)
 71            else:
 72                step.passed("Successfully set boot variables to images provided")
 73
 74    def set_configuration_register(self, steps, device,
 75                                   config_register=CONFIG_REGISTER, timeout=TIMEOUT):
 76        with steps.start("Set config register to boot new image on {}".format(
 77                device.name)) as step:
 78
 79            try:
 80                device.api.execute_set_config_register(
 81                    config_register=config_register, timeout=timeout)
 82            except Exception as e:
 83                step.failed("Failed to set config-register",
 84                            from_exception=e)
 85            else:
 86                step.passed("Successfully set config register")
 87
 88    def write_memory(self, steps, device, timeout=TIMEOUT):
 89        with steps.start("Execute 'write memory' on {}".format(device.name)) as step:
 90            try:
 91                device.api.execute_write_memory(timeout=timeout)
 92            except Exception as e:
 93                step.failed("Failed to execute 'write memory'",
 94                            from_exception=e)
 95            else:
 96                step.passed("Successfully executed 'write memory'")
 97
 98    def verify_boot_variable(self, steps, device, images):
 99        with steps.start("Verify next reload boot variables are correctly set "
100                         "on {}".format(device.name)) as step:
101
102            if not device.api.verify_boot_variable(boot_images=images):
103                step.failed("Boot variables are NOT correctly set")
104            else:
105                step.passed("Boot variables are correctly set")
106
107    def verify_configuration_register(self, steps, device,
108                                      config_register=CONFIG_REGISTER):
109        with steps.start("Verify config-register is as expected on {}".format(
110                device.name)) as step:
111
112            if not device.api.verify_config_register(
113                    config_register=config_register, next_reload=True):
114                step.failed("Config-register is not as expected")
115            else:
116                step.passed("Config-register is as expected")