tmss_test_data_rest.py 21.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#!/usr/bin/env python3

# Copyright (C) 2018    ASTRON (Netherlands Institute for Radio Astronomy)
# P.O. Box 2, 7990 AA Dwingeloo, The Netherlands
#
# This file is part of the LOFAR software suite.
# The LOFAR software suite is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# The LOFAR software suite is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.

################################################################################################
# the methods below can be used to to HTTP REST calls to the django server and check the results
################################################################################################

from datetime import datetime
import uuid
import requests
import json

class TMSSRESTTestDataCreator():
    def __init__(self, django_api_url: str, auth: requests.auth.HTTPBasicAuth):
        self.django_api_url = django_api_url
        self.auth = auth

34 35 36 37
    def post_data_and_get_response(self, data, url_postfix):
        """POST the given data the self.django_api_url+url_postfix, and return the response"""
        return requests.post(self.django_api_url + url_postfix, json=data, auth=self.auth)

38 39
    def post_data_and_get_url(self, data, url_postfix):
        """POST the given data the self.django_api_url+url_postfix, and return the response's url"""
40
        return json.loads(self.post_data_and_get_response(data, url_postfix).content.decode('utf-8'))['url']
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

    #######################################################
    # the methods below can be used to create test data
    # naming convention is: <django_model_name>()
    #######################################################
    
    
    def GeneratorTemplate(self, name="generatortemplate"):
        return {"name": name,
                "description": 'My one observation',
                "version": 'v0.314159265359',
                "schema": {"mykey": "my value"},
                "create_function": 'Funky',
                "tags": ["TMSS", "TESTING"]}
    
    def SchedulingUnitTemplate(self, name="schedulingunittemplate1"):
        return { "name": name,
                 "description": 'My description',
                 "version": 'v0.314159265359',
                 "schema": {"mykey": "my value"},
                 "tags": ["TMSS", "TESTING"]}
    
    def TaskTemplate(self, name="tasktemplate1"):
        return {"name": name,
                "description": 'My one observation',
                "version": 'v0.314159265359',
                "schema": {"mykey": "my value"},
                "tags": ["TMSS", "TESTING"],
                "validation_code_js": "???"}
    
    def WorkRelationSelectionTemplate(self, name="workrelationselectiontemplate1"):
        return {"name": name,
                "description": 'My one observation',
                "version": 'v0.314159265359',
                "schema": {"mykey": "my value"},
                "tags": ["TMSS", "TESTING"]}
    
    def TaskConnectors(self, role="correlator", input_of_url=None, output_of_url=None):
        if input_of_url is None:
            input_of_url = self.post_data_and_get_url(self.TaskTemplate(), '/task_template/')
    
        if output_of_url is None:
            output_of_url = self.post_data_and_get_url(self.TaskTemplate(), '/task_template/')
    
        return {"role": self.django_api_url + '/role/%s/'%role,
                "datatype": self.django_api_url + '/datatype/image/',
                "dataformats": [self.django_api_url + '/dataformat/Beamformed/'],
                "output_of": output_of_url,
                "input_of": input_of_url,
                "tags": []}
    
    def DefaultTemplates(self, name="defaulttemplate"):
        return {"name": name,
                "template": None,
                "tags": []}
    
    def Cycle(self, description="my cycle description"):
        return {"name": 'my_cycle_' + str(uuid.uuid4()),
                "description": description,
                "tags": [],
                "start": datetime.utcnow().isoformat(),
                "stop": datetime.utcnow().isoformat(),
                "number": 1,
                "standard_hours": 2,
                "expert_hours": 3,
                "filler_hours": 4,
                "projects": []}
    
    def Project(self, description="my project description"):
        return {"name": 'my_project_' + str(uuid.uuid4()),
                "description": description,
                "tags": [],
                "priority": 1,
                "can_trigger": False,
                "private_data": True}
    
    def SchedulingSet(self, name="my_scheduling_set", project_url=None, generator_template_url=None):
        if project_url is None:
            project_url = self.post_data_and_get_url(self.Project(), '/project/')
    
        if generator_template_url is None:
            generator_template_url = self.post_data_and_get_url(self.GeneratorTemplate(), '/generator_template/')
    
        return {"name": name,
                "description": "This is my scheduling set",
                "tags": [],
                "generator_doc": "{}",
                "project": project_url,
                "generator_template": generator_template_url,
                "generator_source": None,
                "scheduling_unit_drafts": []}
    
    def SchedulingUnitDraft(self, name="my_scheduling_unit_draft", scheduling_set_url=None, template_url=None):
        if scheduling_set_url is None:
            scheduling_set_url = self.post_data_and_get_url(self.SchedulingSet(), '/scheduling_set/')
    
        if template_url is None:
            template_url = self.post_data_and_get_url(self.SchedulingUnitTemplate(), '/scheduling_unit_template/')
    
        return {"name": name,
                "description": "This is my run draft",
                "tags": [],
                "requirements_doc": "{}",
                "copy_reason": self.django_api_url + '/copy_reason/template/',
                "generator_instance_doc": "{}",
                "copies": None,
                "scheduling_set": scheduling_set_url,
                "requirements_template": template_url,
                "related_scheduling_unit_blueprint": [],
                "task_drafts": []}
    
    def TaskDraft(self, name='my_task_draft', scheduling_unit_draft_url=None, template_url=None):
        if scheduling_unit_draft_url is None:
            scheduling_unit_draft_url = self.post_data_and_get_url(self.SchedulingUnitDraft(), '/scheduling_unit_draft/')
    
        if template_url is None:
            template_url = self.post_data_and_get_url(self.TaskTemplate(), '/task_template/')
    
        return {"name": name,
                "description": "This is my task draft",
                "tags": [],
                "specifications_doc": "{}",
                "copy_reason": self.django_api_url + '/copy_reason/template/',
                "copies": None,
                "scheduling_unit_draft": scheduling_unit_draft_url,
                "specifications_template": template_url,
                'related_task_blueprint': [],
                'produced_by': [],
                'consumed_by': []}
    
    
