cvp-spt, size check image to check Glance upload/download speed can be
changed using env var
It set to 2000 MB by default (because of free space on cid* nodes)

Test vm2vm gracefully skips test if no image found

Change-Id: I3aa5f50bf75b48df528de8c4196ae51c23de4b9e
Fixes-bug: #PROD-27763(PROD:27763)
diff --git a/test_set/cvp-spt/README.md b/test_set/cvp-spt/README.md
index 00a200a..125521f 100644
--- a/test_set/cvp-spt/README.md
+++ b/test_set/cvp-spt/README.md
@@ -1,5 +1,12 @@
 # cvp-spt
 Environment variables
 --
-* Set *keystone_api_version* to required keystone API version (like keystone_api_version=2)
-Or it will be set to '3' otherwise
+
+* Set IMAGE_SIZE_MB env variable to have specific image size in cvp-spt/test_glance.py tests
+
+* *cvp-spt.test_glance*   Error may happen while executing images.upload:
+```python
+CommunicationError: Error finding address for http://os-ctl-vip.<>.local:9292/v2/images/8bce33dd-9837-4646-b747-7f7f5ce01092/file:
+Unable to establish connection to http://os-ctl-vip.<>.local:9292/v2/images/8bce33dd-9837-4646-b747-7f7f5ce01092/file: [Errno 32] Broken pipe
+```
+This may happen because of low disk space on ctl node or old cryptography package (will be fixed after upgrading to Python3)
\ No newline at end of file
diff --git a/test_set/cvp-spt/fixtures/base.py b/test_set/cvp-spt/fixtures/base.py
index 5c4b72d..a79d39d 100644
--- a/test_set/cvp-spt/fixtures/base.py
+++ b/test_set/cvp-spt/fixtures/base.py
@@ -56,8 +56,7 @@
 
     os_images_list = [image.id for image in openstack_clients.image.images.list(filters={'name': image_name})]
     if os_images_list.__len__() == 0:
-        print "No images with name {}. This name can be redefined with 'image_name' env var ".format(image_name)
-        exit()
+        pytest.skip("No images with name {}. This name can be redefined with 'image_name' env var ".format(image_name))
 
     os_resource['image_id'] = str(os_images_list[0])
 
diff --git a/test_set/cvp-spt/global_config.yaml b/test_set/cvp-spt/global_config.yaml
index 51af102..f55a2a6 100644
--- a/test_set/cvp-spt/global_config.yaml
+++ b/test_set/cvp-spt/global_config.yaml
@@ -27,3 +27,4 @@
 CMP_HOSTS: []
 nova_timeout: 30
 iperf_prep_string: "sudo /bin/bash -c 'echo \"91.189.88.161        archive.ubuntu.com\" >> /etc/hosts'"
+IMAGE_SIZE_MB: 2000
\ No newline at end of file
diff --git a/test_set/cvp-spt/tests/test_glance.py b/test_set/cvp-spt/tests/test_glance.py
index ab4fe64..5514944 100644
--- a/test_set/cvp-spt/tests/test_glance.py
+++ b/test_set/cvp-spt/tests/test_glance.py
@@ -1,54 +1,88 @@
 import pytest
 import time
 import subprocess
-import  utils
+import utils
+
+
+def is_parsable(value, to):
+    """
+    Check if value can be converted into some type
+    :param value:  input value that should be converted
+    :param to: type of output value like int, float. It's not a string!
+    :return: bool
+    """
+    try:
+        to(value)
+    except:
+        return False
+    return True
 
 
 @pytest.fixture
 def create_image():
-    line = 'echo "Executing dd on $(hostname -f)"; ' \
-           'dd if=/dev/zero of=/tmp/image_mk_framework.dd bs=1M count=9000 ;' \
-           'echo "Free space :" ; ' \
-           'df -H / '
+    image_size_megabytes = utils.get_configuration().get("IMAGE_SIZE_MB")
+    create_file_cmdline = 'dd if=/dev/zero of=/tmp/image_mk_framework.dd bs=1M count={image_size}'.format(
+        image_size=image_size_megabytes)
 
