def get_source_of_symbolic_link(symbolic): state = os.lstat(symbolic) if stat.S_ISLNK(state.st_mode): dest = os.popen("ls -l %s" % symbolic).readline().strip().split()[-1] return dest else: fail("File is not a symbolic link")
def check_response_code(url, code, timeout=10): try: resp = get_response_code(url, timeout=timeout) except: return False if resp != code: fail("The URL '%s' is reachable but returns HTTP response code of '%s' while I expected '%s'" % (url, resp, code))
def check_running(process_name="apache2"): ''' CHECK if process (default=apache2) is not running ''' if not gurumate.base.get_pid_list(process_name): fail("Apache process '%s' doesn't seem to be working" % process_name) return False #unreachable return True
def check_listening_port(port=80, process_name="apache2"): ''' CHECK if process (default=apache2) is running and listening on port 80 ''' if port not in get_listening_ports(process_name): fail("Apache is not listening on port %s, \ maybe you forgot to configure the port? \ or did you forget to start the service?" % port)
def check_isdir(dirpath): try: st = os.stat(dirpath) except OSError: fail("no directory named (%s) in the system" % dirpath) if not stat.S_ISDIR(st.st_mode): fail("%s is not a directory." % dirpath)
def check_isfile(filepath): try: st = os.stat(filepath) except OSError: fail("no file named (%s) in the system" % filepath) if not stat.S_ISREG(st.st_mode): fail("%s is not a regular file." % filepath)
def check_response_code(url, code, timeout=10): try: resp = get_response_code(url, timeout=timeout) except: return False if resp != code: fail( "The URL '%s' is reachable but returns HTTP response code of '%s' while I expected '%s'" % (url, resp, code))
def check_url_accessibility(url, timeout=10): ''' Check whether the URL accessible and returns HTTP 200 OK or not if not raises ValidationError ''' if(url=='localhost'): url = 'http://127.0.0.1' try: req = urllib2.urlopen(url, timeout=timeout) if (req.getcode()==200): return True except Exception: pass fail("URL '%s' is not accessible from this machine" % url)
def check_url_accessibility(url, timeout=10): ''' Check whether the URL accessible and returns HTTP 200 OK or not if not raises ValidationError ''' if (url == 'localhost'): url = 'http://127.0.0.1' try: req = urllib2.urlopen(url, timeout=timeout) if (req.getcode() == 200): return True except Exception: pass fail("URL '%s' is not accessible from this machine" % url)
def check_writeaccess(filepath, user_name): try: st = os.stat(filepath) except OSError: fail("no file name (%s) in the system" % filepath) mode = st.st_mode # Others if mode & stat.S_IWOTH: return try: passdb_entry = pwd.getpwnam(user_name) except KeyError: fail("no user called (%s) on the system" % user_name) user_id = passdb_entry.pw_uid group_id = passdb_entry.pw_gid owner_uid = st.st_uid owner_gid = st.st_gid if owner_gid == group_id: if mode & stat.S_IWGRP: return if owner_uid == user_id: if mode & stat.S_IWUSR: return fail("user (%s) has no write access on file (%s)" % (user_name, filepath))
rep = req.read() if (not case_sensitive and rep.lower().find(contents.lower()) >= 0) or (case_sensitive and rep.find(contents) >= 0): return True else: return False def get_response_code(url, timeout=10): ''' Visit the URL and return the HTTP response code in 'int' ''' try: req = urllib2.urlopen(url, timeout=timeout) except HTTPError, e: return e.getcode() except Exception, _: fail("Couldn't reach the URL '%s'" % url) else: return req.getcode() def check_response_code(url, code, timeout=10): try: resp = get_response_code(url, timeout=timeout) except: return False if resp != code: fail("The URL '%s' is reachable but returns HTTP response code of '%s' while I expected '%s'" % (url, resp, code)) def compare_content_type(url, content_type): ''' Compare the content type header of url param with content_type param and returns boolean
) or (case_sensitive and rep.find(contents) >= 0): return True else: return False def get_response_code(url, timeout=10): ''' Visit the URL and return the HTTP response code in 'int' ''' try: req = urllib2.urlopen(url, timeout=timeout) except HTTPError, e: return e.getcode() except Exception, _: fail("Couldn't reach the URL '%s'" % url) else: return req.getcode() def check_response_code(url, code, timeout=10): try: resp = get_response_code(url, timeout=timeout) except: return False if resp != code: fail( "The URL '%s' is reachable but returns HTTP response code of '%s' while I expected '%s'" % (url, resp, code))