1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """Compute API Test (Attaching Interfaces)"""
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 NETWORK2_NAME = 'private2'
61
62
63 -def main(c, key_pair=None, flavor=None, image=None, network=None,
64 network2=None, **kwargs):
65
66 LOG.debug("key pair: %s", key_pair)
67 LOG.debug("flavor: %s", flavor)
68 LOG.debug("image: %s", image)
69 LOG.debug("network: %s", network)
70 LOG.debug("network2: %s", network2)
71
72 LOG.info("Create port #1")
73 name = get_random_str('port')
74 with c.port.create(name=name, network=network2) as p:
75
76 test("Port #1 is created", p is not None)
77
78 LOG.info("Create Server #1")
79 name = get_random_str('server')
80 with c.server.create(name=name,
81 networks=[network],
82 image=image,
83 flavor=flavor,
84 key_pair=key_pair,
85 user_data=USER_DATA) as s:
86
87 LOG.debug("list servers: %s", [_.name for _ in c.server.list()])
88
89 LOG.debug("wait for created")
90 s.wait_for_finished()
91 test("Server #1 name is " + name, s.name == name)
92 test("Server #1 is active", s.status == 'ACTIVE')
93
94 def get_guest_stat(cl):
95 match = REPORT_PATTERN.search(cl)
96 if match is None:
97 return
98 return yaml.load(match.group(1))
99
100 for i in range(30):
101 time.sleep(10)
102 cl = s.get_console_log(lines=20)
103 if get_guest_stat(cl):
104 break
105 if 'login:' in cl:
106 raise Exception()
107 else:
108 raise Exception()
109
110 stat = get_guest_stat(cl)
111 LOG.debug("vcpus: %s", stat['vcpus'])
112 LOG.debug("ram: %s", stat['ram'])
113 LOG.debug("nics: %s", stat['nics'])
114 LOG.debug("disks: %s", stat['disks'])
115
116 eth0_mac = stat['nics']['eth0']['mac']
117 port0_mac = c.port.find_one(device=s).mac_address
118 test("eth0 is a port for Server #1", port0_mac == eth0_mac)
119
120 test("eth1 not found", 'eth1' not in stat['nics'])
121 nics = len(stat['nics'])
122
123 LOG.info("Attach a network")
124 ia = s.interface.attach(network=network2)
125 for i in range(30):
126 time.sleep(10)
127 cl = s.get_console_log(lines=20)
128 stat = get_guest_stat(cl)
129
130 if len(stat['nics']) != nics:
131 break
132
133 test("eth1 exists", 'eth1' in stat['nics'])
134
135 eth1_mac = stat['nics']['eth1']['mac']
136 port1_mac = [_.mac_address for _ in c.port.find(device=s)
137 if _.mac_address != eth0_mac][0]
138 test("eth1 is the new port for Server #1", port1_mac == eth1_mac)
139
140 nics = len(stat['nics'])
141
142 LOG.info("Detach a network")
143 ia.detach()
144 for i in range(30):
145 time.sleep(10)
146 cl = s.get_console_log(lines=20)
147 stat = get_guest_stat(cl)
148
149 if len(stat['nics']) != nics:
150 break
151
152 test("eth1 not found", 'eth1' not in stat['nics'])
153 nics = len(stat['nics'])
154
155 LOG.info("Attach a port")
156 ia = s.interface.attach(port=p)
157 for i in range(30):
158 time.sleep(10)
159 cl = s.get_console_log(lines=20)
160 stat = get_guest_stat(cl)
161
162 if len(stat['nics']) != nics:
163 break
164
165 test("eth1 exists", 'eth1' in stat['nics'])
166 eth1_mac = stat['nics']['eth1']['mac']
167 test("eth1 is Port #1", p.mac_address == eth1_mac)
168
169 nics = len(stat['nics'])
170
171 LOG.info("Detach a port")
172 ia.detach()
173 for i in range(30):
174 time.sleep(10)
175 cl = s.get_console_log(lines=20)
176 stat = get_guest_stat(cl)
177
178 if len(stat['nics']) != nics:
179 break
180
181 test("eth1 not found", 'eth1' not in stat['nics'])
182
183 test("Server #1 is deleted", s not in c.server.list())
184
185 test("Port #1 is deleted", p not in c.port.list())
186
187
188 if __name__ == '__main__':
189 c = utils.get_client()
190 k = c.key_pair.find_one(name=KEY_PAIR_NAME)
191 f = c.flavor.find_one(name=FLAVOR_NAME)
192 i = c.image.find_one(name=IMAGE_NAME)
193 n = c.network.find_one(name=NETWORK_NAME)
194 n2 = c.network.find_one(name=NETWORK2_NAME)
195
196 LOG.debug("list servers: %s", [_.name for _ in c.server.list()])
197 LOG.debug("list ports: %s", [_.name for _ in c.port.list()])
198 main(c, key_pair=k, flavor=f, image=i, network=n, network2=n2)
199 LOG.debug("list servers: %s", [_.name for _ in c.server.list()])
200 LOG.debug("list ports: %s", [_.name for _ in c.port.list()])
201
202 show_test_summary()
203