Do not call Zabbix in case of deletion of cluster

No reason to call Zabbix to report about changes because
we delete node with it few seconds after.

Also fixed:

zabbix error respond may not contain data field

Example:

{ 'jsonrpc': '2.0', 'id': '1', 'error': {
  'message': "Error connecting to database [Can't connect
              to local MySQL server through socket
              '/var/run/mysqld/mysqld.sock' (2)]",
  'code': 1}}

Change-Id: Ia25c7107b3dc535b6e14db74a82d096fd6caa4d0
Closes-Bug: #1419775
This commit is contained in:
Vladimir Sharshov (warpc) 2015-02-13 13:19:07 +03:00
parent 71aaefd0c6
commit 2eee83b38d
4 changed files with 71 additions and 3 deletions

View File

@ -380,7 +380,7 @@ class DeletionTask(object):
# TODO(ikalnitsky): remove this, let the flow always go through Astute
# No need to call Astute if no nodes are specified
if respond_to == 'remove_cluster_resp' and \
if task.name == consts.TASK_NAMES.cluster_deletion and \
not (nodes and nodes['nodes_to_delete']):
logger.debug("No nodes specified, exiting")
rcvr = rpc.receiver.NailgunReceiver()
@ -396,7 +396,8 @@ class DeletionTask(object):
# check if there's a Zabbix server in an environment
# and if there is, remove hosts
if ZabbixManager.get_zabbix_node(task.cluster):
if (task.name != consts.TASK_NAMES.cluster_deletion
and ZabbixManager.get_zabbix_node(task.cluster)):
zabbix_credentials = ZabbixManager.get_zabbix_credentials(
task.cluster
)

View File

@ -69,6 +69,29 @@ class TestDeletionTask(BaseIntegrationTest):
self.assertEqual(len(self.cluster_db.nodes), 0)
self.assertEqual(len(ret), 0)
@mock.patch('nailgun.task.task.rpc')
@mock.patch('nailgun.task.task.make_astute_message')
@mock.patch('nailgun.task.task.ZabbixManager')
def test_execute_with_zabbix_node(
self,
mock_zabbix_manager,
mmake_astute_message,
mrpc):
self.add_node(consts.NODE_STATUSES.ready)
nodes = DeletionTask.get_task_nodes_for_cluster(self.cluster_db)
self.assertEqual(len(nodes['nodes_to_delete']), 1)
task = models.Task(
name=consts.TASK_NAMES.cluster_deletion,
cluster=self.cluster_db
)
db().add(task)
db().commit()
DeletionTask.execute(task, nodes=nodes)
self.assertEqual(mock_zabbix_manager.get_zabbix_node.called, False)
@mock.patch('nailgun.task.task.rpc')
@mock.patch('nailgun.task.task.make_astute_message')
@mock.patch.object(DeletionTask, 'remove_undeployed_nodes_from_db')

View File

@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
# Copyright 2015 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import json
from mock import Mock
from mock import patch
from nailgun.errors import errors
from nailgun.test.base import BaseTestCase
from nailgun.utils.zabbix import ZabbixManager
class TestZabbixManager(BaseTestCase):
@patch('nailgun.utils.zabbix.urllib2.urlopen')
def test_error_zabbix_request(self, mock_urlopen):
urlopen_value = Mock()
urlopen_value.read.return_value = json.dumps({
'jsonrpc': '2.0',
'id': '1',
'error': {
'message': "Error connecting to database]",
'code': 1}})
mock_urlopen.return_value = urlopen_value
with self.assertRaises(errors.ZabbixRequestError):
ZabbixManager._make_zabbix_request(
'fake_url',
'user.authenticate',
{'password': 'zabbix', 'user': 'admin'},
auth=None),

View File

@ -49,7 +49,7 @@ class ZabbixManager(object):
if 'error' in result:
code = result['error']['code']
msg = result['error']['message']
data = result['error']['data']
data = result['error'].get('data', '')
raise errors.ZabbixRequestError(
"Zabbix returned error code {0}, {1}: {2}".format(
code, msg, data