Fix share server info in CGs created from CGs

Currently when a POST request is made to /consistency-groups
with a source cg-snapshot, the API does not register the share
network information (share_server_id and share_network_id) in
the database row newly created for the CG being created.

This information is essential to any shares that are being created
along with the consistency group.

- Disallow providing a share_network_id when using a source cg_snapshot_id
- Copy share network information from the parent CG
- Fix the share_server_id that was incorrect in the API response

 APIImpact

 Closes-Bug: #1571594
 Closes-Bug: #1572742

Change-Id: I1c3581c81e0b845f46eef3cd0acddb55850447a5
diff --git a/manila_tempest_tests/tests/api/admin/test_consistency_group_actions.py b/manila_tempest_tests/tests/api/admin/test_consistency_group_actions.py
index 085cb1e..21704ff 100644
--- a/manila_tempest_tests/tests/api/admin/test_consistency_group_actions.py
+++ b/manila_tempest_tests/tests/api/admin/test_consistency_group_actions.py
@@ -43,6 +43,8 @@
         # Create a consistency group
         cls.consistency_group = cls.create_consistency_group(
             share_type_ids=[cls.share_type['id'], cls.share_type2['id']])
+        cls.consistency_group = cls.shares_v2_client.get_consistency_group(
+            cls.consistency_group['id'])
 
     @test.attr(type=["gate", ])
     def test_create_cg_from_cgsnapshot_with_multiple_share_types_v2_4(self):
@@ -103,9 +105,13 @@
             version='2.4',
         )
 
-        self.create_consistency_group(cleanup_in_class=False,
-                                      source_cgsnapshot_id=cgsnapshot['id'],
-                                      version='2.4')
+        new_cg = self.create_consistency_group(
+            cleanup_in_class=False, source_cgsnapshot_id=cgsnapshot['id'],
+            version='2.4')
+        new_cg_shares = self.shares_v2_client.list_shares(
+            detailed=True,
+            params={'consistency_group_id': new_cg['id']},
+            version='2.4')
 
         # TODO(akerr): Skip until bug 1483886 is resolved
         # Verify that the new shares correspond to correct share types
@@ -117,3 +123,29 @@
         #                  'Expected shares of types %s, got %s.' % (
         #                      sorted(expected_share_types),
         #                     sorted(actual_share_types)))
+
+        # Ensure that share_server information of the child CG and associated
+        # shares match with that of the parent CG
+        self.assertEqual(self.consistency_group['share_network_id'],
+                         new_cg['share_network_id'])
+        self.assertEqual(self.consistency_group['share_server_id'],
+                         new_cg['share_server_id'])
+
+        for share in new_cg_shares:
+            msg = ('Share %(share)s has %(attr)s=%(value)s and it does not '
+                   'match that of the parent CG where %(attr)s=%(orig)s.')
+            payload = {
+                'share': share['id'],
+                'attr': 'share_network_id',
+                'value': share['share_network_id'],
+                'orig': self.consistency_group['share_network_id'],
+            }
+            self.assertEqual(self.consistency_group['share_network_id'],
+                             share['share_network_id'], msg % payload)
+
+            payload.update({'attr': 'share_server_id',
+                            'value': share['share_server_id'],
+                            'orig': self.consistency_group['share_server_id'],
+                            })
+            self.assertEqual(self.consistency_group['share_server_id'],
+                             share['share_server_id'], msg % payload)
diff --git a/manila_tempest_tests/tests/api/admin/test_consistency_groups.py b/manila_tempest_tests/tests/api/admin/test_consistency_groups.py
index a121c57..b064dcd 100644
--- a/manila_tempest_tests/tests/api/admin/test_consistency_groups.py
+++ b/manila_tempest_tests/tests/api/admin/test_consistency_groups.py
@@ -66,3 +66,33 @@
                          '%s. Expected %s, got %s' % (consistency_group['id'],
                                                       expected_share_types,
                                                       actual_share_types))
