Esempio n. 1
0
    def test_find_nuvlabox_agent_container(self):
        pods = mock.MagicMock()
        # if cannot find pod, get None
        pods.items = []
        self.obj.client.list_namespaced_pod.return_value = pods
        self.assertEqual(self.obj.find_nuvlabox_agent_container()[0], None,
                         'Found agent pod when it should not exist')
        self.assertTrue(self.obj.find_nuvlabox_agent_container()[1].startswith('There are no pods'),
                        'Got the wrong error message when no pods are found')

        # if it exists, but the name is not "agent", get None
        pods.items = [fake.mock_kubernetes_pod('pod-wrong-name')]
        self.obj.client.list_namespaced_pod.return_value = pods
        self.assertEqual(self.obj.find_nuvlabox_agent_container()[0], None,
                         'Found agent pod when pod name does not match')
        self.assertTrue(self.obj.find_nuvlabox_agent_container()[1].startswith('Cannot find agent container within'),
                        'Got the wrong error message when there is a pod name mismatch')

        # otherwise, get the container back
        pods.items = [fake.mock_kubernetes_pod('agent')]
        self.obj.client.list_namespaced_pod.return_value = pods
        self.assertEqual(self.obj.find_nuvlabox_agent_container()[0].name, 'agent',
                         'Failed to find agent container')
        self.assertIsNone(self.obj.find_nuvlabox_agent_container()[1],
                          'Got an error message on success')
    def test_install_ssh_key(self):
        # if there's an error while looking for an existing SSH installer pod, an exception is raised
        self.obj.client.read_namespaced_pod.side_effect = kubernetes.client.exceptions.ApiException(
        )
        self.assertRaises(kubernetes.client.exceptions.ApiException,
                          self.obj.install_ssh_key, '', '')

        # if the pod already exists, and is running, then we need to wait, and we get False
        self.obj.client.read_namespaced_pod.reset_mock(side_effect=True)
        self.obj.client.read_namespaced_pod.return_value = fake.mock_kubernetes_pod(
        )
        self.assertFalse(
            self.obj.install_ssh_key('', ''),
            'Failed to verify that an SSH installer is already running')

        # otherwise, it deletes the finished previous installer and installs a new key
        self.obj.client.read_namespaced_pod.return_value = fake.mock_kubernetes_pod(
            phase='terminated')
        self.obj.client.delete_namespaced_pod.return_value = True
        self.obj.client.create_namespaced_pod.return_value = True
        self.assertTrue(self.obj.install_ssh_key('', ''),
                        'Failed to install SSH key')
        self.obj.client.create_namespaced_pod.assert_called_once()
        self.obj.client.delete_namespaced_pod.assert_called_once()

        # also, if the initial check for an existing container returns 404, we continue
        self.obj.client.read_namespaced_pod.side_effect = kubernetes.client.exceptions.ApiException(
            status=404)
        self.assertTrue(self.obj.install_ssh_key('', ''),
                        'Failed to install SSH key')
        self.obj.client.delete_namespaced_pod.assert_called_once()
        self.assertEqual(
            self.obj.client.create_namespaced_pod.call_count, 2,
            'Upon a 404, the SSH installer was not deployed as expected')
    def test_collect_container_metrics(self, mock_get_node_info,
                                       mock_pod_metrics):
        pod_list = mock.MagicMock()
        pod_list.items = [
            fake.mock_kubernetes_pod("pod-1"),
            fake.mock_kubernetes_pod("pod-2")
        ]
        self.obj.client.list_pod_for_all_namespaces.return_value = pod_list
        mock_get_node_info.return_value.status.return_value.capacity = {
            'cpu': 1,
            'memory': '1Ki'
        }

        # if there are no pod to collect metrics from, return []
        mock_pod_metrics.return_value = {'items': []}
        self.assertEqual(
            self.obj.collect_container_metrics(), [],
            'Returned container metrics when no pods are running')

        # if there are pod metrics, they must all match with the list of pods
        new_pod = fake.mock_kubernetes_pod_metrics('wrong-name')
        mock_pod_metrics.return_value = {'items': [new_pod]}
        self.assertEqual(
            self.obj.collect_container_metrics(), [],
            'Returned container metrics when there is a mismatch between existing pods and metrics'
        )

        # if pod metrics match the list of pods, then we should get a non-empty list, with cpu and mem values/container
        mock_pod_metrics.return_value = {
            'items': [
                fake.mock_kubernetes_pod_metrics("pod-1"),
                fake.mock_kubernetes_pod_metrics("pod-2")
            ]
        }
        self.assertIsInstance(
            self.obj.collect_container_metrics(), list,
            'Expecting list of pod container metrics, but got something else')

        expected_field = [
            'container-status', 'name', 'id', 'cpu-percent', 'mem-percent'
        ]
        self.assertEqual(
            sorted(expected_field),
            sorted(list(self.obj.collect_container_metrics()[0].keys())),
            'Missing container metrics keys')
        self.assertEqual(
            len(self.obj.collect_container_metrics()), 2,
            'Expecting metrics for 2 containers, but got something else')
    def test_is_nuvla_job_running(self):
        self.obj.client.delete_namespaced_pod.reset_mock()
        # if there's an error while looking for pod, we default to True
        self.obj.client.read_namespaced_pod.side_effect = kubernetes.client.exceptions.ApiException(
        )
        self.assertTrue(
            self.obj.is_nuvla_job_running('', ''),
            'Says Nuvla job is not running even though it cannot be sure of that'
        )

        # if 404, then False
        self.obj.client.read_namespaced_pod.side_effect = kubernetes.client.exceptions.ApiException(
            status=404)
        self.assertFalse(
            self.obj.is_nuvla_job_running('', ''),
            'Says Nuvla job is running, when respective pod could not be found'
        )

        # if found, we continue to see its state
        self.obj.client.read_namespaced_pod.reset_mock(side_effect=True)
        self.obj.client.read_namespaced_pod.return_value = fake.mock_kubernetes_pod(
            phase='running')
        self.assertTrue(self.obj.is_nuvla_job_running('', ''),
                        'Nuvla job is running, but got the opposite message')

        self.obj.client.read_namespaced_pod.return_value = fake.mock_kubernetes_pod(
            phase='pending')
        self.assertFalse(
            self.obj.is_nuvla_job_running('', ''),
            'Says Nuvla job is running, when in fact it is pending')

        # for any other state, delete the pod and return False
        self.obj.client.read_namespaced_pod.return_value = fake.mock_kubernetes_pod(
            phase='succeeded')
        self.obj.client.delete_namespaced_pod.return_value = True
        self.assertFalse(
            self.obj.is_nuvla_job_running('', ''),
            'Says Nuvla job is running, even though it should have been deleted'
        )
        self.obj.client.delete_namespaced_pod.assert_called_once()

        # if deletion fails, return True
        self.obj.client.delete_namespaced_pod.side_effect = kubernetes.client.exceptions.ApiException(
        )
        self.assertTrue(
            self.obj.is_nuvla_job_running('', ''),
            'Dunno if job pod is running, but saying that is is not')
