Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ to docs, or any other relevant information.
customization. `NexusClientInterceptor` beans are automatically registered for the root client.
- **Experimental**: External storage in clients, activity workers, and Nexus workers can now read references that
were encoded with the configured payload codec and marked with `__temporal_external_storage_reference` metadata.
- `workflowcheck check` can load a rules file from `TEMPORAL_WORKFLOWCHECK_CONFIG`.

### Changed
- Updated Cloud API bindings to v0.27.0, including opt-in user group memberships, projects,
Expand Down
6 changes: 6 additions & 0 deletions temporal-workflowcheck/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ The `check` command accepts the following arguments:
`:` for Nix), or prefixed with an `@` symbol saying it's a file with entries one per line, or just as separate
arguments. They are all combined to one large classpath when running.

Set `TEMPORAL_WORKFLOWCHECK_CONFIG` to the path of a `.properties` file to load it without
adding a `--config` argument. The file is loaded after the default rules and before any
`--config` files. An unset or empty variable is ignored.

TEMPORAL_WORKFLOWCHECK_CONFIG=workflowcheck.properties java -jar path/to/temporal-workflowcheck-<version>-all.jar check path/to/my.jar

### Running in a Gradle project

See the [Gradle sample](samples/gradle).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ private static int check(String[] args) throws IOException {
"Analyze Temporal workflows for common mistakes.\n"
+ "\n"
+ "Usage:\n"
+ " workflowcheck check <classpath...> [--config <config-file>] [--no-default-config] [--show-valid]");
+ " workflowcheck check <classpath...> [--config <config-file>] [--no-default-config] [--show-valid]\n"
+ "\n"
+ "Set TEMPORAL_WORKFLOWCHECK_CONFIG to load a configuration file before any --config files.");
return 0;
}
// Args list that removes options as encountered
Expand All @@ -48,6 +50,10 @@ private static int check(String[] args) throws IOException {
if (!argsList.remove("--no-default-config")) {
configProps.add(Config.defaultProperties());
}
String environmentConfig = System.getenv("TEMPORAL_WORKFLOWCHECK_CONFIG");
if (environmentConfig != null && !environmentConfig.isEmpty()) {
configProps.add(loadProperties(environmentConfig));
}
while (true) {
int configIndex = argsList.indexOf("--config");
if (configIndex == -1) {
Expand All @@ -57,11 +63,7 @@ private static int check(String[] args) throws IOException {
return 1;
}
argsList.remove(configIndex);
Properties props = new Properties();
try (FileInputStream is = new FileInputStream(argsList.remove(configIndex))) {
props.load(is);
}
configProps.add(props);
configProps.add(loadProperties(argsList.remove(configIndex)));
}

// Whether we should also show valid
Expand Down Expand Up @@ -114,6 +116,14 @@ private static int check(String[] args) throws IOException {
return anyInvalidImpls ? 1 : 0;
}

private static Properties loadProperties(String path) throws IOException {
Properties props = new Properties();
try (FileInputStream is = new FileInputStream(path)) {
props.load(is);
}
return props;
}

private static int prebuildConfig(String[] args) {
System.err.println("TODO");
return 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package io.temporal.workflowcheck;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import io.temporal.workflowcheck.testdata.BadCalls.ClockWorkflow;
import io.temporal.workflowcheck.testdata.BadCalls.ClockWorkflowImpl;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class MainTest {
@Rule public final TemporaryFolder temp = new TemporaryFolder();

@Test
public void environmentConfigOverridesDefaultConfig() throws Exception {
Path environment = configFile("environment rules.properties", false);
assertCheckExitCode(1, null);
assertCheckExitCode(0, environment.toString());
Path unrelated = configFile("unrelated.properties", "java/lang/System.nanoTime", true);
assertCheckExitCode(1, unrelated.toString());
}

@Test
public void explicitConfigOverridesEnvironment() throws Exception {
Path environment = configFile("environment.properties", false);
Path explicit = configFile("explicit.properties", true);
Path empty = temp.newFile("empty.properties").toPath();
Path unrelated = configFile("unrelated.properties", "java/lang/System.nanoTime", true);
Path specific = configFile("specific.properties", "java/lang/System.currentTimeMillis()", true);
assertCheckExitCode(0, environment.toString(), "--config", empty.toString());
assertCheckExitCode(0, environment.toString(), "--config", unrelated.toString());
assertCheckExitCode(1, environment.toString(), "--config", explicit.toString());
assertCheckExitCode(1, environment.toString(), "--config", specific.toString());
}

@Test
public void laterExplicitConfigStillWins() throws Exception {
Path first = configFile("first.properties", true);
Path second = configFile("second.properties", false);
Path empty = temp.newFile("empty.properties").toPath();
assertCheckExitCode(1, null, "--no-default-config", "--config", first.toString());
assertCheckExitCode(
1, null, "--no-default-config", "--config", first.toString(), "--config", empty.toString());
assertCheckExitCode(
0,
null,
"--no-default-config",
"--config",
first.toString(),
"--config",
second.toString());
}

@Test
public void emptyEnvironmentUsesDefaultConfig() throws Exception {
assertCheckExitCode(1, "");
}

@Test
public void worksWithoutDefaultConfig() throws Exception {
assertCheckExitCode(0, null, "--no-default-config");
assertCheckExitCode(
1, configFile("environment.properties", true).toString(), "--no-default-config");
}

private Path configFile(String name, boolean invalid) throws Exception {
return configFile(name, "java/lang/System.currentTimeMillis", invalid);
}

private Path configFile(String name, String member, boolean invalid) throws Exception {
Path file = temp.newFile(name).toPath();
Files.write(
file,
("temporal.workflowcheck.invalid." + member + "=" + invalid + "\n")
.getBytes(StandardCharsets.UTF_8));
return file;
}

private void assertCheckExitCode(int expected, String environment, String... args)
throws Exception {
Path classpath = temp.newFolder().toPath();
for (Class<?> type : Arrays.asList(ClockWorkflow.class, ClockWorkflowImpl.class)) {
String name = type.getName().replace('.', '/') + ".class";
Path target = classpath.resolve(name);
Files.createDirectories(target.getParent());
try (InputStream input = type.getResourceAsStream("/" + name)) {
Files.copy(input, target);
}
}

String executable = File.separatorChar == '\\' ? "java.exe" : "java";
List<String> command =
new ArrayList<>(
Arrays.asList(
Paths.get(System.getProperty("java.home"), "bin", executable).toString(),
"-cp",
System.getProperty("java.class.path"),
Main.class.getName(),
"check",
classpath.toString()));
command.addAll(Arrays.asList(args));
File output = temp.newFile();
ProcessBuilder builder =
new ProcessBuilder(command).redirectErrorStream(true).redirectOutput(output);
if (environment == null) {
builder.environment().remove("TEMPORAL_WORKFLOWCHECK_CONFIG");
} else {
builder.environment().put("TEMPORAL_WORKFLOWCHECK_CONFIG", environment);
}
Process process = builder.start();
try {
assertTrue("workflowcheck did not exit", process.waitFor(30, TimeUnit.SECONDS));
} finally {
if (process.isAlive()) {
process.destroyForcibly();
}
}
String result = new String(Files.readAllBytes(output.toPath()), StandardCharsets.UTF_8);
assertTrue(result, result.contains("Found 1 class(es) with workflow methods"));
assertEquals(result, expected, process.exitValue());
assertEquals(
result,
expected == 1,
result.contains("java.lang.System.currentTimeMillis() which is configured as invalid"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ public interface BadCalls {
@UpdateValidatorMethod(updateName = "doUpdate")
void doUpdateValidate();

@WorkflowInterface
interface ClockWorkflow {
@WorkflowMethod
long currentTime();
}

class ClockWorkflowImpl implements ClockWorkflow {
@Override
public long currentTime() {
// INVALID: Direct invalid call to the wall clock
// * class: io/temporal/workflowcheck/testdata/BadCalls$ClockWorkflowImpl
// * method: currentTime()J
// * accessedClass: java/lang/System
// * accessedMember: currentTimeMillis()J
return System.currentTimeMillis();
}
}

class BadCallsImpl implements BadCalls {
private static final String FIELD_FINAL = "foo";
private static String FIELD_NON_FINAL = "bar";
Expand Down