Skip to content

Commit

Permalink
Building out tests for verifyNoInvisPlaceStartup, as well as other co…
Browse files Browse the repository at this point in the history
…de cleanup
  • Loading branch information
sambish5 committed Oct 17, 2023
1 parent ca1c1f0 commit 09542df
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/main/java/emissary/admin/Startup.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class Startup {
protected final Map<String, List<String>> placeLists = new ConcurrentHashMap<>();
protected final Map<String, List<String>> pickupLists = new ConcurrentHashMap<>();

// sets to verify no invisible place start-ups
// sets to keep track of possible invisible place startup
protected Set<String> activeDirPlaces = new LinkedHashSet<>();
protected Set<String> placeAlreadyStarted = new LinkedHashSet<>();

Expand Down Expand Up @@ -169,7 +169,9 @@ public void start() throws EmissaryException {
// the pickup places here.
startPickUpPlaces();

verifyNoInvisiblePlacesStarted();
if (!verifyNoInvisiblePlacesStarted()) {
// TODO: If invisible places are started, shutdown the EmissaryServer
}
}


Expand Down Expand Up @@ -506,8 +508,10 @@ private void sortPickupOrPlace(String theLocation, Map<String, List<String>> pla
/**
* Verifies the active directory places vs places started up. Log if any places are started without being announced in
* start-up.
*
* @return true if no invisible places started, false if yes
*/
private void verifyNoInvisiblePlacesStarted() {
public boolean verifyNoInvisiblePlacesStarted() {
try {
IDirectoryPlace dirPlace = DirectoryPlace.lookup();
List<DirectoryEntry> dirEntries = dirPlace.getEntries();
Expand Down Expand Up @@ -539,6 +543,9 @@ private void verifyNoInvisiblePlacesStarted() {
// if any places are left in active dir keys, they are places not announced on startup
if (!activeDirPlaces.isEmpty()) {
logger.warn("{} place(s) started up without being announced! {}", activeDirPlaces.size(), activeDirPlaces);
return false;
}

return true;
}
}
68 changes: 67 additions & 1 deletion src/test/java/emissary/admin/StartupTest.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
package emissary.admin;

import emissary.core.Namespace;
import emissary.directory.DirectoryPlace;
import emissary.directory.EmissaryNode;
import emissary.pickup.file.FilePickUpClient;
import emissary.pickup.file.FilePickUpPlace;
import emissary.place.CoordinationPlace;
import emissary.place.sample.DelayPlace;
import emissary.place.sample.DevNullPlace;
import emissary.test.core.junit5.UnitTest;
import emissary.util.io.ResourceReader;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.spy;

class StartupTest extends UnitTest {

@Test
void testSortPlaces() throws IOException {
List<String> somePlaces = new ArrayList<>();
Expand All @@ -43,4 +49,64 @@ void testSortPlaces() throws IOException {
places);
}

@Test
void testInvisPlaceStart() throws IOException {
// setup node, startup, and DirectoryPlace
EmissaryNode node = new EmissaryNode();
Startup startup = new Startup(node.getNodeConfigurator(), node);
String location = "http://" + node.getNodeName() + ":" + node.getNodePort();
dirStartUp();

// test if place is already started before startup
startup.activeDirPlaces.add(location + "/PlaceAlreadyStartedTest");
startup.placeAlreadyStarted.add(location + "/PlaceAlreadyStartedTest");
assertTrue(startup.verifyNoInvisiblePlacesStarted());
startup.activeDirPlaces.clear();
startup.placeAlreadyStarted.clear();

// test if place is started up normally and is active dir
startup.places.put(location, DevNullPlace.class.getSimpleName());
startup.activeDirPlaces.add(DevNullPlace.class.getSimpleName());
assertTrue(startup.verifyNoInvisiblePlacesStarted());
startup.activeDirPlaces.clear();
startup.places.remove(location, DevNullPlace.class.getSimpleName());

// test unannounced place with active dir
startup.activeDirPlaces.add(location + "/PlaceStartUnannouncedTest");
assertFalse(startup.verifyNoInvisiblePlacesStarted());
startup.activeDirPlaces.clear();

// teardown
dirTeardown();
}

private DirectoryPlace master = null;
private DirectoryPlace client = null;

public void dirStartUp() throws IOException {
// master directory
InputStream configStream = new ResourceReader().getConfigDataAsStream(this);

String masterloc = "http://localhost:8001/TestMasterDirectoryPlace";
this.master = spy(new DirectoryPlace(configStream, masterloc, new EmissaryNode()));
configStream.close();
Namespace.bind(masterloc, this.master);

// non-master directory
configStream = new ResourceReader().getConfigDataAsStream(this);
String clientloc = "http://localhost:8001/DirectoryPlace";
this.client = spy(new DirectoryPlace(configStream, this.master.getDirectoryEntry().getKey(), clientloc, new EmissaryNode()));
configStream.close();
Namespace.bind(clientloc, this.client);
}

public void dirTeardown() {
this.master.shutDown();
this.master = null;
this.client.shutDown();
this.client = null;
for (String s : Namespace.keySet()) {
Namespace.unbind(s);
}
}
}
7 changes: 7 additions & 0 deletions src/test/resources/emissary/admin/StartupTest.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
PLACE_NAME = DirectoryPlace
SERVICE_NAME = DIRECTORY
SERVICE_TYPE = STUDY
SERVICE_DESCRIPTION = "This place will provide location information to agents"
SERVICE_COST = 50
SERVICE_QUALITY = 50
SERVICE_PROXY = "EMISSARY_DIRECTORY_SERVICES"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
PLACE_NAME = DirectoryPlace
SERVICE_NAME = DIRECTORY
SERVICE_TYPE = STUDY
SERVICE_DESCRIPTION = "This place will provide location information to agents"
SERVICE_COST = 50
SERVICE_QUALITY = 50
SERVICE_PROXY = "EMISSARY_DIRECTORY_SERVICES"
# This is too fast for normal operations, just for the test!
HEARTBEAT_DELAY_SECONDS = 2
HEARTBEAT_INTERVAL_SECONDS = 5

0 comments on commit 09542df

Please sign in to comment.