From ff78b91c65e514411640ac6edcf031854b917a13 Mon Sep 17 00:00:00 2001
From: Georgy Moshkin <gmoshkin@picodata.io>
Date: Mon, 15 May 2023 11:36:09 +0300
Subject: [PATCH] test: factor out Instance.ddl_create_space method

---
 test/conftest.py     | 26 ++++++++++++++++++-
 test/int/test_ddl.py | 61 +++++++++++---------------------------------
 test/int/test_sql.py | 13 +++-------
 3 files changed, 44 insertions(+), 56 deletions(-)

diff --git a/test/conftest.py b/test/conftest.py
index fb60284123..1fa77c726a 100644
--- a/test/conftest.py
+++ b/test/conftest.py
@@ -16,7 +16,7 @@ from rand.params import generate_seed
 from functools import reduce
 from datetime import datetime
 from shutil import rmtree
-from typing import Any, Callable, Literal, Generator, Iterator
+from typing import Any, Callable, Literal, Generator, Iterator, Dict
 from itertools import count
 from contextlib import contextmanager, suppress
 from dataclasses import dataclass, field
@@ -531,6 +531,30 @@ class Instance:
 
         return t[1]
 
+    def ddl_create_space(
+        self, space_def: Dict[str, Any], wait_index: bool = True, timeout: int = 3
+    ) -> int:
+        """
+        Proposes a space creation ddl prepare operation. Returns the index of
+        the corresponding finilazing ddl commit or ddl abort entry.
+
+        If `wait_index` is `True` will wait for the finilazing entry
+        to be applied for `timeout` seconds.
+        """
+        op = dict(
+            kind="ddl_prepare",
+            schema_version=self.next_schema_version(),
+            ddl=dict(kind="create_space", **space_def),
+        )
+        # TODO: rewrite the test using pico.cas
+        index = self.call("pico.raft_propose", op)
+        # Index of the corresponding ddl abort / ddl commit entry
+        index_fin = index + 1
+        if wait_index:
+            self.call(".proc_sync_raft", index_fin, [timeout, 0])
+
+        return index_fin
+
     def assert_raft_status(self, state, leader_id=None):
         status = self._raft_status()
 
diff --git a/test/int/test_ddl.py b/test/int/test_ddl.py
index 8fa97dc232..c75890f504 100644
--- a/test/int/test_ddl.py
+++ b/test/int/test_ddl.py
@@ -11,11 +11,8 @@ def test_ddl_create_space_bulky(cluster: Cluster):
     assert i2.call("box.space._pico_property:get", "current_schema_version")[1] == 0
 
     # Propose a space creation which will fail
