Example #1
0
 def _get(self, path, params=None, use_rest_api_root=True, **kwargs):
     """Sends a GET request.
         :param path: Path within API.
         :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.
         :param use_rest_api_root: (optional) Indicated, if Rest API Plugin Context Root should be added
         :param \*\*kwargs: Optional arguments that ``request`` takes.
         :return: json-encoded content of a response, if any.
         :rtype: dict
         """
     if self.dex_cookies:
         cookies = self.dex_cookies
     else:
         cookies = get_session_cookies()
     if use_rest_api_root:
         url = (self.BASE_REST_API_URL_TEMPLATE % self.root_url) + path
     else:
         url = (self.BASE_URL_TEMPLATE % self.root_url) + path
     print('Requesting Airflow URL: {!r}. Params: {!r}. KWARGS: {!r}'.format(url, params, kwargs))
     response = requests.get(url, params, timeout=self._TIMEOUT_SEC, cookies=cookies, **kwargs)
     if response.status_code == 200:
         json = response.json()
         print('Airflow response: {!r}'.format(json))
         return json
     else:
         raise RequestException('HTTP Code %d for "GET %s "' % (response.status_code, url))
Example #2
0
    def metric_should_be_presented(self, model_id, model_version):
        """
        Check that requests count metric for model exists

        :param model_id: model ID
        :type model_id: str
        :param model_version: model version
        :type model_version: str
        :raises: Exception
        :return: None
        """
        url = '{}/api/datasources/proxy/1/render'.format(self._url)

        auth = None
        if self._user and self._password:
            auth = (self._user, self._password)

        headers = {'Content-Type': 'application/x-www-form-urlencoded'}

        model_identifier = '{}.{}'.format(
            normalize_name(model_id, dns_1035=True),
            normalize_name(model_version, dns_1035=True))

        target = 'highestMax(stats.legion.model.{}.request.count, 1)'.format(
            model_identifier)

        payload = {
            'target': target,
            'from': '-5min',
            'until': 'now',
            'format': 'json',
            'cacheTimeout': 0,
            'maxDataPoints': 1000
        }

        response = requests.post(url,
                                 data=payload,
                                 headers=headers,
                                 auth=auth,
                                 cookies=get_session_cookies())
        print('Loading {} metrics. Data: {}'.format(target, response.text))

        data = response.json()
        if not data:
            raise Exception('Data is empty')

        datapoints = data[0]['datapoints']

        for val, time in datapoints:
            if val is not None and val > 0:
                break
        else:
            raise Exception('Cannot find any value > 0')
Example #3
0
    def _get_model_metric(self,
                          model_id,
                          model_version,
                          model_endpoint='default'):
        """
        Gets model metric data and returns it
        :param model_id: model ID
        :type model_id: str
        :param model_version: model version
        :type model_version: str
        :param model_endpoint: model endpoint
        :type model_endpoint: str
        :return: list[dict], list with dict with metrics
        """

        url = '{}/api/datasources/proxy/1/render'.format(self._url)

        auth = None
        if self._user and self._password:
            auth = (self._user, self._password)

        headers = {'Content-Type': 'application/x-www-form-urlencoded'}

        model_identifier = '{}.{}.{}'.format(
            normalize_name(model_id, dns_1035=True),
            normalize_name(model_version, dns_1035=True),
            normalize_name(model_endpoint, dns_1035=True))

        target = 'highestMax(stats.legion.model.{}.request.count, 1)'.format(
            model_identifier)

        payload = {
            'target': target,
            # Add 10 seconds to cover a corner case
            'from': '-{}s'.format(int(time.time() - self._start_time) + 10),
            'until': 'now',
            'format': 'json',
            'cacheTimeout': 0,
            'maxDataPoints': 1000
        }

        response = requests.post(url,
                                 data=payload,
                                 headers=headers,
                                 auth=auth,
                                 cookies=get_session_cookies())
        print('Current time: {}. Start time: {}. Payload: {}'.format(
            time.time(), self._start_time, payload))
        print('Loading {} metrics. Data: {}'.format(target, response.text))

        return response.json()
Example #4
0
    def connect_to_jenkins(self,
                           domain,
                           user=None,
                           password=None,
                           dex_cookies={},
                           timeout=10):
        """
        Connect to Jenkins server

        :param domain: domain name
        :type domain: str
        :param user: login
        :type user: str
        :param password: password
        :type password: str
        :param timeout: timeout for connection process in seconds
        :type timeout: int or str
        :param dex_cookies:  Dex Cookies if they are already received, if empty - get new for Legion static user
        :type dex_cookies: dict
        :return: None
        """
        if get_jenkins_credentials() and not user and not password:
            user, password = get_jenkins_credentials()
        self._client = jenkins.Jenkins(domain,
                                       username=user,
                                       password=password,
                                       timeout=int(timeout))
        if not dex_cookies:
            self._client.crumb = {
                'crumbRequestField':
                'Cookie',
                'crumb':
                ';'.join([
                    '{}={}'.format(k, v)
                    for (k, v) in get_session_cookies().items()
                ])
            }
        else:
            self._client.crumb = {
                'crumbRequestField':
                'Cookie',
                'crumb':
                ';'.join(
                    ['{}={}'.format(k, v) for (k, v) in dex_cookies.items()])
            }
        user = self._client.get_whoami()
        print('Hello %s from Jenkins' % (user['fullName']))
        self._client.wait_for_normal_op(10)
        self._client.get_all_jobs()
