def test_delete_failed(self):
        self.client.delete_pod.side_effect = [
            rest.ApiException(status=500, reason="Test")
        ]

        self.assertRaises(rest.ApiException, self.scenario.run, "test/image",
                          volume_type="Directory", volume_path="/path",
                          mount_path="/opt/check")
        self.client.create_pod.assert_called_once_with(
            "test/image",
            name="name",
            namespace="ns",
            command=None,
            volume={
                "mount_path": [
                    {
                        "mountPath": "/opt/check",
                        "name": "name"
                    }
                ],
                "volume": [
                    {
                        "name": "name",
                        "hostPath": {
                            "type": "Directory",
                            "path": "/path"
                        }
                    }
                ]
            },
            status_wait=True
        )
Beispiel #2
0
    def test_execute_skip_exception(self, mock_get_api, *args):
        mock_api = mock.Mock()
        mock_api.delete_namespaced_service.side_effect = rest.ApiException(
            status=404)

        mock_get_api.return_value = mock_api
        kubernetes_utils.delete_service(mock.ANY, skip_codes=[404])
Beispiel #3
0
    def testWaitUntilRunning(self, mock_time):
        # Prepare mocks and variables.
        self._AssumeInsideKfp()
        runner = self._CreateKubernetesRunner()
        mock_time.side_effect = list(range(20))
        pending_pod = mock.Mock()
        pending_pod.status.phase = 'Pending'
        running_pod = mock.Mock()
        running_pod.status.phase = 'Running'
        self._mock_core_v1_api.read_namespaced_pod.side_effect = [
            rest.ApiException('meh'),  # Error is tolerable.
            pending_pod,
            pending_pod,
            running_pod
        ]

        # Act.
        runner.Start()
        try:
            runner.WaitUntilRunning(deadline=10)
        except Exception as e:  # pylint: disable=broad-except
            self.fail(e)

        # Check calls.
        self.assertEqual(self._mock_core_v1_api.read_namespaced_pod.call_count,
                         4)
    def test_rollout_failed(self):
        self.client.create_deployment.return_value = "test"
        self.client.rollout_deployment.side_effect = [
            rest.ApiException(status=500, reason="Test")
        ]

        self.assertRaises(rest.ApiException,
                          self.scenario.run,
                          "test/image",
                          2,
                          changes={"image": "test/image2"})
        self.client.create_deployment.assert_called_once_with(
            namespace="ns",
            env=None,
            image="test/image",
            replicas=2,
            command=None,
            resources=None,
            status_wait=True)
        self.client.rollout_deployment.assert_called_once_with(
            "test",
            namespace="ns",
            replicas=2,
            changes={"image": "test/image2"},
            status_wait=True)
        self.assertEqual(0, self.client.delete_deployment.call_count)
 def test_create_pod_failed(self):
     kwargs = dict(image="test/image",
                   command=["ls"],
                   mount_path="/opt/check",
                   persistent_volume={
                       "size": "1Gi",
                       "volume_mode": "Block",
                       "local_path": "/check",
                       "access_modes": ["ReadWriteOnly"],
                       "node_affinity": {
                           "stub": "stub"
                       }
                   },
                   persistent_volume_claim={
                       "size": "1Gi",
                       "access_modes": ["ReadWriteOnly"]
                   },
                   check_cmd=["ls"],
                   status_wait=True)
     self.client.create_pod.side_effect = [
         rest.ApiException(status=500, reason="Test")
     ]
     self.assertRaises(rest.ApiException, self.scenario.run, **kwargs)
     self.assertEqual(0, self.client.check_volume_pod.call_count)
     self.assertEqual(0, self.client.delete_pod.call_count)
Beispiel #6
0
    def test_execute_skip_exception(self, mock_log, mock_get_api):
        mock_api = mock.Mock()
        mock_api.delete_namespaced_config_map.side_effect = rest.ApiException(
            status=404)

        mock_get_api.return_value = mock_api
        kubernetes_utils.delete_config_map(mock.ANY, skip_codes=[404])
        mock_log.info.assert_called_once()
