<commit_before>"""
This file is part of Arakoon, a distributed key-value store. Copyright
(C) 2010 Incubaid BVBA

Licensees holding a valid Incubaid license may use this file in
accordance with Incubaid's Arakoon commercial license agreement. For
more information on how to enter into this agreement, please contact
Incubaid (contact details can be found on www.arakoon.org/licensing).

Alternatively, this file may be redistributed and/or modified under
the terms of the GNU Affero General Public License version 3, as
published by the Free Software Foundation. Under this license, this
file is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.

See the GNU Affero General Public License for more details.
You should have received a copy of the
GNU Affero General Public License along with this program (file "COPYING").
If not, see <http://www.gnu.org/licenses/>.
"""

from pymonkey import q
from nose.tools import *
import subprocess
import os
import time
import logging

class TestCmdTools:
    def __init__(self):
        cid = 'sturdy'
        self._clusterId = cid
        self._n0 = "%s_0" % cid
        self._n1 = "%s_1" % cid
        self._n2 = "%s_2" % cid

    def _serverNodes(self):
        return "%s_servernodes" % self._clusterId

    def _getCluster(self):
        return q.manage.arakoon.getCluster(self._clusterId)
    
    def setup(self):
        if self._clusterId in q.config.list():
            q.config.remove(self._clusterId)
        
        servernodes = self._serverNodes()
        if servernodes in q.config.list():
            q.config.remove(servernodes)

        cluster = self._getCluster()
        cluster.setUp(3)

    def teardown(self):
        cluster = self._getCluster()
        cluster.stop()
        cluster.tearDown()
        cluster.remove()

    def _assert_n_running(self,n):
        cluster = self._getCluster()
        status = cluster.getStatus()
        logging.debug('status=%s', status)
        c = 0
        for key in status.keys():
            if status[key] == q.enumerators.AppStatusType.RUNNING:
                c = c + 1
        assert_equals(c, n)

    def testStart(self):
        cluster = self._getCluster()
        cluster.start()
        self._assert_n_running(3)

        #starting twice should not throw anything
        cluster.start()
        self._assert_n_running(3)



    def testStop(self):
        cluster = self._getCluster()
        cluster.start()
        self._assert_n_running(3)

        cluster.stop()
        self._assert_n_running(0)

        #stopping twice should not throw anything
        cluster.stop()
        self._assert_n_running(0)

    def testRestart(self):
        cluster = self._getCluster()
        cluster.start()
        self._assert_n_running(3)

        cluster.restart()
        #@TODO check if the pids are different
        self._assert_n_running(3)
        cluster.stop()
        self._assert_n_running(0)

        cluster.restart()
        self._assert_n_running(3)

    def testStartOne(self):
        cluster = self._getCluster()
        cluster.startOne(self._n0)
        self._assert_n_running(1)
        cluster.startOne(self._n1)
        self._assert_n_running(2)

    def testStartOneUnkown(self):
        cluster = self._getCluster()
        assert_raises(Exception, cluster.startOne, "arakoon0")

    def testStopOne(self):
        cluster = self._getCluster()
        cluster.start()
        t0 = time.clock()
        cluster.stopOne(self._n0)
        t1 = time.clock()
        assert_true(t1 - t0 < 2.0, "took too long")
        self._assert_n_running(2)

    def testStopOneUnknown(self):
        cluster = self._getCluster()
        assert_raises(Exception, cluster.stopOne, "arakoon0")

    def testGetStatus(self):
        cluster = self._getCluster()
        cluster.start()
        assert_equals(cluster.getStatus(),
                      {self._n0: q.enumerators.AppStatusType.RUNNING,
                       self._n1: q.enumerators.AppStatusType.RUNNING,
                       self._n2: q.enumerators.AppStatusType.RUNNING})

        cluster.stopOne(self._n0)
        assert_equals(cluster.getStatus(),
                      {self._n0: q.enumerators.AppStatusType.HALTED, 
                       self._n1: q.enumerators.AppStatusType.RUNNING,
                       self._n2: q.enumerators.AppStatusType.RUNNING})
        
    def testGetStatusOne(self):
        cluster = self._getCluster()
        cluster.start()
        cluster.stopOne(self._n0)<commit_msg>who prepends to my paths & where?<commit_after>"""
This file is part of Arakoon, a distributed key-value store. Copyright
(C) 2010 Incubaid BVBA

Licensees holding a valid Incubaid license may use this file in
accordance with Incubaid's Arakoon commercial license agreement. For
more information on how to enter into this agreement, please contact
Incubaid (contact details can be found on www.arakoon.org/licensing).

Alternatively, this file may be redistributed and/or modified under
the terms of the GNU Affero General Public License version 3, as
published by the Free Software Foundation. Under this license, this
file is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.

See the GNU Affero General Public License for more details.
You should have received a copy of the
GNU Affero General Public License along with this program (file "COPYING").
If not, see <http://www.gnu.org/licenses/>.
"""

