Skip to content

Commit

Permalink
ADD fiware-cloto rest client for testing purposes and Context Update …
Browse files Browse the repository at this point in the history
…test feature
  • Loading branch information
jframos committed Sep 8, 2015
1 parent 5c77883 commit c6b3881
Show file tree
Hide file tree
Showing 15 changed files with 406 additions and 69 deletions.
3 changes: 2 additions & 1 deletion tests/acceptance/commons/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@


# Custom config properties
# Common properties are defined in qautils lib: qautils.configuration.configuration_utils
# Common properties are defined in qautils lib: qautils.configuration.configuration_properties
PROPERTIES_CONFIG_FACTS_SERVICE = "facts_service"
PROPERTIES_CONFIG_CLOTO_SERVICE = "cloto_service"

# Context Requests
ATTRIBUTES_NAME = u'name'
Expand Down
48 changes: 0 additions & 48 deletions tests/acceptance/commons/environment_commons.py

This file was deleted.

34 changes: 34 additions & 0 deletions tests/acceptance/features/component/context_update.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-

Feature: Receive context update requests
As a scalability manager user
I want to receive data from the monitoring architecture through Context Broker notifications
In order to manage facts, and group and send them to be processed.

@basic
Scenario Outline: Receive a context update notification with all parameters
Given the tenant-id registered in CLOTO component
When a context notification is received for "<server_id>" with values:
| cpuLoadPct | usedMemPct | freeSpacePct | netLoadPct |
| <cpu> | <memory> | <disk> | <network> |
Then the context is updated

Examples:

| server_id | cpu | memory | disk | network |
| qatest | 0.75 | 0.8 | 0.1 | 0.15 |
| qatest | 0.00 | 0.8 | 0.1 | 0.15 |
| qatest | 0.75 | 0.0 | 0.1 | 0.15 |
| qatest | 0.75 | 0.8 | 0.0 | 0.15 |
| qatest | 0 | 0.8 | 0.1 | 0.0 |
| qatest | 0.75 | 0 | 0.1 | 0.15 |
| qatest | 0.75 | 0.8 | 0 | 0.15 |
| qatest | 0.75 | 0.8 | 0.1 | 0 |
| qatest | 0.752 | 0.8 | 0.1 | 0.15 |
| qatest | 0.75 | 0.857 | 0.1 | 0.15 |
| qatest | 0.75 | 0.8 | 0.123 | 0.15 |
| qatest | 0.75 | 0.8 | 0.1 | 0.151 |
| qatest | 0.75 | 0.8 | 0.1 | 1 |
| qatest | 0.75 | 0.8 | 1 | 0.151 |
| qatest | 0.75 | 1 | 0.1 | 0.151 |
| qatest | 1 | 0.8 | 0.1 | 0.151 |
31 changes: 29 additions & 2 deletions tests/acceptance/features/component/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,17 @@
__author__ = "@jframos"


from commons.environment_commons import set_up
from qautils.logger.logger_utils import get_logger
from qautils.configuration.configuration_utils import set_up_project
from fiwarefacts_client.client import FactsClient
from fiwarecloto_client.client import ClotoClient
import qautils.configuration.configuration_utils as configuration_utils
from qautils.configuration.configuration_properties import PROPERTIES_CONFIG_SERVICE_PROTOCOL, \
PROPERTIES_CONFIG_SERVICE_RESOURCE, PROPERTIES_CONFIG_SERVICE_PORT, PROPERTIES_CONFIG_SERVICE_HOST, \
PROPERTIES_CONFIG_SERVICE_OS_USERNAME, PROPERTIES_CONFIG_SERVICE_OS_PASSWORD, \
PROPERTIES_CONFIG_SERVICE_OS_TENANT_ID, PROPERTIES_CONFIG_SERVICE_OS_AUTH_URL
from commons.constants import PROPERTIES_CONFIG_FACTS_SERVICE, PROPERTIES_CONFIG_CLOTO_SERVICE


__logger__ = get_logger(__name__)

Expand All @@ -35,7 +44,25 @@ def before_all(context):

__logger__.info("START ...")
__logger__.info("Setting UP acceptance test project ")
set_up(context)

set_up_project() # Load setting using 'qautils.configuration.configuration_utils'

