Commit 2ec58c45 authored by Lukas Burgey's avatar Lukas Burgey

Expand tests

parent 5b1e3520
# pylint: disable=line-too-long,invalid-name
import logging
from django.contrib import auth
from django.contrib.auth.models import Group
from django.test import TestCase
from django_backend.backend import models
from django_backend.backend.auth.v1 import models as auth_models
LOGGER = logging.getLogger(__name__)
TEST_NAME = 'test_name'
TEST_EMAIL = 'test@hdf.org'
TEST_SUB = 'fb0fa558-cfa2-49f9-b847-5c651d1f6135'
TEST_KEY = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC/7NWc1pxCxi1f/+DtgCenIC9DDiT6l1cQSz4vJc1ecJA/gEUkd6CF/uR1Zl+yalWZej9UII5902Dm8WkIsIrHdRXevyt0QSzMbzZvaUAdJWcmWpqEKxJT6KGkd50O+qAJBXd5WZ2jUstmbHjoXio9eBY4hDcOofLD8vJT73DrZAEurd3F2S4s04oSt+onUmQ0inqeN38oqYOZGJhe/zZsDj65ctiTpj85rjglndNq+K7IHETGWXg9ht13FH9trjtEpLmAmR9UZGFkof+P+LiIUSYa05sVh6/MPFCgz7gzMCXw99lxoMz4lX2/wV/jnJ0+itn1WByWFWuqbDXUnH4pjk7K+YalFi2lA7F4ckWSn51IQRnh8Qp3CMuvid/Y3Q0qYa4Mtq5zhDnj+uGAUltoENqQCIKLp2KaCRQSbiDudYHXq9dufGRuAsls3rXnRhRJ26wOW+sggjfhB6LhGD20vSdJbuQQwUxZtRLB+y6uP6HXq/eqq+N+f8lfsw64b16xTOYFSpDniV/OE+PuDSNUFlO1V91FWhhiFWCyi+q6dTW5PTWBnnksXhR/LRaivA+UuNxPPbEfefBWmu7/y3jcqAn+CXSzbnvOrVXCU3E7DKBCR3d8TjBQj2No1qTNz6ZOD+dXEQvvjhl2lXkdGazG18hpaWN2hApzBpUulukg9Q== foo@bar'
TEST_KEY = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC/7NWc1pxCxi1f/+DtgCenIC9DDiT6l1cQSz4vJc1ecJA/gEUkd6CF/uR1Zl+yalWZej9UII5902Dm8WkIsIrHdRXevyt0QSzMbzZvaUAdJWcmWpqEKxJT6KGkd50O+qAJBXd5WZ2jUstmbHjoXio9eBY4hDcOofLD8vJT73DrZAEurd3F2S4s04oSt+onUmQ0inqeN38oqYOZGJhe/zZsDj65ctiTpj85rjglndNq+K7IHETGWXg9ht13FH9trjtEpLmAmR9UZGFkof+P+LiIUSYa05sVh6/MPFCgz7gzMCXw99lxoMz4lX2/wV/jnJ0+itn1WByWFWuqbDXUnH4pjk7K+YalFi2lA7F4ckWSn51IQRnh8Qp3CMuvid/Y3Q0qYa4Mtq5zhDnj+uGAUltoENqQCIKLp2KaCRQSbiDudYHXq9dufGRuAsls3rXnRhRJ26wOW+sggjfhB6LhGD20vSdJbuQQwUxZtRLB+y6uP6HXq/eqq+N+f8lfsw64b16xTOYFSpDniV/OE+PuDSNUFlO1V91FWhhiFWCyi+q6dTW5PTWBnnksXhR/LRaivA+UuNxPPbEfefBWmu7/y3jcqAn+CXSzbnvOrVXCU3E7DKBCR3d8TjBQj2No1qTNz6ZOD+dXEQvvjhl2lXkdGazG18hpaWN2hApzBpUulukg9Q== TEST_KEY'
TEST_PASSWORD = 'asnoteuhsn'
TEST_USERINFO = {
'email': TEST_EMAIL,
......@@ -17,6 +22,10 @@ TEST_USERINFO = {
}
TEST_SERVICE = 'test_service'
TEST_SITE = 'test_site'
# Has no service
TEST_GROUP = 'test_group'
# Has a service
TEST_GROUP_2 = 'test_group_2'
def setup_fixture():
......@@ -42,6 +51,14 @@ def setup_fixture():
user.set_password(TEST_PASSWORD)
user.save()
# TEST_GROUP has no services
group = Group(name=TEST_GROUP)
group.save()
# TEST_GROUP_2 has services
group2 = Group(name=TEST_GROUP_2)
group2.save()
site = models.Site(
name=TEST_SITE,
description=TEST_SITE,
......@@ -54,7 +71,7 @@ def setup_fixture():
)
service.save()
service.site.add(site)
service.save()
service.groups.add(group2)
models.SSHPublicKey(
name=TEST_NAME,
......@@ -66,6 +83,10 @@ def setup_fixture():
def teardown_fixture():
models.SSHPublicKey.objects.get(name=TEST_NAME).delete()
models.User.objects.get(username=TEST_NAME).delete()
models.Site.objects.get(name=TEST_SITE).delete()
models.Service.objects.get(name=TEST_SERVICE).delete()
Group.objects.get(name=TEST_GROUP).delete()
Group.objects.get(name=TEST_GROUP_2).delete()
auth_models.OIDCConfig.objects.get(name=TEST_NAME).delete()
......@@ -102,35 +123,134 @@ class UserTest(TestCase):
class DeploymentTest(TestCase):
def setUp(self):
setup_fixture()
models.Service(name='NEW_SERVICE').save()
def tearDown(self):
teardown_fixture()
models.Service.objects.get(name='NEW_SERVICE').delete()
def test_deployment(self):
# Deployment with a service
def test_deployment_service(self):
user = models.User.objects.get(username=TEST_NAME)
key = models.SSHPublicKey.objects.get(name=TEST_NAME)
service = models.Service.objects.get(name=TEST_SERVICE)
deployment = models.Deployment.get_deployment(
user,
service,
service=service,
)
self.assertIsNotNone(deployment)
deployment.deploy_key(key)
state = models.DeploymentState.get_state(
deployment=deployment,
key=key,
)
self.assertIsNotNone(state)
self.assertFalse(state.target_reached)
# execute deployment
for item in state.state_items.all():
item.client_response({'state':'deployed'})
self.assertTrue(state.target_reached)
# start removal
state.remove()
self.assertFalse(state.target_reached)
# execute removals
for item in state.state_items.all():
item.client_response({'state':'not_deployed'})
self.assertTrue(state.target_reached)
# Deployment with a group, which has one services (yet)
def test_deployment_for_group_with_service(self):
user = models.User.objects.get(username=TEST_NAME)
key = models.SSHPublicKey.objects.get(name=TEST_NAME)
group = Group.objects.get(name=TEST_GROUP_2)
deployment = models.Deployment.get_deployment(
user,
group=group,
)
self.assertIsNotNone(deployment)
deployment.deploy_key(key)
state = models.DeploymentState.get_state(
deployment=deployment,
key=key,
)
self.assertIsNotNone(state)
self.assertFalse(state.target_reached)
# execute deployment
for item in state.state_items.all():
item.client_response({'state':'deployed'})
self.assertTrue(state.target_reached)
# start removal
state.remove()
self.assertFalse(state.target_reached)
# execute removals
for item in state.state_items.all():
item.client_response({'state':'not_deployed'})
self.assertTrue(state.target_reached)
# Deployment with a group, which has no services at first
def test_deployment_for_group_without_service(self):
user = models.User.objects.get(username=TEST_NAME)
key = models.SSHPublicKey.objects.get(name=TEST_NAME)
group = Group.objects.get(name=TEST_GROUP)
site = models.Site.objects.get(name=TEST_SITE)
deployment = models.Deployment.get_deployment(
user,
group=group,
)
self.assertIsNotNone(deployment)
# start deployment
deployment.deploy_key(key)
state = models.DeploymentState.get_state(
deployment=deployment,
key=key,
)
self.assertIsNotNone(state)
self.assertFalse(state.state_items.exists())
self.assertTrue(state.target_reached)
# add service
new_service = models.Service.objects.get(
name='NEW_SERVICE',
)
new_service.site.add(site)
new_service.groups.add(group)
deployment.service_added(new_service)
self.assertTrue(state.state_items.exists())
self.assertFalse(state.target_reached)
# execute deployment
for item in state.state_items.all():
item.client_response({'status':'deployed'})
item.client_response({'state':'deployed'})
# the deployment was not done for new_service
self.assertTrue(state.target_reached)
# start removal
......@@ -140,6 +260,6 @@ class DeploymentTest(TestCase):
# execute removals
for item in state.state_items.all():
item.client_response({'status':'not_deployed'})
item.client_response({'state':'not_deployed'})
self.assertTrue(state.target_reached)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment