-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathrunner_test.go
More file actions
123 lines (116 loc) · 2.77 KB
/
Copy pathrunner_test.go
File metadata and controls
123 lines (116 loc) · 2.77 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
118
119
120
121
122
123
package functions
import (
"errors"
"testing"
)
// TestGetRunFuncErrors ensures that known runtimes which do not yet
// have their runner implemented return a "not yet available" message, as
// distinct from unrecognized runtimes which state as much.
func TestGetRunFuncErrors(t *testing.T) {
tests := []struct {
Runtime string
ExpectedIs error
ExpectedAs any
}{
{"", ErrRuntimeRequired, nil},
{"go", nil, nil},
{"python", nil, nil},
{"rust", nil, &ErrRunnerNotImplemented{}},
{"node", nil, &ErrRunnerNotImplemented{}},
{"typescript", nil, &ErrRunnerNotImplemented{}},
{"quarkus", nil, &ErrRunnerNotImplemented{}},
{"springboot", nil, &ErrRunnerNotImplemented{}},
{"other", nil, &ErrRuntimeNotRecognized{}},
}
for _, test := range tests {
t.Run(test.Runtime, func(t *testing.T) {
ctx := t.Context()
job := Job{Function: Function{Runtime: test.Runtime}}
_, err := getRunFunc(ctx, &job)
if test.ExpectedAs != nil && !errors.As(err, test.ExpectedAs) {
t.Fatalf("did not receive expected error type for %v runtime.", test.Runtime)
}
t.Logf("ok: %v", err)
})
}
}
func TestBuildRunnerEnv_KafkaConfig(t *testing.T) {
job := &Job{
Function: Function{
Root: t.TempDir(),
Runtime: "go",
Run: RunSpec{
Kafka: &KafkaConfig{
Brokers: "broker1:9092",
Topic: "my-topic",
ConsumerGroup: "my-group",
},
},
},
}
env, err := buildRunnerEnv(job, nil)
if err != nil {
t.Fatal(err)
}
want := map[string]string{
"FUNC_TRANSPORT": "kafka",
"KAFKA_BROKERS": "broker1:9092",
"KAFKA_TOPIC": "my-topic",
"KAFKA_CONSUMER_GROUP": "my-group",
}
for key, val := range want {
found := false
for _, e := range env {
if e == key+"="+val {
found = true
break
}
}
if !found {
t.Errorf("expected %s=%s in env, not found", key, val)
}
}
}
func TestBuildRunnerEnv_NoKafka(t *testing.T) {
t.Setenv("FUNC_TRANSPORT", "inherited")
job := &Job{
Function: Function{
Root: t.TempDir(),
Runtime: "go",
},
}
env, err := buildRunnerEnv(job, nil)
if err != nil {
t.Fatal(err)
}
for _, e := range env {
if e == "FUNC_TRANSPORT=kafka" {
t.Error("FUNC_TRANSPORT should not be set to kafka when Kafka is nil")
}
}
}
func TestBuildRunnerEnv_KafkaIncomplete(t *testing.T) {
t.Setenv("FUNC_TRANSPORT", "inherited")
job := &Job{
Function: Function{
Root: t.TempDir(),
Runtime: "go",
Run: RunSpec{
Kafka: &KafkaConfig{
Brokers: "broker1:9092",
Topic: "",
ConsumerGroup: "my-group",
},
},
},
}
env, err := buildRunnerEnv(job, nil)
if err != nil {
t.Fatal(err)
}
for _, e := range env {
if e == "FUNC_TRANSPORT=kafka" {
t.Error("FUNC_TRANSPORT should not be set to kafka when Kafka topic is empty")
}
}
}