#!/usr/bin/env pmpython
""" Test creation of PCP MMV metrics via the pcp.mmv python module """
#
# Copyright (C) 2013 Red Hat Inc.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#

import sys
import unittest
from pcp import mmv, pmapi
from cpmapi import PM_COUNT_ONE, PM_TIME_USEC
import cmmv

class TestSequenceFunctions(unittest.TestCase):
    """ Test driver class for pcp.mmv module verification """

    def test_mmv(self):
        """ Worker which creates mmv instances, indoms, metrics
            and ties 'em all together via the MemoryMappedValues
            class.
            Once executed, this test create an MMV file that can
            be queried using pmdammv (by a PCP test driver script).
        """

        instances = [mmv.mmv_instance(0, "zero"), mmv.mmv_instance(1, "hero")]
        woodlands = [mmv.mmv_instance(0, "bird"), mmv.mmv_instance(1, "tree"),
                     mmv.mmv_instance(2, "eggs"), mmv.mmv_instance(3, "frog")]
        indoms = [mmv.mmv_indom(serial = 1,
                            shorttext = "We can be heroes",
                            helptext = "Set of instances from zero to hero"),
                  mmv.mmv_indom(serial = 2)]
        indoms[0].set_instances(instances)
        indoms[1].set_instances(woodlands)
        metrics = [mmv.mmv_metric(name = "counter",
                              item = 1,
                              typeof = cmmv.MMV_TYPE_U32,
                              semantics = cmmv.MMV_SEM_COUNTER,
                              dimension = pmapi.pmUnits(0,0,1,0,0,PM_COUNT_ONE),
                              shorttext = "Example counter metric",
                              helptext = "Yep, a test counter metric"),
                   mmv.mmv_metric(name = "instant",
                              item = 2,
                              typeof = cmmv.MMV_TYPE_I32,
                              semantics = cmmv.MMV_SEM_INSTANT,
                              dimension = pmapi.pmUnits(0,0,0,0,0,0),
                              shorttext = "Example instant metric",
                              helptext = "Yep, a test instantaneous metric"),
                   mmv.mmv_metric(name = "indom",
                              item = 3,
                              typeof = cmmv.MMV_TYPE_U32,
                              semantics = cmmv.MMV_SEM_DISCRETE,
                              dimension = pmapi.pmUnits(0,0,0,0,0,0),
                              indom = 1),
                   mmv.mmv_metric(name = "interval",
                              item = 4,
                              typeof = cmmv.MMV_TYPE_ELAPSED,
                              semantics = cmmv.MMV_SEM_COUNTER,
                              dimension = pmapi.pmUnits(0,1,0,0,PM_TIME_USEC,0),
                              indom = 2),
                   mmv.mmv_metric(name = "string",
                              item = 5,
                              typeof = cmmv.MMV_TYPE_STRING,
                              dimension = pmapi.pmUnits(0,0,0,0,0,0),
                              semantics = cmmv.MMV_SEM_INSTANT),
                   mmv.mmv_metric(name = "strings",
                              item = 6,
                              typeof = cmmv.MMV_TYPE_STRING,
                              semantics = cmmv.MMV_SEM_INSTANT,
                              dimension = pmapi.pmUnits(0,0,0,0,0,0),
                              indom = 1,
                              shorttext = "test string metrics",
                              helptext = "Yep, string metric with instances")]

        values = mmv.MemoryMappedValues("pytest")
        self.assertTrue(values != None)
        self.assertTrue(values.started() == 0)
        values.add_indoms(indoms)
        self.assertTrue(values.started() == 0)
        values.add_metrics(metrics)
        self.assertTrue(values.started() == 0)

        values.start()
        self.assertTrue(values.started() == 1)
        caliper = values.lookup_interval_start("interval", "eggs")
        self.assertTrue(caliper != None)
        instant = values.lookup_mapping("instant", None)
        self.assertTrue(instant != None)
        values.add(instant, 41)
        values.inc(instant)
        values.interval_end(caliper)
        values.stop()
        self.assertTrue(values.started() == 0)

        # verification of the metrics values, semantics, indoms
        # and so on is performed in conjunction with pmdammv in
        # a driver script in the pcp/qa sources

if __name__ == '__main__':

    sys.exit(unittest.main())

