github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/acceptancetests/assess_juju_output.py (about)

     1  #!/usr/bin/env python
     2  """
     3      Deploy a charm and subordinate charm and verify juju-status attribute of
     4      the deployed charms.
     5      Usage:
     6          python assess_juju_output.py
     7  
     8      NOTE: Currently assess_juju_output looks only for "juju-status" attribute
     9      of the deployed application under application-<charm-name>-units
    10  """
    11  
    12  from __future__ import print_function
    13  
    14  import argparse
    15  import logging
    16  import sys
    17  
    18  from deploy_stack import (
    19      BootstrapManager,
    20      )
    21  from utility import (
    22      add_basic_testing_arguments,
    23      configure_logging,
    24      )
    25  from jujucharm import (
    26      local_charm_path,
    27      )
    28  from assess_min_version import (
    29      JujuAssertionError
    30      )
    31  
    32  __metaclass__ = type
    33  
    34  
    35  log = logging.getLogger("assess_juju_output")
    36  
    37  def verify_juju_status_contains_display_name(client_status):
    38      fields_to_test = [
    39          "instance-id",
    40          "display-name",
    41      ]
    42      for field in fields_to_test:
    43          try:
    44              client_status[field]
    45          except KeyError:
    46              err = "juju status excludes {}".format(field)
    47              raise JujuAssertionError(err)
    48  
    49  def verify_juju_status_attribute_of_charm(charm_details):
    50      """Verify the juju-status of the deployed charm
    51  
    52      :param charm_details: Deployed charm application details
    53      """
    54      try:
    55          app_status = charm_details['units']['dummy-sink/0']['juju-status']
    56      except KeyError:
    57          raise JujuAssertionError(
    58              "juju-status for dummy-sink was not found")
    59      if not app_status:
    60          raise JujuAssertionError(
    61              "App status for dummy-sink is not set")
    62  
    63  
    64  def verify_juju_status_attribute_of_subordinate_charm(charm_details):
    65      """Verify the juju-status of deployed subordinate charm
    66  
    67      :param charm_details: Dictionary representing charm application details
    68      """
    69      try:
    70          sub_status = charm_details['units']['dummy-sink/0']['subordinates'][
    71              'dummy-subordinate/0']['juju-status']
    72      except KeyError:
    73          raise JujuAssertionError(
    74              "juju-status for dummy-subordinate was not found")
    75      if not sub_status:
    76          raise JujuAssertionError(
    77              "App status for dummy-subordinate is not set")
    78  
    79  
    80  def deploy_charm_with_subordinate_charm(client, series):
    81      """Deploy dummy-sink charm and dummy-subordinate charm
    82  
    83      :param client: ModelClient object
    84      :param series: String representing charm series
    85      """
    86      token = "canonical"
    87      charm_sink = local_charm_path(
    88          charm='dummy-sink', series=series, juju_ver=client.version)
    89      client.deploy(charm_sink)
    90      client.wait_for_started()
    91      charm_subordinate = local_charm_path(
    92          charm='dummy-subordinate', series=series, juju_ver=client.version)
    93      client.deploy(charm_subordinate)
    94      client.wait_for_started()
    95      client.set_config('dummy-subordinate', {'token': token})
    96      client.juju('add-relation', ('dummy-subordinate', 'dummy-sink'))
    97      client.juju('expose', ('dummy-sink',))
    98      client.wait_for_workloads()
    99  
   100  
   101  def assess_juju_status(client, series):
   102      """Deploy charm and subordinate charm and verify its juju-status attribute
   103  
   104      :param client: ModelClient object
   105      :param series: String representing charm series
   106      """
   107      deploy_charm_with_subordinate_charm(client, series)
   108      status = client.get_status()
   109      verify_juju_status_fields(status)
   110      charm_details = status.get_applications()['dummy-sink']
   111      verify_juju_status_attribute_of_charm(charm_details)
   112      verify_juju_status_attribute_of_subordinate_charm(charm_details)
   113      log.warning("assess juju-status attribute done successfully")
   114  
   115  
   116  def parse_args(argv):
   117      """Parse all arguments."""
   118  
   119      parser = argparse.ArgumentParser(
   120          description="Test juju-status of charm and its subordinate charm")
   121      add_basic_testing_arguments(parser)
   122      return parser.parse_args(argv)
   123  
   124  
   125  def main(argv=None):
   126      args = parse_args(argv)
   127      series = args.series if args.series else 'xenial'
   128      configure_logging(args.verbose)
   129      bs_manager = BootstrapManager.from_args(args)
   130      with bs_manager.booted_context(args.upload_tools):
   131          assess_juju_status(bs_manager.client, series)
   132      return 0
   133  
   134  
   135  if __name__ == '__main__':
   136      sys.exit(main())