Esempio n. 1
0
 def setUp(self):
     self.client = PyNUTClient(connect=False, debug=True)
     self.client._srv_handler = MockServer(broken=False)
     self.broken_client = PyNUTClient(connect=False, debug=True)
     self.broken_client._srv_handler = MockServer(broken=True)
     self.not_ok_client = PyNUTClient(connect=False, debug=True)
     self.not_ok_client._srv_handler = MockServer(ok=False, broken=False)
     self.valid = "test"
     self.invalid = "does_not_exist"
     self.valid_ups_name = "Test UPS 1"
     self.valid_desc = self.valid_ups_name
     self.valid_value = '100'
     self.valid_command_desc = self.valid_desc
     telnetlib.Telnet = Mock()
 def _startMockErfGeoApi(self):
     def makeResponse(request):
         for key, value in [
             ('%22Verenigde+Staten%22', 'soestdijk'),
             ('%22Leunseweg%22%2C+%22Leunen%22%2C+%22Venray%22', 'leunseweg-leunen-venray'),
             ('GET', 'utrecht')
         ]:
             if key in request:
                 return httputils.okHtml + open(join(self.testdataDir, 'api.erfgeo.nl/%s.response.json' % value)).read()
     self.mockErfGeoApi = MockServer(self.erfGeoApiPort)
     self.mockErfGeoApi.makeResponse = makeResponse
     self.mockErfGeoApi.start()
class TestMockServer(unittest.TestCase):

    def setUp(self):

        self.server = MockServer(host = "localhost", port = 5001)
        self.daemon = True
        self.server.start()

    def test_add_response_json(self):

        # example of how to add response json that needs to be returned for any requests for a specific route

        self.server.add_response_json(url = "/mobiles/manufacturers", serializable = ["samsung", "apple", "mi"],
                                      methods = ('GET',))

        response = requests.get(self.server.url + "/mobiles/manufacturers")
        self.assertEqual(200, response.status_code)
        self.assertEqual(["samsung", "apple", "mi"], response.json())

    def test_no_endpoint_registered(self):

        # call an end point which is not registered

        response = requests.get(self.server.url + "/mobiles/models")
        self.assertEqual(404, response.status_code)

    def tearDown(self):

        self.server.shutdown_server()
    def setUpClass(cls):
        cls.config = cls._load_config(CONFIG_FILE)

        image_repo = cls.config['image']['repo']
        image_tag = cls.config['image']['tag']

        cls.server = MockServer()
        cls.server.start()

        cls.local_image_name_with_tag = create_local_image()

        cls.inline_scan = InlineScan(
            "http://localhost:{}".format(cls.server.port),
            "faketoken", image_repo, image_tag)
Esempio n. 5
0
class TestConnector(TestCase):
    def setUp(self):
        self.server = MockServer()

    def tearDown(self):
        self.server.shutdown()
        del self.server

    def test_iterlines(self):
        thread = Thread(target=self.server.random_send)
        thread.start()
        connector = Connector(('localhost', self.server.port))
        data = open('tests/s2c.log')
        for line in connector.iterlines():
            eq_(line, data.next()[:-1])
        assert_raises(StopIteration, data.next)
        connector.stop_and_wait()

        thread.join()

    def test_send(self):
        messages = ("the first test message\n",
                    "the second message is very l" + "o" * 10000 + "ng\n"
                   )
        message = "".join(messages)
        thread = Thread(target=self.server.recieve_all)
        thread.start()
        connector = Connector(('localhost', self.server.port))

        for m in messages:
            connector.send(m)

        connector.stop_and_wait()
        thread.join()

        eq_(message, self.server.recieved)
Esempio n. 6
0
def mockserver():
    return MockServer()
    def setUp(self):

        self.server = MockServer(host = "localhost", port = 5001)
        self.daemon = True
        self.server.start()
Esempio n. 8
0
 def setUp(self):
     self.server = MockServer()
