Best Python code snippet using autotest_python
test_access_log_delivery.py
Source:test_access_log_delivery.py
...136 parts = p.log_line_parser(log_line)137 clf = access_log_delivery.make_clf_from_parts(parts)138 expect = '2 - - [1/01/3:4:5:6 +0000] "5 /v1/a/c/o?foo 7" 8 13 "9" "10"'139 self.assertEquals(clf, expect)140 def test_convert_log_line(self):141 p = access_log_delivery.AccessLogDelivery({}, DumbLogger())142 log_line = [str(x) for x in range(18)]143 log_line[1] = 'proxy-server'144 log_line[4] = '1/Jan/3/4/5/6'145 log_line[6] = '/v1/a/c/o?foo'146 log_line = 'x' * 16 + ' '.join(log_line)147 res = p.convert_log_line(log_line)148 expected = (149 '2 - - [1/01/3:4:5:6 +0000] "5 /v1/a/c/o?foo 7" 8 13 "9" "10"',150 'a',151 'c')152 self.assertEquals(res, expected)153 def test_get_container_save_log_flag(self):154 p = access_log_delivery.AccessLogDelivery({}, DumbLogger())155 def my_get_metadata_true(*a, **kw):156 return {p.metadata_key: 'yes'}157 def my_get_metadata_true_upper(*a, **kw):158 return {p.metadata_key: 'YES'}159 def my_get_metadata_false(*a, **kw):160 return {p.metadata_key: 'no'}161 p.internal_proxy.get_container_metadata = my_get_metadata_false162 p.memcache = FakeMemcache()163 res = p.get_container_save_log_flag('a', 'c1')164 expected = False165 self.assertEquals(res, expected)166 p.internal_proxy.get_container_metadata = my_get_metadata_true167 p.memcache = FakeMemcache()168 res = p.get_container_save_log_flag('a', 'c2')169 expected = True170 self.assertEquals(res, expected)171 p.internal_proxy.get_container_metadata = my_get_metadata_true_upper172 p.memcache = FakeMemcache()173 res = p.get_container_save_log_flag('a', 'c3')174 expected = True175 self.assertEquals(res, expected)176 def test_process_one_file(self):177 with temptree([]) as t:178 conf = {'working_dir': t}179 p = access_log_delivery.AccessLogDelivery(conf, DumbLogger())180 def my_get_object_data(*a, **kw):181 all_lines = []182 log_line = [str(x) for x in range(18)]183 log_line[1] = 'proxy-server'184 log_line[4] = '1/Jan/3/4/5/6'185 log_line[6] = '/v1/a/c/o?foo'186 yield 'x' * 16 + ' '.join(log_line)187 log_line = [str(x) for x in range(18)]188 log_line[1] = 'proxy-server'189 log_line[4] = '1/Jan/3/4/5/6'190 log_line[6] = '/v1/a/c/o'191 yield 'x' * 16 + ' '.join(log_line)192 log_line = [str(x) for x in range(18)]193 log_line[1] = 'proxy-server'194 log_line[4] = '1/Jan/3/4/5/6'195 log_line[6] = '/v1/a2/c2/o2'196 yield 'x' * 16 + ' '.join(log_line)197 def my_get_container_save_log_flag(*a, **kw):198 return True199 p.get_object_data = my_get_object_data200 p.get_container_save_log_flag = my_get_container_save_log_flag201 res = p.process_one_file('a', 'c', '2011/03/14/12/hash')202 expected = ['%s/a2/c2/2011/03/14/12' % t,203 '%s/a/c/2011/03/14/12' % t]204 self.assertEquals(res, set(expected))205 lines = [p.convert_log_line(x)[0] for x in my_get_object_data()]206 with open(expected[0], 'rb') as f:207 raw = f.read()208 res = '\n'.join(lines[2:]) + '\n'209 self.assertEquals(res, raw)210 with open(expected[1], 'rb') as f:211 raw = f.read()212 res = '\n'.join(lines[:2]) + '\n'213 self.assertEquals(res, raw)214if __name__ == '__main__':...
draft_plotting.py
Source:draft_plotting.py
1import matplotlib.pyplot as plt2import datetime3import formulas4import time5def infinite_update_plots():6 pos_log_file = 07 pos_control_file = 08 time_array = []9 log_data = 18 * []10 time_control_array = []11 control_log_data = 25 * []12 today = str(datetime.date.today())13 today = today[5:7] + today[8:10] + today[2:4]14 while True:15 # getting data from transfer logs16 start_time = time.time()17 file_name = "logs/pr_temp_" + str(today) + '.log'18 f = open(file_name, "r")19 f.seek(pos_log_file)20 for x in f:21 # print(x)22 try:23 line_data = [float(y) for y in x.split()]24 #25 time_array.append(line_data[0])26 log_data.append(line_data)27 except ValueError:28 continue29 # print(time.time() - start_time)30 pos_log_file = f.tell()31 f.close()32 # getting data from control logs33 control_log_filename = '/data/3m/' + today + '/control.log'34 file_control = open(control_log_filename, 'r')35 file_control.seek(pos_control_file)36 for line in file_control:37 try:38 line_data = [float(y) for y in line.split()]39 time_control_array.append(line_data[0])40 control_log_data.append(line_data)41 except ValueError:42 continue43 pos_control_file = file_control.tell()44 file_control.close()45 #46 # pressure plot47 pressure1 = [formulas.pressure_voltage(log_line[7], log_line[11]) if formulas.pressure_voltage(48 log_line[7]) != 666. else None for log_line in log_data]49 pressure2 = [formulas.pressure_voltage(log_line[8], log_line[11]) if formulas.pressure_voltage(50 log_line[8]) != 666. else None for log_line in log_data]51 pressure3 = [formulas.pressure_voltage(log_line[9], log_line[11]) if formulas.pressure_voltage(52 log_line[9]) != 666. else None for log_line in log_data]53 pressure4 = [formulas.pressure_voltage2(log_line[10], log_line[11]) if formulas.pressure_voltage2(54 log_line[10]) != 666. else None for log_line in log_data]55 plt.plot(time_array, pressure1)56 plt.plot(time_array, pressure2)57 plt.plot(time_array, pressure3)58 plt.plot(time_array, pressure4)59 # ymin = min60 # plt.ylim((-15, 30))61 t = time.localtime()62 current_time = time.strftime("%I:%M:%S %p", t)63 plt.title('All day pressure ' + current_time)64 plt.ylabel('Pressure, psi')65 plt.xlabel('Time, SSM')66 plt.legend(['3M probe 1', 'Tank probe 1', 'Tank probe 2', '3M probe 2'])67 plt.savefig('static/pressure_all.png')68 plt.close()69 # second plot here70 plt.plot(time_array[-200:], pressure1[-200:])71 plt.plot(time_array[-200:], pressure2[-200:])72 plt.plot(time_array[-200:], pressure3[-200:])73 plt.plot(time_array[-200:], pressure4[-200:])74 plt.title('Recent pressure ' + current_time)75 plt.ylabel('Pressure, psi')76 plt.xlabel('Time, SSM')77 plt.legend(['3M probe 1', 'Tank probe 1', 'Tank probe 2', '3M probe 2'])78 plt.savefig('static/pressure_recent.png')79 plt.close()80 # plt.show()81 # now temperature82 temperature1 = [log_line[13] if log_line[13] > 0 else None for log_line in log_data]83 temperature2 = [log_line[14] if log_line[14] > 0 else None for log_line in log_data]84 temperature3 = [log_line[15] if log_line[15] > 0 else None for log_line in log_data]85 temperature4 = [log_line[16] if log_line[16] > 0 else None for log_line in log_data]86 temperature5 = [formulas.temperature_volt(log_line[4], log_line[5]) if87 log_line[4] > 0 and formulas.temperature_volt(log_line[4], log_line[5]) < 200 else None88 for log_line in89 log_data]90 temperature6 = [formulas.temperature_volt(log_line[1], log_line[5]) if91 log_line[1] > 0 and formulas.temperature_volt(log_line[1], log_line[5]) < 20092 else None for log_line in93 log_data]94 plt.plot(time_array, temperature1)95 plt.plot(time_array, temperature2)96 plt.plot(time_array, temperature3)97 plt.plot(time_array, temperature4)98 plt.plot(time_array, temperature5)99 # plt.plot(time_array, temperature6)100 plt.title('All day temperature ' + current_time)101 plt.ylabel('Temperature, C')102 plt.xlabel('Time, SSM')103 plt.legend(['Na port', 'Tank west bottom ', 'Tank bottom port', 'Tank top east', '3M finger', 'Transfer line'])104 plt.savefig('static/temperature_all.png')105 plt.close()106 # last temp107 plt.plot(time_array[-200:], temperature1[-200:])108 plt.plot(time_array[-200:], temperature2[-200:])109 plt.plot(time_array[-200:], temperature3[-200:])110 plt.plot(time_array[-200:], temperature4[-200:])111 plt.plot(time_array[-200:], temperature5[-200:])112 # plt.plot(time_array[-200:], temperature6[-200:])113 plt.title('Recent temperature ' + current_time)114 plt.ylabel('Temperature, C')115 plt.xlabel('Time, SSM')116 plt.legend(['Na port', 'Tank west bottom ', 'Tank bottom port', 'Tank top east', '3M finger', 'Transfer line'])117 plt.savefig('static/temperature_recent.png')118 plt.close()119 # adding control log plots120 temperature_in = [ctr_log_line[5] if ctr_log_line[5] > 0 else None for ctr_log_line in control_log_data]121 temperature_out = [ctr_log_line[8] if ctr_log_line[8] > 0 else None for ctr_log_line in control_log_data]122 temperature_ta = [ctr_log_line[11] if ctr_log_line[11] > 0 else None for ctr_log_line in control_log_data]123 heater_power = [ctr_log_line[4] if ctr_log_line[4] >= 0 else None for ctr_log_line in control_log_data]124 plt.plot(time_control_array, temperature_in)125 plt.plot(time_control_array, temperature_out)126 plt.plot(time_array, temperature3)127 plt.plot(time_control_array, heater_power)128 plt.title('All day power, heater and tank bottom port temperature ' + current_time)129 plt.ylabel('Temperature, C')130 plt.xlabel('Time, SSM')131 plt.legend(['T oil in, C', 'T oil out, C', 'T tank bottom port, C', 'Heater power, %'])132 plt.savefig('static/temperature_heater.png')133 plt.close()134 plt.plot(time_control_array[-600 * 3:], temperature_in[-600 * 3:])135 plt.plot(time_control_array[-600 * 3:], temperature_out[-600 * 3:])136 plt.plot(time_array[-600 * 3:], temperature3[-600 * 3:])137 plt.plot(time_control_array[-600 * 3:], heater_power[-600 * 3:])138 plt.title('Last ten minutes power, heater and tank bottom port temperature ' + current_time)139 plt.ylabel('Temperature, C')140 plt.xlabel('Time, SSM')141 plt.legend(['T oil in, C', 'T oil out, C', 'T tank bottom port, C', 'Heater power, %'])142 plt.savefig('static/temperature_heater_recent.png')143 plt.close()144 time.sleep(3)145if __name__ == '__main__':...
test_access_processor.py
Source:test_access_processor.py
1# Copyright (c) 2010-2011 OpenStack, LLC.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or12# implied.13# See the License for the specific language governing permissions and14# limitations under the License.15# TODO: Tests16import unittest17from swift.stats import access_processor18class TestAccessProcessor(unittest.TestCase):19 def test_log_line_parser_field_count(self):20 p = access_processor.AccessLogProcessor({})21 # too few fields22 log_line = [str(x) for x in range(17)]23 log_line[1] = 'proxy-server'24 log_line[4] = '1/Jan/3/4/5/6'25 log_line[6] = '/v1/a/c/o'26 log_line = 'x'*16 + ' '.join(log_line)27 res = p.log_line_parser(log_line)28 expected = {}29 self.assertEquals(res, expected)30 # right amount of fields31 log_line = [str(x) for x in range(18)]32 log_line[1] = 'proxy-server'33 log_line[4] = '1/Jan/3/4/5/6'34 log_line[6] = '/v1/a/c/o'35 log_line = 'x'*16 + ' '.join(log_line)36 res = p.log_line_parser(log_line)37 expected = {'code': 8, 'processing_time': '17', 'auth_token': '11',38 'month': '01', 'second': '6', 'year': '3', 'tz': '+0000',39 'http_version': '7', 'object_name': 'o', 'etag': '14',40 'method': '5', 'trans_id': '15', 'client_ip': '2',41 'bytes_out': 13, 'container_name': 'c', 'day': '1',42 'minute': '5', 'account': 'a', 'hour': '4',43 'referrer': '9', 'request': '/v1/a/c/o',44 'user_agent': '10', 'bytes_in': 12, 'lb_ip': '3'}45 self.assertEquals(res, expected)46 # too many fields47 log_line = [str(x) for x in range(19)]48 log_line[1] = 'proxy-server'49 log_line[4] = '1/Jan/3/4/5/6'50 log_line[6] = '/v1/a/c/o'51 log_line = 'x'*16 + ' '.join(log_line)52 res = p.log_line_parser(log_line)53 expected = {'code': 8, 'processing_time': '17', 'auth_token': '11',54 'month': '01', 'second': '6', 'year': '3', 'tz': '+0000',55 'http_version': '7', 'object_name': 'o', 'etag': '14',56 'method': '5', 'trans_id': '15', 'client_ip': '2',57 'bytes_out': 13, 'container_name': 'c', 'day': '1',58 'minute': '5', 'account': 'a', 'hour': '4',59 'referrer': '9', 'request': '/v1/a/c/o',60 'user_agent': '10', 'bytes_in': 12, 'lb_ip': '3'}61 self.assertEquals(res, expected)62if __name__ == '__main__':...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!