import datetime import json import logging from django import utils from django.test import TestCase from django.contrib.auth import get_user_model from django.utils import timezone from .models import VMInstance from .utils import * from uncloud_pay.models import Order, PricingPlan, BillingAddress, Product, RecurringPeriod, Bill, BillRecord import oca logger = logging.getLogger() vm_product_config = { 'features': { 'cores': { 'min': 1, 'max': 48 }, 'ram_gb': { 'min': 2, 'max': 200 }, }, } class VMInstanceTestCase(TestCase): def setUp(self): RecurringPeriod.populate_db_defaults() self.user = get_user_model().objects.create( username='random_user', email='jane.random@domain.tld') self.config = json.dumps({ 'cores': 1, 'memory': 2, 'storage': 100, 'homeserver_domain': '', 'webclient_domain': '', 'matrix_domain': '', }) self.pricing_plan = PricingPlan.objects.create(name="PricingSample", set_up_fees=35, cores_unit_price=3, ram_unit_price=4, storage_ssd_unit_price=0.35, storage_hd_unit_price=0.02) self.ba = BillingAddress.objects.create( owner=self.user, organization = 'Test org', street="unknown", city="unknown", postal_code="somewhere else", active=True) self.product = Product.objects.create(name="Testproduct", description="Only for testing", config=vm_product_config) self.default_recurring_period = RecurringPeriod.objects.get(name="Per 30 days") self.product.recurring_periods.add(self.default_recurring_period, through_defaults= { 'is_default': True }) def test_delete_matrix_vm_related_to_bill(self): order = Order.objects.create(owner=self.user, recurring_period=self.default_recurring_period, billing_address=self.ba, pricing_plan = self.pricing_plan, product=self.product, config=self.config) VMInstance.create_instance(order) instances = VMInstance.objects.filter(order=order) self.assertEqual(len(instances), 1) bill = Bill.create_next_bill_for_order(order, ending_date=order.next_cancel_or_downgrade_date(order.starting_date)) bill_records = BillRecord.objects.filter(bill=bill) self.assertEqual(len(bill_records), 1) self.assertEqual(bill_records[0].order, order) VMInstance.delete_for_bill(bill) instances = VMInstance.objects.filter(order=order) self.assertEqual(len(instances), 0) def test_create_matrix_vm(self): order = Order.objects.create(owner=self.user, recurring_period=self.default_recurring_period, billing_address=self.ba, pricing_plan = self.pricing_plan, product=self.product, config=self.config) VMInstance.create_instance(order) instances = VMInstance.objects.filter(order=order) self.assertEqual(len(instances), 1) def test_update_dns(self): #add_to_dns_files("test12", "192.168.1.32", "2001:db8::44", settings.MATRIX_DNS_MAIN_DOMAIN) remove_from_dns_files("test1", settings.MATRIX_DNS_MAIN_DOMAIN)