Example #1
0
 def setUp(self):
     Venture(name='Test Venture1', symbol='test_venture1',
             venture_id='1').save()
     Venture(name='Test Venture2', symbol='test_venture2',
             venture_id='2').save()
     Venture(name='Test Venture3', symbol='test_venture3',
             venture_id='3').save()
Example #2
0
    def setUp(self):
        self.splunk_venture = Venture(
            name='Splunk unknown usage',
            venture_id=666,
            symbol='splunk_unknown_usage',
        )
        self.splunk_venture.save()
        venture1 = Venture(name='venture1', venture_id=111, symbol='venture1')
        venture1.save()
        venture2 = Venture(name='venture2', venture_id=222, symbol='venture2')
        venture2.save()

        self.device1 = Device(name='test_host1', device_id=1)
        self.device1.save()
        self.device2 = Device(name='test_host2', device_id=2)
        self.device2.save()

        daily_device1 = DailyDevice(
            date=datetime.datetime.today(),
            name='test_host1',
            pricing_venture=venture1,
            pricing_device=self.device1,
        )
        daily_device1.save()
        daily_device2 = DailyDevice(
            date=datetime.datetime.today(),
            name='test_host2',
            pricing_venture=venture2,
            pricing_device=self.device2,
        )
        daily_device2.save()
Example #3
0
class TestSplunkPluginTest(TestCase):
    """ Splunk costs Test Case """
    def setUp(self):
        self.splunk_venture = Venture(
            name='Splunk unknown usage',
            venture_id=666,
            symbol='splunk_unknown_usage',
        )
        self.splunk_venture.save()
        venture1 = Venture(name='venture1', venture_id=111, symbol='venture1')
        venture1.save()
        venture2 = Venture(name='venture2', venture_id=222, symbol='venture2')
        venture2.save()

        self.device1 = Device(name='test_host1', device_id=1)
        self.device1.save()
        self.device2 = Device(name='test_host2', device_id=2)
        self.device2.save()

        daily_device1 = DailyDevice(
            date=datetime.datetime.today(),
            name='test_host1',
            pricing_venture=venture1,
            pricing_device=self.device1,
        )
        daily_device1.save()
        daily_device2 = DailyDevice(
            date=datetime.datetime.today(),
            name='test_host2',
            pricing_venture=venture2,
            pricing_device=self.device2,
        )
        daily_device2.save()

    def test_set_usages(self):
        """ OpenStack usages Test Case """
        # fake setting need to run plugin
        settings.SPLUNK_HOST = 'test'
        settings.SPLUNK_USER = '******'
        settings.SPLUNK_PASSWORD = '******'
        with mock.patch('ralph_pricing.plugins.splunk.Splunk') as Splunk:
            Splunk.side_effect = MockSplunk
            splunk_runner(today=datetime.date.today())
            usage_device1 = DailyUsage.objects.get(pricing_device=self.device1)
            usage_device2 = DailyUsage.objects.get(pricing_device=self.device2)
            usage_splunk_venture = DailyUsage.objects.get(
                pricing_venture=self.splunk_venture,
            )
            self.assertEqual(usage_device1.value, 10318.234132)
            self.assertEqual(usage_device2.value, 1326.640829)
            self.assertEqual(usage_splunk_venture.value, 1048.363416)

    def test_fail_plugin(self):
            """ Testing not configured plugin """
            with mock.patch('ralph_pricing.plugins.splunk.Splunk') as Splunk:
                Splunk.side_effect = MockSplunk
                status, message, arg = splunk_runner(
                    today=datetime.datetime.today(),
                )
                self.assertFalse(status)
Example #4
0
 def setUp(self):
     self.venture_1 = Venture(name="Test Venture1", symbol="test_venture1", venture_id="1")
     self.venture_1.save()
     self.venture_2 = Venture(name="Test Venture2", symbol="test_venture2", venture_id="2")
     self.venture_2.save()
     self.venture_3 = Venture(name="Test Venture3", symbol="test_venture3", venture_id="3")
     self.venture_3.save()
Example #5
0
    def setUp(self):
        super(TestServiceUsagesApi, self).setUp()
        self.resource = 'serviceusages'
        self.user = User.objects.create_user(
            'ralph',
            '*****@*****.**',
            'ralph'
        )
        date2datetime = lambda d: datetime.datetime.combine(
            d, datetime.datetime.min.time()
        )
        self.date = datetime.date(2013, 10, 10)
        self.datetime = date2datetime(self.date)

        self.today = datetime.date.today()
        self.today_datetime = date2datetime(self.today)

        self.service = Service(name='Service1', symbol='s1')
        self.service.save()

        self.venture1 = Venture(
            name='Venture1',
            venture_id=1,
            symbol='v1',
            is_active=True,
        )
        self.venture1.save()
        self.venture2 = Venture(
            name='Venture2',
            venture_id=2,
            symbol='v2',
            is_active=True,
        )
        self.venture2.save()
        self.venture3 = Venture(
            name='Venture3',
            venture_id=3,
            symbol='v3',
            is_active=True,
        )
        self.venture3.save()
        self.inactive_venture = Venture(
            name='Venture4',
            venture_id=4,
            symbol='v4',
            is_active=False,
        )
        self.inactive_venture.save()

        self.usage_type1 = UsageType(name='UsageType1', symbol='ut1')
        self.usage_type1.save()
        self.usage_type2 = UsageType(name='UsageType2', symbol='ut2')
        self.usage_type2.save()

        self.api_key = self.create_apikey(
            self.user.username,
            self.user.api_key.key,
        )
