Add tests for share server migration

This patch adds new positive and negative tests for
share server migration.

Partially-implements: bp share-server-migration
Co-Authored-By: Daniel Tapia <danielarthurt@gmail.com>

Change-Id: Ice973bd2ad15bfb432e51da595b53c0480adba14
Depends-On: Ic0751027d2c3f1ef7ab0f7836baff3070a230cfd
Depends-On: I46a0cee0a4625d950f02fa7a5bf612de926451b5
Depends-On: I813801fd64d4d605c0a4fe06c7a130ad00865b6d
diff --git a/manila_tempest_tests/common/constants.py b/manila_tempest_tests/common/constants.py
index 88fe411..2750760 100644
--- a/manila_tempest_tests/common/constants.py
+++ b/manila_tempest_tests/common/constants.py
@@ -97,6 +97,7 @@
 
 # Share servers
 SERVER_STATE_ACTIVE = 'active'
+SERVER_STATE_INACTIVE = 'inactive'
 SERVER_STATE_CREATING = 'creating'
 SERVER_STATE_DELETING = 'deleting'
 SERVER_STATE_ERROR = 'error'
@@ -104,3 +105,5 @@
 SERVER_STATE_MANAGE_STARTING = 'manage_starting'
 SERVER_STATE_UNMANAGE_ERROR = 'unmanage_error'
 SERVER_STATE_UNMANAGE_STARTING = 'unmanage_starting'
+STATUS_SERVER_MIGRATING = 'server_migrating'
+STATUS_SERVER_MIGRATING_TO = 'server_migrating_to'
diff --git a/manila_tempest_tests/config.py b/manila_tempest_tests/config.py
index 2b6cfd4..610ae51 100644
--- a/manila_tempest_tests/config.py
+++ b/manila_tempest_tests/config.py
@@ -29,7 +29,7 @@
                help="The minimum api microversion is configured to be the "
                     "value of the minimum microversion supported by Manila."),
     cfg.StrOpt("max_api_microversion",
-               default="2.56",
+               default="2.57",
                help="The maximum api microversion is configured to be the "
                     "value of the latest microversion supported by Manila."),
     cfg.StrOpt("region",
@@ -262,6 +262,10 @@
                 help="Defines whether to run tests that create share from "
                      "snapshots in another pool or az. Enable this "
                      "option if the used driver supports it."),
+    cfg.BoolOpt("run_share_servers_migration_tests",
+                default=False,
+                help="Defines whether to run share servers migration tests. "
+                     "Enable this option if the used driver supports it."),
 
     cfg.StrOpt("image_with_share_tools",
                default="manila-service-image-master",
@@ -279,6 +283,10 @@
                default=1500,
                help="Time to wait for share migration before "
                     "timing out (seconds)."),
