def start_server(hostname):
    """ Start RJE server with DMZ host `hostname` using 'LocalHost'. """
    for name in (_DMZ_ROOT, _RJE_ROOT):
        if os.path.exists(name):
            shutil.rmtree(name)
        os.mkdir(name)

    # Configure ssh to use special test code and DMZ root.
    ssh = ("python", os.path.join(_TST_ROOT, "ssh.py"), os.path.join("..", _DMZ_ROOT))

    # Configure scp to use special test code and DMZ root, and special RJE root.
    scp = ("python", os.path.join(_TST_ROOT, "scp.py"), os.path.join("..", _DMZ_ROOT), "--rje")

    orig_dir = os.getcwd()
    os.chdir(_RJE_ROOT)
    try:
        root = protocol._server_root()
        args = (
            "python",
            "-m",
            "nas_access.rje",
            "--resources",
            "",
            "--allocator",
            "LocalHost",
            "--dmz-host",
            hostname,
            "--poll-delay",
            "1",
            "--ssh",
            " ".join(ssh),
            "--scp",
            " ".join(scp),
        )
        out = open("rje.stdout", "w")
        proc = subprocess.Popen(args, stdout=out, stderr=subprocess.STDOUT)
    finally:
        os.chdir(orig_dir)

    heartbeat = "%s=%s" % (os.path.join(_DMZ_ROOT, root), "heartbeat")
    for retry in range(20):
        time.sleep(0.5)
        if os.path.exists(heartbeat) and os.path.getsize(heartbeat) > 0:
            return proc
    raise RuntimeError("server startup timeout")
def start_server(hostname):
    """ Start RJE server with DMZ host `hostname` using 'LocalHost'. """
    for name in (_DMZ_ROOT, _RJE_ROOT):
        if os.path.exists(name):
            shutil.rmtree(name)
        os.mkdir(name)

    # Configure ssh to use special test code and DMZ root.
    ssh = ('python', os.path.join(_TST_ROOT, 'ssh.py'),
                     os.path.join('..', _DMZ_ROOT))

    # Configure scp to use special test code and DMZ root, and special RJE root.
    scp = ('python', os.path.join(_TST_ROOT, 'scp.py'),
                     os.path.join('..', _DMZ_ROOT), '--rje')

    orig_dir = os.getcwd()
    os.chdir(_RJE_ROOT)
    try:
        root = protocol._server_root()
        args = ('python', '-m', 'nas_access.rje',
                '--resources', '',
                '--allocator', 'LocalHost',
                '--dmz-host', hostname,
                '--poll-delay', '1',
                '--ssh', ' '.join(ssh),
                '--scp', ' '.join(scp))
        out = open('rje.stdout', 'w')
        proc = subprocess.Popen(args, stdout=out, stderr=subprocess.STDOUT)
    finally:
        os.chdir(orig_dir)

    heartbeat = '%s=%s' % (os.path.join(_DMZ_ROOT, root), 'heartbeat')
    for retry in range(20):
        time.sleep(0.5)
        if os.path.exists(heartbeat) and os.path.getsize(heartbeat) > 0:
            return proc
    raise RuntimeError('server startup timeout')