Example #6
0
    def setUp(self):
        super(TestServiceUsagesApi, self).setUp()
        self.resource = 'serviceusages'
        self.user = User.objects.create_user('ralph', '*****@*****.**',
                                             'ralph')
        date2datetime = lambda d: datetime.datetime.combine(
            d, datetime.datetime.min.time())
        self.date = datetime.date(2013, 10, 10)
        self.datetime = date2datetime(self.date)

        self.today = datetime.date.today()
        self.today_datetime = date2datetime(self.today)

        self.service = Service(name='Service1', symbol='s1')
        self.service.save()

        self.venture1 = Venture(
            name='Venture1',
            venture_id=1,
            symbol='v1',
            is_active=True,
        )
        self.venture1.save()
        self.venture2 = Venture(
            name='Venture2',
            venture_id=2,
            symbol='v2',
            is_active=True,
        )
        self.venture2.save()
        self.venture3 = Venture(
            name='Venture3',
            venture_id=3,
            symbol='v3',
            is_active=True,
        )
        self.venture3.save()
        self.inactive_venture = Venture(
            name='Venture4',
            venture_id=4,
            symbol='v4',
            is_active=False,
        )
        self.inactive_venture.save()

        self.usage_type1 = UsageType(name='UsageType1', symbol='ut1')
        self.usage_type1.save()
        self.usage_type2 = UsageType(name='UsageType2', symbol='ut2')
        self.usage_type2.save()

        self.api_key = self.create_apikey(
            self.user.username,
            self.user.api_key.key,
        )
Example #7
0
 def setUp(self):
     self.venture_1 = Venture(
         name='Test Venture1',
         symbol='test_venture1',
         venture_id='1',
     )
     self.venture_1.save()
     self.venture_2 = Venture(
         name='Test Venture2',
         symbol='test_venture2',
         venture_id='2',
     )
     self.venture_2.save()
     self.venture_3 = Venture(
         name='Test Venture3',
         symbol='test_venture3',
         venture_id='3',
     )
     self.venture_3.save()
class TestHamster(TestCase):
    def setUp(self):
        self.venture_1 = Venture(
            name='Test Venture1',
            symbol='test_venture1',
            venture_id='1',
        )
        self.venture_1.save()
        self.venture_2 = Venture(
            name='Test Venture2',
            symbol='test_venture2',
            venture_id='2',
        )
        self.venture_2.save()
        self.venture_3 = Venture(
            name='Test Venture3',
            symbol='test_venture3',
            venture_id='3',
        )
        self.venture_3.save()

    def test_set_usages(self):
        """ Hamster usages Test Case """
        # fake setting need to run plugin
        settings.HAMSTER_API_URL = "/"
        with mock.patch(
            'ralph_pricing.plugins.hamster.get_venture_capacity'
        ) as get_venture_capacity:
            get_venture_capacity.side_effect = mock_get_venture_capacity
            status, message, args = hamster_runner(
                today=datetime.datetime.today()
            )
            self.assertTrue(status)

            usages = DailyUsage.objects.all()
            self.assertEqual(len(usages), 2)

            usage_venture1 = DailyUsage.objects.get(
                pricing_venture=self.venture_1
            )
            usage_venture2 = DailyUsage.objects.get(
                pricing_venture=self.venture_2
            )
            self.assertEqual(
                usage_venture1.value, 2131231233.0 / (1024 * 1024)
            )
            self.assertEqual(
                usage_venture2.value, 4234233423.0 / (1024 * 1024)
            )

    def test_fail_plugin(self):
        """ Testing not configured plugin """
        with mock.patch(
            'ralph_pricing.plugins.hamster.get_venture_capacity'
        ) as get_venture_capacity:
            get_venture_capacity.side_effect = mock_get_venture_capacity
            status, message, args = hamster_runner(
                today=datetime.datetime.today()
            )
            self.assertFalse(status)
Example #9
0
class TestHamster(TestCase):
    def setUp(self):
        self.venture_1 = Venture(
            name='Test Venture1',
            symbol='test_venture1',
            venture_id='1',
        )
        self.venture_1.save()
        self.venture_2 = Venture(
            name='Test Venture2',
            symbol='test_venture2',
            venture_id='2',
        )
        self.venture_2.save()
        self.venture_3 = Venture(
            name='Test Venture3',
            symbol='test_venture3',
            venture_id='3',
        )
        self.venture_3.save()

    def test_set_usages(self):
        """ Hamster usages Test Case """
        # fake setting need to run plugin
        settings.HAMSTER_API_URL = "/"
        with mock.patch(
                'ralph_pricing.plugins.collects.hamster.get_venture_capacity'
        ) as get_venture_capacity:
            get_venture_capacity.side_effect = mock_get_venture_capacity
            status, message, args = hamster_runner(
                today=datetime.datetime.today())
            self.assertTrue(status)

            usages = DailyUsage.objects.all()
            self.assertEqual(len(usages), 2)

            usage_venture1 = DailyUsage.objects.get(
                pricing_venture=self.venture_1)
            usage_venture2 = DailyUsage.objects.get(
                pricing_venture=self.venture_2)
            self.assertEqual(usage_venture1.value,
                             2131231233.0 / (1024 * 1024))
            self.assertEqual(usage_venture2.value,
                             4234233423.0 / (1024 * 1024))

    def test_fail_plugin(self):
        """ Testing not configured plugin """
        with mock.patch(
                'ralph_pricing.plugins.collects.hamster.get_venture_capacity'
        ) as get_venture_capacity:
            get_venture_capacity.side_effect = mock_get_venture_capacity
            status, message, args = hamster_runner(
                today=datetime.datetime.today())
            self.assertFalse(status)
