def test_create_cpu_core_per_hour_tiered_rate(self): """Test creating a cpu_core_per_hour rate.""" rate = { 'provider_uuid': self.provider.uuid, 'metric': Rate.METRIC_CPU_CORE_USAGE_HOUR, 'tiered_rate': [{ 'unit': 'USD', 'value': 0.22, 'usage_start': None, 'usage_end': 10.0 }, { 'unit': 'USD', 'value': 0.26, 'usage_start': 10.0, 'usage_end': None }] } with tenant_context(self.tenant): instance = None serializer = RateSerializer(data=rate) if serializer.is_valid(raise_exception=True): instance = serializer.save() self.assertIsNotNone(instance) self.assertIsNotNone(instance.uuid)
def test_tiered_rate_with_gaps(self): """Test creating a tiered rate with a gap between the tiers.""" rate = { 'provider_uuid': self.provider.uuid, 'metric': Rate.METRIC_CPU_CORE_USAGE_HOUR, 'tiered_rate': [{ 'unit': 'USD', 'value': 0.22, 'usage_start': None, 'usage_end': 7.0 }, { 'unit': 'USD', 'value': 0.26, 'usage_start': 10.0, 'usage_end': None }] } with tenant_context(self.tenant): serializer = RateSerializer(data=rate) with self.assertRaises(serializers.ValidationError): if serializer.is_valid(raise_exception=True): serializer.save()
def test_create_storage_tiered_rate(self): """Test creating a storage tiered rate.""" storage_rates = (Rate.METRIC_STORAGE_GB_REQUEST_MONTH, Rate.METRIC_STORAGE_GB_USAGE_MONTH) for storage_rate in storage_rates: rate = { 'provider_uuid': self.provider.uuid, 'metric': storage_rate, 'tiered_rate': [{ 'unit': 'USD', 'value': 0.22, 'usage_start': None, 'usage_end': 10.0 }, { 'unit': 'USD', 'value': 0.26, 'usage_start': 10.0, 'usage_end': None }] } with tenant_context(self.tenant): instance = None serializer = RateSerializer(data=rate) if serializer.is_valid(raise_exception=True): instance = serializer.save() self.assertIsNotNone(instance) self.assertIsNotNone(instance.uuid)
def test_create_storage_no_tiers_rate(self): """Test creating a non tiered storage rate.""" storage_rates = ( CostModelMetricsMap.OCP_METRIC_STORAGE_GB_REQUEST_MONTH, CostModelMetricsMap.OCP_METRIC_STORAGE_GB_USAGE_MONTH) for storage_rate in storage_rates: rate = { 'provider_uuids': [self.provider.uuid], 'metric': { 'name': storage_rate }, 'tiered_rate': [{ 'unit': 'USD', 'value': 0.22 }] } with tenant_context(self.tenant): instance = None serializer = RateSerializer(data=rate) if serializer.is_valid(raise_exception=True): instance = serializer.save() self.assertIsNotNone(instance) self.assertIsNotNone(instance.uuid)
def test_tiered_rate_null_start_end(self): """Test creating a rate with out a start and end.""" rate = { 'provider_uuids': [self.provider.uuid], 'metric': { 'name': CostModelMetricsMap.OCP_METRIC_CPU_CORE_USAGE_HOUR }, 'tiered_rate': [{ 'unit': 'USD', 'value': 0.22, 'usage': { 'usage_start': 0.0, 'usage_end': 7.0 } }, { 'unit': 'USD', 'value': 0.26, 'usage': { 'usage_start': 10.0, 'usage_end': 20.0 } }] } with tenant_context(self.tenant): serializer = RateSerializer(data=rate) with self.assertRaises(serializers.ValidationError): if serializer.is_valid(raise_exception=True): serializer.save()
def test_error_no_rate(self): """Test error when trying to create an empty rate.""" rate = { 'provider_uuid': self.provider.uuid, 'metric': Rate.METRIC_CPU_CORE_USAGE_HOUR } with tenant_context(self.tenant): serializer = RateSerializer(data=rate) with self.assertRaises(serializers.ValidationError): if serializer.is_valid(raise_exception=True): serializer.save()
def test_error_on_rate_type(self): """Test error when trying to create an invalid rate input.""" rate = { 'provider_uuid': self.provider.uuid, 'metric': Rate.METRIC_CPU_CORE_USAGE_HOUR, 'invalid_rate': { 'value': round(Decimal(random.random()), 6), 'unit': 'USD' } } with tenant_context(self.tenant): serializer = RateSerializer(data=rate) with self.assertRaises(serializers.ValidationError): if serializer.is_valid(raise_exception=True): serializer.save()
def test_error_on_invalid_metric(self): """Test error on an invalid metric rate.""" rate = { 'provider_uuid': self.provider.uuid, 'metric': 'invalid_metric', 'tiered_rate': [{ 'value': round(Decimal(random.random()), 6), 'unit': 'USD', 'usage_start': None, 'usage_end': None }] } with tenant_context(self.tenant): serializer = RateSerializer(data=rate) with self.assertRaises(serializers.ValidationError): if serializer.is_valid(raise_exception=True): serializer.save()
def test_error_on_invalid_provider(self): """Test error with an invalid provider id.""" rate = { 'provider_uuid': '1dd7204c-72c4-4ec4-95bc-d5c447688b27', 'metric': Rate.METRIC_MEM_GB_USAGE_HOUR, 'tiered_rate': [{ 'value': round(Decimal(random.random()), 6), 'unit': 'USD', 'usage_start': None, 'usage_end': None }] } with tenant_context(self.tenant): serializer = RateSerializer(data=rate) with self.assertRaises(serializers.ValidationError): if serializer.is_valid(raise_exception=True): serializer.save()
def test_error_tier_usage_end_less_than(self): """Test error when trying to create a tiered usage_end less than usage_start.""" rate = { 'provider_uuid': self.provider.uuid, 'metric': Rate.METRIC_CPU_CORE_USAGE_HOUR, 'tiered_rate': [{ 'unit': 'USD', 'value': 1.0, 'usage_start': 10.0, 'usage_end': 3.0 }] } with tenant_context(self.tenant): serializer = RateSerializer(data=rate) with self.assertRaises(serializers.ValidationError): if serializer.is_valid(raise_exception=True): serializer.save()
def test_error_neg_tier_value(self): """Test error when trying to create a negative tiered value.""" rate = { 'provider_uuid': self.provider.uuid, 'metric': Rate.METRIC_CPU_CORE_USAGE_HOUR, 'tiered_rate': [{ 'unit': 'USD', 'value': (round(Decimal(random.random()), 6) * -1), 'usage_start': 10.0, 'usage_end': 20.0 }] } with tenant_context(self.tenant): serializer = RateSerializer(data=rate) with self.assertRaises(serializers.ValidationError): if serializer.is_valid(raise_exception=True): serializer.save()
def test_error_on_negative_rate(self): """Test error when trying to create an negative rate input.""" rate = { 'provider_uuids': [self.provider.uuid], 'metric': { 'name': CostModelMetricsMap.OCP_METRIC_CPU_CORE_USAGE_HOUR }, 'tiered_rate': [{ 'value': (round(Decimal(random.random()), 6) * -1), 'unit': 'USD', 'usage': { 'usage_start': None, 'usage_end': None } }] } with tenant_context(self.tenant): serializer = RateSerializer(data=rate) with self.assertRaises(serializers.ValidationError): if serializer.is_valid(raise_exception=True): serializer.save()
def test_error_on_usage_end_larger_then_start(self): """Test error on a larger usage_end then usage_start .""" rate = { 'provider_uuids': [self.provider.uuid], 'metric': { 'name': CostModelMetricsMap.OCP_METRIC_CPU_CORE_USAGE_HOUR }, 'tiered_rate': [{ 'value': round(Decimal(random.random()), 6), 'unit': 'USD', 'usage': { 'usage_start': 5, 'usage_end': 10 } }] } with tenant_context(self.tenant): serializer = RateSerializer(data=rate) with self.assertRaises(serializers.ValidationError): if serializer.is_valid(raise_exception=True): serializer.save()