From 207d8b4d6e24ff4f4a22a5e91eef32f3667df28a Mon Sep 17 00:00:00 2001 From: yangyan Date: Mon, 2 Dec 2019 09:47:22 +0800 Subject: Add ut for dmaap in catalog Change-Id: I5940800c621f309fd6d95872141b2b1c5a7cf73a Issue-ID: VFC-1592 Signed-off-by: yangyan --- .../client/__init__.py | 5 +- .../client/dmaap/consumer.py | 6 +- .../client/dmaap/publisher.py | 2 + .../client/test/test_consumer.py | 81 ++++++++++++++++++++++ .../client/test/test_identity.py | 38 ++++++++++ 5 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/test/test_consumer.py create mode 100644 catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/test/test_identity.py diff --git a/catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/__init__.py b/catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/__init__.py index 0c1e8e15..7ae04f00 100644 --- a/catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/__init__.py +++ b/catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/__init__.py @@ -1,8 +1,11 @@ # Copyright (c) 2019, CMCC Technologies Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License") + +# 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. diff --git a/catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/dmaap/consumer.py b/catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/dmaap/consumer.py index e5677d8c..dcc7f956 100644 --- a/catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/dmaap/consumer.py +++ b/catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/dmaap/consumer.py @@ -24,7 +24,8 @@ logger = logging.getLogger(__name__) class ConsumerClient: - def __init__(self, host, topic, consumer_group, consumer_id, timeout_ms=-1, limit=-1, filter=''): + def __init__(self, host, topic, consumer_group, consumer_id, timeout_ms=-1, limit=-1, filter='', + api_key='', api_secret=''): self.host = host self.topic = topic self.group = consumer_group @@ -32,8 +33,11 @@ class ConsumerClient: self.timeout_ms = timeout_ms self.limit = limit self.filter = filter + self.api_key = api_key + self.api_secret = api_secret def set_api_credentials(self, api_key, api_secret): + self.api_key = api_key self.api_secret = api_secret diff --git a/catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/dmaap/publisher.py b/catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/dmaap/publisher.py index e5d19871..e9ff1b0a 100644 --- a/catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/dmaap/publisher.py +++ b/catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/dmaap/publisher.py @@ -38,6 +38,8 @@ class BatchPublisherClient: self.closed = False self.dont_send_until_ms = 0 self.scheduler = Scheduler(standalone=False) + self.api_key = '', + self.api_secret = '' @self.scheduler.interval_schedule(second=1) def crawl_job(): diff --git a/catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/test/test_consumer.py b/catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/test/test_consumer.py new file mode 100644 index 00000000..2d0447e4 --- /dev/null +++ b/catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/test/test_consumer.py @@ -0,0 +1,81 @@ +# Copyright (c) 2019, CMCC Technologies Co., Ltd. + +# 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. + +import base64 +import datetime +import hmac +import unittest +from _sha1 import sha1 + +from dmaap.consumer import ConsumerClient + + +class CreateApiKeyTest(unittest.TestCase): + def setUp(self): + self.apiKey = "7TuwzpLJ4QfQs4O" + self.apiSecret = "7TuwzpLJ4QfQs4O" + self.host = '127.0.0.1' + self.topic = 'abc' + self.group = 'def' + self.comsumer_id = '123' + self.timeout_ms = 3 + self.limit = 3 + self.filter = 'test' + + def tearDown(self): + self.ret_url = "" + + def test_create_url(self): + exp_url = 'http://127.0.0.1/events/abc/def/123' + consumer = ConsumerClient(self.host, self.topic, self.group, self.comsumer_id) + ret_url = consumer.create_url() + self.assertEqual(exp_url, ret_url) + + def test_create_timeout_url(self): + exp_url = 'http://127.0.0.1/events/abc/def/123?timeout=3' + consumer = ConsumerClient(self.host, self.topic, self.group, self.comsumer_id, self.timeout_ms) + ret_url = consumer.create_url() + self.assertEqual(exp_url, ret_url) + + def test_create_limit_url(self): + + exp_url = 'http://127.0.0.1/events/abc/def/123?timeout=3&limit=3' + consumer = ConsumerClient(self.host, self.topic, self.group, self.comsumer_id, + self.timeout_ms, self.limit) + ret_url = consumer.create_url() + self.assertEqual(exp_url, ret_url) + + def test_create_filter_url(self): + + exp_url = "http://127.0.0.1/events/abc/def/123?timeout=3&limit=3&filter=b'test'" + consumer = ConsumerClient(self.host, self.topic, self.group, self.comsumer_id, + self.timeout_ms, self.limit, self.filter) + ret_url = consumer.create_url() + self.assertEqual(exp_url, ret_url) + + def test_create_headers(self): + data = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S') + '-04:00' + hmac_code = hmac.new(self.apiSecret.encode(), data.encode(), sha1).digest() + signature = base64.b64encode(hmac_code).decode() + auth = self.apiKey + ':' + signature + exp_headers = { + 'X-CambriaDate': data, + 'X-CambriaAuth': auth + } + + consumer = ConsumerClient(self.host, self.topic, self.group, self.comsumer_id, + self.timeout_ms, self.limit, self.filter, self.apiKey, self.apiSecret) + consumer.set_api_credentials(self.apiKey, self.apiSecret) + rea_headers = consumer.create_headers() + self.assertEqual(exp_headers, rea_headers) diff --git a/catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/test/test_identity.py b/catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/test/test_identity.py new file mode 100644 index 00000000..87f63f41 --- /dev/null +++ b/catalog/pub/Dmaap-lib/nfvo-dmaap-python-lib/nfvo-dmaap-nfvo-v4.0-devel-ns/client/test/test_identity.py @@ -0,0 +1,38 @@ +# Copyright (c) 2019, CMCC Technologies Co., Ltd. + +# 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. + +import unittest +import mock + +from dmaap.identity import IdentityClient + + +class CreateApiKeyTest(unittest.TestCase): + def setUp(self): + self.apiKey = "7TuwzpLJ4QfQs4O" + self.apiSecret = "7TuwzpLJ4QfQs4O" + self.host = '127.0.0.1' + + def tearDown(self): + self.ret_url = "" + + @mock.patch.object(IdentityClient, 'create_apikey') + def test_create_apiKey(self, mock_create_apikey): + mock_create_apikey.return_value = { + 'apiKey': "7TuwzpLJ4QfQs4O", + 'apiSecret': "7TuwzpLJ4QfQs4O" + } + resp_data = IdentityClient(self.host).create_apikey('', 'description') + self.assertEqual(self.apiKey, resp_data.get("apiKey")) + self.assertEqual(self.apiSecret, resp_data.get("apiSecret")) -- cgit 1.2.3-korg