Пример #1
0
	def dump_evlist(self, poll, tag, evlist):
		if evlist:
			self.log.info("%s Event list from %s ...", my(self), tag)
			for fd, ev in evlist:
				self.log.info("    %s on fd %s", poll.get_event(ev), str(fd))
		else:
			self.log.info("%s Event list from %s is empty", my(self), tag)
Пример #2
0
 def dump_evlist(self, poll, tag, evlist):
     if evlist:
         self.log.info("%s Event list from %s ...", my(self), tag)
         for fd, ev in evlist:
             self.log.info("    %s on fd %s", poll.get_event(ev), str(fd))
     else:
         self.log.info("%s Event list from %s is empty", my(self), tag)
Пример #3
0
	def Test_C_poll(self):
		log_level = self.log.getEffectiveLevel()

		poll_fd, poll_send = self.self_pipe()

		poll = taskforce.poll.poll()
		poll.register(poll_fd, taskforce.poll.POLLIN)

		#  Check active poll
		os.write(poll_send, '\0'.encode('utf-8'))
		evlist = poll.poll(timeout=30)
		self.dump_evlist(poll, 'active poll', evlist)
		assert evlist
		assert len(os.read(poll_fd, 10)) == 1

		#  Check timeout
		evlist = poll.poll(timeout=30)
		self.dump_evlist(poll, 'timeout poll', evlist)
		assert evlist == []

		#  Check timeout accuracy
		start = time.time()
		delay = 500
		evlist = poll.poll(timeout=500)
		self.dump_evlist(poll, 'timeout accuracy', evlist)
		assert evlist == []
		delta = abs(time.time() - start - delay/1000.0)
		self.log.info("%s poll timeout delta from wall clock %s", my(self), deltafmt(delta, decimals=6))
		assert delta < 0.1

		if poll.get_mode() == taskforce.poll.PL_SELECT:
			self.log.warning("%s Default mode is PL_SELECT so retest skipped", my(self))
		else:
			poll = taskforce.poll.poll()
			poll.set_mode(taskforce.poll.PL_SELECT)
			poll.register(poll_fd, taskforce.poll.POLLIN)

			#  Check active poll
			os.write(poll_send, '\0'.encode('utf-8'))
			evlist = poll.poll(timeout=30)
			self.dump_evlist(poll, 'select active poll', evlist)
			assert evlist
			assert len(os.read(poll_fd, 10)) == 1

			#  Check timeout
			evlist = poll.poll(timeout=30)
			self.dump_evlist(poll, 'select timeout poll', evlist)
			assert evlist == []

			#  Check timeout accuracy
			start = time.time()
			delay = 500
			evlist = poll.poll(timeout=500)
			self.dump_evlist(poll, 'select timeout accuracy', evlist)
			assert evlist == []
			delta = abs(time.time() - start - delay/1000.0)
			self.log.info("%s select poll timeout delta from wall clock %s", my(self), deltafmt(delta, decimals=6))
			assert delta < 0.1

		self.close_pipe()
Пример #4
0
    def Test_A_my(self):
        has_place = re.compile(r'\[\+\d+\s+[^\]]+\]')

        here = my(log=self.log)
        self.log.info("%s with no class reference", here)
        assert here.startswith('Test.Test_A_my()')

        here = my(self)
        self.log.info("%s with class reference", here)
        assert here.startswith('Test.Test_A_my()')

        my_module_level()
        my_class_init()

        here = my(self, place=True, log=self.log)
        self.log.info("%s with place forced on", here)
        assert has_place.search(here)

        here = my(self, place=False, log=self.log)
        self.log.info("%s with place forced off", here)
        assert not has_place.search(here)

        my(self, persist_place=True, log=self.log)
        here = my(self, log=self.log)
        self.log.info("%s with persistent place forced on", here)
        assert has_place.search(here)

        my(self, persist_place=False, log=self.log)
        here = my(self, log=self.log)
        self.log.info("%s with persistent place forced off", here)
        assert not has_place.search(here)
Пример #5
0
    def Test_C_poll(self):
        log_level = self.log.getEffectiveLevel()

        poll_fd, poll_send = self.self_pipe()

        poll = taskforce.poll.poll()
        poll.register(poll_fd, taskforce.poll.POLLIN)

        #  Check active poll
        os.write(poll_send, '\0'.encode('utf-8'))
        evlist = poll.poll(timeout=30)
        self.dump_evlist(poll, 'active poll', evlist)
        assert evlist
        assert len(os.read(poll_fd, 10)) == 1

        #  Check timeout
        evlist = poll.poll(timeout=30)
        self.dump_evlist(poll, 'timeout poll', evlist)
        assert evlist == []

        #  Check timeout accuracy
        start = time.time()
        delay = 500
        evlist = poll.poll(timeout=500)
        self.dump_evlist(poll, 'timeout accuracy', evlist)
        assert evlist == []
        delta = abs(time.time() - start - delay/1000.0)
        self.log.info("%s poll timeout delta from wall clock %s", my(self), deltafmt(delta, decimals=6))
        assert delta < 0.1

        if poll.get_mode() == taskforce.poll.PL_SELECT:
            self.log.warning("%s Default mode is PL_SELECT so retest skipped", my(self))
        else:
            poll = taskforce.poll.poll()
            poll.set_mode(taskforce.poll.PL_SELECT)
            poll.register(poll_fd, taskforce.poll.POLLIN)

            #  Check active poll
            os.write(poll_send, '\0'.encode('utf-8'))
            evlist = poll.poll(timeout=30)
            self.dump_evlist(poll, 'select active poll', evlist)
            assert evlist
            assert len(os.read(poll_fd, 10)) == 1

            #  Check timeout
            evlist = poll.poll(timeout=30)
            self.dump_evlist(poll, 'select timeout poll', evlist)
            assert evlist == []

            #  Check timeout accuracy
            start = time.time()
            delay = 500
            evlist = poll.poll(timeout=500)
            self.dump_evlist(poll, 'select timeout accuracy', evlist)
            assert evlist == []
            delta = abs(time.time() - start - delay/1000.0)
            self.log.info("%s select poll timeout delta from wall clock %s", my(self), deltafmt(delta, decimals=6))
            assert delta < 0.1

        self.close_pipe()
Пример #6
0
    def do_get(self, httpc, httpd, path='/test/json'):
        httpc.request('GET', path)

        pset = taskforce.poll.poll()
        pset.register(httpd, taskforce.poll.POLLIN)

        #  This is a little bit tricky because we are talking to ourselves,
        #  so immediately enter a poll loop, and collect the response once
        #  the daemon thread has been started.  This also means we can't
        #  exercise SSL because the httpd service needs control to complete
        #  the handshake.  SSL testing is done against the actual taskforce
        #  process in another test unit.
        #
        httpr = None
        done = False
        handled = False
        while not done:
            try:
                evlist = pset.poll(5000)
            except OSError as e:
                if e.errno != errno.EINTR:
                    raise e
                else:
                    self.log.info("%s Interrupted poll()", my(self))
                    continue
            if not evlist:
                raise Exception("Event loop timed out")
            for item, mask in evlist:
                if item == httpd:
                    try:
                        item.handle_request()
                    except Exception as e:
                        self.log.warning("%s HTTP error -- %s", my(self), str(e))
                elif item == httpr:
                    assert item.getheader('Content-Type') == 'application/json'
                    text = httpr.read().decode('utf-8')
                    self.log.info('%s %d byte response received', my(self), len(text))
                    self.log.debug('%s Answer ...', my(self))
                    for line in text.splitlines():
                        self.log.debug('%s', line)
                    ans = json.loads(text)
                    assert ans == self.http_test_map
                    done = True
                    break
                else:
                    raise Exception("Unknown event item: " + str(item))
            if not handled:
                httpr = httpc.getresponse()
                pset.register(httpr, taskforce.poll.POLLIN)
                self.log.info("%s HTTP response object successfully registered", my(self))
                handled = True
