Exemple #1
0
def setup_database(cmd,
                   success_msg,
                   failure_msg,
                   log_file,
                   exit_on_fail=False):
    """ Create a subprocess call and outputs success and errors if any.

    Args:
        cmd          (string): docker instance name
        success_msg  (string): success message to be displayed in [OK] format.
        failure_msg  (string): failure text to be displayed with [FAILED] format.
        log_file     (string): log file path
        exit_on_fail   (bool): exit program if failed
    """

    try:
        process = subprocess.check_output(cmd, shell=True)
        register = json.loads(process)
        database_key = register["apiKey"]
        output_ok("REGISTER API: Created entity database. API KEY is " +
                  database_key)
    except:
        output_error(process, error_message=traceback.format_exc())
        # f=open("/tmp/status")
        # print("File contents:" + f.read())
        # f.close()
        exit()
Exemple #2
0
def cleanup_setup_apigateway(cmd,
                             success_msg,
                             info_msg,
                             failure_msg,
                             log_file,
                             exit_on_fail=False):
    """ Create a subprocess call and outputs success and errors if any.

    Args:
        cmd          (string): docker instance name
        success_msg  (string): success message to be displayed in [OK] format.
        failure_msg  (string): failure text to be displayed with [FAILED] format.
        log_file     (string): log file path
        exit_on_fail   (bool): exit program if failed
    """
    try:
        process = subprocess.Popen(cmd.split(" "),
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
        output_info(info_msg,
                    message=process.stdout.read(),
                    stderr=process.stderr.read())
        output_ok(success_msg,
                  message=process.stdout.read(),
                  stderr=process.stderr.read())

    except OSError:
        if exit_on_fail:
            output_error(
                failure_msg +
                "\n           Check logs {0} for more details.".format(
                    log_file),
                error_message=traceback.format_exc())
            exit()
        else:
            output_warning(
                failure_msg +
                "\n           Check logs {0} for more details.".format(
                    log_file),
                error_message=traceback.format_exc())
Exemple #3
0
def test(arguments):
    cmd = "./tests/create_entity.sh api_testing_streetlight"
    api_testing_streetlight_key = ""
    try:
        process = subprocess.check_output(cmd, shell=True)
        register = json.loads(process)
        api_testing_streetlight_key = register["apiKey"]
        output_ok(
            "REGISTER API: Created entity api_testing_streetlight. API KEY is "
            + api_testing_streetlight_key)
    except:
        output_error(process, error_message=traceback.format_exc())
        exit()

    cmd = "./tests/create_entity.sh api_testing_dashboard"
    api_testing_dashboard_key = ""
    try:
        process = subprocess.check_output(cmd, shell=True)
        register = json.loads(process)
        api_testing_dashboard_key = register["apiKey"]
        output_ok(
            "REGISTER API: Created entity api_testing_dashboard. API KEY is " +
            api_testing_dashboard_key)
    except:
        output_error(process, error_message=traceback.format_exc())
        exit()

    cmd = "./tests/publish.sh " + api_testing_streetlight_key
    try:
        process = subprocess.check_output(cmd, shell=True)
        if "Publish message OK" in process:
            output_ok(
                "PUBLISH API: Published message as api_testing_streetlight.")
        else:
            output_error(process, error_message=traceback.format_exc())
    except:
        output_error(process, error_message=traceback.format_exc())
        exit()

    cmd = "./tests/catalogue.sh api_testing_dashboard"
    try:
        process = subprocess.check_output(cmd, shell=True)
        if "api_testing_dashboard" in process:
            output_ok(
                "CATALOGUE API: Device api_testing_dashboard found in catalogue."
            )
        else:
            output_error(process, error_message=traceback.format_exc())
    except:
        output_error(process, error_message=traceback.format_exc())
        exit()

    cmd = "./tests/deregister.sh"
    try:
        process = subprocess.check_output(cmd, shell=True)
        if "success" in process:
            output_ok("DEREGISTER API: Device api_testing_dashboard removed.")
        else:
            output_error(process, error_message=traceback.format_exc())
    except:
        output_error(process, error_message=traceback.format_exc())
        exit()

    cmd = "sh tests/deregister1.sh"
    try:
        process = subprocess.check_output(cmd, shell=True)
        if "success" in process:
            output_ok(
                "DEREGISTER API: Device api_testing_streetlight removed.")
        else:
            output_error(process, error_message=traceback.format_exc())
    except:
        output_error(process, error_message=traceback.format_exc())
        exit()

    cmd = "./tests/catalogue.sh api_testing_dashboard"
    try:
        process = subprocess.check_output(cmd, shell=True)
        if "api_testing_dashboard" not in process:
            output_ok(
                "CATALOGUE API: Device api_testing_dashboard not found in catalogue."
            )
        else:
            output_error(process, error_message=traceback.format_exc())
    except:
        output_error(process, error_message=traceback.format_exc())
        exit()
Exemple #4
0
def test(arguments):
    cmd = "./tests/create_entity.sh testdevice1"
    testdevice1_key = ""

    try:
        process = subprocess.check_output(cmd, shell=True)
        register = json.loads(process)
        testdevice1_key = register["apiKey"]
        output_ok("REGISTER API: Created entity testdevice1. API KEY is " +
                  testdevice1_key)
    except:
        output_error(process, error_message=traceback.format_exc())
        exit()

    cmd = "./tests/create_entity.sh testdevice2"
    testdevice2_key = ""
    try:
        process = subprocess.check_output(cmd, shell=True)
        register = json.loads(process)
        testdevice2_key = register["apiKey"]
        output_ok("REGISTER API: Created entity testdevice2. API KEY is " +
                  testdevice2_key)
    except:
        output_error(process, error_message=traceback.format_exc())
        exit()

    cmd = "./tests/follow.sh " + testdevice2_key + " testdevice2 testdevice1"

    try:
        process = subprocess.check_output(cmd, shell=True)

        if "200 OK" in process:
            output_ok(
                "FOLLOW API: testdevice2 made a follow request to testdevice1")
        else:
            output_error(process, error_message=traceback.format_exc())

    except:
        output_error(process, error_message=traceback.format_exc())
        exit()

    time.sleep(1)

    cmd = "./tests/subscribe.sh testdevice1.follow " + testdevice1_key
    try:
        process = subprocess.check_output(cmd, shell=True)
        subscribe = json.loads(process)

        if subscribe[0]["data"]["requestor"] == "testdevice2" and subscribe[0][
                "data"]["permission"] == "read":
            output_ok(
                "FOLLOW API: testdevice1 has recieved the follow request")
        else:
            output_error(process, error_message=traceback.format_exc())

    except:
        output_error(process, error_message=traceback.format_exc())
        exit()

    cmd = "./tests/share.sh " + testdevice1_key + " testdevice1 testdevice2"

    try:
        process = subprocess.check_output(cmd, shell=True)

        if "200 OK" in process:
            output_ok(
                "SHARE API: testdevice1 authorised a share request to testdevice2"
            )
        else:
            output_error(process, error_message=traceback.format_exc())

    except:
        output_error(process, error_message=traceback.format_exc())
        exit()

    time.sleep(1)

    cmd = "./tests/subscribe.sh testdevice2.notify " + testdevice2_key

    try:
        process = subprocess.check_output(cmd, shell=True)
        subscribe = json.loads(process)

        if "Approved" in json.dumps(subscribe):
            output_ok("SHARE API: testdevice2 has recieved the share approval")
        else:
            output_error(process, error_message=traceback.format_exc())

    except:
        output_error(process, error_message=traceback.format_exc())
        exit()

    cmd = "./tests/bind.sh testdevice2 testdevice1.protected " + testdevice2_key

    try:
        process = subprocess.check_output(cmd, shell=True)

        if "200 OK" in process:
            output_ok(
                "BIND API: testdevice2 has bound its queue to testdevice1.protected exchange"
            )
        else:
            output_error(process, error_message=traceback.format_exc())

    except:
        output_error(process, error_message=traceback.format_exc())
        exit()

    cmd = "./tests/publish.sh testdevice1 " + testdevice1_key
    try:
        process = subprocess.check_output(cmd, shell=True)
        if "202 Accepted" in process:
            output_ok("PUBLISH API: Published message as testdevice1.")
        else:
            output_error(process, error_message=traceback.format_exc())
    except:
        output_error(process, error_message=traceback.format_exc())
        exit()

    time.sleep(1)

    cmd = "./tests/subscribe.sh testdevice2 " + testdevice2_key

    try:
        process = subprocess.check_output(cmd, shell=True)
        subscribe = json.loads(process)

        if "testdata" in subscribe[0]["data"]["body"]:
            output_ok(
                "SUBSCRIBE API: testdevice2 has recieved the data published by testdevice1"
            )
        else:
            output_error(process, error_message=traceback.format_exc())

    except:
        output_error(process, error_message=traceback.format_exc())
        exit()

    cmd = "./tests/unbind.sh testdevice2 testdevice1.protected " + testdevice2_key

    try:
        process = subprocess.check_output(cmd, shell=True)

        if "200 OK" in process:
            output_ok(
                "UNBIND API: testdevice2 has unbound its queue from testdevice1.protected exchange"
            )
        else:
            output_error(process, error_message=traceback.format_exc())

    except:
        output_error(process, error_message=traceback.format_exc())
        exit()

    cmd = "./tests/publish.sh testdevice1 " + testdevice1_key
    try:
        process = subprocess.check_output(cmd, shell=True)
        if "202 Accepted" in process:
            output_ok("PUBLISH API: Published message as testdevice1.")
        else:
            output_error(process, error_message=traceback.format_exc())
    except:
        output_error(process, error_message=traceback.format_exc())
        exit()

    time.sleep(1)

    cmd = "./tests/subscribe.sh testdevice2 " + testdevice2_key

    try:
        process = subprocess.check_output(cmd, shell=True)
        subscribe = json.loads(process)

        if not subscribe:
            output_ok(
                "SUBSCRIBE API: testdevice2 has not recieved the data published by testdevice1"
            )
        else:
            output_error(process, error_message=traceback.format_exc())

    except:
        output_error(process, error_message=traceback.format_exc())
        exit()

    cmd = "./tests/catalogue.sh testdevice1"
    try:
        process = subprocess.check_output(cmd, shell=True)
        if "testdevice1" in process:
            output_ok("CATALOGUE API: Device testdevice1 found in catalogue.")
        else:
            output_error(process, error_message=traceback.format_exc())
    except:
        output_error(process, error_message=traceback.format_exc())
        exit()

    cmd = "./tests/deregister.sh testdevice1"
    try:
        process = subprocess.check_output(cmd, shell=True)
        if "success" in process:
            output_ok("DEREGISTER API: Device testdevice1 removed.")
        else:
            output_error(process, error_message=traceback.format_exc())
    except:
        output_error(process, error_message=traceback.format_exc())
        exit()

    cmd = "sh tests/deregister.sh testdevice2"
    try:
        process = subprocess.check_output(cmd, shell=True)
        if "success" in process:
            output_ok("DEREGISTER API: Device testdevice2 removed.")
        else:
            output_error(process, error_message=traceback.format_exc())
    except:
        output_error(process, error_message=traceback.format_exc())
        exit()

    cmd = "./tests/catalogue.sh testdevice1"
    try:
        process = subprocess.check_output(cmd, shell=True)
        if "apitestingdashboard" not in process:
            output_ok(
                "CATALOGUE API: Device testdevice1 not found in catalogue.")
        else:
            output_error(process, error_message=traceback.format_exc())
    except:
        output_error(process, error_message=traceback.format_exc())
        exit()