Beispiel #7
0
    def test_execute_exception(self, mock_get_api):
        mock_api = mock.Mock()
        mock_api.delete_namespaced_config_map.side_effect = rest.ApiException(
            status=200)

        mock_get_api.return_value = mock_api
        with self.assertRaises(exceptions.KubernetesApiException):
            kubernetes_utils.delete_config_map(mock.ANY, skip_codes=[404])
Beispiel #8
0
    def test_create_failed(self):
        self.client.create_pod.side_effect = [
            rest.ApiException(status=500, reason="Test")
        ]

        self.assertRaises(rest.ApiException, self.scenario.run, "test/image")
        self.assertEqual(0, self.client.get_pod.call_count)
        self.assertEqual(0, self.client.delete_pod.call_count)
    def test_create_failed(self):
        self.client.create_pod.side_effect = [
            rest.ApiException(status=500, reason="Test")
        ]

        self.assertRaises(rest.ApiException, self.scenario.run, "test/image",
                          mount_path="/opt/check", volume_type="Directory",
                          volume_path="/path",)
        self.assertEqual(0, self.client.delete_pod.call_count)
 def test_create_failed(self):
     client = mock.MagicMock()
     client.create_namespace.side_effect = [
         rest.ApiException(status=500, reason="Test")
     ]
     scenario = namespaces.CreateAndDeleteNamespace()
     scenario.generate_random_name = mock.MagicMock()
     scenario.client = client
     self.assertRaises(rest.ApiException, scenario.run)
     client.create_namespace.assert_called_once_with(None, status_wait=True)
     self.assertEqual(0, client.delete_namespace.call_count)
Beispiel #11
0
    def test_create_failed(self):
        self.client.create_pod.side_effect = [
            rest.ApiException(status=500, reason="Test")
        ]

        self.assertRaises(rest.ApiException,
                          self.scenario.run,
                          "test/image",
                          configmap_data={"test.txt": "test"},
                          mount_path="/opt/check")
        self.assertEqual(0, self.client.delete_pod.call_count)
Beispiel #12
0
    def test_delete_failed(self):
        self.client.create_job.return_value = "test"
        self.client.delete_job.side_effect = [
            rest.ApiException(status=500, reason="Test")
        ]

        self.assertRaises(rest.ApiException, self.scenario.run, "test/image",
                          ["ls"])
        self.client.create_job.assert_called_once_with(command=["ls"],
                                                       namespace="ns",
                                                       image="test/image",
                                                       status_wait=True)
    def test_check_health_failed(self):
        self.service_cls.reset_mock()
        self.service.get_version.side_effect = [
            rest.ApiException(status=500, reason="Test")
        ]

        platform = existing.KubernetesPlatform({})
        health = platform.check_health()
        self.assertFalse(health["available"])

        expected_msg = "Something went wrong: (500)\nReason: Test\n"
        self.assertEqual(expected_msg, health["message"])
Beispiel #14
0
    def test_execute_exception(self, mock_get_api):
        mock_api = mock.Mock()
        mock_api.get_cluster_custom_object.side_effect = rest.ApiException(404)
        mock_api.get_namespaced_custom_object.side_effect = rest.ApiException(
            404)
        mock_get_api.return_value = mock_api
        group = 'group.com'
        version = mock.Mock()
        plural = 'networks'
        name = 'net_one'

        network_obj = kubernetes_utils.get_network(constants.SCOPE_CLUSTER,
                                                   group, version, plural,
                                                   name)
        self.assertIsNone(network_obj)

        mock_api.reset_mock()
        network_obj = kubernetes_utils.get_network(constants.SCOPE_NAMESPACED,
                                                   group, version, plural,
                                                   name)
        self.assertIsNone(network_obj)
Beispiel #15
0
    def test_get_pod_failed(self):
        self.client.get_pod.side_effect = [
            rest.ApiException(status=500, reason="Test")
        ]

        self.assertRaises(rest.ApiException, self.scenario.run, "test/image")
        self.client.create_pod.assert_called_once_with(
            "test/image",
            command=None,
            namespace="ns",
            status_wait=True
        )
        self.assertEqual(0, self.client.delete_pod.call_count)
