Exemple #1
0
class ConnectTestCase(unittest.TestCase):
    """ connect/disconnect tests """
    def setUp(self):
        """
        make sure that the prefix dir is empty
        """
        self.zk_host = os.getenv("ZKSHELL_ZK_HOST", "localhost:2181")
        self.output = StringIO()
        self.shell = Shell([], 1, self.output, setup_readline=False, async=False)

    def tearDown(self):
        if self.output:
            self.output.close()
            self.output = None

        if self.shell:
            self.shell._disconnect()
            self.shell = None

    def test_start_connected(self):
        """ test connect command """
        self.shell.onecmd("connect %s" % (self.zk_host))
        self.shell.onecmd("session_info")
        self.assertIn("state=CONNECTED", self.output.getvalue())

    def test_start_disconnected(self):
        """ test session info whilst disconnected """
        self.shell.onecmd("session_info")
        self.assertIn("Not connected.\n", self.output.getvalue())

    def test_start_bad_host(self):
        """ test connecting to a bad host """
        self.shell.onecmd("connect %s" % ("doesnt-exist.itevenworks.net:2181"))
        self.assertEquals("Failed to connect: Connection time-out\n",
                          self.output.getvalue())

    def test_connect_disconnect(self):
        """ test disconnecting """
        self.shell.onecmd("connect %s" % (self.zk_host))
        self.assertTrue(self.shell.connected)
        self.shell.onecmd("disconnect")
        self.assertFalse(self.shell.connected)
Exemple #2
0
class ShellTestCase(unittest.TestCase):
    """ base class for all tests """
    @classmethod
    def setUpClass(cls):
        get_global_cluster().start()

    def setUp(self):
        """
        make sure that the prefix dir is empty
        """
        self.tests_path = os.getenv("ZKSHELL_PREFIX_DIR", "/tests")
        self.zk_hosts = ",".join(server.address
                                 for server in get_global_cluster())
        self.username = os.getenv("ZKSHELL_USER", "user")
        self.password = os.getenv("ZKSHELL_PASSWD", "user")
        self.digested_password = os.getenv("ZKSHELL_DIGESTED_PASSWD",
                                           "F46PeTVYeItL6aAyygIVQ9OaaeY=")
        self.super_password = os.getenv("ZKSHELL_SUPER_PASSWD", "test")
        self.scheme = os.getenv("ZKSHELL_AUTH_SCHEME", "digest")

        self.client = KazooClient(self.zk_hosts, 5)
        self.client.start()
        self.client.add_auth(self.scheme, self.auth_id)
        if self.client.exists(self.tests_path):
            self.client.delete(self.tests_path, recursive=True)
        self.client.create(self.tests_path, str.encode(""))

        self.output = XStringIO()
        self.shell = Shell([self.zk_hosts],
                           5,
                           self.output,
                           setup_readline=False,
                           asynchronous=False)

        # Create an empty test dir (needed for some tests)
        self.temp_dir = tempfile.mkdtemp()

    @property
    def auth_id(self):
        return "%s:%s" % (self.username, self.password)

    @property
    def auth_digest(self):
        return "%s:%s" % (self.username, self.digested_password)

    def tearDown(self):
        if self.output is not None:
            self.output.close()
            self.output = None

        if self.shell is not None:
            self.shell._disconnect()
            self.shell = None

        if os.path.isdir(self.temp_dir):
            shutil.rmtree(self.temp_dir)

        if self.client is not None:
            if self.client.exists(self.tests_path):
                self.client.delete(self.tests_path, recursive=True)

            self.client.stop()
            self.client.close()
            self.client = None

    ###
    # Helpers.
    ##

    def create_compressed(self, path, value):
        """
        ZK Shell doesn't support creating directly from a bytes array so we use a Kazoo client
        to create a znode with zlib compressed content.
        """
        compressed = zlib.compress(bytes(value, "utf-8") if PYTHON3 else value)
        self.client.create(path, compressed, makepath=True)
Exemple #3
0
class ConnectTestCase(unittest.TestCase):
    """ connect/disconnect tests """
    @classmethod
    def setUpClass(cls):
        get_global_cluster().start()

    def setUp(self):
        """
        make sure that the prefix dir is empty
        """
        self.zk_hosts = ",".join(server.address
                                 for server in get_global_cluster())
        self.output = StringIO()
        self.shell = Shell([],
                           1,
                           self.output,
                           setup_readline=False,
                           async=False)

    def tearDown(self):
        if self.output:
            self.output.close()
            self.output = None

        if self.shell:
            self.shell._disconnect()
            self.shell = None

    def test_start_connected(self):
        """ test connect command """
        self.shell.onecmd("connect %s" % (self.zk_hosts))
        self.shell.onecmd("session_info")
        self.assertIn("state=CONNECTED", self.output.getvalue())

    def test_start_disconnected(self):
        """ test session info whilst disconnected """
        self.shell.onecmd("session_info")
        self.assertIn("Not connected.\n", self.output.getvalue())

    def test_start_bad_host(self):
        """ test connecting to a bad host """
        self.shell.onecmd("connect %s" % ("doesnt-exist.itevenworks.net:2181"))
        self.assertEquals("Failed to connect: Connection time-out\n",
                          self.output.getvalue())

    def test_connect_disconnect(self):
        """ test disconnecting """
        self.shell.onecmd("connect %s" % (self.zk_hosts))
        self.assertTrue(self.shell.connected)
        self.shell.onecmd("disconnect")
        self.assertFalse(self.shell.connected)

    def test_connect_async(self):
        """ test async """

        # SIGUSR2 is emitted when connecting asyncronously, so handle it
        def handler(*args, **kwargs):
            pass

        signal.signal(signal.SIGUSR2, handler)

        shell = Shell([], 1, self.output, setup_readline=False, async=True)
        shell.onecmd("connect %s" % (self.zk_hosts))
        self.assertTrue(wait_connected(shell))

    def test_reconnect(self):
        """ force reconnect """
        self.shell.onecmd("connect %s" % (self.zk_hosts))
        self.shell.onecmd("reconnect")
        self.assertTrue(wait_connected(self.shell))
