2021-07-19 14:36:10 +00:00
|
|
|
import datetime
|
|
|
|
import json
|
2021-09-10 07:58:42 +00:00
|
|
|
import logging
|
|
|
|
from django import utils
|
2021-07-19 14:36:10 +00:00
|
|
|
|
|
|
|
from django.test import TestCase
|
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from django.utils import timezone
|
|
|
|
|
|
|
|
from .models import VMInstance
|
2021-09-10 07:58:42 +00:00
|
|
|
from .utils import *
|
2021-08-16 08:46:59 +00:00
|
|
|
from uncloud_pay.models import Order, PricingPlan, BillingAddress, Product, RecurringPeriod, Bill, BillRecord
|
2021-07-19 14:36:10 +00:00
|
|
|
|
2021-09-10 07:58:42 +00:00
|
|
|
import oca
|
|
|
|
|
|
|
|
logger = logging.getLogger()
|
2021-07-19 14:36:10 +00:00
|
|
|
|
|
|
|
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,
|
2021-08-17 17:05:44 +00:00
|
|
|
ram_unit_price=4, storage_ssd_unit_price=0.35, storage_hd_unit_price=0.02)
|
2021-07-19 14:36:10 +00:00
|
|
|
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 })
|
|
|
|
|
2021-08-16 08:46:59 +00:00
|
|
|
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)
|
2021-08-17 17:05:44 +00:00
|
|
|
|
2021-08-16 08:46:59 +00:00
|
|
|
VMInstance.delete_for_bill(bill)
|
|
|
|
instances = VMInstance.objects.filter(order=order)
|
|
|
|
self.assertEqual(len(instances), 0)
|
|
|
|
|
|
|
|
|
2021-07-19 14:36:10 +00:00
|
|
|
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)
|
2021-08-16 08:46:59 +00:00
|
|
|
VMInstance.create_instance(order)
|
2021-07-19 14:36:10 +00:00
|
|
|
instances = VMInstance.objects.filter(order=order)
|
|
|
|
self.assertEqual(len(instances), 1)
|
|
|
|
|
2021-09-10 07:58:42 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-07-19 14:36:10 +00:00
|
|
|
|