-    subprocess.call(line.split())
-    yield
+    is_cmd_successful = subprocess.call(create_file_cmdline.split()) == 0
+    yield is_cmd_successful
     # teardown
-    subprocess.call('rm /tmp/image_mk_framework.dd'.split())
-    subprocess.call('rm /tmp/image_mk_framework.download'.split())
+    subprocess.call('rm -f /tmp/image_mk_framework.dd'.split())
+    subprocess.call('rm -f /tmp/image_mk_framework.download'.split())
 
 
 def test_speed_glance(create_image, openstack_clients, record_property):
     """
-    Simplified Performance Tests Download / upload lance
-    1. Step download image
-    2. Step upload image
+    Simplified Performance Tests Download / upload Glance
+    1. Create file with random data (dd)
+    2. Upload data as image to glance.
+    3. Download image.
+    4. Measure download/upload speed and print them into stdout
     """
-    image = openstack_clients.image.images.create(
-        name="test_image",
-        disk_format='iso',
-        container_format='bare')
+    image_size_megabytes = utils.get_configuration().get("IMAGE_SIZE_MB")
+    if not is_parsable(image_size_megabytes, int):
+        pytest.fail("Can't convert IMAGE_SIZE_MB={} to 'int'".format(image_size_megabytes))
+    image_size_megabytes = int(image_size_megabytes)
+    if not create_image:
+        pytest.skip("Can't create image, maybe there is lack of disk space to create file {}MB".
+                    format(image_size_megabytes))
+    try:
+        image = openstack_clients.image.images.create(
+            name="test_image",
+            disk_format='iso',
+            container_format='bare')
+    except BaseException as e:
+        pytest.fail("Can't create image in Glance. Occurred error: {}".format(e))
 
+    # FIXME: Error may happens while executing images.upload:
+    #  CommunicationError: Error finding address for
+    #  http://os-ctl-vip.harhipova-cicd-os-test.local:9292/v2/images/8bce33dd-9837-4646-b747-7f7f5ce01092/file: Unable to establish connection to http://os-ctl-vip.harhipova-cicd-os-test.local:9292/v2/images/8bce33dd-9837-4646-b747-7f7f5ce01092/file: [Errno 32] Broken pipe
+    # This may happen because of low disk space on ctl node or old cryptography package
+    # (will be fixed after upgrading to Python3)
     start_time = time.time()
-    openstack_clients.image.images.upload(
-        image.id,
-        image_data=open("/tmp/image_mk_framework.dd", 'rb'))
+    try:
+        openstack_clients.image.images.upload(
+            image.id,
+            image_data=open("/tmp/image_mk_framework.dd", 'rb'))
+    except BaseException as e:
+        pytest.fail("Can't upload image in Glance. Occurred error: {}".format(e))
     end_time = time.time()
 
-    speed_upload = 9000 / (end_time - start_time)
+    speed_upload = image_size_megabytes / (end_time - start_time)
 
     start_time = time.time()
+    # it creates new file /tmp/image_mk_framework.download . It should be removed in teardown
     with open("/tmp/image_mk_framework.download", 'wb') as image_file:
         for item in openstack_clients.image.images.data(image.id):
             image_file.write(item)
     end_time = time.time()
 
-    speed_download = 9000 / (end_time - start_time)
+    speed_download = image_size_megabytes / (end_time - start_time)
 
     openstack_clients.image.images.delete(image.id)
     record_property("Upload", speed_upload)
     record_property("Download", speed_download)
 
-    print "++++++++++++++++++++++++++++++++++++++++"
-    print 'upload - {} Mb/s'.format(speed_upload)
-    print 'download - {} Mb/s'.format(speed_download)
+    print("++++++++++++++++++++++++++++++++++++++++")
+    print('upload - {} Mb/s'.format(speed_upload))
+    print('download - {} Mb/s'.format(speed_download))
diff --git a/test_set/cvp-spt/tests/test_vm2vm.py b/test_set/cvp-spt/tests/test_vm2vm.py
index 4c2ec98..fc93347 100644
--- a/test_set/cvp-spt/tests/test_vm2vm.py
+++ b/test_set/cvp-spt/tests/test_vm2vm.py
@@ -7,7 +7,7 @@
 from utils import ssh
 
 