# Create REST Clients
context.facts_client = FactsClient(
protocol=configuration_utils.config[PROPERTIES_CONFIG_FACTS_SERVICE][PROPERTIES_CONFIG_SERVICE_PROTOCOL],
host=configuration_utils.config[PROPERTIES_CONFIG_FACTS_SERVICE][PROPERTIES_CONFIG_SERVICE_HOST],
port=configuration_utils.config[PROPERTIES_CONFIG_FACTS_SERVICE][PROPERTIES_CONFIG_SERVICE_PORT],
resource=configuration_utils.config[PROPERTIES_CONFIG_FACTS_SERVICE][PROPERTIES_CONFIG_SERVICE_RESOURCE])

context.cloto_client = ClotoClient(
username=configuration_utils.config[PROPERTIES_CONFIG_CLOTO_SERVICE][PROPERTIES_CONFIG_SERVICE_OS_USERNAME],
password=configuration_utils.config[PROPERTIES_CONFIG_CLOTO_SERVICE][PROPERTIES_CONFIG_SERVICE_OS_PASSWORD],
tenant_id=configuration_utils.config[PROPERTIES_CONFIG_CLOTO_SERVICE][PROPERTIES_CONFIG_SERVICE_OS_TENANT_ID],
auth_url=configuration_utils.config[PROPERTIES_CONFIG_CLOTO_SERVICE][PROPERTIES_CONFIG_SERVICE_OS_AUTH_URL],
api_protocol=configuration_utils.config[PROPERTIES_CONFIG_CLOTO_SERVICE][PROPERTIES_CONFIG_SERVICE_PROTOCOL],
api_host=configuration_utils.config[PROPERTIES_CONFIG_CLOTO_SERVICE][PROPERTIES_CONFIG_SERVICE_HOST],
api_port=configuration_utils.config[PROPERTIES_CONFIG_CLOTO_SERVICE][PROPERTIES_CONFIG_SERVICE_PORT],
api_resource=configuration_utils.config[PROPERTIES_CONFIG_CLOTO_SERVICE][PROPERTIES_CONFIG_SERVICE_RESOURCE])


def before_feature(context, feature):
Expand Down
1 change: 0 additions & 1 deletion tests/acceptance/features/component/steps/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
__author__ = 'Javier'
75 changes: 75 additions & 0 deletions tests/acceptance/features/component/steps/context_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# -*- coding: utf-8 -*-
#
# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U
#
# This file is part of FI-WARE project.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
#
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
# See the License for the specific language governing permissions and
# limitations under the License.
#
# For those usages not covered by the Apache version 2.0 License please
# contact with opensource@tid.es

__author__ = "@jframos"

import behave
from behave import step
from hamcrest import assert_that
import uuid
from qautils.dataset.dataset_utils import DatasetUtils
import qautils.configuration.configuration_utils as config_utils
from qautils.configuration.configuration_properties import PROPERTIES_CONFIG_SERVICE_OS_TENANT_ID
from commons.constants import PROPERTIES_CONFIG_FACTS_SERVICE

behave.use_step_matcher("re")
dataset_utils = DatasetUtils()


@step(u'the tenant-id registered in CLOTO component')
def tenant_id_is_registered_in_cloto(context):

# A GET request is executed to CLOTO component, to init all data about that tenant in its system.
context.tenant_id_facts = config_utils.config[PROPERTIES_CONFIG_FACTS_SERVICE][PROPERTIES_CONFIG_SERVICE_OS_TENANT_ID]
_, response = context.cloto_client.get_tenant_id_resource_client().get_tenant_id(context.tenant_id_facts)

assert_that(response.ok,
"TenantId '{}' for testing cannot be retrieved from CLOTO: Message: {}".format(context.tenant_id_facts,
response.text))


@step(u'a context notification is received for "(?P<server_id>.*)" with values')
def a_context_update_is_received(context, server_id):

testdata = dataset_utils.prepare_data(context.table)
attribute_list = list()
for header in context.table.headings:
attribute_list.append({"name": header, "type": "string", "value": testdata.rows[0][header]})


