def test_install(self): self.common_code() # record self.host.run.expect_call('dpkg -i "%s"' % (utils.sh_escape(self.remote_filename))) result = common_utils.CmdResult() result.stdout = "1" utils.run.expect_call( 'dpkg-deb -f "%s" version' % utils.sh_escape(self.kernel.source_material)).and_return(result) utils.run.expect_call( 'dpkg-deb -f "%s" version' % utils.sh_escape(self.kernel.source_material)).and_return(result) self.host.run.expect_call('mkinitramfs -o "/boot/initrd.img-1" "1"') utils.run.expect_call( 'dpkg-deb -f "%s" version' % utils.sh_escape(self.kernel.source_material)).and_return(result) utils.run.expect_call( 'dpkg-deb -f "%s" version' % utils.sh_escape(self.kernel.source_material)).and_return(result) self.host.bootloader.add_kernel.expect_call( '/boot/vmlinuz-1', initrd='/boot/initrd.img-1') # run and check self.kernel.install(self.host) self.god.check_playback()
def test_install(self): host = self.god.create_mock_class(hosts.RemoteHost, "host") host.bootloader = self.god.create_mock_class(bootloader.Bootloader, "bootloader") self.god.stub_function(self.kernel, "get_image_name") self.god.stub_function(self.kernel, "get_vmlinux_name") rpm = self.kernel.source_material rpm_package = "package.rpm" # record remote_tmpdir = 'remote_tmp_dir' host.get_tmp_dir.expect_call().and_return(remote_tmpdir) remote_rpm = os.path.join(remote_tmpdir, os.path.basename(rpm)) result = common_utils.CmdResult() result.exit_status = 0 result.stdout = rpm_package utils.run.expect_call('/usr/bin/rpm -q -p %s' % rpm).and_return(result) self.kernel.get_image_name.expect_call().and_return("vmlinuz") host.send_file.expect_call(rpm, remote_rpm) host.run.expect_call('rpm -e ' + rpm_package, ignore_status=True) host.run.expect_call('rpm --force -i ' + remote_rpm) self.kernel.get_vmlinux_name.expect_call().and_return("/boot/vmlinux") host.run.expect_call('cd /;rpm2cpio %s | cpio -imuv ./boot/vmlinux' % remote_rpm) host.run.expect_call('ls /boot/vmlinux') host.bootloader.remove_kernel.expect_call('autotest') host.bootloader.add_kernel.expect_call("vmlinuz", 'autotest', args='', default=False) host.bootloader.boot_once.expect_call('autotest') # run and test self.kernel.install(host) self.god.check_playback()
def test_get_initrd_name(self): # record result = common_utils.CmdResult() result.exit_status = 0 utils.run.expect_call('rpm -q -l -p %s | grep /boot/initrd' % "source.rpm", ignore_status=True).and_return(result) # run and test self.kernel.get_initrd_name() self.god.check_playback()
def test_get_vmlinux_name(self): # record result = common_utils.CmdResult() result.exit_status = 0 result.stdout = "vmlinuz" utils.run.expect_call( 'rpm -q -l -p source.rpm | grep /boot/vmlinux').and_return(result) # run and test self.assertEquals(self.kernel.get_vmlinux_name(), result.stdout) self.god.check_playback()
def test_get_version(self): # record result = common_utils.CmdResult() result.exit_status = 0 result.stdout = "image" cmd = ('rpm -qpi %s | grep Version | awk \'{print($3);}\'' % (utils.sh_escape("source.rpm"))) utils.run.expect_call(cmd).and_return(result) # run and test self.assertEquals(self.kernel.get_version(), result.stdout) self.god.check_playback()
def _init_transport(self): for path, key in self.keys.iteritems(): try: logging.debug("Connecting with %s", path) transport = self._connect_transport(key) transport.set_keepalive(self.KEEPALIVE_TIMEOUT_SECONDS) self.transport = transport self.pid = os.getpid() return except paramiko.AuthenticationException: logging.debug("Authentication failure") else: raise error.AutoservSshPermissionDeniedError( "Permission denied using all keys available to ParamikoHost", utils.CmdResult())
def test_check_diskspace(self): self.god.stub_function(base_classes.Host, 'run') host = base_classes.Host() host.hostname = 'unittest-host' test_df_tail = ('/dev/sda1 1061 939' ' 123 89% /') fake_cmd_status = utils.CmdResult(exit_status=0, stdout=test_df_tail) host.run.expect_call('df -PB 1000000 /foo | tail -1').and_return( fake_cmd_status) self.assertRaises(error.AutoservDiskFullHostError, host.check_diskspace, '/foo', 0.2) host.run.expect_call('df -PB 1000000 /foo | tail -1').and_return( fake_cmd_status) host.check_diskspace('/foo', 0.1) self.god.check_playback()
def test_execute_calls_impl(self): self.god.stub_with(drones._RemoteDrone, '_drone_utility_path', 'mock-drone-utility-path') drones.drone_utility.create_host.expect_call('fakehost').and_return( self._mock_host) self._mock_host.is_up.expect_call().and_return(True) mock_calls = ('foo',) mock_result = utils.CmdResult(stdout=cPickle.dumps('mock return')) self._mock_host.run.expect_call( 'python mock-drone-utility-path', stdin=cPickle.dumps(mock_calls), stdout_tee=None, connect_timeout=mock.is_instance_comparator(int)).and_return( mock_result) drone = drones._RemoteDrone('fakehost') self.assertEqual('mock return', drone._execute_calls_impl(mock_calls)) self.god.check_playback()
timed_out = True break stdin = self.__send_stdin(channel, stdin) time.sleep(1) if timed_out: exit_status = -signal.SIGTERM else: exit_status = channel.recv_exit_status() channel.settimeout(10) self._exhaust_stream(stdout, raw_stdout, channel.recv) self._exhaust_stream(stderr, raw_stderr, channel.recv_stderr) channel.close() duration = time.time() - start_time # create the appropriate results stdout = "".join(raw_stdout) stderr = "".join(raw_stderr) result = utils.CmdResult(command, stdout, stderr, exit_status, duration) if exit_status == -signal.SIGHUP: msg = "ssh connection unexpectedly terminated" raise error.AutoservRunError(msg, result) if timed_out: logging.warn('Paramiko command timed out after %s sec: %s', timeout, command) raise error.AutoservRunError("command timed out", result) if not ignore_status and exit_status: raise error.AutoservRunError(command, result) return result
def run(self, command, timeout=3600, ignore_status=False, stdout_tee=utils.TEE_TO_LOGS, stderr_tee=utils.TEE_TO_LOGS, connect_timeout=30, stdin=None, verbose=True, args=()): """ Run a command on the remote host. @see shared.hosts.host.run() :param connect_timeout: connection timeout (in seconds) :param options: string with additional ssh command options :param verbose: log the commands :raise AutoservRunError: if the command failed :raise AutoservSSHTimeout: ssh connection has timed out """ stdout = utils.get_stream_tee_file(stdout_tee, utils.DEFAULT_STDOUT_LEVEL, prefix=utils.STDOUT_PREFIX) stderr = utils.get_stream_tee_file( stderr_tee, utils.get_stderr_level(ignore_status), prefix=utils.STDERR_PREFIX) for arg in args: command += ' "%s"' % utils.sh_escape(arg) if verbose: logging.debug("Running (ssh-paramiko) '%s'" % command) # start up the command start_time = time.time() try: channel = self._open_channel(timeout) channel.exec_command(command) except (socket.error, paramiko.SSHException, EOFError) as e: # This has to match the string from paramiko *exactly*. if str(e) != 'Channel closed.': raise error.AutoservSSHTimeout("ssh failed: %s" % e) # pull in all the stdout, stderr until the command terminates raw_stdout, raw_stderr = [], [] timed_out = False while not channel.exit_status_ready(): if channel.recv_ready(): raw_stdout.append(channel.recv(self.BUFFSIZE)) stdout.write(raw_stdout[-1]) if channel.recv_stderr_ready(): raw_stderr.append(channel.recv_stderr(self.BUFFSIZE)) stderr.write(raw_stderr[-1]) if timeout and time.time() - start_time > timeout: timed_out = True break stdin = self.__send_stdin(channel, stdin) time.sleep(1) if timed_out: exit_status = -signal.SIGTERM else: exit_status = channel.recv_exit_status() channel.settimeout(10) self._exhaust_stream(stdout, raw_stdout, channel.recv) self._exhaust_stream(stderr, raw_stderr, channel.recv_stderr) channel.close() duration = time.time() - start_time # create the appropriate results stdout = "".join(raw_stdout) stderr = "".join(raw_stderr) result = utils.CmdResult(command, stdout, stderr, exit_status, duration) if exit_status == -signal.SIGHUP: msg = "ssh connection unexpectedly terminated" raise error.AutoservRunError(msg, result) if timed_out: logging.warn('Paramiko command timed out after %s sec: %s', timeout, command) raise error.AutoservRunError("command timed out", result) if not ignore_status and exit_status: raise error.AutoservRunError(command, result) return result