Пример #7
0
 def Test_I_getmap_non_json_error(self):
     self.log.info("Starting %s", my(self))
     gc.collect()
     httpc = taskforce.http.Client(address='mmm.fullford.com:80', log=self.log)
     log_level = self.log.getEffectiveLevel()
     try:
         #  Mask the log message as we expect a failure
         self.log.setLevel(logging.CRITICAL)
         httpc.getmap('/', {'with': 'invalid query'})
         self.log.setLevel(log_level)
         expected_error_occurred = False
     except taskforce.http.HttpError as e:
         self.log.setLevel(log_level)
         self.log.info("%s Received expected error -- %s", my(self), str(e))
         expected_error_occurred = True
     assert expected_error_occurred
Пример #8
0
 def start_client(self, address, use_ssl=None):
     start = time.time()
     give_up = start + 10
     last_exc = None
     while time.time() < give_up:
         try:
             httpc = taskforce.http.Client(address=address, use_ssl=use_ssl, log=self.log)
             last_exc = None
             break
         except Exception as e:
             last_exc = e
             self.log.debug("%s Connection attempt failed after %s -- %s",
                             my(self), deltafmt(time.time() - start), str(e))
         time.sleep(0.5)
     if last_exc:
         self.log.error("%s Connection attempt failed after %s -- %s",
                         my(self), deltafmt(time.time() - start), str(e), exc_info=True)
         raise last_exc
     return httpc
Пример #9
0
    def Test_A_http_basics(self):
        #  Force the default address to one that we can create
        #
        taskforce.httpd.def_address = self.unx_address

        #  Start a service of the default address we just set
        #
        http_service = taskforce.httpd.HttpService()
        httpd = taskforce.httpd.server(http_service, log=self.log)

        #  Check operation with default address
        #
        httpc = taskforce.http.Client(log=self.log)
        self.log.info("%s Default address is: %s", my(self), httpc.address)
        assert isinstance(httpc, taskforce.http.Client)
        del httpc
        del httpd

        #  Check operation with invalid tcp port
        log_level = self.log.getEffectiveLevel()
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            httpc = taskforce.http.Client(address='nohost:noport', log=self.log)
            self.log.setLevel(log_level)
            expected_http_error_occurred = False
        except taskforce.http.HttpError as e:
            self.log.setLevel(log_level)
            self.log.info("%s Received expected connect error -- %s", my(self), str(e))
            expected_http_error_occurred = True
        assert expected_http_error_occurred

        #  Check operation with default non-ssl port
        #
        http_service = taskforce.httpd.HttpService()
        http_service.listen = self.lo_address
        httpd = taskforce.httpd.server(http_service, log=self.log)

        httpc = taskforce.http.Client(address=http_service.listen, log=self.log)
        self.log.info("%s Address with default port is: %s", my(self), httpc.address)
        assert isinstance(httpc, taskforce.http.Client)
        del httpc
        del httpd
Пример #10
0
 def start_client(self, address, use_ssl=None):
     start = time.time()
     give_up = start + 10
     last_exc = None
     while time.time() < give_up:
         try:
             httpc = taskforce.http.Client(address=address, use_ssl=use_ssl, log=self.log)
             last_exc = None
             break
         except Exception as e:
             last_exc = e
             self.log.debug("%s Connection attempt failed after %s -- %s",
                             my(self), deltafmt(time.time() - start), str(e))
         time.sleep(0.5)
     if last_exc:
         self.log.error("%s Connection attempt failed after %s -- %s",
                         my(self), deltafmt(time.time() - start), str(e), exc_info=True)
         raise last_exc
     return httpc
Пример #11
0
    def self_pipe(self):
        """
        A self-pipe is a convenient way of exercising some polling
    """
        self.poll_fd, self.poll_send = os.pipe()
        for fd in [self.poll_fd, self.poll_send]:
            fl = fcntl.fcntl(fd, fcntl.F_GETFL)
            fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)

        self.log.info("%s poll_fd = %d, poll_send = %d", my(self), self.poll_fd, self.poll_send)
        return (self.poll_fd, self.poll_send)
Пример #12
0
 def Test_D_tcp_get(self):
     self.log.info("Starting %s", my(self))
     gc.collect()
     http_service = taskforce.httpd.HttpService()
     http_service.listen = self.tcp_address
     httpd = taskforce.httpd.server(http_service, log=self.log)
     httpc = taskforce.http.Client(address=self.tcp_address, log=self.log)
     httpd.register_get(r'/test/.*', self.getter)
     self.do_get(httpc, httpd)
     httpd.close()
     del httpd
Пример #13
0
	def self_pipe(self):
		"""
		A self-pipe is a convenient way of exercising some polling
	"""
		self.poll_fd, self.poll_send = os.pipe()
		for fd in [self.poll_fd, self.poll_send]:
			fl = fcntl.fcntl(fd, fcntl.F_GETFL)
			fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)

		self.log.info("%s poll_fd = %d, poll_send = %d", my(self), self.poll_fd, self.poll_send)
		return (self.poll_fd, self.poll_send)
Пример #14
0
    def Test_E_unx_get(self):
        self.log.info("Starting %s", my(self))
        gc.collect()
        http_service = taskforce.httpd.HttpService()
        http_service.listen = self.unx_address
        httpd = taskforce.httpd.server(http_service, log=self.log)
        httpc = taskforce.http.Client(address=self.unx_address, log=self.log)
        httpd.register_get(r'/test/.*', self.getter)
        self.do_get(httpc, httpd)

        httpd.register_get(r'/bad/.*', self.bad_request)
        log_level = self.log.getEffectiveLevel()
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            self.do_get(httpc, httpd, path='/bad/path')
            self.log.setLevel(log_level)
            expected_get_error_occurred = False
        except taskforce.http.HttpError as e:
            self.log.setLevel(log_level)
            self.log.info("%s Received expected GET error -- %s", my(self), str(e))
            expected_get_error_occurred = True

        httpd.register_post(r'/bad/.*', self.bad_request)
        log_level = self.log.getEffectiveLevel()
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            self.do_post(httpc, httpd, '', path='/bad/path')
            self.log.setLevel(log_level)
            expected_post_error_occurred = False
        except taskforce.http.HttpError as e:
            self.log.setLevel(log_level)
            self.log.info("%s Received expected POST error -- %s", my(self), str(e))
            expected_post_error_occurred = True

        httpd.close()
        del httpd
        assert expected_get_error_occurred
        assert expected_post_error_occurred
Пример #15
0
    def Test_B_http_unx_status(self):

        httpc = self.start_tf(self.unx_address, use_ssl=None)

        #  Check the version info is sane
        resp = httpc.getmap('/status/version')
        self.log.info("Version info: %s", str(resp))
        assert 'taskforce' in resp

        #  Same, but use post
        resp = httpc.postmap('/status/version')
        self.log.info("Version info: %s", str(resp))
        assert 'taskforce' in resp

        #  Try a control operation that should be disabled on this path
        try:
            resp = httpc.post('/manage/control?db_server=off')
            assert "No exception on unauthorized control" is False
        except taskforce.http.HttpError as e:
            self.log.info("%s Expected exception on bad url: %s", my(self),
                          str(e))

        #  Again but with different arg layout
        try:
            resp = httpc.post('/manage/control', query={'db_server': 'off'})
            assert "No exception on unauthorized control" is False
        except taskforce.http.HttpError as e:
            self.log.info("%s Expected exception on bad url: %s", my(self),
                          str(e))

        #  Try an illegal URL
        try:
            resp = httpc.getmap('/')
            assert "No exception on bad url" is False
        except taskforce.http.HttpError as e:
            self.log.info("%s Expected exception on bad url: %s", my(self),
                          str(e))

        support.check_procsim_errors(self.__module__, env, log=self.log)
        self.stop_tf()
Пример #16
0
    def get_process_count(self, httpc, taskname, expect=None):
        count = None
        initial_delay = 0.5
        backoff_delay = 3.0
        max_delay = 8.0
        max_attempts = 10
        delay = initial_delay
        for attempt in range(max_attempts):
            self.log.debug("%s Attempt %d", my(self), attempt + 1)
            resp = httpc.getmap('/status/tasks')
            if taskname in resp and 'processes' in resp[taskname]:
                count = 0
                for proc in resp[taskname]['processes']:
                    if proc.get('pid'):
                        count += 1
                if attempt + 1 < max_attempts:
                    self.log.info(
                        "%s Task '%s' has %d process%s, expecting %s, next attempt in %s",
                        my(self), taskname, count, ses(count, 'es'), expect,
                        deltafmt(delay))
                else:
                    self.log.error(
                        "%s Task '%s' has %d process%s, expecting %s, giving up",
                        my(self), taskname, count, ses(count, 'es'), expect)
            else:
                count = None
                self.log.info("%s Task '%s' not found, expecting %s processes",
                              my(self), taskname, expect)
            if expect is None:
                self.log.info(
                    "%s Task '%s' has %d process%s with no expectation",
                    my(self), taskname, count, ses(count, 'es'))
                return count
            elif count == expect:
                self.log.info("%s Task '%s' has all %d expected process%s",
                              my(self), taskname, count, ses(count, 'es'))
                return count
            for tname in resp.keys():
                count = 0
                for proc in resp[tname]['processes']:
                    if proc.get('pid'):
                        count += 1
                self.log.debug("While waiting, task '%s' has %d process%s",
                               tname, count, ses(count, 'es'))
            self.log.debug("%s Next attempt in %s", my(self), deltafmt(delay))
            time.sleep(delay)
            delay += backoff_delay
            if delay > max_delay: delay = max_delay

        if count is None:
            raise Exception("No processes seen for task '%s', %d expected" %
                            (taskname, expected))
        else:
            raise Exception("Task '%s' has %d process%s, %d expected" %
                            (taskname, count, ses(count, 'es'), expect))
Пример #17
0
    def Test_J_post(self):
        self.log.info("Starting %s", my(self))
        gc.collect()
        http_service = taskforce.httpd.HttpService()
        http_service.listen = self.tcp_address
        httpd = taskforce.httpd.server(http_service, log=self.log)
        httpd.register_post(r'/test/.*', self.poster)

        body = urlencode({'data': json.dumps(self.http_test_map, indent=4)+'\n'})
        httpc = taskforce.http.Client(address=self.tcp_address)
        self.do_post(httpc, httpd, body, path='/test/json?hello=world')
        httpd.close()
        del httpd
Пример #18
0
 def Test_C_unx_https_connect(self):
     self.log.info("Starting %s", my(self))
     gc.collect()
     http_service = taskforce.httpd.HttpService()
     http_service.listen = self.unx_address
     http_service.certfile = env.cert_file
     httpd = taskforce.httpd.server(http_service, log=self.log)
     l = support.listeners(log=self.log)
     self.log.info("Service active, listening on %s", l.get(self.unx_address))
     assert self.unx_address in l
     del httpd
     l = support.listeners(log=self.log)
     self.log.info("Service deleted, listening on %s", l.get(self.unx_address))
     assert self.tcp_port not in l
Пример #19
0
    def Test_B_http_unx_status(self):

        httpc = self.start_tf(self.unx_address, use_ssl=None)

        #  Check the version info is sane
        resp = httpc.getmap('/status/version')
        self.log.info("Version info: %s", str(resp))
        assert 'taskforce' in resp

        #  Same, but use post
        resp = httpc.postmap('/status/version')
        self.log.info("Version info: %s", str(resp))
        assert 'taskforce' in resp

        #  Try a control operation that should be disabled on this path
        try:
            resp = httpc.post('/manage/control?db_server=off')
            assert "No exception on unauthorized control" is False
        except taskforce.http.HttpError as e:
            self.log.info("%s Expected exception on bad url: %s", my(self), str(e))

        #  Again but with different arg layout
        try:
            resp = httpc.post('/manage/control', query={'db_server': 'off'})
            assert "No exception on unauthorized control" is False
        except taskforce.http.HttpError as e:
            self.log.info("%s Expected exception on bad url: %s", my(self), str(e))

        #  Try an illegal URL
        try:
            resp = httpc.getmap('/')
            assert "No exception on bad url" is False
        except taskforce.http.HttpError as e:
            self.log.info("%s Expected exception on bad url: %s", my(self), str(e))

        support.check_procsim_errors(self.__module__, env, log=self.log)
        self.stop_tf()
Пример #20
0
    def get_process_count(self, httpc, taskname, expect=None):
        count = None
        initial_delay = 0.5
        backoff_delay = 3.0
        max_delay = 8.0
        max_attempts = 10
        delay = initial_delay
        for attempt in range(max_attempts):
            self.log.debug("%s Attempt %d", my(self), attempt+1)
            resp = httpc.getmap('/status/tasks')
            if taskname in resp and 'processes' in resp[taskname]:
                count = 0
                for proc in resp[taskname]['processes']:
                    if proc.get('pid'):
                        count += 1
                if attempt+1 < max_attempts:
                    self.log.info("%s Task '%s' has %d process%s, expecting %s, next attempt in %s",
                            my(self), taskname, count, ses(count, 'es'), expect, deltafmt(delay))
                else:
                    self.log.error("%s Task '%s' has %d process%s, expecting %s, giving up",
                            my(self), taskname, count, ses(count, 'es'), expect)
            else:
                count = None
                self.log.info("%s Task '%s' not found, expecting %s processes", my(self), taskname, expect)
            if expect is None:
                self.log.info("%s Task '%s' has %d process%s with no expectation",
                            my(self), taskname, count, ses(count, 'es'))
                return count
            elif count == expect:
                self.log.info("%s Task '%s' has all %d expected process%s",
                            my(self), taskname, count, ses(count, 'es'))
                return count
            for tname in resp.keys():
                count = 0
                for proc in resp[tname]['processes']:
                    if proc.get('pid'):
                        count += 1
                self.log.debug("While waiting, task '%s' has %d process%s", tname, count, ses(count, 'es'))
            self.log.debug("%s Next attempt in %s", my(self), deltafmt(delay))
            time.sleep(delay)
            delay += backoff_delay
            if delay > max_delay: delay = max_delay

        if count is None:
            raise Exception("No processes seen for task '%s', %d expected" % (taskname, expected))
        else:
            raise Exception("Task '%s' has %d process%s, %d expected" % (taskname, count, ses(count, 'es'), expect))
