def test_list_nodes(self): list_nodes = mock.MagicMock() list_nodes.items = [fake.mock_kubernetes_node('1'), fake.mock_kubernetes_node('2')] self.obj.client.list_node.return_value = list_nodes out = self.obj.list_nodes() self.assertEqual(len(out), 2, 'Failed to list all k8s nodes') self.obj.client.list_node.assert_called_once_with() self.assertEqual(list(map(lambda x: x.metadata.name[0], out)), ['1', '2'], 'Returned k8s nodes do not match the expected')
def test_is_node_active(self): node = fake.mock_kubernetes_node() # if node is ready, should return its name self.assertEqual( self.obj.is_node_active(node), node.metadata.name, 'Saying node is not active when it is, and failed to give back its name' ) # otherwise, always returns None node = fake.mock_kubernetes_node(ready=False) self.assertIsNone(self.obj.is_node_active(node), 'Saying node is active when it is not')
def test_get_node_id(self): name = 'fake-name' node_info = fake.mock_kubernetes_node(name) # should always return the ID value indicated in the passed argument self.assertTrue( self.obj.get_node_id(node_info).startswith(name), 'Returned Node name does not match the real one')
def test_get_cluster_managers(self, mock_list_nodes): mock_list_nodes.return_value = [ fake.mock_kubernetes_node(), fake.mock_kubernetes_node() ] # if there are no managers, then return an empty list self.assertEqual( self.obj.get_cluster_managers(), [], 'There are no manager but got something else than an empty list') # if there's one manager, than get only the one's name manager = fake.mock_kubernetes_node("manager") manager.metadata.labels = {'node-role.kubernetes.io/master': ''} mock_list_nodes.return_value = [ fake.mock_kubernetes_node(), fake.mock_kubernetes_node(), manager ] self.assertEqual( self.obj.get_cluster_managers(), [manager.metadata.name], 'There is one manager (called "manager"), but got some other list of managers' ) # for multiple manager, get them all mock_list_nodes.return_value = [ fake.mock_kubernetes_node(), fake.mock_kubernetes_node(), manager, manager ] self.assertEqual(len(self.obj.get_cluster_managers()), 2, 'There are 2 managers, but got a different number')
def test_list_nodes(self): self.obj.client.list_node.return_value.items = [ fake.mock_kubernetes_node() ] self.assertIsInstance( self.obj.list_nodes(), list, 'List nodes should returns its items, a list, but got something else instead' ) self.obj.client.list_node.assert_called_once()
def test_get_kubelet_version(self, mock_get_node_info): node = fake.mock_kubernetes_node() mock_get_node_info.return_value = node # simple node attr lookup self.assertEqual( self.obj.get_kubelet_version(), node.status.node_info.kubelet_version, f'Expecting Kubelet version {node.status.node_info.kubelet_version} but got something else' )
def test_get_cluster_managers(self, mock_list_nodes): node_one = fake.mock_kubernetes_node('1') node_two = fake.mock_kubernetes_node('2') # if nodes have no labels, then there are no managers mock_list_nodes.return_value = [node_one, node_two] self.assertEqual(self.obj.get_cluster_managers(), [], 'Got cluster managers even though there are none') # if master keywords are not in the labels, then there are no managers again node_one.metadata.labels = ['not-a-manager'] mock_list_nodes.return_value = [node_one, node_two] self.assertEqual(self.obj.get_cluster_managers(), [], 'Mistaken a worker by a manager') # otherwise, get the manager node_two.metadata.labels = ['node-role.kubernetes.io/master'] self.assertEqual(self.obj.get_cluster_managers(), [node_two.metadata.name], 'Failed to get k8s cluster manager')
def test_get_hostname(self): # always gives back the class attribute self.assertEqual(self.obj.get_hostname(), self.obj.host_node_name, 'Failed to get hostname') # even if an arg is provided node = fake.mock_kubernetes_node() self.assertEqual( self.obj.get_hostname(node_info=node), self.obj.host_node_name, 'Failed to get hostname when the node is given as an arg')
def test_get_node_labels(self, mock_get_node_info): node = fake.mock_kubernetes_node() node.metadata.labels = {} # no labels are set by default mock_get_node_info.return_value = node self.assertEqual(self.obj.get_node_labels(), [], 'Unable to get k8s empty node labels') node.metadata.labels = {'fake-label': 'fake-value'} mock_get_node_info.return_value = node self.assertEqual(self.obj.get_node_labels(), ['fake-label=fake-value'], 'Unable to get k8s node labels')
def test_get_cluster_info(self, mock_get_node_info, mock_cluster_id, mock_list_nodes): me = fake.mock_kubernetes_node(uid='myself-fake-id') mock_cluster_id.return_value = 'fake-id' mock_get_node_info.return_value = me mock_list_nodes.return_value = [me, fake.mock_kubernetes_node()] expected_fields = [ 'cluster-id', 'cluster-orchestrator', 'cluster-managers', 'cluster-workers' ] # if all goes well, we should get the above keys self.assertEqual( sorted(expected_fields), sorted(list(self.obj.get_cluster_info().keys())), 'The expected cluster keys were not given back while getting cluster info' ) # as is, we should expect 2 workers and 0 managers self.assertEqual(len(self.obj.get_cluster_info()['cluster-workers']), 2, 'Expecting 2 k8s workers but got something else') self.assertEqual(len(self.obj.get_cluster_info()['cluster-managers']), 0, 'Expecting no k8s manager but got something else') # COE should also match with class' COE self.assertEqual(self.obj.get_cluster_info()['cluster-orchestrator'], self.NuvlaBoxCommon.ORCHESTRATOR_COE, 'Got the wrong cluster-orchestrator') # but if one of the nodes is a master, then we should get 1 worker and 1 manager me.metadata.labels = {'node-role.kubernetes.io/master': ''} mock_get_node_info.return_value = me mock_list_nodes.return_value = [me, fake.mock_kubernetes_node()] self.assertEqual(len(self.obj.get_cluster_info()['cluster-workers']), 1, 'Expecting 1 k8s workers but got something else') self.assertEqual(len(self.obj.get_cluster_info()['cluster-managers']), 1, 'Expecting 1 k8s manager but got something else') self.assertEqual(self.obj.get_cluster_info()['cluster-managers'][0], me.metadata.name, 'Expecting 2 k8s workers but got something else')
def test_get_host_os(self, mock_get_node_info): # if get_node_info returns something valid, we get a valid string out of it node = fake.mock_kubernetes_node() mock_get_node_info.return_value = node self.assertIsInstance(self.obj.get_host_os(), str, 'Host OS should be a string') self.assertEqual( self.obj.get_host_os(), f"{node.status.node_info.os_image} {node.status.node_info.kernel_version}", 'Did not get the expected host OS value') # otherwise, we get None mock_get_node_info.return_value = None self.assertIsNone(self.obj.get_host_os(), 'Host OS should be None cause Node is not defined')
def test_get_cluster_id(self): node_info = fake.mock_kubernetes_node() # should always return the ID value indicated in the passed argument # if Node does not have cluster name, then return the default one passed as an arg default_cluster_name = 'fake-cluster' self.assertEqual( self.obj.get_cluster_id(node_info, default_cluster_name=default_cluster_name), default_cluster_name, 'Returned Cluster name does not match the default one') # but if Node has it, take it from there cluster_name = 'new-cluster-name' node_info.metadata.cluster_name = cluster_name self.assertEqual( self.obj.get_cluster_id(node_info, default_cluster_name=default_cluster_name), cluster_name, 'Returned Cluster name does not match the real one')
def test_get_version(self, mock_get_node_info): # lookup mock_get_node_info.return_value = fake.mock_kubernetes_node() self.assertEqual(self.obj.get_version(), 'v1', 'Failed to get kubelet version')
def test_count_images_in_this_host(self, mock_get_node_info): # lookup mock_get_node_info.return_value = fake.mock_kubernetes_node() self.assertEqual(self.obj.count_images_in_this_host(), len(fake.mock_kubernetes_node().status.images), 'Failed to get count of images in k8s node')
def test_get_node_id(self, mock_get_node_info): mock_get_node_info.return_value = fake.mock_kubernetes_node('node-name') # lookup self.assertTrue(self.obj.get_node_id().startswith('node-name'), 'Failed to get Kubernetes Node ID')
def test_get_host_architecture(self): node_info = fake.mock_kubernetes_node() # simple attribute lookup self.assertEqual(self.obj.get_host_architecture(node_info), node_info.status.node_info.architecture, 'Host architecture does not match the real one')