-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
testing-concord-server: add agent wrapper, simple test
- Loading branch information
Showing
4 changed files
with
248 additions
and
34 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
92 changes: 92 additions & 0 deletions
92
...ng-server/src/main/java/com/walmartlabs/concord/it/testingserver/TestingConcordAgent.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,92 @@ | ||
package com.walmartlabs.concord.it.testingserver; | ||
|
||
/*- | ||
* ***** | ||
* Concord | ||
* ----- | ||
* Copyright (C) 2017 - 2023 Walmart Inc. | ||
* ----- | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* ===== | ||
*/ | ||
|
||
import com.google.inject.Guice; | ||
import com.google.inject.Module; | ||
import com.typesafe.config.Config; | ||
import com.typesafe.config.ConfigFactory; | ||
import com.typesafe.config.ConfigParseOptions; | ||
import com.typesafe.config.ConfigResolveOptions; | ||
import com.walmartlabs.concord.agent.Agent; | ||
import com.walmartlabs.concord.agent.AgentModule; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* A helper class for running concord-agent. | ||
* The agent runs in the same JVM as TestingConcordAgent. | ||
*/ | ||
public class TestingConcordAgent implements AutoCloseable { | ||
|
||
private final Map<String, String> extraConfiguration; | ||
private final List<Function<Config, Module>> extraModules; | ||
private final String agentApiKey; | ||
|
||
private Agent agent; | ||
|
||
public TestingConcordAgent(String agentApiKey) { | ||
this(agentApiKey, Map.of(), List.of()); | ||
} | ||
|
||
public TestingConcordAgent(String agentApiKey, Map<String, String> extraConfiguration, List<Function<Config, Module>> extraModules) { | ||
this.agentApiKey = agentApiKey; | ||
this.extraConfiguration = extraConfiguration; | ||
this.extraModules = extraModules; | ||
} | ||
|
||
public synchronized void start() { | ||
var config = prepareConfig(); | ||
var system = new AgentModule(config); | ||
var allModules = Stream.concat(extraModules.stream().map(f -> f.apply(config)), Stream.of(system)).toList(); | ||
var injector = Guice.createInjector(allModules); | ||
agent = injector.getInstance(Agent.class); | ||
agent.start(); | ||
} | ||
|
||
public synchronized void stop() { | ||
if (agent != null) { | ||
agent.stop(); | ||
agent = null; | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
this.stop(); | ||
} | ||
|
||
private Config prepareConfig() { | ||
var extraConfig = ConfigFactory.parseMap(this.extraConfiguration); | ||
|
||
var testConfig = ConfigFactory.parseMap(Map.of( | ||
"server.apiKey", agentApiKey | ||
)); | ||
|
||
var defaultConfig = ConfigFactory.load("concord-agent.conf", ConfigParseOptions.defaults(), ConfigResolveOptions.defaults().setAllowUnresolved(true)) | ||
.getConfig("concord-agent"); | ||
|
||
return extraConfig.withFallback(testConfig.withFallback(defaultConfig)).resolve(); | ||
} | ||
} |
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
85 changes: 85 additions & 0 deletions
85
...sting-server/src/test/java/com/walmartlabs/concord/it/testingserver/TestingConcordIT.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,85 @@ | ||
package com.walmartlabs.concord.it.testingserver; | ||
|
||
/*- | ||
* ***** | ||
* Concord | ||
* ----- | ||
* Copyright (C) 2017 - 2023 Walmart Inc. | ||
* ----- | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* ===== | ||
*/ | ||
|
||
import com.walmartlabs.concord.client2.ApiClientConfiguration; | ||
import com.walmartlabs.concord.client2.DefaultApiClientFactory; | ||
import com.walmartlabs.concord.client2.ProcessApi; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.time.Duration; | ||
import java.util.Map; | ||
|
||
import static com.walmartlabs.concord.client2.ProcessEntry.StatusEnum.FINISHED; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
/** | ||
* A test that tests TestingConcordServer and TestingConcordAgent can run together in the same JVM. | ||
*/ | ||
public class TestingConcordIT { | ||
|
||
private TestingConcordServer concordServer; | ||
private TestingConcordAgent concordAgent; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
concordServer = new TestingConcordServer(); | ||
concordServer.start(); | ||
|
||
concordAgent = new TestingConcordAgent(concordServer.getAgentApiKey()); | ||
concordAgent.start(); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
if (concordAgent != null) { | ||
concordAgent.close(); | ||
concordAgent = null; | ||
} | ||
|
||
if (concordServer != null) { | ||
concordServer.close(); | ||
concordServer = null; | ||
} | ||
} | ||
|
||
@Test(timeout = 120_000) | ||
public void testRunningSimpleProcess() throws Exception { | ||
var client = new DefaultApiClientFactory("http://localhost:8001") | ||
.create(ApiClientConfiguration.builder() | ||
.apiKey(concordServer.getAdminApiKey()) | ||
.build()); | ||
|
||
var processApi = new ProcessApi(client); | ||
var response = processApi.startProcess(Map.of("concord.yml", """ | ||
flows: | ||
default: | ||
- log: "Hello!" | ||
""".getBytes())); | ||
assertNotNull(response.getInstanceId()); | ||
|
||
var process = processApi.waitForCompletion(response.getInstanceId(), Duration.ofSeconds(60).toMillis()); | ||
assertEquals(FINISHED, process.getStatus()); | ||
} | ||
} |