kls/tests.py

138 lines
5.2 KiB
Python
Raw Normal View History

2024-05-04 10:15:37 +00:00
import unittest
from unittest.mock import patch
import os
os.system("ln -s kls kls.py")
2024-05-06 10:26:08 +00:00
import kls
2024-05-04 10:15:37 +00:00
2024-05-06 13:31:10 +00:00
os.unlink('kls.py')
2024-05-04 10:15:37 +00:00
class TestCircularList(unittest.TestCase):
def setUp(self):
2024-05-06 13:31:10 +00:00
self.circular_list = kls.CircularList(['kube-system', 'default', 'kube-public'])
2024-05-04 10:15:37 +00:00
def test_forward(self):
2024-05-06 08:18:44 +00:00
self.circular_list.shift(1)
2024-05-04 10:15:37 +00:00
self.assertEqual(self.circular_list.index, 1)
def test_backward(self):
2024-05-06 08:18:44 +00:00
self.circular_list.shift(-1)
2024-05-04 10:15:37 +00:00
self.assertEqual(self.circular_list.index, 2) # Since it's circular, it goes to the end
2024-05-06 13:31:10 +00:00
def tearDown(self):
kls.curses.endwin()
2024-05-04 10:15:37 +00:00
class TestScriptFunctions(unittest.TestCase):
@patch('kls.subprocess.check_output')
def test_kubectl(self, mock_check_output):
mock_check_output.return_value = b'pod1\npod2\npod3'
2024-05-06 10:26:08 +00:00
result = kls.kubectl('get pods')
2024-05-04 10:15:37 +00:00
self.assertEqual(result, ['pod1', 'pod2', 'pod3'])
2024-05-06 13:31:10 +00:00
def tearDown(self):
kls.curses.endwin()
2024-05-04 10:15:37 +00:00
class TestMenu(unittest.TestCase):
def setUp(self):
2024-05-06 13:31:10 +00:00
self.rows = ['kube-system', 'default', 'kube-public']
2024-05-06 10:26:08 +00:00
self.menu = kls.Menu('Test', self.rows, 0, 10, 2)
2024-05-06 13:31:10 +00:00
self.second_menu = kls.Menu("Test Menu 2", ["pods", "services", "secrets"], 0, 10, 2)
self.third_menu = kls.Menu("Test Menu 3", ['pod1', 'pod2', 'pod3'], 0, 10, 2)
kls.menus = [self.menu, self.second_menu, self.third_menu] # Add the menu to the list of menus
2024-05-04 10:15:37 +00:00
os.system("ln -s kls kls.py")
2024-05-04 12:50:46 +00:00
def test_menu(self):
self.assertEqual(self.menu.title, 'Test')
2024-05-04 10:15:37 +00:00
self.assertEqual(self.menu.filtered_rows.elements, self.rows)
2024-05-06 13:31:10 +00:00
self.assertEqual(self.menu.visible_rows(), ['kube-system', 'default'])
self.assertEqual(self.menu.selected_row(), 'kube-system')
2024-05-04 10:15:37 +00:00
def test_filter_rows_with_filter(self):
# Apply a filter and test
2024-05-06 13:31:10 +00:00
self.menu.filter = 'kube-system'
2024-05-06 10:26:08 +00:00
self.menu.filtered_rows = kls.CircularList([x for x in self.menu.rows if self.menu.filter in x])
2024-05-06 13:31:10 +00:00
self.assertEqual(self.menu.filtered_rows.elements, ['kube-system'])
2024-05-04 10:15:37 +00:00
def test_filter_rows_with_nonexistent_filter(self):
# Apply a filter that matches no rows
self.menu.filter = 'nonexistent'
2024-05-06 10:26:08 +00:00
self.menu.filtered_rows = kls.CircularList([x for x in self.menu.rows if self.menu.filter in x])
2024-05-04 10:15:37 +00:00
self.assertEqual(self.menu.filtered_rows.elements, [])
2024-05-06 10:26:08 +00:00
def test_vertical_navigation(self):
kls.selected_menu = self.menu
# Test moving down one row
kls.handle_vertical_navigation("KEY_DOWN", self.menu)
self.assertEqual(self.menu.visible_row_index, 0)
# Test moving up one row
kls.handle_vertical_navigation("KEY_UP", self.menu)
self.assertEqual(self.menu.visible_row_index, 0)
# Test moving to the next page
kls.handle_vertical_navigation("KEY_NPAGE", self.menu)
self.assertEqual(self.menu.visible_row_index, 0)
# Test moving to the previous page
kls.handle_vertical_navigation("KEY_PPAGE", self.menu)
self.assertEqual(self.menu.visible_row_index, 0)
# Test moving to the first row
kls.handle_vertical_navigation("KEY_HOME", self.menu)
self.assertEqual(self.menu.visible_row_index, 0)
# Test moving to the last row
kls.handle_vertical_navigation("KEY_END", self.menu)
self.assertEqual(self.menu.visible_row_index, -1)
2024-05-06 13:31:10 +00:00
@patch('kls.subprocess.call')
@patch('kls.curses.reset_prog_mode')
@patch('kls.curses.def_prog_mode')
def test_handle_key_bindings(self, mock_def_prog_mode, mock_reset_prog_mode, mock_subprocess_call):
namespace = self.menu.selected_row()
api_resource = self.second_menu.selected_row()
resource = self.third_menu.selected_row()
key = "1" # Assuming you want to test the case where key is '1'
expected_command = kls.KEY_BINDINGS[key].format(namespace=namespace, api_resource=api_resource, resource=resource)
kls.handle_key_bindings(key, namespace, api_resource, resource)
mock_def_prog_mode.assert_called_once()
mock_reset_prog_mode.assert_called_once()
mock_subprocess_call.assert_called_once_with(expected_command, shell=True)
@patch('kls.curses.def_prog_mode')
def test_handle_key_bindings_empty_resource(self, mock_def_prog_mode):
namespace = self.menu.selected_row()
api_resource = self.second_menu.selected_row()
resource = None
key = "1" # Assuming you want to test the case where key is '1'
kls.handle_key_bindings(key, namespace, api_resource, resource)
mock_def_prog_mode.assert_not_called()
@patch('kls.curses.def_prog_mode')
def test_handle_key_bindings_services(self, mock_def_prog_mode):
namespace = self.menu.selected_row()
api_resource = "services"
resource = self.third_menu.selected_row()
key = "4" # 4 must not be called for services
kls.handle_key_bindings(key, namespace, api_resource, resource)
mock_def_prog_mode.assert_not_called()
2024-05-04 10:15:37 +00:00
def tearDown(self):
2024-05-06 13:31:10 +00:00
os.unlink('kls.py') # Remove the symlink after each test
kls.curses.endwin()
print('\033[?1003l') # Disable mouse tracking with the XTERM API
2024-05-04 10:15:37 +00:00
if __name__ == '__main__':
unittest.main()