Пример #21
0
 def Test_L_cmp(self):
     """
     Test that system won't attempt to unlink an existing unx path that is not a socket
     and won't successfully create a socket anyway.
 """
     http_service = taskforce.httpd.HttpService()
     http_service.listen = env.temp_dir
     log_level = self.log.getEffectiveLevel()
     try:
         #  Mask the log message as we expect a failure
         self.log.setLevel(logging.CRITICAL)
         httpd = taskforce.httpd.server(http_service, log=self.log)
         self.log.setLevel(log_level)
         expected_error_occurred = False
     except Exception as e:
         self.log.setLevel(log_level)
         self.log.info("%s Received expected error -- %s", my(self), str(e))
         expected_error_occurred = True
     assert expected_error_occurred
Пример #22
0
 def __init__(self):
     here = my()
     support.logger().info("%s at class init level", here)
     assert here.startswith('my_class_init()')
Пример #23
0
    def Test_C_https_unx_status(self):

        httpc = self.start_tf(self.unx_address, use_ssl=False, allow_control=True)

        #  Check the version info is sane
        resp = httpc.getmap('/status/version')
        self.log.info("Version info: %s", str(resp))
        assert 'taskforce' in resp
        assert 'platform' in resp
        assert 'release' in resp['platform']

        #  Allow some process startup time
        time.sleep(2)

        taskname = 'ws_server'
        initial_ws_server_count = self.get_process_count(httpc, taskname)
        if initial_ws_server_count != self.expected_ws_server_count:
            self.log.info("%s %d or %d expected %s processs at startup",
                    my(self), initial_ws_server_count, self.expected_ws_server_count, taskname)


        #  Send an invalid management path
        log_level = self.log.getEffectiveLevel()
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            resp = httpc.get('/manage/nosuchpath')
            self.log.setLevel(log_level)
            assert "No exception on bad management path" is False
        except taskforce.http.HttpError as e:
            self.log.setLevel(log_level)
            self.log.info("%s Expected exception on bad management path: %s", my(self), str(e))

        #  Send a now-valid command but have it expect a JSON return, which doesn't happen
        log_level = self.log.getEffectiveLevel()
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            resp = httpc.postmap('/manage/control', valuemap={'db_server': 'off'})
            self.log.setLevel(log_level)
            assert "No exception on bad JSON return" is False
        except taskforce.http.HttpError as e:
            self.log.setLevel(log_level)
            self.log.info("%s Expected exception on bad url: %s", my(self), str(e))

        #  Repeat the last change to test the no-change response
        (off_code, content, content_type) = httpc.post('/manage/control', valuemap={'db_server': 'off'})
        self.log.info('%s repeat-off response info: %d %s "%s"', taskname, off_code, content_type, content.strip())
        assert off_code == 200
        assert content.strip().endswith('no change')

        #  Turn task back on.
        (wait_code, content, content_type) = httpc.post('/manage/control', valuemap={'db_server': 'wait'})
        self.log.info('%s restart response info: %d %s "%s"', taskname, wait_code, content_type, content.strip())
        assert wait_code == 202
        assert content.strip().endswith('ok')

        #  Send a control to an unknown task
        log_level = self.log.getEffectiveLevel()
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            resp = httpc.postmap('/manage/control', valuemap={'no_such_task': 'off'})
            self.log.setLevel(log_level)
            assert "No exception on bad task name" is False
        except taskforce.http.HttpError as e:
            self.log.setLevel(log_level)
            self.log.info("%s Expected exception on bad task name: %s", my(self), str(e))

        #  Send an invalid control to an known task
        log_level = self.log.getEffectiveLevel()
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            resp = httpc.postmap('/manage/control', valuemap={'db_server': 'no_such_control'})
            self.log.setLevel(log_level)
            assert "No exception on bad control name" is False
        except taskforce.http.HttpError as e:
            self.log.setLevel(log_level)
            self.log.info("%s Expected exception on bad control name: %s", my(self), str(e))

        prior_ws_server_count = self.get_process_count(httpc, taskname, self.expected_ws_server_count)
        assert prior_ws_server_count == self.expected_ws_server_count

        #  Change the count
        #
        new_count = 2
        (count_code, content, content_type) = httpc.get('/manage/count?%s=%d' % (taskname, new_count))
        self.log.info('%s count response info: %d %s "%s"', taskname, count_code, content_type, content.strip())
        assert count_code < 300

        ws_server_count = self.get_process_count(httpc, taskname, new_count)
        assert ws_server_count == new_count

        #  Repeat the last change to test the no-change response
        (count_code, content, content_type) = httpc.get('/manage/count?%s=%d' % (taskname, new_count))
        self.log.info('%s repeat-count response info: %d %s "%s"', taskname, off_code, content_type, content.strip())
        assert off_code == 200
        assert content.strip().endswith('no change')

        #  Send a count to an unknown task
        log_level = self.log.getEffectiveLevel()
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            resp = httpc.postmap('/manage/count', valuemap={'no_such_task': '1'})
            self.log.setLevel(log_level)
            assert "No exception on bad task name for count" is False
        except taskforce.http.HttpError as e:
            self.log.setLevel(log_level)
            self.log.info("%s Expected exception on bad task name for count: %s", my(self), str(e))

        #  Send an invalid count
        log_level = self.log.getEffectiveLevel()
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            resp = httpc.get('/manage/count?%s=nonumberhere' % (taskname, ))
            self.log.setLevel(log_level)
            self.log.error("%s Invalid bad count response: %s", my(self), str(resp))
            assert "No exception on bad count" is False
        except taskforce.http.HttpError as e:
            self.log.setLevel(log_level)
            self.log.info("%s Expected exception on bad count value: %s", my(self), str(e))

        #  Send a zero count
        log_level = self.log.getEffectiveLevel()
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            resp = httpc.post('/manage/count?%s=%d' % (taskname, 0, ))
            self.log.setLevel(log_level)
            self.log.error("%s Invalid zero count response: %s", my(self), str(resp))
            assert "No exception on zero count" is False
        except taskforce.http.HttpError as e:
            self.log.setLevel(log_level)
            self.log.info("%s Expected exception on zero count: %s", my(self), str(e))

        #  Send a negative count
        log_level = self.log.getEffectiveLevel()
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            resp = httpc.post('/manage/count', valuemap={taskname: '-42'})
            self.log.setLevel(log_level)
            self.log.error("%s Invalid negative count response: %s", my(self), str(resp))
            assert "No exception on negative count" is False
        except taskforce.http.HttpError as e:
            self.log.setLevel(log_level)
            self.log.info("%s Expected exception on negative count: %s", my(self), str(e))

        #  Reload config to put it back
        (reload_code, content, content_type) = httpc.get('/manage/reload')
        self.log.info('%s reload response info: %d %s "%s"', taskname, count_code, content_type, content.strip())
        assert reload_code == 202
        assert content.strip().endswith('reload initiated')

        ws_server_count = self.get_process_count(httpc, taskname, self.expected_ws_server_count)
        assert ws_server_count == self.expected_ws_server_count

        (reset_code, content, content_type) = httpc.get('/manage/reset')
        self.log.info('%s reset response info: %d %s "%s"', taskname, count_code, content_type, content.strip())
        assert reset_code < 300
        assert content.strip().endswith('reset initiated')

        time.sleep(2)

        #  Reconnect to service and wait for the reset to complete then check that the version info is sane.
        #
        resp = None
        httpc = None
        for attempt in range(30):
            try:
                httpc = self.start_client(self.unx_address, use_ssl=False)
                resp = httpc.getmap('/status/version')
                break
            except taskforce.http.HttpError as e:
                self.log.error("%s HTTP exception while waiting for reset to complete on attempt %d -- %s",
                        my(self), attempt+1, str(e))
                break
            except Exception as e:
                self.log.info("%s Error waiting or reset to complete on attempt %d -- %s",
                        my(self), attempt+1, str(e), exc_info=True)
            time.sleep(0.5)

        self.log.info("Version info: %s", str(resp))
        assert 'taskforce' in resp

        try:
            (stop_code, content, content_type) = httpc.get('/manage/stop')
            self.log.info('%s stop response info: %d %s "%s"', taskname, count_code, content_type, content.strip())
            assert stop_code < 300
            assert content.strip().endswith('exit initiated')
        except (taskforce.http.BadStatusLine, taskforce.http.HttpError) as e:
            self.log.info('%s Expected possible error from stop manage -- %s', my(self), str(e))
        except socket.error as e:
            if e.errno == errno.ECONNRESET:
                self.log.info('%s Expected possible socket error from stop manage -- %s', my(self), str(e))
            else:
                raise e

        support.check_procsim_errors(self.__module__, env, log=self.log)
        self.stop_tf()
