|
| 1 | +package functions |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "slices" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "k8s.io/utils/ptr" |
| 10 | +) |
| 11 | + |
| 12 | +// TestBuildRunnerEnv_InheritsParentEnv ensures that the parent process |
| 13 | +// environment is inherited by the subprocess. |
| 14 | +func TestBuildRunnerEnv_InheritsParentEnv(t *testing.T) { |
| 15 | + const testKey = "FUNC_TEST_INHERIT_CHECK" |
| 16 | + const testVal = "inherited_value" |
| 17 | + t.Setenv(testKey, testVal) |
| 18 | + |
| 19 | + job := &Job{Function: Function{}} |
| 20 | + env, err := buildRunnerEnv(job, nil) |
| 21 | + if err != nil { |
| 22 | + t.Fatalf("unexpected error: %v", err) |
| 23 | + } |
| 24 | + |
| 25 | + expected := testKey + "=" + testVal |
| 26 | + if !slices.Contains(env, expected) { |
| 27 | + t.Errorf("expected parent env var %q in result, but not found", expected) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// TestBuildRunnerEnv_ExtrasAreIncluded ensures that extras like PORT, |
| 32 | +// LISTEN_ADDRESS, and PWD are present in the environment. |
| 33 | +func TestBuildRunnerEnv_ExtrasAreIncluded(t *testing.T) { |
| 34 | + job := &Job{Function: Function{}} |
| 35 | + extras := map[string]string{ |
| 36 | + "PORT": "8080", |
| 37 | + "LISTEN_ADDRESS": "127.0.0.1:8080", |
| 38 | + "PWD": "/tmp/func", |
| 39 | + } |
| 40 | + |
| 41 | + env, err := buildRunnerEnv(job, extras) |
| 42 | + if err != nil { |
| 43 | + t.Fatalf("unexpected error: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + for k, v := range extras { |
| 47 | + expected := k + "=" + v |
| 48 | + if !slices.Contains(env, expected) { |
| 49 | + t.Errorf("expected extra env var %q in result, but not found", expected) |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// TestBuildRunnerEnv_FuncYamlEnvsIncluded ensures that envs from func.yaml |
| 55 | +// (Function.Run.Envs) are interpolated and included. |
| 56 | +func TestBuildRunnerEnv_FuncYamlEnvsIncluded(t *testing.T) { |
| 57 | + job := &Job{ |
| 58 | + Function: Function{ |
| 59 | + Run: RunSpec{ |
| 60 | + Envs: Envs{ |
| 61 | + {Name: ptr.To("MY_VAR"), Value: ptr.To("my_value")}, |
| 62 | + {Name: ptr.To("ANOTHER"), Value: ptr.To("another_value")}, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + env, err := buildRunnerEnv(job, nil) |
| 69 | + if err != nil { |
| 70 | + t.Fatalf("unexpected error: %v", err) |
| 71 | + } |
| 72 | + |
| 73 | + if !slices.Contains(env, "MY_VAR=my_value") { |
| 74 | + t.Error("expected MY_VAR=my_value in result") |
| 75 | + } |
| 76 | + if !slices.Contains(env, "ANOTHER=another_value") { |
| 77 | + t.Error("expected ANOTHER=another_value in result") |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// TestBuildRunnerEnv_FuncYamlEnvsOverrideParent ensures that func.yaml envs |
| 82 | +// take precedence over parent environment variables (last value wins in exec). |
| 83 | +func TestBuildRunnerEnv_FuncYamlEnvsOverrideParent(t *testing.T) { |
| 84 | + const testKey = "FUNC_TEST_OVERRIDE" |
| 85 | + t.Setenv(testKey, "parent_value") |
| 86 | + |
| 87 | + job := &Job{ |
| 88 | + Function: Function{ |
| 89 | + Run: RunSpec{ |
| 90 | + Envs: Envs{ |
| 91 | + {Name: ptr.To(testKey), Value: ptr.To("func_yaml_value")}, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + } |
| 96 | + |
| 97 | + env, err := buildRunnerEnv(job, nil) |
| 98 | + if err != nil { |
| 99 | + t.Fatalf("unexpected error: %v", err) |
| 100 | + } |
| 101 | + |
| 102 | + // The func.yaml value should appear after the parent value. |
| 103 | + // In exec.Cmd, the last duplicate key wins. |
| 104 | + lastValue := "" |
| 105 | + for _, e := range env { |
| 106 | + if v, ok := strings.CutPrefix(e, testKey+"="); ok { |
| 107 | + lastValue = v |
| 108 | + } |
| 109 | + } |
| 110 | + if lastValue != "func_yaml_value" { |
| 111 | + t.Errorf("expected func.yaml value to override parent, got %q", lastValue) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +// TestBuildRunnerEnv_PathInherited is a quick check that PATH specifically |
| 116 | +// is inherited from parent env. |
| 117 | +func TestBuildRunnerEnv_PathInherited(t *testing.T) { |
| 118 | + job := &Job{Function: Function{}} |
| 119 | + env, err := buildRunnerEnv(job, nil) |
| 120 | + if err != nil { |
| 121 | + t.Fatalf("unexpected error: %v", err) |
| 122 | + } |
| 123 | + |
| 124 | + pathPresent := false |
| 125 | + for _, e := range env { |
| 126 | + if strings.HasPrefix(e, "PATH=") { |
| 127 | + pathPresent = true |
| 128 | + break |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // PATH should always be set in a normal Unix environment |
| 133 | + if path := os.Getenv("PATH"); path != "" && !pathPresent { |
| 134 | + t.Error("expected PATH to be inherited from parent environment") |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +// TestBuildRunnerEnv_EmptyRunEnvs ensures no error when there are no |
| 139 | +// func.yaml envs configured. |
| 140 | +func TestBuildRunnerEnv_EmptyRunEnvs(t *testing.T) { |
| 141 | + job := &Job{Function: Function{}} |
| 142 | + env, err := buildRunnerEnv(job, map[string]string{"PORT": "8080"}) |
| 143 | + if err != nil { |
| 144 | + t.Fatalf("unexpected error: %v", err) |
| 145 | + } |
| 146 | + |
| 147 | + if !slices.Contains(env, "PORT=8080") { |
| 148 | + t.Error("expected PORT=8080 in result") |
| 149 | + } |
| 150 | +} |
0 commit comments