Best Python code snippet using autotest_python
test_cassandra.py
Source:test_cassandra.py
...1010 self.assertEqual(write_file.call_count, 2)1011 new_config = write_file.call_args[0][1]1012 self.assertEqual(yaml.safe_load(new_config)['partitioner'],1013 'overridden_partitioner')1014 def test_get_pid_from_file(self):1015 with tempfile.NamedTemporaryFile('w') as pid_file:1016 pid_file.write(' 42\t')1017 pid_file.flush()1018 self.assertEqual(cassandra.get_pid_from_file(pid_file.name), 42)1019 pid_file.write('\nSome Noise')1020 pid_file.flush()1021 self.assertEqual(cassandra.get_pid_from_file(pid_file.name), 42)1022 for invalid_pid in ['-1', '0', 'fred']:1023 with self.subTest(invalid_pid=invalid_pid):1024 with tempfile.NamedTemporaryFile('w') as pid_file:1025 pid_file.write(invalid_pid)1026 pid_file.flush()1027 self.assertRaises(ValueError,1028 cassandra.get_pid_from_file, pid_file.name)1029 with tempfile.TemporaryDirectory() as tmpdir:1030 self.assertRaises(OSError, cassandra.get_pid_from_file,1031 os.path.join(tmpdir, 'invalid.pid'))1032 @patch('cassandra.get_cassandra_pid_file')1033 def test_is_cassandra_running_not_running(self, get_pid_file):1034 # When Cassandra is not running, the pidfile does not exist.1035 get_pid_file.return_value = 'does not exist'...
daemon.py
Source:daemon.py
...83 # Write the pidfile.84 atexit.register(self.delpid)85 pid = str(os.getpid())86 open(self.pidfile, 'w+').write("%s\n" % pid)87 def get_pid_from_file(self):88 "Returns the pid value from the file."89 try:90 pf = open(self.pidfile, 'r')91 pid = int(pf.read().strip())92 pf.close()93 except IOError:94 pid = None95 return pid96 def delpid(self):97 os.remove(self.pidfile)98 def start(self, debug=False):99 """100 Starts the daemon.101 """102 # Check for a pidfile to see whether the daemon is already running.103 if self.get_pid_from_file():104 sys.stderr.write("pidfile %s already exists. Is the daemon already running?\n" % self.pidfile)105 sys.exit(1)106 # Don't detach from foreground if debugging.107 if not debug:108 self.daemonize()109 # Start the daemon.110 self.run()111 def stop(self):112 """113 Stops the daemon.114 """115 # Get the pid from the pidfile.116 pid = self.get_pid_from_file()117 if not pid:118 sys.stderr.write("pidfile %s does not exist. Is the daemon not running?\n" % self.pidfile)119 return # not an error in a restart120 # Try killing the daemon process.121 try:122 while 1:123 os.kill(pid, SIGTERM)124 time.sleep(0.1)125 except OSError, err:126 err = str(err)127 if err.find("No such process") > 0:128 os.remove(self.pidfile)129 else:130 print str(err)...
process.py
Source:process.py
1import os2import psutil3class process:4 # Return a PID contained in a given file5 def get_pid_from_file(self, pid_file):6 if(os.path.isfile(pid_file)):7 with open(pid_file, 'r') as file:8 pid = file.read()9 10 return int(pid)11 else:12 return 013 14 # Kill PID contained in a given file15 def kill_pid_file(self, pid_file):16 if(self.pid_file_running(pid_file)):17 pid = self.get_pid_from_file(pid_file)18 try: 19 os.kill(pid, 15)20 return True21 22 except OSError:23 return False24 25 # Is PID in a given file running 26 def pid_file_running(self, pid_file):27 pid = self.get_pid_from_file(pid_file)28 29 if(pid == 0):30 return False31 32 if(self.pid_is_running(pid)):33 return True34 else:35 os.remove(pid_file) 36 return False37 # Is a PID running38 def pid_is_running(self,pid):39 try:40 os.kill(pid, 0)41 return True...
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!!