Пример #24
0
    def Test_A_https_tcp_status(self):

        httpc = self.start_tf(self.tcp_address, use_ssl=False)

        #  Check the version info is sane
        resp = httpc.getmap('/status/version')
        self.log.info("Version info: %s", str(resp))
        assert 'taskforce' in resp
        assert 'platform' in resp
        #  This is not a control path, so the os release and platform should be hidden
        assert 'release' not in resp['platform']
        assert 'platform' not in resp['platform']

        #  Try a bogus format
        try:
            resp = httpc.getmap('/status/version?indent=4&fmt=xml')
            assert "No 'version' exception on bad 'fmt'" is False
        except taskforce.http.HttpError as e:
            self.log.info("%s Expected 'version' exception on bad format: %s", my(self), str(e))

        give_up = time.time() + 30
        toi = 'db_server'
        toi_started = None
        while time.time() < give_up:
            resp = httpc.getmap('/status/tasks')
            self.log.debug('Resp %s', json.dumps(resp, indent=4))
            if toi in resp:
                if 'processes' in resp[toi] and len(resp[toi]['processes']) > 0:
                    if 'started_t' in resp[toi]['processes'][0]:
                        toi_started = resp[toi]['processes'][0]['started_t']
                        self.log.info("%s Task of interest '%s' started %s ago",
                                my(self), toi, deltafmt(time.time() - toi_started))
                        break
                    else:
                        self.log.info("%s Task of interest '%s' is has procs", my(self), toi)
                else:
                    self.log.info("%s Task of interest '%s' is known", my(self), toi)
            time.sleep(9)

        #  Try a bogus format
        try:
            resp = httpc.getmap('/status/tasks?indent=4&fmt=xml')
            assert "No 'tasks' exception on bad 'fmt'" is False
        except taskforce.http.HttpError as e:
            self.log.info("%s Expected 'tasks' exception on bad format: %s", my(self), str(e))

        #  Check the config info is sane
        resp = httpc.getmap('/status/config?pending=0')
        assert 'tasks' in resp

        #  Try a bogus format
        try:
            resp = httpc.getmap('/status/config?indent=4&fmt=xml')
            assert "No 'config' exception on bad 'fmt'" is False
        except taskforce.http.HttpError as e:
            self.log.info("%s Expected 'config' exception on bad format: %s", my(self), str(e))

        support.check_procsim_errors(self.__module__, env, log=self.log)
        self.stop_tf()

        assert toi_started is not None
Пример #25
0
    def Test_B_register(self):
        log_level = self.log.getEffectiveLevel()
        poll = taskforce.poll.poll()
        currmode = poll.get_mode()
        self.log.info("%s Default polling mode is '%s' of %s",
                        my(self), poll.get_mode_name(mode=currmode), str(poll.get_available_mode_names()))

        #  Find a valid mode that is not the current mode
        #
        nextmode = None
        for mode in poll.get_available_modes():
            if mode != currmode:
                nextmode = mode
        self.log.info("%s Determined valid non-active mode as %s", my(self), poll.get_mode_name(mode=nextmode))

        poll_fd, poll_send = self.self_pipe()
        poll.register(poll_fd)

        #  Test that an attempt to change mode is rejected
        #
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            poll.set_mode(nextmode)
            self.log.setLevel(log_level)
            expected_error_occurred = False
        except Exception as e:
            self.log.setLevel(log_level)
            self.log.info("%s Received expected error -- %s", my(self), str(e))
            expected_error_occurred = True
        assert expected_error_occurred

        #  Test that an attempt to register an invalid fd fails
        #
        inv_fd = 999

        #  Make sure it is invalid
        try: os.close(inv_fd)
        except: pass
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            poll.register(inv_fd)
            self.log.setLevel(log_level)
            expected_error_occurred = False
        except Exception as e:
            self.log.setLevel(log_level)
            self.log.info("%s Received expected invalid fd error -- %s", my(self), str(e))
            expected_error_occurred = True
        assert expected_error_occurred

        #  Confirm new mode is same as previous
        poll = taskforce.poll.poll()
        mode = poll.get_mode()
        self.log.info("%s Default polling mode is '%s' and should be same as previous '%s'",
                        my(self), poll.get_mode_name(mode=mode), poll.get_mode_name(mode=currmode))

        #  Change to PL_SELECT and register
        poll.set_mode(taskforce.poll.PL_SELECT)
        assert poll.get_mode() == taskforce.poll.PL_SELECT
        poll.register(poll_fd)

        #  Check invalid unregister
        #
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            poll.unregister(self)
            self.log.setLevel(log_level)
            expected_error_occurred = False
        except Exception as e:
            self.log.setLevel(log_level)
            self.log.info("%s Received expected error -- %s", my(self), str(e))
            expected_error_occurred = True
        assert expected_error_occurred

        #  Check valid unregister
        #
        poll.unregister(poll_fd)

        self.close_pipe()
Пример #26
0
    def Test_G_connect_to_nothing(self):
        log_level = self.log.getEffectiveLevel()

        #  Test get_query() convenience function
        #
        element_path = 'http://' + self.lo_address + '/path?element=value'
        qdict = taskforce.httpd.get_query(element_path)
        self.log.info("%s get_query() returned: %s", my(self), str(qdict))
        assert 'element' in qdict

        qdict = taskforce.httpd.get_query(element_path, force_unicode=False)
        self.log.info("%s get_query() returned: %s", my(self), str(qdict))
        assert 'element' in qdict

        #  Try to create service with a non-integer port
        #
        http_service = taskforce.httpd.HttpService()
        http_service.listen = self.tcp_address + ':not_an_int'
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            httpd = taskforce.httpd.server(http_service, log=self.log)
            self.log.setLevel(log_level)
            expected_tcp_port_error_occurred = False
        except Exception as e:
            self.log.setLevel(log_level)
            self.log.info("%s Received expected tcp port error -- %s", my(self), str(e))
            expected_tcp_port_error_occurred = True
        assert expected_tcp_port_error_occurred

        #  Try tcp and unx connections with and without ssl, with nothing listening
        #
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            httpc = taskforce.http.Client(address='localhost', log=self.log)
            self.log.setLevel(log_level)
            expected_error_occurred = False
        except Exception as e:
            self.log.setLevel(log_level)
            self.log.info("%s Received expected error -- %s", my(self), str(e))
            expected_error_occurred = True
        assert expected_error_occurred

        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            httpc = taskforce.http.Client(address='/tmp/no/such/socket', log=self.log)
            self.log.setLevel(log_level)
            expected_error_occurred = False
        except Exception as e:
            self.log.setLevel(log_level)
            self.log.info("%s Received expected error -- %s", my(self), str(e))
            expected_error_occurred = True
        assert expected_error_occurred

        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            httpc = taskforce.http.Client(address=self.lo_address, use_ssl=False, log=self.log)
            self.log.setLevel(log_level)
            expected_error_occurred = False
        except Exception as e:
            self.log.setLevel(log_level)
            self.log.info("%s Received expected error -- %s", my(self), str(e))
            expected_error_occurred = True
        assert expected_error_occurred

        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            httpc = taskforce.http.Client(address='/tmp/no/such/socket', use_ssl=False, log=self.log)
            self.log.setLevel(log_level)
            expected_error_occurred = False
        except Exception as e:
            self.log.setLevel(log_level)
            self.log.info("%s Received expected error -- %s", my(self), str(e))
            expected_error_occurred = True
        assert expected_error_occurred
