Skip to content

Commit

Permalink
Merge branch 'hotfix' into 'master'
Browse files Browse the repository at this point in the history
Hotfix

See merge request arenadata/development/adcm!2595
  • Loading branch information
kuhella committed Apr 21, 2023
2 parents f177f1a + 7d08cec commit 7badc3e
Show file tree
Hide file tree
Showing 30 changed files with 1,219 additions and 160 deletions.
33 changes: 19 additions & 14 deletions python/adcm/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
from django.conf import settings
from django.test import Client, TestCase
from django.urls import reverse
from rbac.models import Role, User
from rbac.models import User
from rbac.upgrade.role import init_roles
from rest_framework.response import Response
from rest_framework.status import HTTP_200_OK, HTTP_201_CREATED

Expand Down Expand Up @@ -48,19 +49,11 @@ def setUp(self) -> None:
self.client = Client(HTTP_USER_AGENT="Mozilla/5.0")
self.login()

self.cluster_admin_role = Role.objects.create(
name="Cluster Administrator",
display_name="Cluster Administrator",
)
Role.objects.create(name="Provider Administrator", display_name="Provider Administrator")
Role.objects.create(name="Service Administrator", display_name="Service Administrator")

self.test_bundle_filename = "test_bundle.tar"
self.test_bundle_path = Path(
settings.BASE_DIR,
"python/audit/tests/files",
self.test_bundle_filename,
)
@classmethod
def setUpClass(cls):
super().setUpClass()

init_roles()

def tearDown(self) -> None:
dirs_to_clear = (
Expand Down Expand Up @@ -120,6 +113,18 @@ def another_user_logged_in(self, username: str, password: str):

self.login()

def another_user_log_in(self, username: str, password: str):
self.client.post(path=reverse("rbac:logout"))
response: Response = self.client.post(
path=reverse("rbac:token"),
data={
"username": username,
"password": password,
},
content_type=APPLICATION_JSON,
)
self.client.defaults["Authorization"] = f"Token {response.data['token']}"

def upload_bundle(self, path: Path) -> None:
with open(path, encoding=settings.ENCODING_UTF_8) as f:
response: Response = self.client.post(
Expand Down
Binary file added python/api/tests/files/bundle_cluster.tar
Binary file not shown.
2 changes: 0 additions & 2 deletions python/api/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
from django.urls import reverse
from django.utils import timezone
from init_db import init
from rbac.upgrade.role import init_roles
from rest_framework.response import Response
from rest_framework.status import (
HTTP_200_OK,
Expand All @@ -61,7 +60,6 @@ def setUp(self) -> None:
super().setUp()

init()
init_roles()

self.files_dir = settings.BASE_DIR / "python" / "cm" / "tests" / "files"
self.bundle_adh_name = "adh.1.5.tar"
Expand Down
6 changes: 6 additions & 0 deletions python/api/tests/test_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ def setUp(self) -> None:
license="unaccepted",
)
Prototype.objects.create(bundle=self.bundle_2, name=self.bundle_2.name)
self.test_bundle_filename = "bundle_cluster.tar"
self.test_bundle_path = Path(
settings.BASE_DIR,
"python/api/tests/files",
self.test_bundle_filename,
)

def test_upload_bundle(self) -> None:
self.upload_bundle(path=self.test_bundle_path)
Expand Down
2 changes: 0 additions & 2 deletions python/api/tests/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
from rbac.models import Policy, Role
from rbac.upgrade.role import init_roles
from rest_framework.response import Response
from rest_framework.status import HTTP_201_CREATED

Expand Down Expand Up @@ -208,7 +207,6 @@ def test_task_permissions(self):
cluster_prototype = Prototype.objects.get(bundle=bundle, type="cluster")
cluster = Cluster.objects.create(name="test_cluster", prototype=cluster_prototype)

init_roles()
role = Role.objects.get(name="Cluster Administrator")
policy = Policy.objects.create(name="test_policy", role=role)
policy.user.add(self.no_rights_user)
Expand Down
3 changes: 0 additions & 3 deletions python/audit/tests/test_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
from rbac.models import Policy, Role, User
from rbac.upgrade.role import init_roles
from rest_framework.response import Response
from rest_framework.status import HTTP_201_CREATED, HTTP_404_NOT_FOUND

Expand Down Expand Up @@ -235,8 +234,6 @@ def test_component_launch_denied(self):
)

def test_host_denied(self):
init_roles()

adcm_role = Role.objects.get(name="View ADCM settings")
adcm_policy = Policy.objects.create(name="test_adcm_policy", role=adcm_role)
adcm_policy.user.add(self.no_rights_user)
Expand Down
7 changes: 7 additions & 0 deletions python/audit/tests/test_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ def setUp(self) -> None:
license="unaccepted",
)

