Beispiel #1
0
def normalize_str(value):
    import re
    is_bytes = isinstance(value, bytes)
    regex, sep = r'(\S+)\s*', ' '
    if is_bytes:
        regex, sep = bytes(regex), bytes(sep)
    regex = re.compile(regex)
    matches = regex.findall(value)
    names = (m.capitalize() if len(m) >= 3 else m.lower() for m in matches)
    return sep.join(names)
Beispiel #2
0
    def sending_thread(self, res_q):
        """
        Send results from queue
        """
        self.connect()

        while True:
            try:
                result = res_q.get(True)
                # Not keeping data in memory
                res_q.task_done()

                if not self.sock:
                    self.connect()

                if not self.sock:
                    self.log.info(
                        'Reconnect to %s:%s failed' % (self.host, self.port))
                    continue

                self.log.debug("Sending: %s" % result)

                try:
                    self.sock.send(bytes(json.dumps(result) + '\n'))
                except:
                    self.log.info("Connection reset")
                    self.log.debug(traceback.format_exc())
                    self.sock = None
            except:
                self.log.error("Unknown error sending checks")
Beispiel #3
0
    def test_Diamond(self):
        # Mkfifo done by tantale daemon, but ensure it
        self.start()
        for i in range(20):
            try:
                diamond_fd = os.open(diamond_fifo, os.O_WRONLY | os.O_NONBLOCK)
            except:
                time.sleep(0.5)

        for metric in diamond_input.split("\n"):
            os.write(diamond_fd, bytes("%s %d\n" % (metric, int(time.time()))))

        # Check result from livestatus
        live_s = self.getSocket('Livestatus')
        for nb in range(10):
            time.sleep(1)
            live_s.send(
                self.getLivestatusRequest('get_service') %
                ("fqdn.domain", "fs_root"))
            res = live_s.recv()
            res = eval(res[16:])
            if len(res) > 0:
                break

        self.assertEqual(res[0][3], "fqdn.domain", res)
        self.assertEqual(
            res[0][4],
            "29.85 (10.0, 20.0, None, None)", res)
        self.assertEqual(res[0][-1], 0, res)

        os.close(diamond_fd)

        self.stop()
Beispiel #4
0
def parametrize(cls, template_method, name, params, wrapper=None):
    """Creates a new test method on a TestCase class, which calls a
    specific template method with the given parameters (ie. a
    parametrized test). Given a testcase like this::

        class MyTest(unittest.TestCase):
            def my_general_test(self, parameter):
                self.assertEqual(parameter, "hello")

    and the following top-level initalization code::

        parametrize(MyTest,MyTest.my_general_test, "test_one", ["hello"])
        parametrize(MyTest,MyTest.my_general_test, "test_two", ["world"])

    you end up with a test case class with two methods. Using
    e.g. ``unittest discover`` (or any other unittest-compatible test
    runner), the following should be the result::

        test_one (test_parametric.MyTest) ... ok
        test_two (test_parametric.MyTest) ... FAIL
        
        ======================================================================
        FAIL: test_two (test_parametric.MyTest)
        ----------------------------------------------------------------------
        Traceback (most recent call last):
          File "./ferenda/testutil.py", line 365, in test_method
            template_method(self, *params)
          File "./test_parametric.py", line 6, in my_general_test
            self.assertEqual(parameter, "hello")
        AssertionError: 'world' != 'hello'
        - world
        + hello
        
    :param cls: The ``TestCase`` class to add the parametrized test to.
    :param template_method: The method to use for parametrization
    :param name: The name for the new test method
    :type  name: str
    :param params: The parameter list (Note: keyword parameters are not supported)
    :type  params: list
    :param wrapper: A unittest decorator like :py:func:`unittest.skip` or :py:func:`unittest.expectedFailure`.
    :param wrapper: callable

    """
    # internal entrypoint for tesst

    def test_method(self):
        template_method(self, *params)

    # py2 compat: name is a unicode object, func.__name__ must be a str(?)
    if six.PY3:
        test_method.__name__ = name
    else:
        # note that we have redefined str to six.text_type
        test_method.__name__ = bytes(name)
    # wrapper is a unittest decorator like skip or expectedFailure
    if wrapper:
        setattr(cls, name, wrapper(test_method))
    else:
        setattr(cls, name, test_method)
Beispiel #5
0
    def send(self, msg):
        if not isinstance(msg, binary_type):
            msg = bytes(msg)

        totalsent = 0
        MSGLEN = len(msg)
        while totalsent < MSGLEN:
            sent = self.sock.send(msg[totalsent:])
            if sent == 0:
                raise RuntimeError("socket connection broken")
            totalsent = totalsent + sent
Beispiel #6
0
 def flush(self):
     """
     Dump results to network
     """
     if self.rheader == 'fixed16':
         string = str(self.results)
         self.output_sock.send(
             bytes('%3d %11d %s\n' % (200, len(string) + 1, string)))
     else:
         if len(self.results) > 0:
             raise NotImplementedError
Beispiel #7
0
    def handle_client(self, client_socket):
        run = True
        request = ""
        stack = ""
        while not self.stop.is_set() and run:
            try:
                r = None
                r, w, e = select.select([client_socket], [], [], 300)
            except:
                # Handle "Interrupted system call"
                break

            if r is not None:
                sock = r[0]
                data = sock.recv(4096)
                if data == bytes(''):
                    # Closing thread
                    run = False
                    try:
                        sock.shutdown(socket.SHUT_RDWR)
                        sock.close()
                    except:
                        pass
                    break

                data = stack + data.decode('utf-8')

                for line in data.split("\n"):
                    if line == "":
                        # Empty line - process query
                        if request != "":
                            keep = self.handle_livestatus_query(sock, request)
                            if not keep:
                                run = False
                                break
                            else:
                                request = ""
                    else:
                        # Append to query
                        if data.endswith("\n"):
                            request += line + '\n'
                        else:
                            stack = line
            else:
                # Timeout waiting
                run = False
                break
Beispiel #8
0
def safe_encode(u, encoding=None):
    '''Similar to unicode `encode` method returning bytes.

    Encodes `u` using the given `encoding`, or determining one from the system.

    Returning type is always `bytes`; but in python 2.x is also `str`.

    .. versionadded:: 1.1.3

    '''
    if isinstance(u, bytes):
        return u
    else:
        encoding = force_encoding(encoding)
        try:
            if isinstance(u, _str_base):
                # In Python 2.x bytes does not allows an encoding argument.
                return bytes(u)
            else:
                return _unicode(u).encode(encoding, 'replace')
        except:
            return _unicode(u).encode(encoding, 'replace')
Beispiel #9
0
 def sig_handler(signum, frame):
     self.log.debug("%s received" % signum)
     self.stop.set()
     os.write(pipe[1], bytes('END'))