DaanHoogland commented on code in PR #14804:
URL: https://github.com/apache/cloudstack/pull/14804#discussion_r4767916152


##########
test/integration/smoke/test_kvm_file_based_no_copy_snapshots.py:
##########
@@ -0,0 +1,498 @@
+# 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.
+""" Smoke tests for KVM file-based no-copy volume snapshots (the 
[kvm.file.based.volume.snapshot.no.copy] setting).
+
+With the setting enabled on an NFS, SharedMountPoint or local primary storage 
pool, a volume snapshot freezes the volume's
+current QCOW2 file and moves the volume onto a new delta file backed by it, 
instead of copying the volume. Revert moves the
+volume onto a new delta over the snapshot's file and delete merges the 
snapshot's file into its neighbour.
+
+The tests write data to the volume files on the host with qemu-io while the 
Instance is stopped, and check after every
+operation that the volume holds the expected data and that the files on the 
pool match what CloudStack reports.
+
+Requirements:
+ - a KVM zone with an NFS, SharedMountPoint or local (Filesystem) primary 
storage pool and a QCOW2 template;
+ - [snapshot.backup.to.secondary] set to false (the setting is not dynamic, so 
the test does not change it);
+ - qemu-io and qemu-img on the KVM hosts, and SSH access to them with the host 
credentials of the Marvin configuration.
+"""
+import threading
+import time
+
+from marvin.cloudstackAPI import createTemplate
+from marvin.cloudstackTestCase import cloudstackTestCase
+from marvin.codes import FAILED
+from marvin.lib.base import (Account,
+                             Configurations,
+                             DiskOffering,
+                             Host,
+                             Network,
+                             NetworkOffering,
+                             ServiceOffering,
+                             Snapshot,
+                             StoragePool,
+                             VirtualMachine,
+                             VmSnapshot,
+                             Volume)
+from marvin.lib.common import get_domain, get_suitable_test_template, get_zone
+from marvin.sshClient import SshClient
+from nose.plugins.attrib import attr
+
+NO_COPY_SETTING = "kvm.file.based.volume.snapshot.no.copy"
+BACKUP_TO_SECONDARY_SETTING = "snapshot.backup.to.secondary"
+HOST_CAPABILITY = "host.kvm.diskonlysnapshot.singlevolume"
+SUPPORTED_POOL_TYPES = ("NetworkFilesystem", "SharedMountPoint", "Filesystem")
+# Setting values are cached by the management server for 30 seconds.
+SETTING_CACHE_SECONDS = 35
+MIB = 1024 * 1024
+
+
+CONCURRENT_DELETE_REFUSALS = ("deleted or changed concurrently", "already 
destroyed")
+INCREMENTAL_SNAPSHOT_SETTING = "kvm.incremental.snapshot"
+
+
+class TestKvmFileBasedNoCopySnapshots(cloudstackTestCase):
+
+    @classmethod
+    def setUpClass(cls):
+        test_client = super(TestKvmFileBasedNoCopySnapshots, 
cls).getClsTestClient()
+        cls.apiclient = test_client.getApiClient()
+        cls.services = test_client.getParsedTestDataConfig()
+        cls.hypervisor = test_client.getHypervisorInfo()
+        cls._cleanup = []
+        cls.pools_with_setting_changed = []
+        cls.skip_reason = None
+        cls.ssh_clients = {}
+        try:
+            cls.set_up_class(test_client)
+        except Exception:
+            # unittest does not call tearDownClass when setUpClass fails.
+            cls.tearDownClass()
+            raise
+
+    @classmethod
+    def set_up_class(cls, test_client):
+        if cls.hypervisor.lower() != "kvm":
+            cls.skip_reason = "KVM file-based no-copy snapshots are only 
supported on KVM"
+            return
+
+        cls.zone = get_zone(cls.apiclient, test_client.getZoneForTests())
+        cls.domain = get_domain(cls.apiclient)
+
+        backup_to_secondary = Configurations.list(cls.apiclient, 
name=BACKUP_TO_SECONDARY_SETTING)[0].value
+        if backup_to_secondary.lower() != "false":
+            cls.skip_reason = "[%s] must be false (it is not dynamic, so the 
test does not change it)" % BACKUP_TO_SECONDARY_SETTING
+            return
+
+        pools = StoragePool.list(cls.apiclient, zoneid=cls.zone.id) or []
+        cls.pools = [p for p in pools if p.type in SUPPORTED_POOL_TYPES and 
p.state == "Up"]
+        if not cls.pools:
+            cls.skip_reason = "No Up primary storage pool of type %s in the 
zone" % "/".join(SUPPORTED_POOL_TYPES)
+            return
+
+        hosts = Host.list(cls.apiclient, zoneid=cls.zone.id, type="Routing", 
hypervisor="KVM", state="Up") or []
+        if not hosts or any(str(h.details[HOST_CAPABILITY] if h.details else 
None).lower() != "true" for h in hosts):
+            cls.skip_reason = "Every Up KVM host must advertise [%s] (a KVM 
agent with disk-only snapshot support)" % HOST_CAPABILITY
+            return
+
+        for cluster_id in set(p.clusterid for p in cls.pools if p.clusterid):
+            if Configurations.list(cls.apiclient, 
name=INCREMENTAL_SNAPSHOT_SETTING, clusterid=cluster_id)[0].value.lower() == 
"true":
+                cls.skip_reason = "[%s] is enabled in cluster %s; no-copy 
snapshots are not used with incremental snapshots" % (
+                    INCREMENTAL_SNAPSHOT_SETTING, cluster_id)
+                return
+
+        cls.host_config = 
cls.config.__dict__["zones"][0].__dict__["pods"][0].__dict__["clusters"][0].__dict__["hosts"][0].__dict__
+        for host in hosts:
+            tools = cls.ssh_to_host(host.id).execute("command -v qemu-io 
qemu-img virsh | wc -l")
+            if not tools or tools[-1].strip() != "3":
+                cls.skip_reason = "qemu-io, qemu-img and virsh are needed on 
KVM host %s for the checks" % host.name
+                return
+
+        template = get_suitable_test_template(cls.apiclient, cls.zone.id, 
cls.services["ostype"], cls.hypervisor)
+        if template == FAILED:
+            cls.skip_reason = "No suitable template for KVM in the zone"
+            return
+
+        cls.account = Account.create(cls.apiclient, cls.services["account"], 
domainid=cls.domain.id)
+        cls._cleanup.append(cls.account)
+        cls.service_offering = ServiceOffering.create(cls.apiclient, 
cls.services["service_offering"])
+        cls._cleanup.append(cls.service_offering)
+        cls.disk_offering = DiskOffering.create(cls.apiclient, 
cls.services["disk_offering"])
+        cls._cleanup.append(cls.disk_offering)
+
+        l2_offering = NetworkOffering.list(cls.apiclient, 
name="DefaultL2NetworkOffering")[0]
+        cls.network = Network.create(cls.apiclient, 
cls.services["l2-network"], accountid=cls.account.name,
+                                     domainid=cls.account.domainid, 
networkofferingid=l2_offering.id, zoneid=cls.zone.id)
+
+        cls.virtual_machine = VirtualMachine.create(cls.apiclient, 
cls.services["virtual_machine"], templateid=template.id,
+                                                    
accountid=cls.account.name, domainid=cls.account.domainid,
+                                                    
serviceofferingid=cls.service_offering.id, networkids=[cls.network.id],
+                                                    zoneid=cls.zone.id, 
mode="advanced")
+
+        # Changed last, so that nothing above can fail with the setting 
changed.
+        global_value = Configurations.list(cls.apiclient, 
name=NO_COPY_SETTING)[0].value
+        for pool in cls.pools:
+            current = Configurations.list(cls.apiclient, name=NO_COPY_SETTING, 
storageid=pool.id)[0].value
+            if current.lower() != "true":
+                cls.pools_with_setting_changed.append((pool.id, current, 
global_value))
+                Configurations.update(cls.apiclient, name=NO_COPY_SETTING, 
value="true", storageid=pool.id)
+        if cls.pools_with_setting_changed:
+            time.sleep(SETTING_CACHE_SECONDS)
+
+    @classmethod
+    def tearDownClass(cls):
+        for client in cls.ssh_clients.values():
+            client.close()
+        try:
+            super(TestKvmFileBasedNoCopySnapshots, cls).tearDownClass()
+        finally:
+            for pool_id, value, global_value in cls.pools_with_setting_changed:
+                cls.restore_pool_setting(pool_id, value, global_value)
+
+    @classmethod
+    def restore_pool_setting(cls, pool_id, value, global_value):
+        """Resets the pool's value when it was inherited from the global one, 
so that the pool keeps following later global changes."""
+        if value == global_value:
+            Configurations.reset(cls.apiclient, name=NO_COPY_SETTING, 
storageid=pool_id)
+        else:
+            Configurations.update(cls.apiclient, name=NO_COPY_SETTING, 
value=value, storageid=pool_id)
+
+    def setUp(self):

Review Comment:
   I am missing a `tearDown(self)`. is that not needed?  (parent does have an 
implementation, but I never saw a functioning integration test that doesn't 
have one)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to