Exemplo n.º 1
0
 def setUpClass(cls):
     # Monkeypatch Subscriber.
     cls.original_subscriber = utilities.subscriber
     cls.mock_subscriber = mocks.MockSubscriber()
     utilities.subscriber = cls.mock_subscriber
     # Connect to the GPRSDB.
     cls.gprs_db = gprs_database.GPRSDB()
    def setUpClass(cls):
        """Load up some pricing data into the config db."""
        price_data = [{
            'directionality': 'off_network_send',
            'prefix': '789',
            'country_name': 'Ocenaia',
            'country_code': 'OC',
            'cost_to_subscriber_per_sms': 300,
            'cost_to_subscriber_per_min': 200,
        }, {
            'directionality': 'off_network_receive',
            'cost_to_subscriber_per_sms': 400,
            'cost_to_subscriber_per_min': 100,
        }, {
            'directionality': 'on_network_send',
            'cost_to_subscriber_per_sms': 40,
            'cost_to_subscriber_per_min': 10,
        }, {
            'directionality': 'on_network_receive',
            'cost_to_subscriber_per_sms': 30,
            'cost_to_subscriber_per_min': 40,
        }]
        # Create a simplified checkin response with just price data.
        checkin_response = {'config': {'prices': price_data}}
        # Mock the checkin handler object such that validation just returns the
        # object to-be-validated (without checking JWT).
        mock_checkin_handler = CheckinHandler
        mock_checkin_handler.validate = lambda self, data: data
        mock_checkin_handler(checkin_response)

        # mock subscriber so we dont actually execute DB queries
        mock_subscriber = mocks.MockSubscriber()
        cls.original_subscriber = core.federer_handlers.cdr.subscriber
        core.federer_handlers.cdr.subscriber = mock_subscriber
 def setUpClass(cls):
     """Setup the ConfigDB with a fake secret, and mock other items."""
     cls.config_db = ConfigDB()
     cls.config_db['bts_secret'] = 'test-secret-123'
     cls.original_logger = interconnect.logger
     cls.mock_logger = mock.Mock()
     interconnect.logger = cls.mock_logger
     # Mock bts for TMSIs
     cls.original_bts = interconnect.bts
     interconnect.bts = mocks.MockBTS()
     # Mock subscriber
     cls.original_subscriber = interconnect.subscriber
     interconnect.subscriber = mocks.MockSubscriber()
