Package yakumo :: Package smoketests :: Module st52_attach_volume
[hide private]
[frames] | no frames]

Source Code for Module yakumo.smoketests.st52_attach_volume

  1  #!/usr/bin/env 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  """Compute API Test (Attaching Volumes)""" 
 18   
 19   
 20  import time 
 21  import re 
 22  import yaml 
 23   
 24  from yakumo.smoketest import * 
 25  from yakumo import utils 
 26   
 27   
 28  USER_DATA = r'''#!/bin/sh 
 29  i=0 
 30  while [ $i -lt 30 ]; do 
 31  sleep 5 
 32  echo "BeginSysRepo" 
 33   
 34  CPU=`lscpu | awk '/^CPU.s/ { print $2 }` 
 35  echo "vcpus: $CPU" 
 36  RAM=`free -m | awk '/^Mem:/ { print $2 }` 
 37  echo "ram: $RAM" 
 38   
 39  echo "disks:" 
 40  lsblk -d -n -b | awk '{ print "  " $1 ": { size: " $4 ", type: " $6 " }" }' 
 41   
 42  echo "nics:" 
 43  ip a | sed -re "s/^([0-9])/\n\1/" | awk 'BEGIN { RS="" } 
 44  /^[0-9]: eth/ { gsub(":", "", $2); 
 45  print "  " $2 ": { mac: \"" $11"\", ip: " $15 " }" }' 
 46   
 47  echo "EndSysRepo" 
 48  done 
 49  exit 0 
 50  ''' 
 51   
 52   
 53  REPORT_PATTERN = re.compile(r'''BeginSysRepo\n(.*?)EndSysRepo''', 
 54                              re.MULTILINE | re.DOTALL) 
 55   
 56  KEY_PAIR_NAME = 'key1' 
 57  FLAVOR_NAME = 'm1.small' 
 58  IMAGE_NAME = 'cirros' 
 59  NETWORK_NAME = 'private' 
 60   
 61   
62 -def main(c, key_pair=None, flavor=None, image=None, network=None, **kwargs):
63 64 LOG.debug("key pair: %s", key_pair) 65 LOG.debug("flavor: %s", flavor) 66 LOG.debug("image: %s", image) 67 LOG.debug("network: %s", network) 68 69 LOG.info("Create Volume #1") 70 name = get_random_str('volume') 71 with c.volume.create(name=name, size=1) as v: 72 73 LOG.debug("list volumes: %s", [_.name for _ in c.volume.list()]) 74 75 test("Volume #1 is created", v is not None) 76 77 LOG.debug("wait for created") 78 v.wait_for_finished() 79 80 test("Volume #1 name is " + name, v.name == name) 81 test("Volume #1 is available", v.status == 'available') 82 83 LOG.info("Create Server #1") 84 name = get_random_str('server') 85 with c.server.create(name=name, 86 networks=[network], 87 image=image, 88 flavor=flavor, 89 key_pair=key_pair, 90 user_data=USER_DATA) as s: 91 92 LOG.debug("list servers: %s", [_.name for _ in c.server.list()]) 93 94 LOG.debug("wait for created") 95 s.wait_for_finished() 96 test("Server #1 name is " + name, s.name == name) 97 test("Server #1 is active", s.status == 'ACTIVE') 98 99 def get_guest_stat(cl): 100 match = REPORT_PATTERN.search(cl) 101 if match is None: 102 return 103 return yaml.load(match.group(1))
104 105 for i in range(30): 106 time.sleep(10) 107 cl = s.get_console_log(lines=20) 108 if get_guest_stat(cl): 109 break 110 if 'login:' in cl: 111 raise Exception() 112 else: 113 raise Exception() 114 115 stat = get_guest_stat(cl) 116 LOG.debug("vcpus: %s", stat['vcpus']) 117 LOG.debug("ram: %s", stat['ram']) 118 LOG.debug("nics: %s", stat['nics']) 119 LOG.debug("disks: %s", stat['disks']) 120 121 disks = len(stat['disks']) 122 test("/dev/vdb not found", 'vdb' not in stat['disks']) 123 124 LOG.info("Attach Volume #1") 125 va = s.volume.attach(volume=v) 126 v.wait_for_finished() 127 128 test("Volume #1 is in-use", v.status == 'in-use') 129 130 for i in range(30): 131 time.sleep(10) 132 cl = s.get_console_log(lines=20) 133 stat = get_guest_stat(cl) 134 135 if len(stat['disks']) != disks: 136 break 137 138 test("/dev/vdb exists", 'vdb' in stat['disks']) 139 test("Volume #1 is /dev/vdb", 140 v.size == (stat['disks']['vdb']['size'] / 1024 ** 3)) 141 142 disks = len(stat['disks']) 143 144 LOG.info("Detach Volume #1") 145 va.detach() 146 v.wait_for_finished() 147 148 test("Volume #1 is available", v.status == 'available') 149 150 for i in range(30): 151 time.sleep(10) 152 cl = s.get_console_log(lines=20) 153 stat = get_guest_stat(cl) 154 155 if len(stat['disks']) != disks: 156 break 157 test("/dev/vdb is gone", 'vdb' not in stat['disks']) 158 159 test("Server #1 is deleted", s not in c.server.list()) 160 161 test("Volume #1 is deleted", v not in c.volume.list()) 162 163 164 if __name__ == '__main__': 165 c = utils.get_client() 166 k = c.key_pair.find_one(name=KEY_PAIR_NAME) 167 f = c.flavor.find_one(name=FLAVOR_NAME) 168 i = c.image.find_one(name=IMAGE_NAME) 169 n = c.network.find_one(name=NETWORK_NAME) 170 171 LOG.debug("list servers: %s", [_.name for _ in c.server.list()]) 172 LOG.debug("list volumes: %s", [_.name for _ in c.volume.list()]) 173 main(c, key_pair=k, flavor=f, image=i, network=n) 174 LOG.debug("list servers: %s", [_.name for _ in c.server.list()]) 175 LOG.debug("list volumes: %s", [_.name for _ in c.volume.list()]) 176 177 show_test_summary() 178