Best Python code snippet using pytest
test_cis_dil_benchmark_filesystem.py
Source:test_cis_dil_benchmark_filesystem.py
...10@pytest.mark.workstation111@pytest.mark.server112@pytest.mark.cis13def test_cramfs_module_loaded(record_property, host):14 record_property("code", "1.1.1.1")15 record_property("title", "CIS 1.1.1.1: Disable unused filesystem cramfs (Scored)")16 record_property("impact", 1.0)17 record_property(18 "description",19 """20Ensure mounting of cramfs is disabled.21Solution:22- Add the line `install cramfs /bin/true` to the file `/etc/modprobe.d/cramfs.conf`.23""",24 )25 assert "" == host.run("lsmod | grep cramfs").stdout26 # modprobe = host.run(f"modprobe -n -v cramfs")27 # assert "install /bin/true" == modprobe.stdout28@pytest.mark.workstation129@pytest.mark.server130@pytest.mark.cis31def test_freevxfs_module_loaded(record_property, host):32 record_property("code", "1.1.1.2")33 record_property("title", "CIS 1.1.1.2: Disable unused filesystem freevxfs (Scored)")34 record_property("impact", 1.0)35 record_property(36 "description",37 """38Ensure mounting of freevxfs is disabled.39Solution:40- Add the line `install freevxfs /bin/true` to the file `/etc/modprobe.d/freevxfs.conf`.41""",42 )43 assert "" == host.run("lsmod | grep freevxfs").stdout44@pytest.mark.workstation145@pytest.mark.server146@pytest.mark.cis47def test_iffs2_module_loaded(record_property, host):48 record_property("code", "1.1.1.3")49 record_property("title", "CIS 1.1.1.3: Disable unused filesystem iffs2 (Scored)")50 record_property("impact", 1.0)51 record_property(52 "description",53 """54Ensure mounting of iffs2 is disabled.55Solution:56- Add the line `install iffs2 /bin/true` to the file `/etc/modprobe.d/iffs2.conf`.57""",58 )59 assert "" == host.run("lsmod | grep iffs2").stdout60@pytest.mark.workstation161@pytest.mark.server162@pytest.mark.cis63def test_hfs_module_loaded(record_property, host):64 record_property("code", "1.1.1.4")65 record_property("title", "CIS 1.1.1.4: Disable unused filesystem hfs (Scored)")66 record_property("impact", 1.0)67 record_property(68 "description",69 """70Ensure mounting of hfs is disabled.71Solution:72- Add the line `install hfs /bin/true` to the file `/etc/modprobe.d/hfs.conf`.73""",74 )75 assert "" == host.run("lsmod | grep hfs").stdout76@pytest.mark.workstation177@pytest.mark.server178@pytest.mark.cis79def test_hfsplus_module_loaded(record_property, host):80 record_property("code", "1.1.1.5")81 record_property("title", "CIS 1.1.1.5: Disable unused filesystem hfsplus (Scored)")82 record_property("impact", 1.0)83 record_property(84 "description",85 """86Ensure mounting of hfsplus is disabled.87Solution:88- Add the line `install hfsplus /bin/true` to the file `/etc/modprobe.d/hfsplus.conf`.89""",90 )91 assert "" == host.run("lsmod | grep hfsplus").stdout92@pytest.mark.workstation193@pytest.mark.server194@pytest.mark.cis95def test_squashfs_module_loaded(record_property, host):96 record_property("code", "1.1.1.6")97 record_property("title", "CIS 1.1.1.6: Disable unused filesystem squashfs (Scored)")98 record_property("impact", 1.0)99 record_property(100 "description",101 """102Ensure mounting of squashfs is disabled.103Solution:104- Add the line `install squashfs /bin/true` to the file105 `/etc/modprobe.d/squashfs.conf`.106""",107 )108 assert "" == host.run("lsmod | grep squashfs").stdout109 # modprobe = host.run(f"modprobe -n -v squashfs")110 # assert "install /bin/true" == modprobe.stdout111@pytest.mark.workstation1112@pytest.mark.server1113@pytest.mark.cis114def test_udf_module_loaded(record_property, host):115 record_property("code", "1.1.1.7")116 record_property("title", "CIS 1.1.1.7: Disable unused filesystem udf (Scored)")117 record_property("impact", 1.0)118 record_property(119 "description",120 """121Ensure mounting of udf is disabled.122Solution:123- Add the line `install udf /bin/true` to the file124 `/etc/modprobe.d/udf.conf`.125""",126 )127 assert "" == host.run("lsmod | grep udf").stdout128@pytest.mark.workstation2129@pytest.mark.server2130@pytest.mark.cis131def test_fat_module_loaded(record_property, host):132 record_property("code", "1.1.1.8")133 record_property("title", "CIS 1.1.1.8: Disable unused filesystem fat (Not Scored)")134 record_property("impact", 0)135 record_property(136 "description",137 """138Ensure mounting of fat is disabled.139Solution:140- Add the line `install fat /bin/true` to the file141 `/etc/modprobe.d/fat.conf`.142""",143 )144 assert "" == host.run("lsmod | grep fat").stdout145@pytest.mark.workstation1146@pytest.mark.server1147@pytest.mark.cis148def test_tmp_configured(record_property, host):149 record_property("code", "1.1.2")150 record_property("title", "CIS 1.1.2: Ensure /tmp is configured (Scored)")151 record_property("impact", 1.0)152 record_property(153 "description",154 """155The /tmp directory is a world-writable directory used for temporary storage by156all users and some applications.157Solution:158Configure /etc/fstab as appropriate.159example:160```161tmpfs /tmp tmpfs defaults,rw,nosuid,nodev,noexec,relatime 0 0162```163""",164 )165 assert_is_mounted(host, "/tmp")166@pytest.mark.workstation1167@pytest.mark.server1168@pytest.mark.cis169def test_tmp_with_nodev(record_property, host):170 record_property("code", "1.1.3")171 record_property(172 "title", "CIS 1.1.3: Ensure nodev option set on /tmp partition (Scored)"173 )174 record_property("impact", 1.0)175 record_property(176 "description",177 """178The nodev mount option specifies that the filesystem cannot contain special devices.179Solution:180Edit the /etc/fstab file and add nodev to the fourth field (mounting options) for the /tmp partition.181""",182 )183 assert_is_mounted_with_argument(host, "/tmp", "nodev")184@pytest.mark.workstation1185@pytest.mark.server1186@pytest.mark.cis187def test_tmp_with_nosuid(record_property, host):188 record_property("code", "1.1.4")189 record_property(190 "title", "CIS 1.1.4: Ensure nosuid option set on /tmp partition (Scored)"191 )192 record_property("impact", 1.0)193 record_property(194 "description",195 """196The nosuid mount option specifies that the filesystem cannot contain setuid197files.198Solution:199Edit the /etc/fstab file and add nosuid to the fourth field (mounting options) for the /tmp partition.200""",201 )202 assert_is_mounted_with_argument(host, "/tmp", "nosuid")203@pytest.mark.workstation1204@pytest.mark.server1205@pytest.mark.cis206def test_tmp_with_noexec(record_property, host):207 record_property("code", "1.1.5")208 record_property(209 "title", "CIS 1.1.5: Ensure noexec option set on /tmp partition (Scored)"210 )211 record_property("impact", 1.0)212 record_property(213 "description",214 """215The noexec mount option specifies that the filesystem cannot contain executable binaries.216Solution:217Edit the /etc/fstab file and add noexec to the fourth field (mounting options) for the /tmp partition.218""",219 )220 assert_is_mounted_with_argument(host, "/tmp", "noexec")221@pytest.mark.workstation2222@pytest.mark.server2223@pytest.mark.cis224def test_separate_partition_for_var(record_property, host):225 record_property("code", "1.1.6")226 record_property(227 "title", "CIS 1.1.6: Ensure separate partition exists for /var (Scored)"228 )229 record_property("impact", 1.0)230 record_property(231 "description",232 """233The /var directory is used by daemons and other system services to temporarily store234dynamic data. Some directories created by these processes may be world-writable.235Solution:236Configure /etc/fstab as appropriate.237""",238 )239 assert_is_mounted(host, "/var")240@pytest.mark.workstation2241@pytest.mark.server2242@pytest.mark.cis243def test_separate_partition_for_var_tmp(record_property, host):244 record_property("code", "1.1.7")245 record_property(246 "title", "CIS 1.1.7: Ensure separate partition exists for /var/tmp (Scored)"247 )248 record_property("impact", 1.0)249 record_property(250 "description",251 """252The /var/tmp directory is a world-writable directory used for temporary storage by all253users and some applications.254Solution:255Configure /etc/fstab as appropriate.256""",257 )258 assert_is_mounted(host, "/var/tmp")259@pytest.mark.workstation1260@pytest.mark.server1261@pytest.mark.cis262def test_nodev_on_var_tmp_if_exists(record_property, host):263 record_property("code", "1.1.8")264 record_property(265 "title", "CIS 1.1.8: Ensure nodev option set on /var/tmp partition (Scored)"266 )267 record_property("impact", 1.0)268 record_property(269 "description",270 """271The nodev mount option specifies that the filesystem cannot contain special devices.272Solution:273Edit the /etc/fstab file and add nodev to the fourth field (mounting options) for the274/var/tmp partition275""",276 )277 assert_is_mounted_with_argument(host, "/var/tmp", "nodev")278@pytest.mark.workstation1279@pytest.mark.server1280@pytest.mark.cis281def test_nosuid_on_var_tmp_if_exists(record_property, host):282 record_property("code", "1.1.9")283 record_property(284 "title", "CIS 1.1.9: Ensure nosuid option set on /var/tmp partition (Scored)"285 )286 record_property("impact", 1.0)287 record_property(288 "description",289 """290The nosuid mount option specifies that the filesystem cannot contain setuid files.291Solution:292Edit the /etc/fstab file and add nosuid to the fourth field (mounting options) for the293/var/tmp partition294""",295 )296 assert_is_mounted_with_argument(host, "/var/tmp", "nosuid")297@pytest.mark.workstation1298@pytest.mark.server1299@pytest.mark.cis300def test_noexec_on_var_tmp_if_exists(record_property, host):301 record_property("code", "1.1.10")302 record_property(303 "title", "CIS 1.1.10: Ensure noexec option set on /var/tmp partition (Scored)"304 )305 record_property("impact", 1.0)306 record_property(307 "description",308 """309The noexec mount option specifies that the filesystem cannot contain executable binaries.310Solution:311Edit the /etc/fstab file and add noexec to the fourth field (mounting options) for the312/var/tmp partition313""",314 )315 assert_is_mounted_with_argument(host, "/var/tmp", "noexec")316@pytest.mark.workstation2317@pytest.mark.server2318@pytest.mark.cis319def test_separate_partition_for_var_log(record_property, host):320 record_property("code", "1.1.11")321 record_property(322 "title", "CIS 1.1.11: Ensure separate partition exists for /var/log (Scored)"323 )324 record_property("impact", 1.0)325 record_property(326 "description",327 """328The /var/log directory is used by system services to store log data.329Solution:330Configure /etc/fstab as appropriate.331""",332 )333 assert_is_mounted(host, "/var/log")334@pytest.mark.workstation2335@pytest.mark.server2336@pytest.mark.cis337def test_separate_partition_for_var_log_audit(record_property, host):338 record_property("code", "1.1.12")339 record_property(340 "title",341 "CIS 1.1.12: Ensure separate partition exists for /var/log/audit (Scored)",342 )343 record_property("impact", 1.0)344 record_property(345 "description",346 """347The auditing daemon, auditd , stores log data in the /var/log/audit directory.348Solution:349Configure /etc/fstab as appropriate.350""",351 )352 assert_is_mounted(host, "/var/log/audit")353@pytest.mark.workstation2354@pytest.mark.server2355@pytest.mark.cis356def test_separate_partition_for_home(record_property, host):357 record_property("code", "1.1.13")358 record_property(359 "title", "CIS 1.1.13: Ensure separate partition exists for /home (Scored)"360 )361 record_property("impact", 1.0)362 record_property(363 "description",364 """365The /home directory is used to support disk storage needs of local users.366Solution:367Configure /etc/fstab as appropriate.368""",369 )370 assert_is_mounted(host, "/home")371@pytest.mark.workstation1372@pytest.mark.server1373@pytest.mark.cis374def test_home_with_nodev(record_property, host):375 record_property("code", "1.1.14")376 record_property(377 "title", "CIS 1.1.14: Ensure nodev option set on /home partition (Scored)"378 )379 record_property("impact", 1.0)380 record_property(381 "description",382 """383The nodev mount option specifies that the filesystem cannot contain special devices.384Solution:385Edit the /etc/fstab file and add nodev to the fourth field (mounting options) for the /home partition.386""",387 )388 assert_is_mounted_with_argument(host, "/home", "nodev")389@pytest.mark.workstation1390@pytest.mark.server1391@pytest.mark.cis392def test_dev_shm_with_nodev(record_property, host):393 record_property("code", "1.1.15")394 record_property(395 "title", "CIS 1.1.15: Ensure nodev option set on /dev/shm partition (Scored)"396 )397 record_property("impact", 1.0)398 record_property(399 "description",400 """401The nodev mount option specifies that the filesystem cannot contain special devices.402Solution:403Edit the /etc/fstab file and add nodev to the fourth field (mounting options) for the /dev/shm partition.404""",405 )406 assert_is_mounted_with_argument(host, "/dev/shm", "nodev")407@pytest.mark.workstation1408@pytest.mark.server1409@pytest.mark.cis410def test_dev_shm_with_nosuid(record_property, host):411 record_property("code", "1.1.16")412 record_property(413 "title", "CIS 1.1.16: Ensure nosuid option set on /dev/shm partition (Scored)"414 )415 record_property("impact", 1.0)416 record_property(417 "description",418 """419The nosuid mount option specifies that the filesystem cannot contain setuid files.420Solution:421Edit the /etc/fstab file and add nosuid to the fourth field (mounting options) for the /dev/shm partition.422""",423 )424 assert_is_mounted_with_argument(host, "/dev/shm", "nosuid")425@pytest.mark.workstation1426@pytest.mark.server1427@pytest.mark.cis428def test_dev_shm_with_noexec(record_property, host):429 record_property("code", "1.1.17")430 record_property(431 "title", "CIS 1.1.17: Ensure noexec option set on /dev/shm partition (Scored)"432 )433 record_property("impact", 1.0)434 record_property(435 "description",436 """437The noexec mount option specifies that the filesystem cannot contain executable binaries.438Solution:439Edit the /etc/fstab file and add nosuid to the fourth field (mounting options) for the /dev/shm partition.440""",441 )442 assert_is_mounted_with_argument(host, "/dev/shm", "noexec")443@pytest.mark.workstation1444@pytest.mark.server1445@pytest.mark.cis446def test_all_removable_media_partitions_with_nodev(record_property, host):447 record_property("code", "1.1.18")448 record_property(449 "title",450 "CIS 1.1.18: Ensure nodev option set on removable media partitions(Not Scored)",451 )452 record_property("impact", 0)453 record_property(454 "description",455 """456The nodev mount option specifies that the filesystem cannot contain special devices.457""",458 )459 pytest.skip("Not implemented yet")460@pytest.mark.workstation1461@pytest.mark.server1462@pytest.mark.cis463def test_all_removable_media_partitions_with_nosuid(record_property, host):464 record_property("code", "1.1.19")465 record_property(466 "title",467 "CIS 1.1.19: Ensure nosuid option set on removable media partitions (Not Scored)",468 )469 record_property("impact", 0)470 record_property(471 "description",472 """473The nosuid mount option specifies that the filesystem cannot contain special devices.474""",475 )476 pytest.skip("Not implemented yet")477@pytest.mark.workstation1478@pytest.mark.server1479@pytest.mark.cis480def test_all_removable_media_partitions_with_noexec(record_property, host):481 record_property("code", "1.1.20")482 record_property(483 "title",484 "CIS 1.1.20: Ensure noexec option set on removable media partitions (Not Scored)",485 )486 record_property("impact", 0)487 record_property(488 "description",489 """490The noexecmount option specifies that the filesystem cannot contain special devices.491""",492 )493 pytest.skip("Not implemented yet")494@pytest.mark.workstation1495@pytest.mark.server1496@pytest.mark.cis497@pytest.mark.slow498def test_sticky_for_world_writable_dirs(record_property, host):499 record_property("code", "1.1.21")500 record_property(501 "title",502 "CIS 1.1.21: Ensure sticky bit is set on all world-writable directories (Scored)",503 )504 record_property("impact", 1.0)505 record_property(506 "description",507 """508Setting the sticky bit on world writable directories prevents users from deleting or509renaming files in that directory that are not owned by them.510Solution:511Run the next command:512df --local -P | awk '{if (NR!=1) print $6}' \\513 | xargs -I '{}' find '{}' -xdev -type d \\( -perm -0002 -a ! -perm -1000 \\) 2>/dev/null \\514 | xargs -I '{}' chmod a+t '{}'515""",516 )517 search = host.run(518 "df --local --portability "519 " | awk '{if (NR!=1) print $6}' "520 " | sort " # added to increase speed521 r" | xargs -I '{}' find '{}' -xdev -type d \( -perm -0002 -a ! -perm -1000 \) 2>/dev/null"522 " | head -n1" # added to increase speed523 )524 assert (525 search.stdout == ""526 ), "sticky bit for all world-writable directories should be set."527@pytest.mark.server1528@pytest.mark.workstation2529@pytest.mark.cis530def test_disable_automounting(record_property, host):531 record_property("code", "1.1.22")532 record_property(533 "title",534 "CIS 1.1.22: Disable Automounting (Scored)",535 )536 record_property("impact", 1.0)537 record_property(538 "description",539 """540autofs allows automatic mounting of devices, typically including CD/DVDs and USB drives.541Solution:542Any command of:543# chkconfig autofs off544# systemctl disable autofs545# update-rc.d autofs disable546""",547 )548 autofs = host.service("autofs")549 assert (550 not autofs.is_running and not autofs.is_enabled551 ), "autofs should not be installed"552@pytest.mark.server1553@pytest.mark.workstation2554@pytest.mark.cis555def test_disable_usb_storage(record_property, host):556 record_property("code", "1.1.23")557 record_property(558 "title",559 "CIS 1.1.23: Disable USB Storage(Scored)",560 )561 record_property("impact", 1.0)562 record_property(563 "description",564 """565USB popularity and utility has led to USB-based malware being a simple and566common means for network infiltration and a first step to establishing a567persistent threat within a networked environment.568""",569 )570 assert (571 "" == host.run("lsmod | grep usb-storage").stdout...
test.py
Source:test.py
...31 inputs = [54.560333], [-3.576252]32 time, result = timing(flood_tool.get_easting_northing_from_lat_long,33 *inputs)34 matches = [r == approx(o, abs=tol) for r, o in zip(np.array([r.ravel() for r in result]).T, output)]35 record_property('single_lookup', (time, matches))36 input_headings = data[name]['input headings']37 output_headings = data[name]['output headings']38 idx1 = data[name]['idx1']39 idx2 = data[name]['idx2']40 args = list(testdb.iloc[idx1:idx2][input_headings].to_numpy().T)41 output = testdb.iloc[idx1:idx2][output_headings].to_numpy()42 time, result = timing(flood_tool.get_easting_northing_from_lat_long, *args)43 matches = [r == approx(np.array(o),44 abs=tol) for r, o in zip(np.array(result).T, output)]45 record_property('multiple_lookup', (time, matches))46 record_xml_attribute('points', calculate_score(time, matches,47 data[name]))48@fixture(scope="module")49def timed_tool(data, flood_tool):50 t0 = timer()51 postcode_file = os.sep.join([BASE_PATH] + data["postcode file"])52 risk_file = os.sep.join([BASE_PATH] + data["flood probability file"])53 value_file = os.sep.join([BASE_PATH] + data["property value file"])54 out = flood_tool.Tool(postcode_file,55 risk_file,56 value_file)57 t1 = timer()58 return t1 - t0, out59@fixture(scope="module")60def tool(timed_tool):61 return timed_tool[1]62@mark.timeout(timeouts["tool"])63def test_tool(timed_tool, record_property, record_xml_attribute):64 record_property('tool initialization', (timed_tool[0], [True]))65 record_xml_attribute('points', 100)66@mark.timeout(timeouts["get_lat_long"])67def test_get_lat_long(data, testdb, tool,68 record_property, record_xml_attribute):69 rel = data['get_lat_long']['tolerance']70 output = np.array([[51.379129999999996, 1.3067440000000001]])71 time, result = timing(tool.get_lat_long, ['CT7 9ET'])72 matches = [r == approx(o, rel=rel) for r, o in zip(result, output)]73 record_property('single_postcode_lookup',74 (time, matches))75 input_headings = data['get_lat_long']['input headings']76 output_headings = data['get_lat_long']['output headings']77 idx1 = data['get_lat_long']['idx1']78 idx2 = data['get_lat_long']['idx2']79 args = list(testdb.iloc[idx1:idx2][input_headings].to_numpy().ravel())80 output = testdb.iloc[idx1:idx2][output_headings].to_numpy()81 time, result = timing(tool.get_lat_long, args)82 matches = [r == approx(np.array(o),83 rel=rel) for r, o in zip(result, output)]84 record_property('multiple_postcode_lookup', (time, matches))85 record_xml_attribute('points', calculate_score(time, matches,86 data['get_lat_long']))87@mark.timeout(timeouts["get_easting_northing_flood_probability"])88def test_get_easting_northing_flood_probability(data, testdb, tool, record_property, record_xml_attribute):89 name = 'get_easting_northing_flood_probability'90 output = ['Zero']91 inputs = [[298169], [519487]]92 time, result = timing(getattr(tool, name),93 *inputs)94 matches = [r == o for r, o in zip(result, output)]95 record_property('single_lookup', (time, matches))96 input_headings = data[name]['input headings']97 output_headings = data[name]['output headings']98 idx1 = data[name]['idx1']99 idx2 = data[name]['idx2']100 args = list(testdb.iloc[idx1:idx2][input_headings].to_numpy().T)101 output = list(testdb.iloc[idx1:idx2][output_headings].to_numpy().ravel())102 time, result = timing(getattr(tool, name), *args)103 matches = [r == o for r, o in zip(result, output)]104 record_property('multiple_lookup', (time, matches))105 record_xml_attribute('points', calculate_score(time, matches,106 data[name]))107@mark.timeout(timeouts["get_sorted_flood_probability"])108def test_get_sorted_flood_probability(data, testdb, tool, record_property, record_xml_attribute):109 name = 'get_sorted_flood_probability'110 idx1 = data[name]['idx1']111 idx2 = data[name]['idx2']112 idx3 = data[name]['idx3']113 idx4 = data[name]['idx4']114 args = testdb.iloc[idx1:idx2]['Postcode'].to_numpy().ravel()115 output = testdb.iloc[idx3:idx4][['Postcode', 'Probability Band']]116 output.drop_duplicates(subset='Postcode', inplace=True)117 output.set_index('Postcode', inplace=True)118 time, result = timing(getattr(tool, name), args)119 assert result.index.name == 'Postcode'120 matches = list((result.index == output.index) &121 (result['Probability Band'].to_numpy().ravel()122 == output['Probability Band'].to_numpy().ravel()))123 record_property('multiple_lookup', (time, matches))124 record_xml_attribute('points', calculate_score(time, matches,125 data[name]))126@mark.timeout(timeouts["get_flood_cost"])127def test_get_flood_cost(data, testdb, tool,128 record_property, record_xml_attribute):129 name = 'get_flood_cost'130 rel = data[name]['tolerance']131 output = np.array([4646599.42])132 time, result = timing(getattr(tool, name), ['TN8 6AB'])133 matches = [r == approx(o) for r, o in zip(result, output)]134 record_property('single_postcode_lookup',135 (time, matches))136 input_headings = data[name]['input headings']137 output_headings = data[name]['output headings']138 idx1 = data[name]['idx1']139 idx2 = data[name]['idx2']140 args = list(testdb.iloc[idx1:idx2][input_headings].to_numpy().T)141 output = list(testdb.iloc[idx1:idx2][output_headings].to_numpy().ravel())142 time, result = timing(getattr(tool, name), *args)143 matches = [r == approx(o, rel) for r, o in zip(result, output)]144 record_property('multiple_postcode_lookup', (time, matches))145 record_xml_attribute('points', calculate_score(time, matches,146 data[name]))147@mark.timeout(timeouts["get_annual_flood_risk"])148def test_get_annual_flood_risk(data, testdb, tool,149 record_property, record_xml_attribute):150 name = 'get_annual_flood_risk'151 rel = data[name]['tolerance']152 output = np.array([193.506606])153 time, result = timing(getattr(tool, name), ['DA1 5NU'], ['Very Low'])154 matches = [r == approx(o) for r, o in zip(result, output)]155 record_property('single_postcode_lookup',156 (time, matches))157 input_headings = data[name]['input headings']158 output_headings = data[name]['output headings']159 idx1 = data[name]['idx1']160 idx2 = data[name]['idx2']161 args = list(testdb.iloc[idx1:idx2][input_headings].to_numpy().T)162 output = testdb.iloc[idx1:idx2][output_headings].to_numpy().ravel()163 time, result = timing(getattr(tool, name), *args)164 matches = [r == approx(o, rel) for r, o in zip(result, output)]165 record_property('multiple_postcode_lookup', (time, matches))166 record_xml_attribute('points', calculate_score(time, matches,167 data[name]))168@mark.timeout(timeouts["get_sorted_annual_flood_risk"])169def test_get_sorted_annual_flood_risk(data, testdb, tool,170 record_property, record_xml_attribute):171 name = 'get_sorted_annual_flood_risk'172 rel = data[name]['tolerance']173 idx1 = data[name]['idx1']174 idx2 = data[name]['idx2']175 idx3 = data[name]['idx3']176 idx4 = data[name]['idx4']177 args = testdb.iloc[idx1:idx2][['Postcode']].to_numpy().ravel()178 output = testdb.iloc[idx3:idx4][['Postcode', 'Flood Risk']]179 output.drop_duplicates(subset='Postcode', inplace=True)180 output.set_index('Postcode', inplace=True)181 time, result = timing(getattr(tool, name), args)182 assert result.index.name == 'Postcode'183 matches = list((result.index == output.index) &184 (result['Flood Risk'].to_numpy().ravel()185 == approx(output['Flood Risk'].to_numpy().ravel(), rel)))186 record_property('multiple_postcode_lookup', (time, matches))187 record_xml_attribute('points', calculate_score(time, matches,...
test_applet.py
Source:test_applet.py
...55############################################################################################56#: Case 1 is we send an APDU with no data, and we expect a simple 90 00 resp57#: with no data58def case1(record_property):59 record_property("case", 1)60 record_property("extended", False)61 apdu = APDU(cla=0x00, ins=0x01, p1=0x00, p2=0x00, lc=0x00, le=0x00, send_le=1)62 print(apdu)63 record_property("apdu", apdu)64 resp = reader.send_APDU(apdu)65 print(resp)66 record_property("resp", resp)67 assert hex(resp.sw1) == hex(0x90)68 assert hex(resp.sw2) == hex(0x00)69 assert resp.le == 070if TEST_T0:71 def test_case1_t0(mode_t0, select_applet, record_property):72 record_property("T", 0)73 case1(record_property)74if TEST_T1:75 def test_case1_t1(mode_t1, select_applet, record_property):76 record_property("T", 1)77 case1(record_property)78############################################################################################79#: Case 2 is we sned an APDU with no data, and we except a 90 00 resp with80#: recv bytes of data.81def case2(recv, record_property):82 record_property("case", 2)83 record_property("extended", False if recv < 256 else True)84 # Compute recv on two bytes85 ab = recv // 25686 cd = recv % 25687 # Adapt the case where we need an extended APDU88 send_le = 1 if recv < 256 else 289 # Prepare the APDU90 apdu = APDU(91 cla=0x00, ins=0x02, p1=ab, p2=cd, lc=0x0, le=0, send_le=send_le92 ) # le = recv93 print(apdu)94 record_property("apdu", apdu)95 # Send the APDU and get the response96 resp = reader.send_APDU(apdu)97 print(resp)98 record_property("resp", resp)99 # Check for 90 00100 assert hex(resp.sw1) == hex(0x90) or hex(resp.sw1) == hex(0x6C)101 assert hex(resp.sw2) == hex(0x00) or (102 hex(resp.sw2) == hex(cd) and hex(resp.sw1) == hex(0x6C)103 )104 # If 90 00 check that the length of the response is recv105 if resp.sw1 == 0x90 and resp.sw2 == 0:106 assert resp.le == recv107 else:108 assert resp.le == 0109 # Check each byte of the resp110 if resp.le == recv:111 for v in range(recv):112 assert resp.data[v] == v % 256113if TEST_T0:114 @pytest.mark.parametrize("recv", range(1, 300, STEP))115 def test_case2_t0(mode_t0, select_applet, recv, record_property):116 record_property("T", 0)117 case2(recv, record_property)118if TEST_T1:119 @pytest.mark.parametrize("x", range(1, 300, STEP))120 def test_case2_t1(mode_t1, select_applet, x, record_property):121 record_property("T", 1)122 case2(x, record_property)123def case3(send, record_property):124 record_property("case", 3)125 apdu = APDU(126 cla=0x00,127 ins=0x03,128 p1=0x00,129 p2=0x00,130 lc=send,131 data=range(send),132 le=0x00,133 send_le=0,134 )135 if send >= 256:136 record_property("extended", True)137 else:138 record_property("extended", False)139 print(apdu)140 record_property("apdu", apdu)141 resp = reader.send_APDU(apdu)142 print(resp)143 record_property("resp", resp)144 assert resp.sw1 == 0x90145 assert resp.sw2 == 0x00146 assert resp.le == 0147if TEST_T0:148 @pytest.mark.parametrize("send", range(1, 300, STEP))149 def test_case3_t0(mode_t0, select_applet, send, record_property):150 record_property("T", 0)151 case3(send, record_property)152if TEST_T1:153 @pytest.mark.parametrize("send", range(1, 300, STEP))154 def test_case3_t1(mode_t1, select_applet, send, record_property):155 record_property("T", 1)156 case3(send, record_property)157def case4(send, recv, record_property):158 record_property("case", 4)159 ab = recv // 256160 cd = recv % 256161 send_le = 1 if recv < 256 else 2162 apdu = APDU(163 cla=0x00,164 ins=0x04,165 p1=ab,166 p2=cd,167 lc=send,168 le=0,169 data=range(send),170 send_le=send_le,171 )172 if send >= 256 or recv >= 256:173 record_property("extended", True)174 else:175 record_property("extended", False)176 print(apdu)177 record_property("apdu", apdu)178 resp = reader.send_APDU(apdu)179 print(resp)180 record_property("resp", resp)181 assert resp.sw1 == 0x90182 assert resp.sw2 == 0x00183 assert resp.le == recv184if TEST_T0:185 @pytest.mark.parametrize("send", range(1, 300, STEP))186 @pytest.mark.parametrize("recv", range(1, 300, STEP))187 def test_case4_t0(mode_t0, select_applet, send, recv, record_property):188 record_property("T", 0)189 case4(send, recv, record_property)190if TEST_T1:191 @pytest.mark.parametrize("send", range(1, 300, STEP))192 @pytest.mark.parametrize("recv", range(1, 300, STEP))193 def test_case4_t1(mode_t1, select_applet, send, recv, record_property):194 record_property("T", 1)195 case4(send, recv, record_property)196def main(output_file="output.csv"):197 args = [198 sys.argv[0],199 "--csv",200 output_file,201 "--csv-columns",202 "module,doc,success,parameters_as_columns,properties_as_columns",203 ]204 pytest.main(args)205if __name__ == "__main__":...
test_vm2vm.py
Source:test_vm2vm.py
...65 66 transport1 = ssh.SSHTransport(vm_info[0]['fip'], 'ubuntu', password='dd', private_key=os_resources['keypair'].private_key)67 result1 = transport1.exec_command('iperf -c {} | tail -n 1'.format(vm_info[1]['private_address']))68 print ' '.join(result1.split()[-2::])69 record_property("same {0}-{1}".format(zone1[0],zone2[0]), ' '.join(result1.split()[-2::]))70 result2 = transport1.exec_command('iperf -c {} | tail -n 1'.format(vm_info[2]['private_address']))71 print ' '.join(result2.split()[-2::])72 record_property("diff host {0}-{1}".format(zone1[0],zone2[0]), ' '.join(result2.split()[-2::]))73 result3 = transport1.exec_command('iperf -c {} -P 10 | tail -n 1'.format(vm_info[2]['private_address']))74 print ' '.join(result3.split()[-2::])75 record_property("dif host 10 threads {0}-{1}".format(zone1[0],zone2[0]), ' '.join(result3.split()[-2::]))76 result4 = transport1.exec_command('iperf -c {} | tail -n 1'.format(vm_info[2]['fip']))77 print ' '.join(result4.split()[-2::])78 record_property("diff host fip {0}-{1}".format(zone1[0],zone2[0]), ' '.join(result4.split()[-2::]))79 result5 = transport1.exec_command('iperf -c {} | tail -n 1'.format(vm_info[3]['private_address']))80 print ' '.join(result5.split()[-2::])81 record_property("diff host, diff net {0}-{1}".format(zone1[0],zone2[0]), ' '.join(result5.split()[-2::]))82 print "Remove VMs"83 for vm in vms:84 openstack_clients.compute.servers.delete(vm)85 print "Remove FIPs"86 for fip in fips:87 openstack_clients.compute.floating_ips.delete(fip)88 except Exception as e:89 print e90 print "Something went wrong"91 for vm in vms:92 openstack_clients.compute.servers.delete(vm)93 for fip in fips:94 openstack_clients.compute.floating_ips.delete(fip)95 pytest.fail("Something went wrong")
Looking for an in-depth tutorial around pytest? LambdaTest covers the detailed pytest tutorial that has everything related to the pytest, from setting up the pytest framework to automation testing. Delve deeper into pytest testing by exploring advanced use cases like parallel testing, pytest fixtures, parameterization, executing multiple test cases from a single file, and more.
Skim our below pytest tutorial playlist to get started with automation testing using the pytest framework.
https://www.youtube.com/playlist?list=PLZMWkkQEwOPlcGgDmHl8KkXKeLF83XlrP
Get 100 minutes of automation test minutes FREE!!