Example #10
0
class TestScaleme(TestCase):
    def setUp(self):
        self.venture_1 = Venture(name="Test Venture1", symbol="test_venture1", venture_id="1")
        self.venture_1.save()
        self.venture_2 = Venture(name="Test Venture2", symbol="test_venture2", venture_id="2")
        self.venture_2.save()
        self.venture_3 = Venture(name="Test Venture3", symbol="test_venture3", venture_id="3")
        self.venture_3.save()

    def test_set_usages(self):
        """Scaleme usages Test Case"""
        settings.SCALEME_API_URL = "/"
        with mock.patch("ralph_pricing.plugins.scaleme.get_ventures_capacities") as get_ventures_capacities:
            get_ventures_capacities.side_effect = mock_get_ventures_capacities
            status, message, args = scaleme_runner(today=datetime.datetime.today())
            self.assertTrue(status)
            usages = DailyUsage.objects.all()
            self.assertEqual(usages.count(), 4)

            usage_backend = UsageType.objects.get(name="Scaleme transforming an image 10000 events")
            usage_cache = UsageType.objects.get(name="Scaleme serving image from cache 10000 events")

            usages_venture1 = DailyUsage.objects.filter(pricing_venture=self.venture_1)
            usages_venture2 = DailyUsage.objects.filter(pricing_venture=self.venture_2)
            usages_venture3 = DailyUsage.objects.filter(pricing_venture=self.venture_3)

            self.assertEqual(usages_venture1.count(), 2)
            usage_backend_venture1 = usages_venture1.filter(type=usage_backend)
            usage_cache_venture1 = usages_venture1.filter(type=usage_cache)
            self.assertEqual(usage_backend_venture1[0].value, 987987)
            self.assertEqual(usage_cache_venture1[0].value, 7878)

            self.assertEqual(usages_venture2.count(), 2)
            usage_backend_venture2 = usages_venture2.filter(type=usage_backend)
            usage_cache_venture2 = usages_venture2.filter(type=usage_cache)
            self.assertEqual(usage_backend_venture2[0].value, 12346699)
            self.assertEqual(usage_cache_venture2[0].value, 666999)

            self.assertEqual(usages_venture3.count(), 0)

    def test_fail_plugin(self):
        """Testing not configured plugin"""
        with mock.patch("ralph_pricing.plugins.scaleme.get_ventures_capacities") as get_ventures_capacities:
            get_ventures_capacities.side_effect = mock_get_ventures_capacities
            status, message, args = scaleme_runner(today=datetime.datetime.today())
            self.assertFalse(status)
Example #11
0
 def setUp(self):
     self.venture_1 = Venture(
         name='Test Venture1',
         symbol='test_venture1',
         venture_id='1',
     )
     self.venture_1.save()
     self.venture_2 = Venture(
         name='Test Venture2',
         symbol='test_venture2',
         venture_id='2',
     )
     self.venture_2.save()
     self.venture_3 = Venture(
         name='Test Venture3',
         symbol='test_venture3',
         venture_id='3',
     )
     self.venture_3.save()
Example #12
0
class TestServiceUsagesApi(ResourceTestCase):
    def setUp(self):
        super(TestServiceUsagesApi, self).setUp()
        self.resource = 'serviceusages'
        self.user = User.objects.create_user('ralph', '*****@*****.**',
                                             'ralph')
        date2datetime = lambda d: datetime.datetime.combine(
            d, datetime.datetime.min.time())
        self.date = datetime.date(2013, 10, 10)
        self.datetime = date2datetime(self.date)

        self.today = datetime.date.today()
        self.today_datetime = date2datetime(self.today)

        self.service = Service(name='Service1', symbol='s1')
        self.service.save()

        self.venture1 = Venture(
            name='Venture1',
            venture_id=1,
            symbol='v1',
            is_active=True,
        )
        self.venture1.save()
        self.venture2 = Venture(
            name='Venture2',
            venture_id=2,
            symbol='v2',
            is_active=True,
        )
        self.venture2.save()
        self.venture3 = Venture(
            name='Venture3',
            venture_id=3,
            symbol='v3',
            is_active=True,
        )
        self.venture3.save()
        self.inactive_venture = Venture(
            name='Venture4',
            venture_id=4,
            symbol='v4',
            is_active=False,
        )
        self.inactive_venture.save()

        self.usage_type1 = UsageType(name='UsageType1', symbol='ut1')
        self.usage_type1.save()
        self.usage_type2 = UsageType(name='UsageType2', symbol='ut2')
        self.usage_type2.save()

        self.api_key = self.create_apikey(
            self.user.username,
            self.user.api_key.key,
        )

    def _get_sample_service_usages_object(self, overwrite=None):
        service_usages = ServiceUsageObject(
            date=self.date,
            service=self.service.symbol,
            venture_usages=[
                VentureUsageObject(
                    venture=self.venture1.symbol,
                    usages=[
                        UsageObject(symbol=self.usage_type1.symbol, value=123),
                        UsageObject(symbol=self.usage_type2.symbol, value=1.2),
                    ]),
                VentureUsageObject(
                    venture=self.venture2.symbol,
                    usages=[
                        UsageObject(symbol=self.usage_type1.symbol, value=3.3),
                        UsageObject(symbol=self.usage_type2.symbol, value=44),
                    ])
            ])
        if overwrite is not None:
            service_usages.overwrite = overwrite
        return service_usages

    def test_save_usages(self):
        service_usages = self._get_sample_service_usages_object()
        ServiceUsageResource.save_usages(service_usages)
        self.assertEquals(DailyUsage.objects.count(), 4)

        daily_usage_1 = DailyUsage.objects.order_by('id')[0]
        self.assertEquals(daily_usage_1.pricing_venture, self.venture1)
        self.assertEquals(daily_usage_1.date, self.datetime)
        self.assertEquals(daily_usage_1.type, self.usage_type1)
        self.assertEquals(daily_usage_1.value, 123)

    def test_to_dict(self):
        service_usages = self._get_sample_service_usages_object()
        self.assertEquals(
            service_usages.to_dict(), {
                'service':
                self.service.symbol,
                'date':
                self.date,
                'overwrite':
                'no',
                'venture_usages': [{
                    'venture':
                    self.venture1.symbol,
                    'usages': [
                        {
                            'symbol': self.usage_type1.symbol,
                            'value': 123,
                        },
                        {
                            'symbol': self.usage_type2.symbol,
                            'value': 1.2,
                        },
                    ]
                }, {
                    'venture':
                    self.venture2.symbol,
                    'usages': [
                        {
                            'symbol': self.usage_type1.symbol,
                            'value': 3.3,
                        },
                        {
                            'symbol': self.usage_type2.symbol,
                            'value': 44,
                        },
                    ]
                }]
            })

    def test_api(self):
        service_usages = self._get_sample_service_usages_object()
        data = service_usages.to_dict()
        resp = self.api_client.post('/scrooge/api/v0.9/{0}/'.format(
            self.resource),
                                    format='json',
                                    authentication=self.api_key,
                                    data=data)
        self.assertEquals(resp.status_code, 201)
        self.assertEquals(DailyUsage.objects.count(), 4)

        daily_usage_1 = DailyUsage.objects.order_by('id')[0]
        self.assertEquals(daily_usage_1.pricing_venture, self.venture1)
        self.assertEquals(daily_usage_1.date, self.datetime)
        self.assertEquals(daily_usage_1.type, self.usage_type1)
        self.assertEquals(daily_usage_1.value, 123)

    def test_api_invalid_service(self):
        service_usages = self._get_sample_service_usages_object()
        data = service_usages.to_dict()
        service_symbol = 'invalid_service'
        data['service'] = service_symbol
        resp = self.api_client.post('/scrooge/api/v0.9/{0}/'.format(
            self.resource),
                                    format='json',
                                    authentication=self.api_key,
                                    data=data)
        self.assertEquals(resp.status_code, 400)
        self.assertEquals(resp.content,
                          'Invalid service symbol: {}'.format(service_symbol))
        self.assertEquals(DailyUsage.objects.count(), 0)

    def test_api_invalid_venture(self):
        service_usages = self._get_sample_service_usages_object()
        data = service_usages.to_dict()
        venture_symbol = 'invalid_venture'
        data['venture_usages'][1]['venture'] = venture_symbol
        resp = self.api_client.post('/scrooge/api/v0.9/{0}/'.format(
            self.resource),
                                    format='json',
                                    authentication=self.api_key,
                                    data=data)
        self.assertEquals(resp.status_code, 400)
        self.assertEquals(
            resp.content,
            'Invalid or inactive venture (symbol: {})'.format(venture_symbol))
        self.assertEquals(DailyUsage.objects.count(), 0)

    def test_api_invalid_usage(self):
        service_usages = self._get_sample_service_usages_object()
        data = service_usages.to_dict()
        usage_type_symbol = 'invalid_usage'
        data['venture_usages'][1]['usages'][1]['symbol'] = usage_type_symbol
        resp = self.api_client.post('/scrooge/api/v0.9/{0}/'.format(
            self.resource),
                                    format='json',
                                    authentication=self.api_key,
                                    data=data)
        self.assertEquals(resp.status_code, 400)
        self.assertEquals(
            resp.content,
            'Invalid usage type symbol: {}'.format(usage_type_symbol))
        self.assertEquals(DailyUsage.objects.count(), 0)

    def test_api_inactive_venture(self):
        service_usages = self._get_sample_service_usages_object()
        service_usages.venture_usages[0].venture = self.inactive_venture.symbol
        data = service_usages.to_dict()
        resp = self.api_client.post('/scrooge/api/v0.9/{0}/'.format(
            self.resource),
                                    format='json',
                                    authentication=self.api_key,
                                    data=data)
        self.assertEquals(resp.status_code, 400)
        self.assertEquals(
            resp.content, 'Invalid or inactive venture (symbol: {})'.format(
                self.inactive_venture.symbol, ))
        self.assertEquals(DailyUsage.objects.count(), 0)

    def _basic_call(self):
        service_usages = self._get_sample_service_usages_object()
        data = service_usages.to_dict()
        resp = self.api_client.post('/scrooge/api/v0.9/{0}/'.format(
            self.resource),
                                    format='json',
                                    authentication=self.api_key,
                                    data=data)
        self.assertEquals(resp.status_code, 201)
        usages = DailyUsage.objects.filter(type=self.usage_type1).values_list(
            'pricing_venture__symbol',
            'value',
        )
        self.assertEquals(dict(usages), {
            self.venture1.symbol: 123.0,
            self.venture2.symbol: 3.3,
        })

    def test_overwrite_values_only(self):
        self._basic_call()

        service_usages = self._get_sample_service_usages_object(
            overwrite='values_only')
        service_usages.venture_usages[0].venture = self.venture3.symbol
        data = service_usages.to_dict()
        resp = self.api_client.post('/scrooge/api/v0.9/{0}/'.format(
            self.resource),
                                    format='json',
                                    authentication=self.api_key,
                                    data=data)
        self.assertEquals(resp.status_code, 201)
        usages = DailyUsage.objects.filter(type=self.usage_type1).values_list(
            'pricing_venture__symbol',
            'value',
        )
        self.assertEquals(
            dict(usages), {
                self.venture1.symbol: 123.0,
                self.venture2.symbol: 3.3,
                self.venture3.symbol: 123.0,
            })

    def test_overwrite_delete_all_previous(self):
        self._basic_call()

        service_usages = self._get_sample_service_usages_object(
            overwrite='delete_all_previous')
        service_usages.venture_usages[0].venture = self.venture3.symbol
        data = service_usages.to_dict()
        resp = self.api_client.post('/scrooge/api/v0.9/{0}/'.format(
            self.resource),
                                    format='json',
                                    authentication=self.api_key,
                                    data=data)
        self.assertEquals(resp.status_code, 201)
        usages = DailyUsage.objects.filter(type=self.usage_type1).values_list(
            'pricing_venture__symbol',
            'value',
        )
        self.assertEquals(dict(usages), {
            self.venture2.symbol: 3.3,
            self.venture3.symbol: 123.0,
        })

    def test_not_overwriting(self):
        self._basic_call()

        service_usages = self._get_sample_service_usages_object(overwrite='no')
        service_usages.venture_usages[0].venture = self.venture3.symbol
        data = service_usages.to_dict()
        resp = self.api_client.post('/scrooge/api/v0.9/{0}/'.format(
            self.resource),
                                    format='json',
                                    authentication=self.api_key,
                                    data=data)
        self.assertEquals(resp.status_code, 201)
        usages = DailyUsage.objects.filter(type=self.usage_type1).values_list(
            'pricing_venture__symbol',
            'value',
        )
        self.assertEquals([a for a in usages], [(self.venture1.symbol, 123.0),
                                                (self.venture2.symbol, 3.3),
                                                (self.venture3.symbol, 123.0),
                                                (self.venture2.symbol, 3.3)])
Example #13
0
class TestServiceUsagesApi(ResourceTestCase):
    def setUp(self):
        super(TestServiceUsagesApi, self).setUp()
        self.resource = 'serviceusages'
        self.user = User.objects.create_user(
            'ralph',
            '*****@*****.**',
            'ralph'
        )
        date2datetime = lambda d: datetime.datetime.combine(
            d, datetime.datetime.min.time()
        )
        self.date = datetime.date(2013, 10, 10)
        self.datetime = date2datetime(self.date)

        self.today = datetime.date.today()
        self.today_datetime = date2datetime(self.today)

        self.service = Service(name='Service1', symbol='s1')
        self.service.save()

        self.venture1 = Venture(
            name='Venture1',
            venture_id=1,
            symbol='v1',
            is_active=True,
        )
        self.venture1.save()
        self.venture2 = Venture(
            name='Venture2',
            venture_id=2,
            symbol='v2',
            is_active=True,
        )
        self.venture2.save()
        self.inactive_venture = Venture(
            name='Venture3',
            venture_id=3,
            symbol='v3',
            is_active=False,
        )
        self.inactive_venture.save()

        self.usage_type1 = UsageType(name='UsageType1', symbol='ut1')
        self.usage_type1.save()
        self.usage_type2 = UsageType(name='UsageType2', symbol='ut2')
        self.usage_type2.save()

        self.api_key = self.create_apikey(
            self.user.username,
            self.user.api_key.key,
        )

    def _get_sample_service_usages_object(self):
        service_usages = ServiceUsageObject(
            date=self.date,
            service=self.service.symbol,
            venture_usages=[
                VentureUsageObject(
                    venture=self.venture1.symbol,
                    usages=[
                        UsageObject(symbol=self.usage_type1.symbol, value=123),
                        UsageObject(symbol=self.usage_type2.symbol, value=1.2),
                    ]
                ),
                VentureUsageObject(
                    venture=self.venture2.symbol,
                    usages=[
                        UsageObject(symbol=self.usage_type1.symbol, value=3.3),
                        UsageObject(symbol=self.usage_type2.symbol, value=44),
                    ]
                )
            ]
        )
        return service_usages

    def test_save_usages(self):
        service_usages = self._get_sample_service_usages_object()
        ServiceUsageResource.save_usages(service_usages)
        self.assertEquals(DailyUsage.objects.count(), 4)

        daily_usage_1 = DailyUsage.objects.order_by('id')[0]
        self.assertEquals(daily_usage_1.pricing_venture, self.venture1)
        self.assertEquals(daily_usage_1.date, self.datetime)
        self.assertEquals(daily_usage_1.type, self.usage_type1)
        self.assertEquals(daily_usage_1.value, 123)

    def test_to_dict(self):
        service_usages = self._get_sample_service_usages_object()
        self.assertEquals(service_usages.to_dict(), {
            'service': self.service.symbol,
            'date': self.date,
            'venture_usages': [
                {
                    'venture': self.venture1.symbol,
                    'usages': [
                        {
                            'symbol': self.usage_type1.symbol,
                            'value': 123,
                        },
                        {
                            'symbol': self.usage_type2.symbol,
                            'value': 1.2,
                        },
                    ]
                },
                {
                    'venture': self.venture2.symbol,
                    'usages': [
                        {
                            'symbol': self.usage_type1.symbol,
                            'value': 3.3,
                        },
                        {
                            'symbol': self.usage_type2.symbol,
                            'value': 44,
                        },
                    ]
                }
            ]
        })

    def test_api(self):
        service_usages = self._get_sample_service_usages_object()
        data = service_usages.to_dict()
        resp = self.api_client.post(
            '/scrooge/api/v0.9/{0}/'.format(self.resource),
            format='json',
            authentication=self.api_key,
            data=data
        )
        self.assertEquals(resp.status_code, 201)
        self.assertEquals(DailyUsage.objects.count(), 4)

        daily_usage_1 = DailyUsage.objects.order_by('id')[0]
        self.assertEquals(daily_usage_1.pricing_venture, self.venture1)
        self.assertEquals(daily_usage_1.date, self.datetime)
        self.assertEquals(daily_usage_1.type, self.usage_type1)
        self.assertEquals(daily_usage_1.value, 123)

    def test_api_invalid_service(self):
        service_usages = self._get_sample_service_usages_object()
        data = service_usages.to_dict()
        data['service'] = 'invalid_service'
        resp = self.api_client.post(
            '/scrooge/api/v0.9/{0}/'.format(self.resource),
            format='json',
            authentication=self.api_key,
            data=data
        )
        self.assertEquals(resp.status_code, 400)
        self.assertEquals(resp.content, 'Invalid service symbol')
        self.assertEquals(DailyUsage.objects.count(), 0)

    def test_api_invalid_venture(self):
        service_usages = self._get_sample_service_usages_object()
        data = service_usages.to_dict()
        data['venture_usages'][1]['venture'] = 'invalid_venture'
        resp = self.api_client.post(
            '/scrooge/api/v0.9/{0}/'.format(self.resource),
            format='json',
            authentication=self.api_key,
            data=data
        )
        self.assertEquals(resp.status_code, 400)
        self.assertEquals(
            resp.content,
            'Invalid venture symbol or venture is inactive'
        )
        self.assertEquals(DailyUsage.objects.count(), 0)

    def test_api_invalid_usage(self):
        service_usages = self._get_sample_service_usages_object()
        data = service_usages.to_dict()
        data['venture_usages'][1]['usages'][1]['symbol'] = 'invalid_usage'
        resp = self.api_client.post(
            '/scrooge/api/v0.9/{0}/'.format(self.resource),
            format='json',
            authentication=self.api_key,
            data=data
        )
        self.assertEquals(resp.status_code, 400)
        self.assertEquals(resp.content, 'Invalid usage type symbol')
        self.assertEquals(DailyUsage.objects.count(), 0)

    def test_api_inactive_venture(self):
        service_usages = self._get_sample_service_usages_object()
        service_usages.venture_usages[0].venture = self.inactive_venture.symbol
        data = service_usages.to_dict()
        resp = self.api_client.post(
            '/scrooge/api/v0.9/{0}/'.format(self.resource),
            format='json',
            authentication=self.api_key,
            data=data
        )
        self.assertEquals(resp.status_code, 400)
        self.assertEquals(
            resp.content,
            'Invalid venture symbol or venture is inactive'
        )
        self.assertEquals(DailyUsage.objects.count(), 0)
Example #14
0
class TestScaleme(TestCase):
    def setUp(self):
        self.venture_1 = Venture(
            name='Test Venture1',
            symbol='test_venture1',
            venture_id='1',
        )
        self.venture_1.save()
        self.venture_2 = Venture(
            name='Test Venture2',
            symbol='test_venture2',
            venture_id='2',
        )
        self.venture_2.save()
        self.venture_3 = Venture(
            name='Test Venture3',
            symbol='test_venture3',
            venture_id='3',
        )
        self.venture_3.save()

    def test_set_usages(self):
        """Scaleme usages Test Case"""
        settings.SCALEME_API_URL = "/"
        with mock.patch(
                'ralph_pricing.plugins.collects.scaleme.get_ventures_capacities'
        ) as get_ventures_capacities:
            get_ventures_capacities.side_effect = mock_get_ventures_capacities
            status, message, args = scaleme_runner(
                today=datetime.datetime.today())
            self.assertTrue(status)
            usages = DailyUsage.objects.all()
            self.assertEqual(usages.count(), 4)

            usage_backend = UsageType.objects.get(
                name='Scaleme transforming an image 10000 events', )
            usage_cache = UsageType.objects.get(
                name='Scaleme serving image from cache 10000 events', )

            usages_venture1 = DailyUsage.objects.filter(
                pricing_venture=self.venture_1, )
            usages_venture2 = DailyUsage.objects.filter(
                pricing_venture=self.venture_2, )
            usages_venture3 = DailyUsage.objects.filter(
                pricing_venture=self.venture_3, )

            self.assertEqual(usages_venture1.count(), 2)
            usage_backend_venture1 = usages_venture1.filter(type=usage_backend)
            usage_cache_venture1 = usages_venture1.filter(type=usage_cache)
            self.assertEqual(usage_backend_venture1[0].value, 987987)
            self.assertEqual(usage_cache_venture1[0].value, 7878)

            self.assertEqual(usages_venture2.count(), 2)
            usage_backend_venture2 = usages_venture2.filter(type=usage_backend)
            usage_cache_venture2 = usages_venture2.filter(type=usage_cache)
            self.assertEqual(usage_backend_venture2[0].value, 12346699)
            self.assertEqual(usage_cache_venture2[0].value, 666999)

            self.assertEqual(usages_venture3.count(), 0)

    def test_fail_plugin(self):
        """Testing not configured plugin"""
        with mock.patch(
                'ralph_pricing.plugins.collects.scaleme.get_ventures_capacities'
        ) as get_ventures_capacities:
            get_ventures_capacities.side_effect = mock_get_ventures_capacities
            status, message, args = scaleme_runner(
                today=datetime.datetime.today(), )
            self.assertFalse(status)
Example #15
0
class TestServiceUsagesApi(ResourceTestCase):
    def setUp(self):
        super(TestServiceUsagesApi, self).setUp()
        self.resource = 'serviceusages'
        self.user = User.objects.create_user(
            'ralph',
            '*****@*****.**',
            'ralph'
        )
        date2datetime = lambda d: datetime.datetime.combine(
            d, datetime.datetime.min.time()
        )
        self.date = datetime.date(2013, 10, 10)
        self.datetime = date2datetime(self.date)

        self.today = datetime.date.today()
        self.today_datetime = date2datetime(self.today)

        self.service = Service(name='Service1', symbol='s1')
        self.service.save()

        self.venture1 = Venture(
            name='Venture1',
            venture_id=1,
            symbol='v1',
            is_active=True,
        )
        self.venture1.save()
        self.venture2 = Venture(
            name='Venture2',
            venture_id=2,
            symbol='v2',
            is_active=True,
        )
        self.venture2.save()
        self.venture3 = Venture(
            name='Venture3',
            venture_id=3,
            symbol='v3',
            is_active=True,
        )
        self.venture3.save()
        self.inactive_venture = Venture(
            name='Venture4',
            venture_id=4,
            symbol='v4',
            is_active=False,
        )
        self.inactive_venture.save()

        self.usage_type1 = UsageType(name='UsageType1', symbol='ut1')
        self.usage_type1.save()
        self.usage_type2 = UsageType(name='UsageType2', symbol='ut2')
        self.usage_type2.save()

        self.api_key = self.create_apikey(
            self.user.username,
            self.user.api_key.key,
        )

    def _get_sample_service_usages_object(self, overwrite=None):
        service_usages = ServiceUsageObject(
            date=self.date,
            service=self.service.symbol,
            venture_usages=[
                VentureUsageObject(
                    venture=self.venture1.symbol,
                    usages=[
                        UsageObject(symbol=self.usage_type1.symbol, value=123),
                        UsageObject(symbol=self.usage_type2.symbol, value=1.2),
                    ]
                ),
                VentureUsageObject(
                    venture=self.venture2.symbol,
                    usages=[
                        UsageObject(symbol=self.usage_type1.symbol, value=3.3),
                        UsageObject(symbol=self.usage_type2.symbol, value=44),
                    ]
                )
            ]
        )
        if overwrite is not None:
            service_usages.overwrite = overwrite
        return service_usages

    def test_save_usages(self):
        service_usages = self._get_sample_service_usages_object()
        ServiceUsageResource.save_usages(service_usages)
        self.assertEquals(DailyUsage.objects.count(), 4)

        daily_usage_1 = DailyUsage.objects.order_by('id')[0]
        self.assertEquals(daily_usage_1.pricing_venture, self.venture1)
        self.assertEquals(daily_usage_1.date, self.datetime)
        self.assertEquals(daily_usage_1.type, self.usage_type1)
        self.assertEquals(daily_usage_1.value, 123)

    def test_to_dict(self):
        service_usages = self._get_sample_service_usages_object()
        self.assertEquals(service_usages.to_dict(), {
            'service': self.service.symbol,
            'date': self.date,
            'overwrite': 'no',
            'venture_usages': [
                {
                    'venture': self.venture1.symbol,
                    'usages': [
                        {
                            'symbol': self.usage_type1.symbol,
                            'value': 123,
                        },
                        {
                            'symbol': self.usage_type2.symbol,
                            'value': 1.2,
                        },
                    ]
                },
                {
                    'venture': self.venture2.symbol,
                    'usages': [
                        {
                            'symbol': self.usage_type1.symbol,
                            'value': 3.3,
                        },
                        {
                            'symbol': self.usage_type2.symbol,
                            'value': 44,
                        },
                    ]
                }
            ]
        })

    def test_api(self):
        service_usages = self._get_sample_service_usages_object()
        data = service_usages.to_dict()
        resp = self.api_client.post(
            '/pricing/api/v0.9/{0}/'.format(self.resource),
            format='json',
            authentication=self.api_key,
            data=data
        )
        self.assertEquals(resp.status_code, 201)
        self.assertEquals(DailyUsage.objects.count(), 4)

        daily_usage_1 = DailyUsage.objects.order_by('id')[0]
        self.assertEquals(daily_usage_1.pricing_venture, self.venture1)
        self.assertEquals(daily_usage_1.date, self.datetime)
        self.assertEquals(daily_usage_1.type, self.usage_type1)
        self.assertEquals(daily_usage_1.value, 123)

    def test_api_invalid_service(self):
        service_usages = self._get_sample_service_usages_object()
        data = service_usages.to_dict()
        service_symbol = 'invalid_service'
        data['service'] = service_symbol
        resp = self.api_client.post(
            '/pricing/api/v0.9/{0}/'.format(self.resource),
            format='json',
            authentication=self.api_key,
            data=data
        )
        self.assertEquals(resp.status_code, 400)
        self.assertEquals(
            resp.content,
            'Invalid service symbol: {}'.format(service_symbol)
        )
        self.assertEquals(DailyUsage.objects.count(), 0)

    def test_api_invalid_venture(self):
        service_usages = self._get_sample_service_usages_object()
        data = service_usages.to_dict()
        venture_symbol = 'invalid_venture'
        data['venture_usages'][1]['venture'] = venture_symbol
        resp = self.api_client.post(
            '/pricing/api/v0.9/{0}/'.format(self.resource),
            format='json',
            authentication=self.api_key,
            data=data
        )
        self.assertEquals(resp.status_code, 400)
        self.assertEquals(
            resp.content,
            'Invalid or inactive venture (symbol: {})'.format(venture_symbol)
        )
        self.assertEquals(DailyUsage.objects.count(), 0)

    def test_api_invalid_usage(self):
        service_usages = self._get_sample_service_usages_object()
        data = service_usages.to_dict()
        usage_type_symbol = 'invalid_usage'
        data['venture_usages'][1]['usages'][1]['symbol'] = usage_type_symbol
        resp = self.api_client.post(
            '/pricing/api/v0.9/{0}/'.format(self.resource),
            format='json',
            authentication=self.api_key,
            data=data
        )
        self.assertEquals(resp.status_code, 400)
        self.assertEquals(
            resp.content,
            'Invalid usage type symbol: {}'.format(usage_type_symbol)
        )
        self.assertEquals(DailyUsage.objects.count(), 0)

    def test_api_inactive_venture(self):
        service_usages = self._get_sample_service_usages_object()
        service_usages.venture_usages[0].venture = self.inactive_venture.symbol
        data = service_usages.to_dict()
        resp = self.api_client.post(
            '/pricing/api/v0.9/{0}/'.format(self.resource),
            format='json',
            authentication=self.api_key,
            data=data
        )
        self.assertEquals(resp.status_code, 400)
        self.assertEquals(
            resp.content,
            'Invalid or inactive venture (symbol: {})'.format(
                self.inactive_venture.symbol,
            )
        )
        self.assertEquals(DailyUsage.objects.count(), 0)

    def _basic_call(self):
        service_usages = self._get_sample_service_usages_object()
        data = service_usages.to_dict()
        resp = self.api_client.post(
            '/pricing/api/v0.9/{0}/'.format(self.resource),
            format='json',
            authentication=self.api_key,
            data=data
        )
        self.assertEquals(resp.status_code, 201)
        usages = DailyUsage.objects.filter(type=self.usage_type1).values_list(
            'pricing_venture__symbol',
            'value',
        )
        self.assertEquals(dict(usages), {
            self.venture1.symbol: 123.0,
            self.venture2.symbol: 3.3,
        })

    def test_overwrite_values_only(self):
        self._basic_call()

        service_usages = self._get_sample_service_usages_object(
            overwrite='values_only'
        )
        service_usages.venture_usages[0].venture = self.venture3.symbol
        data = service_usages.to_dict()
        resp = self.api_client.post(
            '/pricing/api/v0.9/{0}/'.format(self.resource),
            format='json',
            authentication=self.api_key,
            data=data
        )
        self.assertEquals(resp.status_code, 201)
        usages = DailyUsage.objects.filter(type=self.usage_type1).values_list(
            'pricing_venture__symbol',
            'value',
        )
        self.assertEquals(dict(usages), {
            self.venture1.symbol: 123.0,
            self.venture2.symbol: 3.3,
            self.venture3.symbol: 123.0,
        })

    def test_overwrite_delete_all_previous(self):
        self._basic_call()

        service_usages = self._get_sample_service_usages_object(
            overwrite='delete_all_previous'
        )
        service_usages.venture_usages[0].venture = self.venture3.symbol
        data = service_usages.to_dict()
        resp = self.api_client.post(
            '/pricing/api/v0.9/{0}/'.format(self.resource),
            format='json',
            authentication=self.api_key,
            data=data
        )
        self.assertEquals(resp.status_code, 201)
        usages = DailyUsage.objects.filter(type=self.usage_type1).values_list(
            'pricing_venture__symbol',
            'value',
        )
        self.assertEquals(dict(usages), {
            self.venture2.symbol: 3.3,
            self.venture3.symbol: 123.0,
        })

    def test_not_overwriting(self):
        self._basic_call()

        service_usages = self._get_sample_service_usages_object(
            overwrite='no'
        )
        service_usages.venture_usages[0].venture = self.venture3.symbol
        data = service_usages.to_dict()
        resp = self.api_client.post(
            '/pricing/api/v0.9/{0}/'.format(self.resource),
            format='json',
            authentication=self.api_key,
            data=data
        )
        self.assertEquals(resp.status_code, 201)
        usages = DailyUsage.objects.filter(type=self.usage_type1).values_list(
            'pricing_venture__symbol',
            'value',
        )
        self.assertEquals([a for a in usages], [
            (self.venture1.symbol, 123.0),
            (self.venture2.symbol, 3.3),
            (self.venture3.symbol, 123.0),
            (self.venture2.symbol, 3.3)]
        )