-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support for multiple paths, more tests
- Loading branch information
Showing
7 changed files
with
253 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestLoadConfig(t *testing.T) { | ||
yamlContent := ` | ||
logs: | ||
- path: "/path/to/log/*.log" | ||
type: delete | ||
- path: | ||
- "/path/to/log1/*.log" | ||
- "/path/to/log2/*.log" | ||
type: rotate | ||
condition: | ||
size: "100MB" | ||
schedule: "*/5 * * * *" | ||
` | ||
|
||
tempFile := filepath.Join(t.TempDir(), "config.yaml") | ||
if err := os.WriteFile(tempFile, []byte(yamlContent), 0644); err != nil { | ||
t.Fatalf("Failed to create temp config file: %v", err) | ||
} | ||
|
||
config, err := loadConfig(tempFile) | ||
if err != nil { | ||
t.Fatalf("Failed to load config: %v", err) | ||
} | ||
|
||
// Verify loaded config values | ||
if len(config.Logs) != 2 { | ||
t.Errorf("Expected 2 log entries, got %d", len(config.Logs)) | ||
} | ||
|
||
if config.Logs[0].Type != "delete" { | ||
t.Errorf("Expected type 'delete', got %s", config.Logs[0].Type) | ||
} | ||
|
||
if len(config.Logs[1].Path) != 2 { | ||
t.Errorf("Expected 2 paths, got %d", len(config.Logs[1].Path)) | ||
} | ||
|
||
if config.Schedule != "*/5 * * * *" { | ||
t.Errorf("Expected schedule '*/5 * * * *', got %s", config.Schedule) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
schedule: "0 0 * * *" | ||
|
||
logs: | ||
- path: "/tmp/test/logs/delete/*.log" | ||
type: delete | ||
condition: | ||
age: "30m" | ||
|
||
- path: "/tmp/test/logs/compress/*.log" | ||
type: rotate | ||
condition: | ||
max_keep: 5 | ||
size: "30MB" | ||
compress: true | ||
|
||
- path: | ||
- "/path1/*.log" | ||
- "/path2/*.log" | ||
type: delete |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestRotateLogFiles(t *testing.T) { | ||
tempDir := t.TempDir() | ||
|
||
// Create test log files | ||
file1 := filepath.Join(tempDir, "log1.log") | ||
file2 := filepath.Join(tempDir, "log2.log") | ||
_ = os.WriteFile(file1, make([]byte, 1024*1024*5), 0644) // 5MB log file | ||
_ = os.WriteFile(file2, make([]byte, 1024*1024*10), 0644) // 10MB log file | ||
|
||
logEntry := LogEntry{ | ||
Path: Paths{filepath.Join(tempDir, "*.log")}, | ||
Type: "rotate", | ||
Condition: &Condition{ | ||
Size: stringPtr("1MB"), // Should trigger rotation | ||
Compress: boolPtr(true), | ||
MaxKeep: intPtr(1), | ||
}, | ||
} | ||
|
||
rotateLogFiles(logEntry) | ||
|
||
compressedFiles, _ := filepath.Glob(filepath.Join(tempDir, "*.gz")) | ||
if len(compressedFiles) == 0 { | ||
t.Errorf("Expected compressed log files, but found none.") | ||
} | ||
|
||
remainingFiles, _ := filepath.Glob(filepath.Join(tempDir, "*.log")) | ||
if len(remainingFiles) > 1 { | ||
t.Errorf("Expected at most 1 log file, but found %d", len(remainingFiles)) | ||
} | ||
} | ||
|
||
// Helper functions for creating pointers to primitives | ||
func stringPtr(s string) *string { | ||
return &s | ||
} | ||
|
||
func boolPtr(b bool) *bool { | ||
return &b | ||
} | ||
|
||
func intPtr(i int) *int { | ||
return &i | ||
} | ||
|
||
func TestCreateTask(t *testing.T) { | ||
logEntry := LogEntry{ | ||
Path: Paths{"/tmp/test/logs/delete/*.log"}, | ||
Type: "delete", | ||
Condition: &Condition{ | ||
Age: stringPtr("1h"), | ||
}, | ||
} | ||
|
||
task := createTask(logEntry) | ||
|
||
// Mock logging | ||
logBuf := new(bytes.Buffer) | ||
log.SetOutput(logBuf) | ||
|
||
// Run the task | ||
task() | ||
|
||
if !strings.Contains(logBuf.String(), "Running task for path: /tmp/test/logs/delete/*.log") { | ||
t.Errorf("Expected log output for running task, got %s", logBuf.String()) | ||
} | ||
} |