+
+    @testtools.skipIf(
+        not CONF.share.multitenancy_enabled, "Only for multitenancy.")
+    def test_create_cg_from_cgsnapshot_verify_share_server_information(self):
+        # Create a consistency group
+        orig_consistency_group = self.create_consistency_group(
+            cleanup_in_class=False,
+            share_type_ids=[self.share_type['id']],
+            version='2.4')
+
+        # Get latest CG information
+        orig_consistency_group = self.shares_v2_client.get_consistency_group(
+            orig_consistency_group['id'], version='2.4')
+
+        # Assert share server information
+        self.assertIsNotNone(orig_consistency_group['share_network_id'])
+        self.assertIsNotNone(orig_consistency_group['share_server_id'])
+
+        cg_snapshot = self.create_cgsnapshot_wait_for_active(
+            orig_consistency_group['id'], cleanup_in_class=False,
+            version='2.4')
+        new_consistency_group = self.create_consistency_group(
+            cleanup_in_class=False, version='2.4',
+            source_cgsnapshot_id=cg_snapshot['id'])
+
+        # Assert share server information
+        self.assertEqual(orig_consistency_group['share_network_id'],
+                         new_consistency_group['share_network_id'])
+        self.assertEqual(orig_consistency_group['share_server_id'],
+                         new_consistency_group['share_server_id'])
diff --git a/manila_tempest_tests/tests/api/base.py b/manila_tempest_tests/tests/api/base.py
index 387b5d5..6a75bee 100644
--- a/manila_tempest_tests/tests/api/base.py
+++ b/manila_tempest_tests/tests/api/base.py
@@ -431,8 +431,9 @@
     def create_consistency_group(cls, client=None, cleanup_in_class=True,
                                  share_network_id=None, **kwargs):
         client = client or cls.shares_v2_client
-        kwargs['share_network_id'] = (share_network_id or
-                                      client.share_network_id or None)
+        if kwargs.get('source_cgsnapshot_id') is None:
+            kwargs['share_network_id'] = (share_network_id or
+                                          client.share_network_id or None)
         consistency_group = client.create_consistency_group(**kwargs)
         resource = {
             "type": "consistency_group",
diff --git a/manila_tempest_tests/tests/api/test_consistency_group_actions.py b/manila_tempest_tests/tests/api/test_consistency_group_actions.py
index 59e07be..6a80590 100644
--- a/manila_tempest_tests/tests/api/test_consistency_group_actions.py
+++ b/manila_tempest_tests/tests/api/test_consistency_group_actions.py
@@ -263,6 +263,13 @@
             version='2.4'
         )
 
+        new_consistency_group = self.shares_v2_client.get_consistency_group(
+            new_consistency_group['id'], version='2.4')
+
+        # Verify that share_network information matches source CG
+        self.assertEqual(self.cg['share_network_id'],
+                         new_consistency_group['share_network_id'])
+
         new_shares = self.shares_v2_client.list_shares(
             params={'consistency_group_id': new_consistency_group['id']},
             detailed=True,
@@ -292,6 +299,8 @@
                     # TODO(akerr): Add back assert when bug 1483886 is fixed
                     # self.assertEqual(member['share_type_id'],
                     #                  share['share_type'])
+                    self.assertEqual(self.cg['share_network_id'],
+                                     share['share_network_id'])
 
 
 @testtools.skipUnless(CONF.share.run_consistency_group_tests,
diff --git a/manila_tempest_tests/tests/api/test_consistency_groups.py b/manila_tempest_tests/tests/api/test_consistency_groups.py
index bebd88f..5af251a 100644
--- a/manila_tempest_tests/tests/api/test_consistency_groups.py
+++ b/manila_tempest_tests/tests/api/test_consistency_groups.py
@@ -125,7 +125,7 @@
         self.assertEmpty(new_shares,
                          'Expected 0 new shares, got %s' % len(new_shares))
 
-        msg = 'Expected cgsnapshot_id %s as source of share %s' % (
+        msg = 'Expected cgsnapshot_id %s as source of consistency group %s' % (
             cgsnapshot['id'], new_consistency_group['source_cgsnapshot_id'])
         self.assertEqual(new_consistency_group['source_cgsnapshot_id'],
                          cgsnapshot['id'], msg)
@@ -135,3 +135,11 @@
                                 new_consistency_group['share_types']))
         self.assertEqual(sorted(consistency_group['share_types']),
                          sorted(new_consistency_group['share_types']), msg)
+
+        # Assert the share_network information is the same
+        msg = 'Expected share_network %s as share_network of cg %s' % (
+            consistency_group['share_network_id'],
+            new_consistency_group['share_network_id'])
+        self.assertEqual(consistency_group['share_network_id'],
+                         new_consistency_group['share_network_id'],
+                         msg)