172
    def TaskRelationDraft(self, producer_url=None, consumer_url=None, template_url=None, input_url=None, output_url=None):
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
        if producer_url is None:
            producer_url = self.post_data_and_get_url(self.TaskDraft(), '/task_draft/')
    
        if consumer_url is None:
            consumer_url = self.post_data_and_get_url(self.TaskDraft(),'/task_draft/')
    
        if template_url is None:
            template_url = self.post_data_and_get_url(self.WorkRelationSelectionTemplate(), '/work_relation_selection_template/')
    
        if input_url is None:
            input_url = self.post_data_and_get_url(self.TaskConnectors(), '/task_connectors/')
    
        if output_url is None:
            output_url = self.post_data_and_get_url(self.TaskConnectors(), '/task_connectors/')
    
        return {"tags": [],
                "selection_doc": "{}",
                "dataformat": self.django_api_url + "/dataformat/Beamformed/",
                "producer": producer_url,
                "consumer": consumer_url,
                "input": input_url,
                "output": output_url,
                "selection_template": template_url,
                'related_task_relation_blueprint': []}
    
    def SchedulingUnitBlueprint(self, name="my_scheduling_unit_blueprint", scheduling_unit_draft_url=None, template_url=None):
        if scheduling_unit_draft_url is None:
            scheduling_unit_draft_url = self.post_data_and_get_url(self.SchedulingUnitDraft(), '/scheduling_unit_draft/')
    
        if template_url is None:
            template_url = self.post_data_and_get_url(self.SchedulingUnitTemplate(), '/scheduling_unit_template/')
    
        return {"name": name,
                "description": "This is my run blueprint",
                "tags": [],
                "requirements_doc": "{}",
                "do_cancel": False,
                "draft": scheduling_unit_draft_url,
                "requirements_template": template_url}
    
    def TaskBlueprint(self, name="my_TaskBlueprint", draft_url=None, template_url=None, scheduling_unit_blueprint_url=None):
        if draft_url is None:
            draft_url = self.post_data_and_get_url(self.TaskDraft(), '/task_draft/')
    
        if template_url is None:
            template_url = self.post_data_and_get_url(self.TaskTemplate(), '/task_template/')
    
        if scheduling_unit_blueprint_url is None:
            scheduling_unit_blueprint_url = self.post_data_and_get_url(self.SchedulingUnitBlueprint(), '/scheduling_unit_blueprint/')
    
        return {"name": name,
                "description": "This is my work request blueprint",
                "tags": [],
                "specifications_doc": "{}",
                "do_cancel": False,
                "draft": draft_url,
                "specifications_template": template_url,
                "scheduling_unit_blueprint": scheduling_unit_blueprint_url,
                "subtasks": [],
                "produced_by": [],
                "consumed_by": []}
    
    def TaskRelationBlueprint(self, draft_url=None, template_url=None, input_url=None, output_url=None, consumer_url=None, producer_url=None):
        if draft_url is None:
            draft_url = self.post_data_and_get_url(self.TaskRelationDraft(), '/task_relation_draft/')
    
        if producer_url is None:
            producer_url = self.post_data_and_get_url(self.TaskBlueprint(), '/task_blueprint/')
    
        if consumer_url is None:
            consumer_url = self.post_data_and_get_url(self.TaskBlueprint(),'/task_blueprint/')
    
        if template_url is None:
            template_url = self.post_data_and_get_url(self.WorkRelationSelectionTemplate(), '/work_relation_selection_template/')
    
        if input_url is None:
            input_url = self.post_data_and_get_url(self.TaskConnectors(), '/task_connectors/')
    
        if output_url is None:
            output_url = self.post_data_and_get_url(self.TaskConnectors(), '/task_connectors/')
    
        # test data
        return {"tags": [],
                "selection_doc": "{}",
                "dataformat": self.django_api_url + '/dataformat/MeasurementSet/',
                "input": input_url,
                "output": output_url,
                "draft": draft_url,
                "selection_template": template_url,
                "producer": producer_url,
                "consumer": consumer_url}
    
