Exemplo n.º 1
0
    def setUp(self):
        super(DbTestCases, self).setUp()

        if DbTestCases.python_version < 270:
            self.db = db.connect('mysql://*****:*****@127.0.0.1/schema')
            self.db.session_open()

        timestamp = self.timestamp()
        self.meter = Meter(name='test_' + timestamp,
                      description='created in unit test',
                      type='gauge',
                      unit_name='kb', data_type='int')
        self.db.save(self.meter)
        self.db.commit()

        self.host = Host(name='test_' + timestamp)
        self.db.save(self.host)
        self.db.commit()

        self.record = MeterRecord(meter_id=self.meter.id,
                                 host_id=self.host.id,
                                 user_id='unit_test_user_id',
                                 resource_id='unit_test_resource_id',
                                 project_id='uni_test_project_id',
                                 value='10',
                                 duration=0,
                                 timestamp=self.timestamp())
        self.db.save(self.record)
        self.db.commit()
Exemplo n.º 2
0
 def test_save_meter_record_insert(self):
     record = MeterRecord(meter_id=self.meter.id,
                          host_id=self.host.id,
                          user_id='unit_test_user_id',
                          resource_id='unit_test_resource_id',
                          project_id='uni_test_project_id',
                          value='10',
                          duration=0,
                          timestamp=self.timestamp())
     self.db.save(record)
Exemplo n.º 3
0
    def serialize(element, *args):
        catalog = args[0] if args else {}

        try:
            record = MeterRecord()
            record.id, record.host_id, record.resource_id, \
            record.project_id, record.user_id, record.meter_id, \
            record.timestamp, record.duration = \
                element['id'], element['host_id'], \
                element['resource_id'], element['project_id'], \
                element['user_id'], int(element['meter_id']), \
                element['timestamp'], element['duration']

            value_type = catalog.get(record.meter_id)
            record.value = getattr(__builtin__, value_type)(element['value']) \
                               if value_type \
                               else element['value']

        except Exception as e:
            # logger.exception
            raise e

        finally:
            return record
Exemplo n.º 4
0
    def serialize(element, *args):
        catalog = args[0] if args else {}

        try:
            record = MeterRecord()
            record.id, record.host_id, record.resource_id, \
            record.project_id, record.user_id, record.meter_id, \
            record.timestamp, record.duration = \
                element['id'], element['host_id'], \
                element['resource_id'], element['project_id'], \
                element['user_id'], int(element['meter_id']), \
                element['timestamp'], element['duration']

            value_type = catalog.get(record.meter_id)
            record.value = getattr(__builtin__, value_type)(element['value']) \
                               if value_type \
                               else element['value']

        except Exception as e:
            # logger.exception
            raise e

        finally:
            return record
Exemplo n.º 5
0
 def test_sum(self):
     record = MeterRecord(meter_id=self.meter.id,
                          host_id=self.host.id,
                          user_id='unit_test_user_id',
                          resource_id='unit_test_resource_id',
                          project_id='uni_test_project_id',
                          value='100',
                          duration=0,
                          timestamp=self.timestamp())
     self.db.save(record)
     args = {'meter_id': self.meter.id}
     records = self.db.load(MeterRecord, args=args)
     value_sum = 0
     for r in records:
         value_sum += int(r.value)
     self.assertEqual(value_sum, self.db.sum(MeterRecord, 'value',
                                             args=args))
Exemplo n.º 6
0
class DbTestCases(unittest.TestCase):

    python_version = int(''.join([str(i) for i in sys.version_info[0:3]]))

    @classmethod
    def timestamp(self):
        return datetime.now().strftime("%Y-%m-%d %H:%M:%S")

    @classmethod
    def setUpClass(cls):
        cls.db = db.connect('mysql://*****:*****@127.0.0.1/schema')
        cls.db.session_open()

    @classmethod
    def tearDownClass(cls):
        cls.db.session_close()

    def setUp(self):
        super(DbTestCases, self).setUp()

        if DbTestCases.python_version < 270:
            self.db = db.connect('mysql://*****:*****@127.0.0.1/schema')
            self.db.session_open()

        timestamp = self.timestamp()
        self.meter = Meter(name='test_' + timestamp,
                      description='created in unit test',
                      type='gauge',
                      unit_name='kb', data_type='int')
        self.db.save(self.meter)
        self.db.commit()

        self.host = Host(name='test_' + timestamp)
        self.db.save(self.host)
        self.db.commit()

        self.record = MeterRecord(meter_id=self.meter.id,
                                 host_id=self.host.id,
                                 user_id='unit_test_user_id',
                                 resource_id='unit_test_resource_id',
                                 project_id='uni_test_project_id',
                                 value='10',
                                 duration=0,
                                 timestamp=self.timestamp())
        self.db.save(self.record)
        self.db.commit()

    def tearDown(self):
        super(DbTestCases, self).tearDown()
        self.db.rollback()
        self.db.delete(self.record)
        self.db.delete(self.meter)
        self.db.delete(self.host)
        self.db.commit()
        self.db.session_close()

    def test_save_meter_insert(self):
        meter = Meter(name='test_save_meter_insert',
                      description='created in unit test',
                      unit_name='kb', data_type='int')
        self.db.save(meter)

    def test_load_meter(self):
        meter = self.db.load(Meter, {'id': self.meter.id}, limit=1)[0]
        self.assertEqual(meter.id, self.meter.id)

    def test_save_meter_update(self):
        self.meter.description = 'updated in unit test'
        self.db.save(self.meter)
        meter = self.db.load(Meter, {'id': self.meter.id}, limit=1)[0]
        self.assertEqual(self.meter.description, meter.description)

    def test_delete_meter(self):
        self.db.delete(self.record)
        self.db.delete(self.meter)

    def test_save_meter_record_insert(self):
        record = MeterRecord(meter_id=self.meter.id,
                             host_id=self.host.id,
                             user_id='unit_test_user_id',
                             resource_id='unit_test_resource_id',
                             project_id='uni_test_project_id',
                             value='10',
                             duration=0,
                             timestamp=self.timestamp())
        self.db.save(record)

    def test_load_meter_record(self):
        record = self.db.load(MeterRecord, {'id': self.record.id}, limit=1)[0]
        self.assertEqual(record.id, self.record.id)

    def test_save_meter_record_update(self):
        self.record.value = '20'
        self.db.save(self.record)
        record = self.db.load(MeterRecord, {'id': self.record.id}, limit=1)[0]
        self.assertEqual(record.value, self.record.value)

    def test_delete_meter_record(self):
        self.db.delete(self.record)

    def test_to_dict(self):
        d = self.record.to_dict()
        self.assertEqual(d['value'], self.record.value)

    def test_load_order(self):
        meter = Meter(name='test_load_meter_order',
                      description='created in unit test',
                      type='gauge',
                      unit_name='kb', data_type='int')
        self.db.save(meter)

        meters = self.db.load(Meter, order='asc', order_attr='id')
        self.assertTrue(True if len(meters) > 1 else False)
        self.assertEqual(True if meters[0].id < meters[1].id else False, True)

        meters = self.db.load(Meter, order='desc', order_attr='id')
        self.assertTrue(True if len(meters) > 1 else False)
        self.assertEqual(True if meters[0].id > meters[1].id else False, True)

    def test_count(self):
        meters = self.db.load(Meter)
        meter_count = self.db.count(Meter)
        self.assertEqual(len(meters), meter_count)

    def test_distinct(self):
        meter_name = 'test_distinct_' + self.timestamp()
        meter = Meter(name=meter_name,
                      description='created in unit test',
                      type='gauge',
                      unit_name='bytes', data_type='int')
        self.db.save(meter)
        distinct_values = self.db.distinct_values(Meter, 'name')
        self.assertTrue(meter_name in distinct_values)

    def test_sum(self):
        record = MeterRecord(meter_id=self.meter.id,
                             host_id=self.host.id,
                             user_id='unit_test_user_id',
                             resource_id='unit_test_resource_id',
                             project_id='uni_test_project_id',
                             value='100',
                             duration=0,
                             timestamp=self.timestamp())
        self.db.save(record)
        args = {'meter_id': self.meter.id}
        records = self.db.load(MeterRecord, args=args)
        value_sum = 0
        for r in records:
            value_sum += int(r.value)
        self.assertEqual(value_sum, self.db.sum(MeterRecord, 'value',
                                                args=args))
Exemplo n.º 7
0
    def _collector_callback(self, params):
        envelope = EnvelopeAdapter()

        # check whether incorrectly formatted message
        try:
            envelope.deserialize_from_str(params)
        except:
            return

        message = MessageAdapter(envelope.message)
        # validate signature
        if not validateSignature(str(message), self.shared_secret,
                                 envelope.signature):
            return

        self.db.session_open()

        # load all meters now to avoid queries later
        meters = self.db.load(Meter)
        meter_dict = {}
        for meter in meters:
            meter_dict[meter.name] = meter

        # insert host if it does not exist yet
        hosts = self.db.load(Host, {'name': message.host_name}, limit=1)
        if not hosts:
            host = Host(name=message.host_name)
            self.db.save(host)
            self.db.commit()
        else:
            host = hosts[0]

        # insert all host records
        for r in message.host_records:
            if r.meter_name not in meter_dict:
                _logger.warning('Unknown meter_name "%s"' % r.meter_name)
                continue
            try:
                record = MeterRecord(meter_id=meter_dict[r.meter_name].id,
                                     host_id=host.id,
                                     user_id=None,
                                     resource_id=None,
                                     project_id=None,
                                     value=r.value,
                                     duration=r.duration,
                                     timestamp=r.timestamp)
                self.db.save(record)
                _logger.debug("New %s" % record)

                # update host activity
                record_timestamp = self._str_to_datetime(r.timestamp)
                if not host.activity or record_timestamp > host.activity:
                    host.activity = record_timestamp

            except Exception as e:
                _logger.exception(e)

        # insert all instance records
        for r in message.inst_records:
            if r.meter_name not in meter_dict:
                _logger.warning('Unknown meter_name "%s"' % r.meter_name)
                continue
            try:
                # @[fbahr] - TODO: `self.known_instances` grows over time
                #                  towards inf. - clean-up?
                if not r.inst_id in self.known_instances:
                    self.known_instances[r.inst_id] = self._metadata(
                        uuid=r.inst_id)

                r.project_id, r.user_id = self.known_instances[r.inst_id]

                # insert project if it does not exist yet
                projects = self.db.load(Project, {'uuid': r.project_id},
                                        limit=1)
                if not projects:
                    project = Project(uuid=r.project_id,
                                      created_at=self._str_to_datetime(
                                          r.timestamp))
                    self.db.save(project)
                    self.db.commit()
                else:
                    project = projects[0]

                record = MeterRecord(meter_id=meter_dict[r.meter_name].id,
                                     host_id=host.id,
                                     user_id=r.user_id,
                                     resource_id=r.inst_id,
                                     project_id=r.project_id,
                                     value=r.value,
                                     duration=r.duration,
                                     timestamp=r.timestamp)
                self.db.save(record)
                _logger.debug("New %s" % record)

                # update host and project activity
                record_timestamp = self._str_to_datetime(r.timestamp)
                if not host.activity or record_timestamp > host.activity:
                    host.activity = record_timestamp
                if not project.updated_at or record_timestamp > project.updated_at:
                    project.updated_at = record_timestamp

            except Exception as e:
                _logger.exception(e)

        try:
            self.db.commit()
        except Exception as e:
            self.db.rollback()
            _logger.exception(e)
        self.db.session_close()