def setUp(self):
     loop = asyncio.new_event_loop()
     asyncio.set_event_loop(loop)
     self._loop = loop
     self._sync_rpc_client = SyncRPCClient(loop=loop, response_timeout=3)
     self._sync_rpc_client._conn_closed_table = {12345: False}
     ServiceRegistry.add_service('test', '0.0.0.0', 0)
     ServiceRegistry._PROXY_CONFIG = {
         'local_port': 2345,
         'cloud_address': 'test',
         'proxy_cloud_connections': True
     }
     self._req_body = GatewayRequest(gwId="test id",
                                     authority='mobility',
                                     path='/magma.MobilityService'
                                     '/ListAddedIPv4Blocks',
                                     headers={
                                         'te': 'trailers',
                                         'content-type': 'application/grpc',
                                         'user-agent': 'grpc-python/1.4.0',
                                         'grpc-accept-encoding': 'identity'
                                     },
                                     payload=bytes.fromhex('0000000000'))
     self._expected_resp = GatewayResponse(status="400",
                                           headers={"test_key": "test_val"},
                                           payload=b'\x00'
                                           b'\x00\x00\x00\n\n\x08')
     self._expected_err_msg = "test error"
Beispiel #2
0
 def setUp(self):
     loop = asyncio.new_event_loop()
     asyncio.set_event_loop(loop)
     self._loop = loop
     self._proxy_client = ControlProxyHttpClient()
     ServiceRegistry._REGISTRY = {
         "services": {
             "mobilityd":
             {
                 "ip_address": "0.0.0.0",
                 "port": 3456,
             },
         },
     }
     ServiceRegistry.add_service('test', '0.0.0.0', 0)
     self._req_body = GatewayRequest(
         gwId="test id", authority='mobilityd',
         path='/magma.MobilityService'
              '/ListAddedIPv4Blocks',
         headers={
             'te': 'trailers',
             'content-type':
                 'application/grpc',
             'user-agent':
                 'grpc-python/1.4.0',
             'grpc-accept-encoding':
                 'identity',
         },
         payload=bytes.fromhex('0000000000'),
     )
Beispiel #3
0
    def test_service_run(self, mock_get_proxy_config):
        """
        Test if the service starts and stops gracefully.
        """
        self.assertEqual(self._service.state, ServiceInfo.STARTING)

        mock_get_proxy_config.return_value = {
            'cloud_address': '127.0.0.1',
            'proxy_cloud_connections': True
        }

        # Start the service and pause the loop
        self._service.loop.stop()
        self._service.run()
        self.assertEqual(self._service.state, ServiceInfo.ALIVE)

        # Create a rpc stub and query the Service303 interface
        ServiceRegistry.add_service('test', '0.0.0.0', self._service.port)
        channel = ServiceRegistry.get_rpc_channel('test', ServiceRegistry.LOCAL)
        self._stub = Service303Stub(channel)

        info = ServiceInfo(name='test',
                           version='0.0.0',
                           state=ServiceInfo.ALIVE,
                           health=ServiceInfo.APP_HEALTHY,
                           start_time_secs=12345)
        self.assertEqual(self._stub.GetServiceInfo(Void()), info)

        # Stop the service
        self._stub.StopService(Void())
        self._service.loop.run_forever()
        self.assertEqual(self._service.state, ServiceInfo.STOPPED)
Beispiel #4
0
    def setUp(self):
        """Initialize client tests"""
        # Create sqlite3 database for testing
        self._tmpfile = tempfile.TemporaryDirectory()
        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(self.loop)
        store = SqliteStore(
            '{filename}{slash}'.format(
                filename=self._tmpfile.name,
                slash='/',
            ),
        )

        ServiceRegistry.add_service('test', '0.0.0.0', 0)  # noqa: S104
        ServiceRegistry._PROXY_CONFIG = {
            'local_port': 1234,
            'cloud_address': '',
            'proxy_cloud_connections': False,
        }
        ServiceRegistry._REGISTRY = {
            "services": {
                "s6a_service":
                {
                    "ip_address": "0.0.0.0",  # noqa: S104
                    "port": 2345,
                },
            },
        }

        self.service = MagicMock()
        self.service.loop = self.loop

        # Bind the rpc server to a free port
        self._rpc_server = grpc.server(
            futures.ThreadPoolExecutor(max_workers=10),
        )
        port = self._rpc_server.add_insecure_port('0.0.0.0:0')
        # Add the servicer
        self._servicer = MockSubscriberDBServer()
        self._servicer.add_to_server(self._rpc_server)
        self._rpc_server.start()
        # Create a rpc stub
        self.channel = grpc.insecure_channel(
            '0.0.0.0:{port}'.format(
                port=port,
            ),
        )
        grpc_client_manager = GRPCClientManager(
            service_name="subscriberdb",
            service_stub=SubscriberDBCloudStub,
            max_client_reuse=60,
        )
        self.subscriberdb_cloud_client = SubscriberDBCloudClient(
            loop=self.service.loop,
            store=store,
            subscriber_page_size=2,
            sync_interval=10,
            grpc_client_manager=grpc_client_manager,
        )
        self.subscriberdb_cloud_client.start()
Beispiel #5
0
    def setUp(self):
        ServiceRegistry.add_service('test', '0.0.0.0', 0)
        self._stub = None

        # Use a new event loop to ensure isolated tests
        self._service = MagmaService('test', loop=asyncio.new_event_loop())
        # Clear the global event loop so tests rely only on the event loop that
        # was manually set
        asyncio.set_event_loop(None)
Beispiel #6
0
 def setUp(self):
     ServiceRegistry.add_service('test1', '0.0.0.0', 0)
     ServiceRegistry.add_service('test2', '0.0.0.0', 0)
     config = {
         'magma_services': ['test1', 'test2'],
         'non_service303_services': ['test2']
     }
     self._loop = asyncio.new_event_loop()
     self._service_poller = ServicePoller(self._loop, config)
Beispiel #7
0
    def setUp(self):
        ServiceRegistry.add_service('test', '0.0.0.0', 0)
        self._stub = None

        self._loop = asyncio.new_event_loop()
        # Use a new event loop to ensure isolated tests
        self._service = MagmaService(
            name='test',
            empty_mconfig=mconfigs_pb2.MagmaD(),
            loop=self._loop,
        )
        asyncio.set_event_loop(self._service.loop)
Beispiel #8
0
 def setUp(self):
     # Create sqlite3 database for testing
     self._tmpfile = tempfile.TemporaryDirectory()
     store = SqliteStore(self._tmpfile.name +'/')
     self._streamer_callback = \
         SubscriberDBStreamerCallback(store, loop=asyncio.new_event_loop())
     ServiceRegistry.add_service('test', '0.0.0.0', 0)
     ServiceRegistry._PROXY_CONFIG = {'local_port': 1234,
                                      'cloud_address': '',
                                      'proxy_cloud_connections': False}
     ServiceRegistry._REGISTRY = {"services": {"s6a_service":
                                               {"ip_address": "0.0.0.0",
                                                "port": 2345}}
                                  }
Beispiel #9
0
    def setUp(self):
        ServiceRegistry.add_service('test', '0.0.0.0', 0)
        ServiceRegistry._PROXY_CONFIG = {'local_port': 1234,
                                         'cloud_address': 'test',
                                         'proxy_cloud_connections': True}

        self._services = ['test']
        self.gateway_id = "2876171d-bf38-4254-b4da-71a713952904"
        self.queue_length = 5
        self.timeout = 1
        self._collector = MetricsCollector(self._services, 5, 10,
                                           self.timeout,
                                           grpc_max_msg_size_mb=4,
                                           queue_length=self.queue_length,
                                           loop=asyncio.new_event_loop())