Exemple #4
0
class ConnectTestCase(unittest.TestCase):
    """ connect/disconnect tests """
    @classmethod
    def setUpClass(cls):
        get_global_cluster().start()

    def setUp(self):
        """
        make sure that the prefix dir is empty
        """
        self.zk_hosts = ",".join(server.address for server in get_global_cluster())
        self.output = StringIO()
        self.shell = Shell([], 1, self.output, setup_readline=False, async=False)

    def tearDown(self):
        if self.output:
            self.output.close()
            self.output = None

        if self.shell:
            self.shell._disconnect()
            self.shell = None

    def test_start_connected(self):
        """ test connect command """
        self.shell.onecmd("connect %s" % (self.zk_hosts))
        self.shell.onecmd("session_info")
        self.assertIn("state=CONNECTED", self.output.getvalue())

    def test_start_disconnected(self):
        """ test session info whilst disconnected """
        self.shell.onecmd("session_info")
        self.assertIn("Not connected.\n", self.output.getvalue())

    def test_start_bad_host(self):
        """ test connecting to a bad host """
        self.shell.onecmd("connect %s" % ("doesnt-exist.itevenworks.net:2181"))
        self.assertEquals("Failed to connect: Connection time-out\n",
                          self.output.getvalue())

    def test_connect_disconnect(self):
        """ test disconnecting """
        self.shell.onecmd("connect %s" % (self.zk_hosts))
        self.assertTrue(self.shell.connected)
        self.shell.onecmd("disconnect")
        self.assertFalse(self.shell.connected)

    def test_connect_async(self):
        """ test async """

        # SIGUSR2 is emitted when connecting asyncronously, so handle it
        def handler(*args, **kwargs):
            pass
        signal.signal(signal.SIGUSR2, handler)

        shell = Shell([], 1, self.output, setup_readline=False, async=True)
        shell.onecmd("connect %s" % (self.zk_hosts))
        self.assertTrue(wait_connected(shell))

    def test_reconnect(self):
        """ force reconnect """
        self.shell.onecmd("connect %s" % (self.zk_hosts))
        self.shell.onecmd("reconnect")
        self.assertTrue(wait_connected(self.shell))
Exemple #5
0
class ShellTestCase(unittest.TestCase):
    """ base class for all tests """

    @classmethod
    def setUpClass(cls):
        get_global_cluster().start()

    def setUp(self):
        """
        make sure that the prefix dir is empty
        """
        self.tests_path = os.getenv("ZKSHELL_PREFIX_DIR", "/tests")
        self.zk_hosts = ",".join(server.address for server in get_global_cluster())
        self.username = os.getenv("ZKSHELL_USER", "user")
        self.password = os.getenv("ZKSHELL_PASSWD", "user")
        self.digested_password = os.getenv("ZKSHELL_DIGESTED_PASSWD", "F46PeTVYeItL6aAyygIVQ9OaaeY=")
        self.super_password = os.getenv("ZKSHELL_SUPER_PASSWD", "test")
        self.scheme = os.getenv("ZKSHELL_AUTH_SCHEME", "digest")

        self.client = KazooClient(self.zk_hosts, 5)
        self.client.start()
        self.client.add_auth(self.scheme, self.auth_id)
        if self.client.exists(self.tests_path):
            self.client.delete(self.tests_path, recursive=True)
        self.client.create(self.tests_path, str.encode(""))

        self.output = XStringIO()
        self.shell = Shell([self.zk_hosts], 5, self.output, setup_readline=False, asynchronous=False)

        # Create an empty test dir (needed for some tests)
        self.temp_dir = tempfile.mkdtemp()

    @property
    def auth_id(self):
        return "%s:%s" % (self.username, self.password)

    @property
    def auth_digest(self):
        return "%s:%s" % (self.username, self.digested_password)

    def tearDown(self):
        if self.output is not None:
            self.output.close()
            self.output = None

        if self.shell is not None:
            self.shell._disconnect()
            self.shell = None

        if os.path.isdir(self.temp_dir):
            shutil.rmtree(self.temp_dir)

        if self.client is not None:
            if self.client.exists(self.tests_path):
                self.client.delete(self.tests_path, recursive=True)

            self.client.stop()
            self.client.close()
            self.client = None

    ###
    # Helpers.
    ##

    def create_compressed(self, path, value):
        """
        ZK Shell doesn't support creating directly from a bytes array so we use a Kazoo client
        to create a znode with zlib compressed content.
        """
        compressed = zlib.compress(bytes(value, "utf-8") if PYTHON3 else value)
        self.client.create(path, compressed, makepath=True)