def test_ceilometer_mysql_plugin(
        self,
        save_ceilometer_usages_mock,
        clear_ceilometer_stats_mock,
        get_ceilometer_usages_mock,
    ):
        get_ceilometer_usages_mock.return_value = SAMPLE_CEILOMETER
        save_ceilometer_usages_mock.return_value = (3, 4)

        warehouse1 = WarehouseFactory(name='WH1')
        warehouse2 = WarehouseFactory(name='WH2')

        result = ceilometer_mysql_plugin(self.today)
        self.assertEquals(result, (True, 'Ceilometer usages: 6 new, 8 total'))
        clear_ceilometer_stats_mock.assert_called(self.today)
        self.assertEquals(get_ceilometer_usages_mock.call_count, 2)
        get_ceilometer_usages_mock.assert_any_call(
            self.today,
            CEILOMETER_SETTINGS[0]['CEILOMETER_CONNECTION'],
        )
        self.assertEquals(get_ceilometer_usages_mock.call_count, 2)
        save_ceilometer_usages_mock.assert_any_call(
            SAMPLE_CEILOMETER,
            self.today,
            warehouse1,
        )
        save_ceilometer_usages_mock.assert_any_call(
            SAMPLE_CEILOMETER,
            self.today,
            warehouse2,
        )
Пример #2
0
    def setUp(self):
        self.se1, self.se2 = ServiceEnvironmentFactory.create_batch(2)
        self.po1 = PricingObjectFactory(service_environment=self.se1)
        self.po2 = PricingObjectFactory(service_environment=self.se2)

        self.bu1 = ExtraCostTypeFactory()
        self.bu2 = UsageTypeFactory()
        self.bu3 = PricingServiceFactory()
        self.bu4 = PricingServiceFactory()

        self.wh1 = WarehouseFactory()
        self.wh2 = WarehouseFactory()
 def test_save_ceilometer_usages(self):
     warehouse = WarehouseFactory()
     daily_tenant1 = DailyTenantInfoFactory(date=self.today)
     daily_tenant2 = DailyTenantInfoFactory(date=self.today)
     daily_tenant3 = DailyTenantInfoFactory.build(date=self.today)
     instance1_usage_type = get_usage_type('instance1')
     usages = [
         (daily_tenant1.tenant_info.tenant_id, 100, 'instance1'),
         (daily_tenant1.tenant_info.tenant_id, 200, 'instance2'),
         (daily_tenant2.tenant_info.tenant_id, 300, 'instance1'),
         (daily_tenant3.tenant_info.tenant_id, 100, 'instance1'),
     ]
     self.assertEquals(
         (3, 4),
         save_ceilometer_usages(usages, self.today, warehouse)
     )
     daily_tenant2_usage = daily_tenant2.dailyusage_set.all()[:1].get()
     self.assertEquals(DailyUsage.objects.count(), 3)
     self.assertEquals(daily_tenant2_usage.date, self.today)
     self.assertEquals(
         daily_tenant2_usage.service_environment,
         daily_tenant2.service_environment
     )
     self.assertEquals(
         daily_tenant2_usage.daily_pricing_object,
         daily_tenant2.dailypricingobject_ptr
     )
     self.assertEquals(daily_tenant2_usage.value, 50)
     self.assertEquals(daily_tenant2_usage.type, instance1_usage_type)
     self.assertEquals(daily_tenant2_usage.warehouse, warehouse)
Пример #4
0
 def setUp(self):
     # self.service = ServiceFactory.create()
     # self.environment = EnvironmentFactory.create()
     self.service_environment = ServiceEnvironmentFactory()
     self.date = datetime.date.today()
     self.warehouse = WarehouseFactory.create()
     self.model = PricingObjectModelFactory()
     self.value = 100
     self.data = {
         'asset_id': 1,
         'sn': 'SerialNumber',
         'barcode': 'Barcode',
         'device_id': 1,
         'asset_name': 'AssetName',
         'depreciation_rate': 25,
         'is_depreciated': True,
         'price': 100,
         'service_id': self.service_environment.service.ci_id,
         'warehouse_id': self.warehouse.id_from_assets,
         'environment_id': self.service_environment.environment.ci_id,  # noqa
         'cores_count': 4,
         'power_consumption': 200,
         'collocation': 2,
         'model_id': self.model.model_id,
     }
Пример #5
0
 def setUp(self):
     # self.service = ServiceFactory.create()
     # self.environment = EnvironmentFactory.create()
     self.service_environment = ServiceEnvironmentFactory()
     self.date = datetime.date.today()
     self.warehouse = WarehouseFactory.create()
     self.model = PricingObjectModelFactory()
     self.value = 100
     self.data = {
         'asset_id': 1,
         'sn': 'SerialNumber',
         'barcode': 'Barcode',
         'device_id': 1,
         'asset_name': 'AssetName',
         'depreciation_rate': 25,
         'is_depreciated': True,
         'price': 100,
         'service_id': self.service_environment.service.ci_id,
         'warehouse_id': self.warehouse.id_from_assets,
         'environment_id':
         self.service_environment.environment.ci_id,  # noqa
         'cores_count': 4,
         'power_consumption': 200,
         'collocation': 2,
         'model_id': self.model.model_id,
     }
Пример #6
0
 def test_update_assets_when_warehouse_does_not_exist(self):
     self.data['warehouse_id'] = WarehouseFactory.build().id_from_assets
     with self.assertRaises(asset.WarehouseDoesNotExistError):
         asset.update_assets(
             self.data, self.date, {
                 'depreciation': UsageTypeFactory.create(),
                 'assets_count': UsageTypeFactory.create(),
                 'cores_count': UsageTypeFactory.create(),
                 'power_consumption': UsageTypeFactory.create(),
                 'collocation': UsageTypeFactory.create(),
             })
Пример #7
0
 def test_update_assets_when_warehouse_does_not_exist(self):
     self.data['warehouse_id'] = WarehouseFactory.build().id_from_assets
     with self.assertRaises(asset.WarehouseDoesNotExistError):
         asset.update_assets(
             self.data,
             self.date,
             {
                 'depreciation': UsageTypeFactory.create(),
                 'assets_count': UsageTypeFactory.create(),
                 'cores_count': UsageTypeFactory.create(),
                 'power_consumption': UsageTypeFactory.create(),
                 'collocation': UsageTypeFactory.create(),
             }
         )
Пример #8
0
 def setUp(self):
     self.today = datetime.date(2014, 7, 1)
     self.yesterday = self.today - datetime.timedelta(days=1)
     self.warehouse = WarehouseFactory()
Пример #9
0
    def _set_sample_usages(self):
        # 3 Pricing Services (1 tested and dependent from others)
        # usage types:
        self.ps1, self.ps2, self.ps3 = PricingServiceFactory.create_batch(3)
        se1 = ServiceEnvironmentFactory(service__pricing_service=self.ps1)
        se2 = ServiceEnvironmentFactory(service__pricing_service=self.ps1)

        ut1, ut2, ut3 = UsageTypeFactory.create_batch(3, usage_type='SU')

        models.ServiceUsageTypes(
            usage_type=ut1,
            pricing_service=self.ps2,
            start=datetime.date(2013, 10, 1),
            end=datetime.date(2013, 10, 30),
            percent=50,
        ).save()
        models.ServiceUsageTypes(
            usage_type=ut2,
            pricing_service=self.ps2,
            start=datetime.date(2013, 10, 1),
            end=datetime.date(2013, 10, 30),
            percent=50,
        ).save()
        models.ServiceUsageTypes(
            usage_type=ut3,
            pricing_service=self.ps3,
            start=datetime.date(2013, 10, 1),
            end=datetime.date(2013, 10, 30),
            percent=100,
        ).save()

        # sample usages
        models.DailyUsage(
            daily_pricing_object=DailyPricingObjectFactory(),
            type=ut1,
            date=datetime.date(2013, 10, 3),
            service_environment=se1,
            value=100,
        ).save()
        models.DailyUsage(
            daily_pricing_object=DailyPricingObjectFactory(),
            type=ut2,
            date=datetime.date(2013, 10, 20),
            service_environment=se1,
            value=100,
        ).save()
        models.DailyUsage(
            daily_pricing_object=DailyPricingObjectFactory(),
            type=ut1,
            date=datetime.date(2013, 10, 28),
            service_environment=se2,
            value=100,
            warehouse=WarehouseFactory(),
        ).save()
        models.DailyUsage(
            daily_pricing_object=DailyPricingObjectFactory(),
            type=ut3,
            date=datetime.date(2013, 10, 3),
            service_environment=se2,
            value=100,
        ).save()
Пример #10
0
    def setUp(self):
        self.plugin = SampleCostPlugin()

        # usage types
        self.usage_type = UsageTypeFactory(
            symbol='ut1',
            by_warehouse=False,
            by_cost=False,
            usage_type='BU',
        )
        self.usage_type_cost_wh = UsageTypeFactory(
            symbol='ut2',
            by_warehouse=True,
            by_cost=True,
            usage_type='BU',
        )

        # warehouses
        self.default_warehouse = models.Warehouse.objects.get(name='Default')
        self.warehouse1 = WarehouseFactory(show_in_report=True)
        self.warehouse2 = WarehouseFactory(show_in_report=True)
        self.warehouses = models.Warehouse.objects.exclude(
            pk=self.default_warehouse.pk)
        # services
        self.service_environment1 = ServiceEnvironmentFactory()
        self.service_environment2 = ServiceEnvironmentFactory()
        self.service_environment3 = ServiceEnvironmentFactory()
        self.service_environment4 = ServiceEnvironmentFactory()
        self.service_environments = models.ServiceEnvironment.objects.all()

        # daily usages of base type
        # ut1:
        #   service1: 10
        #   service2: 20
        # ut2:
        #   service1: 20 (half in warehouse1, half in warehouse2)
        #   service2: 40 (half in warehouse1, half in warehouse2)
        start = datetime.date(2013, 10, 8)
        end = datetime.date(2013, 10, 22)
        base_usage_types = models.UsageType.objects.filter(usage_type='BU')
        for i, ut in enumerate(base_usage_types, start=1):
            days = rrule.rrule(rrule.DAILY, dtstart=start, until=end)
            for j, day in enumerate(days, start=1):
                for k, service_environment in enumerate(
                        self.service_environments, start=1):
                    daily_usage = DailyUsageFactory(
                        date=day,
                        service_environment=service_environment,
                        value=10 * i * k,
                        type=ut,
                        warehouse=self.default_warehouse,
                    )
                    if ut.by_warehouse:
                        daily_usage.warehouse = (
                            self.warehouses[j % len(self.warehouses)])
                    daily_usage.save()
        # usage prices
        dates = [
            (datetime.date(2013, 10, 5), datetime.date(2013, 10, 12)),
            (datetime.date(2013, 10, 13), datetime.date(2013, 10, 17)),
            (datetime.date(2013, 10, 18), datetime.date(2013, 10, 25)),
        ]
        # (real cost/price, forecast cost/price)
        ut_prices_costs = [
            (self.usage_type, [(10, 50), (20, 60), (30, 70)]),
            (
                self.usage_type_cost_wh,
                [
                    [(3600, 2400), (5400, 5400), (4800, 12000)],  # warehouse1
                    [(3600, 5400), (3600, 1200), (7200, 3600)],  # warehouse2
                ]),
        ]

        def add_usage_price(usage_type, prices_costs, warehouse=None):
            for daterange, price_cost in zip(dates, prices_costs):
                start, end = daterange
                usage_price = models.UsagePrice(
                    type=usage_type,
                    start=start,
                    end=end,
                )
                if warehouse is not None:
                    usage_price.warehouse = warehouse
                if usage_type.by_cost:
                    usage_price.cost = price_cost[0]
                    usage_price.forecast_cost = price_cost[1]
                else:
                    usage_price.price = price_cost[0]
                    usage_price.forecast_price = price_cost[1]
                usage_price.save()

        for ut, prices in ut_prices_costs:
            if ut.by_warehouse:
                for i, prices_wh in enumerate(prices):
                    warehouse = self.warehouses[i]
                    add_usage_price(ut, prices_wh, warehouse)
            else:
                add_usage_price(ut, prices)