Beispiel #16
0
    def test_delete_namespace_and_wait_termination_success(self):
        self.config_cls.reset_mock()
        self.api_cls.reset_mock()
        self.client_cls.reset_mock()

        self.client.read_namespace.side_effect = [
            rest.ApiException(status=404, reason="Not found")
        ]

        self.k8s_client.delete_namespace("test")

        self.client.delete_namespace.assert_called_once()
        self.client.read_namespace.assert_called_once_with("test")
    def test_create_failed(self):
        self.client.create_rc.side_effect = [
            rest.ApiException(status=500, reason="Test")
        ]

        self.assertRaises(rest.ApiException, self.scenario.run, "test/image",
                          2)
        self.client.create_rc.assert_called_once_with(namespace="ns",
                                                      image="test/image",
                                                      replicas=2,
                                                      command=None,
                                                      status_wait=True)
        self.assertEqual(0, self.client.delete_rc.call_count)
Beispiel #18
0
    def __deserialize_date(self, string):
        """Deserializes string to date.

        :param string: str.
        :return: date.
        """
        try:
            return parse(string).date()
        except ImportError:
            return string
        except ValueError:
            raise rest.ApiException(
                status=0,
                reason="Failed to parse `{0}` as date object".format(string))
Beispiel #19
0
    def test_delete_namespace_and_wait_termination_read_failed(self):
        self.config_cls.reset_mock()
        self.api_cls.reset_mock()
        self.client_cls.reset_mock()

        self.client.read_namespace.side_effect = [
            rest.ApiException(status=500, reason="Test")
        ]

        self.assertRaises(rest.ApiException, self.k8s_client.delete_namespace,
                          "test")

        self.client.delete_namespace.assert_called_once()
        self.client.read_namespace.assert_called_once()
Beispiel #20
0
    def test_execute_skip_exception(self, mock_log, mock_get_api, mock_delobj):
        mock_delete_crd = mock.Mock()
        mock_delete_crd.delete_custom_resource_definition.side_effect = rest.ApiException(
            status=404)

        mock_get_api.return_value = mock_delete_crd
        kubernetes_utils.delete_custom_resource_definition('name',
                                                           skip_codes=[404])

        mock_delobj.assert_called_once()
        mock_delete_crd.delete_custom_resource_definition.assert_called_once_with(
            'name', 'del_obj')

        mock_log.info.assert_called_once()
Beispiel #21
0
    def test_execute_skip_exception(self, mock_log, mock_get_api):
        mock_api = mock.Mock()
        mock_api.delete_cluster_custom_object.side_effect = rest.ApiException(
            status=404)

        mock_get_api.return_value = mock_api
        kubernetes_utils.delete_network(constants.SCOPE_CLUSTER,
                                        mock.ANY,
                                        mock.ANY,
                                        mock.ANY,
                                        mock.ANY,
                                        skip_codes=[404])

        mock_log.info.assert_called_once()
    def test_delete_failed(self):
        self.client.create_statefulset.return_value = "test"
        self.client.delete_statefulset.side_effect = [
            rest.ApiException(status=500, reason="Test")
        ]

        self.assertRaises(rest.ApiException, self.scenario.run, "test/image",
                          2, 3)
        self.client.create_statefulset.assert_called_once_with(
            command=None,
            namespace="ns",
            replicas=2,
            image="test/image",
            status_wait=True)
        self.assertEqual(2, self.client.scale_statefulset.call_count)
    def test_delete_failed(self):
        self.client.create_daemonset.return_value = "test", "testapp"
        self.client.delete_daemonset.side_effect = [
            rest.ApiException(status=500, reason="Test")
        ]

        self.assertRaises(rest.ApiException, self.scenario.run, "test/image")
        self.client.create_daemonset.assert_called_once_with(
            command=None,
            namespace="ns",
            node_labels=None,
            image="test/image",
            status_wait=True
        )
        self.client.check_daemonset.assert_called_once()
Beispiel #24
0
    def test_delete_pod_and_wait_termination_delete_failed(self):
        self.config_cls.reset_mock()
        self.api_cls.reset_mock()
        self.client_cls.reset_mock()

        self.client.delete_namespaced_pod.side_effect = [
            rest.ApiException(status=500, reason="Test")
        ]

        self.assertRaises(rest.ApiException,
                          self.k8s_client.delete_pod,
                          "test",
                          namespace="ns")

        self.client.delete_namespaced_pod.assert_called_once()
        self.assertEqual(0, self.client.read_namespaced_pod.call_count)