Esempio n. 5
0
 def test_fetch_container_logs(self):
     self.obj.client.read_namespaced_pod_log.return_value = 'foo\nbar'
     component = fake.mock_kubernetes_pod('name')
     expected_output = [[
         ' [name] foo',
         ' [name] bar'
     ]]  # just for 1 container
     self.assertEqual(self.obj.fetch_container_logs(component, 0), expected_output,
                      'Failed to get container logs')
Esempio n. 6
0
    def test_list_all_containers_in_this_node(self):
        # no containers = get []
        pods = mock.MagicMock()
        pods.items = []
        self.obj.client.list_pod_for_all_namespaces.return_value = pods
        self.assertEqual(self.obj.list_all_containers_in_this_node(), [],
                         'Got k8s containers when there are none')

        # otherwise, get all their info
        pods = mock.MagicMock()
        pods.items = [fake.mock_kubernetes_pod('one'), fake.mock_kubernetes_pod('two')]
        self.obj.client.list_pod_for_all_namespaces.return_value = pods
        self.assertEqual(len(self.obj.list_all_containers_in_this_node()),
                         len(pods.items) * len(fake.mock_kubernetes_pod().status.container_statuses),
                         'Failed to get all k8s containers')

        self.assertTrue(all(True for x in self.obj.list_all_containers_in_this_node() if isinstance(x, str)),
                        'Expecting list of strings')
    def test_is_vpn_client_running(self):
        pod = fake.mock_kubernetes_pod()
        # if there are no pods with the vpn-client label, we get False
        self.obj.client.list_pod_for_all_namespaces.return_value.items = []
        self.assertFalse(self.obj.is_vpn_client_running(),
                         'Saying VPN client is running even though it is not')

        # but if there are matching pods, returns False if no containers match the vpn-client name, True otherwise
        self.obj.client.list_pod_for_all_namespaces.return_value.items = [
            pod, pod
        ]
        self.assertFalse(
            self.obj.is_vpn_client_running(),
            'Says VPN client is running when none of the pods are from the VPN component'
        )

        vpn_pod = fake.mock_kubernetes_pod()
        vpn_pod.status.container_statuses[
            0].name = self.obj.vpn_client_component
        self.obj.client.list_pod_for_all_namespaces.return_value.items = [
            pod, vpn_pod
        ]
        self.assertTrue(self.obj.is_vpn_client_running(),
                        'Says VPN client is not running, but it is')
Esempio n. 8
0
    def test_get_component_id(self):
        component = fake.mock_kubernetes_pod('id')

        # simple lookup
        self.assertEqual(self.obj.get_component_id(component), 'id',
                         'Failed to lookup pod ID')
Esempio n. 9
0
    def test_get_component_name(self):
        component = fake.mock_kubernetes_pod('name')

        # simple lookup
        self.assertEqual(self.obj.get_component_name(component), 'name',
                         'Failed to lookup pod name')