Skip to content

Commit 889c7b7

Browse files
committed
Sync - Discover should properly pass env to the exec command
1 parent 7a08977 commit 889c7b7

File tree

3 files changed

+159
-1
lines changed

3 files changed

+159
-1
lines changed

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"github.com/spf13/viper"
2525
)
2626

27-
var Version string = "31.5"
27+
var Version string = "31.6"
2828

2929
var cfgFile string
3030
var userAgent string

lib/crontab.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"runtime"
1515
"strconv"
1616
"strings"
17+
18+
"github.com/spf13/viper"
1719
)
1820

1921
const DROP_IN_DIRECTORY = "/etc/cron.d"
@@ -490,6 +492,13 @@ func (l Line) Write() string {
490492

491493
if code := l.GetCode(); code != "" {
492494
lineParts = append(lineParts, "cronitor")
495+
496+
// Add the --env flag if environment is set
497+
if env := viper.GetString("CRONITOR_ENV"); env != "" {
498+
lineParts = append(lineParts, "--env")
499+
lineParts = append(lineParts, env)
500+
}
501+
493502
if l.Mon.NoStdoutPassthru {
494503
lineParts = append(lineParts, "--no-stdout")
495504
}

lib/crontab_test.go

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"regexp"
55
"strings"
66
"testing"
7+
8+
"github.com/spf13/viper"
79
)
810

911
func TestCronitorIgnoreComment(t *testing.T) {
@@ -118,3 +120,150 @@ func TestCronitorIgnoreComment(t *testing.T) {
118120
t.Errorf("Expected Write() output to NOT contain '# cronitor: ignore' for second line, got: %s", secondLineOutput)
119121
}
120122
}
123+
124+
func TestLineWriteWithEnvFlag(t *testing.T) {
125+
// Save original viper value
126+
originalEnv := viper.GetString("CRONITOR_ENV")
127+
128+
// Test cases
129+
testCases := []struct {
130+
name string
131+
envValue string
132+
line Line
133+
shouldHaveEnv bool
134+
}{
135+
{
136+
name: "With environment set",
137+
envValue: "production",
138+
line: Line{
139+
IsJob: true,
140+
CronExpression: "0 * * * *",
141+
CommandToRun: "/path/to/script.sh",
142+
Code: "abc123",
143+
Mon: Monitor{Code: "abc123"},
144+
},
145+
shouldHaveEnv: true,
146+
},
147+
{
148+
name: "Without environment set",
149+
envValue: "",
150+
line: Line{
151+
IsJob: true,
152+
CronExpression: "0 * * * *",
153+
CommandToRun: "/path/to/script.sh",
154+
Code: "def456",
155+
Mon: Monitor{Code: "def456"},
156+
},
157+
shouldHaveEnv: false,
158+
},
159+
{
160+
name: "With staging environment",
161+
envValue: "staging",
162+
line: Line{
163+
IsJob: true,
164+
CronExpression: "*/5 * * * *",
165+
CommandToRun: "/usr/bin/backup.sh",
166+
Code: "xyz789",
167+
Mon: Monitor{Code: "xyz789"},
168+
},
169+
shouldHaveEnv: true,
170+
},
171+
}
172+
173+
for _, tc := range testCases {
174+
t.Run(tc.name, func(t *testing.T) {
175+
// Set the environment value
176+
viper.Set("CRONITOR_ENV", tc.envValue)
177+
178+
// Write the line
179+
output := tc.line.Write()
180+
181+
// Check if --env flag is present
182+
hasEnvFlag := strings.Contains(output, "--env")
183+
if tc.shouldHaveEnv && !hasEnvFlag {
184+
t.Errorf("Expected --env flag in output but not found. Output: %s", output)
185+
}
186+
if !tc.shouldHaveEnv && hasEnvFlag {
187+
t.Errorf("Did not expect --env flag in output but found it. Output: %s", output)
188+
}
189+
190+
// If env should be present, verify the value is included
191+
if tc.shouldHaveEnv {
192+
expectedEnvString := "--env " + tc.envValue
193+
if !strings.Contains(output, expectedEnvString) {
194+
t.Errorf("Expected '%s' in output but not found. Output: %s", expectedEnvString, output)
195+
}
196+
}
197+
198+
// Verify the cronitor exec structure is correct
199+
if strings.Contains(output, "cronitor") {
200+
// Check the order: cronitor [--env <value>] [--no-stdout] exec <code> <command>
201+
parts := strings.Fields(output)
202+
cronitorIndex := -1
203+
for i, part := range parts {
204+
if part == "cronitor" {
205+
cronitorIndex = i
206+
break
207+
}
208+
}
209+
210+
if cronitorIndex >= 0 {
211+
execIndex := -1
212+
for i := cronitorIndex + 1; i < len(parts); i++ {
213+
if parts[i] == "exec" {
214+
execIndex = i
215+
break
216+
}
217+
}
218+
219+
if execIndex < 0 {
220+
t.Errorf("'exec' not found after 'cronitor' in output: %s", output)
221+
}
222+
223+
// If env flag is present, it should come before 'exec'
224+
if tc.shouldHaveEnv {
225+
envFlagIndex := -1
226+
for i := cronitorIndex + 1; i < execIndex; i++ {
227+
if parts[i] == "--env" {
228+
envFlagIndex = i
229+
break
230+
}
231+
}
232+
if envFlagIndex < 0 {
233+
t.Errorf("--env flag should appear between 'cronitor' and 'exec'. Output: %s", output)
234+
}
235+
}
236+
}
237+
}
238+
})
239+
}
240+
241+
// Restore original viper value
242+
viper.Set("CRONITOR_ENV", originalEnv)
243+
}
244+
245+
func TestLineWriteWithNoStdoutAndEnv(t *testing.T) {
246+
// Save original viper value
247+
originalEnv := viper.GetString("CRONITOR_ENV")
248+
249+
// Set environment
250+
viper.Set("CRONITOR_ENV", "testing")
251+
252+
line := Line{
253+
IsJob: true,
254+
CronExpression: "0 0 * * *",
255+
CommandToRun: "/bin/daily-task",
256+
Code: "test123",
257+
Mon: Monitor{Code: "test123", NoStdoutPassthru: true},
258+
}
259+
260+
output := line.Write()
261+
262+
// Check that both flags are present and in correct order
263+
if !strings.Contains(output, "cronitor --env testing --no-stdout exec test123") {
264+
t.Errorf("Expected 'cronitor --env testing --no-stdout exec test123' in output but got: %s", output)
265+
}
266+
267+
// Restore original viper value
268+
viper.Set("CRONITOR_ENV", originalEnv)
269+
}

0 commit comments

Comments
 (0)