1 # -------------------------------------------------------------------------
2 # Copyright (c) 2017-2018 AT&T Intellectual Property
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # -------------------------------------------------------------------------
22 from osdf.logging import osdf_logging as L1
23 from osdf.logging.osdf_logging import OOF_OSDFLogMessageHelper as MH
24 from osdf.logging.osdf_logging import OOF_OSDFLogMessageFormatter as formatter
25 from unittest import mock
28 class TestOSDFLogging(unittest.TestCase):
30 def __init__(self, *args, **kwargs):
31 super(self.__class__, self).__init__(*args, **kwargs)
33 self.err = Exception("Some Exception")
34 self.req_id = "TEST-Req-ID"
35 self.url = mock.MagicMock()
36 self.request = mock.MagicMock()
37 self.client = mock.MagicMock()
38 self.service_name = mock.MagicMock()
39 self.callback_url = mock.MagicMock()
40 self.service_name = mock.MagicMock()
41 self.body = mock.MagicMock()
42 self.desc = mock.MagicMock()
43 self.response = mock.MagicMock()
44 self.remote_addr = mock.MagicMock()
45 self.json_body = mock.MagicMock()
48 def test_log_handlers_pre_onap(self):
49 res = L1.log_handlers_pre_onap()
50 assert type(res) == dict
52 def test_format_exception(self):
53 res = L1.format_exception(Exception("Some error"))
55 def test_accepted_valid_request(self):
56 res = formatter.accepted_valid_request(self.req_id, self.request)
57 assert res.startswith("Accepted valid request")
59 def test_invalid_request(self):
60 res = formatter.invalid_request(self.req_id, self.err)
61 assert res.startswith("Invalid request")
63 def test_invalid_response(self):
64 res = formatter.invalid_response(self.req_id, self.err)
65 assert res.startswith("Invalid response")
67 def test_malformed_request(self):
68 res = formatter.malformed_request(self.request, self.err)
69 assert res.startswith("Malformed request")
71 def test_malformed_response(self):
72 res = formatter.malformed_response(self.response, self.client, self.err)
73 assert res.startswith("Malformed response")
75 def test_need_policies(self):
76 res = formatter.need_policies(self.req_id)
77 assert res.startswith("Policies required but")
79 def test_policy_service_error(self):
80 res = formatter.policy_service_error(self.url, self.req_id, self.err)
81 assert res.startswith("Unable to call policy")
83 def test_requesting_url(self):
84 res = formatter.requesting_url(self.url, self.req_id)
85 assert res.startswith("Making a call to URL")
87 def test_requesting(self):
88 res = formatter.requesting(self.service_name, self.req_id)
89 assert res.startswith("Making a call to service")
91 def test_error_requesting(self):
92 res = formatter.error_requesting(self.service_name, self.req_id, self.err)
93 assert res.startswith("Error while requesting service")
95 def test_error_calling_back(self):
96 res = formatter.error_calling_back(self.service_name, self.callback_url, self.err)
97 assert res.startswith("Error while posting result to callback URL")
99 def test_calling_back(self):
100 res = formatter.calling_back(self.req_id, self.callback_url)
101 assert res.startswith("Posting result to callback URL")
103 def test_calling_back_with_body(self):
104 res = formatter.calling_back_with_body(self.req_id, self.callback_url, self.body)
105 assert res.startswith("Posting result to callback URL")
107 def test_received_request(self):
108 res = formatter.received_request(self.url, self.remote_addr, self.json_body)
109 assert res.startswith("Received a call")
111 def test_new_worker_thread(self):
112 res = formatter.new_worker_thread(self.req_id)
113 assert res.startswith("Initiating new worker thread")
115 def test_inside_worker_thread(self):
116 res = formatter.inside_worker_thread(self.req_id)
117 assert res.startswith("Inside worker thread for request ID")
119 def test_inside_new_thread(self):
120 res = formatter.inside_new_thread(self.req_id)
121 assert res.startswith("Spinning up a new thread for request ID")
123 def test_processing(self):
124 res = formatter.processing(self.req_id, self.desc)
125 assert res.startswith("Processing request ID")
127 def test_processed(self):
128 res = formatter.processed(self.req_id, self.desc)
129 assert res.startswith("Processed request ID")
131 def test_error_while_processing(self):
132 res = formatter.error_while_processing(self.req_id, self.desc, self.err)
133 assert res.startswith("Error while processing request ID")
135 def test_creating_local_env(self):
136 res = formatter.creating_local_env(self.req_id)
137 assert res.startswith("Creating local environment request ID")
139 def test_error_local_env(self):
140 res = formatter.error_local_env(self.req_id, self.desc, self.err)
141 assert res.startswith("Error while creating local environment for request ID")
143 def test_error_response_posting(self):
144 res = formatter.error_response_posting(self.req_id, self.desc, self.err)
145 assert res.startswith("Error while posting a response for a request ID")
147 def test_received_http_response(self):
148 res = formatter.received_http_response(self.response)
149 assert res.startswith("Received response [code: ")
151 def test_sending_response(self):
152 res = formatter.sending_response(self.req_id, self.desc)
153 assert res.startswith("Response is sent for request ID")
155 def test_listening_response(self):
156 res = formatter.listening_response(self.req_id, self.desc)
157 assert res.startswith("Response is sent for request ID")
159 def test_items_received(self):
160 res = formatter.items_received(10, "messages")
161 assert res == "Received 10 messages"
163 def test_items_sent(self):
164 res = formatter.items_sent(10, "messages")
165 assert res == "Published 10 messages"
167 def test_warn_audit_error(self):
168 """Log the message to error_log.warn and audit_log.warn"""
169 L1.warn_audit_error("Some warning message")
171 def test_log_message_multi(msg):
172 X = L1.log_handlers_pre_onap()
174 X["error"].error, X["error"].warn, X["audit"].info,
175 X["metrics"].info, X["debug"].debug, X["error"].fatal
177 L1.log_message_multi("Some log message", *wanted_methods)
180 if __name__ == "__main__":