forked from triggerdotdev/trigger.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvUtil.test.ts
More file actions
218 lines (185 loc) · 7.2 KB
/
Copy pathenvUtil.test.ts
File metadata and controls
218 lines (185 loc) · 7.2 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { describe, it, expect } from "vitest";
import { z } from "zod";
import { BoolEnv, AdditionalEnvVars, JsonObjectEnv, JsonAny } from "./envUtil.js";
describe("BoolEnv", () => {
it("should parse string 'true' as true", () => {
expect(BoolEnv.parse("true")).toBe(true);
expect(BoolEnv.parse("TRUE")).toBe(true);
expect(BoolEnv.parse("True")).toBe(true);
});
it("should parse string '1' as true", () => {
expect(BoolEnv.parse("1")).toBe(true);
});
it("should parse string 'false' as false", () => {
expect(BoolEnv.parse("false")).toBe(false);
expect(BoolEnv.parse("FALSE")).toBe(false);
expect(BoolEnv.parse("False")).toBe(false);
});
it("should handle whitespace", () => {
expect(BoolEnv.parse(" true ")).toBe(true);
expect(BoolEnv.parse(" 1 ")).toBe(true);
});
it("should pass through boolean values", () => {
expect(BoolEnv.parse(true)).toBe(true);
expect(BoolEnv.parse(false)).toBe(false);
});
it("should return false for invalid inputs", () => {
expect(BoolEnv.parse("invalid")).toBe(false);
expect(BoolEnv.parse("")).toBe(false);
});
});
describe("AdditionalEnvVars", () => {
it("should parse single key-value pair", () => {
expect(AdditionalEnvVars.parse("FOO=bar")).toEqual({ FOO: "bar" });
});
it("should parse multiple key-value pairs", () => {
expect(AdditionalEnvVars.parse("FOO=bar,BAZ=qux")).toEqual({
FOO: "bar",
BAZ: "qux",
});
});
it("should handle whitespace", () => {
expect(AdditionalEnvVars.parse(" FOO = bar , BAZ = qux ")).toEqual({
FOO: "bar",
BAZ: "qux",
});
});
it("should return undefined for empty string", () => {
expect(AdditionalEnvVars.parse("")).toBeUndefined();
});
it("should return undefined for invalid format", () => {
expect(AdditionalEnvVars.parse("invalid")).toBeUndefined();
});
it("should skip invalid pairs but include valid ones", () => {
expect(AdditionalEnvVars.parse("FOO=bar,INVALID,BAZ=qux")).toEqual({
FOO: "bar",
BAZ: "qux",
});
});
it("should pass through undefined", () => {
expect(AdditionalEnvVars.parse(undefined)).toBeUndefined();
});
it("should handle empty values", () => {
expect(AdditionalEnvVars.parse("FOO=,BAR=value")).toEqual({
BAR: "value",
});
});
});
describe("JsonObjectEnv (string-valued)", () => {
const schema = JsonObjectEnv("TEST_ENV");
it("returns empty object for default (no value)", () => {
expect(schema.parse(undefined)).toEqual({});
});
it("parses a simple string-valued JSON object", () => {
expect(schema.parse('{"a":"1","b":"2"}')).toEqual({ a: "1", b: "2" });
});
it("parses an empty JSON object", () => {
expect(schema.parse("{}")).toEqual({});
});
it("rejects non-JSON input", () => {
expect(() => schema.parse("not json")).toThrowError(/not valid JSON/);
});
it("rejects JSON arrays", () => {
expect(() => schema.parse("[]")).toThrowError(/must be a JSON object \(got array\)/);
});
it("rejects JSON primitives", () => {
expect(() => schema.parse('"foo"')).toThrowError(/must be a JSON object \(got string\)/);
expect(() => schema.parse("42")).toThrowError(/must be a JSON object \(got number\)/);
expect(() => schema.parse("null")).toThrowError(/must be a JSON object \(got object\)/);
});
it("rejects values that are not strings (with default validator)", () => {
expect(() => schema.parse('{"a": 1}')).toThrowError(/has invalid value/);
expect(() => schema.parse('{"a": true}')).toThrowError(/has invalid value/);
});
});
describe("JsonObjectEnv (arbitrary-value)", () => {
const schema = JsonObjectEnv("TEST_ANY", { valueValidator: JsonAny });
it("accepts nested objects", () => {
expect(
schema.parse(
JSON.stringify({
runAsNonRoot: true,
runAsUser: 1000,
capabilities: { drop: ["ALL"] },
})
)
).toEqual({
runAsNonRoot: true,
runAsUser: 1000,
capabilities: { drop: ["ALL"] },
});
});
it("accepts mixed value types", () => {
expect(schema.parse('{"s":"x","n":1,"b":true,"a":[1,2],"o":{"k":"v"}}')).toEqual({
s: "x",
n: 1,
b: true,
a: [1, 2],
o: { k: "v" },
});
});
it("still rejects non-object roots", () => {
expect(() => schema.parse('"x"')).toThrowError(/must be a JSON object/);
expect(() => schema.parse("[1,2,3]")).toThrowError(/must be a JSON object/);
});
it("includes the env var name in error messages", () => {
const named = JsonObjectEnv("KUBERNETES_WORKER_POD_SECURITY_CONTEXT");
expect(() => named.parse("{notjson")).toThrowError(/KUBERNETES_WORKER_POD_SECURITY_CONTEXT/);
});
});
describe("JsonObjectEnv (flat string values, K8s-annotation shape)", () => {
// Regression test for a real production incident on FedStart: passing
// KUBERNETES_WORKER_POD_ANNOTATIONS with a nested-map value silently
// passed zod validation under the default JsonStringMap validator
// (because `{}` is a valid empty `Record<string,string>`), but then
// got spread directly into a K8s Pod's metadata.annotations and
// rejected by the apiserver:
//
// "Pod in version v1 cannot be handled as a Pod: json: cannot
// unmarshal object into Go struct field
// ObjectMeta.metadata.annotations of type string"
//
// K8s annotations are flat `map[string]string` per the API spec, so
// KUBERNETES_WORKER_POD_ANNOTATIONS must validate values as `z.string()`,
// not as a nested string map.
const schema = JsonObjectEnv("KUBERNETES_WORKER_POD_ANNOTATIONS", {
valueValidator: z.string(),
});
it("accepts the Rubix pod-cert annotation as a string value", () => {
// Rubix's `com.palantir.rubix.service/pod-cert` annotation expects
// its VALUE to be the 2-char string "{}" (which Rubix internally
// parses as JSON to determine cert minting config). The env var
// input is itself JSON-stringified, so the value here is the
// 4-char string `"{}"` — two quotes around two braces.
expect(schema.parse('{"com.palantir.rubix.service/pod-cert":"{}"}')).toEqual({
"com.palantir.rubix.service/pod-cert": "{}",
});
});
it("accepts multiple flat string annotations", () => {
expect(
schema.parse(
'{"com.palantir.rubix.service/pod-cert":"{}","trigger.dev/owner":"supervisor"}'
)
).toEqual({
"com.palantir.rubix.service/pod-cert": "{}",
"trigger.dev/owner": "supervisor",
});
});
it("rejects object values (annotations must be flat strings)", () => {
// The pre-fix bug: this would have passed zod with the default
// JsonStringMap validator, then crashed at K8s apiserver. Schema
// must reject before the value ever reaches K8s.
expect(() => schema.parse('{"com.palantir.rubix.service/pod-cert":{}}')).toThrowError(
/has invalid value/
);
});
it("rejects nested string maps as values", () => {
expect(() => schema.parse('{"some/annotation":{"nested":"value"}}')).toThrowError(
/has invalid value/
);
});
it("rejects numeric and boolean values", () => {
expect(() => schema.parse('{"some/annotation":42}')).toThrowError(/has invalid value/);
expect(() => schema.parse('{"some/annotation":true}')).toThrowError(/has invalid value/);
});
});