Skip to content

Instantly share code, notes, and snippets.

@marcaurele
Created August 23, 2018 15:22
Show Gist options
  • Save marcaurele/e842e46076ae318a89b2abce8b55e27a to your computer and use it in GitHub Desktop.
Save marcaurele/e842e46076ae318a89b2abce8b55e27a to your computer and use it in GitHub Desktop.
import asyncio
import hashlib
import io
import os
import time
import asyncssh
import pytest
@pytest.fixture(params=[20])
def nb_vms(request):
return request.param
@pytest.mark.usefixtures("timing")
class TestPerformance:
@pytest.mark.asyncio
async def test_vm_deploy(
self, acs_mgr, cs_zone, template, service_offering, nb_vms
):
started = time.time()
tasks = [
asyncio.ensure_future(
self.deploy_vm(
x, acs_mgr, cs_zone.id, template.id, service_offering.id
)
)
for x in range(nb_vms)
]
results = []
done_t, pending_t = await asyncio.wait(tasks)
assert len(pending_t) == 0
exceptions = 0
for t in done_t:
if t.exception():
exceptions += 1
elif t.result():
results.append(t.result())
print("Startup time: {:0.2f}s".format(time.time() - started))
await asyncio.sleep(30)
print("Check vm meta-data")
tasks = [asyncio.ensure_future(self.check_vm(vm)) for vm in results]
await asyncio.wait(tasks)
print("Destroying all")
tasks = [
acs_mgr.destroyVirtualMachine(id=vm['id'], fetch_result=False)
for vm in results
]
results = await asyncio.wait(tasks)
assert exceptions == 0
async def deploy_vm(
self, counter, cs_mgr, zoneid, templateid, serviceofferingid
):
m = hashlib.md5()
m.update(str(zoneid).encode('ascii'))
m.update(str(templateid).encode('ascii'))
m.update(str(serviceofferingid).encode('ascii'))
m.update(str(time.time()).encode('ascii'))
filename = __file__.split(os.sep)[-1]
name = 'pytest-{}-{}'.format(os.getpid(), m.hexdigest())
displayname = 'py.test {} #{} {}'.format(
filename, os.getpid(), m.hexdigest()
)
vm = await cs_mgr.deployVirtualMachine(
zoneid=zoneid,
serviceofferingid=serviceofferingid,
templateid=templateid,
name=name,
displayname=displayname,
)
vm = vm['virtualmachine']
print(
"VM {:03}: {} is deployed with password {} and ip {}".format(
counter, vm['id'], vm['password'], vm['nic'][0]['ipaddress']
)
)
assert vm['state'] == 'Running'
return vm
async def check_vm(self, vm):
"""Logs into the VO using the password and verfiy a meta-data."""
vm_ip = vm['nic'][0]['ipaddress']
vm_gateway = vm['nic'][0]['gateway']
vm_password = vm['password']
async with asyncssh.connect(
vm_ip, username='ubuntu', known_hosts=None, password=vm_password
) as con:
result = await con.run(
'curl -s http://{}/latest/meta-data/instance-id'.format(
vm_gateway
),
check=True,
)
vm_id = io.StringIO(result.stdout).readline()
assert vm_id == vm['id']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment