Exemple #1
0
class TestDeviceAdmin(TestAdminHelper):
    def setUp(self):
        self.bc = BaseComponent()
        self.dbconn = self.bc.dbconn
        self.cacheconn = self.bc.cacheconn
        cursor = self.dbconn.cursor()
        cursor.execute('DELETE FROM `hlr_cache`')
        cursor.execute('DELETE FROM `hlr`')
        cursor.executemany('INSERT INTO `hlr` VALUES (%s,%s,%s,%s,%s,%s)', [
          ('1', '2', 'DKI Jakarta', '100,101,102,103', '2010-10-22 15:29:52', 'bernard'),
          ('2', '1', 'Meong1', '110,111,112', '0000-00-00 00:00:00', 'test'),
          ('3', '1', 'Meong2', '210,211,212', '0000-00-00 00:00:00', 'test'),
          ('4', '3', 'Guk1', '330,331,332', '0000-00-00 00:00:00', 'test'),
        ])
        cursor.execute('DELETE FROM `devices`')
        cursor.executemany('INSERT INTO `devices` VALUES (%s, %s, %s, %s, %s, %s,\
          %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)', [
        ('SERVER1.COM10', 'SERVER1', 'COM10', '1', '201001240601026', 'WAVECOM', 'GSM', '1', '0', '', 'MKIOS1', '0', '0', '', '1', '0000-00-00', '2010-11-19 17:40:39', ''),
        ('SERVER1.COM11', 'SERVER1', 'COM11', '1', '201001240600838', 'WAVECOM', 'GSM', '2', '0', '', '', '0', '0', '', '1', '0000-00-00', '2010-11-19 17:22:31', ''),
        ('SERVER1.COM20', 'SERVER1', 'COM20', '1', '201001240600135', 'WAVECOM', 'GSM', '3', '0', '', '', '0', '0', '', '1', '0000-00-00', '2010-11-19 17:28:38', ''),
        ('SERVER1.COM21', 'SERVER1', 'COM21', '1', '201001240600994', 'WAVECOM', 'GSM', '1', '0', '', '', '0', '0', '', '1', '0000-00-00', '2010-11-19 17:28:58', ''),
        ('SERVER1.COM22', 'SERVER1', 'COM22', '1', '201001240601067', 'WAVECOM', 'GSM', '0', '0', '', '', '0', '0', '', '1', '0000-00-00', '2010-11-09 21:12:47', ''),
        ('SERVER1.COM23', 'SERVER1', 'COM23', '1', '201001240601042', 'WAVECOM', 'GSM', '0', '0', '', '', '0', '0', '', '1', '0000-00-00', '2010-11-09 21:12:47', ''),
        ('SERVER1.COM24', 'SERVER1', 'COM24', '1', '201001240600671', 'WAVECOM', 'GSM', '0', '0', '', '', '0', '0', '', '1', '0000-00-00', '2010-11-09 21:12:47', ''),
        ('SERVER1.COM25', 'SERVER1', 'COM25', '1', '351011513521015', 'SIEMENS', 'C55', '0', '0', '', '', '0', '0', '', '1', '0000-00-00', '2010-11-09 21:12:47', ''),
        ('SERVER1.COM26', 'SERVER1', 'COM26', '1', '010348005275525', 'SIEMENS', 'C55', '0', '0', '', '', '0', '0', '', '1', '0000-00-00', '2010-11-09 21:12:47', ''),
        ('SERVER1.COM27', 'SERVER1', 'COM27', '1', '03316190103', 'NOKIA', '6235i', '0', '0', '', '', '0', '0', '', '1', '0000-00-00', '2010-11-09 21:12:47', ''),
        ('SERVER1.COM5', 'SERVER1', 'COM5', '1', '201001240600259', 'WAVECOM', 'GSM', '0', '0', '', '', '0', '0', '', '1', '0000-00-00', '2010-11-09 21:12:47', ''),
        ('SERVER1.COM6', 'SERVER1', 'COM6', '1', '201001240600762', 'WAVECOM', 'GSM', '0', '0', '', '', '0', '0', '', '1', '0000-00-00', '2010-11-09 21:12:47', ''),
        ('SERVER1.COM7', 'SERVER1', 'COM7', '1', '201001240600564', 'WAVECOM', 'GSM', '0', '0', '', '', '0', '0', '', '1', '0000-00-00', '2010-11-09 21:12:47', ''),
        ('SERVER1.COM8', 'SERVER1', 'COM8', '1', '201001240600515', 'WAVECOM', 'GSM', '0', '0', '', '', '0', '0', '', '1', '0000-00-00', '2010-11-09 21:12:47', ''),
        ('SERVER1.COM9', 'SERVER1', 'COM9', '1', '201001240600655', 'WAVECOM', 'GSM', '0', '0', '', '', '0', '0', '', '1', '0000-00-00', '2010-11-09 21:12:47', ''),
        ])
        self.dbconn.commit()
        self.cacheconn.delete(HLR_CACHEKEY)
        self.h2 = HLRCacheForLB(self.dbconn, self.cacheconn)
        cursor.close()
        self.c_dev = DBCache(const.DEVICES_PREFIX, config.DEFAULT_EXPIRE,
            const.DEVICES_SQL)
        self.c_dev.setConn(self.dbconn, self.cacheconn)
        self.c_dev.sGet('SERVER1.COM10')
        self.c_dev.sGet('SERVER1.COM11')
        self.c_dev.sGet('SERVER1.COM20')
        self.c_dev.sGet('SERVER1.COM21')
        self.c_dev.sGet('SERVER1.COM22')
        self.c_dev.sGet('SERVER1.COM23')
        self.c_dev.sGet('SERVER1.COM24')
        self.c_dev.sGet('SERVER1.COM25')
        self.c_dev.sGet('SERVER1.COM26')
        self.c_dev.sGet('SERVER1.COM27')
        self.c_dev.sGet('SERVER1.COM5')
        self.c_dev.sGet('SERVER1.COM6')
        self.c_dev.sGet('SERVER1.COM7')
        self.c_dev.sGet('SERVER1.COM8')
        self.c_dev.sGet('SERVER1.COM9')
#----------------------
    def runTest(self):
        test_function = [x for x in dir(self) if x[:6] == 'srtest']
        map(lambda x: getattr(self, x)(), sorted(test_function))
#----------------------
    def srtest10_adminHelper(self):
        post_data = {
          'device_id': 'SERVER1.COM10,SERVER1.COM11,SERVER1.COM20,SERVER1.COM21'
        }
        self._callAdminHelper('device_preupdate', post_data)
        cursor = self.dbconn.cursor()
        cursor.execute('UPDATE `devices` set `function`=%s, `hlr_id`=%s where `device_id`=%s',
          (const.DR_TOPUP, 1, 'SERVER1.COM10'))
        cursor.execute('UPDATE `devices` set `function`=%s, `hlr_id`=%s where `device_id`=%s',
          (const.DR_TOPUP, 1, 'SERVER1.COM11'))
        cursor.execute('UPDATE `devices` set `function`=%s, `hlr_id`=%s, `active`=0 WHERE \
          `device_id`=%s',
          (const.DR_TOPUP, 1, 'SERVER1.COM20'))
        cursor.execute('UPDATE `devices` set `function`=%s, `hlr_id`=%s where `device_id`=%s',
          (const.DR_TOPUP, 1, 'SERVER1.COM21'))
        cursor.close()
        self.dbconn.commit()
        self._callAdminHelper('device_postupdate', post_data)
        tmp = self.h2._getHLRCache()
        self.assertEqual(
          ['SERVER1.COM10', 'SERVER1.COM11', 'SERVER1.COM21'],
          sorted(tmp[HLR_DEVICE][1]))
        x = self.c_dev.sGet('SERVER1.COM10')
        self.assertEqual(int(x['function']), const.DR_TOPUP)
        x = self.c_dev.sGet('SERVER1.COM11')
        self.assertEqual(int(x['function']), const.DR_TOPUP)
        x = self.c_dev.sGet('SERVER1.COM21')
        self.assertEqual(int(x['function']), const.DR_TOPUP)
#----------------------
    def srtest20_adminHelper(self):
        post_data = {
          'device_id': 'SERVER1.COM5,SERVER1.COM6,SERVER1.COM7'
        }
        self._callAdminHelper('device_preupdate', post_data)
        cursor = self.dbconn.cursor()
        cursor.execute('UPDATE `devices` set `function`=%s, `hlr_id`=%s where `device_id`=%s',
          (const.DR_TOPUP, 2, 'SERVER1.COM5'))
        cursor.execute('UPDATE `devices` set `function`=%s, `hlr_id`=%s where `device_id`=%s',
          (const.DR_TOPUP, 2, 'SERVER1.COM6'))
        cursor.execute('UPDATE `devices` set `function`=%s, `hlr_id`=%s where `device_id`=%s',
          (const.DR_TOPUP, 2, 'SERVER1.COM7'))
        cursor.close()
        self.dbconn.commit()
        self._callAdminHelper('device_postupdate', post_data)
        tmp = self.h2._getHLRCache()
        self.assertEqual(
          ['SERVER1.COM5', 'SERVER1.COM6', 'SERVER1.COM7'],
          sorted(tmp[HLR_DEVICE][2]))
#----------------------
    def srtest30_adminHelper(self):
        post_data = {
          'device_id': 'SERVER1.COM20'
        }
        self._callAdminHelper('device_preupdate', post_data)
        cursor = self.dbconn.cursor()
        cursor.execute('UPDATE `devices` set `active`=1, `hlr_id`=%s where `device_id`=%s',
          (3, 'SERVER1.COM20'))
        cursor.close()
        self.dbconn.commit()
        self._callAdminHelper('device_postupdate', post_data)
        tmp = self.h2._getHLRCache()
        self.assertEqual(
          ['SERVER1.COM10', 'SERVER1.COM11', 'SERVER1.COM21'],
          sorted(tmp[HLR_DEVICE][1]))
        self.assertEqual(
          ['SERVER1.COM20',],
          sorted(tmp[HLR_DEVICE][3]))
Exemple #2
0
class TestHLRCache(unittest.TestCase):
    def setUp(self):
        self.bc = BaseComponent()
        self.dbconn = self.bc.dbconn
        self.cacheconn = self.bc.cacheconn
        cursor = self.dbconn.cursor()
        cursor.execute('DELETE FROM `hlr_cache`')
        cursor.execute('DELETE FROM `hlr`')
        cursor.executemany('INSERT INTO `hlr` VALUES (%s,%s,%s,%s,%s,%s)', [
          ('1', '2', 'DKI Jakarta', '100,101,102,103', '2010-10-22 15:29:52', 'bernard'),
          ('2', '1', 'Meong1', '110,111,112', '0000-00-00 00:00:00', 'test'),
          ('3', '1', 'Meong2', '210,211,212', '0000-00-00 00:00:00', 'test'),
          ('4', '3', 'Guk1', '330,331,332', '0000-00-00 00:00:00', 'test'),
        ])
        cursor.close()
        self.cacheconn.delete(HLR_CACHEKEY)
        self.h1 = HLRCacheForLB(self.dbconn, self.cacheconn)
        self.h2 = HLRCacheForLB(self.dbconn, self.cacheconn)
        self.h1.addDevice('SERVER1.COM1', 2, 1, False)
        self.h1.addDevice('SERVER1.COM1', 2, 1, False)
        self.h1.addDevice('SERVER1.COM3', 2, 1, False)
        self.h1.addDevice('SERVER1.COM3', 2, 1, False)
        self.h1.addDevice('SERVER1.COM3', 2, 1, False)
        self.h1.addDevice('SERVER1.COM4', 2, 1, False)
        self.h1.addDevice('SERVER1.COM4', 2, 1, False)
        self.h1.addDevice('SERVER1.COM5', 2, 1, False)
        self.h1.addDevice('SERVER1.COM6', 1, 2, False)
        self.h1.addDevice('SERVER1.COM6', 1, 2, False)
        self.h1.addDevice('SERVER1.COM1', 2, 1, False)
        self.h1.addDevice('SERVER1.COM7', 1, 2, False)
        self.h1.addDevice('SERVER1.COM8', 1, 2, False)
        self.h1.addDevice('SERVER1.COM1', 2, 1, False)
        self.h1.addDevice('SERVER1.COM8', 1, 2, False)
        self.h1.addDevice('SERVER1.COM9', 1, 0, False)
        self.h1.addDevice('SERVER1.COM9', 1, 0, False)
        self.h1.addDevice('SERVER1.COM9', 1, 0, False)
        self.h1.addDevice('SERVER1.COM10', 2, 0, False)
        self.dbconn.commit()
        # print self.cacheconn.get(HLR_CACHEKEY)
#----------------------
    def runTest(self):
        test_function = [x for x in dir(self) if x[:6] == 'srtest']
        map(lambda x: getattr(self, x)(), sorted(test_function))
#----------------------
    def srtest10(self):
        self.h2.addDevice('SERVER2.COM99', 1, 2)
        tmp = self.h2._getHLRCache()
        self.assertEqual(
          ['SERVER1.COM6', 'SERVER1.COM7', 'SERVER1.COM8', 'SERVER2.COM99'],
          sorted(tmp[HLR_DEVICE][2]))
        self.assertEqual(['SERVER1.COM9',], tmp[HLR_FREE][1])
        tmp = []
        tmp.append(self.h2.nextDeviceByHLRId(1))
        tmp.append(self.h2.nextDeviceSameHLR())
        tmp.append(self.h2.nextDeviceByHLRId(1))
        tmp.append(self.h2.nextDeviceSameHLR())
        self.assertEqual(
          ['SERVER1.COM1', 'SERVER1.COM3', 'SERVER1.COM4', 'SERVER1.COM5'],
          sorted(tmp)
        )
        x = self.h2.nextDeviceSameHLR()
        self.assertTrue(x in tmp)
        ##
        tmp = []
        tmp.append(self.h2.nextDevice(2, '100'))
        tmp.append(self.h2.nextDeviceSameHLR())
        tmp.append(self.h2.nextDevice(2, '103'))
        tmp.append(self.h2.nextDeviceSameHLR())
        self.assertEqual(
          ['SERVER1.COM1', 'SERVER1.COM3', 'SERVER1.COM4', 'SERVER1.COM5'],
          sorted(tmp)
        )
        x = self.h2.nextDevice(2, '102')
        self.assertTrue(x in tmp)
#----------------------
    def srtest20(self):
        self.h2.removeDevice('SERVER1.COM4', 2, 1)
        tmp = self.h2._getHLRCache()
        self.assertEqual(
          ['SERVER1.COM1', 'SERVER1.COM3', 'SERVER1.COM5'],
          sorted(tmp[HLR_DEVICE][1]))
#----------------------
    def srtest30_free_device(self):
        tmp = self.h2.nextFreeDevice(2)
        self.assertEqual(tmp, 'SERVER1.COM10')
        tmp = self.h2.nextFreeDevice(2)
        self.assertEqual(tmp, 'SERVER1.COM10')
        tmp = self.h2.nextDeviceSameHLR()
        self.assertEqual(tmp, 'SERVER1.COM10')