From 970e7ce191729f0df59ce14d4912fbdc4e72f6f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20Andr=C3=A9?= <92062874+matheusandre1@users.noreply.github.com> Date: Wed, 2 Dec 2026 12:08:47 -0300 Subject: [PATCH] test(ec2): fold the remaining mock-mode Ec2Service copies into mockModeService() (#9988) --- .../aws/services/ec2/Ec2ServiceTest.java | 235 ++++++++---------- 1 file changed, 104 insertions(+), 131 deletions(-) diff --git a/src/test/java/io/floci/aws/services/ec2/Ec2ServiceTest.java b/src/test/java/io/floci/aws/services/ec2/Ec2ServiceTest.java index a7dea3eafe..4857323546 100644 --- a/src/test/java/io/floci/aws/services/ec2/Ec2ServiceTest.java +++ b/src/test/java/io/floci/aws/services/ec2/Ec2ServiceTest.java @@ -554,9 +554,7 @@ class Ec2ServiceTest { @Test void aLaunchRefusedWhenItTakesCapacityLeavesNoVolumeBehind() { - Ec2Service service = spy(new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class), - mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), - new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory())); + Ec2Service service = spy(mockModeService()); // Another launch took the capacity between this launch's check and its claim on it. doThrow(new AwsException("InsufficientInstanceCapacity", "taken in the meantime", 400)) .when(service).acquireCapacity(anyString(), anyList()); @@ -568,9 +566,7 @@ class Ec2ServiceTest { @Test void aRollbackWhoseTerminationFailsKeepsEachVolumesDeleteOnTermination() { - Ec2Service service = spy(new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class), - mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), - new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory())); + Ec2Service service = spy(mockModeService()); Reservation launch = launchKeepingADataVolume(service); doThrow(new AwsException("InternalError", "termination failed", 500)) .when(service).terminateInstances(anyString(), anyList()); @@ -2093,9 +2089,7 @@ class Ec2ServiceTest { @Test void aContainerInstanceReportingItsBridgeAddressStillTakesAnElasticIpForItsLaunchAddress() { EmulatorConfig config = mockConfig(true); - Ec2Service service = new Ec2Service(config, mock(Ec2ContainerManager.class), - mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), - new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory()); + Ec2Service service = mockModeService(config); String region = "us-east-1"; Instance instance = service.runInstances(region, "ami-1234567890abcdef0", "t3.micro", 1, 1, null, List.of(), null, null, List.of(), null, null, null).getInstances().getFirst(); @@ -2744,9 +2738,7 @@ class Ec2ServiceTest { @Test void awaitContainerLaunchReportsTerminatedContainer() { - Ec2Service service = new Ec2Service(mockConfig(false), mock(Ec2ContainerManager.class), - mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), - new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory()); + Ec2Service service = mockModeService(mockConfig(false)); Instance instance = new Instance(); instance.setInstanceId("i-terminated"); instance.setState(InstanceState.terminated()); @@ -4038,7 +4030,7 @@ class Ec2ServiceTest { @ParameterizedTest @ValueSource(strings = {"create", "modify"}) void launchTemplateMutationByNameCannotReplaceANewTemplate(String operation) throws Exception { - Ec2Service service = sgService(); + Ec2Service service = mockModeService(); String name = "reused-template-name"; LaunchTemplate original = service.createLaunchTemplate( "us-east-1", name, new LaunchTemplateData(), List.of()); @@ -4081,7 +4073,7 @@ class Ec2ServiceTest { @ParameterizedTest @ValueSource(strings = {"create", "modify"}) void launchTemplateMutationStillWorksWhileTemplateExists(String operation) { - Ec2Service service = sgService(); + Ec2Service service = mockModeService(); LaunchTemplate template = service.createLaunchTemplate( "us-east-1", "existing-template", new LaunchTemplateData(), List.of()); String id = template.getLaunchTemplateId(); @@ -7633,7 +7625,7 @@ class Ec2ServiceTest { @Test void deletingAnIpv6RuleRevokesThatRangeAndNotAnotherOnTheSamePort() { - Ec2Service service = sgService(); + Ec2Service service = mockModeService(); String groupId = service.createSecurityGroup("us-east-1", "web", "web", null).getGroupId(); String first = onlyRuleId(service.authorizeSecurityGroupIngress("us-east-1", groupId, List.of(ipv6Permission("2001:db8:1::/64")))); @@ -7654,7 +7646,7 @@ class Ec2ServiceTest { @Test void deletingAPeerGroupRuleRevokesThatPeerAndNotAnotherOnTheSamePort() { - Ec2Service service = sgService(); + Ec2Service service = mockModeService(); String groupId = service.createSecurityGroup("us-east-1", "web", "web", null).getGroupId(); String first = onlyRuleId(service.authorizeSecurityGroupIngress("us-east-1", groupId, List.of(peerPermission("sg-peer-a")))); @@ -7673,7 +7665,7 @@ class Ec2ServiceTest { @Test void ruleRecordsCarryTheirPeerIdentity() { - Ec2Service service = sgService(); + Ec2Service service = mockModeService(); String groupId = service.createSecurityGroup("us-east-1", "web", "web", null).getGroupId(); service.authorizeSecurityGroupIngress("us-east-1", groupId, List.of(peerPermission("sg-peer-a"))); service.authorizeSecurityGroupIngress("us-east-1", groupId, List.of(ipv6Permission("2001:db8:1::/64"))); @@ -7722,7 +7714,7 @@ class Ec2ServiceTest { @ParameterizedTest @CsvSource({"false, false", "false, true", "true, false", "true, true"}) void revokingAStoredPermissionKeepsLaterSources(boolean egress, boolean repeatedRemoval) { - Ec2Service service = sgService(); + Ec2Service service = mockModeService(); String groupId = service.createSecurityGroup("us-east-1", "web", "web", null).getGroupId(); IpPermission retired = basePermission(); retired.getIpRanges().add(new IpRange("192.0.2.1/32", null)); @@ -7793,27 +7785,10 @@ class Ec2ServiceTest { return perm; } - private static Ec2Service sgService() { - return new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class), - mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), - mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory()); - } - // ========================================================================= // Managed prefix lists // ========================================================================= - private static Ec2Service prefixListService() { - return prefixListService(mockConfig(true)); - } - - private static Ec2Service prefixListService(EmulatorConfig config) { - return new Ec2Service(config, mock(Ec2ContainerManager.class), - mock(Ec2PortForwardManager.class), - mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(), - new InMemoryStorageFactory()); - } - @Test void configuredAwsManagedPrefixListUsesExistingRegionalReadOnlyApis(@TempDir Path temp) throws IOException { Path catalog = temp.resolve("prefix-lists.json"); @@ -7824,7 +7799,7 @@ class Ec2ServiceTest { EmulatorConfig config = mockConfig(true); when(config.services().ec2().enabled()).thenReturn(true); when(config.services().ec2().managedPrefixListCatalogPath()).thenReturn(Optional.of(catalog.toString())); - Ec2Service service = prefixListService(config); + Ec2Service service = mockModeService(config); String id = "pl-0123456789abcdef0"; ManagedPrefixList managed = service.describeManagedPrefixLists("eu-west-1", List.of(id), Map.of()).getFirst(); @@ -7877,7 +7852,7 @@ class Ec2ServiceTest { when(config.services().ec2().enabled()).thenReturn(true); when(config.services().ec2().managedPrefixListCatalogPath()).thenReturn(Optional.of(catalog.toString())); - IllegalStateException error = assertThrows(IllegalStateException.class, () -> prefixListService(config)); + IllegalStateException error = assertThrows(IllegalStateException.class, () -> mockModeService(config)); assertTrue(error.getMessage().contains(catalog.toString())); } @@ -7895,7 +7870,7 @@ class Ec2ServiceTest { when(config.services().ec2().enabled()).thenReturn(true); when(config.services().ec2().managedPrefixListCatalogPath()).thenReturn(Optional.of(catalog.toString())); - Ec2Service service = assertDoesNotThrow(() -> prefixListService(config)); + Ec2Service service = assertDoesNotThrow(() -> mockModeService(config)); List cloudFront = service.describeManagedPrefixLists("eu-west-1", List.of("pl-3b927c52"), Map.of()); @@ -7918,7 +7893,7 @@ class Ec2ServiceTest { Path catalog = temp.resolve("missing.json"); when(config.services().ec2().managedPrefixListCatalogPath()).thenReturn(Optional.of(catalog.toString())); - assertThrows(IllegalStateException.class, () -> prefixListService(config)); + assertThrows(IllegalStateException.class, () -> mockModeService(config)); } @ParameterizedTest @@ -7933,7 +7908,7 @@ class Ec2ServiceTest { when(config.services().ec2().enabled()).thenReturn(true); when(config.services().ec2().managedPrefixListCatalogPath()).thenReturn(Optional.of(catalog.toString())); - assertThrows(IllegalStateException.class, () -> prefixListService(config)); + assertThrows(IllegalStateException.class, () -> mockModeService(config)); } @Test @@ -7943,7 +7918,7 @@ class Ec2ServiceTest { when(config.services().ec2().managedPrefixListCatalogPath()) .thenReturn(Optional.of(temp.resolve("missing.json").toString())); - assertDoesNotThrow(() -> prefixListService(config)); + assertDoesNotThrow(() -> mockModeService(config)); } @ParameterizedTest @@ -8011,7 +7986,7 @@ class Ec2ServiceTest { @Test void createManagedPrefixListStoresEntriesAtVersionOne() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); ManagedPrefixList list = service.createManagedPrefixList("us-east-1", "corp", "IPv4", 5, List.of(new PrefixListEntry("10.0.0.0/8", "corporate")), List.of()); @@ -8029,7 +8004,7 @@ class Ec2ServiceTest { @ParameterizedTest @ValueSource(strings = {"192.0.2.19/24", "192.0.2.0/33", "example.com/24", "192.0.2.0"}) void createManagedPrefixListRejectsInvalidIpv4BeforeAllocation(String cidr) { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); AwsException error = assertThrows(AwsException.class, () -> service.createManagedPrefixList( "us-east-1", "rejected-cidr", "IPv4", 2, List.of(new PrefixListEntry("198.51.100.0/24", null), new PrefixListEntry(cidr, null)), @@ -8043,7 +8018,7 @@ class Ec2ServiceTest { @Test void rejectedPrefixListCidrPreservesEntriesVersionAndMetadata() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); ManagedPrefixList list = service.createManagedPrefixList("us-east-1", "original", "IPv4", 2, List.of(new PrefixListEntry("192.0.2.0/24", "original")), List.of()); AwsException error = assertThrows(AwsException.class, () -> service.modifyManagedPrefixList( @@ -8062,7 +8037,7 @@ class Ec2ServiceTest { @ParameterizedTest @ValueSource(strings = {"0.0.0.0/0", "192.0.2.0/24", "192.0.2.19/32"}) void createManagedPrefixListPreservesCanonicalIpv4(String cidr) { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); ManagedPrefixList list = service.createManagedPrefixList("us-east-1", "canonical", "IPv4", 1, List.of(new PrefixListEntry(cidr, null)), List.of()); assertEquals(cidr, list.currentEntries().getFirst().getCidr()); @@ -8070,7 +8045,7 @@ class Ec2ServiceTest { @Test void createManagedPrefixListPreservesAlternateIpv6Spelling() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String cidr = "2001:DB8:0000:0000:0000:0000:0000:0000/32"; ManagedPrefixList list = service.createManagedPrefixList("us-east-1", "ipv6", "IPv6", 1, List.of(new PrefixListEntry(cidr, null)), List.of()); @@ -8079,7 +8054,7 @@ class Ec2ServiceTest { @Test void createManagedPrefixListRejectsMoreEntriesThanMaxEntries() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); AwsException error = assertThrows(AwsException.class, () -> service.createManagedPrefixList( "us-east-1", "corp", "IPv4", 1, @@ -8090,7 +8065,7 @@ class Ec2ServiceTest { @Test void createManagedPrefixListRejectsCidrOfTheWrongAddressFamily() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); AwsException error = assertThrows(AwsException.class, () -> service.createManagedPrefixList( "us-east-1", "corp", "IPv4", 5, @@ -8100,7 +8075,7 @@ class Ec2ServiceTest { @Test void describeManagedPrefixListsIncludesAwsManagedAndIsRegionScoped() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); service.createManagedPrefixList("us-east-1", "corp", "IPv4", 5, List.of(), List.of()); List east = service.describeManagedPrefixLists("us-east-1", List.of(), Map.of()); @@ -8117,7 +8092,7 @@ class Ec2ServiceTest { @Test void createManagedPrefixListAcceptsIpv6Entries() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); ManagedPrefixList list = service.createManagedPrefixList("us-east-1", "corp-v6", "IPv6", 5, List.of(new PrefixListEntry("2001:db8::/32", "lab")), List.of()); @@ -8137,7 +8112,7 @@ class Ec2ServiceTest { @Test void managedPrefixListLookupsRejectAMissingId() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); for (String missing : new String[] {null, " "}) { assertEquals("MissingParameter", assertThrows(AwsException.class, () -> @@ -8157,7 +8132,7 @@ class Ec2ServiceTest { */ @Test void createManagedPrefixListRejectsNamesReservedByAws() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); for (String reserved : new String[] {"com.amazonaws.probe", "com.amazon.probe", "com.aws.probe"}) { AwsException error = assertThrows(AwsException.class, () -> service.createManagedPrefixList( @@ -8178,7 +8153,7 @@ class Ec2ServiceTest { */ @Test void renamingToAReservedNameIsRejected() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); ManagedPrefixList list = service.createManagedPrefixList("us-east-1", "corp", "IPv4", 5, List.of(), List.of()); @@ -8197,7 +8172,7 @@ class Ec2ServiceTest { @Test void describeManagedPrefixListsFiltersByName() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); service.createManagedPrefixList("us-east-1", "corp", "IPv4", 5, List.of(), List.of()); List found = service.describeManagedPrefixLists("us-east-1", List.of(), @@ -8209,7 +8184,7 @@ class Ec2ServiceTest { @Test void describeManagedPrefixListsRejectsUnknownId() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); AwsException error = assertThrows(AwsException.class, () -> service.describeManagedPrefixLists("us-east-1", List.of("pl-missing"), Map.of())); @@ -8218,7 +8193,7 @@ class Ec2ServiceTest { @Test void modifyBumpsVersionAndKeepsEarlierVersionsRetrievable() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); ManagedPrefixList created = service.createManagedPrefixList("us-east-1", "corp", "IPv4", 5, List.of(new PrefixListEntry("10.0.0.0/8", null)), List.of()); @@ -8233,7 +8208,7 @@ class Ec2ServiceTest { @Test void modifyAppliesRemovalsBeforeAdditionsSoADescriptionCanBeReplaced() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); ManagedPrefixList created = service.createManagedPrefixList("us-east-1", "corp", "IPv4", 5, List.of(new PrefixListEntry("10.0.0.0/8", "old")), List.of()); @@ -8248,7 +8223,7 @@ class Ec2ServiceTest { @Test void renamingDoesNotCreateANewVersion() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); ManagedPrefixList created = service.createManagedPrefixList("us-east-1", "corp", "IPv4", 5, List.of(new PrefixListEntry("10.0.0.0/8", null)), List.of()); @@ -8261,7 +8236,7 @@ class Ec2ServiceTest { @Test void modifyWithStaleCurrentVersionIsRejected() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); ManagedPrefixList created = service.createManagedPrefixList("us-east-1", "corp", "IPv4", 5, List.of(new PrefixListEntry("10.0.0.0/8", null)), List.of()); service.modifyManagedPrefixList("us-east-1", created.getPrefixListId(), null, null, null, @@ -8276,7 +8251,7 @@ class Ec2ServiceTest { @ParameterizedTest @ValueSource(strings = {"pl-63a5400a", "pl-3b927c52"}) void awsManagedListsCannotBeModifiedOrDeleted(String prefixListId) { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); AwsException modifyError = assertThrows(AwsException.class, () -> service.modifyManagedPrefixList("us-east-1", prefixListId, null, "hijacked", null, @@ -8290,7 +8265,7 @@ class Ec2ServiceTest { @Test void cloudFrontOriginFacingListExistsInEveryCommercialRegion() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); Map> byName = Map.of("prefix-list-name", List.of("com.amazonaws.global.cloudfront.origin-facing")); @@ -8315,7 +8290,7 @@ class Ec2ServiceTest { @Test void cloudFrontOriginFacingListIsAbsentOutsideTheCommercialPartition() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); for (String region : List.of("cn-north-1", "us-gov-west-1")) { assertTrue(service.describeManagedPrefixLists(region, List.of(), Map.of()).stream() @@ -8335,7 +8310,7 @@ class Ec2ServiceTest { "us-east-1", "aws"); for (Map.Entry deployment : expectedPartitions.entrySet()) { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); service.regionResolver = new RegionResolver(deployment.getKey(), "000000000000"); List lists = service.describeManagedPrefixLists(unknown, List.of(), Map.of()); @@ -8349,7 +8324,7 @@ class Ec2ServiceTest { @Test void deleteRemovesTheListFromDescribe() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); ManagedPrefixList created = service.createManagedPrefixList("us-east-1", "corp", "IPv4", 5, List.of(), List.of()); @@ -8362,7 +8337,7 @@ class Ec2ServiceTest { @Test void legacyDescribePrefixListsProjectsTheSameAwsManagedData() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); List legacy = service.describePrefixLists("us-east-1", List.of(), Map.of("prefix-list-name", List.of("com.amazonaws.us-east-1.s3"))); @@ -8374,7 +8349,7 @@ class Ec2ServiceTest { @Test void modifyRejectsANonPositiveMaxEntries() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); ManagedPrefixList created = service.createManagedPrefixList("us-east-1", "corp", "IPv4", 5, List.of(), List.of()); @@ -8389,7 +8364,7 @@ class Ec2ServiceTest { @Test void createTagsOnAPrefixListIsVisibleToDescribeAndTagFilters() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); ManagedPrefixList created = service.createManagedPrefixList("us-east-1", "corp", "IPv4", 5, List.of(), List.of()); @@ -8427,7 +8402,7 @@ class Ec2ServiceTest { @Test void authorizeIngressFromAPrefixListCreatesARuleCarryingIt() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); ManagedPrefixList list = service.createManagedPrefixList("us-east-1", "corp", "IPv4", 5, List.of(new PrefixListEntry("10.0.0.0/8", null)), List.of()); String groupId = service.createSecurityGroup("us-east-1", "db", "db", null).getGroupId(); @@ -8447,7 +8422,7 @@ class Ec2ServiceTest { @Test void authorizeAgainstAnUnknownPrefixListIsRejected() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String groupId = service.createSecurityGroup("us-east-1", "db", "db", null).getGroupId(); IpPermission perm = tcpPermission(5432); @@ -8465,7 +8440,7 @@ class Ec2ServiceTest { /** AWS emits one rule per source, so a permission naming both expands to two. */ @Test void aPermissionNamingBothACidrAndAPrefixListYieldsARuleForEach() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); ManagedPrefixList list = service.createManagedPrefixList("us-east-1", "corp", "IPv4", 5, List.of(), List.of()); String groupId = service.createSecurityGroup("us-east-1", "db", "db", null).getGroupId(); @@ -8486,7 +8461,7 @@ class Ec2ServiceTest { */ @Test void anUnknownPrefixListLeavesNoPartialRuleFromTheSamePermission() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String groupId = service.createSecurityGroup("us-east-1", "db", "db", null).getGroupId(); IpPermission perm = tcpPermission(5432); @@ -8503,7 +8478,7 @@ class Ec2ServiceTest { /** A later bad permission must not leave an earlier good one applied either. */ @Test void anUnknownPrefixListInASecondPermissionLeavesTheFirstUnapplied() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String groupId = service.createSecurityGroup("us-east-1", "db", "db", null).getGroupId(); IpPermission good = tcpPermission(443); @@ -8523,7 +8498,7 @@ class Ec2ServiceTest { */ @Test void revokingAPrefixListSourceLeavesACidrOnTheSameTupleAlone() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); ManagedPrefixList list = service.createManagedPrefixList("us-east-1", "corp", "IPv4", 5, List.of(), List.of()); String groupId = service.createSecurityGroup("us-east-1", "db", "db", null).getGroupId(); @@ -8544,7 +8519,7 @@ class Ec2ServiceTest { @Test void anEgressRuleCanAlsoComeFromAPrefixList() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); ManagedPrefixList list = service.createManagedPrefixList("us-east-1", "corp", "IPv4", 5, List.of(), List.of()); String groupId = service.createSecurityGroup("us-east-1", "db", "db", null).getGroupId(); @@ -8565,7 +8540,7 @@ class Ec2ServiceTest { */ @Test void revokingAGroupSourceNamedByNameOnlyStillMatchesTheStoredReference() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String sourceId = service.createSecurityGroup("us-east-1", "app", "app", null).getGroupId(); String targetId = service.createSecurityGroup("us-east-1", "db", "db", null).getGroupId(); @@ -8593,7 +8568,7 @@ class Ec2ServiceTest { */ @Test void tagsOnASecurityGroupRuleAreTypedAsSecurityGroupRule() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String groupId = service.createSecurityGroup("us-east-1", "db", "db", null).getGroupId(); IpPermission perm = new IpPermission(); perm.setIpProtocol("tcp"); @@ -8630,7 +8605,7 @@ class Ec2ServiceTest { */ @Test void createTransitGatewayAppliesTheDefaultsAwsApplies() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); TransitGateway gateway = service.createTransitGateway("us-east-1", "hub", null, List.of()); @@ -8659,7 +8634,7 @@ class Ec2ServiceTest { */ @Test void createTransitGatewayMintsTheDefaultRouteTableAndReportsItsId() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); TransitGatewayOptions options = service.createTransitGateway("us-east-1", null, null, List.of()).getOptions(); @@ -8672,7 +8647,7 @@ class Ec2ServiceTest { @Test void aGatewayThatOptsOutOfBothDefaultsGetsNoRouteTable() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); TransitGatewayOptions requested = new TransitGatewayOptions(); requested.setDefaultRouteTableAssociation("disable"); requested.setDefaultRouteTablePropagation("disable"); @@ -8686,7 +8661,7 @@ class Ec2ServiceTest { @Test void requestedOptionsOverrideTheDefaults() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); TransitGatewayOptions requested = new TransitGatewayOptions(); requested.setAmazonSideAsn(65001L); requested.setDnsSupport("disable"); @@ -8704,7 +8679,7 @@ class Ec2ServiceTest { @Test void describeTransitGatewaysFiltersAndRejectsUnknownIds() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); TransitGateway gateway = service.createTransitGateway("us-east-1", "hub", null, List.of(new Tag("env", "prod"))); service.createTransitGateway("us-east-1", "spoke", null, List.of()); @@ -8728,7 +8703,7 @@ class Ec2ServiceTest { @Test void modifyTransitGatewayUpdatesDescriptionOptionsAndCidrBlocks() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String id = service.createTransitGateway("us-east-1", "before", null, List.of()).getTransitGatewayId(); TransitGatewayOptions changes = new TransitGatewayOptions(); changes.setDnsSupport("disable"); @@ -8756,7 +8731,7 @@ class Ec2ServiceTest { */ @Test void enablingADefaultRouteTableOptionRequiresAnExistingRouteTable() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); TransitGatewayOptions createdWithout = new TransitGatewayOptions(); createdWithout.setDefaultRouteTableAssociation("disable"); createdWithout.setDefaultRouteTablePropagation("disable"); @@ -8841,7 +8816,7 @@ class Ec2ServiceTest { */ @Test void aRouteTableIdOnItsOwnFollowsTheStoredFlag() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); TransitGateway gateway = service.createTransitGateway("us-east-1", "hub", null, List.of()); String id = gateway.getTransitGatewayId(); String routeTableId = gateway.getOptions().getAssociationDefaultRouteTableId(); @@ -8885,7 +8860,7 @@ class Ec2ServiceTest { /** Removals apply before additions, so a CIDR added and removed in one call survives. */ @Test void aCidrBlockAddedAndRemovedInOneCallSurvives() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String id = service.createTransitGateway("us-east-1", null, null, List.of()).getTransitGatewayId(); TransitGatewayOptions after = service.modifyTransitGateway("us-east-1", id, null, null, @@ -8897,7 +8872,7 @@ class Ec2ServiceTest { /** Repointing the default route table at the gateway's own table is accepted. */ @Test void aDefaultRouteTableOptionCanBeSetWhenItsRouteTableIsNamed() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); TransitGateway gateway = service.createTransitGateway("us-east-1", null, null, List.of()); String routeTableId = gateway.getOptions().getAssociationDefaultRouteTableId(); @@ -8972,7 +8947,7 @@ class Ec2ServiceTest { */ @Test void tagsChangedAfterCreationAreVisibleOnDescribe() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String id = service.createTransitGateway("us-east-1", "hub", null, List.of(new Tag("Name", "hub"))).getTransitGatewayId(); @@ -8993,7 +8968,7 @@ class Ec2ServiceTest { @Test void tagsOnATransitGatewayAreTypedAsTransitGateway() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String id = service.createTransitGateway("us-east-1", "hub", null, List.of(new Tag("env", "prod"))).getTransitGatewayId(); @@ -9023,7 +8998,7 @@ class Ec2ServiceTest { */ @Test void createVpcAttachmentAppliesTheAttachmentsOwnDefaults() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = attachmentFixture(service); TransitGatewayVpcAttachment attachment = service.createTransitGatewayVpcAttachment( @@ -9042,7 +9017,7 @@ class Ec2ServiceTest { /** The association follows the gateway's own default-association setting. */ @Test void anAttachmentAssociatesOnlyWhenTheGatewayAsksForIt() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = attachmentFixture(service); TransitGateway gateway = service.describeTransitGateways("us-east-1", List.of(fixture[0]), Map.of()) .getFirst(); @@ -9078,7 +9053,7 @@ class Ec2ServiceTest { // Kept deliberately small: without the lock the race shows on the first trial, and this // suite has thread-timing-sensitive tests elsewhere that a heavier harness disturbs. for (int trial = 0; trial < 10; trial++) { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = attachmentFixture(service); int threads = 6; CountDownLatch start = new CountDownLatch(1); @@ -9123,7 +9098,7 @@ class Ec2ServiceTest { record Race(String name, boolean deleteGateway) {} for (Race race : List.of(new Race("gateway", true), new Race("vpc", false))) { for (int trial = 0; trial < 10; trial++) { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = attachmentFixture(service); CountDownLatch start = new CountDownLatch(1); ExecutorService pool = @@ -9182,7 +9157,7 @@ class Ec2ServiceTest { void taggingCannotResurrectSomethingBeingDeleted() throws Exception { for (boolean attachment : List.of(true, false)) { for (int trial = 0; trial < 10; trial++) { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = attachmentFixture(service); String attachmentId = service.createTransitGatewayVpcAttachment("us-east-1", fixture[0], fixture[1], List.of(fixture[2]), null, List.of()).getTransitGatewayAttachmentId(); @@ -9241,7 +9216,7 @@ class Ec2ServiceTest { */ @Test void anAttachedVpcOrSubnetCannotBeDeleted() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = attachmentFixture(service); String attachmentId = service.createTransitGatewayVpcAttachment("us-east-1", fixture[0], fixture[1], List.of(fixture[2]), null, List.of()).getTransitGatewayAttachmentId(); @@ -9266,7 +9241,7 @@ class Ec2ServiceTest { */ @Test void aDeletedVpcAttachmentStaysDescribableButReleasesEverything() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = attachmentFixture(service); TransitGatewayVpcAttachment created = service.createTransitGatewayVpcAttachment("us-east-1", fixture[0], fixture[1], List.of(fixture[2]), null, List.of()); @@ -9303,7 +9278,7 @@ class Ec2ServiceTest { @Test void aDeletedVpcAttachmentDescribesWithoutTags() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = attachmentFixture(service); String attachmentId = service.createTransitGatewayVpcAttachment("us-east-1", fixture[0], fixture[1], List.of(fixture[2]), null, List.of(new Tag("Name", "gone"))).getTransitGatewayAttachmentId(); @@ -9314,7 +9289,7 @@ class Ec2ServiceTest { @Test void aDeletedVpcAttachmentDoesNotBlockReattaching() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = attachmentFixture(service); String attachmentId = service.createTransitGatewayVpcAttachment("us-east-1", fixture[0], fixture[1], List.of(fixture[2]), null, List.of()).getTransitGatewayAttachmentId(); @@ -9325,7 +9300,7 @@ class Ec2ServiceTest { @Test void aRepeatPeeringDeleteDoesNotReturnTheDeletedTags() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String requester = service.createTransitGateway("us-east-1", "a", null, List.of()).getTransitGatewayId(); String accepter = service.createTransitGateway("us-east-1", "b", null, List.of()).getTransitGatewayId(); String id = service.createTransitGatewayPeeringAttachment("us-east-1", requester, accepter, @@ -9338,7 +9313,7 @@ class Ec2ServiceTest { @Test void peeringAttachmentResponsesAreCopiesOfTheStoredRecord() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String requester = service.createTransitGateway("us-east-1", "a", null, List.of()).getTransitGatewayId(); String accepter = service.createTransitGateway("us-east-1", "b", null, List.of()).getTransitGatewayId(); TransitGatewayPeeringAttachment created = service.createTransitGatewayPeeringAttachment("us-east-1", @@ -9364,7 +9339,7 @@ class Ec2ServiceTest { @Test void aDeletedPeeringAttachmentStaysDescribableButHoldsNothing() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String requester = service.createTransitGateway("us-east-1", "a", null, List.of()).getTransitGatewayId(); String accepter = service.createTransitGateway("us-east-1", "b", null, List.of()).getTransitGatewayId(); String id = service.createTransitGatewayPeeringAttachment("us-east-1", requester, accepter, @@ -9391,7 +9366,7 @@ class Ec2ServiceTest { */ @Test void anAttachmentCannotBeCreatedWithoutSubnets() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = attachmentFixture(service); assertEquals("MissingParameter", assertThrows(AwsException.class, @@ -9406,7 +9381,7 @@ class Ec2ServiceTest { */ @Test void anExistingAssociationSurvivesTheGatewayDisablingItsDefault() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = attachmentFixture(service); TransitGatewayVpcAttachment attachment = service.createTransitGatewayVpcAttachment( "us-east-1", fixture[0], fixture[1], List.of(fixture[2]), null, List.of()); @@ -9431,7 +9406,7 @@ class Ec2ServiceTest { */ @Test void aMalformedAttachmentIdIsRejectedBeforeTheLookup() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); for (String malformed : List.of("tgw-attach-nope", "vpc-0123456789abcdef0")) { assertEquals("InvalidTransitGatewayAttachmentID.Malformed", assertThrows(AwsException.class, @@ -9453,14 +9428,14 @@ class Ec2ServiceTest { */ @Test void describeTransitGatewayConnectsReturnsEmptyWhenNoneExist() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); assertDoesNotThrow(() -> service.describeTransitGatewayConnects("us-east-1", List.of(), Map.of())); } @Test void describeTransitGatewayConnectsRejectsAMalformedOrUnknownId() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); assertEquals("InvalidTransitGatewayAttachmentID.Malformed", assertThrows(AwsException.class, () -> service.describeTransitGatewayConnects("us-east-1", List.of("tgw-connect-nope"), Map.of())) @@ -9473,7 +9448,7 @@ class Ec2ServiceTest { /** The gateway's owner is its own field, sourced from the gateway rather than from the VPC. */ @Test void anAttachmentRecordsBothOwnersSeparately() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = attachmentFixture(service); TransitGatewayVpcAttachment attachment = service.createTransitGatewayVpcAttachment( @@ -9487,7 +9462,7 @@ class Ec2ServiceTest { @Test void aVpcCanOnlyBeAttachedToAGatewayOnce() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = attachmentFixture(service); service.createTransitGatewayVpcAttachment("us-east-1", fixture[0], fixture[1], List.of(fixture[2]), null, List.of()); @@ -9500,7 +9475,7 @@ class Ec2ServiceTest { /** Verified live: a subnet from another VPC is reported missing rather than mismatched. */ @Test void subnetsMustBelongToTheAttachedVpcAndBeOnePerZone() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = attachmentFixture(service); String otherVpc = service.createVpc("us-east-1", "10.96.0.0/16", false).getVpcId(); String foreignSubnet = service.createSubnet("us-east-1", otherVpc, "10.96.1.0/24", "us-east-1a") @@ -9519,7 +9494,7 @@ class Ec2ServiceTest { @Test void modifyVpcAttachmentAddsAndRemovesSubnetsAndOptions() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = attachmentFixture(service); String attachmentId = service.createTransitGatewayVpcAttachment("us-east-1", fixture[0], fixture[1], List.of(fixture[2]), null, List.of()).getTransitGatewayAttachmentId(); @@ -9551,7 +9526,7 @@ class Ec2ServiceTest { /** Verified live: the gateway refuses to go while anything is still attached to it. */ @Test void aGatewayWithAnAttachmentCannotBeDeleted() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = attachmentFixture(service); String attachmentId = service.createTransitGatewayVpcAttachment("us-east-1", fixture[0], fixture[1], List.of(fixture[2]), null, List.of()).getTransitGatewayAttachmentId(); @@ -9568,7 +9543,7 @@ class Ec2ServiceTest { @Test void tagsOnAnAttachmentAreTypedAsTransitGatewayAttachment() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = attachmentFixture(service); String attachmentId = service.createTransitGatewayVpcAttachment("us-east-1", fixture[0], fixture[1], List.of(fixture[2]), null, List.of(new Tag("Name", "hub-attach"))) @@ -9599,7 +9574,7 @@ class Ec2ServiceTest { @Test void aRequestedRouteTableIsNeitherDefault() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = attachmentFixture(service); TransitGatewayRouteTable routeTable = service.createTransitGatewayRouteTable("us-east-1", @@ -9616,7 +9591,7 @@ class Ec2ServiceTest { /** Verified live: an attachment associates with exactly one route table. */ @Test void anAttachmentAssociatesWithOneRouteTableAtATime() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = routeTableFixture(service); AwsException already = assertThrows(AwsException.class, () -> service @@ -9640,7 +9615,7 @@ class Ec2ServiceTest { /** Route-table operations keep the generic attachment wording for an unknown id. */ @Test void routeTableOperationsNameAnUnknownAttachmentGenerically() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = routeTableFixture(service); String unknown = "tgw-attach-0123456789abcdef0"; @@ -9654,7 +9629,7 @@ class Ec2ServiceTest { /** Verified live: propagation reports the settled state at once, and refuses a duplicate. */ @Test void propagationIsEnabledAtOnceAndOnlyOnce() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = routeTableFixture(service); TransitGatewayRouteTablePropagation propagation = service @@ -9679,7 +9654,7 @@ class Ec2ServiceTest { */ @Test void propagationProducesRoutesForTheAttachedVpcsCidr() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = routeTableFixture(service); service.enableTransitGatewayRouteTablePropagation("us-east-1", fixture[4], fixture[3]); @@ -9699,7 +9674,7 @@ class Ec2ServiceTest { /** A blackhole is a state of a static route, not a type, and carries no attachment. */ @Test void staticAndBlackholeRoutesDifferByStateNotType() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = routeTableFixture(service); TransitGatewayRoute viaAttachment = service.createTransitGatewayRoute("us-east-1", fixture[4], @@ -9733,7 +9708,7 @@ class Ec2ServiceTest { */ @Test void aRouteTableInUseCannotBeDeleted() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = routeTableFixture(service); TransitGateway gateway = service.describeTransitGateways("us-east-1", List.of(fixture[0]), Map.of()) .getFirst(); @@ -9763,7 +9738,7 @@ class Ec2ServiceTest { /** Verified live, casing included: NotFound spells it ID, Malformed spells it Id. */ @Test void routeTableIdErrorsMatchTheLiveApiIncludingCasing() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); assertEquals("InvalidRouteTableID.NotFound", assertThrows(AwsException.class, () -> service.describeTransitGatewayRouteTables("us-east-1", @@ -9780,7 +9755,7 @@ class Ec2ServiceTest { */ @Test void deletingAnAttachmentClearsPropagationsAndBlackholesItsRoutes() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = routeTableFixture(service); service.enableTransitGatewayRouteTablePropagation("us-east-1", fixture[4], fixture[3]); service.createTransitGatewayRoute("us-east-1", fixture[4], "10.99.0.0/16", fixture[3], false); @@ -9799,7 +9774,7 @@ class Ec2ServiceTest { /** A gateway's route tables take their propagations, routes and tags with them. */ @Test void deletingAGatewayLeavesNothingBehindItsRouteTables() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = routeTableFixture(service); service.enableTransitGatewayRouteTablePropagation("us-east-1", fixture[4], fixture[3]); service.createTransitGatewayRoute("us-east-1", fixture[4], "10.99.0.0/16", null, true); @@ -9822,7 +9797,7 @@ class Ec2ServiceTest { @Test void modifyingAGatewayCannotResurrectADeletedRouteTable() throws Exception { for (int trial = 0; trial < 10; trial++) { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = attachmentFixture(service); String spare = service.createTransitGatewayRouteTable("us-east-1", fixture[0], List.of()) .getTransitGatewayRouteTableId(); @@ -9871,7 +9846,7 @@ class Ec2ServiceTest { */ @Test void aRouteTableOnlyReachesItsOwnGatewaysAttachments() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = routeTableFixture(service); String otherGateway = service.createTransitGateway("us-east-1", "other", null, List.of()) .getTransitGatewayId(); @@ -9902,7 +9877,7 @@ class Ec2ServiceTest { */ @Test void replacingARouteMovesItsTargetAndUpsertsWhenAbsent() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = routeTableFixture(service); service.createTransitGatewayRoute("us-east-1", fixture[4], "10.88.0.0/16", fixture[3], false); @@ -9949,7 +9924,7 @@ class Ec2ServiceTest { */ @Test void routeSearchFiltersMatchTheLiveApi() { - Ec2Service service = prefixListService(); + Ec2Service service = mockModeService(); String[] fixture = routeTableFixture(service); for (String cidr : List.of("10.0.0.0/8", "10.1.0.0/16", "10.1.1.0/24", "192.168.0.0/16")) { service.createTransitGatewayRoute("us-east-1", fixture[4], cidr, null, true); @@ -10519,7 +10494,7 @@ class Ec2ServiceTest { @Test void deletingOnlyReconstructedLegacyVersionPreservesItsData() { - Ec2Service service = sgService(); + Ec2Service service = mockModeService(); LaunchTemplateData data = new LaunchTemplateData(); data.setImageId("ami-legacy-latest"); LaunchTemplate template = service.createLaunchTemplate("us-east-1", "legacy-singleton", data, List.of()); @@ -10537,7 +10512,7 @@ class Ec2ServiceTest { @Test void templateResolutionAfterVersionDeletionReturnsNotFound() { - Ec2Service service = sgService(); + Ec2Service service = mockModeService(); LaunchTemplate template = service.createLaunchTemplate("us-east-1", "already-deleted-version", new LaunchTemplateData(), List.of()); service.createLaunchTemplateVersion("us-east-1", template.getLaunchTemplateId(), null, null, @@ -10551,7 +10526,7 @@ class Ec2ServiceTest { @Test void deletingOnlyDefaultVersionStillReturnsDefaultError() { - Ec2Service service = sgService(); + Ec2Service service = mockModeService(); LaunchTemplate template = service.createLaunchTemplate("us-east-1", "default-singleton", new LaunchTemplateData(), List.of()); Ec2Service.LaunchTemplateVersionDeletion result = service.deleteLaunchTemplateVersions( @@ -11418,9 +11393,7 @@ class Ec2ServiceTest { EmulatorConfig config = mockConfig(true); when(config.services().ec2().enabled()).thenReturn(false); when(config.defaultRegion()).thenReturn("us-east-1"); - Ec2Service service = spy(new Ec2Service(config, mock(Ec2ContainerManager.class), - mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), - new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory())); + Ec2Service service = spy(mockModeService(config)); service.seedDefaultRegionIfEnabled();