def test_host_in_hc(self): cls = cook_cluster() self.assertEqual( variant_host(cls, { 'predicate': 'in_hc', 'args': None }), []) service = cook_service(cls) comp1 = cook_component(cls, service, 'Server') provider, hp = cook_provider() h1 = add_host(hp, provider, 'h10') h2 = add_host(hp, provider, 'h11') add_host_to_cluster(cls, h1) add_host_to_cluster(cls, h2) self.assertEqual( variant_host(cls, { 'predicate': 'in_hc', 'args': None }), []) self.add_hc(cluster=cls, service=service, component=comp1, host=h2) self.assertEqual( variant_host(cls, { 'predicate': 'in_hc', 'args': None }), ['h11'])
def test_host_in_service(self): cls = cook_cluster() service = cook_service(cls) comp = cook_component(cls, service, 'Server') provider, hp = cook_provider() h1 = add_host(hp, provider, 'h10') add_host_to_cluster(cls, h1) self.add_hc(cluster=cls, service=service, component=comp, host=h1) with self.assertRaises(AdcmEx) as e: variant_host(cls, {'predicate': 'in_service', 'args': {}}) self.assertEqual(e.exception.msg, 'no "service" argument for predicate "in_service"') with self.assertRaises(AdcmEx) as e: variant_host(cls, { 'predicate': 'in_service', 'args': { 'service': 'qwe' } }) self.assertTrue('ClusterObject {' in e.exception.msg) self.assertTrue('} does not exist' in e.exception.msg) args = {'predicate': 'in_service', 'args': {'service': 'UBER'}} hosts = variant_host(cls, args) self.assertEqual(hosts, ['h10'])
def test_host_in_cluster(self): cls = cook_cluster() provider, hp = cook_provider() h1 = add_host(hp, provider, 'h10') add_host_to_cluster(cls, h1) hosts = variant_host(cls, {'predicate': 'in_cluster', 'args': []}) self.assertEqual(hosts, ['h10'])
def test_host_in_cluster(self): cls = cook_cluster() provider, hp = cook_provider() h1 = add_host(hp, provider, 'h10') limits = {"source": {"type": "builtin", "name": "host_in_cluster"}} self.assertEqual(get_variant(cls, None, limits), []) add_host_to_cluster(cls, h1) self.assertEqual(get_variant(cls, None, limits), ['h10'])
def test_host_in_component(self): cls = cook_cluster() service = cook_service(cls) comp1 = cook_component(cls, service, 'Server') comp2 = cook_component(cls, service, 'Node') provider, hp = cook_provider() h1 = add_host(hp, provider, 'h10') h2 = add_host(hp, provider, 'h11') add_host_to_cluster(cls, h1) add_host_to_cluster(cls, h2) self.add_hc(cluster=cls, service=service, component=comp1, host=h1) self.add_hc(cluster=cls, service=service, component=comp2, host=h2) with self.assertRaises(AdcmEx) as e: variant_host(cls, {'predicate': 'in_component'}) self.assertEqual(e.exception.msg, 'no "args" key in solver args') with self.assertRaises(AdcmEx) as e: variant_host(cls, {'predicate': 'in_component', 'args': 123}) self.assertEqual(e.exception.msg, 'arguments of solver should be a list or a map') with self.assertRaises(AdcmEx) as e: variant_host(cls, {'predicate': 'in_component', 'args': []}) self.assertEqual(e.exception.msg, 'no "service" argument for predicate "in_component"') with self.assertRaises(AdcmEx) as e: variant_host(cls, { 'predicate': 'in_component', 'args': { 'service': 'qwe' } }) self.assertTrue('ClusterObject {' in e.exception.msg) self.assertTrue('} does not exist' in e.exception.msg) with self.assertRaises(AdcmEx) as e: args = { 'predicate': 'in_component', 'args': { 'service': 'UBER', 'component': 'asd' } } variant_host(cls, args) self.assertTrue('ServiceComponent {' in e.exception.msg) self.assertTrue('} does not exist' in e.exception.msg) args = { 'predicate': 'in_component', 'args': { 'service': 'UBER', 'component': 'Node' } } hosts = variant_host(cls, args) self.assertEqual(hosts, ['h11'])
def post(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) if serializer.is_valid(raise_exception=True): validated_data = serializer.validated_data if 'cluster_id' in kwargs: cluster = check_obj(Cluster, kwargs['cluster_id']) host = check_obj(Host, validated_data.get('id')) self.check_host_perm('map_host_to', 'cluster', cluster) add_host_to_cluster(cluster, host) return Response(self.get_serializer(host).data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def test_host_in_cluster_service(self): cls = cook_cluster() service = cook_service(cls) comp1 = cook_component(cls, service, 'Server') provider, hp = cook_provider() h1 = add_host(hp, provider, 'h10') h2 = add_host(hp, provider, 'h11') limits = {"source": {"type": "builtin", "name": "host_in_cluster"}} self.assertEqual(get_variant(cls, None, limits), []) add_host_to_cluster(cls, h1) add_host_to_cluster(cls, h2) self.assertEqual(get_variant(cls, None, limits), ['h10', 'h11']) limits['source']['args'] = {'service': 'QWE'} self.assertEqual(get_variant(cls, None, limits), []) self.add_hc(cluster=cls, service=service, component=comp1, host=h2) self.assertEqual(get_variant(cls, None, limits), []) limits['source']['args']['service'] = 'UBER' self.assertEqual(get_variant(cls, None, limits), ['h11'])
def test_add_hc(): user = cook_user('Joe') cluster, service1, _, comp11, comp12, _, _ = cook_cluster1() _, host1, host2, host3 = cook_hosts() api.add_host_to_cluster(cluster, host1) HostComponent.obj.create(cluster=cluster, service=service1, component=comp11, host=host1) p = Policy.objects.create(role=cook_conf_role()) p.user.add(user) p.add_object(service1) assert not user.has_perm('cm.edit_conf_cluster', cluster) assert not user.has_perm('cm.edit_conf_host', host1) assert not user.has_perm('cm.edit_conf_host', host2) assert not user.has_perm('cm.edit_conf_host', host3) p.apply() assert user.has_perm('cm.edit_conf_clusterobject', service1) assert user.has_perm('cm.edit_conf_host', host1) assert not user.has_perm('cm.edit_conf_host', host2) assert not user.has_perm('cm.edit_conf_host', host3) api.add_host_to_cluster(cluster, host2) api.add_hc( cluster, [ { "service_id": service1.id, "component_id": comp11.id, "host_id": host1.id }, { "service_id": service1.id, "component_id": comp12.id, "host_id": host2.id }, ], ) assert user.has_perm('cm.edit_conf_host', host2)
def test_parent_policy4host_in_cluster(): user = cook_user('Joe') cluster, _, _, _, _, _, _ = cook_cluster1() _, host1, host2, host3 = cook_hosts() api.add_host_to_cluster(cluster, host1) api.add_host_to_cluster(cluster, host2) p = Policy.objects.create(role=cook_conf_role()) p.user.add(user) p.add_object(cluster) assert not user.has_perm('cm.edit_conf_cluster', cluster) assert not user.has_perm('cm.edit_conf_host', host1) assert not user.has_perm('cm.edit_conf_host', host2) assert not user.has_perm('cm.edit_conf_host', host3) p.apply() assert user.has_perm('cm.edit_conf_cluster', cluster) assert user.has_perm('cm.edit_conf_host', host1) assert user.has_perm('cm.edit_conf_host', host2) assert not user.has_perm('cm.edit_conf_host', host3)
def test_host_and(self): cls = cook_cluster() service = cook_service(cls) comp1 = cook_component(cls, service, 'Server') comp2 = cook_component(cls, service, 'Node') provider, hp = cook_provider() h1 = add_host(hp, provider, 'h10') h2 = add_host(hp, provider, 'h11') h3 = add_host(hp, provider, 'h12') add_host_to_cluster(cls, h1) add_host_to_cluster(cls, h2) add_host_to_cluster(cls, h3) self.add_hc(cluster=cls, service=service, component=comp1, host=h1) self.add_hc(cluster=cls, service=service, component=comp2, host=h2) self.add_hc(cluster=cls, service=service, component=comp2, host=h3) with self.assertRaises(AdcmEx) as e: variant_host(cls, {'predicate': 'and', 'args': 123}) self.assertEqual(e.exception.msg, 'arguments of solver should be a list or a map') with self.assertRaises(AdcmEx) as e: variant_host(cls, {'predicate': 'and', 'args': [123]}) self.assertEqual(e.exception.msg, 'predicte item should be a map') with self.assertRaises(AdcmEx) as e: args = { 'predicate': 'and', 'args': [{ 'predicate': 'qwe', 'args': 123 }] } variant_host(cls, args) self.assertEqual(e.exception.msg, 'no "qwe" in list of host functions') self.assertEqual(variant_host(cls, { 'predicate': 'and', 'args': [] }), []) args = { 'predicate': 'and', 'args': [ { 'predicate': 'in_service', 'args': { 'service': 'UBER' } }, { 'predicate': 'in_component', 'args': { 'service': 'UBER', 'component': 'Node' } }, ], } hosts = variant_host(cls, args) self.assertEqual(hosts, ['h11', 'h12'])
def test_parent_policy4host_in_component(): user = cook_user('Joe') cluster, service1, service2, comp11, _, comp21, _ = cook_cluster1() _, host1, host2, host3 = cook_hosts() api.add_host_to_cluster(cluster, host1) api.add_host_to_cluster(cluster, host2) api.add_host_to_cluster(cluster, host3) HostComponent.obj.create(cluster=cluster, service=service2, component=comp21, host=host1) HostComponent.obj.create(cluster=cluster, service=service2, component=comp21, host=host2) HostComponent.obj.create(cluster=cluster, service=service1, component=comp11, host=host3) p = Policy.objects.create(role=cook_conf_role()) p.user.add(user) p.add_object(comp21) assert not user.has_perm('cm.edit_conf_cluster', cluster) assert not user.has_perm('cm.edit_conf_clusterobject', service1) assert not user.has_perm('cm.edit_conf_servicecomponent', comp21) assert not user.has_perm('cm.edit_conf_host', host1) assert not user.has_perm('cm.edit_conf_host', host2) assert not user.has_perm('cm.edit_conf_host', host3) p.apply() assert not user.has_perm('cm.edit_conf_cluster', cluster) assert not user.has_perm('cm.edit_conf_clusterobject', service1) assert user.has_perm('cm.edit_conf_servicecomponent', comp21) assert user.has_perm('cm.edit_conf_host', host1) assert user.has_perm('cm.edit_conf_host', host2) assert not user.has_perm('cm.edit_conf_host', host3)
def test_host_not_in_component(self): cls = cook_cluster() service = cook_service(cls) comp1 = cook_component(cls, service, 'Server') comp2 = cook_component(cls, service, 'Node') service2 = cook_service(cls, 'Gett') comp3 = cook_component(cls, service2, 'Server') provider, hp = cook_provider() h1 = add_host(hp, provider, 'h10') h2 = add_host(hp, provider, 'h11') h3 = add_host(hp, provider, 'h12') h4 = add_host(hp, provider, 'h13') add_host(hp, provider, 'h14') add_host_to_cluster(cls, h1) add_host_to_cluster(cls, h2) add_host_to_cluster(cls, h3) add_host_to_cluster(cls, h4) self.add_hc(cluster=cls, service=service, component=comp1, host=h1) self.add_hc(cluster=cls, service=service, component=comp2, host=h2) self.add_hc(cluster=cls, service=service2, component=comp3, host=h3) with self.assertRaises(AdcmEx) as e: variant_host(cls, {'predicate': 'not_in_component', 'args': []}) self.assertEqual( e.exception.msg, 'no "service" argument for predicate "not_in_component"') with self.assertRaises(AdcmEx) as e: variant_host(cls, { 'predicate': 'not_in_component', 'args': { 'service': 'qwe' } }) self.assertTrue('ClusterObject {' in e.exception.msg) self.assertTrue('} does not exist' in e.exception.msg) with self.assertRaises(AdcmEx) as e: args = { 'predicate': 'not_in_component', 'args': { 'service': 'UBER', 'component': 'asd' }, } variant_host(cls, args) self.assertTrue('ServiceComponent {' in e.exception.msg) self.assertTrue('} does not exist' in e.exception.msg) args = { 'predicate': 'not_in_component', 'args': { 'service': 'UBER', 'component': 'Node' } } self.assertEqual(variant_host(cls, args), ['h10', 'h12', 'h13']) args = { 'predicate': 'not_in_component', 'args': { 'service': 'UBER', 'component': 'Server' } } self.assertEqual(variant_host(cls, args), ['h11', 'h12', 'h13']) args = { 'predicate': 'not_in_component', 'args': { 'service': 'Gett', 'component': 'Server' } } self.assertEqual(variant_host(cls, args), ['h10', 'h11', 'h13'])