-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathScriptTest.java
More file actions
117 lines (99 loc) · 4.08 KB
/
ScriptTest.java
File metadata and controls
117 lines (99 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package io.kestra.plugins.scripts.go;
import java.io.InputStreamReader;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
import org.junit.jupiter.api.Test;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.CharStreams;
import io.kestra.core.junit.annotations.KestraTest;
import io.kestra.core.models.executions.LogEntry;
import io.kestra.core.models.property.Property;
import io.kestra.core.queues.DispatchQueueInterface;
import io.kestra.core.runners.RunContextFactory;
import io.kestra.core.storages.StorageInterface;
import io.kestra.core.tenant.TenantService;
import io.kestra.core.utils.TestsUtils;
import io.kestra.plugin.scripts.go.Script;
import jakarta.inject.Inject;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
@KestraTest
public class ScriptTest {
@Inject
RunContextFactory runContextFactory;
@Inject
private DispatchQueueInterface<LogEntry> logQueue;
@Inject
StorageInterface storageInterface;
@Test
void should_print_hello_there() throws Exception {
List<LogEntry> logs = new CopyOnWriteArrayList<>();
logQueue.addListener(logs::add);
var script = Script.builder()
.id("go-script-" + UUID.randomUUID())
.type(Script.class.getName())
.allowWarning(true)
.script(Property.ofValue("""
package main
import "fmt"
func main() {
fmt.Println("hello there!")
}
"""))
.build();
var runContext = TestsUtils.mockRunContext(runContextFactory, script, ImmutableMap.of());
var run = script.run(runContext);
assertThat(run.getExitCode(), is(0));
assertThat(run.getStdOutLineCount(), is(1));
assertThat(run.getStdErrLineCount(), is(0));
TestsUtils.awaitLog(logs, log -> log.getMessage() != null && log.getMessage().contains("hello there!"));
assertThat(List.copyOf(logs).stream().filter(logEntry -> logEntry.getMessage() != null && logEntry.getMessage().contains("hello there!")).count(), is(1L));
}
@Test
void should_create_output_csv() throws Exception {
var outputFile = "output.csv";
var script = Script.builder()
.id("go_script")
.type(Script.class.getName())
.allowWarning(true)
.script(Property.ofValue("""
package main
import (
"os"
"github.com/go-gota/gota/dataframe"
"github.com/go-gota/gota/series"
)
func main() {
names := series.New([]string{"Alice", "Bob", "Charlie"}, series.String, "Name")
ages := series.New([]int{25, 30, 35}, series.Int, "Age")
df := dataframe.New(names, ages)
file, _ := os.Create("output.csv")
df.WriteCSV(file)
defer file.Close()
}
"""))
.outputFiles(Property.ofValue(List.of(outputFile)))
.beforeCommands(
Property.ofValue(
List.of(
"go mod init go_script",
"go get github.com/go-gota/gota",
"go mod tidy"
)
)
)
.build();
var runContext = TestsUtils.mockRunContext(runContextFactory, script, ImmutableMap.of());
var run = script.run(runContext);
assertThat(run.getExitCode(), is(0));
assertThat(run.getOutputFiles().containsKey(outputFile), is(true));
var outputCsvInputStream = storageInterface.get(TenantService.MAIN_TENANT, null, run.getOutputFiles().get(outputFile));
assertThat(CharStreams.toString(new InputStreamReader(outputCsvInputStream)), is("""
Name,Age
Alice,25
Bob,30
Charlie,35
"""));
}
}