def security_startup_check():
    if SettingsInfo().constant.SECURITY_SERVICE_NEEDED == 'true':
        SettingsInfo().TestLog.info("Check security services ... ")
        token = edgex.access_token("-useradd")
    else:
        token = ''
    return token
Beispiel #2
0
def shutdown_services(*args):
    SettingsInfo().TestLog.info("Shutdown all services")
    script_path = "{}/TAF/utils/scripts/{}/shutdown.sh".format(
        SettingsInfo().workDir,
        SettingsInfo().constant.DEPLOY_TYPE)
    cmd = ["sh", script_path, *args]
    run_command(cmd)
Beispiel #3
0
def check_value_equal(value_type, expect, val):
    SettingsInfo().TestLog.info('Check the {} value {} equal to {}'.format(
        value_type, expect, val))

    if value_type == FLOAT32:
        # Decode base64 to bytes
        byte_val = binascii.a2b_base64(str(val))
        # Convert bytes to float, 'f' can refer to https://docs.python.org/2/library/struct.html#format-characters
        decode_val = struct.unpack('>f', byte_val)[0]
        decode_val = round(decode_val, 2)
        SettingsInfo().TestLog.info('Decode {} to {}'.format(val, decode_val))
        res = decode_val == float(expect)
        return res
    elif value_type == FLOAT64:
        byte_val = binascii.a2b_base64(val)
        decode_val = struct.unpack('>d', byte_val)[0]
        SettingsInfo().TestLog.info('Decode {} to {}'.format(val, decode_val))
        res = decode_val == float(expect)
        return res
    # elif value_type == BOOL:
    #     return bool(val) == expect
    else:
        return val == expect
    SettingsInfo().TestLog.info("Unsupported data type {}".format(value_type))
    return False
def check_service_startup_by_log(service):
    recheck_times = int(SettingsInfo().constant.SERVICE_STARTUP_RECHECK_TIMES)
    wait_time = int(SettingsInfo().constant.SERVICE_STARTUP_WAIT_TIME)
    if 'modbus' not in service:
        service = "edgex-{}".format(service)

    for i in range(recheck_times):
        SettingsInfo().TestLog.info(
            "Check {} is startup or not.".format(service))
        try:
            logs = subprocess.check_output("docker logs {}".format(service),
                                           shell=True)
            keyword = "Service started in:".encode('utf-8')
        except:
            time.sleep(wait_time)
            continue

        if keyword in logs:
            SettingsInfo().TestLog.info("{} is started.".format(service))
            return True
        else:
            SettingsInfo().TestLog.info("Fail to start {}...".format(service))
            time.sleep(wait_time)
            continue
    raise Exception("Start {} failed.".format(service))
Beispiel #5
0
def remove_services(*args):
    SettingsInfo().TestLog.info("Remove services {}".format(args))
    script_path = "{}/TAF/utils/scripts/{}/remove-services.sh".format(
        SettingsInfo().workDir,
        SettingsInfo().constant.DEPLOY_TYPE)
    cmd = ["sh", script_path, *args]
    run_command(cmd)
Beispiel #6
0
def compare_binary_footprint_size_with_prior_release(usages):
    isfailed = 0
    for k in usages:
        if k not in exclude_services:
            threshold_limit = float(usages[k]["priorBinaryFootprint"]) * float(
                SettingsInfo().profile_constant.FOOTPRINT_THRESHOLD)
            try:
                if float(usages[k]["priorBinaryFootprint"]) != 0.0:
                    if float(usages[k]["binaryFootprint"]) >= threshold_limit:
                        logger.error(
                            "{} binary size {} > Prior release size {} * {}".
                            format(
                                k, str(usages[k]["binaryFootprint"]),
                                str(usages[k]["priorBinaryFootprint"]),
                                str(SettingsInfo().profile_constant.
                                    FOOTPRINT_THRESHOLD)))
                        isfailed = 1
                else:
                    # Failure if prior release is no binary file and current binary size is over than 50MB
                    if float(usages[k]["binaryFootprint"]) >= 50.0:
                        logger.error(
                            "{} binary size is over than 50MB".format(k))
                        isfailed = 1
            except:
                pass

    if isfailed == 1:
        raise Exception("One of container binary size is abnormal")