Esempio n. 9
0
class TestClientCore(TestCase):
    def setUp(self):
        self.server = MockServer()
    
    def tearDown(self):
        self.server.shutdown()
        del self.server

    def test_set_handler(self):
        thread = Thread(target=self.server.random_send)
        thread.start()
        core = ClientCore(('localhost', self.server.port))

        def c_generator():
            for line in open('tests/s2c.log'):
                if line[0] == 'c':
                    yield line[:-1]
        c_generator = c_generator()

        def c_handler(c_message):
            eq_(c_generator.next(), c_message)

        def lab_generator():
            for line in open('tests/s2c.log'):
                if line[0] in 'lab':
                    yield line[:-1]
        lab_generator = lab_generator()

        def lab_handler(lab_message):
            eq_(lab_generator.next(), lab_message)

        def R_handler(R_message):
            raise Exception("Unreachable code")

        core.set_handler('c', c_handler)
        core.set_handler('lab', lab_handler)
        core.set_handler('R', R_handler)
        core.remove_handler('R')
        core.run()
        core.stop_and_wait()

        thread.join()

        assert_raises(StopIteration, c_generator.next)
        assert_raises(StopIteration, lab_generator.next)


    def test_send(self):
        messages = ("the first test message\n",
                    "the second message is very l" + "o" * 10000 + "ng\n"
                   )
        message = "".join(messages)
        thread = Thread(target=self.server.recieve_all)
        thread.start()
        core = ClientCore(('localhost', self.server.port))

        for m in messages:
            core.send(m)

        core.stop()
        thread.join()

        eq_(message, self.server.recieved)