Exemple #3
0
def start_server(hostname):
    """ Start RJE server with DMZ host `hostname` using 'LocalHost'. """
    for name in (_DMZ_ROOT, _RJE_ROOT):
        if os.path.exists(name):
            shutil.rmtree(name)
        os.mkdir(name)

    # Configure ssh to use special test code and DMZ root.
    ssh = ('python', os.path.join(_TST_ROOT, 'ssh.py'),
                     os.path.join('..', _DMZ_ROOT))

    # Configure scp to use special test code and DMZ root, and special RJE root.
    scp = ('python', os.path.join(_TST_ROOT, 'scp.py'),
                     os.path.join('..', _DMZ_ROOT), '--rje')

    orig_dir = os.getcwd()
    os.chdir(_RJE_ROOT)
    try:
        root = protocol._server_root()
        args = ('python', '-m', 'nas_access.rje',
                '--resources', '',
                '--allocator', 'LocalHost',
                '--dmz-host', hostname,
                '--poll-delay', '1',
                '--ssh', ' '.join(ssh),
                '--scp', ' '.join(scp))
        out = open('rje.stdout', 'w')
        proc = subprocess.Popen(args, stdout=out, stderr=subprocess.STDOUT)
    finally:
        os.chdir(orig_dir)

    heartbeat = '%s=%s' % (os.path.join(_DMZ_ROOT, root), 'heartbeat')
    for retry in range(20):
        time.sleep(0.5)
        if os.path.exists(heartbeat) and os.path.getsize(heartbeat) > 0:
            return proc
    raise RuntimeError('server startup timeout')
    def test_errors(self):
        logging.debug("")
        logging.debug("test_errors")

        logging.debug("allocate server")
        server, server_info = RAM.allocate(dict(allocator=self.allocator.name))
        try:
            logging.debug("execute bad command")
            code = "server.execute_command(dict(remote_command='no-such-command'))"
            if sys.platform == "win32":
                msg = "WindowsError: [Error 2] The system cannot find the file specified"
            else:
                msg = "OSError: [Errno 2] No such file or directory"
            try:
                server.execute_command(dict(remote_command="no-such-command"))
            except protocol.RemoteError as exc:
                exc_msg = str(exc)
                if msg not in exc_msg:
                    self.fail("%s not in %s" % (msg, exc_msg))
            else:
                self.fail("Expecting protocol.RemoteError")

            logging.debug("open bad file")
            msg = "Can\\'t open \\'../../illegal-access\\', not within root"
            msg = "RuntimeError: %s" % msg
            try:
                server.open("../../illegal-access", "r")
            except protocol.RemoteError as exc:
                exc_msg = str(exc)
                if msg not in exc_msg:
                    self.fail("%s not in %s" % (msg, exc_msg))
            else:
                self.fail("Expecting protocol.RemoteError")

            logging.debug("open missing file")
            msg = "[Errno 2] No such file or directory: \\'no-such-file\\'"
            msg = "IOError: %s" % msg
            try:
                server.open("no-such-file", "r")
            except protocol.RemoteError as exc:
                exc_msg = str(exc)
                if msg not in exc_msg:
                    self.fail("%s not in %s" % (msg, exc_msg))
            else:
                self.fail("Expecting protocol.RemoteError")
        finally:
            logging.debug("release")
            RAM.release(server)

        # Test for exited or never started server.
        logging.debug("dead server")
        self.proc.terminate()
        self.proc = None
        time.sleep(2)
        hostname = socket.gethostname()
        if sys.platform == "win32":  # Server doesn't clean up.
            root = protocol._server_root(hostname)
            mapped_root = os.path.join(_DMZ_ROOT, protocol._map_dir(root))
            for name in glob.glob("%s*" % mapped_root):
                os.remove(name)
        code = "NAS_Allocator(dmz_host=hostname, server_host=hostname)"
        assert_raises(
            self,
            code,
            globals(),
            locals(),
            RuntimeError,
            "NAS_Allocator: can't connect: server root 'RJE-%s='" " on '%s' not found" % (hostname, hostname),
        )

        # Test for missing heartbeat.
        logging.debug("no heartbeat")
        with open(os.path.join(_DMZ_ROOT, "RJE-%s=" % hostname), "w") as out:
            out.write("empty\n")
        try:
            NAS_Allocator(dmz_host=hostname, server_host=hostname)
        except RuntimeError as exc:
            msg = "IOError: [Errno 2] No such file or directory:" " 'RJE-%s=heartbeat'\n" % hostname
            logging.debug(str(exc))
            self.assertTrue(str(exc).endswith(msg))
        else:
            self.fail("Expected RuntimeError")

        # Test for stale heartbeat.
        logging.debug("stale heartbeat")
        protocol.server_heartbeat(hostname, 1, logging.getLogger())
        time.sleep(5)
        assert_raises(
            self,
            code,
            globals(),
            locals(),
            RuntimeError,
            "NAS_Allocator: can't connect: server heartbeat" " hasn't been updated in 0:00:0",
        )
    def test_errors(self):
        logging.debug('')
        logging.debug('test_errors')

        logging.debug('allocate server')
        server, server_info = RAM.allocate(dict(allocator=self.allocator.name))
        try:
            logging.debug('execute bad command')
            code = "server.execute_command(dict(remote_command='no-such-command'))"
            if sys.platform == 'win32':
                msg = "WindowsError('[Error 2] The system cannot find the file specified')"
            else:
                msg = "OSError('[Errno 2] No such file or directory')"
            assert_raises(self, code, globals(), locals(),
                          protocol.RemoteError, msg)

            logging.debug('open bad file')
            msg = "Can't open '../../illegal-access', not within root"
            msg = 'RuntimeError("%s' % msg
            assert_raises(self, "server.open('../../illegal-access', 'r')",
                          globals(), locals(), protocol.RemoteError, msg)

            logging.debug('open missing file')
            msg = "[Errno 2] No such file or directory: 'no-such-file'"
            msg = 'IOError("%s' % msg
            assert_raises(self, "server.open('no-such-file', 'r')",
                          globals(), locals(), protocol.RemoteError, msg)
        finally:
            logging.debug('release')
            RAM.release(server)

        # Test for exited or never started server.
        logging.debug('dead server')
        self.proc.terminate()
        self.proc = None
        time.sleep(2)
        hostname = socket.gethostname()
        if sys.platform == 'win32':  # Server doesn't clean up.
            root = protocol._server_root(hostname)
            mapped_root = os.path.join(_DMZ_ROOT, protocol._map_dir(root))
            for name in glob.glob('%s*' % mapped_root):
                os.remove(name)
        code = 'NAS_Allocator(dmz_host=hostname, server_host=hostname)'
        assert_raises(self, code, globals(), locals(), RuntimeError,
                      "NAS_Allocator: can't connect: server root 'RJE-%s='"
                      " on '%s' not found" % (hostname, hostname))

        # Test for missing heartbeat.
        logging.debug('no heartbeat')
        with open(os.path.join(_DMZ_ROOT, 'RJE-%s=' % hostname), 'w') as out:
            out.write('empty\n')
        try:
            NAS_Allocator(dmz_host=hostname, server_host=hostname)
        except RuntimeError as exc:
            msg = "IOError: [Errno 2] No such file or directory:" \
                  " 'RJE-%s=heartbeat'\n" % hostname
            logging.debug(str(exc))
            self.assertTrue(str(exc).endswith(msg))
        else:
            self.fail('Expected RuntimeError')

        # Test for stale heartbeat.
        logging.debug('stale heartbeat')
        protocol.server_heartbeat(hostname, 1, logging.getLogger())
        time.sleep(5)
        assert_raises(self, code, globals(), locals(), RuntimeError,
                      "NAS_Allocator: can't connect: server heartbeat"
                      " hasn't been updated in 0:00:0")
