Repository navigation
Expand file tree
/
Copy pathcheck_test.go
More file actions
124 lines (110 loc) · 3.12 KB
/
Copy pathcheck_test.go
File metadata and controls
124 lines (110 loc) · 3.12 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
package main
import (
"bufio"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/mackerelio/checkers"
)
func getCmdOpt(t *testing.T, command string, args []string) (Opt, *os.File) {
workdir := t.TempDir()
filename := filepath.Join(workdir, "test.txt")
file, err := os.Create(filename)
if err != nil {
t.Fatalf("Failed to create file: %v", err)
}
return Opt{
Command: command,
Args: args,
Identifier: "test",
Workdir: workdir,
}, file
}
// Test cmd
func TestCmd(t *testing.T) {
opt, file := getCmdOpt(t, "echo", []string{"Hello, World!"})
defer file.Close()
if err := opt.cmd(file); err != nil {
t.Fatalf("cmd failed: %v", err)
}
_, err := file.Seek(0, 0) // Reset file pointer to the beginning
if err != nil {
t.Fatalf("Failed to seek file: %v", err)
}
scanner := bufio.NewScanner(file)
if scanner.Scan() {
output := scanner.Text()
expected := "Hello, World!"
if output != expected {
t.Errorf("Expected output %q, got %q", expected, output)
}
} else {
t.Errorf("No output from command")
}
}
func TestCmdFailedCase(t *testing.T) {
opt, file := getCmdOpt(t, "nonexistent_command", []string{})
defer file.Close()
if err := opt.cmd(file); err == nil {
t.Fatalf("Expected cmd to fail, but it succeeded")
}
}
func TestCmdFailedCommand(t *testing.T) {
opt, file := getCmdOpt(t, "ls", []string{"nonexistent_directory"})
defer file.Close()
if err := opt.cmd(file); err == nil {
t.Fatalf("Expected cmd to fail, but it succeeded")
}
}
func createTempFile(t *testing.T, content string) (*os.File, func()) {
tmpfile, err := os.CreateTemp("", "testfile")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
if _, err := tmpfile.WriteString(content); err != nil {
t.Fatalf("Failed to write to temp file: %v", err)
}
if err := tmpfile.Close(); err != nil {
t.Fatalf("Failed to close temp file: %v", err)
}
return tmpfile, func() {
_ = os.Remove(tmpfile.Name())
}
}
func TestCheck(t *testing.T) {
opt, file := getCmdOpt(t, "echo", []string{"Hello, World!"})
defer file.Close()
ckr := opt.check()
if ckr.Status != checkers.OK {
t.Errorf("Expected OK status, got %v", ckr.Status)
}
if !strings.Contains(ckr.Message, "first time execution command") {
t.Errorf("Expected first time execution message, got %q", ckr.Message)
}
// Check again to check for no difference
ckr = opt.check()
if ckr.Status != checkers.OK {
t.Errorf("Expected OK status, got %v", ckr.Status)
}
if !strings.Contains(ckr.Message, "no difference") {
t.Errorf("Expected no difference message, got %q", ckr.Message)
}
}
func TestCheckWithDifference(t *testing.T) {
opt, file := getCmdOpt(t, "date", []string{})
defer file.Close()
// First check to create the initial state
opt.check()
// Change the command to produce a different output
time.Sleep(2 * time.Second) // Ensure the date command produces a different output
opt.Command = "date"
ckr := opt.check()
if ckr.Status != checkers.CRITICAL {
t.Errorf("Expected CRITICAL status, got %v", ckr.Status)
}
if !strings.Contains(ckr.Message, "found difference") {
t.Errorf("Expected found difference message, got %q", ckr.Message)
}
}