self.test_bundle_filename = "test_bundle.tar"
self.test_bundle_path = Path(
settings.BASE_DIR,
"python/audit/tests/files",
self.test_bundle_filename,
)

def check_log_upload(self, log: AuditLog, operation_result: AuditLogOperationResult, user: User) -> None:
self.assertFalse(log.audit_object)
self.assertEqual(log.operation_name, "Bundle uploaded")
Expand Down
2 changes: 0 additions & 2 deletions python/audit/tests/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
from django.conf import settings
from django.urls import reverse
from rbac.models import Policy, Role, User
from rbac.upgrade.role import init_roles
from rest_framework.response import Response
from rest_framework.status import (
HTTP_201_CREATED,
Expand Down Expand Up @@ -246,7 +245,6 @@ def get_component(self) -> tuple[ServiceComponent, ConfigLog]:
)

def add_no_rights_user_cluster_view_rights(self) -> None:
init_roles()
role = Role.objects.get(name="View cluster configurations")
policy = Policy.objects.create(name="test_policy", role=role)
policy.user.add(self.no_rights_user)
Expand Down
2 changes: 0 additions & 2 deletions python/audit/tests/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
)
from django.urls import reverse
from rbac.models import Policy, Role, User
from rbac.upgrade.role import init_roles
from rest_framework.response import Response
from rest_framework.status import (
HTTP_200_OK,
Expand Down Expand Up @@ -202,7 +201,6 @@ def test_delete_denied_view_permission(self):
prototype=self.prototype,
)

init_roles()
role = Role.objects.get(name="View provider configurations")
policy = Policy.objects.create(name="test_policy", role=role)
policy.user.add(self.no_rights_user)
Expand Down
3 changes: 0 additions & 3 deletions python/audit/tests/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
from django.conf import settings
from django.urls import reverse
from rbac.models import Policy, Role, User
from rbac.upgrade.role import init_roles
from rest_framework.response import Response
from rest_framework.status import (
HTTP_201_CREATED,
Expand Down Expand Up @@ -311,7 +310,6 @@ def test_delete(self):
self.assertFalse(log.audit_object)

def test_delete_denied(self):
init_roles()
role = Role.objects.get(name="View service config")
policy = Policy.objects.create(name="test_policy", role=role)
policy.user.add(self.no_rights_user)
Expand Down Expand Up @@ -339,7 +337,6 @@ def test_delete_denied(self):
)

def test_delete_new(self):
init_roles()
role = Role.objects.get(name="View service configurations")
bundle_filename = "import.tar"
with open(
Expand Down
2 changes: 0 additions & 2 deletions python/audit/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
)
from django.urls import reverse
from init_db import init as init_adcm
from rbac.upgrade.role import init_roles
from rest_framework.response import Response
from rest_framework.status import HTTP_200_OK, HTTP_403_FORBIDDEN

Expand All @@ -38,7 +37,6 @@ def setUp(self) -> None:
super().setUp()

init_adcm()
init_roles()

self.object_name_first = "object_name_first"
self.object_name_second = "object_name_second"
Expand Down
27 changes: 16 additions & 11 deletions python/cm/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,9 +681,10 @@ def save_hc(cluster, host_comp_list):
# pylint: disable=too-many-locals

hc_queryset = HostComponent.objects.filter(cluster=cluster).order_by("id")
service_map = {hc.service for hc in hc_queryset}
service_set = {hc.service for hc in hc_queryset}
old_hosts = {i.host for i in hc_queryset.select_related("host")}
new_hosts = {i[1] for i in host_comp_list}

for removed_host in old_hosts.difference(new_hosts):
removed_host.remove_from_concerns(CTX.lock)

Expand All @@ -692,6 +693,7 @@ def save_hc(cluster, host_comp_list):

still_hc = still_existed_hc(cluster, host_comp_list)
host_service_of_still_hc = {(hc.host, hc.service) for hc in still_hc}