Exemple #6
0
    def test_errors(self):
        logging.debug('')
        logging.debug('test_errors')

        logging.debug('allocate server')
        server, server_info = RAM.allocate(dict(allocator=self.allocator.name))
        try:
            logging.debug('execute bad command')
            code = "server.execute_command(dict(remote_command='no-such-command'))"
            if sys.platform == 'win32':
                msg = "WindowsError: [Error 2] The system cannot find the file specified"
            else:
                msg = "OSError: [Errno 2] No such file or directory"
            try:
                server.execute_command(dict(remote_command='no-such-command'))
            except protocol.RemoteError as exc:
                exc_msg = str(exc)
                if msg not in exc_msg:
                    self.fail('%s not in %s' % (msg, exc_msg))
            else:
                self.fail('Expecting protocol.RemoteError')

            logging.debug('open bad file')
            msg = "Can\\'t open \\'../../illegal-access\\', not within root"
            msg = 'RuntimeError: %s' % msg
            try:
                server.open('../../illegal-access', 'r')
            except protocol.RemoteError as exc:
                exc_msg = str(exc)
                if msg not in exc_msg:
                    self.fail('%s not in %s' % (msg, exc_msg))
            else:
                self.fail('Expecting protocol.RemoteError')

            logging.debug('open missing file')
            msg = "[Errno 2] No such file or directory: \\'no-such-file\\'"
            msg = 'IOError: %s' % msg
            try:
                server.open('no-such-file', 'r')
            except protocol.RemoteError as exc:
                exc_msg = str(exc)
                if msg not in exc_msg:
                    self.fail('%s not in %s' % (msg, exc_msg))
            else:
                self.fail('Expecting protocol.RemoteError')
        finally:
            logging.debug('release')
            RAM.release(server)

        # Test for exited or never started server.
        logging.debug('dead server')
        self.proc.terminate()
        self.proc = None
        time.sleep(2)
        hostname = socket.gethostname()
        if sys.platform == 'win32':  # Server doesn't clean up.
            root = protocol._server_root(hostname)
            mapped_root = os.path.join(_DMZ_ROOT, protocol._map_dir(root))
            for name in glob.glob('%s*' % mapped_root):
                os.remove(name)
        code = 'NAS_Allocator(dmz_host=hostname, server_host=hostname)'
        assert_raises(self, code, globals(), locals(), RuntimeError,
                      "NAS_Allocator: can't connect: server root 'RJE-%s='"
                      " on '%s' not found" % (hostname, hostname))

        # Test for missing heartbeat.
        logging.debug('no heartbeat')
        with open(os.path.join(_DMZ_ROOT, 'RJE-%s=' % hostname), 'w') as out:
            out.write('empty\n')
        try:
            NAS_Allocator(dmz_host=hostname, server_host=hostname)
        except RuntimeError as exc:
            msg = "IOError: [Errno 2] No such file or directory:" \
                  " 'RJE-%s=heartbeat'\n" % hostname
            logging.debug(str(exc))
            self.assertTrue(str(exc).endswith(msg))
        else:
            self.fail('Expected RuntimeError')

        # Test for stale heartbeat.
        logging.debug('stale heartbeat')
        protocol.server_heartbeat(hostname, 1, logging.getLogger())
        time.sleep(5)
        assert_raises(self, code, globals(), locals(), RuntimeError,
                      "NAS_Allocator: can't connect: server heartbeat"
                      " hasn't been updated in 0:00:0")