Best Python code snippet using localstack_python
test_partitioning.py
Source:test_partitioning.py
1import datetime2import unittest3from elasticsearch_partition import formatters, partitioning4class _PyDateFormatter(formatters.DateFormatter):5 def fmt_year(self, year, wildcard):6 date = datetime.date(year=year, month=1, day=1)7 if wildcard:8 return date.strftime("%Y{sep}*".format(sep=self.sep))9 return date.strftime("%Y")10 def fmt_month(self, year, month, wildcard):11 date = datetime.date(year=year, month=month, day=1)12 if wildcard:13 return date.strftime("%Y{sep}%m{sep}*".format(sep=self.sep))14 return date.strftime("%Y{sep}%m".format(sep=self.sep))15 def fmt_day(self, year, month, day):16 date = datetime.date(year, month, day)17 return date.strftime("%Y{sep}%m{sep}%d".format(sep=self.sep))18class TestRangePartition(unittest.TestCase):19 def setUp(self):20 self.cls = partitioning.RangePartition21 self.since = datetime.date(2014, 9, 27)22 self.until = datetime.date(2018, 2, 4)23 def test_partition_by_day(self):24 partition = self.cls(frequency=partitioning.DAY)25 expected = [26 "logs-2014-09-27",27 "logs-2014-09-28",28 "logs-2014-09-29",29 "logs-2014-09-30",30 "logs-2014-10-*",31 "logs-2014-11-*",32 "logs-2014-12-*",33 "logs-2015-*",34 "logs-2016-*",35 "logs-2017-*",36 "logs-2018-01-*",37 "logs-2018-02-01",38 "logs-2018-02-02",39 "logs-2018-02-03",40 "logs-2018-02-04",41 ]42 actual = partition("logs-*", self.since, self.until)43 self.assertListEqual(actual, expected)44 def test_partition_by_day_only_since(self):45 partition = self.cls(46 frequency=partitioning.DAY,47 now_func=lambda: datetime.date(2018, 7, 4)48 )49 expected = [50 "logs-2014-09-27",51 "logs-2014-09-28",52 "logs-2014-09-29",53 "logs-2014-09-30",54 "logs-2014-10-*",55 "logs-2014-11-*",56 "logs-2014-12-*",57 "logs-2015-*",58 "logs-2016-*",59 "logs-2017-*",60 "logs-2018-01-*",61 "logs-2018-02-*",62 "logs-2018-03-*",63 "logs-2018-04-*",64 "logs-2018-05-*",65 "logs-2018-06-*",66 "logs-2018-07-01",67 "logs-2018-07-02",68 "logs-2018-07-03",69 "logs-2018-07-04",70 ]71 actual = partition("logs-*", self.since)72 self.assertSequenceEqual(actual, expected)73 def test_partition_by_day_only_until(self):74 partition = self.cls(75 frequency=partitioning.DAY,76 now_func=lambda: datetime.date(2018, 7, 4)77 )78 expected = [79 "-logs-2018-02-04",80 "-logs-2018-02-05",81 "-logs-2018-02-06",82 "-logs-2018-02-07",83 "-logs-2018-02-08",84 "-logs-2018-02-09",85 "-logs-2018-02-10",86 "-logs-2018-02-11",87 "-logs-2018-02-12",88 "-logs-2018-02-13",89 "-logs-2018-02-14",90 "-logs-2018-02-15",91 "-logs-2018-02-16",92 "-logs-2018-02-17",93 "-logs-2018-02-18",94 "-logs-2018-02-19",95 "-logs-2018-02-20",96 "-logs-2018-02-21",97 "-logs-2018-02-22",98 "-logs-2018-02-23",99 "-logs-2018-02-24",100 "-logs-2018-02-25",101 "-logs-2018-02-26",102 "-logs-2018-02-27",103 "-logs-2018-02-28",104 "-logs-2018-03-*",105 "-logs-2018-04-*",106 "-logs-2018-05-*",107 "-logs-2018-06-*",108 "-logs-2018-07-01",109 "-logs-2018-07-02",110 "-logs-2018-07-03",111 "-logs-2018-07-04",112 "logs-*"113 ]114 actual = partition("logs-*", until=self.until)115 self.assertSequenceEqual(actual, expected)116 def test_partition_by_month(self):117 partition = self.cls(frequency=partitioning.MONTH)118 expected = [119 "logs-2014-09",120 "logs-2014-10",121 "logs-2014-11",122 "logs-2014-12",123 "logs-2015-*",124 "logs-2016-*",125 "logs-2017-*",126 "logs-2018-01",127 "logs-2018-02",128 ]129 actual = partition("logs-*", self.since, self.until)130 self.assertListEqual(actual, expected)131 def test_partition_by_month_only_since(self):132 partition = self.cls(133 frequency=partitioning.MONTH,134 now_func=lambda: datetime.date(2018, 7, 4)135 )136 expected = [137 "logs-2014-09",138 "logs-2014-10",139 "logs-2014-11",140 "logs-2014-12",141 "logs-2015-*",142 "logs-2016-*",143 "logs-2017-*",144 "logs-2018-01",145 "logs-2018-02",146 "logs-2018-03",147 "logs-2018-04",148 "logs-2018-05",149 "logs-2018-06",150 "logs-2018-07",151 ]152 actual = partition("logs-*", self.since)153 self.assertListEqual(actual, expected)154 def test_partition_by_month_only_until(self):155 partition = self.cls(156 frequency=partitioning.MONTH,157 now_func=lambda: datetime.date(2018, 7, 4)158 )159 expected = [160 "-logs-2018-02",161 "-logs-2018-03",162 "-logs-2018-04",163 "-logs-2018-05",164 "-logs-2018-06",165 "-logs-2018-07",166 "logs-*"167 ]168 actual = partition("logs-*", until=self.until)169 self.assertListEqual(actual, expected)170 def test_partition_by_year(self):171 partition = self.cls(frequency=partitioning.YEAR)172 expected = [173 "logs-2014",174 "logs-2015",175 "logs-2016",176 "logs-2017",177 "logs-2018",178 ]179 actual = partition("logs-*", self.since, self.until)180 self.assertListEqual(actual, expected)181 def test_partition_by_same_year(self):182 partition = self.cls(frequency=partitioning.YEAR)183 expected = ["logs-2018"]184 actual = partition(185 "logs-*",186 datetime.date(2018, 7, 4),187 datetime.date(2018, 8, 9)188 )189 self.assertListEqual(actual, expected)190 def test_partition_by_year_only_since(self):191 partition = self.cls(192 frequency=partitioning.YEAR,193 now_func=lambda: datetime.date(2018, 7, 4)194 )195 expected = [196 "logs-2014",197 "logs-2015",198 "logs-2016",199 "logs-2017",200 "logs-2018",201 ]202 actual = partition("logs-*", self.since)203 self.assertListEqual(actual, expected)204 def test_partition_by_year_only_until(self):205 partition = self.cls(206 frequency=partitioning.YEAR,207 now_func=lambda: datetime.date(2018, 7, 4)208 )209 expected = ["logs-*"]210 actual = partition("logs-*", until=self.until)211 self.assertListEqual(actual, expected)212 def test_partition_with_custom_formatter(self):213 partition = self.cls(214 frequency=partitioning.DAY,215 formatter=formatters.MiddleEndianDateFormatter(),216 escape="@"217 )218 expected = [219 "logs-09-27-2014",220 "logs-09-28-2014",221 "logs-09-29-2014",222 "logs-09-30-2014",223 "logs-10-*-2014",224 "logs-11-*-2014",225 "logs-12-*-2014",226 "logs-*-2015",227 "logs-*-2016",228 "logs-*-2017",229 "logs-01-*-2018",230 "logs-02-01-2018",231 "logs-02-02-2018",232 "logs-02-03-2018",233 "logs-02-04-2018",234 ]235 actual = partition("logs-@", self.since, self.until)236 self.assertListEqual(actual, expected)237 def test_partition_with_custom_py_formatter(self):238 partition = self.cls(239 frequency=partitioning.DAY,240 formatter=_PyDateFormatter(),241 )242 expected = [243 "logs-2014-09-27",244 "logs-2014-09-28",245 "logs-2014-09-29",246 "logs-2014-09-30",247 "logs-2014-10-*",248 "logs-2014-11-*",249 "logs-2014-12-*",250 "logs-2015-*",251 "logs-2016-*",252 "logs-2017-*",253 "logs-2018-01-*",254 "logs-2018-02-01",255 "logs-2018-02-02",256 "logs-2018-02-03",257 "logs-2018-02-04",258 ]259 actual = partition("logs-*", self.since, self.until)260 self.assertListEqual(actual, expected)261 def test_partition_with_wrong_pattern(self):262 partition = self.cls(escape="@")263 with self.assertRaises(ValueError) as ctx:264 partition("logs-*")265 self.assertEqual(266 "Index pattern 'logs-*' doesn't contain a special character '@'",267 str(ctx.exception)268 )269 def test_partition_without_tw_parameters(self):270 partition = self.cls()271 with self.assertRaises(ValueError) as ctx:272 partition("logs-*")273 self.assertEqual(274 "You should use 'since' or 'until' for searching by "275 "partitioning index",276 str(ctx.exception)277 )278if __name__ == "__main__":...
test_logutils.py
Source:test_logutils.py
...66 assert logs_filesystem.read(job, start=1, end=2) == "or\n" # nosec67 assert logs_filesystem.read(job, start=1, end=20) == "or\nnot" # nosec68 assert logs_filesystem.read(job, start=2, end=2) == "" # nosec69 assert logs_filesystem.read(job, start=1, end=0) == "" # nosec70def test_size_logs(mocker, tmpdir, logs_filesystem):71 job = mocker.Mock()72 job.output_dir = tmpdir73 with lzma.open(str(tmpdir / "output.yaml.xz"), "wb") as f_logs:74 f_logs.write("hello world\nhow are you?\n".encode("utf-8"))75 # "output.yaml.size" is missing76 assert logs_filesystem.size(job) is None # nosec77 (tmpdir / "output.yaml.size").write_text("25", encoding="utf-8")78 assert logs_filesystem.size(job) == 25 # nosec79 with open(str(tmpdir / "output.yaml"), "wb") as f_logs:80 f_logs.write("hello world!\n".encode("utf-8"))81 assert logs_filesystem.size(job) == 13 # nosec82def test_write_logs(mocker, tmpdir, logs_filesystem):83 job = mocker.Mock()84 job.output_dir = tmpdir85 with open(str(tmpdir / "output.yaml"), "wb") as f_logs:86 with open(str(tmpdir / "output.idx"), "wb") as f_idx:87 logs_filesystem.write(job, "hello world\n".encode("utf-8"), f_logs, f_idx)88 logs_filesystem.write(job, "how are you?\n".encode("utf-8"), f_logs, f_idx)89 assert logs_filesystem.read(job) == "hello world\nhow are you?\n" # nosec90 assert logs_filesystem.size(job) == 25 # nosec91 with open(str(tmpdir / "output.idx"), "rb") as f_idx:92 assert f_idx.read(8) == b"\x00\x00\x00\x00\x00\x00\x00\x00" # nosec93 assert f_idx.read(8) == b"\x0c\x00\x00\x00\x00\x00\x00\x00" # nosec94@unittest.skipIf(check_pymongo(), "openocd not installed")95def test_mongo_logs(mocker):96 mocker.patch("pymongo.database.Database.command")97 mocker.patch("pymongo.collection.Collection.create_index")98 logs_mongo = LogsMongo()99 job = mocker.Mock()100 job.id = 1101 insert_one = mocker.MagicMock()102 find = mocker.MagicMock()103 find_ret_val = [104 {"dt": "2020-03-25T19:44:36.209548", "lvl": "info", "msg": "first message"},105 {"dt": "2020-03-26T19:44:36.209548", "lvl": "info", "msg": "second message"},106 ]107 find.return_value = find_ret_val108 mocker.patch("pymongo.collection.Collection.find", find)109 mocker.patch("pymongo.collection.Collection.insert_one", insert_one)110 logs_mongo.write(111 job,112 '- {"dt": "2020-03-25T19:44:36.209548", "lvl": "info", "msg": "lava-dispatcher, installed at version: 2020.02"}',113 )114 insert_one.assert_called_with(115 {116 "job_id": 1,117 "dt": "2020-03-25T19:44:36.209548",118 "lvl": "info",119 "msg": "lava-dispatcher, installed at version: 2020.02",120 }121 ) # nosec122 result = yaml_load(logs_mongo.read(job))123 assert len(result) == 2 # nosec124 assert result == find_ret_val # nosec125 # size of find_ret_val in bytes126 assert logs_mongo.size(job) == 137 # nosec127 assert logs_mongo.open(job).read() == yaml_dump(find_ret_val).encode("utf-8")128def test_elasticsearch_logs(mocker, logs_elasticsearch):129 job = mocker.Mock()130 job.id = 1131 post = mocker.MagicMock()132 get = mocker.MagicMock()133 get_ret_val = mocker.Mock()134 # Test with empty object first.135 get_ret_val.text = "{}"136 get.return_value = get_ret_val137 mocker.patch("requests.get", get)138 result = logs_elasticsearch.read(job)139 assert result == ""140 # Normal test.141 get_ret_val.text = '{"hits":{"hits":[{"_source":{"dt": 1585165476209, "lvl": "info", "msg": "first message"}}, {"_source":{"dt": 1585165476210, "lvl": "info", "msg": "second message"}}]}}'142 get.return_value = get_ret_val...
log_reader.py
Source:log_reader.py
...76 for fname in logs:77 yield fname78 def api_read(self):79 result = {}80 def parse_apache_logs(config_file):81 error_log = re.search('(?<=ErrorLog )(.*?)\s', config_file)82 custom_log = re.search('(?<=CustomLog )(.*?)\s', config_file)83 if error_log and custom_log:84 return [error_log.group(1), custom_log.group(1)]85 elif error_log:86 return [error_log.group(1)]87 elif custom_log:88 return [custom_log.group(1)]89 else:90 return ''91 config_file = self.exec_payload('apache_config_files')['apache_config']92 for config in config_file:93 apache_logs = parse_apache_logs(self.shell.read(config))94 for file_path, content in self.read_multi(apache_logs):95 if content:96 result[file_path] = content97 fname_iter = self.fname_generator()98 for file_path, content in self.read_multi(fname_iter):99 if content:100 result[file_path] = content101 return result102 def run_read(self):103 api_result = self.api_read()104 if not api_result:105 return 'No log files not found.'106 else:107 rows = []...
admin_logs_routes.js
Source:admin_logs_routes.js
1/**2 Index redirects to a default logs index.3 @class AdminLogsIndexRoute4 @extends Discourse.Route5 @namespace Discourse6 @module Discourse7**/8Discourse.AdminLogsIndexRoute = Discourse.Route.extend({9 redirect: function() {10 this.transitionTo('adminLogs.staffActionLogs');11 }12});13/**14 The route that lists staff actions that were logged.15 @class AdminLogsStaffActionLogsRoute16 @extends Discourse.Route17 @namespace Discourse18 @module Discourse19**/20Discourse.AdminLogsStaffActionLogsRoute = Discourse.Route.extend({21 renderTemplate: function() {22 this.render('admin/templates/logs/staff_action_logs', {into: 'adminLogs'});23 },24 setupController: function(controller) {25 var queryParams = Discourse.URL.get('queryParams');26 if (queryParams) {27 controller.set('filters', queryParams);28 }29 return controller.show();30 },31 actions: {32 showDetailsModal: function(logRecord) {33 Discourse.Route.showModal(this, 'admin_staff_action_log_details', logRecord);34 this.controllerFor('modal').set('modalClass', 'log-details-modal');35 },36 showCustomDetailsModal: function(logRecord) {37 Discourse.Route.showModal(this, logRecord.action_name + '_details', logRecord);38 this.controllerFor('modal').set('modalClass', 'tabbed-modal log-details-modal');39 }40 },41 deactivate: function() {42 this._super();43 // Clear any filters when we leave the route44 Discourse.URL.set('queryParams', null);45 }46});47/**48 The route that lists blocked email addresses.49 @class AdminLogsScreenedEmailsRoute50 @extends Discourse.Route51 @namespace Discourse52 @module Discourse53**/54Discourse.AdminLogsScreenedEmailsRoute = Discourse.Route.extend({55 renderTemplate: function() {56 this.render('admin/templates/logs/screened_emails', {into: 'adminLogs'});57 },58 setupController: function() {59 return this.controllerFor('adminLogsScreenedEmails').show();60 }61});62/**63 The route that lists screened IP addresses.64 @class AdminLogsScreenedIpAddresses65 @extends Discourse.Route66 @namespace Discourse67 @module Discourse68**/69Discourse.AdminLogsScreenedIpAddressesRoute = Discourse.Route.extend({70 renderTemplate: function() {71 this.render('admin/templates/logs/screened_ip_addresses', {into: 'adminLogs'});72 },73 setupController: function() {74 return this.controllerFor('adminLogsScreenedIpAddresses').show();75 }76});77/**78 The route that lists screened URLs.79 @class AdminLogsScreenedUrlsRoute80 @extends Discourse.Route81 @namespace Discourse82 @module Discourse83**/84Discourse.AdminLogsScreenedUrlsRoute = Discourse.Route.extend({85 renderTemplate: function() {86 this.render('admin/templates/logs/screened_urls', {into: 'adminLogs'});87 },88 setupController: function() {89 return this.controllerFor('adminLogsScreenedUrls').show();90 }...
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!!