blob: 460d9000376b6fb0ff3db61ad4617330ef86fbf7 [file] [log] [blame]
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +02001import pytest
2import time
3import subprocess
4import random
5import logging
6
7import utils
8
9logger = logging.getLogger(__name__)
10
11
12def is_parsable(value, to):
13 """
14 Check if value can be converted into some type
15 :param value: input value that should be converted
16 :param to: type of output value like int, float. It's not a string!
17 :return: bool
18 """
19 try:
20 to(value)
21 except:
22 return False
23 return True
24
25
26@pytest.fixture
27def create_image():
28 image_size_megabytes = utils.get_configuration().get("IMAGE_SIZE_MB", 9000)
29 create_file_cmdline = 'dd if=/dev/zero of=/tmp/image_mk_framework.dd ' \
30 'bs=1M count={} 2>/dev/null' \
31 ''.format(image_size_megabytes)
32 is_cmd_successful = subprocess.call(create_file_cmdline, shell=True) == 0
33 logger.info("Created local image file /tmp/image_mk_framework.dd")
34 yield is_cmd_successful
35
36 # teardown
37 logger.info("Deleting /tmp/image_mk_framework.dd file")
38 subprocess.call('rm -f /tmp/image_mk_framework.dd', shell=True)
39 subprocess.call('rm -f /tmp/image_mk_framework.download', shell=True)
40
41
42def test_speed_glance(create_image, openstack_clients, record_property):
43 """
44 Simplified Performance Tests Download / upload Glance
45 1. Create file with random data (dd)
46 2. Upload data as image to glance.
47 3. Download image.
48 4. Measure download/upload speed and print them into stdout
49 """
50 image_size_megabytes = utils.get_configuration().get("IMAGE_SIZE_MB")
51 if not is_parsable(image_size_megabytes, int):
52 pytest.fail("Can't convert IMAGE_SIZE_MB={} to 'int'".format(
53 image_size_megabytes))
54 image_size_megabytes = int(image_size_megabytes)
55 if not create_image:
56 pytest.skip("Can't create image, maybe there is lack of disk "
57 "space to create file {}MB".
58 format(image_size_megabytes))
59 image_name = "spt-test-image-{}".format(random.randrange(100, 999))
60 try:
61 image = openstack_clients.image.images.create(
62 name=image_name,
63 disk_format='iso',
64 container_format='bare')
65 logger.info("Created an image {} in Glance.".format(image_name))
66 except BaseException as e:
67 logger.info("Could not create image in Glance. See details: {}"
68 "".format(e))
69 pytest.fail("Can't create image in Glance. Occurred error: {}"
70 "".format(e))
71
72 logger.info("Testing upload file speed...")
73 start_time = time.time()
74 try:
75 openstack_clients.image.images.upload(
76 image.id, image_data=open("/tmp/image_mk_framework.dd", 'rb'))
77 except BaseException as e:
78 pytest.fail("Can't upload image in Glance. "
79 "Occurred error: {}".format(e))
80 end_time = time.time()
81
82 speed_upload = image_size_megabytes / (end_time - start_time)
83
84 logger.info("Testing download file speed...")
85 start_time = time.time()
86 with open("/tmp/image_mk_framework.download", 'wb') as image_file:
87 for item in openstack_clients.image.images.data(image.id):
88 image_file.write(item)
89 end_time = time.time()
90
91 speed_download = image_size_megabytes / (end_time - start_time)
92 logger.info("Deleted image {}.".format(image.id))
93 openstack_clients.image.images.delete(image.id)
94 record_property("Upload", speed_upload)
95 record_property("Download", speed_download)
96
97 print("++++++++++++++++++++++++++++++++++++++++")
98 print(('upload - {} MB/s'.format(speed_upload)))
99 print(('download - {} MB/s'.format(speed_download)))
100 print("++++++++++++++++++++++++++++++++++++++++")