for removed_hc in set(hc_queryset) - set(still_hc):
groupconfigs = GroupConfig.objects.filter(
object_type__model__in=["clusterobject", "servicecomponent"],
Expand All @@ -706,32 +708,35 @@ def save_hc(cluster, host_comp_list):
group_config.hosts.remove(removed_hc.host)

hc_queryset.delete()
result = []
host_component_list = []

for proto, host, comp in host_comp_list:
hostcomponent = HostComponent(
host_component = HostComponent(
cluster=cluster,
service=proto,
host=host,
component=comp,
)
hostcomponent.save()
result.append(hostcomponent)
host_component.save()
host_component_list.append(host_component)

CTX.event.send_state()
post_event(event="change_hostcomponentmap", obj=cluster)
update_hierarchy_issues(cluster)
for provider in [host.provider for host in Host.objects.filter(cluster=cluster)]:

for provider in {host.provider for host in Host.objects.filter(cluster=cluster)}:
update_hierarchy_issues(provider)

update_issue_after_deleting()
load_service_map()
for service in service_map:
re_apply_object_policy(service)

for hostcomponent in result:
re_apply_object_policy(hostcomponent.service)
for host_component_item in host_component_list:
service_set.add(host_component_item.service)

return result
for service in service_set:
re_apply_object_policy(apply_object=service)

return host_component_list


def add_hc(cluster, hc_in):
Expand Down
22 changes: 3 additions & 19 deletions python/cm/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import casestyle
from audit.models import MODEL_TO_AUDIT_OBJECT_TYPE_MAP, AuditObject
from audit.utils import mark_deleted_audit_object
from cm.logger import logger
from cm.models import (
ADCM,
ADCMEntity,
Expand Down Expand Up @@ -109,16 +108,12 @@ def model_change(sender, **kwargs):
if kwargs["raw"]:
return

name, module, obj = get_names(sender, **kwargs)
if "filter_out" in kwargs:
if kwargs["filter_out"](module, name, obj):
return
_, module, obj = get_names(sender, **kwargs)

action = "update"
if kwargs.get("created"):
action = "create"

logger.info("%s %s %s #%s", action, module, name, obj.pk)
_post_event(action=action, module=module, obj=obj)


Expand All @@ -128,15 +123,8 @@ def model_change(sender, **kwargs):
@receiver(post_delete, sender=Role)
@receiver(post_delete, sender=GroupConfig)
def model_delete(sender, **kwargs):
name, module, obj = get_names(sender, **kwargs)

if "filter_out" in kwargs:
if kwargs["filter_out"](module, name, obj):
return

action = "delete"
logger.info("%s %s %s #%s", action, module, name, obj.pk)
_post_event(action=action, module=module, obj=obj)
_, module, obj = get_names(sender, **kwargs)
_post_event(action="delete", module=module, obj=obj)


@receiver(m2m_changed, sender=GroupConfig)
Expand All @@ -147,9 +135,6 @@ def model_delete(sender, **kwargs):
@receiver(m2m_changed, sender=Group)
def m2m_change(sender, **kwargs):
name, module, obj = get_names(sender, **kwargs)
if "filter_out" in kwargs:
if kwargs["filter_out"](module, name, obj):
return

if kwargs["action"] == "post_add":
action = "add"
Expand All @@ -158,5 +143,4 @@ def m2m_change(sender, **kwargs):
else:
return

logger.info("%s %s %s #%s", action, module, name, obj.pk)
_post_event(action=action, module=module, obj=obj, model_name=name)
1 change: 0 additions & 1 deletion python/cm/status_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ def post_event(event: str, obj, details: dict = None) -> Response | None:
},
}

logger.debug("post_event %s", data)
return api_request(method="post", url="event/", data=data)


Expand Down
4 changes: 2 additions & 2 deletions python/cm/tests/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
from django.urls import reverse
from django.utils import timezone
from init_db import init
from rbac.upgrade.role import init_roles
from rest_framework.response import Response
from rest_framework.status import HTTP_200_OK, HTTP_201_CREATED, HTTP_409_CONFLICT

Expand All @@ -71,6 +70,8 @@ class TestJob(BaseTestCase):
# pylint: disable=too-many-public-methods

def setUp(self):
super().setUp()

self.files_dir = settings.BASE_DIR / "python" / "cm" / "tests" / "files"
self.multijob_bundle = "multijob_cluster.tar"
self.multijob_cluster_name = "multijob_cluster"
Expand All @@ -82,7 +83,6 @@ def setUp(self):
@staticmethod
def init_adcm():
init()
init_roles()

def create_multijob_cluster(self) -> Response:
bundle_id = self.upload_and_load_bundle(path=Path(self.files_dir, self.multijob_bundle)).pk
Expand Down
Loading

0 comments on commit 7badc3e

Please sign in to comment.