| 
									
										
										
										
											2022-02-15 13:33:47 -01:00
										 |  |  | """Unit tests for servicediscovery-supported APIs.""" | 
					
						
							| 
									
										
										
										
											2023-08-07 12:52:34 -04:00
										 |  |  | import re | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-15 13:33:47 -01:00
										 |  |  | import boto3 | 
					
						
							| 
									
										
										
										
											2023-08-07 12:52:34 -04:00
										 |  |  | from botocore.exceptions import ClientError | 
					
						
							| 
									
										
										
										
											2022-02-15 13:33:47 -01:00
										 |  |  | import pytest | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from moto import mock_servicediscovery | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # See our Development Tips on writing tests for hints on how to write good tests: | 
					
						
							|  |  |  | # http://docs.getmoto.org/en/latest/docs/contributing/development_tips/tests.html | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_servicediscovery | 
					
						
							|  |  |  | def test_list_operations_initial(): | 
					
						
							|  |  |  |     client = boto3.client("servicediscovery", region_name="eu-west-1") | 
					
						
							|  |  |  |     resp = client.list_operations() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 12:52:34 -04:00
										 |  |  |     assert resp["Operations"] == [] | 
					
						
							| 
									
										
										
										
											2022-02-15 13:33:47 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_servicediscovery | 
					
						
							|  |  |  | def test_list_operations(): | 
					
						
							|  |  |  |     client = boto3.client("servicediscovery", region_name="eu-west-2") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = client.create_http_namespace(Name="n/a") | 
					
						
							| 
									
										
										
										
											2023-08-07 12:52:34 -04:00
										 |  |  |     assert "OperationId" in resp | 
					
						
							| 
									
										
										
										
											2022-02-15 13:33:47 -01:00
										 |  |  |     op_id = resp["OperationId"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = client.list_operations() | 
					
						
							| 
									
										
										
										
											2023-08-07 12:52:34 -04:00
										 |  |  |     assert len(resp["Operations"]) == 1 | 
					
						
							|  |  |  |     assert resp["Operations"] == [{"Id": op_id, "Status": "SUCCESS"}] | 
					
						
							| 
									
										
										
										
											2022-02-15 13:33:47 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_servicediscovery | 
					
						
							|  |  |  | def test_get_create_http_namespace_operation(): | 
					
						
							|  |  |  |     client = boto3.client("servicediscovery", region_name="eu-west-1") | 
					
						
							|  |  |  |     resp = client.create_http_namespace(Name="mynamespace") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 12:52:34 -04:00
										 |  |  |     assert re.match("[a-z0-9]{32}-[a-z0-9]{8}", resp["OperationId"]) | 
					
						
							| 
									
										
										
										
											2022-02-15 13:33:47 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     operation_id = resp["OperationId"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = client.get_operation(OperationId=operation_id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 12:52:34 -04:00
										 |  |  |     assert "Operation" in resp | 
					
						
							| 
									
										
										
										
											2022-02-15 13:33:47 -01:00
										 |  |  |     operation = resp["Operation"] | 
					
						
							| 
									
										
										
										
											2023-08-07 12:52:34 -04:00
										 |  |  |     assert operation["Id"] == operation_id | 
					
						
							|  |  |  |     assert operation["Type"] == "CREATE_NAMESPACE" | 
					
						
							|  |  |  |     assert operation["Status"] == "SUCCESS" | 
					
						
							|  |  |  |     assert "CreateDate" in operation | 
					
						
							|  |  |  |     assert "UpdateDate" in operation | 
					
						
							|  |  |  |     assert "Targets" in operation | 
					
						
							| 
									
										
										
										
											2022-02-15 13:33:47 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     targets = operation["Targets"] | 
					
						
							| 
									
										
										
										
											2023-08-07 12:52:34 -04:00
										 |  |  |     assert "NAMESPACE" in targets | 
					
						
							| 
									
										
										
										
											2022-02-15 13:33:47 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     namespaces = client.list_namespaces()["Namespaces"] | 
					
						
							| 
									
										
										
										
											2023-08-07 12:52:34 -04:00
										 |  |  |     assert targets["NAMESPACE"] in [ns["Id"] for ns in namespaces] | 
					
						
							| 
									
										
										
										
											2022-02-15 13:33:47 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_servicediscovery | 
					
						
							|  |  |  | def test_get_private_dns_namespace_operation(): | 
					
						
							|  |  |  |     client = boto3.client("servicediscovery", region_name="eu-west-1") | 
					
						
							|  |  |  |     resp = client.create_private_dns_namespace(Name="dns_ns", Vpc="vpc_id") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 12:52:34 -04:00
										 |  |  |     assert re.match("[a-z0-9]{32}-[a-z0-9]{8}", resp["OperationId"]) | 
					
						
							| 
									
										
										
										
											2022-02-15 13:33:47 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     operation_id = resp["OperationId"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = client.get_operation(OperationId=operation_id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 12:52:34 -04:00
										 |  |  |     assert "Operation" in resp | 
					
						
							| 
									
										
										
										
											2022-02-15 13:33:47 -01:00
										 |  |  |     operation = resp["Operation"] | 
					
						
							| 
									
										
										
										
											2023-08-07 12:52:34 -04:00
										 |  |  |     assert operation["Id"] == operation_id | 
					
						
							|  |  |  |     assert operation["Type"] == "CREATE_NAMESPACE" | 
					
						
							|  |  |  |     assert operation["Status"] == "SUCCESS" | 
					
						
							|  |  |  |     assert "CreateDate" in operation | 
					
						
							|  |  |  |     assert "UpdateDate" in operation | 
					
						
							|  |  |  |     assert "Targets" in operation | 
					
						
							| 
									
										
										
										
											2022-02-15 13:33:47 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_servicediscovery | 
					
						
							|  |  |  | def test_get_public_dns_namespace_operation(): | 
					
						
							|  |  |  |     client = boto3.client("servicediscovery", region_name="eu-west-1") | 
					
						
							|  |  |  |     resp = client.create_public_dns_namespace(Name="dns_ns") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 12:52:34 -04:00
										 |  |  |     assert re.match("[a-z0-9]{32}-[a-z0-9]{8}", resp["OperationId"]) | 
					
						
							| 
									
										
										
										
											2022-02-15 13:33:47 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     operation_id = resp["OperationId"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = client.get_operation(OperationId=operation_id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 12:52:34 -04:00
										 |  |  |     assert "Operation" in resp | 
					
						
							| 
									
										
										
										
											2022-02-15 13:33:47 -01:00
										 |  |  |     operation = resp["Operation"] | 
					
						
							| 
									
										
										
										
											2023-08-07 12:52:34 -04:00
										 |  |  |     assert operation["Id"] == operation_id | 
					
						
							|  |  |  |     assert operation["Type"] == "CREATE_NAMESPACE" | 
					
						
							|  |  |  |     assert operation["Status"] == "SUCCESS" | 
					
						
							|  |  |  |     assert "CreateDate" in operation | 
					
						
							|  |  |  |     assert "UpdateDate" in operation | 
					
						
							|  |  |  |     assert "Targets" in operation | 
					
						
							| 
									
										
										
										
											2022-02-15 13:33:47 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_servicediscovery | 
					
						
							|  |  |  | def test_get_update_service_operation(): | 
					
						
							|  |  |  |     client = boto3.client("servicediscovery", region_name="eu-west-1") | 
					
						
							|  |  |  |     service_id = client.create_service( | 
					
						
							| 
									
										
										
										
											2022-03-10 13:39:59 -01:00
										 |  |  |         Name="my service", NamespaceId="ns_id", Description="first desc" | 
					
						
							| 
									
										
										
										
											2022-02-15 13:33:47 -01:00
										 |  |  |     )["Service"]["Id"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = client.update_service(Id=service_id, Service={"Description": "updated desc"}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 12:52:34 -04:00
										 |  |  |     assert re.match("[a-z0-9]{32}-[a-z0-9]{8}", resp["OperationId"]) | 
					
						
							| 
									
										
										
										
											2022-02-15 13:33:47 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     operation_id = resp["OperationId"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = client.get_operation(OperationId=operation_id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 12:52:34 -04:00
										 |  |  |     assert "Operation" in resp | 
					
						
							| 
									
										
										
										
											2022-02-15 13:33:47 -01:00
										 |  |  |     operation = resp["Operation"] | 
					
						
							| 
									
										
										
										
											2023-08-07 12:52:34 -04:00
										 |  |  |     assert operation["Id"] == operation_id | 
					
						
							|  |  |  |     assert operation["Type"] == "UPDATE_SERVICE" | 
					
						
							|  |  |  |     assert operation["Status"] == "SUCCESS" | 
					
						
							|  |  |  |     assert "CreateDate" in operation | 
					
						
							|  |  |  |     assert "UpdateDate" in operation | 
					
						
							|  |  |  |     assert "Targets" in operation | 
					
						
							| 
									
										
										
										
											2022-02-15 13:33:47 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_servicediscovery | 
					
						
							|  |  |  | def test_get_unknown_operation(): | 
					
						
							|  |  |  |     client = boto3.client("servicediscovery", region_name="eu-west-1") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(ClientError) as exc: | 
					
						
							|  |  |  |         client.get_operation(OperationId="unknown") | 
					
						
							|  |  |  |     err = exc.value.response["Error"] | 
					
						
							| 
									
										
										
										
											2023-08-07 12:52:34 -04:00
										 |  |  |     assert err["Code"] == "OperationNotFound" |