test.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import os.path
  17. from flow_sdk.client.api.base import BaseFlowAPI
  18. from flow_sdk.utils import preprocess
  19. class Test(BaseFlowAPI):
  20. def toy(self, guest_party_id: str, host_party_id: str, guest_user_name: str = "", host_user_name: str = "",
  21. task_cores: int = 2, timeout: int = 60):
  22. kwargs = locals()
  23. config_data, dsl_data = preprocess(**kwargs)
  24. conf = self.toy_conf(**kwargs)
  25. return self._post(url='job/submit', json={
  26. 'job_runtime_conf': conf,
  27. 'job_dsl': self.toy_dsl(),
  28. })
  29. @classmethod
  30. def toy_conf(cls, guest_party_id: str, host_party_id: str, guest_user_name: str = "", host_user_name: str = "",
  31. task_cores: int = 2, **kwargs):
  32. job_conf = {
  33. "dsl_version": 2,
  34. "job_parameters": {
  35. },
  36. "role": {
  37. "guest": [
  38. guest_party_id
  39. ],
  40. "host": [
  41. host_party_id
  42. ]
  43. },
  44. "component_parameters": {
  45. "role": {
  46. "guest": {
  47. "0": {
  48. "secure_add_example_0": {
  49. "seed": 123
  50. }
  51. }
  52. },
  53. "host": {
  54. "secure_add_example_0": {
  55. "seed": 321
  56. }
  57. }
  58. },
  59. "common": {
  60. "secure_add_example_0": {
  61. "partition": 4,
  62. "data_num": 1000
  63. }
  64. }
  65. }
  66. }
  67. job_conf["initiator"] = {
  68. "role": "guest",
  69. "party_id": guest_party_id
  70. }
  71. job_conf["role"]["guest"] = [guest_party_id]
  72. job_conf["role"]["host"] = [host_party_id]
  73. job_conf["job_parameters"]["common"] = {
  74. "task_cores": task_cores
  75. }
  76. job_conf["job_parameters"]["role"] = {
  77. "guest": {"0": {"user": guest_user_name}},
  78. "host": {"0": {"user": host_user_name}}
  79. }
  80. return job_conf
  81. @classmethod
  82. def toy_dsl(cls):
  83. dsl = {
  84. "components": {
  85. "secure_add_example_0": {
  86. "module": "SecureAddExample"
  87. }
  88. }
  89. }
  90. return dsl
  91. @classmethod
  92. def check_toy(cls, guest_party_id, job_status, log_dir):
  93. if job_status in {"success", "canceled"}:
  94. info_log = os.path.join(log_dir, "guest", guest_party_id, "INFO.log")
  95. with open(info_log, "r") as fin:
  96. for line in fin:
  97. if line.find("secure_add_guest") != -1:
  98. yield line.strip()
  99. else:
  100. error_log = os.path.join(log_dir, "guest", guest_party_id, "ERROR.log")
  101. with open(error_log, "r") as fin:
  102. for line in fin:
  103. yield line.strip()