Пример #27
0
def my_module_level():
    here = my()
    support.logger().info("%s at module level", here)
    assert here.startswith('test_01_utils.my_module_level()')
Пример #28
0
	def Test_B_register(self):
		log_level = self.log.getEffectiveLevel()
		poll = taskforce.poll.poll()
		currmode = poll.get_mode()
		self.log.info("%s Default polling mode is '%s' of %s",
						my(self), poll.get_mode_name(mode=currmode), str(poll.get_available_mode_names()))

		#  Find a valid mode that is not the current mode
		#
		nextmode = None
		for mode in poll.get_available_modes():
			if mode != currmode:
				nextmode = mode
		self.log.info("%s Determined valid non-active mode as %s", my(self), poll.get_mode_name(mode=nextmode))

		poll_fd, poll_send = self.self_pipe()
		poll.register(poll_fd)

		#  Test that an attempt to change mode is rejected
		#
		try:
			#  Mask the log message as we expect a failure
			self.log.setLevel(logging.CRITICAL)
			poll.set_mode(nextmode)
			self.log.setLevel(log_level)
			expected_error_occurred = False
		except Exception as e:
			self.log.setLevel(log_level)
			self.log.info("%s Received expected error -- %s", my(self), str(e))
			expected_error_occurred = True
		assert expected_error_occurred

		#  Test that an attempt to register an invalid fd fails
		#
		inv_fd = 999

		#  Make sure it is invalid
		try: os.close(inv_fd)
		except: pass
		try:
			#  Mask the log message as we expect a failure
			self.log.setLevel(logging.CRITICAL)
			poll.register(inv_fd)
			self.log.setLevel(log_level)
			expected_error_occurred = False
		except Exception as e:
			self.log.setLevel(log_level)
			self.log.info("%s Received expected invalid fd error -- %s", my(self), str(e))
			expected_error_occurred = True
		assert expected_error_occurred

		#  Confirm new mode is same as previous
		poll = taskforce.poll.poll()
		mode = poll.get_mode()
		self.log.info("%s Default polling mode is '%s' and should be same as previous '%s'",
						my(self), poll.get_mode_name(mode=mode), poll.get_mode_name(mode=currmode))

		#  Change to PL_SELECT and register
		poll.set_mode(taskforce.poll.PL_SELECT)
		assert poll.get_mode() == taskforce.poll.PL_SELECT
		poll.register(poll_fd)

		#  Check invalid unregister
		#
		try:
			#  Mask the log message as we expect a failure
			self.log.setLevel(logging.CRITICAL)
			poll.unregister(self)
			self.log.setLevel(log_level)
			expected_error_occurred = False
		except Exception as e:
			self.log.setLevel(log_level)
			self.log.info("%s Received expected error -- %s", my(self), str(e))
			expected_error_occurred = True
		assert expected_error_occurred

		#  Check valid unregister
		#
		poll.unregister(poll_fd)

		self.close_pipe()
Пример #29
0
    def Test_A_mode(self):
        log_level = self.log.getEffectiveLevel()

        poll = taskforce.poll.poll()
        mode = poll.get_mode()
        allowed_modes = poll.get_available_modes()
        self.log.info("%s Default polling mode is '%s' of %s",
                        my(self), poll.get_mode_name(mode=mode), str(poll.get_available_mode_names()))

        #  get_mode_name() should always return a string
        #
        name = poll.get_mode_name(mode='junk')
        self.log.info("%s get_mode_name() response to invalid mode %s", my(self), name)
        assert type(name) is str

        #  Format multiple events
        #
        evtext = poll.get_event(taskforce.poll.POLLIN|taskforce.poll.POLLOUT)
        self.log.info("%s get_event() response to multiple events %s", my(self), evtext)
        assert type(name) is str

        #  Format bad events
        #
        evtext = poll.get_event(taskforce.poll.POLLIN|taskforce.poll.POLLOUT|0x800)
        self.log.info("%s get_event() response to multiple events %s", my(self), evtext)
        assert type(name) is str

        #  Invalid event input
        #
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            poll.get_event(None)
            self.log.setLevel(log_level)
            expected_error_occurred = False
        except Exception as e:
            self.log.setLevel(log_level)
            self.log.info("%s Received expected invalid event error -- %s", my(self), str(e))
            expected_error_occurred = True
        assert expected_error_occurred

        #  Should always be able to force PL_SELECT
        poll.set_mode(taskforce.poll.PL_SELECT)
        assert poll.get_mode() == taskforce.poll.PL_SELECT

        #  Find a mode that is not available
        bad_mode = None
        for mode in known_polling_modes:
            if mode not in allowed_modes:
                bad_mode = mode
                break
        self.log.info("%s Determined unavailable mode as %s", my(self), poll.get_mode_name(mode=bad_mode))

        #  Check that we can't set mode to None
        #
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            poll.set_mode(None)
            self.log.setLevel(log_level)
            expected_error_occurred = False
        except Exception as e:
            self.log.setLevel(log_level)
            self.log.info("%s Received expected error -- %s", my(self), str(e))
            expected_error_occurred = True
        assert expected_error_occurred

        #  Check that we can't set mode to an impossible value
        #
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            poll.set_mode(-1)
            self.log.setLevel(log_level)
            expected_error_occurred = False
        except Exception as e:
            self.log.setLevel(log_level)
            self.log.info("%s Received expected error -- %s", my(self), str(e))
            expected_error_occurred = True
        assert expected_error_occurred

        #  Check that we can't set an unavailable mode
        #
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            poll.set_mode(bad_mode)
            self.log.setLevel(log_level)
            expected_error_occurred = False
        except Exception as e:
            self.log.setLevel(log_level)
            self.log.info("%s Received expected error -- %s", my(self), str(e))
            expected_error_occurred = True
        assert expected_error_occurred