Beispiel #7
0
    def retrieve_events_from_subscriber(self):
        SettingsInfo().TestLog.info("Run Subscriber And Get Events")
        times = SettingsInfo().profile_constant.EXPORTED_LOOP_TIMES
        device_int = []
        device_bool = []
        device_uint = []

        full_subscriber_logs = subprocess.check_output(
            "python ${WORK_DIR}/TAF/utils/src/setup/mqtt-subscriber.py perf",
            shell=True)
        subscriber_logs = full_subscriber_logs.decode("utf-8").replace(
            "Connected to MQTT with result code 0", "")
        events = subscriber_logs.split('Got mqtt export data!!')
        for event in events:
            if "deviceName" in event:
                event_str = event.split('\n')
                event_json = json.loads(event_str[2])
                event_json['received'] = int(event_str[1])

                if str(devices[0]) == event_json['deviceName']:
                    device_int.append(event_json)
                elif str(devices[1]) == event_json['deviceName']:
                    device_bool.append(event_json)
                elif str(devices[2]) == event_json['deviceName']:
                    device_uint.append(event_json)
                else:
                    continue

        result["devices"][devices[0]] = device_int[-times:]
        result["devices"][devices[1]] = device_bool[-times:]
        result["devices"][devices[2]] = device_uint[-times:]
Beispiel #8
0
def compare_image_footprint_size_with_prior_release(usages):
    isfailed = 0
    for k in usages:
        threshold_limit = float(usages[k]["priorImageFootprint"]) * float(
            SettingsInfo().profile_constant.FOOTPRINT_THRESHOLD)
        try:
            if float(usages[k]["priorImageFootprint"]) != 0.0:
                if float(usages[k]["imageFootprint"]) >= threshold_limit:
                    logger.error(
                        "{} image size {} > Prior release size {} * {}".format(
                            k, str(usages[k]["imageFootprint"]),
                            str(usages[k]["priorImageFootprint"]),
                            str(SettingsInfo().profile_constant.
                                FOOTPRINT_THRESHOLD)))
                    isfailed = 1

            else:
                # Failure if no prior release image and current image size is over than 100MB
                if float(usages[k]["imageFootprint"]) >= 100.0:
                    logger.error("{} image size is over than 100MB".format(k))
                    isfailed = 1
        except:
            pass

    if isfailed == 1:
        raise Exception("One of container image size is abnormal")
Beispiel #9
0
def update_services(*args):
    SettingsInfo().TestLog.info('Update services {}'.format(args))
    cmd = [
        "sh", "{}/TAF/utils/scripts/{}/update-services.sh".format(
            SettingsInfo().workDir,
            SettingsInfo().constant.DEPLOY_TYPE), *args
    ]
    run_command(cmd)
Beispiel #10
0
def compare_startup_time_with_threshold(list):
    for x in list:
        compare_value = int(
            SettingsInfo().profile_constant.STARTUP_TIME_THRESHOLD)
        if compare_value < x:
            raise Exception("Startup time is longer than {} seconds".format(
                SettingsInfo().profile_constant.STARTUP_TIME_THRESHOLD))
    return True
Beispiel #11
0
def deploy_services(*args):
    SettingsInfo().TestLog.info("Deploy services {}".format(args))
    cmd = [
        "sh", "{}/TAF/utils/scripts/{}/deploy-services.sh".format(
            SettingsInfo().workDir,
            SettingsInfo().constant.DEPLOY_TYPE), *args
    ]
    run_command(cmd)
    checker.check_services_startup([*args])
Beispiel #12
0
def deploy_device_service(device_service, *args):
    SettingsInfo().TestLog.info(
        'Deploy device service {}'.format(device_service))
    cmd = [
        "sh", "{}/TAF/utils/scripts/{}/deploy-device-service.sh".format(
            SettingsInfo().workDir,
            SettingsInfo().constant.DEPLOY_TYPE), device_service, *args
    ]
    run_command(cmd)
    checker.check_service_startup_by_log(device_service)
Beispiel #13
0
def deploy_edgex(*args):
    SettingsInfo().TestLog.info('Deploy EdgeX')
    cmd = [
        "sh", "{}/TAF/utils/scripts/{}/deploy-edgex.sh".format(
            SettingsInfo().workDir,
            SettingsInfo().constant.DEPLOY_TYPE), *args
    ]
    run_command(cmd)

    checker.check_services_startup(
        ["data", "metadata", "command", "support-notifications"])
def check_service_is_stopped_or_not():
    for i in range(SettingsInfo().profile_constant.RETRY_TIMES):
        logs = subprocess.check_output("docker ps -f name=edgex-", shell=True)
        keyword = "Up ".encode('utf-8')
        if keyword in logs:
            SettingsInfo().TestLog.info("Waiting for all services stop")
            time.sleep(SettingsInfo().profile_constant.WAIT_TIME)
            continue
        else:
            SettingsInfo().TestLog.info("All services are stopped")
            return True
    raise Exception("Not all services are stopped")
