def test_tty_recycle(self):
        """
        check if ttys are being released after ssh connections are closed.

        ssh'ing into a workspace, opening a shell, and closing the ssh
        connection should release the pty. tool session containers get 15 ptys.
        we are checking to see if ptys are being released after the user exits
        the pty.

        the failue occurs in the call to cm.access().
        """

        session_number = 0
        cm = ContainerManager()

        for i in range(31):

            try:
                # access a tool session container
                ws = cm.access(host=self.hubname,
                               username=self.username,
                               password=self.userpass)

                session_number,es = ws.execute('echo $SESSION')

                # start up a new bash shell manually
                ws.send('/bin/bash')

                # exit the workspace
                ws.close()
            except (ConnectionClosedError,socket.timeout) as e:
                if session_number > 0:
                    # container is hosed, shut it down.
                    cm.stop(self.hubname,self.username,int(session_number))
                assert False, "After connecting %s time(s): %s" \
                    % (i,sys.exc_info()[0])
class TestIcewmUserConfig(TestCase2):

    def setup_method(self,method):

        # get user account info
        self.username,self.userpass = \
            self.testdata.find_account_for('registeredworkspace')
        self.hubname = self.testdata.find_url_for('https')

        # get into a workspace
        self.cm = ContainerManager()
        self.ws = self.cm.access(host=self.hubname,
                                 username=self.username,
                                 password=self.userpass)


    def teardown_method(self,method):
        pass


    def _check_icewm_config_file(self,fname,points_to):

        # check the file is a link
        assert self.ws.bash_test('-L {0}'.format(fname)), \
            '"{0}" not a link'.format(fname)

        # check the file points to default config
        command = 'readlink -e {0}'.format(fname)
        output,es = self.ws.execute(command)
        assert output == points_to, \
            '"{0}" points to "{1}", expected "{2}"'.format(fname,output,points_to)

        # check the default config is readable
        assert self.ws.bash_test('-r {0}'.format(points_to)), \
            '"{0}" not a readable'.format(points_to)


    def test_icewm_config(self):
        """
        check icewm config files point to hubzero config
        """

        # check if the icewm directory was created.
        msg = "User's IceWM config directory (~/.icewm) was not recreated" + \
              " by new workspace after being deleted"
        assert self.ws.bash_test('-d {0}'.format(ICEWM_CONF_DIR)), msg

        msg = "User's IceWM config directory (~/.icewm) is not readable" + \
              " by new workspace after being deleted"
        assert self.ws.bash_test('-r {0}'.format(ICEWM_CONF_DIR)), msg


        # check if the user has the default icewm user config for hubzero

        for (fname,points_to) in ICEWM_CONF_FILE_SPEC:
            self._check_icewm_config_file(fname,points_to)


    def test_updated_icewm_config(self):
        """
        check if starting a workspace created a new icewm config directory
        after the user deleted their original one.
        """

        # remove the user's icewm config dir
        self.ws.execute('rm -rf ~/.icewm')

        session_number = 0

        # create a new workspace
        ws2 = self.cm.create(host=self.hubname,
                             username=self.username,
                             password=self.userpass)

        session_number,es = ws2.execute('echo $SESSION')

        # exit the new workspace
        ws2.close()

        # stop the new container
        self.cm.stop(self.hubname,self.username,int(session_number))


        # check if the icewm directory was created.
        msg = "User's IceWM config directory (~/.icewm) was not recreated" + \
              " by new workspace after being deleted"
        assert self.ws.bash_test('-d {0}'.format(ICEWM_CONF_DIR)), msg

        msg = "User's IceWM config directory (~/.icewm) is not readable" + \
              " by new workspace after being deleted"
        assert self.ws.bash_test('-r {0}'.format(ICEWM_CONF_DIR)), msg


        # check if the user has the default icewm user config for hubzero

        for (fname,points_to) in ICEWM_CONF_FILE_SPEC:
            self._check_icewm_config_file(fname,points_to)
