71 lines
2.4 KiB
Python
71 lines
2.4 KiB
Python
# Copyright 2018 Red Hat, 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 mock
|
|
import os
|
|
|
|
from oslo_config import cfg
|
|
|
|
from kuryr_kubernetes.tests import base as test_base
|
|
from kuryr_kubernetes import utils
|
|
|
|
CONF = cfg.CONF
|
|
|
|
|
|
class TestUtils(test_base.TestCase):
|
|
@mock.patch('socket.gethostname')
|
|
def test_get_node_name_socket(self, m_gethostname):
|
|
try:
|
|
del os.environ['KUBERNETES_NODE_NAME']
|
|
except KeyError:
|
|
pass
|
|
|
|
m_gethostname.return_value = 'foo'
|
|
res = utils.get_node_name()
|
|
self.assertEqual('foo', res)
|
|
m_gethostname.assert_called_once_with()
|
|
|
|
@mock.patch('socket.gethostname')
|
|
def test_get_node_name_envvar(self, m_gethostname):
|
|
os.environ['KUBERNETES_NODE_NAME'] = 'bar'
|
|
m_gethostname.return_value = 'foo'
|
|
res = utils.get_node_name()
|
|
self.assertEqual('bar', res)
|
|
m_gethostname.assert_not_called()
|
|
|
|
@mock.patch('requests.get')
|
|
def test_get_leader_name(self, m_get):
|
|
m_get.return_value = mock.Mock(json=mock.Mock(
|
|
return_value={'name': 'foo'}))
|
|
res = utils.get_leader_name()
|
|
m_get.assert_called_once_with(
|
|
'http://localhost:%d' % CONF.kubernetes.controller_ha_elector_port)
|
|
self.assertEqual('foo', res)
|
|
|
|
@mock.patch('requests.get')
|
|
def test_get_leader_name_malformed(self, m_get):
|
|
m_get.return_value = mock.Mock(json=mock.Mock(
|
|
return_value={'name2': 'foo'}))
|
|
res = utils.get_leader_name()
|
|
m_get.assert_called_once_with(
|
|
'http://localhost:%d' % CONF.kubernetes.controller_ha_elector_port)
|
|
self.assertIsNone(res)
|
|
|
|
@mock.patch('requests.get')
|
|
def test_get_leader_name_exc(self, m_get):
|
|
m_get.side_effect = Exception
|
|
res = utils.get_leader_name()
|
|
m_get.assert_called_once_with(
|
|
'http://localhost:%d' % CONF.kubernetes.controller_ha_elector_port)
|
|
self.assertIsNone(res)
|