Exemplo n.º 4
0
 def setUpClass(cls):
     # Monkeypatch Subscriber so sub balance lookups succeed.
     cls.original_subscriber = utilities.subscriber
     cls.mock_subscriber = mocks.MockSubscriber()
     utilities.subscriber = cls.mock_subscriber
     subscriber.create_subscriber('IMSI901550000000084', '5551234')
     subscriber.create_subscriber('IMSI901550000000082', '5551235')
     # Connect to the GPRSDB and EventStore.
     cls.gprs_db = gprs_database.GPRSDB()
     cls.event_store = events.EventStore()
     # Add some records to the GPRSDB.  The method we're testing should
     # extract these records and create events in the EventStore.
     cls.now = time.time()
     records = [
         (psycopg2.TimestampFromTicks(cls.now - 120), 'IMSI901550000000084',
          '192.168.99.1', 50, 80, 50, 80),
         (psycopg2.TimestampFromTicks(cls.now - 60), 'IMSI901550000000084',
          '192.168.99.1', 50, 80, 0, 0),
         (psycopg2.TimestampFromTicks(cls.now - 30), 'IMSI901550000000084',
          '192.168.99.1', 300, 500, 250, 420),
         (psycopg2.TimestampFromTicks(cls.now - 10), 'IMSI901550000000084',
          '192.168.99.1', 700, 600, 400, 100),
         (psycopg2.TimestampFromTicks(cls.now - 5), 'IMSI901550000000084',
          '192.168.99.1', 750, 625, 50, 25),
         # Create events for a different IMSI.
         (psycopg2.TimestampFromTicks(cls.now - 60), 'IMSI901550000000082',
          '192.168.99.2', 50, 80, 0, 0),
         (psycopg2.TimestampFromTicks(cls.now - 10), 'IMSI901550000000082',
          '192.168.99.2', 400, 300, 350, 220),
         (psycopg2.TimestampFromTicks(cls.now - 5), 'IMSI901550000000082',
          '192.168.99.2', 450, 325, 50, 25),
     ]
     schema = ('record_timestamp, imsi, ipaddr, uploaded_bytes,'
               ' downloaded_bytes, uploaded_bytes_delta,'
               ' downloaded_bytes_delta')
     connection = psycopg2.connect(host='localhost',
                                   database='endaga',
                                   user=PG_USER,
                                   password=PG_PASSWORD)
     with connection.cursor() as cursor:
         for record in records:
             values = "%s, '%s', '%s', %s, %s, %s, %s" % record
             command = 'insert into gprs_records (%s) values(%s)' % (schema,
                                                                     values)
             cursor.execute(command)
     connection.commit()
    def setUpClass(cls):
        """Generate one set of checkin data to be analyzed in the tests."""
        # Mock the requests module so we don't actually POST.
        cls.original_requests = core.interconnect.requests
        cls.mock_requests = mocks.MockRequests(200)
        core.interconnect.requests = cls.mock_requests
        # Mock subscriber
        cls.original_subscriber = core.interconnect.subscriber
        core.interconnect.subscriber = mocks.MockSubscriber()

        # Mock core.events for generating usage events and handling the checkin
        # response.
        cls.original_events = core.interconnect.events
        core.interconnect.events = mocks.MockEvents()
        # Mock snowflake.
        cls.original_snowflake = core.interconnect.snowflake
        cls.mock_uuid = '09031a16-6361-4a93-a934-24c990ef4b87'
        core.interconnect.snowflake = mocks.MockSnowflake(cls.mock_uuid)
        # Mock BTS for TMSIs
        cls.original_bts = core.interconnect.bts
        core.interconnect.bts = mocks.MockBTS()
        # Mock a lot the package version numbers that should be sent in the
        # checkin.
        config_db = ConfigDB()
        cls.package_versions = {
            'endaga': '1.2.3',
            'freeswitch': '2.3.4',
            'gsm': '3.4.5',
            'python-endaga-core': '4.5.6',
            'python-gsm': '5.6.7',
        }
        for key in cls.package_versions:
            config_db['%s_version' % key] = cls.package_versions[key]
        # Mock psutil for system utilization stats.
        cls.original_psutil = core.system_utilities.psutil
        utilization = {
            'cpu_percent': 20.1,
            'memory_percent': 33.3,
            'disk_percent': 52.2,
            'bytes_sent': 1234,
            'bytes_received': 5678,
        }
        core.system_utilities.psutil = mocks.MockPSUtil(utilization)

        # Create some fake events
        es = core.interconnect.events.EventStore()
        es.add(
            cls.original_events._create_event(imsi="IMSI123",
                                              old_credit=0,
                                              new_credit=100,
                                              reason="Foo",
                                              write=False))
        es.add(
            cls.original_events._create_event(imsi="IMSI123",
                                              old_credit=0,
                                              new_credit=100,
                                              reason="Foo",
                                              write=False))
        es.add(
            cls.original_events._create_event(imsi="IMSI321",
                                              old_credit=0,
                                              new_credit=100,
                                              reason="Foo",
                                              write=False))

        # Setup a secret key.
        config_db['bts_secret'] = cls.mock_uuid
        # Attempt a checkin.
        config_db = core.config_database.ConfigDB()
        cls.endaga_ic = core.interconnect.endaga_ic(config_db)
        cls.endaga_ic.checkin()
        # Get the POSTed data and a deserialized form for convenience.
        cls.data = cls.mock_requests.post_data
        cls.deserialized_status = json.loads(cls.data['status'])