class TestHCUnitContainerManager(object):

    def test_get_buffer_3(self,ssh_shell):
        """check for empty buffer after retrieving prompt
        """

        p = ssh_shell.get_prompt()
        buf = ssh_shell.get_buffer()
        assert buf == '', "buf = '%s', expected ''" % (buf)



    def setUp(self):

        self.cm = ContainerManager()

        self.host1 = ''
        self.user11 = ''
        self.pass11 = ''
        self.user12 = ''
        self.pass12 = ''

        self.host2 = ''
        self.user21 = ''
        self.pass21 = ''
        self.user22 = ''
        self.pass22 = ''

    def tearDown(self):

        self.cm.stop_all()
        self.cm.clear()


    def test_access_new_session(self,cm):
        """
        """

        ws = cm.access(self.host1,self.user11,self.pass11)
        session_number,es = ws.execute('echo $SESSION')
        self.assertTrue(int(session_number) > 0,
            "session_number = %s, i don't think we are in a workspace" \
            % session_number)
        ws.close()


    def test_access_previously_opened_session(self):
        """
        """

        # open the first tool session container
        ws1 = self.cm.access(self.host1,self.user11,self.pass11)
        session_number1,es = ws1.execute('echo $SESSION')
        # exit the container
        ws1.close()

        # make sure we got into the container
        self.assertTrue(int(session_number1) > 0,
            "session_number = %s, i don't think we are in a workspace" \
            % session_number1)

        # open the second tool session container
        ws2 = self.cm.access(self.host1,self.user11,self.pass11)
        session_number2,es = ws2.execute('echo $SESSION')
        ws2.close()

        # make sure we got into the second container
        self.assertTrue(int(session_number2) > 0,
            "session_number = %s, i don't think we are in a workspace" \
            % session_number2)

        # check that the first and second containers were the same
        # by comparing the session number

        self.assertTrue(int(session_number1) == int(session_number2),
            "connecting twice to the same session failed: session_number1 = %s, session_number2 = %s" \
            % (session_number1,session_number2))


    def test_access_sessions_for_different_users_same_host(self):
        """
        """

        # open tool session container for user1
        ws1 = self.cm.access(self.host1,self.user11,self.pass11)
        session_number1,es = ws1.execute('echo $SESSION')
        # exit the container
        ws1.close()

        # make sure we got into the container
        self.assertTrue(int(session_number1) > 0,
            "session_number = %s, i don't think we are in a workspace" \
            % session_number1)

        # open tool session container for user2
        ws2 = self.cm.access(self.host1,self.user12,self.pass12)
        session_number2,es = ws2.execute('echo $SESSION')
        ws2.close()

        # make sure we got into the second container
        self.assertTrue(int(session_number2) > 0,
            "session_number = %s, i don't think we are in a workspace" \
            % session_number2)

        # check that the first and second containers are different
        # by comparing the session number

        self.assertTrue(int(session_number1) != int(session_number2),
            "trying to connect to two different containers, as different users failed: session_number1 = %s, session_number2 = %s" \
            % (session_number1,session_number2))


    def test_access_sessions_for_different_users_different_host(self):
        """
        """

        # open tool session container for user1
        ws1 = self.cm.access(self.host1,self.user11,self.pass11)
        session_number1,es = ws1.execute('echo $SESSION')
        # exit the container
        ws1.close()

        # make sure we got into the container
        self.assertTrue(int(session_number1) > 0,
            "session_number = %s, i don't think we are in a workspace" \
            % session_number1)

        # open tool session container for user2
        ws2 = self.cm.access(self.host2,self.user21,self.pass21)
        session_number2,es = ws2.execute('echo $SESSION')
        ws2.close()

        # make sure we got into the second container
        self.assertTrue(int(session_number2) > 0,
            "session_number = %s, i don't think we are in a workspace" \
            % session_number2)

        # check that the first and second containers are different
        # by comparing the session number

        self.assertTrue(int(session_number1) != int(session_number2),
            "trying to connect to two different containers, as different users failed: session_number1 = %s, session_number2 = %s" \
            % (session_number1,session_number2))


    def test_stop_session_1(self):
        """
        close a session that is open
        """

        # open a session
        ws1 = self.cm.access(self.host1,self.user11,self.pass11)
        session_number1,es = ws1.execute('echo $SESSION')
        self.assertTrue(int(session_number1) > 0,
            "session_number = %s, i don't think we are in a workspace" \
            % session_number1)
        ws1.close()

        # stop the tool session container
        self.cm.stop(self.host1,self.user11,session_number1)

        session = ToolSession(host=self.host1,
                              username=self.user11,
                              password=self.pass11)

        data = session.get_open_session_detail()
        for k,v in data.items():
            if v['session_number'] == session_number1:
                self.assertTrue(v['session_number'] == session_number1,
                    "session %s still open, after calling stop()" \
                    % (session_number1))


    def test_stop_session_2(self):
        """
        try to close a session that is not open
        """

        # open a temporary session
        # so we can get a session in the stop() method
        ws1 = self.cm.access(self.host1,self.user11,self.pass11)
        session_number1,es = ws1.execute('echo $SESSION')
        self.assertTrue(int(session_number1) > 0,
            "session_number = %s, i don't think we are in a workspace" \
            % session_number1)
        ws1.close()


        try:
            # stop the tool session container
            session_number2 = 1234
            self.cm.stop(self.host1,self.user11,session_number2)
        finally:
            # close the temporary session
            self.cm.stop(self.host1,self.user11,session_number1)