Best Python code snippet using gabbi_python
test-semanage.py
Source:test-semanage.py
...12 '"not found" not found in %r' % err)13 def assertFailure(self, status):14 self.assertTrue(status != 0,15 '"semanage succeeded when it should have failed')16 def assertSuccess(self, status, err):17 self.assertTrue(status == 0,18 '"semanage should have succeeded for this test %r' % err)19 def test_extract(self):20 for object in object_list:21 if object in ["dontaudit","module","permissive"]:22 continue23 "Verify semanage %s -E" % object24 p = Popen(['semanage', object, '-E'], stdout = PIPE)25 out, err = p.communicate()26 self.assertSuccess(p.returncode, err)27 def test_input_output(self):28 print("Verify semanage export -f /tmp/out")29 p = Popen(['semanage', "export", '-f', '/tmp/out'], stdout = PIPE)30 out, err = p.communicate()31 self.assertSuccess(p.returncode, err)32 print("Verify semanage export -S targeted -f -")33 p = Popen(["semanage","export","-S","targeted","-f","-"], stdout = PIPE)34 out, err = p.communicate()35 self.assertSuccess(p.returncode, err)36 print("Verify semanage -S targeted -o -")37 p = Popen(["semanage","-S","targeted","-o","-"], stdout = PIPE)38 out, err = p.communicate()39 self.assertSuccess(p.returncode, err)40 print("Verify semanage import -f /tmp/out")41 p = Popen(['semanage', "import", '-f', '/tmp/out'], stdout = PIPE)42 out, err = p.communicate()43 self.assertSuccess(p.returncode, err)44 print("Verify semanage import -S targeted -f /tmp/out")45 p = Popen(["semanage","import","-S","targeted","-f", "/tmp/out"], stdout = PIPE)46 out, err = p.communicate()47 self.assertSuccess(p.returncode, err)48 print("Verify semanage -S targeted -i /tmp/out")49 p = Popen(["semanage", "-S","targeted","-i", "/tmp/out"], stdout = PIPE)50 out, err = p.communicate()51 self.assertSuccess(p.returncode, err)52 def test_list(self):53 for object in object_list:54 if object in ["dontaudit"]:55 continue56 "Verify semanage %s -l" % object57 p = Popen(['semanage', object, '-l'], stdout = PIPE)58 out, err = p.communicate()59 self.assertSuccess(p.returncode, err)60 def test_list_c(self):61 for object in object_list:62 if object in ["module", "permissive", "dontaudit"]:63 continue64 print("Verify semanage %s -l" % object)65 p = Popen(['semanage', object, '-lC'], stdout = PIPE)66 out, err = p.communicate()67 self.assertSuccess(p.returncode, err)68 def test_fcontext(self):69 p = Popen(["semanage", "fcontext", "-d", "/ha-web(/.*)?" ], stderr = PIPE)70 out, err = p.communicate()71 print("Verify semanage fcontext -a")72 p = Popen(["semanage", "fcontext", "-a", "-t", "httpd_sys_content_t", "/ha-web(/.*)?" ], stdout = PIPE)73 out, err = p.communicate()74 self.assertSuccess(p.returncode, err)75 print("Verify semanage fcontext -m")76 p = Popen(["semanage", "fcontext", "-m", "-t", "default_t", "/ha-web(/.*)?" ], stdout = PIPE)77 out, err = p.communicate()78 self.assertSuccess(p.returncode, err)79 print("Verify semanage fcontext -d")80 p = Popen(["semanage", "fcontext", "-d", "/ha-web(/.*)?" ], stdout = PIPE)81 out, err = p.communicate()82 self.assertSuccess(p.returncode, err)83 def test_fcontext_e(self):84 p = Popen(["semanage", "fcontext", "-d", "/myhome" ], stderr = PIPE)85 out, err = p.communicate()86 p = Popen(["semanage", "fcontext", "-d", "/myhome1" ], stderr = PIPE)87 out, err = p.communicate()88 print("Verify semanage fcontext -a -e")89 p = Popen(["semanage", "fcontext", "-a", "-e", "/home", "/myhome" ], stdout = PIPE)90 out, err = p.communicate()91 self.assertSuccess(p.returncode, err)92 print("Verify semanage fcontext -m -e")93 p = Popen(["semanage", "fcontext", "-a", "-e", "/home", "/myhome1" ], stdout = PIPE)94 out, err = p.communicate()95 self.assertSuccess(p.returncode, err)96 print("Verify semanage fcontext -d -e")97 p = Popen(["semanage", "fcontext", "-d", "/myhome1" ], stdout = PIPE)98 out, err = p.communicate()99 self.assertSuccess(p.returncode, err)100 def test_port(self):101 # Cleanup102 p = Popen(["semanage", "port", "-d", "-p", "tcp", "55" ], stdout = PIPE, stderr = PIPE)103 out, err = p.communicate()104 # test105 print("Verify semanage port -a")106 p = Popen(["semanage", "port", "-a", "-t", "ssh_port_t", "-p", "tcp", "55" ], stdout = PIPE)107 out, err = p.communicate()108 self.assertSuccess(p.returncode, err)109 print("Verify semanage port -m")110 p = Popen(["semanage", "port", "-m", "-t", "http_port_t", "-p", "tcp", "55" ], stdout = PIPE)111 out, err = p.communicate()112 self.assertSuccess(p.returncode, err)113 print("Verify semanage port -d")114 p = Popen(["semanage", "port", "-d", "-p", "tcp", "55" ], stdout = PIPE)115 out, err = p.communicate()116 self.assertSuccess(p.returncode, err)117 def test_login(self):118 # Cleanup119 p = Popen(["userdel", "-f", "-r", "testlogin" ], stderr = PIPE, stdout = PIPE)120 out, err = p.communicate()121 p = Popen(["semanage", "user", "-d", "testuser_u" ], stderr = PIPE, stdout = PIPE)122 out, err = p.communicate()123 p = Popen(["semanage", "login", "-d", "testlogin" ], stderr = PIPE, stdout = PIPE)124 out, err = p.communicate()125 #test126 print("Verify semanage user -a")127 p = Popen(["semanage", "user", "-a", "-R", "staff_r", "-r", "s0-s0:c0.c1023", "testuser_u" ], stdout = PIPE)128 out, err = p.communicate()129 self.assertSuccess(p.returncode, err)130 print("Verify useradd ")131 p = Popen(["useradd", "testlogin" ], stdout = PIPE)132 out, err = p.communicate()133 self.assertSuccess(p.returncode, err)134 print("Verify semanage login -a")135 p = Popen(["semanage", "login", "-a", "-s", "testuser_u", "testlogin" ], stdout = PIPE)136 out, err = p.communicate()137 self.assertSuccess(p.returncode, err)138 print("Verify semanage login -m -r")139 p = Popen(["semanage", "login", "-m", "-r", "s0-s0:c1", "testlogin" ], stdout = PIPE)140 out, err = p.communicate()141 self.assertSuccess(p.returncode, err)142 print("Verify semanage login -m -s")143 p = Popen(["semanage", "login", "-m", "-s", "staff_u", "testlogin" ], stdout = PIPE)144 out, err = p.communicate()145 self.assertSuccess(p.returncode, err)146 print("Verify semanage login -m -s -r")147 p = Popen(["semanage", "login", "-m", "-s", "testuser_u", "-r", "s0", "testlogin" ], stdout = PIPE)148 out, err = p.communicate()149 self.assertSuccess(p.returncode, err)150 print("Verify semanage login -d")151 p = Popen(["semanage", "login", "-d", "testlogin" ], stdout = PIPE)152 out, err = p.communicate()153 print("Verify userdel ")154 p = Popen(["userdel", "-f", "-r", "testlogin" ], stderr = PIPE, stdout = PIPE)155 out, err = p.communicate()156 self.assertSuccess(p.returncode, err)157 print("Verify semanage user -d")158 p = Popen(["semanage", "user", "-d", "testuser_u" ], stdout = PIPE)159 out, err = p.communicate()160 self.assertSuccess(p.returncode, err)161 def test_user(self):162 # Cleanup163 p = Popen(["semanage", "user", "-d", "testuser_u" ], stderr = PIPE, stdout = PIPE)164 out, err = p.communicate()165 # test166 print("Verify semanage user -a")167 p = Popen(["semanage", "user", "-a", "-R", "staff_r", "-r", "s0-s0:c0.c1023", "testuser_u" ], stdout = PIPE)168 out, err = p.communicate()169 self.assertSuccess(p.returncode, err)170 print("Verify semanage user -m -R")171 p = Popen(["semanage", "user", "-m", "-R", "sysadm_r unconfined_r", "testuser_u" ], stdout = PIPE)172 out, err = p.communicate()173 self.assertSuccess(p.returncode, err)174 print("Verify semanage user -m -r")175 p = Popen(["semanage", "user", "-m", "-r", "s0-s0:c1", "testuser_u" ], stdout = PIPE)176 out, err = p.communicate()177 self.assertSuccess(p.returncode, err)178 print("Verify semanage user -d")179 p = Popen(["semanage", "user", "-d", "testuser_u" ], stdout = PIPE)180 out, err = p.communicate()181 self.assertSuccess(p.returncode, err)182 def test_boolean(self):183 import selinux184 boolean_status={0:"--off",1:"--on"}185 boolean_state=selinux.security_get_boolean_active("httpd_anon_write")186 # Test187 print("Verify semanage boolean -m %s httpd_anon_write" % boolean_status[not boolean_state])188 p = Popen(["semanage","boolean","-m",boolean_status[(not boolean_state)],"httpd_anon_write"], stdout = PIPE)189 out, err = p.communicate()190 self.assertSuccess(p.returncode, err)191 print("Verify semanage boolean -m %s httpd_anon_write" % boolean_status[boolean_state])192 p = Popen(["semanage","boolean","-m",boolean_status[boolean_state],"httpd_anon_write"], stdout = PIPE)193 out, err = p.communicate()194 self.assertSuccess(p.returncode, err)195def semanage_suite():196 semanage_suite = unittest.TestSuite()197 semanage_suite.addTest(unittest.makeSuite(SemanageTests))198 return semanage_suite199def semanage_custom_suite(test_list):200 suiteSemanage=unittest.TestSuite()201 for t in test_list:202 suiteSemanage.addTest(SemanageTests(t))203 return suiteSemanage204def semanage_run_test(suite):205 unittest.TextTestRunner(verbosity=2).run(suite)206class CheckTest(argparse.Action):207 def __call__(self, parser, namespace, values, option_string=None):208 newval = getattr(namespace, self.dest)...
test_sepolicy.py
Source:test_sepolicy.py
...10 '"not found" not found in %r' % err)11 def assertFailure(self, status):12 self.assert_(status != 0,13 '"Succeeded when it should have failed')14 def assertSuccess(self, status, err):15 self.assert_(status == 0,16 '"sepolicy should have succeeded for this test %r' % err)17 def test_man_domain(self):18 "Verify sepolicy manpage -d works"19 p = Popen(['sepolicy', 'manpage', '-d', 'httpd_t'], stdout = PIPE)20 out, err = p.communicate()21 print out, err22 self.assertSuccess(p.returncode, err)23 def test_man_all(self):24 "Verify sepolicy manpage -a works"25 p = Popen(['sepolicy', 'manpage', '-a'], stdout = PIPE)26 out, err = p.communicate()27 self.assertSuccess(p.returncode, err)28 def test_network_l(self):29 "Verify sepolicy network -l works"30 p = Popen(['sepolicy', 'network', '-l'], stdout = PIPE)31 out, err = p.communicate()32 self.assertSuccess(p.returncode, err)33 def test_network_t(self):34 "Verify sepolicy network -t works"35 p = Popen(['sepolicy', 'network', '-t', 'http_port_t'], stdout = PIPE)36 out, err = p.communicate()37 self.assertSuccess(p.returncode, err)38 def test_network_p(self):39 "Verify sepolicy network -p works"40 p = Popen(['sepolicy', 'network', '-p', '80'], stdout = PIPE)41 out, err = p.communicate()42 self.assertSuccess(p.returncode, err)43 def test_network_d(self):44 "Verify sepolicy network -d works"45 p = Popen(['sepolicy', 'network', '-d', 'httpd_t'], stdout = PIPE)46 out, err = p.communicate()47 self.assertSuccess(p.returncode, err)48 def test_transition_s(self):49 "Verify sepolicy transition -l works"50 p = Popen(['sepolicy', 'transition', '-s', 'httpd_t'], stdout = PIPE)51 out, err = p.communicate()52 self.assertSuccess(p.returncode, err)53 def test_transition_t(self):54 "Verify sepolicy transition -t works"55 p = Popen(['sepolicy', 'transition', '-s', 'httpd_t', '-t', 'sendmail_t'], stdout = PIPE)56 out, err = p.communicate()57 self.assertSuccess(p.returncode, err)58 def test_booleans_a(self):59 "Verify sepolicy booleans -a works"60 p = Popen(['sepolicy', 'booleans', '-a'], stdout = PIPE)61 out, err = p.communicate()62 self.assertSuccess(p.returncode, err)63 def test_booleans_b_alias(self):64 "Verify sepolicy booleans -b works"65 p = Popen(['sepolicy', 'booleans', '-b', 'allow_ypbind'], stdout = PIPE)66 out, err = p.communicate()67 self.assertSuccess(p.returncode, err)68 def test_booleans_b(self):69 "Verify sepolicy booleans -b works"70 p = Popen(['sepolicy', 'booleans', '-b', 'nis_enabled'], stdout = PIPE)71 out, err = p.communicate()72 self.assertSuccess(p.returncode, err)73 def test_interface_l(self):74 "Verify sepolicy interface -l works"75 p = Popen(['sepolicy', 'interface', '-l'], stdout = PIPE)76 out, err = p.communicate()77 self.assertSuccess(p.returncode, err)78 def test_interface_a(self):79 "Verify sepolicy interface -a works"80 p = Popen(['sepolicy', 'interface', '-a'], stdout = PIPE)81 out, err = p.communicate()82 self.assertSuccess(p.returncode, err)83 def test_interface_p(self):84 "Verify sepolicy interface -u works"85 p = Popen(['sepolicy', 'interface', '-u'], stdout = PIPE)86 out, err = p.communicate()87 self.assertSuccess(p.returncode, err)88 def test_interface_ci(self):89 "Verify sepolicy interface -c -i works"90 p = Popen(['sepolicy', 'interface', '-c', '-i', 'apache_admin'], stdout = PIPE)91 out, err = p.communicate()92 self.assertSuccess(p.returncode, err)93if __name__ == "__main__":94 import selinux95 if selinux.security_getenforce() == 1:96 unittest.main()97 else:...
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!!