Beispiel #10
0
    def setUp(self):
        ServiceRegistry.add_service('s6a_proxy', '0.0.0.0', 0)
        proxy_config = {
            'local_port': 1234,
            'cloud_address': 'test',
            'proxy_cloud_connections': True,
        }

        self._base_manager = base.BaseApplication(
            self.REALM,
            self.HOST,
            self.HOST_ADDR,
        )
        self._proxy_client = Mock()
        self._s6a_manager = s6a_relay.S6ARelayApplication(
            Mock(),
            self.REALM,
            self.HOST,
            self.HOST_ADDR,
            proxy_client=self._proxy_client,
        )
        self._server = server.S6aServer(
            self._base_manager,
            self._s6a_manager,
            self.REALM,
            self.HOST,
        )

        self.get_proxy_config_patcher = patch.object(
            s6a_relay.ServiceRegistry,
            'get_proxy_config',
            Mock(return_value=proxy_config),
        )
        self.mock_get_proxy_config = self.get_proxy_config_patcher.start()
        self.addCleanup(self.get_proxy_config_patcher.stop)

        # Mock the writes to check responses
        self._writes = Mock()

        def convert_memview_to_bytes(memview):
            """ Deep copy the memoryview for checking later  """
            return self._writes(memview.tobytes())

        self._transport = MockTransport()
        self._transport.write = Mock(side_effect=convert_memview_to_bytes)

        # Here goes nothing..
        self._server.connection_made(self._transport)
Beispiel #11
0
 def setUp(self):
     store = SqliteStore('file::memory:')
     self._streamer_callback = \
         SubscriberDBStreamerCallback(store, loop=asyncio.new_event_loop())
     ServiceRegistry.add_service('test', '0.0.0.0', 0)
     ServiceRegistry._PROXY_CONFIG = {
         'local_port': 1234,
         'cloud_address': '',
         'proxy_cloud_connections': False
     }
     ServiceRegistry._REGISTRY = {
         "services": {
             "s6a_service": {
                 "ip_address": "0.0.0.0",
                 "port": 2345
             }
         }
     }
Beispiel #12
0
    def setUp(self):
        ServiceRegistry.add_service('test1', '0.0.0.0', 0)
        ServiceRegistry.add_service('test2', '0.0.0.0', 0)
        ServiceRegistry.add_service('test3', '0.0.0.0', 0)

        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(self.loop)

        service = MagicMock()
        service.config = {
            'magma_services': ['test1', 'test2', 'test3'],
            'non_service303_services': ['test2'],
            'skip_checkin_if_missing_meta_services': ['test3'],
        }
        service.mconfig.checkin_interval = 60
        service.mconfig.checkin_timeout = 30
        service.mconfig_metadata.created_at = 0
        service.version = "1.1.1.1"
        service.loop = self.loop

        # Bind the rpc server to a free port
        self._rpc_server = grpc.server(
            futures.ThreadPoolExecutor(max_workers=10))
        port = self._rpc_server.add_insecure_port('0.0.0.0:0')
        # Add the servicer
        self._servicer = DummpyStateServer()
        self._servicer.add_to_server(self._rpc_server)
        self._rpc_server.start()
        # Create a rpc stub
        self.channel = grpc.insecure_channel('0.0.0.0:{}'.format(port))

        # Set up and start state reporting loop
        self.service_poller = unittest.mock.Mock()
        self.service_poller.service_info = unittest.mock.Mock()
        self.service_poller.service_info.return_value = {}

        # Mock out GatewayStatusFactory
        gateway_status_factory = \
            GatewayStatusFactory(service, self.service_poller, None)
        # Mock _system_status since it tried to read from /data/flash
        gateway_status_factory._system_status = unittest.mock.Mock()
        gateway_status_factory._system_status.return_value = \
            self.default_system_status()

        grpc_client_manager = GRPCClientManager(
            service_name="state",
            service_stub=StateServiceStub,
            max_client_reuse=60,
        )

        # Mock out bootstrap manager
        bootstrap_manager = unittest.mock.Mock()
        bootstrap_manager.schedule_bootstrap_now = unittest.mock.Mock()
        bootstrap_manager.schedule_bootstrap_now.return_value = None

        self.state_reporter = StateReporter(
            config=service.config,
            mconfig=service.mconfig,
            loop=service.loop,
            bootstrap_manager=bootstrap_manager,
            gw_status_factory=gateway_status_factory,
            grpc_client_manager=grpc_client_manager,
        )
        self.state_reporter.FAIL_THRESHOLD = 0

        self.state_reporter.start()
Beispiel #13
0
 def setUp(self):
     self._scribe_client = ScribeClient(loop=asyncio.new_event_loop())
     ServiceRegistry.add_service('test', '0.0.0.0', 0)
     ServiceRegistry._PROXY_CONFIG = {'local_port': 1234,
                                      'cloud_address': 'test',
                                      'proxy_cloud_connections': True}