def protocol(self, value): try: if value.lower() in self.VALID_PROTOCOLS: self._protocol = value.lower() else: raise Exception() except: ## XXX: throw a proper ITT exception? msg = "Invalid protocol" log.error(msg) raise Exception(msg)
def get_test_client(self, test): if test.connection.protocol == "http": client = itt.HttpClient(test) elif test.connection.protocol == "ftp": client = itt.FtpClient(test) elif test.connection.protocol == "tftp": client = itt.TftpClient(test) else: msg = "Invalid protocol" log.error(msg) raise Exception(msg) return client
def _start_daemon(self): """Start the daemon process. ..note:: Daemon will only start if PID file exists (not ``None``). The :meth:`start` method checks for an existing PID before preparing the daemon environment. Finally, it will initiate the daemon process run sequence. **Returns:** boolean:: ``True`` -- success ``False`` -- failure **Raises:** :mod:`itt.utils.daemon.DaemonError`, if: * PID file has not been defined * PID file is not writable """ start_status = False # If we have got this far, check that we have a valid PID file. if self.pidfile is None: raise DaemonError('PID file has not been defined') self._debug('checking daemon status with PID: %s' % self.pid) if self.pid is not None: msg = ('PID file "%s" exists. Daemon may be running?\n' % self.pidfile) log.error(msg) else: # Check if PID file is writable to save hassles later on. self._debug('No PID file -- creating handle') try: self.pidfs = is_writable(self.pidfile) self._debug('starting daemon') self.daemonize() self._start_server(self.exit_event) start_status = True except IOError as err: err_msg = 'Cannot write to PID file: IOError "%s"' % err raise DaemonError(err_msg) return start_status
def download(self): try: log.info("HTTP client begins download (HTTP GET):") bytes_qs = "" if self.test.content.static: url_path = self.test.content.filename log.info(" content: %s" % url_path) else: url_path = "/testing/dev/random" log.info(" content : (random data)") log.info(" size : %s bytes" % self.test.content.bytes) bytes_qs = "&bytes=%s" % ( self.test.content.bytes, ) min_gap = self.getGap() chunk = self.getChunk() generated_url = urlparse.urljoin( "http://%s" % self.test.connection.netloc, "%s?minimum_gap=%s&chunk_size=%s%s" % ( url_path, min_gap, chunk, bytes_qs, ## May be set to "&bytes=x" if random data ), ) log.info(" url : %s" % generated_url) download = requests.get(generated_url) shasum = hashlib.sha1(download.content).hexdigest() log.info("HTTP client finishes download (HTTP GET):") log.info(" url : %s" % generated_url) log.info(" received : %s bytes" % len(download.content)) log.info(" sha1_sum : %s" % shasum) log.info(" http_response : %s" % (download.status_code)) except requests.ConnectionError, e: log.error("Download (HTTP GET) failed with a connection error: %s" % (str(e)))
def upload(self): HTTP_DEVNULL = "/testing/dev/null" ## Size of data bytes = int(0) try: log.info("HTTP client begins upload (HTTP POST):") generated_url = "http://%s/%s" % ( self.test.connection.netloc, HTTP_DEVNULL, ) log.info(" url : %s" % generated_url) if self.test.content.static: log.info(" content : %s" % self.test.content.filename) else: log.info(" content : (random data)") bytes = self.test.content.bytes log.info(" size : %s bytes" % bytes) min_gap = self.getGap() chunk = self.getChunk() ## Open the file for sending self.test.content.open() ## Open HTTP connection & send headers http = self.setupUploadConnection(HTTP_DEVNULL) ## Send data i = int(0) while i < bytes: if chunk > 0: if (bytes - i) < chunk: bytes_to_send = (bytes - i) else: bytes_to_send = chunk ## Send only as much as we're allowed data = self.test.content.read(bytes=bytes_to_send) if data == "": ## File was shorter than we thought? break self.addAndSend(http, data) i = i + bytes_to_send ## Sleep for the minimum gap size if i < bytes: time.sleep(min_gap) else: ## Send everything as fast as possible self.addAndSend(http, self.test.content.read()) i = i + bytes ## Close the file self.test.content.close() ## XXX: todo: generalise the sha1sum stuff shasum = hashlib.sha1(self.sent_data).hexdigest() log.info("HTTP client finishes upload (HTTP POST):") log.info(" url : %s" % generated_url) log.info(" sent : %s bytes" % len(self.sent_data)) log.info(" sha1_sum : %s" % shasum) ## XXX: todo: if response ==200, else: error response = http.getresponse() log.info(" http_response : %s" % ( response.status, )) except (socket.error, httplib.HTTPException), e: log.error("Upload (HTTP POST) failed with an error: %s" % (str(e))) return
def parse_args(self): ONE_MEGABYTE = 1048576 parser = optparse.OptionParser( add_help_option=True, usage='%prog [options]', prog='itt.IttClient', ) parser.add_option( '-D', '--direction', default='foo', help='Direction of test (upload [u, up], or, download [d, down])', ) parser.add_option( '-H', '--host', default='127.0.0.1', help='Host to connect to (either DNS name, or IP address)', ) parser.add_option( '-p', '--port', help='Port number to connect to on the host', ) parser.add_option( '-P', '--protocol', help='Protocol to use to transfer the content (HTTP, FTP, or TFTP)', ) parser.add_option( '-s', '--size', default=ONE_MEGABYTE, help= 'Size of the content to be sent in bytes (applicable to random content only) [default: %default]', ) parser.add_option( '-f', '--filename', default=None, help= 'File to upload or download (optional: if omitted, then random data will be used)', ) parser.add_option( '-g', '--minimum_gap', default=None, help='Minium gap between chunks (given in seconds)', ) parser.add_option( '-S', '--chunk_size', default=None, help= 'Size of data (in bytes) to be sent in a "chunk" (i.e. with no deliberate pauses)', ) (self.opts, args) = parser.parse_args() if len(args) != 0: log.warning( "ITT client takes no arguments, ignoring the ones provided") if self.opts.direction.lower() == 'u' or \ self.opts.direction.lower() == 'up' or \ self.opts.direction.lower() == 'upload': self.upload = True elif self.opts.direction.lower() == 'd' or \ self.opts.direction.lower() == 'down' or \ self.opts.direction.lower() == 'download': self.upload = False else: ## XXX: Throw an ITT exception properly msg = "ITT client needs to be told to do an upload or a download" log.error(msg) raise Exception(msg) if self.opts.port is None: ## XXX: Throw an ITT exception properly msg = "ITT client needs to be told a port number" log.error(msg) raise Exception(msg) if self.opts.filename is not None: ## Size doesn't matter if filename is specified self.opts.size = None
def test_log(self): """Test the log facility. """ log.debug("debug message") log.error("error message")
def logAndThrow(self, message): ## XXX: throw a proper ITT exception? log.error(message) raise Exception(message)