def modify_consul_config(path, value):
    conn = http.client.HTTPConnection(host=SettingsInfo().constant.BASE_URL,
                                      port=8500)
    conn.request(method="PUT", url=path, body=value)
    try:
        r1 = conn.getresponse()
    except Exception as e:
        raise e
    if int(r1.status) == 200:
        SettingsInfo().TestLog.info(
            "Modify consul with key {} and value {}".format(path, value))
    else:
        raise Exception("Fail to enable MarkPushed.")
Beispiel #16
0
def setup_config(args):
    os.environ['WORK_DIR'] = WORK_DIR
    os.environ['PROFILE'] = args.profile

    # Read environment variable and store to SettingsInfo
    SettingsInfo().add_name('workDir', os.environ['WORK_DIR'])
    SettingsInfo().add_name('profile', os.environ['PROFILE'])

    # Read config file and store to SettingsInfo
    constant = __import__("TAF.config." + os.environ['PROFILE'] +
                          ".configuration",
                          fromlist=['configuration'])
    SettingsInfo().add_name('constant', constant)
Beispiel #17
0
def deploy_device_service_with_the_confdir_option(device_service, confdir):
    SettingsInfo().TestLog.info(
        'Deploy device service {} with confdir option {}'.format(
            device_service, confdir))
    cmd = [
        "sh",
        "{}/TAF/utils/scripts/{}/deploy-device-service-with-confdir-option.sh".
        format(SettingsInfo().workDir,
               SettingsInfo().constant.DEPLOY_TYPE), device_service, confdir
    ]
    run_command(cmd)

    checker.check_services_startup([device_service, confdir])
Beispiel #18
0
def run_command(cmd):
    p = subprocess.Popen(cmd, stderr=subprocess.PIPE)
    for line in p.stderr:
        SettingsInfo().TestLog.info(line.decode("utf-8"))

    p.wait()
    SettingsInfo().TestLog.info("exit " + str(p.returncode))
    if p.returncode != 0:
        msg = "Fail to execute cmd: " + " ".join(str(x) for x in cmd)
        SettingsInfo().TestLog.error(msg)
        raise Exception(msg)
    else:
        msg = "Success to execute cmd: " + " ".join(str(x) for x in cmd)
        SettingsInfo().TestLog.info(msg)
Beispiel #19
0
def deploy_device_service_with_registry_url(device_service, registry_url):
    SettingsInfo().TestLog.info(
        'Deploy device service {} with registry url {}'.format(
            device_service, registry_url))
    cmd = [
        "sh",
        "{}/TAF/utils/scripts/{}/deploy-device-service-with-registry-url.sh".
        format(SettingsInfo().workDir,
               SettingsInfo().constant.DEPLOY_TYPE), device_service,
        registry_url
    ]
    run_command(cmd)

    checker.check_services_startup([device_service, registry_url])
Beispiel #20
0
def compare_export_time_with_threshold():
    for device in result["devices"]:
        for event in result["devices"][device]:
            compare_value = int(
                SettingsInfo().profile_constant.EXPORT_TIME_THRESHOLD)
            if event["exported"] == "":
                continue
            else:
                if compare_value < event["exported"]:
                    raise Exception(
                        "{} event exported time is longer than {} ms".format(
                            device,
                            SettingsInfo().profile_constant.
                            EXPORT_TIME_THRESHOLD))
    return True
def check_services_startup(check_list):
    token = security_startup_check()
    for item in check_list:
        if item in services:
            SettingsInfo().TestLog.info("Check service " + item +
                                        " is startup...")
            check_service_startup(services[item], token)
Beispiel #22
0
def suite_setup(suite_name, logfile, loglevel="DEBUG"):

    """
    Suite_Setup sets up the test cases

    Gets the log and config and puts them in the SettingsInfo object

    The API between Robot and this script is that this script
    return True or False.  The policy here is catch exceptions,
    log them, and return False to Robot.

    @param suite_name:   required suite name
    @param logfile:   required logfile
    @param cfgfile:   required configuration file
    @param loglevel:  sets loglevel
    @retval    True returned to if all code was executed successfully.
    """
    tc_name = "Suite Setup:"

    # Setup logger
    logging.getLogger().setLevel(loglevel)
    test_log = ColorLog(filename=logfile, lvl=loglevel, logName=suite_name, useBackGroundLogger=False)

    loglvl = logging.getLogger().getEffectiveLevel()
    utils.print_log_header(test_log)
    test_log.info('{} Logging started, level: {}'.format(tc_name, logging.getLevelName(loglvl)))
    test_log.info('{} python version: {}'.format(tc_name, sys.version))
    test_log.info('{} testcases version: {}'.format(tc_name, __VERSION__))
    # test_log.info('{} Testcase configuration set from {}'.format(tc_name, cfgfile))
    SettingsInfo().add_name('TestLog', test_log)
    return True