265 266 267 268
    def SubtaskTemplate(self, name="subtask1", schema=None):
        if schema is None:
            schema = {}

269 270 271 272
        return {"type": self.django_api_url + '/subtask_type/copy/',
                       "name": name,
                       "description": 'My one observation',
                       "version": 'v0.314159265359',
273
                       "schema": schema,
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
                       "realtime": True,
                       "queue": False,
                       "tags": ["TMSS", "TESTING"]}
    
    def DataproductSpecificationsTemplate(self, name="my_DataproductSpecificationsTemplate"):
        return  {"name": name,
                 "description": 'My one date',
                 "version": 'v0.314159265359',
                 "schema": {"mykey": "my value"},
                 "tags": ["TMSS", "TESTING"]}
    
    def DataproductFeedbackTemplate(self, name="my_DataproductFeedbackTemplate"):
        return  {"name": name,
                 "description": 'My one date',
                 "version": 'v0.314159265359',
                 "schema": {"mykey": "my value"},
                 "tags": ["TMSS", "TESTING"]}
    
    def DefaultSubtaskTemplates(self, name=None, template_url=None):
        if template_url is None:
            template_url = self.post_data_and_get_url(self.SubtaskTemplate(), '/subtask_template/')
    
        return {"name": name if name else "default_template_%s" % uuid.uuid4(),
                "template": template_url,
                "tags": []}
    
    def Cluster(self, name=None):
        return {"name": name if name else "Cluster %s" % uuid.uuid4(),
                "description": 'My one cluster',
                "location": "upstairs",
                "tags": ['tmss', 'testing']}
    