context.response = context.facts_client.send_monitored_data(subscription_id = str(uuid.uuid1()),
originator=server_id,
status_code="200",
details="OK", type="vm",
is_pattern="false",
id=server_id,
attribute_list=attribute_list,
tenant_id=context.tenant_id_facts,
server_id=server_id)

@step(u'the context is updated')
def the_context_is_updated(context):

assert_that(context.response.ok,
"Response to CB notification is not the expected one: Message: {}".format(context.response.text))

4 changes: 2 additions & 2 deletions tests/acceptance/features/component/steps/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@

import behave
from behave import step
from hamcrest import *
from hamcrest import assert_that, equal_to, is_, contains_string

behave.use_step_matcher("re")


@step(u'the fiware-facts service properly deployed')
def fiware_facts_service_properly_deployed(context):
# So far, nothing to do
# Nnothing to do, so far
pass


Expand Down
Empty file.
112 changes: 112 additions & 0 deletions tests/acceptance/fiwarecloto_client/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# -*- coding: utf-8 -*-

# Copyright 2015 Telefonica Investigación y Desarrollo, S.A.U
#
# This file is part of FIWARE project.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
#
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
# See the License for the specific language governing permissions and
# limitations under the License.
#
# For those usages not covered by the Apache version 2.0 License please
# contact with opensource@tid.es

__author__ = "@jframos"


from qautils.http.headers_utils import set_representation_headers, HEADER_REPRESENTATION_JSON
from qautils.logger.logger_utils import get_logger
from keystoneclient.v2_0 import Client as KeystoneClient
from fiwarecloto_client.tenantid_resource import TenantIdResourceClient

__logger__ = get_logger(__name__)


# HEADERS
X_AUTH_TOKEN = "X-Auth-Token"
TENANT_ID = "Tenant-Id"


class ClotoClient():

def __init__(self, username, password, tenant_id, auth_url, api_protocol, api_host, api_port, api_resource):
"""
Init a new Client for CLOTO component.
:param username (string): The username (OpenStack)
:param password (string): The password
:param tenant_id (string): TenantID
:param auth_url (string): Keystone/IdM auth URL
:param api_protocol (string): API protocol
:param api_host (string): API host
:param api_port (string): API port
:param api_resource (string): API base resource
:return: None
"""

__logger__.info("Init CLOTO Client")
__logger__.debug("Client parameters: Username: %s, Password: %s, TenantId: %s, API protocol: %s, API host: %s, "
"API port: %s, Base resource: %s", username, password, tenant_id, api_protocol, api_host,
api_port, api_resource)

self.headers = dict()
self.api_protocol = api_protocol
self.api_host = api_host
self.api_port = api_port
self.api_resource = api_resource

set_representation_headers(self.headers, content_type=HEADER_REPRESENTATION_JSON,
accept=HEADER_REPRESENTATION_JSON)

self._init_keystone_client(username, password, tenant_id, auth_url)
self.token = self._get_auth_token()
__logger__.debug("Token: %s", self.token)

self.headers.update({X_AUTH_TOKEN: self.token})
self.headers.update({TENANT_ID: tenant_id})
__logger__.debug("Headers with OpenStack credentials: %s", self.headers)

def _init_keystone_client(self, username, password, tenant_id, auth_url):
"""
Init the keystone client to request token and endpoint data
:param string username: Username for authentication.
:param string password: Password for authentication.
:param string tenant_id: Tenant id.
:param string auth_url: Keystone service endpoint for authorization.
:param string region_name: Name of a region to select when choosing an
endpoint from the service catalog.
:return None
"""

__logger__.debug("Init Keystone Client")
self.keystone_client = KeystoneClient(username=username, password=password, tenant_id=tenant_id,
auth_url=auth_url)

def _get_auth_token(self):
"""
Get token from Keystone
:return: Token (String)
"""

__logger__.debug("Getting auth Token")
return self.keystone_client.auth_ref['token']['id']

def get_tenant_id_resource_client(self):
"""
Create an API resource REST client
:return: Rest client for 'TenantId' API resource
"""

__logger__.info("Creating TenantIdResource")

return TenantIdResourceClient(protocol=self.api_protocol, host=self.api_host,
port=self.api_port, resource=self.api_resource, headers=self.headers)
Loading

0 comments on commit c6b3881

Please sign in to comment.