Пример #30
0
	def Test_A_mode(self):
		log_level = self.log.getEffectiveLevel()

		poll = taskforce.poll.poll()
		mode = poll.get_mode()
		allowed_modes = poll.get_available_modes()
		self.log.info("%s Default polling mode is '%s' of %s",
						my(self), poll.get_mode_name(mode=mode), str(poll.get_available_mode_names()))

		#  get_mode_name() should always return a string
		#
		name = poll.get_mode_name(mode='junk')
		self.log.info("%s get_mode_name() response to invalid mode %s", my(self), name)
		assert type(name) is str

		#  Format multiple events
		#
		evtext = poll.get_event(taskforce.poll.POLLIN|taskforce.poll.POLLOUT)
		self.log.info("%s get_event() response to multiple events %s", my(self), evtext)
		assert type(name) is str

		#  Format bad events
		#
		evtext = poll.get_event(taskforce.poll.POLLIN|taskforce.poll.POLLOUT|0x800)
		self.log.info("%s get_event() response to multiple events %s", my(self), evtext)
		assert type(name) is str

		#  Invalid event input
		#
		try:
			#  Mask the log message as we expect a failure
			self.log.setLevel(logging.CRITICAL)
			poll.get_event(None)
			self.log.setLevel(log_level)
			expected_error_occurred = False
		except Exception as e:
			self.log.setLevel(log_level)
			self.log.info("%s Received expected invalid event error -- %s", my(self), str(e))
			expected_error_occurred = True
		assert expected_error_occurred

		#  Should always be able to force PL_SELECT
		poll.set_mode(taskforce.poll.PL_SELECT)
		assert poll.get_mode() == taskforce.poll.PL_SELECT

		#  Find a mode that is not available
		bad_mode = None
		for mode in known_polling_modes:
			if mode not in allowed_modes:
				bad_mode = mode
				break
		self.log.info("%s Determined unavailable mode as %s", my(self), poll.get_mode_name(mode=bad_mode))

		#  Check that we can't set mode to None
		#
		try:
			#  Mask the log message as we expect a failure
			self.log.setLevel(logging.CRITICAL)
			poll.set_mode(None)
			self.log.setLevel(log_level)
			expected_error_occurred = False
		except Exception as e:
			self.log.setLevel(log_level)
			self.log.info("%s Received expected error -- %s", my(self), str(e))
			expected_error_occurred = True
		assert expected_error_occurred

		#  Check that we can't set mode to an impossible value
		#
		try:
			#  Mask the log message as we expect a failure
			self.log.setLevel(logging.CRITICAL)
			poll.set_mode(-1)
			self.log.setLevel(log_level)
			expected_error_occurred = False
		except Exception as e:
			self.log.setLevel(log_level)
			self.log.info("%s Received expected error -- %s", my(self), str(e))
			expected_error_occurred = True
		assert expected_error_occurred

		#  Check that we can't set an unavailable mode
		#
		try:
			#  Mask the log message as we expect a failure
			self.log.setLevel(logging.CRITICAL)
			poll.set_mode(bad_mode)
			self.log.setLevel(log_level)
			expected_error_occurred = False
		except Exception as e:
			self.log.setLevel(log_level)
			self.log.info("%s Received expected error -- %s", my(self), str(e))
			expected_error_occurred = True
		assert expected_error_occurred
Пример #31
0
    def Test_C_https_unx_status(self):

        httpc = self.start_tf(self.unx_address,
                              use_ssl=False,
                              allow_control=True)

        #  Check the version info is sane
        resp = httpc.getmap('/status/version')
        self.log.info("Version info: %s", str(resp))
        assert 'taskforce' in resp
        assert 'platform' in resp
        assert 'release' in resp['platform']

        #  Allow some process startup time
        time.sleep(2)

        taskname = 'ws_server'
        initial_ws_server_count = self.get_process_count(httpc, taskname)
        if initial_ws_server_count != self.expected_ws_server_count:
            self.log.info("%s %d or %d expected %s processs at startup",
                          my(self), initial_ws_server_count,
                          self.expected_ws_server_count, taskname)

        #  Send an invalid management path
        log_level = self.log.getEffectiveLevel()
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            resp = httpc.get('/manage/nosuchpath')
            self.log.setLevel(log_level)
            assert "No exception on bad management path" is False
        except taskforce.http.HttpError as e:
            self.log.setLevel(log_level)
            self.log.info("%s Expected exception on bad management path: %s",
                          my(self), str(e))

        #  Send a now-valid command but have it expect a JSON return, which doesn't happen
        log_level = self.log.getEffectiveLevel()
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            resp = httpc.postmap('/manage/control',
                                 valuemap={'db_server': 'off'})
            self.log.setLevel(log_level)
            assert "No exception on bad JSON return" is False
        except taskforce.http.HttpError as e:
            self.log.setLevel(log_level)
            self.log.info("%s Expected exception on bad url: %s", my(self),
                          str(e))

        #  Repeat the last change to test the no-change response
        (off_code, content,
         content_type) = httpc.post('/manage/control',
                                    valuemap={'db_server': 'off'})
        self.log.info('%s repeat-off response info: %d %s "%s"', taskname,
                      off_code, content_type, content.strip())
        assert off_code == 200
        assert content.strip().endswith('no change')

        #  Turn task back on.
        (wait_code, content,
         content_type) = httpc.post('/manage/control',
                                    valuemap={'db_server': 'wait'})
        self.log.info('%s restart response info: %d %s "%s"', taskname,
                      wait_code, content_type, content.strip())
        assert wait_code == 202
        assert content.strip().endswith('ok')

        #  Send a control to an unknown task
        log_level = self.log.getEffectiveLevel()
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            resp = httpc.postmap('/manage/control',
                                 valuemap={'no_such_task': 'off'})
            self.log.setLevel(log_level)
            assert "No exception on bad task name" is False
        except taskforce.http.HttpError as e:
            self.log.setLevel(log_level)
            self.log.info("%s Expected exception on bad task name: %s",
                          my(self), str(e))

        #  Send an invalid control to an known task
        log_level = self.log.getEffectiveLevel()
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            resp = httpc.postmap('/manage/control',
                                 valuemap={'db_server': 'no_such_control'})
            self.log.setLevel(log_level)
            assert "No exception on bad control name" is False
        except taskforce.http.HttpError as e:
            self.log.setLevel(log_level)
            self.log.info("%s Expected exception on bad control name: %s",
                          my(self), str(e))

        prior_ws_server_count = self.get_process_count(
            httpc, taskname, self.expected_ws_server_count)
        assert prior_ws_server_count == self.expected_ws_server_count

        #  Change the count
        #
        new_count = 2
        (count_code, content, content_type) = httpc.get('/manage/count?%s=%d' %
                                                        (taskname, new_count))
        self.log.info('%s count response info: %d %s "%s"', taskname,
                      count_code, content_type, content.strip())
        assert count_code < 300

        ws_server_count = self.get_process_count(httpc, taskname, new_count)
        assert ws_server_count == new_count

        #  Repeat the last change to test the no-change response
        (count_code, content, content_type) = httpc.get('/manage/count?%s=%d' %
                                                        (taskname, new_count))
        self.log.info('%s repeat-count response info: %d %s "%s"', taskname,
                      off_code, content_type, content.strip())
        assert off_code == 200
        assert content.strip().endswith('no change')

        #  Send a count to an unknown task
        log_level = self.log.getEffectiveLevel()
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            resp = httpc.postmap('/manage/count',
                                 valuemap={'no_such_task': '1'})
            self.log.setLevel(log_level)
            assert "No exception on bad task name for count" is False
        except taskforce.http.HttpError as e:
            self.log.setLevel(log_level)
            self.log.info(
                "%s Expected exception on bad task name for count: %s",
                my(self), str(e))

        #  Send an invalid count
        log_level = self.log.getEffectiveLevel()
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            resp = httpc.get('/manage/count?%s=nonumberhere' % (taskname, ))
            self.log.setLevel(log_level)
            self.log.error("%s Invalid bad count response: %s", my(self),
                           str(resp))
            assert "No exception on bad count" is False
        except taskforce.http.HttpError as e:
            self.log.setLevel(log_level)
            self.log.info("%s Expected exception on bad count value: %s",
                          my(self), str(e))

        #  Send a zero count
        log_level = self.log.getEffectiveLevel()
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            resp = httpc.post('/manage/count?%s=%d' % (
                taskname,
                0,
            ))
            self.log.setLevel(log_level)
            self.log.error("%s Invalid zero count response: %s", my(self),
                           str(resp))
            assert "No exception on zero count" is False
        except taskforce.http.HttpError as e:
            self.log.setLevel(log_level)
            self.log.info("%s Expected exception on zero count: %s", my(self),
                          str(e))

        #  Send a negative count
        log_level = self.log.getEffectiveLevel()
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            resp = httpc.post('/manage/count', valuemap={taskname: '-42'})
            self.log.setLevel(log_level)
            self.log.error("%s Invalid negative count response: %s", my(self),
                           str(resp))
            assert "No exception on negative count" is False
        except taskforce.http.HttpError as e:
            self.log.setLevel(log_level)
            self.log.info("%s Expected exception on negative count: %s",
                          my(self), str(e))

        #  Reload config to put it back
        (reload_code, content, content_type) = httpc.get('/manage/reload')
        self.log.info('%s reload response info: %d %s "%s"', taskname,
                      count_code, content_type, content.strip())
        assert reload_code == 202
        assert content.strip().endswith('reload initiated')

        ws_server_count = self.get_process_count(httpc, taskname,
                                                 self.expected_ws_server_count)
        assert ws_server_count == self.expected_ws_server_count

        (reset_code, content, content_type) = httpc.get('/manage/reset')
        self.log.info('%s reset response info: %d %s "%s"', taskname,
                      count_code, content_type, content.strip())
        assert reset_code < 300
        assert content.strip().endswith('reset initiated')

        time.sleep(2)

        #  Reconnect to service and wait for the reset to complete then check that the version info is sane.
        #
        resp = None
        httpc = None
        for attempt in range(30):
            try:
                httpc = self.start_client(self.unx_address, use_ssl=False)
                resp = httpc.getmap('/status/version')
                break
            except taskforce.http.HttpError as e:
                self.log.error(
                    "%s HTTP exception while waiting for reset to complete on attempt %d -- %s",
                    my(self), attempt + 1, str(e))
                break
            except Exception as e:
                self.log.info(
                    "%s Error waiting or reset to complete on attempt %d -- %s",
                    my(self),
                    attempt + 1,
                    str(e),
                    exc_info=True)
            time.sleep(0.5)

        self.log.info("Version info: %s", str(resp))
        assert 'taskforce' in resp

        try:
            (stop_code, content, content_type) = httpc.get('/manage/stop')
            self.log.info('%s stop response info: %d %s "%s"', taskname,
                          count_code, content_type, content.strip())
            assert stop_code < 300
            assert content.strip().endswith('exit initiated')
        except (taskforce.http.BadStatusLine, taskforce.http.HttpError) as e:
            self.log.info('%s Expected possible error from stop manage -- %s',
                          my(self), str(e))
        except socket.error as e:
            if e.errno == errno.ECONNRESET:
                self.log.info(
                    '%s Expected possible socket error from stop manage -- %s',
                    my(self), str(e))
            else:
                raise e

        support.check_procsim_errors(self.__module__, env, log=self.log)
        self.stop_tf()
