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

     1  #!/usr/bin/env python
     2  
     3  """ Assess using charms with lxd-profiles in different deployment scenarios.
     4  """
     5  
     6  from __future__ import print_function
     7  
     8  import argparse
     9  import logging
    10  import os
    11  import sys
    12  
    13  from deploy_stack import (
    14      BootstrapManager,
    15  )
    16  from jujucharm import (
    17      local_charm_path,
    18  )
    19  from utility import (
    20      add_basic_testing_arguments,
    21      configure_logging,
    22      JujuAssertionError,
    23  )
    24  from jujupy.wait_condition import (
    25      AgentsIdle,
    26      WaitForLXDProfileCondition,
    27  )
    28  
    29  __metaclass__ = type
    30  
    31  log = logging.getLogger("assess_lxdprofile_charm")
    32  charm_profile_subordinate = 'lxd-profile-subordinate'
    33  charm_profile_subordinate_path = 'charms/'+charm_profile_subordinate
    34  charm_profile = 'lxd-profile'
    35  charm_profile_path = 'charms/'+charm_profile
    36  charm_ubuntu = 'ubuntu'
    37  charm_ubuntu_path = 'charms/'+charm_ubuntu
    38  
    39  def setup_principal(client, series, charm_principal_path):
    40      charm_sink = local_charm_path(
    41          charm=charm_principal_path,
    42          juju_ver=client.version,
    43          series=series,
    44          repository=os.environ['JUJU_REPOSITORY'])
    45      _, primary = client.deploy(charm_sink, series=series)
    46      client.wait_for(primary)
    47  
    48  def setup_subordinate(client, series, charm_principal, charm_subordinate, charm_subordinate_path):
    49      charm_sink_2 = local_charm_path(
    50          charm=charm_subordinate_path,
    51          juju_ver=client.version,
    52          series=series,
    53          repository=os.environ['JUJU_REPOSITORY'])
    54      _, secondary = client.deploy(charm_sink_2, series=series)
    55      client.juju('add-relation', (charm_principal, charm_subordinate))
    56      client.wait_for_subordinate_units(charm_principal, charm_subordinate)
    57  
    58  def assess_juju_lxdprofile_machine_upgrade(client, charm_principal, verify_principal, unit_num, charm_subordinate):
    59      """ Tests juju status and lxd profiles
    60  
    61      Verify with juju status
    62      Upgrade the both charms
    63      Verify new lxd-profiles in juju status
    64  
    65      Assumes that it's acting on principal unit 0
    66      Assumes only 1 subordinate
    67      """
    68  
    69      lxdprofile_machine_verify(client, charm_principal, verify_principal, unit_num)
    70  
    71      repository = os.environ['JUJU_REPOSITORY']
    72      client.upgrade_charm(charm_principal, repository+"/charms/"+charm_principal)
    73      client.wait_for(AgentsIdle([charm_principal+"/"+unit_num]))
    74  
    75      if verify_principal:
    76          lxdprofile_machine_verify(client, charm_principal, verify_principal,unit_num)
    77  
    78      principal_unit = client.get_status().get_unit(charm_principal+"/"+unit_num)
    79      subordinate_unit = principal_unit['subordinates'].keys()[0]
    80      client.upgrade_charm(charm_subordinate, repository+"/charms/"+charm_subordinate)
    81      client.wait_for(AgentsIdle([subordinate_unit]))
    82  
    83      lxdprofile_machine_verify(client, charm_principal, verify_principal, unit_num)
    84  
    85  def lxdprofile_machine_verify(client, charm_name, verify_principal, unit_num):
    86      """ Checks the status output is the same as derived profile name.
    87  
    88      :param client: Juju client
    89      :param charm_name: LXD Profile name to expect in the output, principal units only
    90      :param verify_principal: Should the principal charm's lxd profile be verified?
    91      :param unit_num: Which unit of the principal charm to check
    92      :return: None
    93      :raises JujuAssertionError: if lxd profile is not appropriately found.
    94      """
    95      status = client.get_status()
    96  
    97      unit_info = status.get_unit(charm_name+"/"+unit_num)
    98      try:
    99          machine_num = unit_info['machine']
   100      except KeyError:
   101          log.warning("lxdprofile_machine_verify called on subordinate {}/{}, invalid".format(charm_name, unit_num))
   102          return
   103  
   104      application_info = status.get_applications()
   105      charm_rev = application_info[charm_name]['charm-rev']
   106      profile_name = "juju-{}-{}-{}".format(client.model_name,charm_name,charm_rev)
   107  
   108      if verify_principal:
   109          client.wait_for(WaitForLXDProfileCondition(machine_num, profile_name))
   110  
   111      # check subordinates, do their profile names match the charm rev?
   112      if 'subordinates' in unit_info:
   113          for key in unit_info['subordinates'].keys():
   114              sub_app_name = key.split('/')[0]
   115              sub_charm_rev = application_info[sub_app_name]['charm-rev']
   116              sub_profile_name = "juju-{}-{}-{}".format(client.model_name,sub_app_name,sub_charm_rev)
   117              client.wait_for(WaitForLXDProfileCondition(machine_num, sub_profile_name))
   118  
   119      log.info("juju machine {} is using {}: verification succeeded".format(machine_num,profile_name))
   120  
   121  def parse_args(argv):
   122      parser = argparse.ArgumentParser(description="Test juju lxd profile deploy.")
   123      add_basic_testing_arguments(parser)
   124      return parser.parse_args(argv)
   125  
   126  def main(argv=None):
   127      args = parse_args(argv)
   128      configure_logging(args.verbose)
   129      bs_manager = BootstrapManager.from_args(args)
   130      with bs_manager.booted_context(args.upload_tools):
   131          client = bs_manager.client
   132  
   133          setup_principal(client, args.series, charm_profile_path)
   134          setup_subordinate(client, args.series, charm_profile, charm_profile_subordinate, charm_profile_subordinate_path)
   135          assess_juju_lxdprofile_machine_upgrade(client, charm_profile, True, "0", charm_profile_subordinate)
   136  
   137          setup_principal(client, args.series, charm_ubuntu_path)
   138          client.juju('add-relation', (charm_ubuntu, charm_profile_subordinate))
   139          client.wait_for_subordinate_units(charm_ubuntu, charm_profile_subordinate)
   140          assess_juju_lxdprofile_machine_upgrade(client, charm_ubuntu, False,"0",  charm_profile_subordinate)
   141  
   142          client.juju('add-unit', charm_profile)
   143          client.juju('add-unit', (charm_profile, '--to', 'lxd'))
   144          client.wait_for_started()
   145          client.wait_for_subordinate_units(charm_profile, charm_profile_subordinate)
   146          assess_juju_lxdprofile_machine_upgrade(client, charm_profile, True, "1", charm_profile_subordinate)
   147      return 0
   148  
   149  if __name__ == '__main__':
   150      sys.exit(main())