Beispiel #1
0
def compile_endpoint(endpoint_file):

    user_endpoint_module = get_endpoint_module_from_file(endpoint_file)
    UserEndpoint = get_endpoint_class_from_module(user_endpoint_module)
    if UserEndpoint is None:
        return None

    endpoint_payload = None
    UserEndpointPayload, _ = create_endpoint_payload(UserEndpoint)
    endpoint_payload = UserEndpointPayload.get_dict()

    return endpoint_payload
Beispiel #2
0
    def test_rb_crud(self):
        """
        test_runbook_create, test_runbook_update, test_runbook_unicode_description
        test_runbook_run, test_runbook_delete, test_runbook_download_and_upload
        """

        client = get_api_client()
        runbook = change_uuids(RunbookPayload, {})

        # Runbook Create
        res, err = client.runbook.create(runbook)
        if err:
            pytest.fail("[{}] - {}".format(err["code"], err["error"]))
        rb = res.json()
        rb_state = rb["status"]["state"]
        rb_uuid = rb["metadata"]["uuid"]
        rb_name = rb["spec"]["name"]
        print(">> Runbook state: {}".format(rb_state))
        assert rb_state == "ACTIVE"

        # reading the runbook using get call
        print("\n>>Reading Runbook")
        res, err = client.runbook.read(rb_uuid)
        if err:
            pytest.fail("[{}] - {}".format(err["code"], err["error"]))
        else:
            assert res.ok is True
            res = res.json()
            assert rb_name == res["spec"]["name"]
            assert rb_name == res["metadata"]["name"]
            assert rb_name == res["metadata"]["name"]
            print(">> Get call to runbook is successful >>")

        # creating an endpoint
        EndpointPayload, _ = create_endpoint_payload(linux_endpoint)
        ep_payload = EndpointPayload.get_dict()
        res, err = client.endpoint.upload_with_secrets(
            "endpoint_" + str(uuid.uuid4())[-10:], "", ep_payload["spec"]["resources"]
        )
        if err:
            pytest.fail("[{}] - {}".format(err["code"], err["error"]))
        endpoint = res.json()
        endpoint_state = endpoint["status"]["state"]
        endpoint_name = endpoint["status"]["name"]
        endpoint_uuid = endpoint["metadata"]["uuid"]
        assert endpoint_state == "ACTIVE"

        # updating the runbook
        del rb["status"]
        resources = change_uuids(RunbookUpdatePayload["spec"]["resources"], {})
        rb["spec"]["resources"]["credential_definition_list"] = resources[
            "credential_definition_list"
        ]
        rb["spec"]["resources"]["runbook"]["task_definition_list"][1] = resources[
            "runbook"
        ]["task_definition_list"][1]
        rb["spec"]["resources"]["runbook"]["task_definition_list"][0][
            "child_tasks_local_reference_list"
        ][0]["uuid"] = resources["runbook"]["task_definition_list"][1]["uuid"]
        rb["spec"]["resources"]["runbook"]["variable_list"].append(
            resources["runbook"]["variable_list"][0]
        )
        rb["spec"]["resources"]["default_target_reference"] = {
            "uuid": endpoint_uuid,
            "name": endpoint_name,
            "kind": "app_endpoint",
        }
        rb["spec"]["description"] = "user-\u018e-name-\xf1"
        res, err = client.runbook.update(rb_uuid, rb)
        if err:
            pytest.fail("[{}] - {}".format(err["code"], err["error"]))

        rb = res.json()
        assert rb["status"]["state"] == "ACTIVE"
        assert len(rb["spec"]["resources"]["credential_definition_list"]) == 1

        # run the runbook
        print("\n>>Running the runbook")
        res, err = client.runbook.run(rb_uuid, {})
        if err:
            pytest.fail("[{}] - {}".format(err["code"], err["error"]))

        response = res.json()
        runlog_uuid = response["status"]["runlog_uuid"]

        # polling till runbook run gets to terminal state
        state, reasons = poll_runlog_status(client, runlog_uuid, RUNLOG.TERMINAL_STATES)

        print(">> Runbook Run state: {}\n{}".format(state, reasons))
        assert state == RUNLOG.STATUS.SUCCESS

        # download the runbook
        file_path = client.runbook.export_file(rb_uuid, passphrase="test_passphrase")

        # upload the runbook
        res, err = client.runbook.import_file(
            file_path,
            rb_name + "-uploaded",
            rb["metadata"].get("project_reference", {}).get("uuid", ""),
            passphrase="test_passphrase",
        )
        if err:
            pytest.fail("[{}] - {}".format(err["code"], err["error"]))
        uploaded_rb = res.json()
        uploaded_rb_state = uploaded_rb["status"]["state"]
        uploaded_rb_uuid = uploaded_rb["metadata"]["uuid"]
        assert uploaded_rb_state == "ACTIVE"

        # delete uploaded runbook
        _, err = client.runbook.delete(uploaded_rb_uuid)
        if err:
            pytest.fail("[{}] - {}".format(err["code"], err["error"]))
        else:
            print("uploaded endpoint deleted")

        # delete downloaded file
        os.remove(file_path)

        # deleting runbook
        print("\n>>Deleting runbook")
        res, err = client.runbook.delete(rb_uuid)
        if err:
            pytest.fail("[{}] - {}".format(err["code"], err["error"]))
        else:
            assert res.ok is True
            res = res.json()
            print("API Response: {}".format(res["description"]))
            print(">> Delete call to runbook is successful >>")

        # deleting endpoint
        _, err = client.endpoint.delete(endpoint_uuid)
        if err:
            pytest.fail("[{}] - {}".format(err["code"], err["error"]))
