Exemplo n.º 1
0
    def test_get_snapshot(self):
        mock_snapshot = Mock()
        mock_snapshot_operations = Mock(get=Mock(return_value=mock_snapshot))
        with patch("azure.cli.command_modules.acs._helpers.cf_snapshots",
                   return_value=mock_snapshot_operations):
            snapshot = helpers.get_snapshot("mock_cli_ctx", "mock_rg",
                                            "mock_snapshot_name")
            self.assertEqual(snapshot, mock_snapshot)

        mock_snapshot_operations_2 = Mock(get=Mock(
            side_effect=AzureError("mock snapshot was not found")))
        with patch("azure.cli.command_modules.acs._helpers.cf_snapshots",
                   return_value=mock_snapshot_operations_2), self.assertRaises(
                       ResourceNotFoundError):
            helpers.get_snapshot("mock_cli_ctx", "mock_rg",
                                 "mock_snapshot_name")

        http_response_error = HttpResponseError()
        http_response_error.status_code = 400
        http_response_error.message = "test_error_msg"
        mock_snapshot_operations_3 = Mock(get=Mock(
            side_effect=http_response_error))
        with patch("azure.cli.command_modules.acs._helpers.cf_snapshots",
                   return_value=mock_snapshot_operations_3), self.assertRaises(
                       BadRequestError):
            helpers.get_snapshot("mock_cli_ctx", "mock_rg",
                                 "mock_snapshot_name")
Exemplo n.º 2
0
    def test_get_nodepool_snapshot(self):
        mock_snapshot = Mock()
        mock_snapshot_operations = Mock(get=Mock(return_value=mock_snapshot))
        with patch("azext_aks_preview._helpers.get_nodepool_snapshots_client",
                   return_value=mock_snapshot_operations):
            snapshot = get_nodepool_snapshot("mock_cli_ctx", "test_sub",
                                             "mock_rg", "mock_snapshot_name")
            self.assertEqual(snapshot, mock_snapshot)

        mock_snapshot_operations_2 = Mock(get=Mock(
            side_effect=AzureError("mock snapshot was not found")))
        with patch("azext_aks_preview._helpers.get_nodepool_snapshots_client",
                   return_value=mock_snapshot_operations_2), self.assertRaises(
                       ResourceNotFoundError):
            get_nodepool_snapshot("mock_cli_ctx", "test_sub", "mock_rg",
                                  "mock_snapshot_name")

        http_response_error = HttpResponseError()
        http_response_error.status_code = 400
        http_response_error.message = "test_error_msg"
        mock_snapshot_operations_3 = Mock(get=Mock(
            side_effect=http_response_error))
        with patch("azext_aks_preview._helpers.get_nodepool_snapshots_client",
                   return_value=mock_snapshot_operations_3), self.assertRaises(
                       BadRequestError):
            get_nodepool_snapshot("mock_cli_ctx", "test_sub", "mock_rg",
                                  "mock_snapshot_name")
Exemplo n.º 3
0
 def test_http_response_error(self):
     status_codes = [x for x in range(400, 405)] + [500, 1000, None]
     cli_errors = [
         BadRequestError,
         UnauthorizedError,
         UnclassifiedUserFault,
         ForbiddenError,
         ResourceNotFoundError,
         AzureInternalError,
         ServiceError,
         ServiceError,
     ]
     status_code_cli_error_pairs = list(zip(status_codes, cli_errors))
     azure_error = HttpResponseError()
     for idx, status_code_cli_error_pair in enumerate(
             status_code_cli_error_pairs, 1):
         # get mapped error
         status_code = status_code_cli_error_pair[0]
         azure_error.status_code = status_code
         azure_error.message = f"error_msg_{idx}"
         mapped_error = helpers.map_azure_error_to_cli_error(azure_error)
         # get mock error
         cli_error = status_code_cli_error_pair[1]
         mock_error = cli_error(f"error_msg_{idx}")
         self.check_error_equality(mapped_error, mock_error)