blob: 1b6f9734b7f38078c12f48ec9380d4164c4919dd [file] [log] [blame]
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +02001import logging
Ievgeniia Zadorozhna97dfde42022-06-17 20:05:09 +03002import pytest
3import random
4import subprocess
5import sys
6import time
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +02007
8import utils
Ievgeniia Zadorozhna9664b422023-03-28 21:09:46 +03009from utils import helpers
10
11from texttable import Texttable
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +020012
13logger = logging.getLogger(__name__)
14
15
16def is_parsable(value, to):
17 """
18 Check if value can be converted into some type
19 :param value: input value that should be converted
20 :param to: type of output value like int, float. It's not a string!
21 :return: bool
22 """
23 try:
24 to(value)
Ievgeniia Zadorozhna670e7ff2023-03-29 00:22:22 +030025 except BaseException:
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +020026 return False
27 return True
28
29
30@pytest.fixture
31def create_image():
32 image_size_megabytes = utils.get_configuration().get("IMAGE_SIZE_MB", 9000)
33 create_file_cmdline = 'dd if=/dev/zero of=/tmp/image_mk_framework.dd ' \
34 'bs=1M count={} 2>/dev/null' \
35 ''.format(image_size_megabytes)
36 is_cmd_successful = subprocess.call(create_file_cmdline, shell=True) == 0
37 logger.info("Created local image file /tmp/image_mk_framework.dd")
38 yield is_cmd_successful
39
40 # teardown
41 logger.info("Deleting /tmp/image_mk_framework.dd file")
42 subprocess.call('rm -f /tmp/image_mk_framework.dd', shell=True)
43 subprocess.call('rm -f /tmp/image_mk_framework.download', shell=True)
44
45
Ievgeniia Zadorozhna9664b422023-03-28 21:09:46 +030046def test_speed_glance(create_image, openstack_clients,
47 request, html_report):
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +020048 """
49 Simplified Performance Tests Download / upload Glance
50 1. Create file with random data (dd)
51 2. Upload data as image to glance.
52 3. Download image.
53 4. Measure download/upload speed and print them into stdout
54 """
Ievgeniia Zadorozhna9664b422023-03-28 21:09:46 +030055 result_table = Texttable(max_width=120)
56 table_rows = [["Test Speed Glance", "Image Size", "Time Consumed",
57 "Result"]]
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +020058 image_size_megabytes = utils.get_configuration().get("IMAGE_SIZE_MB")
59 if not is_parsable(image_size_megabytes, int):
60 pytest.fail("Can't convert IMAGE_SIZE_MB={} to 'int'".format(
61 image_size_megabytes))
62 image_size_megabytes = int(image_size_megabytes)
63 if not create_image:
64 pytest.skip("Can't create image, maybe there is lack of disk "
65 "space to create file {}MB".
66 format(image_size_megabytes))
67 image_name = "spt-test-image-{}".format(random.randrange(100, 999))
68 try:
69 image = openstack_clients.image.images.create(
70 name=image_name,
Ievgeniia Zadorozhna6ba79092022-12-13 03:06:05 +030071 disk_format='raw',
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +020072 container_format='bare')
73 logger.info("Created an image {} in Glance.".format(image_name))
74 except BaseException as e:
75 logger.info("Could not create image in Glance. See details: {}"
76 "".format(e))
77 pytest.fail("Can't create image in Glance. Occurred error: {}"
78 "".format(e))
79
80 logger.info("Testing upload file speed...")
81 start_time = time.time()
82 try:
83 openstack_clients.image.images.upload(
84 image.id, image_data=open("/tmp/image_mk_framework.dd", 'rb'))
85 except BaseException as e:
86 pytest.fail("Can't upload image in Glance. "
87 "Occurred error: {}".format(e))
88 end_time = time.time()
89
Ievgeniia Zadorozhna9664b422023-03-28 21:09:46 +030090 time_diff = end_time - start_time
91 speed_upload = image_size_megabytes / time_diff
92 table_rows.append(["Upload",
93 "{} MB".format(image_size_megabytes),
94 "{} s".format(round(time_diff, 3)),
95 "{} MB/s".format(round(speed_upload, 2))])
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +020096
97 logger.info("Testing download file speed...")
98 start_time = time.time()
99 with open("/tmp/image_mk_framework.download", 'wb') as image_file:
100 for item in openstack_clients.image.images.data(image.id):
101 image_file.write(item)
102 end_time = time.time()
103
Ievgeniia Zadorozhna9664b422023-03-28 21:09:46 +0300104 time_diff = end_time - start_time
105 speed_download = image_size_megabytes / time_diff
106 table_rows.append(["Download",
107 "{} MB".format(image_size_megabytes),
108 "{} s".format(round(time_diff, 3)),
109 "{} MB/s".format(round(speed_download, 2))])
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +0200110 logger.info("Deleted image {}.".format(image.id))
111 openstack_clients.image.images.delete(image.id)
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +0200112
Ievgeniia Zadorozhna9664b422023-03-28 21:09:46 +0300113 result_table.add_rows(table_rows)
114 sys.stdout.write('\n{}\n'.format(result_table.draw()))
115
116 # Send the results to CSV file at reports/ directory
117 helpers.create_test_result_table_csv_file(
118 table_rows, request.node.name)