-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow STDOUT overrides for AgentBasedEnvironments (#147)
* Allow for STDOUT overrides when using an AgentBasedEnvironment * replace double space indents with 4 space indents * read WRITE_TO_STDOUT env var to the Configuration object * add AWS_EMF_ prefix to the stdout docs * add blump in README for how the new environment variable will work * run spotlessApply --------- Co-authored-by: Paul Schellenberg <[email protected]>
- Loading branch information
1 parent
6b97f76
commit 2713006
Showing
8 changed files
with
167 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/test/java/software/amazon/cloudwatchlogs/emf/environment/AgentBasedEnvironmentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package software.amazon.cloudwatchlogs.emf.environment; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.powermock.api.mockito.PowerMockito.mock; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
import software.amazon.cloudwatchlogs.emf.config.Configuration; | ||
import software.amazon.cloudwatchlogs.emf.config.SystemWrapper; | ||
import software.amazon.cloudwatchlogs.emf.model.MetricsContext; | ||
import software.amazon.cloudwatchlogs.emf.sinks.AgentSink; | ||
import software.amazon.cloudwatchlogs.emf.sinks.ConsoleSink; | ||
import software.amazon.cloudwatchlogs.emf.sinks.Endpoint; | ||
import software.amazon.cloudwatchlogs.emf.sinks.ISink; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest({SystemWrapper.class, AgentBasedEnvironment.class}) | ||
public class AgentBasedEnvironmentTest { | ||
public static class AgentBasedEnvironmentTestImplementation extends AgentBasedEnvironment { | ||
protected AgentBasedEnvironmentTestImplementation(Configuration config) { | ||
super(config); | ||
} | ||
|
||
@Override | ||
public boolean probe() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void configureContext(MetricsContext context) {} | ||
} | ||
|
||
private Configuration configuration; | ||
|
||
@Before | ||
public void setup() { | ||
this.configuration = new Configuration(); | ||
} | ||
|
||
@Test | ||
public void testGetSinkWithDefaultEndpoint() throws Exception { | ||
AgentSink mockedSink = mock(AgentSink.class); | ||
PowerMockito.whenNew(AgentSink.class) | ||
.withAnyArguments() | ||
.then( | ||
invocation -> { | ||
Endpoint endpoint = invocation.getArgument(2); | ||
assertEquals(Endpoint.DEFAULT_TCP_ENDPOINT, endpoint); | ||
return mockedSink; | ||
}); | ||
|
||
AgentBasedEnvironment env = new AgentBasedEnvironmentTestImplementation(configuration); | ||
ISink sink = env.getSink(); | ||
|
||
assertEquals(mockedSink, sink); | ||
} | ||
|
||
@Test | ||
public void testGetSinkWithConfiguredEndpoint() throws Exception { | ||
String endpointUrl = "http://configured-endpoint:1234"; | ||
configuration.setAgentEndpoint(endpointUrl); | ||
AgentSink mockedSink = mock(AgentSink.class); | ||
PowerMockito.whenNew(AgentSink.class) | ||
.withAnyArguments() | ||
.then( | ||
invocation -> { | ||
Endpoint endpoint = invocation.getArgument(2); | ||
assertEquals(Endpoint.fromURL(endpointUrl), endpoint); | ||
return mockedSink; | ||
}); | ||
|
||
AgentBasedEnvironment env = new AgentBasedEnvironmentTestImplementation(configuration); | ||
ISink sink = env.getSink(); | ||
|
||
assertEquals(mockedSink, sink); | ||
} | ||
|
||
@Test | ||
public void testGetSinkOverrideToStdOut() { | ||
configuration.setShouldWriteToStdout(true); | ||
|
||
AgentBasedEnvironment env = new AgentBasedEnvironmentTestImplementation(configuration); | ||
ISink sink = env.getSink(); | ||
|
||
assertEquals(ConsoleSink.class, sink.getClass()); | ||
} | ||
|
||
@Test | ||
public void testGetSinkOverrideToStdOutFailFastOnImproperOverride() throws Exception { | ||
configuration.setShouldWriteToStdout(false); | ||
|
||
testGetSinkWithDefaultEndpoint(); | ||
} | ||
} |