mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
The generated request resources give OpenSearch's domain type the aws:ResourceTag/ key, but no service answered the es namespace's stored tags, so a condition on a domain's tags saw none. OpenSearchService now answers it: storedTags reads the domain in the request's account, where the tag handlers read, in place under the key the ARN gives or the name a domain was stored under before its key named the Region, and matched on the whole ARN. AddTags and RemoveTags changed the domain's map in place with no lock; they now give it a new map one call at a time, and the tags field is volatile.
369 lines
17 KiB
Java
369 lines
17 KiB
Java
package io.floci.aws.services.opensearch;
|
|
|
|
import io.floci.aws.config.EmulatorConfig;
|
|
import io.floci.aws.core.common.AwsArnUtils;
|
|
import io.floci.aws.core.common.AwsException;
|
|
import io.floci.aws.core.common.RegionResolver;
|
|
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
|
import io.floci.aws.services.opensearch.model.ClusterConfig;
|
|
import io.floci.aws.services.opensearch.model.Domain;
|
|
import io.floci.aws.services.opensearch.model.VolumeOwnership;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.mockito.Mockito;
|
|
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.concurrent.CompletableFuture;
|
|
import java.util.concurrent.CountDownLatch;
|
|
import java.util.concurrent.ExecutionException;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
import static org.mockito.Mockito.atLeast;
|
|
import static org.mockito.Mockito.mock;
|
|
import static org.mockito.Mockito.verify;
|
|
import static org.mockito.Mockito.when;
|
|
|
|
/**
|
|
* A domain stays {@code Processing=true} until the readiness poller flips it. Mock mode and a
|
|
* Floci with no reachable Docker daemon report {@code Processing=false} from the first read.
|
|
*
|
|
* <p>Domains are scoped to an account and a region: the same name can exist in two regions,
|
|
* and describe, update, delete and list only ever see the caller's region.
|
|
*/
|
|
class OpenSearchServiceTest {
|
|
|
|
private static final String ACCOUNT = "000000000000";
|
|
private static final String US_EAST_1 = "us-east-1";
|
|
private static final String EU_WEST_1 = "eu-west-1";
|
|
|
|
private OpenSearchService service;
|
|
private AccountAwareStorageBackend<Domain> domainStore;
|
|
private OpenSearchDomainManager domainManager;
|
|
private EmulatorConfig.OpenSearchServiceConfig osConfig;
|
|
|
|
@BeforeEach
|
|
void setUp() {
|
|
domainManager = mock(OpenSearchDomainManager.class);
|
|
EmulatorConfig config = mock(EmulatorConfig.class);
|
|
EmulatorConfig.ServicesConfig servicesConfig = mock(EmulatorConfig.ServicesConfig.class);
|
|
osConfig = mock(EmulatorConfig.OpenSearchServiceConfig.class);
|
|
when(config.services()).thenReturn(servicesConfig);
|
|
when(servicesConfig.opensearch()).thenReturn(osConfig);
|
|
when(osConfig.mock()).thenReturn(false);
|
|
|
|
RegionResolver regionResolver = new RegionResolver(US_EAST_1, ACCOUNT);
|
|
domainStore = AccountAwareStorageBackend.inMemory(ACCOUNT);
|
|
service = new OpenSearchService(domainStore, config, regionResolver, domainManager);
|
|
}
|
|
|
|
@Test
|
|
void createDomainKeepsProcessingTrueWhileContainerStarts() {
|
|
when(domainManager.tryStartDomain(any())).thenReturn(true);
|
|
|
|
Domain domain = service.createDomain("docker-domain", "OpenSearch_2.11",
|
|
null, null, null, US_EAST_1);
|
|
|
|
assertTrue(domain.isProcessing(),
|
|
"the readiness poller flips Processing once the container answers");
|
|
assertTrue(service.describeDomain("docker-domain", US_EAST_1).isProcessing());
|
|
verify(domainManager).tryStartDomain(any());
|
|
}
|
|
|
|
/**
|
|
* Two concurrent creates of one name start one container: the second is refused as the name
|
|
* already exists, rather than starting a second container whose domain overwrites the first
|
|
* and leaves the first container running with nothing to stop it.
|
|
*/
|
|
@Test
|
|
void concurrentCreatesOfOneNameStartOneContainerAndRefuseTheSecond() throws Exception {
|
|
CountDownLatch firstStarting = new CountDownLatch(1);
|
|
CountDownLatch releaseFirst = new CountDownLatch(1);
|
|
CountDownLatch secondStarting = new CountDownLatch(1);
|
|
AtomicInteger starts = new AtomicInteger();
|
|
when(domainManager.tryStartDomain(any())).thenAnswer(invocation -> {
|
|
if (starts.incrementAndGet() == 1) {
|
|
firstStarting.countDown();
|
|
assertTrue(releaseFirst.await(10, TimeUnit.SECONDS), "the test never released the first start");
|
|
} else {
|
|
secondStarting.countDown();
|
|
}
|
|
return true;
|
|
});
|
|
|
|
CompletableFuture<Domain> first = CompletableFuture.supplyAsync(() -> service.createDomain(
|
|
"raced-domain", "OpenSearch_2.11", null, null, null, US_EAST_1));
|
|
assertTrue(firstStarting.await(10, TimeUnit.SECONDS), "the first create never started its container");
|
|
CompletableFuture<Domain> second = CompletableFuture.supplyAsync(() -> service.createDomain(
|
|
"raced-domain", "OpenSearch_2.11", null, null, null, US_EAST_1));
|
|
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10);
|
|
while (secondStarting.getCount() > 0 && !second.isDone() && System.nanoTime() < deadline) {
|
|
Thread.sleep(10);
|
|
}
|
|
releaseFirst.countDown();
|
|
|
|
first.get(10, TimeUnit.SECONDS);
|
|
ExecutionException refused = assertThrows(ExecutionException.class, () -> second.get(10, TimeUnit.SECONDS));
|
|
AwsException cause = assertInstanceOf(AwsException.class, refused.getCause());
|
|
assertEquals("ResourceAlreadyExistsException", cause.getErrorCode());
|
|
assertEquals(1, starts.get());
|
|
}
|
|
|
|
@Test
|
|
void createDomainWhoseContainerFailedToStartCanBeRetried() {
|
|
when(domainManager.tryStartDomain(any()))
|
|
.thenThrow(new IllegalStateException("container failed to start"))
|
|
.thenReturn(true);
|
|
|
|
assertThrows(IllegalStateException.class, () -> service.createDomain(
|
|
"retried-domain", "OpenSearch_2.11", null, null, null, US_EAST_1));
|
|
Domain domain = service.createDomain("retried-domain", "OpenSearch_2.11", null, null, null, US_EAST_1);
|
|
|
|
assertEquals("retried-domain", domain.getDomainName());
|
|
}
|
|
|
|
/**
|
|
* The volume name and the ownership marker are persisted at creation, so deletion
|
|
* acts on what was stamped instead of recomputing ownership from whatever
|
|
* configuration holds at delete time.
|
|
*/
|
|
@Test
|
|
void createDomainStampsManagedVolumeNameAndOwnership() {
|
|
when(domainManager.tryStartDomain(any())).thenReturn(false);
|
|
|
|
Domain domain = service.createDomain("stamped-domain", "OpenSearch_2.11",
|
|
null, null, null, US_EAST_1);
|
|
|
|
assertEquals(VolumeOwnership.FLOCI_MANAGED, domain.getVolumeOwnership());
|
|
assertTrue(domain.getDockerVolumeName().startsWith("floci-aws-opensearch-"),
|
|
"the stamped name carries the current managed prefix");
|
|
}
|
|
|
|
@Test
|
|
void createDomainWithoutDockerDaemonStillSucceedsAndReportsProcessingFalse() {
|
|
// tryStartDomain() returns false when no Docker daemon is reachable. The domain record is
|
|
// metadata, so the create still succeeds and the first describe already reports the
|
|
// terminal state.
|
|
when(domainManager.tryStartDomain(any())).thenReturn(false);
|
|
|
|
Domain domain = service.createDomain("no-docker-domain", "OpenSearch_2.11",
|
|
null, null, null, US_EAST_1);
|
|
|
|
assertFalse(domain.isProcessing());
|
|
assertEquals("no-docker-domain", service.describeDomain("no-docker-domain", US_EAST_1).getDomainName());
|
|
assertFalse(service.describeDomain("no-docker-domain", US_EAST_1).isProcessing());
|
|
}
|
|
|
|
@Test
|
|
void mockModeKeepsProcessingFalse() {
|
|
when(osConfig.mock()).thenReturn(true);
|
|
|
|
Domain domain = service.createDomain("mock-domain", "OpenSearch_2.11",
|
|
null, null, null, US_EAST_1);
|
|
|
|
assertFalse(domain.isProcessing());
|
|
verify(domainManager, Mockito.never()).tryStartDomain(any());
|
|
}
|
|
|
|
@Test
|
|
void readinessPollerContinuesAfterOneReadinessFailure() {
|
|
when(domainManager.tryStartDomain(any())).thenReturn(true);
|
|
when(domainManager.isReady(any()))
|
|
.thenThrow(new RuntimeException("temporary readiness failure"))
|
|
.thenReturn(true);
|
|
|
|
service.createDomain("ready-recovery", "OpenSearch_2.11",
|
|
null, null, null, US_EAST_1);
|
|
|
|
service.pollReadiness();
|
|
assertTrue(service.describeDomain("ready-recovery", US_EAST_1).isProcessing());
|
|
|
|
service.pollReadiness();
|
|
assertFalse(service.describeDomain("ready-recovery", US_EAST_1).isProcessing());
|
|
verify(domainManager, atLeast(2)).isReady(any());
|
|
}
|
|
|
|
@Test
|
|
void shutdownLeavesDomainContainersRunningWhenRetentionIsEnabled() {
|
|
when(domainManager.tryStartDomain(any())).thenReturn(true);
|
|
when(osConfig.keepRunningOnShutdown()).thenReturn(true);
|
|
service.createDomain("kept-domain", "OpenSearch_2.11", null, null, null, US_EAST_1);
|
|
|
|
service.shutdown();
|
|
|
|
verify(domainManager, Mockito.never()).stopDomain(any());
|
|
}
|
|
|
|
@Test
|
|
void sameNameInTwoRegionsCoexistsAndEachArnCarriesItsRegion() {
|
|
when(domainManager.tryStartDomain(any())).thenReturn(false);
|
|
|
|
Domain east = service.createDomain("dual-region", "OpenSearch_2.11", null, null, null, US_EAST_1);
|
|
Domain west = service.createDomain("dual-region", "OpenSearch_2.11", null, null, null, EU_WEST_1);
|
|
|
|
assertEquals(AwsArnUtils.Arn.of("es", US_EAST_1, ACCOUNT, "domain/dual-region").toString(),
|
|
east.getArn());
|
|
assertEquals(AwsArnUtils.Arn.of("es", EU_WEST_1, ACCOUNT, "domain/dual-region").toString(),
|
|
west.getArn());
|
|
assertEquals(US_EAST_1, service.describeDomain("dual-region", US_EAST_1).getRegion());
|
|
assertEquals(EU_WEST_1, service.describeDomain("dual-region", EU_WEST_1).getRegion());
|
|
}
|
|
|
|
/**
|
|
* IAM's tag lookup reads the tags of the domain whose ARN it is given, as tag calls leave them, a tag call giving
|
|
* the domain a new map rather than changing the one a reader holds, and those of one stored under its name from
|
|
* before keys named the Region, in place, without migrating it; nothing for an ARN of another account or Region, or
|
|
* of no domain.
|
|
*/
|
|
@Test
|
|
void storedTagsAreTheTagsOfTheDomainTheArnNamesAndTaggingPutsANewMap() {
|
|
when(domainManager.tryStartDomain(any())).thenReturn(false);
|
|
String arn = service.createDomain("tagged", "OpenSearch_2.11", null, null, Map.of("env", "dev"), US_EAST_1)
|
|
.getArn();
|
|
Map<String, String> held = service.listTags(arn);
|
|
service.addTags(arn, Map.of("team", "data"));
|
|
service.removeTags(arn, List.of("env"));
|
|
String legacyArn = AwsArnUtils.Arn.of("es", US_EAST_1, ACCOUNT, "domain/legacy").toString();
|
|
Domain legacy = new Domain();
|
|
legacy.setDomainName("legacy");
|
|
legacy.setArn(legacyArn);
|
|
legacy.setTags(Map.of("tier", "web"));
|
|
domainStore.putForAccount(ACCOUNT, "legacy", legacy);
|
|
|
|
assertEquals(Map.of("env", "dev"), held);
|
|
assertEquals(Map.of("team", "data"), service.storedTags(arn));
|
|
assertEquals(Map.of("tier", "web"), service.storedTags(legacyArn));
|
|
assertTrue(domainStore.getForAccount(ACCOUNT, "legacy").isPresent());
|
|
assertEquals(Map.of(), service.storedTags(arn.replace(":" + ACCOUNT + ":", ":111122223333:")));
|
|
assertEquals(Map.of(), service.storedTags(arn.replace(":" + US_EAST_1 + ":", ":" + EU_WEST_1 + ":")));
|
|
assertEquals(Map.of(), service.storedTags(arn + "0"));
|
|
assertEquals(Map.of(), service.storedTags("arn:aws:s3:::tagged"));
|
|
}
|
|
|
|
@Test
|
|
void sameNameInTheSameRegionStillConflicts() {
|
|
when(domainManager.tryStartDomain(any())).thenReturn(false);
|
|
service.createDomain("occupied-name", "OpenSearch_2.11", null, null, null, US_EAST_1);
|
|
|
|
AwsException duplicate = assertThrows(AwsException.class, () -> service.createDomain(
|
|
"occupied-name", "OpenSearch_2.11", null, null, null, US_EAST_1));
|
|
|
|
assertEquals("ResourceAlreadyExistsException", duplicate.getErrorCode());
|
|
}
|
|
|
|
@Test
|
|
void describeFromAnotherRegionDoesNotSeeTheDomain() {
|
|
when(domainManager.tryStartDomain(any())).thenReturn(false);
|
|
service.createDomain("east-only", "OpenSearch_2.11", null, null, null, US_EAST_1);
|
|
|
|
AwsException absent = assertThrows(AwsException.class,
|
|
() -> service.describeDomain("east-only", EU_WEST_1));
|
|
|
|
assertEquals("ResourceNotFoundException", absent.getErrorCode());
|
|
}
|
|
|
|
@Test
|
|
void updateInOneRegionLeavesTheOtherRegionUntouched() {
|
|
when(domainManager.tryStartDomain(any())).thenReturn(false);
|
|
service.createDomain("split-update", "OpenSearch_2.11", null, null, null, US_EAST_1);
|
|
service.createDomain("split-update", "OpenSearch_2.11", null, null, null, EU_WEST_1);
|
|
|
|
ClusterConfig eastConfig = new ClusterConfig();
|
|
eastConfig.setInstanceCount(5);
|
|
service.updateDomainConfig("split-update", null, eastConfig, null, US_EAST_1);
|
|
|
|
assertEquals(5, service.describeDomain("split-update", US_EAST_1).getClusterConfig().getInstanceCount());
|
|
assertEquals(1, service.describeDomain("split-update", EU_WEST_1).getClusterConfig().getInstanceCount());
|
|
}
|
|
|
|
@Test
|
|
void deleteInOneRegionLeavesTheOtherRegionIntact() {
|
|
when(domainManager.tryStartDomain(any())).thenReturn(false);
|
|
service.createDomain("split-delete", "OpenSearch_2.11", null, null, null, US_EAST_1);
|
|
service.createDomain("split-delete", "OpenSearch_2.11", null, null, null, EU_WEST_1);
|
|
|
|
Domain deleted = service.deleteDomain("split-delete", US_EAST_1);
|
|
|
|
assertTrue(deleted.isDeleted());
|
|
assertThrows(AwsException.class, () -> service.describeDomain("split-delete", US_EAST_1));
|
|
assertEquals(EU_WEST_1, service.describeDomain("split-delete", EU_WEST_1).getRegion());
|
|
}
|
|
|
|
@Test
|
|
void listDomainNamesReturnsOnlyTheRequestRegionsDomains() {
|
|
when(domainManager.tryStartDomain(any())).thenReturn(false);
|
|
service.createDomain("east-listed", "OpenSearch_2.11", null, null, null, US_EAST_1);
|
|
service.createDomain("west-listed", "OpenSearch_2.11", null, null, null, EU_WEST_1);
|
|
|
|
List<Domain> east = service.listDomainNames(null, US_EAST_1);
|
|
List<Domain> west = service.listDomainNames(null, EU_WEST_1);
|
|
|
|
assertTrue(east.stream().anyMatch(d -> d.getDomainName().equals("east-listed")));
|
|
assertFalse(east.stream().anyMatch(d -> d.getDomainName().equals("west-listed")));
|
|
assertTrue(west.stream().anyMatch(d -> d.getDomainName().equals("west-listed")));
|
|
assertFalse(west.stream().anyMatch(d -> d.getDomainName().equals("east-listed")));
|
|
}
|
|
|
|
/**
|
|
* A record persisted before domains were keyed by region sits under the bare name; its ARN,
|
|
* which has always carried the region, decides which region may still see it, and the
|
|
* lookup moves it onto the region-scoped key.
|
|
*/
|
|
@Test
|
|
void preRegionScopingRecordResolvesByItsArnRegionAndMigrates() {
|
|
Domain legacy = new Domain();
|
|
legacy.setDomainName("legacy-record");
|
|
legacy.setAccountId(ACCOUNT);
|
|
legacy.setArn(AwsArnUtils.Arn.of("es", US_EAST_1, ACCOUNT, "domain/legacy-record").toString());
|
|
domainStore.putForAccount(ACCOUNT, "legacy-record", legacy);
|
|
|
|
Domain resolved = service.describeDomain("legacy-record", US_EAST_1);
|
|
|
|
assertEquals(US_EAST_1, AwsArnUtils.parse(resolved.getArn()).region());
|
|
assertFalse(domainStore.keysForAccount(ACCOUNT).contains("legacy-record"),
|
|
"the lookup migrates the bare-name key onto the region-scoped key");
|
|
assertTrue(domainStore.keysForAccount(ACCOUNT).contains(US_EAST_1 + "::legacy-record"));
|
|
assertThrows(AwsException.class, () -> service.describeDomain("legacy-record", EU_WEST_1));
|
|
}
|
|
|
|
/**
|
|
* A pre-scoping record whose ARN names another region is invisible from, and does not
|
|
* collide with, a same-named create in this region.
|
|
*/
|
|
@Test
|
|
void preRegionScopingRecordOfAnotherRegionDoesNotConflictWithACreateHere() {
|
|
when(domainManager.tryStartDomain(any())).thenReturn(false);
|
|
Domain legacy = new Domain();
|
|
legacy.setDomainName("legacy-elsewhere");
|
|
legacy.setAccountId(ACCOUNT);
|
|
legacy.setArn(AwsArnUtils.Arn.of("es", EU_WEST_1, ACCOUNT, "domain/legacy-elsewhere").toString());
|
|
domainStore.putForAccount(ACCOUNT, "legacy-elsewhere", legacy);
|
|
|
|
Domain created = service.createDomain("legacy-elsewhere", "OpenSearch_2.11",
|
|
null, null, null, US_EAST_1);
|
|
|
|
assertEquals(US_EAST_1, AwsArnUtils.parse(created.getArn()).region());
|
|
assertEquals(EU_WEST_1,
|
|
AwsArnUtils.parse(service.describeDomain("legacy-elsewhere", EU_WEST_1).getArn()).region());
|
|
}
|
|
|
|
@Test
|
|
void pollerFlipsReadinessUnderTheScopedKey() {
|
|
when(domainManager.tryStartDomain(any())).thenReturn(true);
|
|
when(domainManager.isReady(any())).thenReturn(true);
|
|
service.createDomain("poller-scoped", "OpenSearch_2.11", null, null, null, EU_WEST_1);
|
|
|
|
service.pollReadiness();
|
|
|
|
assertFalse(service.describeDomain("poller-scoped", EU_WEST_1).isProcessing());
|
|
assertTrue(domainStore.keysForAccount(ACCOUNT).contains(EU_WEST_1 + "::poller-scoped"));
|
|
}
|
|
}
|