dashboard/infrastructure/tests.py

105 lines
3.3 KiB
Python

# Copyright © 2022 Aravinth Manivannan <realaravinth@batsense.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from pathlib import Path
from django.test import TestCase, Client, override_settings
from django.conf import settings
from dash.models import Instance
from .utils import Infra
from accounts.tests import register_util, login_util
from dash.tests import create_configurations, create_instance_util
class InfraUtilTest(TestCase):
"""
Tests billing system
"""
def setUp(self):
self.username = "infrautil_user"
register_util(t=self, username=self.username)
create_configurations(t=self)
@override_settings(
HOSTEA={
"INFRA": {
"HOSTEA_REPO": {
"PATH": "/tmp/hostea/dashboard/test_path_util/repo/",
"REMOTE": "git@git.batsense.net:realaravinth/dummy-hostea-dash-test",
"SSH_KEY": "/src/atm/.ssh/aravinth",
},
}
}
)
def test_path_utils(self):
infra = Infra()
subdomain = "foo"
base = infra.repo_path
self.assertEqual(
base.joinpath(f"inventory/host_vars/{subdomain}-host/"),
infra._host_vars_dir(subdomain=subdomain),
)
self.assertEqual(
base.joinpath(f"inventory/host_vars/{subdomain}-host/gitea.yml"),
infra._gitea_path(subdomain=subdomain),
)
self.assertEqual(
base.joinpath(f"inventory/host_vars/{subdomain}-host/provision.yml"),
infra._provision_path(subdomain=subdomain),
)
self.assertEqual(
base.joinpath(f"inventory/{subdomain}-backup.yml"),
infra._backup_path(subdomain=subdomain),
)
self.assertEqual(
base.joinpath(f"inventory/hosts-scripts/{subdomain}-host.sh"),
infra._hostscript_path(subdomain=subdomain),
)
@override_settings(
HOSTEA={
"INFRA": {
"HOSTEA_REPO": {
"PATH": "/tmp/hostea/dashboard/test_add_vm/repo/",
"REMOTE": "git@git.batsense.net:realaravinth/dummy-hostea-dash-test",
"SSH_KEY": "/src/atm/.ssh/aravinth",
},
}
}
)
def test_add_vm(self):
infra = Infra()
c = Client()
login_util(self, c, "accounts.home")
subdomain = "add_vm"
base = infra.repo_path
create_instance_util(
t=self, c=c, instance_name=subdomain, config=self.instance_config[0]
)
instance = Instance.objects.get(name=subdomain)
woodpecker_agent_secret = infra.add_vm(instance=instance)
# infra.remove_vm(instance=instance)