-    op = dict(
-        kind="ddl_prepare",
-        schema_version=i1.next_schema_version(),
-        ddl=dict(
-            kind="create_space",
+    abort_index = i1.ddl_create_space(
+        dict(
             id=666,
             name="stuff",
             format=[dict(name="id", type="unsigned", is_nullable=False)],
@@ -23,11 +20,7 @@ def test_ddl_create_space_bulky(cluster: Cluster):
             distribution=dict(kind="global"),
         ),
     )
-    # TODO: rewrite the test using pico.cas, when it supports ddl
-    index = i1.call("pico.raft_propose", op)
-    abort_index = index + 1
 
-    i1.call(".proc_sync_raft", abort_index, (3, 0))
     i2.call(".proc_sync_raft", abort_index, (3, 0))
 
     # No space was created
@@ -41,11 +34,8 @@ def test_ddl_create_space_bulky(cluster: Cluster):
     assert i2.call("box.space._pico_property:get", "current_schema_version")[1] == 0
 
     # Propose a space creation which will succeed
-    op = dict(
-        kind="ddl_prepare",
-        schema_version=i1.next_schema_version(),
-        ddl=dict(
-            kind="create_space",
+    commit_index = i1.ddl_create_space(
+        dict(
             id=666,
             name="stuff",
             format=[dict(name="id", type="unsigned", is_nullable=False)],
@@ -53,11 +43,8 @@ def test_ddl_create_space_bulky(cluster: Cluster):
             distribution=dict(kind="global"),
         ),
     )
-    # TODO: rewrite the test using pico.cas, when it supports ddl
-    index = i1.call("pico.raft_propose", op)
 
-    i1.call(".proc_sync_raft", index, (3, 0))
-    i2.call(".proc_sync_raft", index, (3, 0))
+    i2.call(".proc_sync_raft", commit_index, (3, 0))
 
     # Schema version was updated
     assert i1.call("box.space._pico_property:get", "current_schema_version")[1] == 2
@@ -134,11 +121,8 @@ def test_ddl_create_sharded_space(cluster: Cluster):
 
     # Propose a space creation which will succeed
     schema_version = i1.next_schema_version()
-    op = dict(
-        kind="ddl_prepare",
-        schema_version=schema_version,
-        ddl=dict(
-            kind="create_space",
+    index = i1.ddl_create_space(
+        dict(
             id=666,
             name="stuff",
             format=[
@@ -150,8 +134,6 @@ def test_ddl_create_sharded_space(cluster: Cluster):
             distribution=dict(kind="sharded_implicitly", sharding_key=["foo", "bar"]),
         ),
     )
-    # TODO: rewrite the test using pico.cas, when it supports ddl
-    index = i1.call("pico.raft_propose", op)
 
     i1.call(".proc_sync_raft", index, (3, 0))
     i2.call(".proc_sync_raft", index, (3, 0))
@@ -163,6 +145,7 @@ def test_ddl_create_sharded_space(cluster: Cluster):
         ["sharded_implicitly", ["foo", "bar"], "murmur3"],
         [
             ["id", "unsigned", False],
+            # Automatically generated by picodata
             ["bucket_id", "unsigned", False],
             ["foo", "integer", False],
             ["bar", "string", False],
@@ -249,11 +232,8 @@ def test_ddl_create_space_partial_failure(cluster: Cluster):
     i3.eval("box.schema.space.create(...)", "space_name_conflict")
 
     # Propose a space creation which will fail
-    op = dict(
-        kind="ddl_prepare",
-        schema_version=i1.next_schema_version(),
-        ddl=dict(
-            kind="create_space",
+    index = i1.ddl_create_space(
+        dict(
             id=666,
             name="space_name_conflict",
             format=[dict(name="id", type="unsigned", is_nullable=False)],
@@ -261,13 +241,9 @@ def test_ddl_create_space_partial_failure(cluster: Cluster):
             distribution=dict(kind="global"),
         ),
     )
-    # TODO: rewrite the test using pico.cas, when it supports ddl
-    prepare_index = i1.call("pico.raft_propose", op)
-    abort_index = prepare_index + 1
 
-    i1.call(".proc_sync_raft", abort_index, (3, 0))
-    i2.call(".proc_sync_raft", abort_index, (3, 0))
-    i3.call(".proc_sync_raft", abort_index, (3, 0))
+    i2.call(".proc_sync_raft", index, (3, 0))
+    i3.call(".proc_sync_raft", index, (3, 0))
 
     # No space was created
     assert i1.call("box.space._pico_space:get", 666) is None
@@ -289,11 +265,8 @@ def test_ddl_from_snapshot(cluster: Cluster):
 
     # TODO: check other ddl operations
     # Propose a space creation which will succeed
-    op = dict(
-        kind="ddl_prepare",
-        schema_version=i1.next_schema_version(),
-        ddl=dict(
-            kind="create_space",
+    index = i1.ddl_create_space(
+        dict(
             id=666,
             name="stuff",
             format=[dict(name="id", type="unsigned", is_nullable=False)],
@@ -301,12 +274,8 @@ def test_ddl_from_snapshot(cluster: Cluster):
             distribution=dict(kind="global"),
         ),
     )
-    # TODO: rewrite the test using pico.cas, when it supports ddl
-    ret = i1.call("pico.raft_propose", op)
 
-    # Make sure everyone is synchronized
-    i1.call(".proc_sync_raft", ret, (3, 0))
-    i2.call(".proc_sync_raft", ret, (3, 0))
+    i2.call(".proc_sync_raft", index, (3, 0))
 
     tt_space_def = [
         666,
diff --git a/test/int/test_sql.py b/test/int/test_sql.py
index 5768c6fed9..a2cd2ddecb 100644
--- a/test/int/test_sql.py
+++ b/test/int/test_sql.py
@@ -64,11 +64,8 @@ def test_select(cluster: Cluster):
     cluster.deploy(instance_count=2)
     i1, i2 = cluster.instances
 
-    op = dict(
-        kind="ddl_prepare",
-        schema_version=i1.next_schema_version(),
-        ddl=dict(
-            kind="create_space",
+    index = i1.ddl_create_space(
+        dict(
             id=666,
             name="T",
             format=[
@@ -77,11 +74,9 @@ def test_select(cluster: Cluster):
             primary_key=[dict(field="A")],
             # sharding function is implicitly murmur3
             distribution=dict(kind="sharded_implicitly", sharding_key=["A"]),
-        ),
+        )
     )
-    # TODO: rewrite the test using pico.cas, when it supports ddl
-    index = i1.call("pico.raft_propose", op)
-    i1.call(".proc_sync_raft", index, [3, 0])
+    i2.call(".proc_sync_raft", index, [3, 0])
 
     data = i1.sql("""insert into t values(1);""")
     assert data["row_count"] == 1
-- 
GitLab