Example #5
0
    def connect_to_grafana(self, domain, user, password):
        """
        Connect to grafana server

        :param domain: domain name
        :type domain: str
        :param user: login
        :type user: str
        :param password: password
        :type password: str
        :return: None
        """
        self._url = domain.strip('/')
        self._user = user
        self._password = password
        self._client = GrafanaClient(self._url, self._user, self._password)
        self._client.set_cookies(get_session_cookies())
Example #6
0
 def _get(self, path, params=None, parse_json=True, **kwargs):
     """Sends a GET request.
         :param path: Path within API.
         :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.
         :param \*\*kwargs: Optional arguments that ``request`` takes.
         :return: json-encoded content of a response, if any.
         :param parse_json: Indicates, if response should be parsed into Json
         :type parse_json: bool
         :rtype: dict or str
         """
     response = requests.get(self.base_url + path,
                             params,
                             timeout=self._TIMEOUT_SEC,
                             cookies=get_session_cookies(),
                             **kwargs)
     if response.status_code == 200:
         if parse_json:
             return response.json()
         else:
             return response
     else:
         raise RequestException('HTTP Code %d for "GET %s "' %
                                (response.status_code, path))
Example #7
0
def get_variables(arg=None):
    """
    Gather and return all variables to robot

    :param arg: path to directory with profiles
    :type args: str or None
    :return: dict[str, Any] -- values for robot
    """
    # Build default path to profiles directory
    if not arg:
        arg = os.getenv(PATH_TO_PROFILES_DIR)
        if not arg:
            arg = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'deploy', 'profiles'))

    # load Profile
    profile = os.getenv(PROFILE_ENVIRON_KEY)
    if not profile:
        raise Exception('Cannot get profile - {} env variable is not set'.format(PROFILE_ENVIRON_KEY))

    profile = os.path.abspath(os.path.join(arg, '{}.yml'.format(profile)))
    if not os.path.exists(profile):
        raise Exception('Cannot get profile - file not found {}'.format(profile))

    with open(profile, 'r') as stream:
        data = yaml.load(stream)

    # load Secrets
    secrets = os.getenv(CREDENTIAL_SECRETS_ENVIRONMENT_KEY)
    if not secrets:
        raise Exception('Cannot get secrets - {} env variable is not set'.format(CREDENTIAL_SECRETS_ENVIRONMENT_KEY))

    if not os.path.exists(secrets):
        raise Exception('Cannot get secrets - file not found {}'.format(secrets))

    with open(secrets, 'r') as stream:
        data.update(yaml.load(stream))

    variables = {
        'CLUSTER_NAMESPACE': data['namespace'],
        'DEPLOYMENT': data['deployment'],

        'HOST_BASE_DOMAIN': data.get('test_base_domain', data['base_domain']),
        'USE_HTTPS_FOR_TESTS': data.get('use_https_for_tests', 'yes') == 'yes',

        'JENKINS_JOBS': data['examples_to_test'],
        'MODEL_ID': data['model_id_to_test'],
        'ENCLAVES': data.get('enclaves', []),

        'CLUSTER_NAME': data['cluster_name'],
        'NEXUS_DOCKER_REPO': data['docker_repo'],
        'TEMP_DIRECTORY': data['tmp_dir'],
        'FEEDBACK_BUCKET': '{}-{}-{}'.format(data['legion_data_bucket_prefix'],
                                              data['env_type'],
                                              data['enclaves'][0]),
    }

    variables['HOST_PROTOCOL'] = 'https' if variables['USE_HTTPS_FOR_TESTS'] else 'http'
    variables['MODEL_TEST_ENCLAVE'] = variables['ENCLAVES'][0] if len(variables['ENCLAVES']) > 0 else 'UNKNOWN_ENCLAVE'

    cookies = os.getenv(PATH_TO_COOKIES_FILE)
    cookies_data = None
    if cookies:
        try:
            with open(cookies, 'r') as stream:
                lines = stream.readlines()
                cookies_data = {
                    'jenkins_user': lines[0].rstrip(),
                    'jenkins_password': lines[1].rstrip(),
                    'cookies': lines[2].rstrip()
                }
        except IOError:
            pass
        except IndexError:
            pass

    if 'dex' in data and data['dex']['enabled'] and 'staticPasswords' in data['dex']['config'] and \
            data['dex']['config']['staticPasswords']:
        static_user = data['dex']['config']['staticPasswords'][0]
        if not cookies_data:
            init_session_id(static_user['email'], static_user['password'], data.get('test_base_domain', data['base_domain']))
        else:
            init_session_id_from_data(cookies_data)
        variables['STATIC_USER_EMAIL'] = static_user['email']
        variables['STATIC_USER_PASS'] = static_user['password']

        variables['DEX_TOKEN'] = dex_client.get_token()
        variables['DEX_COOKIES'] = dex_client.get_session_cookies()
    else:
        variables['STATIC_USER_EMAIL'] = ''
        variables['STATIC_USER_PASS'] = ''

    return variables