from pymonkey import q
from nose.tools import *
import subprocess
import os
import time
import logging

class TestCmdTools:
    def __init__(self):
        cid = 'sturdy'
        self._clusterId = cid
        self._n0 = "%s_0" % cid
        self._n1 = "%s_1" % cid
        self._n2 = "%s_2" % cid

    def _serverNodes(self):
        return "%s_servernodes" % self._clusterId

    def _getCluster(self):
        return q.manage.arakoon.getCluster(self._clusterId)
    
    def setup(self):
        if self._clusterId in q.config.list():
            q.config.remove(self._clusterId)
        
        servernodes = self._serverNodes()
        if servernodes in q.config.list():
            q.config.remove(servernodes)

        cluster = self._getCluster()
        cluster.setUp(3)

    def teardown(self):
        cluster = self._getCluster()
        cluster.stop()
        cluster.tearDown()
        cluster.remove()

    def _assert_n_running(self,n):
        cluster = self._getCluster()
        status = cluster.getStatus()
        logging.debug('status=%s', status)
        c = 0
        for key in status.keys():
            if status[key] == q.enumerators.AppStatusType.RUNNING:
                c = c + 1
        assert_equals(c, n)

    def testStart(self):
        cluster = self._getCluster()
        cluster.start()
        self._assert_n_running(3)

        #starting twice should not throw anything
        cluster.start()
        self._assert_n_running(3)



    def testStop(self):
        cluster = self._getCluster()
        cluster.start()
        self._assert_n_running(3)

        cluster.stop()
        self._assert_n_running(0)

        #stopping twice should not throw anything
        cluster.stop()
        self._assert_n_running(0)

    def testRestart(self):
        cluster = self._getCluster()
        cluster.start()
        self._assert_n_running(3)

        cluster.restart()
        #@TODO check if the pids are different
        self._assert_n_running(3)
        cluster.stop()
        self._assert_n_running(0)

        cluster.restart()
        self._assert_n_running(3)

    def testStartOne(self):
        cluster = self._getCluster()
        cluster.startOne(self._n0)
        self._assert_n_running(1)
        cluster.startOne(self._n1)
        self._assert_n_running(2)

    def testStartOneUnkown(self):
        cluster = self._getCluster()
        assert_raises(Exception, cluster.startOne, "arakoon0")

    def testStopOne(self):
        cluster = self._getCluster()
        cluster.start()
        t0 = time.clock()
        cluster.stopOne(self._n0)
        t1 = time.clock()
        assert_true(t1 - t0 < 2.0, "took too long")
        self._assert_n_running(2)

    def testStopOneUnknown(self):
        cluster = self._getCluster()
        assert_raises(Exception, cluster.stopOne, "arakoon0")

    def testGetStatus(self):
        logging.info('1')
        cluster = self._getCluster()
        logging.info('2')
        cluster.start()
        logging.info('3')
        assert_equals(cluster.getStatus(),
                      {self._n0: q.enumerators.AppStatusType.RUNNING,
                       self._n1: q.enumerators.AppStatusType.RUNNING,
                       self._n2: q.enumerators.AppStatusType.RUNNING})
        logging.info('4')
        cluster.stopOne(self._n0)
        assert_equals(cluster.getStatus(),
                      {self._n0: q.enumerators.AppStatusType.HALTED, 
                       self._n1: q.enumerators.AppStatusType.RUNNING,
                       self._n2: q.enumerators.AppStatusType.RUNNING})
        
    def testGetStatusOne(self):
        cluster = self._getCluster()
        cluster.start()
        cluster.stopOne(self._n0)
{'fraction_non_alphanumeric': 0.09255490091055169, 'fraction_numerical': 0.010069630423138725, 'mean_word_length': 2.363112391930836, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2356280', 'n_tokens_mistral': 2787, 'n_tokens_neox': 2689, 'n_words': 702}