Пример #32
0
    def Test_L_pidclaim(self):
        args = list(sys.argv)
        args.pop(0)
        pidfile = './%s-%s.pid' % (utils.appname(), env.edition)

        #  Run the test as a forked process so we can test for
        #  pid file creation and removal.
        #
        start = time.time()
        pid = os.fork()
        if pid == 0:
            args = ['pidclaim']
            self.set_path('PYTHONPATH', env.base_dir)
            if 'NOSE_WITH_COVERAGE' in os.environ:
                exe = 'coverage'
                args.append('run')
            else:
                exe = 'python'
            args.extend(['tests/scripts/pidclaim', pidfile])
            self.log.info("%s child, running '%s' %s", my(self), exe, args)
            os.execvp(exe, args)
        else:
            time.sleep(1)
            self.log.info("Child PID is: %d", pid)
            with open(pidfile, 'r') as f:
                claim_pid = int(f.readline().strip())
                self.log.info("PID read back as: %d", claim_pid)
            assert claim_pid == pid
            (wpid, status) = os.wait()
            self.log.info("Child ran %s", utils.deltafmt(time.time() - start, decimals=3))
            self.log.info("Child %s", utils.statusfmt(status))
            assert status == 0
            assert not os.path.exists(pidfile)

        #  Bad pid param
        #
        log_level = self.log.getEffectiveLevel()
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            utils.pidclaim(pid='abc')
            self.log.setLevel(log_level)
            expected_error_occurred = False
        except Exception as e:
            self.log.setLevel(log_level)
            self.log.info("%s Received expected bad pid error -- %s", my(self), str(e))
            expected_error_occurred = True
        assert expected_error_occurred

        #  Invalid pid param
        #
        log_level = self.log.getEffectiveLevel()
        try:
            #  Mask the log message as we expect a failure
            self.log.setLevel(logging.CRITICAL)
            utils.pidclaim(pid=0)
            self.log.setLevel(log_level)
            expected_error_occurred = False
        except Exception as e:
            self.log.setLevel(log_level)
            self.log.info("%s Received expected invalid pid error -- %s", my(self), str(e))
            expected_error_occurred = True
        assert expected_error_occurred
Пример #33
0
    def Test_A_https_tcp_status(self):

        httpc = self.start_tf(self.tcp_address, use_ssl=False)

        #  Check the version info is sane
        resp = httpc.getmap('/status/version')
        self.log.info("Version info: %s", str(resp))
        assert 'taskforce' in resp
        assert 'platform' in resp
        #  This is not a control path, so the os release and platform should be hidden
        assert 'release' not in resp['platform']
        assert 'platform' not in resp['platform']

        #  Try a bogus format
        try:
            resp = httpc.getmap('/status/version?indent=4&fmt=xml')
            assert "No 'version' exception on bad 'fmt'" is False
        except taskforce.http.HttpError as e:
            self.log.info("%s Expected 'version' exception on bad format: %s",
                          my(self), str(e))

        give_up = time.time() + 30
        toi = 'db_server'
        toi_started = None
        while time.time() < give_up:
            resp = httpc.getmap('/status/tasks')
            self.log.debug('Resp %s', json.dumps(resp, indent=4))
            if toi in resp:
                if 'processes' in resp[toi] and len(
                        resp[toi]['processes']) > 0:
                    if 'started_t' in resp[toi]['processes'][0]:
                        toi_started = resp[toi]['processes'][0]['started_t']
                        self.log.info(
                            "%s Task of interest '%s' started %s ago",
                            my(self), toi, deltafmt(time.time() - toi_started))
                        break
                    else:
                        self.log.info("%s Task of interest '%s' is has procs",
                                      my(self), toi)
                else:
                    self.log.info("%s Task of interest '%s' is known",
                                  my(self), toi)
            time.sleep(9)

        #  Try a bogus format
        try:
            resp = httpc.getmap('/status/tasks?indent=4&fmt=xml')
            assert "No 'tasks' exception on bad 'fmt'" is False
        except taskforce.http.HttpError as e:
            self.log.info("%s Expected 'tasks' exception on bad format: %s",
                          my(self), str(e))

        #  Check the config info is sane
        resp = httpc.getmap('/status/config?pending=0')
        assert 'tasks' in resp

        #  Try a bogus format
        try:
            resp = httpc.getmap('/status/config?indent=4&fmt=xml')
            assert "No 'config' exception on bad 'fmt'" is False
        except taskforce.http.HttpError as e:
            self.log.info("%s Expected 'config' exception on bad format: %s",
                          my(self), str(e))

        support.check_procsim_errors(self.__module__, env, log=self.log)
        self.stop_tf()

        assert toi_started is not None