-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
195 lines (177 loc) · 4.81 KB
/
Copy pathmain_test.go
File metadata and controls
195 lines (177 loc) · 4.81 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
// Copyright 2026 HAProxy Technologies LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/haproxytech/gopherd/internal/yml"
)
func TestLoadConfigValid(t *testing.T) {
t.Parallel()
dir := t.TempDir()
cfgPath := filepath.Join(dir, "test.yml")
os.WriteFile(cfgPath, []byte(`
processes:
- name: app
command: "true"
`), 0o644)
cfg, err := yml.Load(cfgPath)
if err != nil {
t.Fatalf("Load: %v", err)
}
if len(cfg.Processes) != 1 {
t.Fatalf("expected 1 process, got %d", len(cfg.Processes))
}
if cfg.Processes[0].Name != "app" {
t.Errorf("process name = %q, want app", cfg.Processes[0].Name)
}
}
func TestLoadConfigNoProcesses(t *testing.T) {
t.Parallel()
dir := t.TempDir()
cfgPath := filepath.Join(dir, "empty.yml")
os.WriteFile(cfgPath, []byte("# empty\n"), 0o644)
_, err := yml.Load(cfgPath)
if err == nil {
t.Error("expected error for config with no processes")
}
}
func TestLoadConfigFileNotFound(t *testing.T) {
t.Parallel()
_, err := yml.Load("/nonexistent/path.yml")
if err == nil {
t.Error("expected error for missing file")
}
}
func TestLoadConfigFull(t *testing.T) {
t.Parallel()
dir := t.TempDir()
cfgPath := filepath.Join(dir, "full.yml")
os.WriteFile(cfgPath, []byte(`
prefix: "service"
control:
socket: /tmp/test.sock
processes:
- name: init-task
command: "true"
startup: oneshot
- name: app
command: myapp
args: ["--verbose"]
working-dir: /tmp
stop-signal: SIGUSR1
kill-delay: 30s
on-success: ignore
on-failure: restart
backoff-delay: 1s
backoff-factor: 1.5
backoff-limit: 60s
after: [init-task]
requires: [init-task]
ready-check: app-health
ready-timeout: 30s
environment:
FOO: bar
on-check-failure:
app-health: restart
checks:
app-health:
http:
url: http://localhost:8080/health
socket: /var/run/app.sock
period: 5s
timeout: 2s
threshold: 3
initial-delay: 10s
tcp-check:
tcp:
host: localhost
port: 5432
period: 10s
exec-check:
exec:
command: "true"
log-targets:
syslog:
type: syslog
location: udp://localhost:514
services: [app]
labels:
env: test
`), 0o644)
cfg, err := yml.Load(cfgPath)
if err != nil {
t.Fatalf("Load: %v", err)
}
if cfg.Prefix != "service" {
t.Errorf("expected Prefix=%q, got %q", "service", cfg.Prefix)
}
if cfg.Control.SocketPath != "/tmp/test.sock" {
t.Errorf("socket = %q", cfg.Control.SocketPath)
}
if len(cfg.Processes) != 2 {
t.Fatalf("expected 2 processes, got %d", len(cfg.Processes))
}
if cfg.Processes[0].Startup != "oneshot" {
t.Errorf("init.Startup = %q", cfg.Processes[0].Startup)
}
app := cfg.Processes[1]
if app.StopSignal != "SIGUSR1" {
t.Errorf("app.StopSignal = %q", app.StopSignal)
}
if app.ReadyCheck != "app-health" {
t.Errorf("app.ReadyCheck = %q", app.ReadyCheck)
}
if app.Environment["FOO"] != "bar" {
t.Errorf("env FOO = %q", app.Environment["FOO"])
}
if len(cfg.Checks) != 3 {
t.Fatalf("expected 3 checks, got %d", len(cfg.Checks))
}
if cfg.Checks["app-health"].HTTP.Socket != "/var/run/app.sock" {
t.Errorf("http socket = %q", cfg.Checks["app-health"].HTTP.Socket)
}
if cfg.Checks["app-health"].InitialDelay != "10s" {
t.Errorf("initial-delay = %q", cfg.Checks["app-health"].InitialDelay)
}
if len(cfg.LogTargets) != 1 {
t.Fatalf("expected 1 log target, got %d", len(cfg.LogTargets))
}
if cfg.LogTargets["syslog"].Labels["env"] != "test" {
t.Errorf("label env = %q", cfg.LogTargets["syslog"].Labels["env"])
}
}
// TestReadConfigFileRejectsOversized verifies that readConfigFile refuses a
// config file larger than the size cap. Without the cap, a misconfigured
// GOPHERD_CONFIG or a swapped-out config could drive PID 1 into an
// unbounded allocation.
func TestReadConfigFileRejectsOversized(t *testing.T) {
t.Parallel()
dir := t.TempDir()
path := filepath.Join(dir, "big.yml")
huge := make([]byte, maxConfigFileSize+1)
for i := range huge {
huge[i] = ' '
}
if err := os.WriteFile(path, huge, 0o644); err != nil {
t.Fatalf("write: %v", err)
}
if _, err := readConfigFile(path); err == nil {
t.Fatal("expected error for oversized config, got nil")
} else if !strings.Contains(err.Error(), "size cap") {
t.Errorf("error %q does not mention size cap", err.Error())
}
}