Beispiel #3
0
    def test_rb_execute_with_deleted_ep(self):
        """
        test_runbook_run with deleted ep
        """

        client = get_api_client()
        runbook = change_uuids(RunbookPayload, {})

        # Runbook Create
        res, err = client.runbook.create(runbook)
        if err:
            pytest.fail("[{}] - {}".format(err["code"], err["error"]))
        rb = res.json()
        rb_state = rb["status"]["state"]
        rb_uuid = rb["metadata"]["uuid"]
        rb_name = rb["spec"]["name"]
        print(">> Runbook state: {}".format(rb_state))
        assert rb_state == "ACTIVE"

        # reading the runbook using get call
        print("\n>>Reading Runbook")
        res, err = client.runbook.read(rb_uuid)
        if err:
            pytest.fail("[{}] - {}".format(err["code"], err["error"]))
        else:
            assert res.ok is True
            res = res.json()
            assert rb_name == res["spec"]["name"]
            assert rb_name == res["metadata"]["name"]
            assert rb_name == res["metadata"]["name"]
            print(">> Get call to runbook is successful >>")

        # creating an endpoint
        EndpointPayload, _ = create_endpoint_payload(linux_endpoint)
        ep_payload = EndpointPayload.get_dict()
        res, err = client.endpoint.upload_with_secrets(
            "endpoint_" + str(uuid.uuid4())[-10:], "", ep_payload["spec"]["resources"]
        )
        if err:
            pytest.fail("[{}] - {}".format(err["code"], err["error"]))
        endpoint = res.json()
        endpoint_state = endpoint["status"]["state"]
        endpoint_name = endpoint["status"]["name"]
        endpoint_uuid = endpoint["metadata"]["uuid"]
        assert endpoint_state == "ACTIVE"

        # updating the runbook
        del rb["status"]
        rb["spec"]["resources"]["default_target_reference"] = {
            "uuid": endpoint_uuid,
            "name": endpoint_name,
            "kind": "app_endpoint",
        }
        res, err = client.runbook.update(rb_uuid, rb)
        if err:
            pytest.fail("[{}] - {}".format(err["code"], err["error"]))

        rb = res.json()
        assert rb["status"]["state"] == "ACTIVE"

        # deleting endpoint
        _, err = client.endpoint.delete(endpoint_uuid)
        if err:
            pytest.fail("[{}] - {}".format(err["code"], err["error"]))

        # run the runbook
        print("\n>>Running the runbook")
        res, err = client.runbook.run(rb_uuid, {})
        if err["code"] != 400:
            pytest.fail("[{}] - {}".format(err["code"], err["error"]))

        res = res.json()
        errors = ""
        for message in res.get("message_list", []):
            errors += message["message"]

        assert "Default target is not in active state" in errors

        # deleting runbook
        print("\n>>Deleting runbook")
        res, err = client.runbook.delete(rb_uuid)
        if err:
            pytest.fail("[{}] - {}".format(err["code"], err["error"]))
        else:
            assert res.ok is True
            res = res.json()
            print("API Response: {}".format(res["description"]))
            print(">> Delete call to runbook is successful >>")