Beispiel #25
0
    def test_create_and_wait_namespace_fail_read(self):
        self.config_cls.reset_mock()
        self.api_cls.reset_mock()
        self.client_cls.reset_mock()

        self.client.read_namespace.side_effect = [
            rest.ApiException(status=500, reason="Test")
        ]

        self.assertRaises(rest.ApiException,
                          self.k8s_client.create_namespace,
                          "test",
                          status_wait=True)

        self.client.create_namespace.assert_called_once()
        self.client.read_namespace.assert_called_once()
Beispiel #26
0
    def __deserialize_datetime(self, string):
        """Deserializes string to datetime.

        The string should be in iso8601 datetime format.

        :param string: str.
        :return: datetime.
        """
        try:
            return parse(string)
        except ImportError:
            return string
        except ValueError:
            raise rest.ApiException(
                status=0,
                reason=(
                    "Failed to parse `{0}` as datetime object".format(string)))
    def test_second_scale_failed(self):
        self.client.create_rc.return_value = "test"
        self.client.scale_rc.side_effect = [
            None, rest.ApiException(status=500, reason="Test")
        ]

        self.assertRaises(rest.ApiException, self.scenario.run, "test/image",
                          2, 3)
        self.client.create_rc.assert_called_once_with(namespace="ns",
                                                      image="test/image",
                                                      replicas=2,
                                                      command=None,
                                                      status_wait=True)
        self.assertEqual(2, self.client.scale_rc.call_count)
        self.assertEqual(dict(namespace="ns", replicas=2, status_wait=True),
                         self.client.scale_rc.call_args[1])
        self.assertEqual(0, self.client.delete_rc.call_count)
Beispiel #28
0
    def test_create_and_wait_pod_fail_create(self):
        self.config_cls.reset_mock()
        self.api_cls.reset_mock()
        self.client_cls.reset_mock()

        self.client.create_namespaced_pod.side_effect = [
            rest.ApiException(status=500, reason="Test")
        ]

        self.assertRaises(rest.ApiException,
                          self.k8s_client.create_pod,
                          "name",
                          image="test/image",
                          namespace="ns",
                          status_wait=True)

        self.client.create_namespaced_pod.assert_called_once()
        self.assertEqual(0, self.client.read_namespaced_pod.call_count)
Beispiel #29
0
    def test_delete_pod_failed(self):
        resp = mock.MagicMock()
        resp.status.conditions = []
        self.client.create_pod.return_value = "test"
        self.client.get_pod.return_value = resp
        self.client.delete_pod.side_effect = [
            rest.ApiException(status=500, reason="Test")
        ]

        self.assertRaises(rest.ApiException, self.scenario.run, "test/image")
        self.client.create_pod.assert_called_once_with(
            "test/image",
            command=None,
            namespace="ns",
            status_wait=True
        )
        self.client.get_pod.assert_called_once_with(
            "test",
            namespace="ns"
        )
Beispiel #30
0
    def test_delete_failed(self):
        self.client.delete_pod.side_effect = [
            rest.ApiException(status=500, reason="Test")
        ]

        self.assertRaises(rest.ApiException,
                          self.scenario.run,
                          "test/image",
                          configmap_data={"test.txt": "test"},
                          subpath="test.txt",
                          mount_path="/opt/check",
                          check_cmd=["ls"])
        self.client.create_pod.assert_called_once_with("test/image",
                                                       name="name",
                                                       namespace="ns",
                                                       command=None,
                                                       volume={
                                                           "mount_path": [{
                                                               "mountPath":
                                                               "/opt/check",
                                                               "name":
                                                               "name",
                                                               "subPath":
                                                               "test.txt"
                                                           }],
                                                           "volume": [{
                                                               "name": "name",
                                                               "configMap": {
                                                                   "name":
                                                                   "name"
                                                               }
                                                           }]
                                                       },
                                                       status_wait=True)
        self.client.check_volume_pod.assert_called_once()
        self.client.delete_pod.assert_called_once()