github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/tests/openapi/client/openapi_source_check (about)

     1  #!/usr/bin/env python
     2  import sys
     3  import requests
     4  import ssl
     5  
     6  SOURCE1_NAME = "mysql-01"
     7  SOURCE2_NAME = "mysql-02"
     8  WORKER1_NAME = "worker1"
     9  WORKER2_NAME = "worker2"
    10  
    11  
    12  API_ENDPOINT = "http://127.0.0.1:8261/api/v1/sources"
    13  API_ENDPOINT_NOT_LEADER = "http://127.0.0.1:8361/api/v1/sources"
    14  
    15  API_ENDPOINT_HTTPS = "https://127.0.0.1:8261/api/v1/sources"
    16  API_ENDPOINT_NOT_LEADER_HTTPS = "https://127.0.0.1:8361/api/v1/sources"
    17  
    18  
    19  
    20  def create_source_failed():
    21      resp = requests.post(url=API_ENDPOINT)
    22      assert resp.status_code == 400
    23      print("create_source_failed resp=", resp.json())
    24  
    25  
    26  def create_source1_success():
    27      req = {
    28          "source": {
    29              "case_sensitive": False,
    30              "enable": True,
    31              "enable_gtid": False,
    32              "host": "127.0.0.1",
    33              "password": "123456",
    34              "port": 3306,
    35              "source_name": SOURCE1_NAME,
    36              "user": "root",
    37          }
    38      }
    39      resp = requests.post(url=API_ENDPOINT, json=req)
    40      print("create_source1_success resp=", resp.json())
    41      assert resp.status_code == 201
    42  
    43  
    44  def create_source2_success():
    45      req = {
    46          "source": {
    47              "enable": True,
    48              "case_sensitive": False,
    49              "enable_gtid": False,
    50              "host": "127.0.0.1",
    51              "password": "123456",
    52              "port": 3307,
    53              "source_name": SOURCE2_NAME,
    54              "user": "root",
    55          }
    56      }
    57      resp = requests.post(url=API_ENDPOINT, json=req)
    58      print("create_source1_success resp=", resp.json())
    59      assert resp.status_code == 201
    60  
    61  def create_source_success_https(ssl_ca, ssl_cert, ssl_key):
    62      req = {
    63          "source": {
    64              "case_sensitive": False,
    65              "enable": True,
    66              "enable_gtid": False,
    67              "host": "127.0.0.1",
    68              "password": "123456",
    69              "port": 3306,
    70              "source_name": SOURCE1_NAME,
    71              "user": "root",
    72          }
    73      }
    74      resp = requests.post(url=API_ENDPOINT_HTTPS, json=req, verify=ssl_ca, cert=(ssl_cert, ssl_key))
    75      print("create_source_success_https resp=", resp.json())
    76      assert resp.status_code == 201
    77  
    78  def update_source1_without_password_success():
    79      req = {
    80          "source": {
    81              "case_sensitive": False,
    82              "enable": True,
    83              "enable_gtid": False,
    84              "host": "127.0.0.1",
    85              "port": 3306,
    86              "source_name": SOURCE1_NAME,
    87              "user": "root",
    88          }
    89      }
    90      resp = requests.put(url=API_ENDPOINT + "/" + SOURCE1_NAME, json=req)
    91      print("update_source1_without_password_success resp=", resp.json())
    92      assert resp.status_code == 200
    93  
    94  def list_source_success(source_count):
    95      resp = requests.get(url=API_ENDPOINT)
    96      assert resp.status_code == 200
    97      data = resp.json()
    98      print("list_source_by_openapi_success resp=", data)
    99      assert data["total"] == int(source_count)
   100  
   101  def list_source_success_https(source_count, ssl_ca, ssl_cert, ssl_key):
   102      resp = requests.get(url=API_ENDPOINT_HTTPS, verify=ssl_ca, cert=(ssl_cert, ssl_key))
   103      assert resp.status_code == 200
   104      data = resp.json()
   105      print("list_source_success_https resp=", data)
   106      assert data["total"] == int(source_count)
   107  
   108  def list_source_with_status_success(source_count, status_count):
   109      resp = requests.get(url=API_ENDPOINT + "?with_status=true")
   110      assert resp.status_code == 200
   111      data = resp.json()
   112      print("list_source_with_status_success resp=", data)
   113      assert data["total"] == int(source_count)
   114      for i in range(int(source_count)):
   115          assert len(data["data"][i]["status_list"]) == int(status_count)
   116  
   117  
   118  def list_source_with_reverse(source_count):
   119      resp = requests.get(url=API_ENDPOINT_NOT_LEADER)
   120      assert resp.status_code == 200
   121      data = resp.json()
   122      print("list_source_with_reverse resp=", data)
   123      assert data["total"] == int(source_count)
   124  
   125  def list_source_with_reverse_https(source_count, ssl_ca, ssl_cert, ssl_key):
   126      resp = requests.get(url=API_ENDPOINT_NOT_LEADER_HTTPS, verify=ssl_ca, cert=(ssl_cert, ssl_key))
   127      assert resp.status_code == 200
   128      data = resp.json()
   129      print("list_source_with_reverse_https resp=", data)
   130      assert data["total"] == int(source_count)
   131  
   132  def delete_source_success(source_name):
   133      resp = requests.delete(url=API_ENDPOINT + "/" + source_name)
   134      assert resp.status_code == 204
   135      print("delete_source_success")
   136  
   137  
   138  def delete_source_with_force_success(source_name):
   139      resp = requests.delete(url=API_ENDPOINT + "/" + source_name + "?force=true")
   140      assert resp.status_code == 204
   141      print("delete_source_with_force_success")
   142  
   143  
   144  def delete_source_failed(source_name):
   145      resp = requests.delete(url=API_ENDPOINT + "/" + source_name)
   146      print("delete_source_failed msg=", resp.json())
   147      assert resp.status_code == 400
   148  
   149  
   150  def enable_relay_failed(source_name, worker_name):
   151      url = API_ENDPOINT + "/" + source_name + "/relay/enable"
   152      req = {
   153          "worker_name_list": [worker_name],
   154      }
   155      resp = requests.post(url=url, json=req)
   156      print("enable_relay_failed resp=", resp.json())
   157      assert resp.status_code == 400
   158  
   159  
   160  def enable_relay_success(source_name, worker_name):
   161      url = API_ENDPOINT + "/" + source_name + "/relay/enable"
   162      req = {
   163          "purge": {"interval": 3600, "expires": 0, "remain_space": 15},
   164      }
   165      resp = requests.post(url=url, json=req)
   166      assert resp.status_code == 200
   167  
   168  
   169  def enable_relay_success_with_two_worker(source_name, worker1_name, worker2_name):
   170      url = API_ENDPOINT + "/" + source_name + "/relay/enable"
   171      req = {
   172          "worker_name_list": [worker1_name, worker2_name],
   173          "purge": {"interval": 3600, "expires": 0, "remain_space": 15},
   174      }
   175      resp = requests.post(url=url, json=req)
   176      assert resp.status_code == 200
   177  
   178  
   179  def disable_relay_failed(source_name, worker_name):
   180      url = API_ENDPOINT + "/" + source_name + "/relay/disable"
   181      req = {
   182          "worker_name_list": [worker_name],
   183      }
   184      resp = requests.post(url=url, json=req)
   185      print("disable_relay_failed resp=", resp.json())
   186      assert resp.status_code == 400
   187  
   188  
   189  def disable_relay_success(source_name, worker_name):
   190      url = API_ENDPOINT + "/" + source_name + "/relay/disable"
   191      req = {
   192          "worker_name_list": [worker_name],
   193      }
   194      resp = requests.post(url=url, json=req)
   195      assert resp.status_code == 200
   196  
   197  
   198  def purge_relay_success(source_name, relay_binlog_name, relay_dir=""):
   199      url = API_ENDPOINT + "/" + source_name + "/relay/purge"
   200      req = {
   201          "relay_binlog_name": relay_binlog_name,
   202          "relay_dir": relay_dir,
   203      }
   204      resp = requests.post(url=url, json=req)
   205      if resp.status_code != 200:
   206          print("purge relay  failed resp=", resp.json())
   207      assert resp.status_code == 200
   208  
   209  
   210  def get_source_status_failed(source_name):
   211      url = API_ENDPOINT + "/" + source_name + "/status"
   212      resp = requests.get(url=url)
   213      print("get_source_status_failed resp=", resp.json())
   214      assert resp.status_code == 400
   215  
   216  
   217  def get_source_status_success(source_name, status_count=1):
   218      url = API_ENDPOINT + "/" + source_name + "/status"
   219      resp = requests.get(url=url)
   220      assert resp.status_code == 200
   221      print("get_source_status_success resp=", resp.json())
   222      assert int(status_count) == resp.json()["total"]
   223  
   224  
   225  def get_source_status_success_with_relay(source_name, source_idx=0):
   226      url = API_ENDPOINT + "/" + source_name + "/status"
   227      resp = requests.get(url=url)
   228      assert resp.status_code == 200
   229      res = resp.json()
   230      print("get_source_status_success_with_relay resp=", res)
   231      assert res["data"][int(source_idx)]["relay_status"] is not None
   232  
   233  
   234  def get_source_status_success_no_relay(source_name, source_idx=0):
   235      url = API_ENDPOINT + "/" + source_name + "/status"
   236      resp = requests.get(url=url)
   237      assert resp.status_code == 200
   238      res = resp.json()
   239      print("get_source_status_success_no_relay resp=", res)
   240      assert res["data"][int(source_idx)].get("relay_status") is None
   241  
   242  
   243  def transfer_source_success(source_name, worker_name):
   244      url = API_ENDPOINT + "/" + source_name + "/transfer"
   245      req = {
   246          "worker_name": worker_name,
   247      }
   248      resp = requests.post(url=url, json=req)
   249      assert resp.status_code == 200
   250  
   251  
   252  def get_source_schemas_and_tables_success(source_name, schema_name="", table_name=""):
   253      schema_url = API_ENDPOINT + "/" + source_name + "/schemas"
   254      schema_resp = requests.get(url=schema_url)
   255      assert schema_resp.status_code == 200
   256      print("get_source_schemas_and_tables_success schema_resp=", schema_resp.json())
   257      schema_list = schema_resp.json()
   258      assert schema_name in schema_list
   259  
   260      table_url = API_ENDPOINT + "/" + source_name + "/schemas/openapi"
   261      table_resp = requests.get(url=table_url)
   262      print("get_source_schemas_and_tables_success table_resp=", table_resp.json())
   263      table_list = table_resp.json()
   264      assert table_name in table_list
   265  
   266  
   267  if __name__ == "__main__":
   268      FUNC_MAP = {
   269          "create_source_failed": create_source_failed,
   270          "create_source1_success": create_source1_success,
   271          "create_source2_success": create_source2_success,
   272          "create_source_success_https": create_source_success_https,
   273          "update_source1_without_password_success": update_source1_without_password_success,
   274          "list_source_success": list_source_success,
   275          "list_source_success_https": list_source_success_https,
   276          "list_source_with_reverse_https": list_source_with_reverse_https,
   277          "list_source_with_reverse": list_source_with_reverse,
   278          "list_source_with_status_success": list_source_with_status_success,
   279          "delete_source_failed": delete_source_failed,
   280          "delete_source_success": delete_source_success,
   281          "delete_source_with_force_success": delete_source_with_force_success,
   282          "enable_relay_failed": enable_relay_failed,
   283          "enable_relay_success": enable_relay_success,
   284          "enable_relay_success_with_two_worker": enable_relay_success_with_two_worker,
   285          "disable_relay_failed": disable_relay_failed,
   286          "disable_relay_success": disable_relay_success,
   287          "purge_relay_success": purge_relay_success,
   288          "get_source_status_failed": get_source_status_failed,
   289          "get_source_status_success": get_source_status_success,
   290          "get_source_status_success_with_relay": get_source_status_success_with_relay,
   291          "get_source_status_success_no_relay": get_source_status_success_no_relay,
   292          "transfer_source_success": transfer_source_success,
   293          "get_source_schemas_and_tables_success": get_source_schemas_and_tables_success,
   294      }
   295  
   296      func = FUNC_MAP[sys.argv[1]]
   297      if len(sys.argv) >= 2:
   298          func(*sys.argv[2:])
   299      else:
   300          func()