306
    def Subtask(self, cluster_url=None, task_blueprint_url=None, specifications_template_url=None, specifications_doc=None):
307 308 309 310 311 312 313 314
        if cluster_url is None:
            cluster_url = self.post_data_and_get_url(self.Cluster(), '/cluster/')
    
        if task_blueprint_url is None:
            task_blueprint_url = self.post_data_and_get_url(self.TaskBlueprint(), '/task_blueprint/')
    
        if specifications_template_url is None:
            specifications_template_url = self.post_data_and_get_url(self.SubtaskTemplate(), '/subtask_template/')
315 316 317 318

        if specifications_doc is None:
            specifications_doc = "{}"

319 320 321
        return {"start_time": datetime.utcnow().isoformat(),
                "stop_time": datetime.utcnow().isoformat(),
                "state": self.django_api_url + '/subtask_state/scheduling/',
322
                "specifications_doc": specifications_doc,
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
                "task_blueprint": task_blueprint_url,
                "specifications_template": specifications_template_url,
                "tags": ["TMSS", "TESTING"],
                "do_cancel": datetime.utcnow().isoformat(),
                "priority": 1,
                "schedule_method": self.django_api_url + '/schedule_method/manual/',
                "cluster": cluster_url,
                "scheduler_input_doc": "{}" }
    
    def SubtaskOutput(self, subtask_url=None, subtask_connector_url=None):
        if subtask_url is None:
            subtask_url = self.post_data_and_get_url(self.Subtask(), '/subtask/')
    
        if subtask_connector_url is None:
            subtask_connector_url = self.post_data_and_get_url(self.SubtaskConnector(), '/subtask_connector/')
    
        return {"subtask": subtask_url,
                "connector": subtask_connector_url,
                "tags": []}
    
    def SubtaskConnector(self, input_of_url=None, output_of_url=None):
        if input_of_url is None:
            input_of_url = self.post_data_and_get_url(self.SubtaskTemplate(), '/subtask_template/')
    
        if output_of_url is None:
            output_of_url = self.post_data_and_get_url(self.SubtaskTemplate(), '/subtask_template/')
    
        return {"role": self.django_api_url + '/role/correlator/',
                "datatype": self.django_api_url + '/datatype/image/',
                "dataformats": [self.django_api_url + '/dataformat/Beamformed/'],
                "output_of": output_of_url,
                "input_of": input_of_url,
                "tags": []}
    
    def Dataproduct(self, filename="my_filename", specifications_template_url=None, subtask_output_url=None, dataproduct_feedback_template_url=None):
        if specifications_template_url is None:
            specifications_template_url = self.post_data_and_get_url(self.SubtaskTemplate(), '/dataproduct_specifications_template/')
    
        if subtask_output_url is None:
            subtask_output_url = self.post_data_and_get_url(self.SubtaskOutput(), '/subtask_output/')
    
        if dataproduct_feedback_template_url is None:
            dataproduct_feedback_template_url = self.post_data_and_get_url(self.DataproductFeedbackTemplate(), '/dataproduct_feedback_template/')
    
        return {"filename": "my.file",
                "directory": "/home/boskabouter/",
                "dataformat": self.django_api_url + '/dataformat/Beamformed/',
                "deleted_since": None,
                "pinned_since": None,
                "specifications_doc": "{}",
                "specifications_template": specifications_template_url,
                "tags": ["TMSS", "TESTING"],
                "producer": subtask_output_url,
                "do_cancel": datetime.utcnow().isoformat(),
                "expected_size": 1234,
                "size": 123,
                "feedback_doc": "{}",
                "feedback_template": dataproduct_feedback_template_url
                }
    
    def AntennaSet(self, name="antennaset1"):
        return {"name": name,
                "description": 'My one observation',
                "station_type": self.django_api_url + '/station_type/core/',
                "rcus": [1,2,3,4,5],
                "inputs": ['input1', 'input2'],
                "tags": ['tmss', 'testing']}
    
    def DataproductTransform(self, input_dataproduct_url=None, output_dataproduct_url=None):
        if input_dataproduct_url is None:
            input_dataproduct_url = self.post_data_and_get_url(self.Dataproduct(), '/dataproduct/')
    
        if output_dataproduct_url is None:
            output_dataproduct_url = self.post_data_and_get_url(self.Dataproduct(), '/dataproduct/')
    
        return {"input": input_dataproduct_url,
                "output": output_dataproduct_url,
                "identity": True,
                "tags": ['tmss', 'testing']}
    
    def DataproductHash(self, algorithm_url=None, hash="my_hash", dataproduct_url=None):
        if algorithm_url is None:
            algorithm_url = self.django_api_url + '/algorithm/md5/'
    
        if dataproduct_url is None:
            dataproduct_url = self.post_data_and_get_url(self.Dataproduct(), '/dataproduct/')
    
        return {"dataproduct": dataproduct_url,
                "algorithm": algorithm_url,
                "hash": hash,
                "tags": ['tmss', 'testing']}
    
    
    def DataproductArchiveInfo(self, storage_ticket="my_storage_ticket", dataproduct_url=None):
        if dataproduct_url is None:
            dataproduct_url = self.post_data_and_get_url(self.Dataproduct(), '/dataproduct/')
    
        return {"dataproduct": dataproduct_url,
                "storage_ticket": storage_ticket,
                "public_since": datetime.utcnow().isoformat(),
                "corrupted_since": datetime.utcnow().isoformat(),
                "tags": ['tmss', 'testing']}
    
    def SubtaskInputSelectionTemplate(self, name="my_SubtaskInputSelectionTemplate"):
        return  {"name": name,
                 "description": 'My one date',
                 "version": 'v0.314159265359',
                 "schema": {"mykey": "my value"},
                 "tags": ["TMSS", "TESTING"]}
    
    def SubtaskInput(self, subtask_url=None, task_relation_blueprint_url=None, dataproduct_urls=None, subtask_connector_url=None, subtask_output_url=None, subtask_input_selection_template_url=None):
        if subtask_url is None:
            subtask_url = self.post_data_and_get_url(self.Subtask(), '/subtask/')
    
        if task_relation_blueprint_url is None:
            task_relation_blueprint_url = self.post_data_and_get_url(self.TaskRelationBlueprint(), '/task_relation_blueprint/')
    
        if dataproduct_urls is None:
            dataproduct_urls = [self.post_data_and_get_url(self.Dataproduct(), '/dataproduct/'),
                                self.post_data_and_get_url(self.Dataproduct(), '/dataproduct/')]
    
        if subtask_connector_url is None:
            subtask_connector_url = self.post_data_and_get_url(self.SubtaskConnector(), '/subtask_connector/')
    
        if subtask_output_url is None:
            subtask_output_url = self.post_data_and_get_url(self.SubtaskOutput(), '/subtask_output/')
    
        if subtask_input_selection_template_url is None:
            subtask_input_selection_template_url = self.post_data_and_get_url(self.SubtaskInputSelectionTemplate(), '/subtask_input_selection_template/')
    
        return {"subtask": subtask_url,
                "task_relation_blueprint": task_relation_blueprint_url,
                "connector": subtask_connector_url,
                "producer": subtask_output_url,
                "dataproducts": dataproduct_urls,
                "selection_doc": "{}",
                "selection_template": subtask_input_selection_template_url,
                "tags": []}
    
    def Filesystem(self, name="my_Filesystem", cluster_url=None):
        if cluster_url is None:
            cluster_url = self.post_data_and_get_url(self.Cluster(), '/cluster/')
    
        return {"name": name,
                "description": 'My one filesystem',
                "capacity": 1111111111,
                "cluster": cluster_url,
                "tags": ['tmss', 'testing']}