cloudstack/test/integration/smoke/test_service_offerings.py
Marcus Sorensen 697e12f8f7
kvm: volume encryption feature (#6522)
This PR introduces a feature designed to allow CloudStack to manage a generic volume encryption setting. The encryption is handled transparently to the guest OS, and is intended to handle VM guest data encryption at rest and possibly over the wire, though the actual encryption implementation is up to the primary storage driver.

In some cases cloud customers may still prefer to maintain their own guest-level volume encryption, if they don't trust the cloud provider. However, for private cloud cases this greatly simplifies the guest OS experience in terms of running volume encryption for guests without the user having to manage keys, deal with key servers and guest booting being dependent on network connectivity to them (i.e. Tang), etc, especially in cases where users are attaching/detaching data disks and moving them between VMs occasionally.

The feature can be thought of as having two parts - the API/control plane (which includes scheduling aspects), and the storage driver implementation.

This initial PR adds the encryption setting to disk offerings and service offerings (for root volume), and implements encryption support for KVM SharedMountPoint, NFS, Local, and ScaleIO storage pools.

NOTE: While not required, operations can be significantly sped up by ensuring that hosts have the `rng-tools` package and service installed and running on the management server and hypervisors. For EL hosts the service is `rngd` and for Debian it is `rng-tools`. In particular, the use of SecureRandom for generating volume passphrases can be slow if there isn't a good source of entropy. This could affect testing and build environments, and otherwise would only affect users who actually use the encryption feature. If you find tests or volume creates blocking on encryption, check this first.

### Management Server

##### API

* createDiskOffering now has an 'encrypt' Boolean
* createServiceOffering now has an 'encryptroot' Boolean. The 'root' suffix is added here in case there is ever any other need to encrypt something related to the guest configuration, like the RAM of a VM.  This has been refactored to deal with the new separation of service offering from disk offering internally.
* listDiskOfferings shows encryption support on each offering, and has an encrypt boolean to choose to list only offerings that do or do not support encryption
* listServiceOfferings shows encryption support on each offering, and has an encrypt boolean to choose to list only offerings that do or do not support encryption
* listHosts now shows encryption support of each hypervisor host via `encryptionsupported`
* Volumes themselves don't show encryption on/off, rather the offering should be referenced. This follows the same pattern as other disk offering based settings such as the IOPS of the volume.

##### Volume functions

A decent effort has been made to ensure that the most common volume functions have either been cleanly supported or blocked. However, for the first release it is advised to mark this feature as *experimental*, as the code base is complex and there are certainly edge cases to be found.

Many of these features could eventually be supported over time, such as creating templates from encrypted volumes, but the effort and size of the change is already overwhelming.

Supported functions:
* Data Volume create
* VM root volume create
* VM root volume reinstall
* Offline volume snapshot/restore
* Migration of VM with storage (e.g. local storage VM migration)
* Resize volume
* Detach/attach volume

Blocked functions:
* Online volume snapshot
* VM snapshot w/memory
* Scheduled snapshots (would fail when VM is running)
* Disk offering migration to offerings that don't have matching encryption
* Creating template from encrypted volume
* Creating volume from encrypted volume
* Volume extraction (would we decrypt it first, or expose the key? Probably the former).

##### Primary Storage Support

For storage developers, adding encryption support involves:

1. Updating the `StoragePoolType` for your primary storage to advertise encryption support. This is used during allocation of storage to match storage types that support encryption to storage that supports it.

2. Implementing encryption feature when your `PrimaryDataStoreDriver` is called to perform volume lifecycle functions on volumes that are requesting encryption. You are free to do what your storage supports - this could be as simple as calling a storage API with the right flag when creating a volume. Or (as is the case with the KVM storage types), as complex as managing volume details directly at the hypervisor host. The data objects passed to the storage driver will contain volume passphrases, if encryption is requested.

##### Scheduling

For the KVM implementations specified above, we are dependent on the KVM hosts having support for volume encryption tools. As such, the hosts `StartupRoutingCommand` has been modified to advertise whether the host supports encryption. This is done via a probe during agent startup to look for functioning `cryptsetup` and support in `qemu-img`. This is also visible via the listHosts API and the host details in the UI.  This was patterned after other features that require hypervisor support such as UEFI.

The `EndPointSelector` interface and `DefaultEndpointSelector` have had new methods added, which allow the caller to ask for endpoints that support encryption.  This can be used by storage drivers to find the proper hosts to send storage commands that involve encryption. Not all volume activities will require a host to support encryption (for example a snapshot backup is a simple file copy), and this is the reason why the interface has been modified to allow for the storage driver to decide, rather than just passing the data objects to the EndpointSelector and letting the implementation decide.

VM scheduling has also been modified. When a VM start is requested, if any volume that requires encryption is attached, it will filter out hosts that don't support encryption.

##### DB Changes

A volume whose disk offering enables encryption will get a passphrase generated for it before its first use. This is stored in the new 'passphrase' table, and is encrypted using the CloudStack installation's standard configured DB encryption. A field has been added to the volumes table, referencing this passphrase, and a foreign key added to ensure passphrases that are referenced can't be removed from the database.  The volumes table now also contains an encryption format field, which is set by the implementer of the encryption and used as it sees fit.

#### KVM Agent

For the KVM storage pool types supported, the encryption has been implemented at Qemu itself, using the built-in LUKS storage support. This means that the storage remains encrypted all the way to the VM process, and decrypted before the block device is visible to the guest.  This may not be necessary in order to implement encryption for /your/ storage pool type, maybe you have a kernel driver that decrypts before the block device on the system, or something like that. However, it seemed like the simplest, common place to terminate the encryption, and provides the lowest surface area for decrypted guest data.

For qcow2 based storage, `qemu-img` is used to set up a qcow2 file with LUKS encryption. For block based (currently just ScaleIO storage), the `cryptsetup` utility is used to format the block device as LUKS for data disks, but `qemu-img` and its LUKS support is used for template copy.

Any volume that requires encryption will contain a passphrase ID as a byte array when handed down to the KVM agent. Care has been taken to ensure this doesn't get logged, and it is cleared after use in attempt to avoid exposing it before garbage collection occurs.  On the agent side, this passphrase is used in two ways:

1. In cases where the volume experiences some libvirt interaction it is loaded into libvirt as an ephemeral, private secret and then referenced by secret UUID in any libvirt XML. This applies to things like VM startup, migration preparation, etc.

2. In cases where `qemu-img` needs to use this passphrase for volume operations, it is written to a `KeyFile` on the cloudstack agent's configured tmpfs and passed along. The `KeyFile` is a `Closeable` and when it is closed, it is deleted. This allows us to try-with-resources any volume operations and get the KeyFile removed regardless.

In order to support the advanced syntax required to handle encryption and passphrases with `qemu-img`, the `QemuImg` utility has been modified to support the new `--object` and `--image-opts` flags. These are modeled as `QemuObject` and `QemuImageOptions`.  These `qemu-img` flags have been designed to supersede some of the existing, older flags being used today (such as choosing file formats and paths), and an effort could be made to switch over to these wholesale. However, for now we have instead opted to keep existing functions and do some wrapping to ensure backward compatibility, so callers of `QemuImg` can choose to use either way.

It should be noted that there are also a few different Enums that represent the encryption format for various purposes. While these are analogous in principle, they represent different things and should not be confused. For example, the supported encryption format strings for the `cryptsetup` utility has `LuksType.LUKS` while `QemuImg` has a `QemuImg.PhysicalDiskFormat.LUKS`.

Some additional effort could potentially be made to support advanced encryption configurations, such as choosing between LUKS1 and LUKS2 or changing cipher details. These may require changes all the way up through the control plane. However, in practice Libvirt and Qemu currently only support LUKS1 today. Additionally, the cipher details aren't required in order to use an encrypted volume, as they're stored in the LUKS header on the volume there is no need to store these elsewhere.  As such, we need only set the one encryption format upon volume creation, which is persisted in the volumes table and then available later as needed.  In the future when LUKS2 is standard and fully supported, we could move to it as the default and old volumes will still reference LUKS1 and have the headers on-disk to ensure they remain usable. We could also possibly support an automatic upgrade of the headers down the road, or a volume migration mechanism.

Every version of cryptsetup and qemu-img tested on variants of EL7 and Ubuntu that support encryption use the XTS-AES 256 cipher, which is the leading industry standard and widely used cipher today (e.g. BitLocker and FileVault).

Signed-off-by: Marcus Sorensen <mls@apple.com>
Co-authored-by: Marcus Sorensen <mls@apple.com>
2022-09-27 10:20:59 +05:30

1066 lines
37 KiB
Python

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
""" BVT tests for Service offerings"""
# Import Local Modules
from marvin.codes import FAILED
from marvin.cloudstackTestCase import cloudstackTestCase
from marvin.cloudstackAPI import (scaleVirtualMachine,
updateServiceOffering)
from marvin.lib.utils import (isAlmostEqual,
cleanup_resources,
random_gen)
from marvin.lib.base import (ServiceOffering,
Configurations,
DiskOffering,
Account,
StoragePool,
VirtualMachine)
from marvin.lib.common import (list_service_offering,
list_virtual_machines,
get_domain,
get_zone,
get_test_template,
list_hosts)
from nose.plugins.attrib import attr
import time
from marvin.sshClient import SshClient
from marvin.lib.decoratorGenerators import skipTestIf
_multiprocess_shared_ = True
class TestCreateServiceOffering(cloudstackTestCase):
def setUp(self):
self.apiclient = self.testClient.getApiClient()
self.dbclient = self.testClient.getDbConnection()
self.cleanup = []
self.services = self.testClient.getParsedTestDataConfig()
def tearDown(self):
try:
# Clean up, terminate the created templates
cleanup_resources(self.apiclient, self.cleanup)
except Exception as e:
raise Exception("Warning: Exception during cleanup : %s" % e)
return
@attr(
tags=[
"advanced",
"advancedns",
"smoke",
"basic",
"eip",
"sg", "diskencrypt"],
required_hardware="false")
def test_01_create_service_offering(self):
"""Test to create service offering"""
# Validate the following:
# 1. createServiceOfferings should return a valid information
# for newly created offering
# 2. The Cloud Database contains the valid information
service_offering = ServiceOffering.create(
self.apiclient,
self.services["service_offerings"]["tiny"]
)
self.cleanup.append(service_offering)
self.debug(
"Created service offering with ID: %s" %
service_offering.id)
list_service_response = list_service_offering(
self.apiclient,
id=service_offering.id
)
self.assertEqual(
isinstance(list_service_response, list),
True,
"Check list response returns a valid list"
)
self.assertNotEqual(
len(list_service_response),
0,
"Check Service offering is created"
)
self.assertEqual(
list_service_response[0].cpunumber,
self.services["service_offerings"]["tiny"]["cpunumber"],
"Check server id in createServiceOffering"
)
self.assertEqual(
list_service_response[0].cpuspeed,
self.services["service_offerings"]["tiny"]["cpuspeed"],
"Check cpuspeed in createServiceOffering"
)
self.assertEqual(
list_service_response[0].displaytext,
self.services["service_offerings"]["tiny"]["displaytext"],
"Check server displaytext in createServiceOfferings"
)
self.assertEqual(
list_service_response[0].memory,
self.services["service_offerings"]["tiny"]["memory"],
"Check memory in createServiceOffering"
)
self.assertEqual(
list_service_response[0].name,
self.services["service_offerings"]["tiny"]["name"],
"Check name in createServiceOffering"
)
self.assertEqual(
list_service_response[0].encryptroot,
False,
"Ensure encrypt is false by default"
)
return
@attr(
tags=[
"advanced",
"advancedns",
"smoke",
"basic",
"eip",
"sg"],
required_hardware="false")
def test_02_create_iops_offering(self):
"""Test to create service io burst offering"""
# Validate the following:
# 1. createServiceOfferings should return a valid information
# for newly created offering
# 2. The Cloud Database contains the valid information
svcs = self.services["service_offerings"]["tiny"]
kws = {}
for key in self.services["ioburst"]:
if str(key).startswith("bytes") or str(key).startswith("iops"):
kws[key] = self.services["ioburst"][key]
else:
svcs[key] = self.services["ioburst"][key]
service_offering = ServiceOffering.create(
self.apiclient,
svcs,
None,
None,
**kws
)
self.cleanup.append(service_offering)
self.debug(
"Created service offering with ID: %s" %
service_offering.id)
list_service_response = list_service_offering(
self.apiclient,
id=service_offering.id
)
self.assertEqual(
isinstance(list_service_response, list),
True,
"Check list response returns a valid list"
)
self.assertNotEqual(
len(list_service_response),
0,
"Check Service offering is created"
)
for key in kws:
k = str(key)
mapped = 'disk' + k[:1].upper() + k[1:]
self.assertEqual(
list_service_response[0][mapped],
kws[key],
"Check " + str(key) + " => " + str(mapped) + " in createServiceOffering"
)
return
@attr(
tags=[
"advanced",
"advancedns",
"smoke",
"basic",
"eip",
"sg"],
required_hardware="false")
def test_03_create_service_offering_with_cache_mode_type(self):
"""Test to create service offering with each one of the valid cache mode types : none, writeback and writethrough"""
# Validate the following:
# 1. createServiceOfferings should return a valid information
# for newly created offering
# 2. The Cloud Database contains the valid information
cache_mode_types=["none", "writeback", "writethrough"]
for i in range(3):
service_offering = ServiceOffering.create(
self.apiclient,
self.services["service_offerings"]["tiny"],
cacheMode=cache_mode_types[i]
)
self.cleanup.append(service_offering)
self.debug(
"Created service offering with ID: %s" %
service_offering.id)
list_service_response = list_service_offering(
self.apiclient,
id=service_offering.id
)
self.assertEqual(
isinstance(list_service_response, list),
True,
"Check list response returns a valid list"
)
self.assertNotEqual(
len(list_service_response),
0,
"Check Service offering is created"
)
self.assertEqual(
list_service_response[0].cpunumber,
self.services["service_offerings"]["tiny"]["cpunumber"],
"Check server id in createServiceOffering"
)
self.assertEqual(
list_service_response[0].cpuspeed,
self.services["service_offerings"]["tiny"]["cpuspeed"],
"Check cpuspeed in createServiceOffering"
)
self.assertEqual(
list_service_response[0].displaytext,
self.services["service_offerings"]["tiny"]["displaytext"],
"Check server displaytext in createServiceOfferings"
)
self.assertEqual(
list_service_response[0].memory,
self.services["service_offerings"]["tiny"]["memory"],
"Check memory in createServiceOffering"
)
self.assertEqual(
list_service_response[0].name,
self.services["service_offerings"]["tiny"]["name"],
"Check name in createServiceOffering"
)
self.assertEqual(
list_service_response[0].cacheMode,
cache_mode_types[i],
"Check cacheMode in createServiceOffering"
)
return
@attr(
tags=[
"advanced",
"advancedns",
"smoke",
"basic",
"eip",
"sg"],
required_hardware="false")
def test_04_create_service_offering_with_invalid_cache_mode_type(self):
"""Test to create service offering with invalid cache mode type"""
# Validate the following:
# 1. createServiceOfferings should return a valid information
# for newly created offering
# 2. The Cloud Database contains the valid information
with self.assertRaises(Exception):
service_offering = ServiceOffering.create(
self.apiclient,
self.services["service_offerings"]["tiny"],
cacheMode="invalid_cache_mode_type"
)
return
@attr(
tags=[
"advanced",
"advancedns",
"smoke",
"basic",
"eip",
"sg",
"diskencrypt"],
required_hardware="false")
def test_05_create_service_offering_with_root_encryption_type(self):
"""Test to create service offering with root encryption"""
# Validate the following:
# 1. createServiceOfferings should return a valid information
# for newly created offering
service_offering = ServiceOffering.create(
self.apiclient,
self.services["service_offerings"]["tiny"],
name="tiny-encrypted-root",
encryptRoot=True
)
self.cleanup.append(service_offering)
self.debug(
"Created service offering with ID: %s" %
service_offering.id)
list_service_response = list_service_offering(
self.apiclient,
id=service_offering.id
)
self.assertNotEqual(
len(list_service_response),
0,
"Check Service offering is created"
)
self.assertEqual(
list_service_response[0].encryptroot,
True,
"Check encrypt root is true"
)
return
class TestServiceOfferings(cloudstackTestCase):
def setUp(self):
self.apiclient = self.testClient.getApiClient()
self.dbclient = self.testClient.getDbConnection()
self.cleanup = []
def tearDown(self):
try:
# Clean up, terminate the created templates
cleanup_resources(self.apiclient, self.cleanup)
except Exception as e:
raise Exception("Warning: Exception during cleanup : %s" % e)
return
@classmethod
def setUpClass(cls):
testClient = super(TestServiceOfferings, cls).getClsTestClient()
cls.apiclient = testClient.getApiClient()
cls.services = testClient.getParsedTestDataConfig()
cls.hypervisor = testClient.getHypervisorInfo()
domain = get_domain(cls.apiclient)
cls.zone = get_zone(cls.apiclient, testClient.getZoneForTests())
cls.services['mode'] = cls.zone.networktype
cls.service_offering_1 = ServiceOffering.create(
cls.apiclient,
cls.services["service_offerings"]["tiny"]
)
cls.service_offering_2 = ServiceOffering.create(
cls.apiclient,
cls.services["service_offerings"]["tiny"]
)
cls.template = get_test_template(
cls.apiclient,
cls.zone.id,
cls.hypervisor
)
if cls.template == FAILED:
assert False, "get_test_template() failed to return template"
# Set Zones and disk offerings
cls.services["small"]["zoneid"] = cls.zone.id
cls.services["small"]["template"] = cls.template.id
# Create VMs, NAT Rules etc
cls.account = Account.create(
cls.apiclient,
cls.services["account"],
domainid=domain.id
)
cls.small_offering = ServiceOffering.create(
cls.apiclient,
cls.services["service_offerings"]["small"]
)
cls.medium_offering = ServiceOffering.create(
cls.apiclient,
cls.services["service_offerings"]["medium"]
)
cls.medium_virtual_machine = VirtualMachine.create(
cls.apiclient,
cls.services["small"],
accountid=cls.account.name,
domainid=cls.account.domainid,
serviceofferingid=cls.medium_offering.id,
mode=cls.services["mode"]
)
cls._cleanup = [
cls.small_offering,
cls.medium_offering,
cls.account
]
return
@classmethod
def tearDownClass(cls):
try:
cls.apiclient = super(
TestServiceOfferings,
cls).getClsTestClient().getApiClient()
# Clean up, terminate the created templates
cleanup_resources(cls.apiclient, cls._cleanup)
except Exception as e:
raise Exception("Warning: Exception during cleanup : %s" % e)
return
@attr(
tags=[
"advanced",
"advancedns",
"smoke",
"basic",
"eip",
"sg"],
required_hardware="false")
def test_02_edit_service_offering(self):
"""Test to update existing service offering"""
# Validate the following:
# 1. updateServiceOffering should return
# a valid information for newly created offering
# Generate new name & displaytext from random data
random_displaytext = random_gen()
random_name = random_gen()
random_tag = random_gen()
random_hosttag = random_gen()
self.debug("Updating service offering with ID: %s" %
self.service_offering_1.id)
cmd = updateServiceOffering.updateServiceOfferingCmd()
# Add parameters for API call
cmd.id = self.service_offering_1.id
cmd.displaytext = random_displaytext
cmd.name = random_name
cmd.storagetags = random_tag
cmd.hosttags = random_hosttag
self.apiclient.updateServiceOffering(cmd)
list_service_response = list_service_offering(
self.apiclient,
id=self.service_offering_1.id
)
self.assertEqual(
isinstance(list_service_response, list),
True,
"Check list response returns a valid list"
)
self.assertNotEqual(
len(list_service_response),
0,
"Check Service offering is updated"
)
self.assertEqual(
list_service_response[0].displaytext,
random_displaytext,
"Check server displaytext in updateServiceOffering"
)
self.assertEqual(
list_service_response[0].name,
random_name,
"Check server name in updateServiceOffering"
)
self.assertEqual(
list_service_response[0].storagetags,
random_tag,
"Check storage tags in updateServiceOffering"
)
self.assertEqual(
list_service_response[0].hosttags,
random_hosttag,
"Check host tags in updateServiceOffering"
)
return
@attr(
tags=[
"advanced",
"advancedns",
"smoke",
"basic",
"eip",
"sg"],
required_hardware="false")
def test_03_delete_service_offering(self):
"""Test to delete service offering"""
# Validate the following:
# 1. deleteServiceOffering should return
# a valid information for newly created offering
self.debug("Deleting service offering with ID: %s" %
self.service_offering_2.id)
self.service_offering_2.delete(self.apiclient)
list_service_response = list_service_offering(
self.apiclient,
id=self.service_offering_2.id
)
self.assertEqual(
list_service_response,
None,
"Check if service offering exists in listDiskOfferings"
)
return
@attr(tags=["advanced", "advancedns", "smoke"], required_hardware="true")
def test_04_change_offering_small(self):
"""Test to change service to a small capacity
"""
# Validate the following
# 1. Log in to the Vm .We should see that the CPU and memory Info of
# this Vm matches the one specified for "Small" service offering.
# 2. Using listVM command verify that this Vm
# has Small service offering Id.
if self.hypervisor.lower() == "lxc":
self.skipTest("Skipping this test for {} due to bug CS-38153".format(self.hypervisor))
try:
self.medium_virtual_machine.stop(self.apiclient)
timeout = self.services["timeout"]
while True:
time.sleep(self.services["sleep"])
# Ensure that VM is in stopped state
list_vm_response = list_virtual_machines(
self.apiclient,
id=self.medium_virtual_machine.id
)
if isinstance(list_vm_response, list):
vm = list_vm_response[0]
if vm.state == 'Stopped':
self.debug("VM state: %s" % vm.state)
break
if timeout == 0:
raise Exception(
"Failed to stop VM (ID: %s) in change service offering" % vm.id)
timeout = timeout - 1
except Exception as e:
self.fail("Failed to stop VM: %s" % e)
cmd = scaleVirtualMachine.scaleVirtualMachineCmd()
cmd.id = self.medium_virtual_machine.id
cmd.serviceofferingid = self.small_offering.id
self.apiclient.scaleVirtualMachine(cmd)
self.debug("Starting VM - ID: %s" % self.medium_virtual_machine.id)
self.medium_virtual_machine.start(self.apiclient)
# Ensure that VM is in running state
list_vm_response = list_virtual_machines(
self.apiclient,
id=self.medium_virtual_machine.id
)
if isinstance(list_vm_response, list):
vm = list_vm_response[0]
if vm.state == 'Running':
self.debug("VM state: %s" % vm.state)
else:
raise Exception(
"Failed to start VM (ID: %s) after changing\
service offering" % vm.id)
try:
ssh = self.medium_virtual_machine.get_ssh_client()
except Exception as e:
self.fail(
"SSH Access failed for %s: %s" %
(self.medium_virtual_machine.ipaddress, e)
)
cpuinfo = ssh.execute("cat /proc/cpuinfo")
cpu_cnt = len([i for i in cpuinfo if "processor" in i])
# 'cpu MHz\t\t: 2660.499'
cpu_speed = [i for i in cpuinfo if "cpu MHz" in i][0].split()[3]
meminfo = ssh.execute("cat /proc/meminfo")
# MemTotal: 1017464 kB
total_mem = [i for i in meminfo if "MemTotal" in i][0].split()[1]
self.debug(
"CPU count: %s, CPU Speed: %s, Mem Info: %s" % (
cpu_cnt,
cpu_speed,
total_mem
))
self.assertAlmostEqual(
int(cpu_cnt),
self.small_offering.cpunumber,
"Check CPU Count for small offering"
)
self.assertAlmostEqual(
list_vm_response[0].cpuspeed,
self.small_offering.cpuspeed,
"Check CPU Speed for small offering"
)
range = 25
if self.hypervisor.lower() == "hyperv":
range = 200
# TODO: Find the memory allocated to VM on hyperv hypervisor using
# powershell commands and use that value to equate instead of
# manipulating range, currently we get the memory count much less
# because of the UI component
self.assertTrue(
isAlmostEqual(int(int(total_mem) / 1024),
int(self.small_offering.memory),
range=range
),
"Check Memory(kb) for small offering"
)
return
@attr(tags=["advanced", "advancedns", "smoke"], required_hardware="true")
def test_05_disk_offering_strictness_true(self):
"""Test to see change service offering is not possible when disk offering strictness is set to true
"""
# Validate the following
# 1. Create service offering linked a disk offering and disk offering strictness is true
# 2. Create a VM with that service offering
# 3. Create another service offering with a different disk offering
# 4. Try change service offering for VM and it will fail since disk offering strictness is true (not allowed to change the disk offering)
if self.hypervisor.lower() == "lxc":
self.skipTest("Skipping this test for {} due to bug CS-38153".format(self.hypervisor))
offering_data = {
'displaytext': 'TestDiskOfferingStrictnessTrue',
'cpuspeed': 512,
'cpunumber': 2,
'name': 'TestDiskOfferingStrictnessTrue',
'memory': 1024,
'diskofferingstrictness': True
}
self.serviceOfferingWithDiskOfferingStrictnessTrue = ServiceOffering.create(
self.apiclient,
offering_data,
)
self._cleanup.append(self.serviceOfferingWithDiskOfferingStrictnessTrue)
self.virtual_machine_with_diskoffering_strictness_true = VirtualMachine.create(
self.apiclient,
self.services["small"],
accountid=self.account.name,
domainid=self.account.domainid,
serviceofferingid=self.serviceOfferingWithDiskOfferingStrictnessTrue.id,
mode=self.services["mode"]
)
try:
self.virtual_machine_with_diskoffering_strictness_true.stop(self.apiclient)
timeout = self.services["timeout"]
while True:
time.sleep(self.services["sleep"])
# Ensure that VM is in stopped state
list_vm_response = list_virtual_machines(
self.apiclient,
id=self.virtual_machine_with_diskoffering_strictness_true.id
)
if isinstance(list_vm_response, list):
vm = list_vm_response[0]
if vm.state == 'Stopped':
self.debug("VM state: %s" % vm.state)
break
if timeout == 0:
raise Exception(
"Failed to stop VM (ID: %s) in change service offering" % vm.id)
timeout = timeout - 1
except Exception as e:
self.fail("Failed to stop VM: %s" % e)
offering_data = {
'displaytext': 'TestDiskOfferingStrictnessTrue2',
'cpuspeed': 1000,
'cpunumber': 2,
'name': 'TestDiskOfferingStrictnessTrue2',
'memory': 1024,
'diskofferingstrictness': True
}
self.serviceOfferingWithDiskOfferingStrictnessTrue2 = ServiceOffering.create(
self.apiclient,
offering_data,
)
self._cleanup.append(self.serviceOfferingWithDiskOfferingStrictnessTrue2)
cmd = scaleVirtualMachine.scaleVirtualMachineCmd()
cmd.id = self.virtual_machine_with_diskoffering_strictness_true.id
cmd.serviceofferingid = self.serviceOfferingWithDiskOfferingStrictnessTrue2.id
with self.assertRaises(Exception) as e:
self.apiclient.scaleVirtualMachine(cmd)
self.debug("Upgrade VM with new service offering having different disk offering operation failed as expected with exception: %s" %
e.exception)
return
@attr(tags=["advanced", "advancedns", "smoke"], required_hardware="true")
def test_06_disk_offering_strictness_false(self):
"""Test to see change service offering is possible when disk offering strictness is set to false
"""
# Validate the following
# 1. Create service offering linked a disk offering and disk offering strictness is false
# 2. Create a VM with that service offering
# 3. Create another service offering with a different disk offering and disk offering strictness is false
# 4. Try change service offering for VM should succeed
if self.hypervisor.lower() == "lxc":
self.skipTest("Skipping this test for {} due to bug CS-38153".format(self.hypervisor))
self.storeCloneValues = {}
if self.hypervisor.lower() == "vmware":
self.fullClone = Configurations.list(self.apiclient, name="vmware.create.full.clone")
assert isinstance(self.fullClone, list), "Config list not retrieved for vmware.create.full.clone"
allStoragePools = StoragePool.list(
self.apiclient
)
for pool in allStoragePools:
self.storeCloneValues[pool.id] = Configurations.list(self.apiclient, name="vmware.create.full.clone", storageid=pool.id)[0].value.lower()
self.updateVmwareCreateFullCloneSetting(False)
offering_data = {
'displaytext': 'TestDiskOfferingStrictnessFalse',
'cpuspeed': 512,
'cpunumber': 2,
'name': 'TestDiskOfferingStrictnessFalse',
'memory': 1024,
'diskofferingstrictness': False
}
self.serviceOfferingWithDiskOfferingStrictnessFalse = ServiceOffering.create(
self.apiclient,
offering_data,
)
self._cleanup.append(self.serviceOfferingWithDiskOfferingStrictnessFalse)
self.virtual_machine_with_diskoffering_strictness_false = VirtualMachine.create(
self.apiclient,
self.services["small"],
accountid=self.account.name,
domainid=self.account.domainid,
serviceofferingid=self.serviceOfferingWithDiskOfferingStrictnessFalse.id,
mode=self.services["mode"]
)
try:
self.virtual_machine_with_diskoffering_strictness_false.stop(self.apiclient)
timeout = self.services["timeout"]
while True:
time.sleep(self.services["sleep"])
# Ensure that VM is in stopped state
list_vm_response = list_virtual_machines(
self.apiclient,
id=self.virtual_machine_with_diskoffering_strictness_false.id
)
if isinstance(list_vm_response, list):
vm = list_vm_response[0]
if vm.state == 'Stopped':
self.debug("VM state: %s" % vm.state)
break
if timeout == 0:
raise Exception(
"Failed to stop VM (ID: %s) in change service offering" % vm.id)
timeout = timeout - 1
except Exception as e:
self.fail("Failed to stop VM: %s" % e)
self.disk_offering2 = DiskOffering.create(
self.apiclient,
self.services["disk_offering"],
)
self._cleanup.append(self.disk_offering2)
offering_data = {
'displaytext': 'TestDiskOfferingStrictnessFalse2',
'cpuspeed': 1000,
'cpunumber': 2,
'name': 'TestDiskOfferingStrictnessFalse2',
'memory': 1024,
'diskofferingstrictness': False,
'diskofferingid': self.disk_offering2.id
}
self.serviceOfferingWithDiskOfferingStrictnessFalse2 = ServiceOffering.create(
self.apiclient,
offering_data,
)
self._cleanup.append(self.serviceOfferingWithDiskOfferingStrictnessFalse2)
cmd = scaleVirtualMachine.scaleVirtualMachineCmd()
cmd.id = self.virtual_machine_with_diskoffering_strictness_false.id
cmd.serviceofferingid = self.serviceOfferingWithDiskOfferingStrictnessFalse2.id
self.apiclient.scaleVirtualMachine(cmd)
list_vm_response = VirtualMachine.list(
self.apiclient,
id=self.virtual_machine_with_diskoffering_strictness_false.id
)
vm_response = list_vm_response[0]
self.assertEqual(
vm_response.id,
self.virtual_machine_with_diskoffering_strictness_false.id,
"Check virtual machine ID of upgraded VM"
)
self.assertEqual(
vm_response.serviceofferingid,
self.serviceOfferingWithDiskOfferingStrictnessFalse2.id,
"Check service offering of the VM"
)
if self.hypervisor.lower() == "vmware":
self.updateVmwareCreateFullCloneSetting(True)
return
def updateVmwareCreateFullCloneSetting(self, tearDown):
if not tearDown:
Configurations.update(self.apiclient,
"vmware.create.full.clone",
"true")
allStoragePools = StoragePool.list(
self.apiclient
)
for pool in allStoragePools:
Configurations.update(self.apiclient,
storageid=pool.id,
name="vmware.create.full.clone",
value="true")
else:
Configurations.update(self.apiclient,
"vmware.create.full.clone",
self.fullClone[0].value.lower())
allStoragePools = StoragePool.list(
self.apiclient
)
for pool in allStoragePools:
Configurations.update(self.apiclient,
storageid=pool.id,
name="vmware.create.full.clone",
value=self.storeCloneValues[pool.id])
class TestCpuCapServiceOfferings(cloudstackTestCase):
def setUp(self):
self.apiclient = self.testClient.getApiClient()
self.dbclient = self.testClient.getDbConnection()
self.cleanup = []
def tearDown(self):
try:
# Clean up, terminate the created templates
cleanup_resources(self.apiclient, self.cleanup)
except Exception as e:
raise Exception("Warning: Exception during cleanup : %s" % e)
return
def get_ssh_client(self, id, public_ip, username, password, retries):
""" Setup ssh client connection and return connection
vm requires attributes public_ip, public_port, username, password """
try:
ssh_client = SshClient(
public_ip,
22,
username,
password,
retries)
except Exception as e:
self.fail("Unable to create ssh connection: " % e)
self.assertIsNotNone(
ssh_client, "Failed to setup ssh connection to host=%s on public_ip=%s" % (id, public_ip))
return ssh_client
@classmethod
def setUpClass(cls):
testClient = super(TestCpuCapServiceOfferings, cls).getClsTestClient()
cls.apiclient = testClient.getApiClient()
cls.services = testClient.getParsedTestDataConfig()
cls.hypervisor = testClient.getHypervisorInfo()
cls._cleanup = []
cls.hypervisorNotSupported = False
if cls.hypervisor.lower() not in ["kvm"]:
cls.hypervisorNotSupported = True
return
domain = get_domain(cls.apiclient)
cls.zone = get_zone(cls.apiclient, testClient.getZoneForTests())
cls.services['mode'] = cls.zone.networktype
template = get_test_template(cls.apiclient, cls.zone.id, cls.hypervisor)
if template == FAILED:
assert False, "get_test_template() failed to return template"
cls.services["small"]["zoneid"] = cls.zone.id
cls.services["small"]["template"] = template.id
cls.services["small"]["hypervisor"] = cls.hypervisor
cls.hostConfig = cls.config.__dict__["zones"][0].__dict__["pods"][0].__dict__["clusters"][0].__dict__["hosts"][0].__dict__
cls.account = Account.create(
cls.apiclient,
cls.services["account"],
domainid=domain.id
)
offering_data = {
'displaytext': 'TestOffering',
'cpuspeed': 512,
'cpunumber': 2,
'name': 'TestOffering',
'memory': 1024
}
cls.offering = ServiceOffering.create(
cls.apiclient,
offering_data,
limitcpuuse=True
)
def getHost(self, hostId=None):
response = list_hosts(
self.apiclient,
type='Routing',
hypervisor='kvm',
id=hostId
)
# Check if more than one kvm hosts are available in order to successfully configure host-ha
if response and len(response) > 0:
self.host = response[0]
return self.host
raise self.skipTest("Not enough KVM hosts found, skipping host-ha test")
cls.host = getHost(cls)
cls.vm = VirtualMachine.create(
cls.apiclient,
cls.services["small"],
accountid=cls.account.name,
domainid=cls.account.domainid,
serviceofferingid=cls.offering.id,
mode=cls.services["mode"],
hostid=cls.host.id
)
cls._cleanup = [
cls.offering,
cls.account
]
@classmethod
def tearDownClass(cls):
try:
cls.apiclient = super(
TestCpuCapServiceOfferings,
cls).getClsTestClient().getApiClient()
# Clean up, terminate the created templates
cleanup_resources(cls.apiclient, cls._cleanup)
except Exception as e:
raise Exception("Warning: Exception during cleanup : %s" % e)
return
@skipTestIf("hypervisorNotSupported")
@attr(tags=["advanced", "advancedns", "smoke"], required_hardware="true")
def test_01_service_offering_cpu_limit_use(self):
"""
Test CPU Cap on KVM
"""
ssh_host = self.get_ssh_client(self.host.id, self.host.ipaddress, self.hostConfig["username"], self.hostConfig["password"], 10)
#Get host CPU usage from top command before and after VM consuming 100% CPU
find_pid_cmd = "ps -ax | grep '%s' | head -1 | awk '{print $1}'" % self.vm.id
pid = ssh_host.execute(find_pid_cmd)[0]
cpu_usage_cmd = "top -b n 1 p %s | tail -1 | awk '{print $9}'" % pid
host_cpu_usage_before_str = ssh_host.execute(cpu_usage_cmd)[0]
host_cpu_usage_before = round(float(host_cpu_usage_before_str))
self.debug("Host CPU usage before the infinite loop on the VM: " + str(host_cpu_usage_before))
#Execute loop command in background on the VM
ssh_vm = self.vm.get_ssh_client(reconnect=True)
ssh_vm.execute("echo 'while true; do x=$(($x+1)); done' > cputest.sh")
ssh_vm.execute("sh cputest.sh > /dev/null 2>&1 &")
time.sleep(5)
host_cpu_usage_after_str = ssh_host.execute(cpu_usage_cmd)[0]
host_cpu_usage_after = round(float(host_cpu_usage_after_str))
self.debug("Host CPU usage after the infinite loop on the VM: " + str(host_cpu_usage_after))
limit = 95
self.assertTrue(host_cpu_usage_after < limit, "Host CPU usage after VM usage increased is high")
return