+    cfg.IntOpt("share_server_migration_timeout",
+               default="1500",
+               help="Time to wait for share server migration before "
+                    "timing out (seconds)."),
     cfg.StrOpt("default_share_type_name",
                help="Default share type name to use in tempest tests."),
     cfg.StrOpt("backend_replication_type",
diff --git a/manila_tempest_tests/services/share/v2/json/shares_client.py b/manila_tempest_tests/services/share/v2/json/shares_client.py
index 050642b..c1a0f3c 100644
--- a/manila_tempest_tests/services/share/v2/json/shares_client.py
+++ b/manila_tempest_tests/services/share/v2/json/shares_client.py
@@ -578,7 +578,7 @@
             time.sleep(self.build_interval)
             body = self.get_snapshot(snapshot_id, version=version)
             snapshot_status = body['status']
-            if snapshot_status == status:
+            if snapshot_status in status:
                 return
             if 'error' in snapshot_status:
                 raise (share_exceptions.
@@ -1522,7 +1522,7 @@
             time.sleep(self.build_interval)
             body = self.show_share_server(server_id)
             server_status = body[status_attr]
-            if server_status == status:
+            if server_status in status:
                 return
             elif constants.STATUS_ERROR in server_status.lower():
                 raise share_exceptions.ShareServerBuildErrorException(
@@ -2107,3 +2107,90 @@
         return body
 
 ###############
+
+    def share_server_migration_check(
+        self, share_server_id, host, writable=False,
+        preserve_snapshots=False, nondisruptive=False,
+        new_share_network_id=None, version=LATEST_MICROVERSION):
+        body = {
+            'migration_check': {
+                'host': host,
+                'writable': writable,
+                'preserve_snapshots': preserve_snapshots,
+                'nondisruptive': nondisruptive,
+                'new_share_network_id': new_share_network_id,
+            }
+        }
+
+        body = json.dumps(body)
+        resp, body = self.post('share-servers/%s/action' % share_server_id,
+                               body, headers=EXPERIMENTAL, extra_headers=True,
+                               version=version)
+        self.expected_success(200, resp.status)
+
+        return json.loads(body)
+
+    def share_server_migration_start(self, share_server_id, host,
+                                     writable=False, new_share_network_id=None,
+                                     preserve_snapshots=False,
+                                     nondisruptive=False,
+                                     version=LATEST_MICROVERSION):
+        body = {
+            'migration_start': {
+                'host': host,
+                'writable': writable,
+                'preserve_snapshots': preserve_snapshots,
+                'nondisruptive': nondisruptive,
+                'new_share_network_id': new_share_network_id,
+            }
+        }
+
+        body = json.dumps(body)
+        resp, body = self.post('share-servers/%s/action' % share_server_id,
+                               body, headers=EXPERIMENTAL, extra_headers=True,
+                               version=version)
+        self.expected_success(202, resp.status)
+
+        return body
+
+    def share_server_migration_complete(self, share_server_id,
+                                        version=LATEST_MICROVERSION):
+        body = {
+            'migration_complete': None
+        }
+
+        body = json.dumps(body)
+        resp, body = self.post('share-servers/%s/action' % share_server_id,
+                               body, headers=EXPERIMENTAL, extra_headers=True,
+                               version=version)
+        self.expected_success(200, resp.status)
+
+        return body
+
+    def share_server_migration_cancel(self, share_server_id,
+                                      version=LATEST_MICROVERSION):
+        body = {
+            'migration_cancel': None
+        }
+
+        body = json.dumps(body)
+        resp, body = self.post('share-servers/%s/action' % share_server_id,
+                               body, headers=EXPERIMENTAL, extra_headers=True,
+                               version=version)
+        self.expected_success(202, resp.status)
+
+        return body
+
+    def share_server_migration_get_progress(self, share_server_id,
+                                            version=LATEST_MICROVERSION):
+        body = {
+            'migration_get_progress': None
+        }
+
+        body = json.dumps(body)
+        resp, body = self.post('share-servers/%s/action' % share_server_id,
+                               body, headers=EXPERIMENTAL, extra_headers=True,
+                               version=version)
+        self.expected_sucess(200, resp.status)
+
+        return json.loads(body)
diff --git a/manila_tempest_tests/share_exceptions.py b/manila_tempest_tests/share_exceptions.py
index 67b4fca..9466afe 100644
--- a/manila_tempest_tests/share_exceptions.py
+++ b/manila_tempest_tests/share_exceptions.py
@@ -80,3 +80,8 @@
 class ShareServerBuildErrorException(exceptions.TempestException):
     message = ("Share server %(server_id)s failed to build and is in ERROR "
                "status")
+
+
+class ShareServerMigrationException(exceptions.TempestException):
+    message = ("Share server %(server_id)s failed to migrate and is in ERROR "
+               "status")
diff --git a/manila_tempest_tests/tests/api/admin/test_share_servers_migration.py b/manila_tempest_tests/tests/api/admin/test_share_servers_migration.py
new file mode 100644
index 0000000..813cb76
--- /dev/null
+++ b/manila_tempest_tests/tests/api/admin/test_share_servers_migration.py
@@ -0,0 +1,407 @@
+# Copyright 2020 NetApp Inc.
+# All Rights Reserved.
+#
+#    Licensed 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.
+
+import ddt
+from tempest import config
+from tempest.lib import decorators
+from tempest.lib import exceptions
+from testtools import testcase as tc
+
+from manila_tempest_tests.common import constants
+from manila_tempest_tests.tests.api import base
+from manila_tempest_tests import utils
+
+CONF = config.CONF
+
+
+class MigrationShareServerBase(base.BaseSharesAdminTest):
+    protocol = None
+
+    @classmethod
+    def skip_checks(cls):
+        super(MigrationShareServerBase, cls).skip_checks()
+        if cls.protocol not in CONF.share.enable_protocols:
+            raise cls.skipException('%s tests are disabled.' % cls.protocol)
+        if not CONF.share.multitenancy_enabled:
+            raise cls.skipException('Multitenancy tests are disabled.')
+        if not CONF.share.run_share_servers_migration_tests:
+            raise cls.skipException(
+                'Share servers migration tests are disabled.')
+        utils.check_skip_if_microversion_lt('2.57')
+
+    @classmethod
+    def resource_setup(cls):
+        super(MigrationShareServerBase, cls).resource_setup()
+        cls.all_hosts = cls.shares_v2_client.list_pools(detail=True)
+        cls.backends = set()
+        for pool in cls.all_hosts['pools']:
+            if pool['capabilities'].get('driver_handles_share_servers'):
+                cls.backends.add(pool['name'].split('#')[0])
+
+        if len(cls.backends) < 2:
+            msg = ("Could not find the necessary backends. At least two"
+                   " are needed to run the tests of share server migration")
+            raise cls.skipException(msg)
+
+        # create share type (generic)
+        cls.share_type = cls._create_share_type()
+
+    def _setup_migration(self, share):
+        """Initial share server migration setup."""
+
+        share = self.shares_v2_client.get_share(share['id'])
+        server_id = share['share_server_id']
+
+        # (andrer) Verify if have at least one backend compatible with
+        # the specified share server.
+        dest_host, compatible = self._choose_matching_backend_for_share_server(
+            server_id)
+
+        snapshot = False
+        if compatible['supported_capabilities']['preserve_snapshots']:
+            snapshot = self.create_snapshot_wait_for_active(
+                share['id'], cleanup_in_class=False)['id']
+
+        # (andrer) Check the share export locations.
+        old_exports = self.shares_v2_client.list_share_export_locations(
+            share['id'])
+        self.assertNotEmpty(old_exports)
+        old_exports = [x['path'] for x in old_exports
+                       if x['is_admin_only'] is False]
+        self.assertNotEmpty(old_exports)
+
+        # (andrer) Create the access rules, considering NFS and CIFS
+        # protocols.
+        access_rules = self._get_access_rule_data_for_protocols()
+        self.shares_v2_client.create_access_rule(
+            share['id'], access_type=access_rules[0].get('access_type'),
+            access_to=access_rules[0].get('access_to'),
+            access_level=access_rules[0].get('access_level'))
+
+        self.shares_v2_client.wait_for_share_status(
+            share['id'], constants.RULE_STATE_ACTIVE,
+            status_attr='access_rules_status')
+
+        if self.protocol == 'nfs':
+            self.shares_v2_client.create_access_rule(
+                share['id'], access_type=access_rules[1].get('access_type'),
+                access_to=access_rules[1].get('access_to'),
+                access_level=access_rules[1].get('access_level'))
+
+            self.shares_v2_client.wait_for_share_status(
+                share['id'], constants.RULE_STATE_ACTIVE,
+                status_attr='access_rules_status')
+
+        share = self.shares_v2_client.get_share(share['id'])
+
+        return share, server_id, dest_host, snapshot
+
+    def _validate_instances_states(self, share, instance_status,
+                                   snapshot_id):
+        """Validates the share and snapshot status."""
+        statuses = ((instance_status,)
+                    if not isinstance(instance_status, (tuple, list, set))
+                    else instance_status)
+
+        share = self.shares_v2_client.get_share(share['id'])
+        self.assertIn(share['status'], statuses)
+
+        if snapshot_id:
+            snapshot = self.shares_v2_client.get_snapshot(snapshot_id)
+            self.assertIn(snapshot['status'], statuses)
+
+    def _validate_share_server_migration_complete(
+        self, share, dest_host, src_server_id, dest_server_id,
+        snapshot_id=None, share_network_id=None,
+        version=CONF.share.max_api_microversion):
+        """Validates the share server migration complete. """
+
+        # Check the export locations
+        new_exports = self.shares_v2_client.list_share_export_locations(
+            share['id'], version=version)
+        self.assertNotEmpty(new_exports)
+        new_exports = [x['path'] for x in new_exports if
+                       x['is_admin_only'] is False]
+        self.assertNotEmpty(new_exports)
+
+        # Check the share host, share_network, share_server and status.
+        share = self.shares_v2_client.get_share(share['id'])
+        self.assertEqual(share['host'].split('#')[0], dest_host)
+        self.assertEqual(share_network_id, share['share_network_id'])
+        self.assertEqual(dest_server_id, share['share_server_id'])
+        self.assertEqual(share['status'], constants.STATUS_AVAILABLE)
+
+        # Check the snapshot status if possible.
+        if snapshot_id:
+            self.shares_v2_client.wait_for_snapshot_status(
+                snapshot_id, constants.STATUS_AVAILABLE)
+
+        # Check the share server destination status.
+        dest_server = self.shares_v2_client.show_share_server(dest_server_id)
+        self.assertIn(dest_server['task_state'],
+                      constants.TASK_STATE_MIGRATION_SUCCESS)
+
+        # Check if the access rules are in the share.
+        rules = self.shares_v2_client.list_access_rules(share['id'])
+        if self.protocol == 'cifs':
+            expected_rules = [{
+                'state': constants.RULE_STATE_ACTIVE,
+                'access_to': CONF.share.username_for_user_rules,
+                'access_type': 'user',
+                'access_level': 'rw',
+            }]
+        elif self.protocol == 'nfs':
+            expected_rules = [{
+                'state': constants.RULE_STATE_ACTIVE,
+                'access_to': '50.50.50.50',
+                'access_type': 'ip',
+                'access_level': 'rw',
+            }, {
+                'state': constants.RULE_STATE_ACTIVE,
+                'access_to': '51.51.51.51',
+                'access_type': 'ip',
+                'access_level': 'ro',
+            }]
+
+        filtered_rules = [{'state': rule['state'],
+                           'access_to': rule['access_to'],
+                           'access_level': rule['access_level'],
+                           'access_type': rule['access_type']}
+                          for rule in rules]
+
+        for r in expected_rules:
+            self.assertIn(r, filtered_rules)
+        self.assertEqual(len(expected_rules), len(filtered_rules))
+
+    @classmethod
+    def _choose_matching_backend_for_share_server(self, server_id):
+        """Choose a compatible host for the share server migration."""
+        for backend in self.backends:
+            # This try is necessary since if you try migrate the share server
+            # using the same backend and share network will raise an exception.
+            try:
+                compatibility = (
+                    self.admin_shares_v2_client.share_server_migration_check(
+                        share_server_id=server_id, host=backend))
+            except exceptions.Conflict or exceptions.ServerFault:
+                continue
+            if compatibility['compatible']:
+                return backend, compatibility
+
+        raise self.skipException(
+            "Not found compatible host for the share server migration.")
+
+    def _choose_incompatible_backend_for_share_server(self, server_id):
+        """Choose a not compatible host for the share server migration."""
+        for backend in self.backends:
+            # This try is necessary since if you try migrate the share server
+            # using the same backend and share network will raise an exception.
+            try:
+                compatibility = (
+                    self.admin_shares_v2_client.share_server_migration_check(
+                        share_server_id=server_id, host=backend))
+            except exceptions.Conflict or exceptions.ServerFault:
+                continue
+            if not compatibility['compatible']:
+                return backend, compatibility
+
+        raise self.skipException(
+            "Not found incompatible host for the share server migration.")
+
+    def _get_share_server_destination_for_migration(self, src_server_id):
+        """Find the destination share server choosed for the migration."""
+        params = {'source_share_server_id': src_server_id,
+                  'status': constants.STATUS_SERVER_MIGRATING_TO}
+        dest_server = self.admin_shares_v2_client.list_share_servers(
+            search_opts=params)
+        dest_server_id = dest_server[0]['id'] if dest_server else None
+
+        return dest_server_id
+
+    def _get_access_rule_data_for_protocols(self):
+        """Return fake data for access rules based on configured protocol."""
+        if self.protocol == 'nfs':
+            return [{
+                'access_type': 'ip',
+                'access_to': '50.50.50.50',
+                'access_level': 'rw',
+            }, {
+                'access_type': 'ip',
+                'access_to': '51.51.51.51',
+                'access_level': 'ro',
+            }]
+        elif self.protocol == 'cifs':
+            return [{
+                'access_type': 'user',
+                'access_to': CONF.share.username_for_user_rules,
+                'access_level': 'rw',
+            }]
+        else:
+            message = "Unrecognized protocol and access rules configuration"
+            raise self.skipException(message)
+
+
+@ddt.ddt
+class ShareServerMigrationBasicNFS(MigrationShareServerBase):
+    protocol = "nfs"
+
+    @decorators.idempotent_id('5b84bcb6-17d8-4073-8e02-53b54aee6f8b')
+    @tc.attr(base.TAG_POSITIVE, base.TAG_BACKEND)
+    def test_share_server_migration_cancel(self):
+        """Test the share server migration cancel."""
+        share_network_id = self.provide_share_network(
+            self.shares_v2_client, self.networks_client)
+        share = self.create_share(share_protocol=self.protocol,
+                                  share_type_id=self.share_type['id'],
+                                  share_network_id=share_network_id,
+                                  cleanup_in_class=False)
+        share = self.shares_v2_client.get_share(share['id'])
+
+        # Initial migration setup.
+        share, src_server_id, dest_host, snapshot_id = self._setup_migration(
+            share)
+
+        preserve_snapshots = True if snapshot_id else False
+
+        # Start share server migration.
+        self.shares_v2_client.share_server_migration_start(
+            src_server_id, dest_host, preserve_snapshots=preserve_snapshots)
+
+        task_state = constants.TASK_STATE_MIGRATION_DRIVER_PHASE1_DONE
+        self.shares_v2_client.wait_for_share_server_status(
+            src_server_id, task_state, status_attr='task_state')
+        # Get for the destination share server.
+        dest_server_id = self._get_share_server_destination_for_migration(
+            src_server_id)
+
+        dest_server = self.shares_v2_client.show_share_server(dest_server_id)
+        self.assertEqual(dest_host, dest_server['host'])
+        self.assertEqual(share_network_id, dest_server['share_network_id'])
+
+        # Validate the share instances status.
+        share_status = constants.STATUS_SERVER_MIGRATING
+        self._validate_instances_states(share, share_status, snapshot_id)
+
+        # Cancel the share server migration.
+        self.shares_v2_client.share_server_migration_cancel(src_server_id)
+
+        # Wait for the migration cancelled status.
+        task_state = constants.TASK_STATE_MIGRATION_CANCELLED
+        self.shares_v2_client.wait_for_share_server_status(
+            src_server_id, task_state, status_attr='task_state')
+
+        # After the cancel operation, we need to validate again the resources.
+        share_status = constants.STATUS_AVAILABLE
+        self._validate_instances_states(share, share_status, snapshot_id)
+
+    @decorators.idempotent_id('99e439a8-a716-4205-bf5b-af50128cb908')
+    @tc.attr(base.TAG_POSITIVE, base.TAG_BACKEND)
+    @ddt.data(False, True)
+    def test_share_server_migration_complete(self, new_share_network):
+        """Test the share server migration complete."""
+        share_network_id = self.provide_share_network(
+            self.shares_v2_client, self.networks_client)
+        dest_share_network_id = share_network_id
+        if new_share_network:
+            src_share_network = self.shares_v2_client.get_share_network(
+                share_network_id)
+            share_net_info = (
+                utils.share_network_get_default_subnet(src_share_network))
+            dest_share_network_id = self.create_share_network(
+                neutron_net_id=share_net_info['neutron_net_id'],
+                neutron_subnet_id=share_net_info['neutron_subnet_id'],
+                cleanup_in_class=False)['id']
+
+        share = self.create_share(share_protocol=self.protocol,
+                                  share_type_id=self.share_type['id'],
+                                  share_network_id=share_network_id,
+                                  cleanup_in_class=False)
+        share = self.shares_v2_client.get_share(share['id'])
+
+        # Initial migration setup.
+        share, src_server_id, dest_host, snapshot_id = self._setup_migration(
+            share)
+
+        preserve_snapshots = True if snapshot_id else False
+
+        # Start share server migration.
+        self.shares_v2_client.share_server_migration_start(
+            src_server_id, dest_host,
+            new_share_network_id=dest_share_network_id,
+            preserve_snapshots=preserve_snapshots)
+
+        task_state = constants.TASK_STATE_MIGRATION_DRIVER_PHASE1_DONE
+        self.shares_v2_client.wait_for_share_server_status(
+            src_server_id, task_state, status_attr='task_state')
+        # Get for the destination share server.
+        dest_server_id = self._get_share_server_destination_for_migration(
+            src_server_id)
+
+        dest_server = self.shares_v2_client.show_share_server(dest_server_id)
+        self.assertEqual(dest_host, dest_server['host'])
+        self.assertEqual(dest_share_network_id,
+                         dest_server['share_network_id'])
+
+        share_status = constants.STATUS_SERVER_MIGRATING
+        self._validate_instances_states(share, share_status, snapshot_id)
+
+        # Share server migration complete.
+        self.shares_v2_client.share_server_migration_complete(src_server_id)
+
+        # It's necessary wait for the migration success state and
+        # active status.
+        task_state = [constants.TASK_STATE_MIGRATION_SUCCESS,
+                      constants.SERVER_STATE_INACTIVE]
+        self.shares_v2_client.wait_for_share_server_status(
+            src_server_id, task_state, status_attr='task_state')
+
+        # Validate the share server migration complete.
+        share = self.shares_v2_client.get_share(share['id'])
+        self._validate_share_server_migration_complete(
+            share, dest_host, src_server_id, dest_server_id,
+            snapshot_id=snapshot_id, share_network_id=dest_share_network_id)
+
+    @decorators.idempotent_id('52e154eb-2d39-45af-b5c1-49ea569ab804')
+    @tc.attr(base.TAG_POSITIVE, base.TAG_BACKEND)
+    @ddt.data(True, False)
+    def test_share_server_migration_check(self, compatible):
+        """The share server migration check compatibility tests."""
+        share = self.create_share(share_protocol=self.protocol,
+                                  share_type_id=self.share_type['id'],
+                                  cleanup_in_class=False)
+        share = self.shares_v2_client.get_share(share['id'])
+        # Find a backend compatible or not for the share server
+        # check compatibility operation.
+        if compatible:
+            dest_host, result = self._choose_matching_backend_for_share_server(
+                server_id=share['share_server_id'])
+            self.assertTrue(result['compatible'])
+            self.assertEqual(result['requested_capabilities']['host'],
+                             dest_host)
+        else:
+            dest_host, result = (
+                self._choose_incompatible_backend_for_share_server(
+                    server_id=share['share_server_id']))
+            if dest_host is None:
+                raise self.skipException(
+                    "Not found any compatible destination host for the share "
+                    "server %s." % share['share_server_id'])
+            self.assertFalse(result['compatible'])
+            self.assertEqual(result['requested_capabilities'].get('host'),
+                             dest_host)
+
+
+class ShareServerMigrationBasicCIFS(ShareServerMigrationBasicNFS):
+    protocol = "cifs"
diff --git a/manila_tempest_tests/tests/api/admin/test_share_servers_migration_negative.py b/manila_tempest_tests/tests/api/admin/test_share_servers_migration_negative.py
new file mode 100644
index 0000000..9924f82
--- /dev/null
+++ b/manila_tempest_tests/tests/api/admin/test_share_servers_migration_negative.py
@@ -0,0 +1,335 @@
+# Copyright 2020 NetApp Inc.
+# All Rights Reserved.
+#
+#    Licensed 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.
+
+from tempest import config
+from tempest.lib.common.utils import data_utils
+from tempest.lib import decorators
+from tempest.lib import exceptions as lib_exc
+from testtools import testcase as tc
+
+
+from manila_tempest_tests.common import constants
+from manila_tempest_tests.tests.api.admin import test_share_servers_migration
+from manila_tempest_tests.tests.api import base
+
+CONF = config.CONF
+
+
+class MigrationShareServerNegative(
+        test_share_servers_migration.MigrationShareServerBase):
+    protocool = None
+
+    @classmethod
+    def _setup_migration(self, cleanup_in_class=True):
+        """Setup migration for negative tests."""
+        extra_specs = {
+            'driver_handles_share_servers': CONF.share.multitenancy_enabled}
+        if CONF.share.capability_snapshot_support:
+            extra_specs['snapshot_support'] = True
+        share_type = self.create_share_type(
+            name=data_utils.rand_name("tempest-share-type"),
+            extra_specs=extra_specs,
+            cleanup_in_class=cleanup_in_class)
+        share = self.create_share(share_protocol=self.protocol,
+                                  share_type_id=share_type['share_type']['id'],
+                                  cleanup_in_class=cleanup_in_class)
+        share = self.shares_v2_client.get_share(share['id'])
+        share_server_id = share['share_server_id']
+        dest_host, compatible = self._choose_matching_backend_for_share_server(
+            share_server_id)
+
+        return share, share_server_id, dest_host
+
+
+class ShareServerMigrationInvalidParametersNFS(MigrationShareServerNegative):
+    """Tests related to share server not found."""
+    protocol = "nfs"
+
+    @classmethod
+    def resource_setup(cls):
+        super(ShareServerMigrationInvalidParametersNFS, cls).resource_setup()
+        cls.share = cls.create_share(
+            share_protocol=cls.protocol,
+            share_type_id=cls.share_type['id'])
+        cls.share = cls.shares_v2_client.get_share(cls.share['id'])
+        cls.share_server_id = cls.share['share_server_id']
+        cls.fake_server_id = 'fake_server_id'
+        cls.fake_host = 'fake_host@fake_backend'
+
+    @decorators.idempotent_id('1be6ec2a-3118-4033-9cdb-ea6d199d97f4')
+    @tc.attr(base.TAG_NEGATIVE, base.TAG_API_WITH_BACKEND)
+    def test_share_server_invalid_server_migration_check(self):
+        """Not found share server in migration check."""
+        self.assertRaises(lib_exc.NotFound,
+                          self.shares_v2_client.share_server_migration_check,
+                          self.fake_server_id,
+                          self.fake_host)
+
+    @decorators.idempotent_id('2aeffcfa-4e68-40e4-8a75-03b017503501')
+    @tc.attr(base.TAG_NEGATIVE, base.TAG_API_WITH_BACKEND)
+    def test_share_server_invalid_server_migration_cancel(self):
+        """Not found share server in migration cancel."""
+        self.assertRaises(lib_exc.NotFound,
+                          self.shares_v2_client.share_server_migration_cancel,
+                          self.fake_server_id)
+
+    @decorators.idempotent_id('52d23980-80e7-40de-8dba-1bb1382ef995')
+    @tc.attr(base.TAG_NEGATIVE, base.TAG_API_WITH_BACKEND)
+    def test_share_server_invalid_server_migration_start(self):
+        """Not found share server in migration start."""
+        self.assertRaises(lib_exc.NotFound,
+                          self.shares_v2_client.share_server_migration_start,
+                          self.fake_server_id,
+                          self.fake_host)
+
+    @decorators.idempotent_id('47795631-eb50-424b-9fac-d2ee832cd01c')
+    @tc.attr(base.TAG_NEGATIVE, base.TAG_API_WITH_BACKEND)
+    def test_share_server_invalid_server_migration_get_progress(self):
+        """Not found share server in migration get progress."""
+        self.assertRaises(
+            lib_exc.BadRequest,
+            self.shares_v2_client.share_server_migration_get_progress,
+            self.fake_server_id)
+
+    @decorators.idempotent_id('3b464298-a4e4-417b-92d6-acfbd30ac45b')
+    @tc.attr(base.TAG_NEGATIVE, base.TAG_API_WITH_BACKEND)
+    def test_share_server_invalid_server_migration_complete(self):
+        """Not found share server in migration """
+        self.assertRaises(
+            lib_exc.NotFound,
+            self.shares_v2_client.share_server_migration_complete,
+            self.fake_server_id)
+
+    @decorators.idempotent_id('2d25cf84-0b5c-4a9f-ae20-9bec09bb6914')
+    @tc.attr(base.TAG_NEGATIVE, base.TAG_API_WITH_BACKEND)
+    def test_share_server_invalid_host_migration_start(self):
+        """Not found share server in migration start."""
+        self.assertRaises(lib_exc.NotFound,
+                          self.shares_v2_client.share_server_migration_start,
+                          self.share_server_id,
+                          self.fake_host)
+
+    @decorators.idempotent_id('e7e2c19c-a0ed-41ab-b666-b2beae4a690c')
+    @tc.attr(base.TAG_NEGATIVE, base.TAG_API_WITH_BACKEND)
+    def test_share_server_invalid_host_migration_check(self):
+        """Not found share server in migration check."""
+        self.assertRaises(lib_exc.NotFound,
+                          self.shares_v2_client.share_server_migration_check,
+                          self.share_server_id,
+                          self.fake_host)
+
+
+class ShareServerErrorStatusOperationNFS(MigrationShareServerNegative):
+    protocol = "nfs"
+
+    @classmethod
+    def resource_setup(cls):
+        super(ShareServerErrorStatusOperationNFS, cls).resource_setup()
+        cls.share = cls.create_share(
+            share_protocol=cls.protocol,
+            share_type_id=cls.share_type['id'])
+        cls.share = cls.shares_v2_client.get_share(cls.share['id'])
+        cls.share_server_id = cls.share['share_server_id']
+        cls.shares_v2_client.share_server_reset_state(
+            cls.share_server_id, status=constants.STATUS_ERROR)
+        cls.fake_host = 'fake_host@fake_backend'
+
+    @decorators.idempotent_id('1f8d75c1-aa3c-465a-b2dd-9ad33933944f')
+    @tc.attr(base.TAG_NEGATIVE, base.TAG_API_WITH_BACKEND)
+    def test_share_server_invalid_operation_migration_check(self):
+        """Share server migration check invalid operation."""
+        self.assertRaises(lib_exc.Conflict,
+                          self.shares_v2_client.share_server_migration_check,
+                          self.share_server_id,
+                          self.fake_host)
+
+    @decorators.idempotent_id('c256c5f5-b4d1-47b7-a1f4-af21f19ce600')
+    @tc.attr(base.TAG_NEGATIVE, base.TAG_API_WITH_BACKEND)
+    def test_share_server_invalid_operation_migration_start(self):
+        """Share server migration start invalid operation."""
+        self.assertRaises(lib_exc.Conflict,
+                          self.shares_v2_client.share_server_migration_start,
+                          self.share_server_id,
+                          self.fake_host)
+
+    @decorators.idempotent_id('d2830fe4-8d13-40d2-b987-18d414bb6196')
+    @tc.attr(base.TAG_NEGATIVE, base.TAG_API_WITH_BACKEND)
+    def test_share_server_invalid_operation_migration_get_progress(self):
+        """Share server migration get progress invalid operation."""
+        self.assertRaises(
+            lib_exc.BadRequest,
+            self.shares_v2_client.share_server_migration_get_progress,
+            self.share_server_id,
+            self.fake_host)
+
+    @decorators.idempotent_id('245f39d7-bcbc-4711-afd7-651a5535a880')
+    @tc.attr(base.TAG_NEGATIVE, base.TAG_API_WITH_BACKEND)
+    def test_share_server_invalid_operation_migration_cancel(self):
+        """Share server migration cancel invalid operation."""
+        self.assertRaises(lib_exc.BadRequest,
+                          self.shares_v2_client.share_server_migration_cancel,
+                          self.share_server_id,
+                          self.fake_host)
+
+    @decorators.idempotent_id('3db45440-2c70-4fa4-b5eb-75e3cb0204f8')
+    @tc.attr(base.TAG_NEGATIVE, base.TAG_API_WITH_BACKEND)
+    def test_share_server_invalid_operation_migration_complete(self):
+        """Share server migration complete invalid operation."""
+        self.assertRaises(
+            lib_exc.BadRequest,
+            self.shares_v2_client.share_server_migration_complete,
+            self.share_server_id,
+            self.fake_host)
+
+
+class ShareServerMigrationStartNegativesNFS(MigrationShareServerNegative):
+    protocol = "nfs"
+
+    @classmethod
+    def resource_setup(cls):
+        super(ShareServerMigrationStartNegativesNFS, cls).resource_setup()
+        cls.share, cls.server_id, cls.dest_host = cls._setup_migration()
+        cls.shares_v2_client.share_server_migration_start(
+            cls.server_id, cls.dest_host)
+
+    @classmethod
+    def resource_cleanup(cls):
+        states = [constants.TASK_STATE_MIGRATION_DRIVER_IN_PROGRESS,
+                  constants.TASK_STATE_MIGRATION_DRIVER_PHASE1_DONE]
+        cls.shares_v2_client.wait_for_share_server_status(
+            cls.server_id, status=states, status_attr="task_state")
+        cls.shares_v2_client.share_server_migration_cancel(cls.server_id)
+        cls.shares_v2_client.wait_for_share_status(cls.share['id'],
+                                                   status="available")
+        super(ShareServerMigrationStartNegativesNFS, cls).resource_cleanup()
+
+    @decorators.idempotent_id('5b904db3-fc36-4c35-a8ef-cf6b80315388')
+    @tc.attr(base.TAG_NEGATIVE, base.TAG_API_WITH_BACKEND)
+    def test_share_server_migration_start_try_create_snapshot(self):
+        """Try create snap during a server migration."""
+        if not CONF.share.capability_snapshot_support:
+            raise self.skipException(
+                'Snapshot tests are disabled or unsupported.')
+        self.assertRaises(
+            lib_exc.BadRequest,
+            self.shares_v2_client.create_snapshot,
+            self.share['id']
+        )
+
+    @decorators.idempotent_id('93882b54-78d4-4c4e-95b5-993de0cdb25d')
+    @tc.attr(base.TAG_NEGATIVE, base.TAG_API_WITH_BACKEND)
+    def test_share_server_migration_start_try_create_access_rule(self):
+        """Try create access rule during a server migration."""
+        self.assertRaises(
+            lib_exc.BadRequest,
+            self.shares_v2_client.create_access_rule,
+            self.share['id']
+        )
+
+    @decorators.idempotent_id('7c74a4a8-61b2-4c55-bc4b-02eac73d2c6e')
+    @tc.attr(base.TAG_NEGATIVE, base.TAG_API_WITH_BACKEND)
+    def test_share_server_migration_start_try_delete_share_network(self):
+        """Try delete share network during a server migration."""
+        self.assertRaises(
+            lib_exc.Conflict,
+            self.shares_v2_client.delete_share_network,
+            self.share['share_network_id']
+        )
+
+
+class ShareServerMigrationStartInvalidStatesNFS(MigrationShareServerNegative):
+    protocol = "nfs"
+
+    @decorators.idempotent_id('bcec0503-b2a9-4514-bf3f-a30d55f41e78')
+    @tc.attr(base.TAG_NEGATIVE, base.TAG_API_WITH_BACKEND)
+    def test_share_server_migration_start_invalid_network(self):
+        """Try server migration start with invalid network."""
+        share, share_server_id, dest_host = self._setup_migration(
+            cleanup_in_class=False)
+        share_network = self.create_share_network(cleanup_in_class=False)
+
+        self.assertRaises(
+            lib_exc.ServerFault,
+            self.shares_v2_client.share_server_migration_start,
+            share_server_id,
+            dest_host,
+            new_share_network_id=share_network)
+
+    @decorators.idempotent_id('11374277-efcf-4992-ad94-c8f4a393d41b')
+    @tc.attr(base.TAG_NEGATIVE, base.TAG_API_WITH_BACKEND)
+    def test_share_server_migration_start_invalid_share_state(self):
+        """Try server migration start with invalid share state."""
+        share, share_server_id, dest_host = self._setup_migration(
+            cleanup_in_class=False)
+        self.shares_v2_client.reset_state(share['id'])
+
+        self.assertRaises(
+            lib_exc.Conflict,
+            self.shares_v2_client.share_server_migration_start,
+            share_server_id,
+            dest_host
+        )
+
+    @decorators.idempotent_id('ebe8da5b-ee9c-48c7-a7e4-9e71839f813f')
+    @tc.attr(base.TAG_NEGATIVE, base.TAG_API_WITH_BACKEND)
+    def test_share_server_migration_start_with_share_replica(self):
+        """Try server migration start with share replica."""
+        if not CONF.share.backend_replication_type or (
+                not CONF.share.run_replication_tests):
+            raise self.skipException(
+                'Share replica tests are disabled or unsupported.')
+        extra_specs = {
+            'driver_handles_share_servers': CONF.share.multitenancy_enabled,
+            'replication_type': CONF.share.backend_replication_type
+        }
+        share_type = self.shares_v2_client.create_share_type(
+            name=data_utils.rand_name("tempest-share-type"),
+            extra_specs=extra_specs,
+            cleanup_in_class=False)
+        share = self.create_share(share_type_id=share_type['share_type']['id'],
+                                  share_protocol=self.protocol,
+                                  cleanup_in_class=False)
+        share = self.shares_v2_client.get_share(share['id'])
+        share_server_id = share['share_server_id']
+        dest_host, _ = self._choose_matching_backend_for_share_server(
+            share_server_id)
+        self.create_share_replica(
+            share['id'],
+            cleanup_in_class=False)
+        self.assertRaises(
+            lib_exc.Conflict,
+            self.shares_v2_client.share_server_migration_start,
+            share_server_id,
+            dest_host
+        )
+
+
+class ShareServerMigrationInvalidParametersCIFS(
+    ShareServerMigrationInvalidParametersNFS):
+    protocol = "cifs"
+
+
+class ShareServerErrorStatusOperationCIFS(ShareServerErrorStatusOperationNFS):
+    protocol = "cifs"
+
+
+class ShareServerMigrationStartNegativesCIFS(
+        ShareServerMigrationStartNegativesNFS):
+    protocol = "cifs"
+
+
+class ShareServerMigrationInvalidStatesCIFS(
+        ShareServerMigrationStartInvalidStatesNFS):
+    protocol = "cifs"
diff --git a/manila_tempest_tests/tests/api/base.py b/manila_tempest_tests/tests/api/base.py
index 6bdc8e0..fe4616e 100755
--- a/manila_tempest_tests/tests/api/base.py
+++ b/manila_tempest_tests/tests/api/base.py
@@ -677,8 +677,9 @@
         return rep_domain, pools_in_rep_domain
 
     @classmethod
-    def create_share_replica(cls, share_id, availability_zone, client=None,
-                             cleanup_in_class=False, cleanup=True,
+    def create_share_replica(cls, share_id, availability_zone=None,
+                             client=None, cleanup_in_class=False,
+                             cleanup=True,
                              version=CONF.share.max_api_microversion):
         client = client or cls.shares_v2_client
         replica = client.create_share_replica(
diff --git a/zuul.d/manila-tempest-jobs.yaml b/zuul.d/manila-tempest-jobs.yaml
index 1dfe3f8..cb7c080 100644
--- a/zuul.d/manila-tempest-jobs.yaml
+++ b/zuul.d/manila-tempest-jobs.yaml
@@ -45,6 +45,7 @@
         MANILA_DEFAULT_SHARE_TYPE_EXTRA_SPECS: 'snapshot_support=True create_share_from_snapshot_support=True'
         MANILA_CONFIGURE_DEFAULT_TYPES: true
         MANILA_SHARE_MIGRATION_PERIOD_TASK_INTERVAL: 1
+        MANILA_SERVER_MIGRATION_PERIOD_TASK_INTERVAL: 10
         MANILA_REPLICA_STATE_UPDATE_INTERVAL: 10
 
 
@@ -106,6 +107,7 @@
         MANILA_INSTALL_TEMPEST_PLUGIN_SYSTEMWIDE: false
         MANILA_SERVICE_IMAGE_ENABLED: false
         MANILA_SHARE_MIGRATION_PERIOD_TASK_INTERVAL: 1
+        MANILA_SERVER_MIGRATION_PERIOD_TASK_INTERVAL: 10
         MANILA_REPLICA_STATE_UPDATE_INTERVAL: 10
       devstack_services:
         tls-proxy: true
@@ -241,6 +243,7 @@
               multitenancy_enabled: true
               backend_names: LONDON,PARIS
               multi_backend: true
+              run_share_servers_migration_tests: true
 
 - job:
     name: manila-tempest-plugin-generic
@@ -480,6 +483,7 @@
               run_mount_snapshot_tests: true
               run_replication_tests: true
               run_revert_to_snapshot_tests: true
+              run_share_servers_migration_tests: true
 
 - job:
     name: manila-tempest-plugin-glusterfs-native