class ErfGeoIntegrationState(IntegrationState):
    def __init__(self, stateName, tests=None, fastMode=False):
        IntegrationState.__init__(self, stateName, tests=tests, fastMode=fastMode)

        self.testdataDir = join(mydir, 'data')

        self.digitaleCollectiePort = PortNumberGenerator.next()

        self.erfGeoEnrichmentPort = PortNumberGenerator.next()
        self.erfGeoEnrichmentLocalStatePath = join(self.integrationTempdir, 'erfGeoEnrichmentLocal')

        self.erfGeoEnrichmentIndexPort = PortNumberGenerator.next()
        self.erfGeoEnrichmentIndexLocalStatePath = join(self.integrationTempdir, 'erfGeoEnrichmentIndexLocal')

        erfGeoRepositorySetsSelectionFile = join(self.erfGeoEnrichmentLocalStatePath, 'erfgeo_dc_sets.json')
        if not self.fastMode:
            clearOrCreateDir(self.erfGeoEnrichmentLocalStatePath)
            open(erfGeoRepositorySetsSelectionFile, 'w').write(ERFGEO_SETS_SELECTION_JSON)

        self.erfGeoApiPort = PortNumberGenerator.next()

        self.globalStatePath = join(self.integrationTempdir, 'global')

        self.saharaGetPort = PortNumberGenerator.next()

        self.config = config = readConfig(join(documentationDir, 'examples', 'dc-erfgeo-enrich.config'))

        # test example config has necessary parameters
        def setConfig(config, parameter, value):
            assert config.get(parameter), "key '%s' should only be given a value if it is already declared in source config %s." % (parameter, config)
            print "config[%s] = %s" % (repr(parameter), repr(value))
            config[parameter] = value

        setConfig(config, 'erfgeoEnrich.portNumber', self.erfGeoEnrichmentPort)
        setConfig(config, 'erfgeoEnrich.index.portNumber', self.erfGeoEnrichmentIndexPort)
        setConfig(config, 'digitaleCollectie.host', 'localhost')
        setConfig(config, 'digitaleCollectie.port', self.digitaleCollectiePort)
        setConfig(config, 'erfgeo.searchApiBaseUrl', 'http://localhost:%s' % self.erfGeoApiPort)

        config['global.apacheLogStream'] = 'disabled'
        config['global.debug.periodicdownload.period'] = '0.1'

        self.configFile = join(self.integrationTempdir, 'erfgeo.config')
        with open(self.configFile, 'w') as f:
            for item in config.items():
                f.write('%s = %s\n' % item)

    def binDir(self, executable=None):
        binDir = join(projectDir, 'bin')
        if not isdir(binDir):
            binDir = '/usr/bin'
        result = binDir if executable is None else join(binDir, executable)
        return result

    def setUp(self):
        self._startMockErfGeoApi()
        self._startMockDigitaleCollectie()
        self._startErfGeoEnrichmentServer()
        self._startErfGeoEnrichmentIndexServer()
        self._createDatabase()

    def tearDown(self):
        IntegrationState.tearDown(self)
        self.mockErfGeoApi.halt = True

    def _startMockDigitaleCollectie(self):
        self._startServer(
            serviceName='DigitaleCollectie-mock',
            executable=join(mydir, 'testutils/start-mockdc'),
            serviceReadyUrl='http://localhost:%s/ready' % self.digitaleCollectiePort,
            port=self.digitaleCollectiePort,
            dataDir=join(self.testdataDir, 'dc_summaries')
        )

    def _startErfGeoEnrichmentServer(self):
        self._startServer(
            serviceName='erfGeoEnrichment',
            executable=self.binDir('erfgeo-enrichment-server'),
            serviceReadyUrl='http://localhost:%s/info/version' % self.erfGeoEnrichmentPort,
            stateDir=self.erfGeoEnrichmentLocalStatePath,
            configFile=self.configFile)

    def _startErfGeoEnrichmentIndexServer(self):
        self._startServer(
            serviceName='erfGeoEnrichmentIndex',
            executable=self.binDir('erfgeo-enrichment-index-server'),
            serviceReadyUrl='http://localhost:%s/info/version' % self.erfGeoEnrichmentIndexPort,
            stateDir=self.erfGeoEnrichmentIndexLocalStatePath,
            configFile=self.configFile)

    def _startMockErfGeoApi(self):
        def makeResponse(request):
            for key, value in [
                ('%22Verenigde+Staten%22', 'soestdijk'),
                ('%22Leunseweg%22%2C+%22Leunen%22%2C+%22Venray%22', 'leunseweg-leunen-venray'),
                ('GET', 'utrecht')
            ]:
                if key in request:
                    return httputils.okHtml + open(join(self.testdataDir, 'api.erfgeo.nl/%s.response.json' % value)).read()
        self.mockErfGeoApi = MockServer(self.erfGeoApiPort)
        self.mockErfGeoApi.makeResponse = makeResponse
        self.mockErfGeoApi.start()

    def _createDatabase(self):
        if self.fastMode:
            print "Reusing database in", self.integrationTempdir
            return
        print "Creating database in", self.integrationTempdir
        sleepWheel(28)  # give ErfGeoEnrichment service etc. some time to process and commit

    def _uploadUpdateRequests(self, datadir, uploadPath, uploadPorts, filter=None):
        requests = (join(datadir, r) for r in sorted(listdir(datadir)) if r.endswith('.updateRequest'))
        for filename in requests:
            if filter is None or filter(filename):
                self._uploadUpdateRequest(filename, uploadPath, uploadPorts)

    def _uploadUpdateRequest(self, filename, uploadPath, uploadPorts):
        aPort = choice(uploadPorts)
        print 'http://localhost:%s%s' % (aPort, uploadPath), '<-', basename(filename)[:-len('.updateRequest')]
        updateRequest = open(filename).read()
        lxml = parse(StringIO(updateRequest))
        uploadIdentifier = xpathFirst(lxml, '//ucp:recordIdentifier/text()')
        self.uploaded.append((uploadIdentifier, updateRequest))
        header, body = postRequest(aPort, uploadPath, updateRequest, parse=False, timeOutInSeconds=18.0)
        if '200' not in header.split('\r\n', 1)[0]:
            print 'No 200 response, but:\n', header
            exit(123)
        if "srw:diagnostics" in body:
            print body
            exit(1234)

    def stopServer(self, *args, **kwargs):
        self._stopServer(*args, **kwargs)