-def test_vm2vm (openstack_clients, pair, os_resources, record_property):
+def test_vm2vm(openstack_clients, pair, os_resources, record_property):
     os_actions = os_client.OSCliActions(openstack_clients)
     config = utils.get_configuration()
     timeout = int(config.get('nova_timeout', 30))
@@ -52,7 +52,7 @@
             fips.append(fip.id)
             status = openstack_clients.compute.servers.get(vms[i]).status
             if status != 'ACTIVE':
-                print "VM #{0} {1} is not ready. Status {2}".format(i,vms[i].id,status)
+                print("VM #{0} {1} is not ready. Status {2}".format(i,vms[i].id,status))
                 time.sleep(timeout)
                 status = openstack_clients.compute.servers.get(vms[i]).status
             if status != 'ACTIVE':
@@ -63,39 +63,44 @@
             try:
                 ssh.prepare_iperf(fip.ip,private_key=os_resources['keypair'].private_key)
             except Exception as e:
-                print e
-                print "ssh.prepare_iperf was not successful, retry after {} sec".format(timeout)
+                print(e)
+                print("ssh.prepare_iperf was not successful, retry after {} sec".format(timeout))
                 time.sleep(timeout)
                 ssh.prepare_iperf(fip.ip,private_key=os_resources['keypair'].private_key)
-            vm_info.append({'vm': vms[i], 'fip': fip.ip, 'private_address': private_address})   
-        
+            vm_info.append({'vm': vms[i], 'fip': fip.ip, 'private_address': private_address})
+
         transport1 = ssh.SSHTransport(vm_info[0]['fip'], 'ubuntu', password='dd', private_key=os_resources['keypair'].private_key)
 
         result1 = transport1.exec_command('iperf -c {} -t 60 | tail -n 1'.format(vm_info[1]['private_address']))
-        print ' '.join(result1.split()[-2::])
+        print(' '.join(result1.split()[-2::]))
+
         record_property("same {0}-{1}".format(zone1[0],zone2[0]), ' '.join(result1.split()[-2::]))
         result2 = transport1.exec_command('iperf -c {} -t 60 | tail -n 1'.format(vm_info[2]['private_address']))
-        print ' '.join(result2.split()[-2::])
+        print(' '.join(result2.split()[-2::]))
+
         record_property("diff host {0}-{1}".format(zone1[0],zone2[0]), ' '.join(result2.split()[-2::]))
         result3 = transport1.exec_command('iperf -c {} -P 10 -t 60 | tail -n 1'.format(vm_info[2]['private_address']))
-        print ' '.join(result3.split()[-2::])
+        print(' '.join(result3.split()[-2::]))
+
         record_property("dif host 10 threads {0}-{1}".format(zone1[0],zone2[0]), ' '.join(result3.split()[-2::]))
         result4 = transport1.exec_command('iperf -c {} -t 60 | tail -n 1'.format(vm_info[2]['fip']))
-        print ' '.join(result4.split()[-2::])
+        print(' '.join(result4.split()[-2::]))
+
         record_property("diff host fip {0}-{1}".format(zone1[0],zone2[0]), ' '.join(result4.split()[-2::]))
         result5 = transport1.exec_command('iperf -c {} -t 60 | tail -n 1'.format(vm_info[3]['private_address']))
-        print ' '.join(result5.split()[-2::])
+        print(' '.join(result5.split()[-2::]))
+
         record_property("diff host, diff net {0}-{1}".format(zone1[0],zone2[0]), ' '.join(result5.split()[-2::]))
 
-        print "Remove VMs"
+        print("Remove VMs")
         for vm in vms:
             openstack_clients.compute.servers.delete(vm)
-        print "Remove FIPs"
+        print("Remove FIPs")
         for fip in fips:
             openstack_clients.compute.floating_ips.delete(fip)
     except Exception as e:
-        print e
-        print "Something went wrong"
+        print(e)
+        print("Something went wrong")
         for vm in vms:
             openstack_clients.compute.servers.delete(vm)
         for fip in fips: