def setUp(self): # fake settings needed to run plugin, even with mocked library. settings.OPENSTACK_URL = '/' settings.OPENSTACK_USER = '******' settings.OPENSTACK_PASSWORD = '******' # model group for sample costs calculations. self.cmg = ComponentModelGroup() self.cmg.price = 100.0 self.cmg.per_size = 1 self.cmg.size_modifier = 1 self.cmg.save() self.mk = MarginKind() self.mk.margin = 200 self.mk.remarks = 'Margin kind 200%' self.mk.save()
def test_cost(self): dev = Device.create(sn='device', model_type=DeviceType.rack_server, model_name='device') dmg = DeviceModelGroup(name='DeviceModelGroup') dmg.price = 100 dmg.save() dev.model.group = dmg dev.model.save() mk = MarginKind(name='50%', margin=50) mk.save() dev.margin_kind = mk dk = DeprecationKind(name='10 months', months=10) dk.save() dev.deprecation_kind = dk dev.save() self.assertEqual(pricing.get_device_cost(dev), 15) pricing.device_update_cached(dev) dev = Device.objects.get(id=dev.id) self.assertEqual(pricing.get_device_cost(dev), 15) self.assertEqual(dev.cached_cost, 15)
def test_price_deprecation(self): # Device after deprecation period should have raw price = 0 dev = Device.create(sn='device', model_type=DeviceType.rack_server, model_name='device') dmg = DeviceModelGroup(name='DeviceModelGroup') dmg.price = 100 dmg.save() dev.model.group = dmg dev.purchase_date = datetime.today() - timedelta(11 * (365 / 12)) dev.model.save() mk = MarginKind(name='50%', margin=50) mk.save() dev.margin_kind = mk dk = DeprecationKind(name='10 months', months=10) dk.save() dev.deprecation_kind = dk dev.save() pricing.device_update_cached(dev) dev = Device.objects.get(id=dev.id) self.assertEqual(get_device_raw_price(dev), 0) self.assertEqual(dev.cached_price, 0) self.assertEqual(dev.cached_cost, 0)
def test_price_deprecation_in_progress(self): # Device in deprecation period should have raw price >0 if price is set dev = Device.create(sn='device', model_type=DeviceType.rack_server, model_name='device') dmg = DeviceModelGroup(name='DeviceModelGroup') dmg.price = 100 dmg.save() dev.model.group = dmg # currently first month in deprecation period dev.purchase_date = datetime.today() - timedelta(1 * (365 / 12)) dev.model.save() dev.margin_kind = MarginKind(name='50%', margin=50) dev.margin_kind.save() dev.deprecation_kind = DeprecationKind(name='10 months', months=10) dev.deprecation_kind.save() dev.save() pricing.device_update_cached(dev) self.assertEqual(get_device_raw_price(dev), 100) self.assertEqual(dev.cached_cost, 15) self.assertEqual(dev.cached_price, 100)
class OpenStackPluginTest(TestCase): """ OpenStack costs Test Case """ def setUp(self): # fake settings needed to run plugin, even with mocked library. settings.OPENSTACK_URL = '/' settings.OPENSTACK_USER = '******' settings.OPENSTACK_PASSWORD = '******' # model group for sample costs calculations. self.cmg = ComponentModelGroup() self.cmg.price = 100.0 self.cmg.per_size = 1 self.cmg.size_modifier = 1 self.cmg.save() self.mk = MarginKind() self.mk.margin = 200 self.mk.remarks = 'Margin kind 200%' self.mk.save() def test_plugin(self): """ Black box for openstack plugin """ with mock.patch('ralph.discovery.plugins.openstack.OpenStack') as OpenStack: OpenStack.side_effect = MockOpenStack openstack_runner() # testdata has 47 entries. self.assertEqual(47, HistoryCost.objects.count()) # if no group assigned, daily cost = 0 for all historycost self.assertEqual(0, sum([x.daily_cost for x in HistoryCost.objects.all()])) # if we assign them to the group ... for x in ComponentModel.objects.all(): x.group=self.cmg x.save() # and run again openstack runner openstack_runner() # data couns will be the same (data overwritten) self.assertEqual(47, HistoryCost.objects.count()) # costs will be calculated as below. self.assertEqual(31733606.18055556, sum([x.daily_cost for x in HistoryCost.objects.all()])) # now test with marginkind for x in Device.objects.filter(model__name='OpenStack Tenant'): x.margin_kind = self.mk x.save() openstack_runner() # margin kind = 200% = multiplies costs 3 times. self.assertEqual(31733606.18055556*3, sum([x.daily_cost for x in HistoryCost.objects.all()])) def test_make_tenant(self): """ Detailed test for make_tenant plugin sub-function """ tenant_params = dict( tenant_id='3ff63bf0e1384a1d87b6eaba8dad1196', total_volume_gb_usage=1, total_memory_mb_usage=1024, total_local_gb_usage=1, total_hours=1, total_vcpus_usage=1, tenant_fake=999, ) with mock.patch('ralph.discovery.plugins.openstack.OpenStack') as OpenStack: OpenStack.side_effect = MockOpenStack result = make_tenant(tenant_params) # no componentModel assigned to the group. Cost will be 0 self.assertEqual(result[1], 0) d=Device.objects.get(model__name='OpenStack Tenant') self.assertEqual(d.model.get_type_display(), 'cloud server') self.assertEqual(d.sn, 'openstack-3ff63bf0e1384a1d87b6eaba8dad1196') # only recognized component models auto-created. self.assertEqual(set([ u'OpenStack 10000 Memory GiB Hours', u'OpenStack 10000 CPU Hours', u'OpenStack 10000 Disk GiB Hours', u'OpenStack 10000 Volume GiB Hours']), set([x.name for x in ComponentModel.objects.all()])) # only recognized components auto-created. self.assertEqual(set([ u'OpenStack 10000 Memory GiB Hours', u'OpenStack 10000 CPU Hours', u'OpenStack 10000 Disk GiB Hours', u'OpenStack 10000 Volume GiB Hours']), set([x.model.name for x in GenericComponent.objects.all()])) # and every component is binded to open stack tenant device for x in GenericComponent.objects.all(): self.assertEqual(x.device.model.name, 'OpenStack Tenant') # if we assign 2 component model items into the specified group than... for i in ComponentModel.objects.all()[0:2]: i.group=self.cmg i.save() # then the costs of half components appears when reimporting tenant. \ # Rest of components are unassigned to the group, so not counted result = make_tenant(tenant_params) self.assertEqual(result[1], 200.0)
class OpenStackPluginTest(TestCase): """ OpenStack costs Test Case """ def setUp(self): # fake settings needed to run plugin, even with mocked library. settings.OPENSTACK_URL = '/' settings.OPENSTACK_USER = '******' settings.OPENSTACK_PASSWORD = '******' # model group for sample costs calculations. self.cmg = ComponentModelGroup() self.cmg.price = 100.0 self.cmg.per_size = 1 self.cmg.size_modifier = 1 self.cmg.save() self.mk = MarginKind() self.mk.margin = 200 self.mk.remarks = 'Margin kind 200%' self.mk.save() def test_plugin(self): """ Black box for openstack plugin """ with mock.patch( 'ralph.discovery.plugins.openstack.OpenStack') as OpenStack: OpenStack.side_effect = MockOpenStack openstack_runner() # testdata has 47 entries. self.assertEqual(47, HistoryCost.objects.count()) # if no group assigned, daily cost = 0 for all historycost self.assertEqual( 0, sum([x.daily_cost for x in HistoryCost.objects.all()])) # if we assign them to the group ... for x in ComponentModel.objects.all(): x.group = self.cmg x.save() # and run again openstack runner openstack_runner() # data couns will be the same (data overwritten) self.assertEqual(47, HistoryCost.objects.count()) # costs will be calculated as below. self.assertEqual( 31733606.18055556, sum([x.daily_cost for x in HistoryCost.objects.all()])) # now test with marginkind for x in Device.objects.filter(model__name='OpenStack Tenant'): x.margin_kind = self.mk x.save() openstack_runner() # margin kind = 200% = multiplies costs 3 times. self.assertEqual( 31733606.18055556 * 3, sum([x.daily_cost for x in HistoryCost.objects.all()])) def test_make_tenant(self): """ Detailed test for make_tenant plugin sub-function """ tenant_params = dict( tenant_id='3ff63bf0e1384a1d87b6eaba8dad1196', total_volume_gb_usage=1, total_memory_mb_usage=1024, total_local_gb_usage=1, total_hours=1, total_vcpus_usage=1, tenant_fake=999, ) with mock.patch( 'ralph.discovery.plugins.openstack.OpenStack') as OpenStack: OpenStack.side_effect = MockOpenStack result = make_tenant(tenant_params) # no componentModel assigned to the group. Cost will be 0 self.assertEqual(result[1], 0) d = Device.objects.get(model__name='OpenStack Tenant') self.assertEqual(d.model.get_type_display(), 'cloud server') self.assertEqual(d.sn, 'openstack-3ff63bf0e1384a1d87b6eaba8dad1196') # only recognized component models auto-created. self.assertEqual( set([ u'OpenStack 10000 Memory GiB Hours', u'OpenStack 10000 CPU Hours', u'OpenStack 10000 Disk GiB Hours', u'OpenStack 10000 Volume GiB Hours' ]), set([x.name for x in ComponentModel.objects.all()])) # only recognized components auto-created. self.assertEqual( set([ u'OpenStack 10000 Memory GiB Hours', u'OpenStack 10000 CPU Hours', u'OpenStack 10000 Disk GiB Hours', u'OpenStack 10000 Volume GiB Hours' ]), set([x.model.name for x in GenericComponent.objects.all()])) # and every component is binded to open stack tenant device for x in GenericComponent.objects.all(): self.assertEqual(x.device.model.name, 'OpenStack Tenant') # if we assign 2 component model items into the specified group than... for i in ComponentModel.objects.all()[0:2]: i.group = self.cmg i.save() # then the costs of half components appears when reimporting tenant. \ # Rest of components are unassigned to the group, so not counted result = make_tenant(tenant_params) self.assertEqual(result[1], 200.0)