Package yakumo :: Module smoketest
[hide private]
[frames] | no frames]

Source Code for Module yakumo.smoketest

  1  #!/usr/bin/python 
  2  # 
  3  # Copyright 2014-2017 by Akira Yoshiyama <akirayoshiyama@gmail.com>. 
  4  # All Rights Reserved. 
  5  # 
  6  #    Licensed under the Apache License, Version 2.0 (the "License"); you may 
  7  #    not use this file except in compliance with the License. You may obtain 
  8  #    a copy of the License at 
  9  # 
 10  #         http://www.apache.org/licenses/LICENSE-2.0 
 11  # 
 12  #    Unless required by applicable law or agreed to in writing, software 
 13  #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 14  #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 15  #    License for the specific language governing permissions and limitations 
 16  #    under the License. 
 17   
 18   
 19  import argparse 
 20  from contextlib import contextmanager 
 21  import logging 
 22  import os 
 23  import os.path 
 24  import random 
 25  import sys 
 26   
 27  import os_client_config 
 28   
 29  from yakumo import Client, utils 
 30   
 31   
 32  __all__ = ('c', 'LOG', 'cleaner', 'get_random_str', 'test', 
 33             'show_test_summary') 
 34   
 35  SOURCE = "abcdefghijklmnopqrstuvwxyz" \ 
 36           "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \ 
 37           "0123456789" 
 38   
 39  TEST_LOGS = [] 
 40   
 41  ENVIRONMENT_VARIABLES = { 
 42      'os_cloud': 'OS_CLOUD', 
 43      'os_cert': 'OS_CERT', 
 44      'os_cacert': 'OS_CACERT', 
 45      'os_region_name': 'OS_REGION_NAME', 
 46      'os_interface': 'OS_INTERFACE', 
 47      'os_key': 'OS_KEY', 
 48      'os_auth_type': 'OS_AUTH_TYPE', 
 49  } 
 50   
 51   
 52  kwargs = {dest: os.environ.get(env) 
 53            for dest, env in ENVIRONMENT_VARIABLES.items()} 
 54  parser = argparse.ArgumentParser() 
 55  cloud_config = os_client_config.OpenStackConfig() 
 56  cloud_config.register_argparse_arguments(parser, sys.argv) 
 57  for opt in parser._actions: 
 58      if opt.dest in ENVIRONMENT_VARIABLES: 
 59          opt.metavar = ENVIRONMENT_VARIABLES[opt.dest] 
 60  parser.set_defaults(timeout=None, insecure=False, **kwargs) 
 61  options = parser.parse_args() 
 62  c = Client(**options.__dict__) 
 63   
 64  LOG = logging.getLogger(os.path.basename(sys.argv[0])) 
 65  LOG.setLevel(logging.DEBUG) 
 66  ch = logging.StreamHandler() 
 67  ch.setLevel(logging.DEBUG) 
 68  formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s') 
 69  ch.setFormatter(formatter) 
 70  LOG.addHandler(ch) 
71 72 73 @contextmanager 74 -def cleaner(resource):
75 try: 76 yield resource 77 except Exception as e: 78 LOG.exception("Error occured: %s", e) 79 TEST_LOGS.append((str(e), False)) 80 finally: 81 if hasattr(resource, 'name'): 82 LOG.info("Delete %s", resource.name) 83 else: 84 LOG.info("Delete %s", resource.get_id()) 85 resource.delete() 86 LOG.debug("waiting for deleted") 87 resource.wait_for_finished()
88
89 90 -def get_random_str(prefix="test"):
91 return prefix + "-" + "".join(random.sample(SOURCE, 10))
92
93 94 -def test(label, condition):
95 TEST_LOGS.append((label, condition)) 96 if condition: 97 LOG.debug("%s ... OK", label) 98 else: 99 LOG.debug("%s ... NG", label)
100
101 102 -def show_test_summary():
103 _ok = 0 104 _ng = 0 105 for label, condition in TEST_LOGS: 106 if condition: 107 _ok += 1 108 else: 109 _ng += 1 110 LOG.info("Test results: OK=%s, NG=%s" % (_ok, _ng)) 111 for label, condition in TEST_LOGS: 112 if not condition: 113 LOG.debug("NG: %s" % label)
114