Beispiel #23
0
 def init_demo(self):
     """
     This method initialize the instance of the current class, this must be called after the config files have been
     initialized.
     @param    self   represents the instance of the class.
     """
     self.username = SettingsInfo().Demo['username']
Beispiel #24
0
 def test_error(self):
     """
     This method is an example on error usage.
     @param    self   represents the instance of the class.
     @retval   True
     """
     SettingsInfo().TestLog.error('Error: Hello World!!')
     return True
Beispiel #25
0
 def test_log_exception(self):
     """
     This method is an example on debug usage.
     @param    self   represents the instance of the class.
     @retval   True
     """
     SettingsInfo().TestLog.warn('Log Exception: Hello World!!')
     return True
Beispiel #26
0
def access_token(arg):
    cmd = [
        "sh", "{}/TAF/utils/scripts/{}/api-gateway-token.sh".format(
            SettingsInfo().workDir,
            SettingsInfo().constant.DEPLOY_TYPE), arg
    ]
    try:
        output = subprocess.run(cmd, stdout=subprocess.PIPE,
                                check=True).stdout.decode('utf-8').rstrip('\n')
        SettingsInfo().TestLog.info(
            "./api-gateway-token.sh {} output: {} ".format(arg, output))
        return output
    except subprocess.CalledProcessError as e:
        msg = "Fail to execute cmd: " + " ".join(str(x) for x in cmd)
        SettingsInfo().TestLog.info("exit " + str(e.returncode))
        SettingsInfo().TestLog.error(msg)
        raise Exception(msg)
Beispiel #27
0
 def test_pass(self):
     """
     This method is an example on pass log usage.
     @param    self   represents the instance of the class.
     @retval   True
     """
     SettingsInfo().TestLog.PASS('Pass: Hello World!!')
     return True
Beispiel #28
0
 def test_warn(self):
     """
     This method is an example on warn usage.
     @param    self   represents the instance of the class.
     @retval   True
     """
     SettingsInfo().TestLog.warn('Warn: Hello World!!')
     return True
def demo_suite_setup(logfile, cfgfile, loglevel=None):
    """
    Suite_Setup sets up the test cases

    Gets the log and config and puts them in the SettingsInfo object

    The API between Robot and this script is that this script
    return True or False.  The policy here is catch exceptions,
    log them, and return False to Robot.

    @param logfile:   required logfile
    @param cfgfile:   required configuration file
    @param loglevel:  sets loglevel
    @retval    True returned to if all code was executed successfully.
    """
    tc_name = "Demo Suite Setup:"
    testcfg = TestSettings(cfgfile)
    for each_section in testcfg.sections():
        SettingsInfo().add_name(each_section,
                                testcfg.get_section(each_section))
    SettingsInfo().add_name('testcfg', testcfg)
    tc_cfg_section = testcfg.get_section('Demo')
    if 'loglevel' in tc_cfg_section:
        lvl = tc_cfg_section['loglevel']
    else:
        lvl = loglevel if loglevel is not None else 'INFO'
    logging.getLogger().setLevel(lvl)
    testLog = ColorLog(filename=logfile,
                       lvl=lvl,
                       logName="demo",
                       useBackGroundLogger=False)
    loglvl = logging.getLogger().getEffectiveLevel()
    utils.print_log_header(testLog)
    testLog.info('{} Logging started, level: {}'.format(
        tc_name, logging.getLevelName(loglvl)))
    testLog.info('{} python version: {}'.format(tc_name, sys.version))
    testLog.info('{} testcases version: {}'.format(tc_name, __VERSION__))
    testLog.info('{} Testcase configuration set from {}'.format(
        tc_name, cfgfile))
    SettingsInfo().add_name('TestLog', testLog)
    if not utils.verify_config(testcfg, testLog):
        testLog.error(
            "{}: Check config file: it appears to be missing sections or items"
        )
        return False
    return True
Beispiel #30
0
 def test_debug(self):
     """
     This method is an example on debug usage.
     @param    self   represents the instance of the class.
     @retval   True
     """
     SettingsInfo().TestLog.debug('Debug: Hello World!!')
     return True